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
1efcb36626a0135012f8e82ca4dcc73876dc039f
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/group_theory/submonoid/basic.lean
cf82c99c0e3b7863728e9bf1695e0d811dad43f9
[ "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
22,091
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Kenny Lau, Johan Commelin, Mario Carneiro, Kevin Buzzard, Amelia Livingston, Yury Kudryashov -/ import algebra.hom.group -- Only needed for notation import algebra.group.units import group_theory.subsemigroup.basic /-! # Submonoids: definition and `complete_lattice` structure This file defines bundled multiplicative and additive submonoids. We also define a `complete_lattice` structure on `submonoid`s, define the closure of a set as the minimal submonoid that includes this set, and prove a few results about extending properties from a dense set (i.e. a set with `closure s = ⊤`) to the whole monoid, see `submonoid.dense_induction` and `monoid_hom.of_mclosure_eq_top_left`/`monoid_hom.of_mclosure_eq_top_right`. ## Main definitions * `submonoid M`: the type of bundled submonoids of a monoid `M`; the underlying set is given in the `carrier` field of the structure, and should be accessed through coercion as in `(S : set M)`. * `add_submonoid M` : the type of bundled submonoids of an additive monoid `M`. For each of the following definitions in the `submonoid` namespace, there is a corresponding definition in the `add_submonoid` namespace. * `submonoid.copy` : copy of a submonoid with `carrier` replaced by a set that is equal but possibly not definitionally equal to the carrier of the original `submonoid`. * `submonoid.closure` : monoid closure of a set, i.e., the least submonoid that includes the set. * `submonoid.gi` : `closure : set M → submonoid M` and coercion `coe : submonoid M → set M` form a `galois_insertion`; * `monoid_hom.eq_mlocus`: the submonoid of elements `x : M` such that `f x = g x`; * `monoid_hom.of_mclosure_eq_top_right`: if a map `f : M → N` between two monoids satisfies `f 1 = 1` and `f (x * y) = f x * f y` for `y` from some dense set `s`, then `f` is a monoid homomorphism. E.g., if `f : ℕ → M` satisfies `f 0 = 0` and `f (x + 1) = f x + f 1`, then `f` is an additive monoid homomorphism. ## Implementation notes Submonoid inclusion is denoted `≤` rather than `⊆`, although `∈` is defined as membership of a submonoid's underlying set. Note that `submonoid M` does not actually require `monoid M`, instead requiring only the weaker `mul_one_class M`. This file is designed to have very few dependencies. In particular, it should not use natural numbers. `submonoid` is implemented by extending `subsemigroup` requiring `one_mem'`. ## Tags submonoid, submonoids -/ variables {M : Type*} {N : Type*} variables {A : Type*} section non_assoc variables [mul_one_class M] {s : set M} variables [add_zero_class A] {t : set A} /-- `one_mem_class S M` says `S` is a type of subsets `s ≤ M`, such that `1 ∈ s` for all `s`. -/ class one_mem_class (S : Type*) (M : out_param $ Type*) [has_one M] [set_like S M] := (one_mem : ∀ (s : S), (1 : M) ∈ s) export one_mem_class (one_mem) /-- `zero_mem_class S M` says `S` is a type of subsets `s ≤ M`, such that `0 ∈ s` for all `s`. -/ class zero_mem_class (S : Type*) (M : out_param $ Type*) [has_zero M] [set_like S M] := (zero_mem : ∀ (s : S), (0 : M) ∈ s) export zero_mem_class (zero_mem) attribute [to_additive] one_mem_class section set_option old_structure_cmd true /-- A submonoid of a monoid `M` is a subset containing 1 and closed under multiplication. -/ @[ancestor subsemigroup] structure submonoid (M : Type*) [mul_one_class M] extends subsemigroup M := (one_mem' : (1 : M) ∈ carrier) end /-- A submonoid of a monoid `M` can be considered as a subsemigroup of that monoid. -/ add_decl_doc submonoid.to_subsemigroup /-- `submonoid_class S M` says `S` is a type of subsets `s ≤ M` that contain `1` and are closed under `(*)` -/ class submonoid_class (S : Type*) (M : out_param $ Type*) [mul_one_class M] [set_like S M] extends mul_mem_class S M := (one_mem : ∀ (s : S), (1 : M) ∈ s) section set_option old_structure_cmd true /-- An additive submonoid of an additive monoid `M` is a subset containing 0 and closed under addition. -/ @[ancestor add_subsemigroup] structure add_submonoid (M : Type*) [add_zero_class M] extends add_subsemigroup M := (zero_mem' : (0 : M) ∈ carrier) end /-- An additive submonoid of an additive monoid `M` can be considered as an additive subsemigroup of that additive monoid. -/ add_decl_doc add_submonoid.to_add_subsemigroup /-- `add_submonoid_class S M` says `S` is a type of subsets `s ≤ M` that contain `0` and are closed under `(+)` -/ class add_submonoid_class (S : Type*) (M : out_param $ Type*) [add_zero_class M] [set_like S M] extends add_mem_class S M := (zero_mem : ∀ (s : S), (0 : M) ∈ s) attribute [to_additive] submonoid submonoid_class @[to_additive, priority 100] -- See note [lower instance priority] instance submonoid_class.to_one_mem_class (S : Type*) (M : out_param $ Type*) [mul_one_class M] [set_like S M] [h : submonoid_class S M] : one_mem_class S M := { ..h } @[to_additive] lemma pow_mem {M} [monoid M] {A : Type*} [set_like A M] [submonoid_class A M] {S : A} {x : M} (hx : x ∈ S) : ∀ (n : ℕ), x ^ n ∈ S | 0 := by { rw pow_zero, exact one_mem_class.one_mem S } | (n + 1) := by { rw pow_succ, exact mul_mem_class.mul_mem hx (pow_mem n) } namespace submonoid @[to_additive] instance : set_like (submonoid M) M := { coe := submonoid.carrier, coe_injective' := λ p q h, by cases p; cases q; congr' } @[to_additive] instance : submonoid_class (submonoid M) M := { one_mem := submonoid.one_mem', mul_mem := submonoid.mul_mem' } /-- See Note [custom simps projection] -/ @[to_additive " See Note [custom simps projection]"] def simps.coe (S : submonoid M) : set M := S initialize_simps_projections submonoid (carrier → coe) initialize_simps_projections add_submonoid (carrier → coe) @[simp, to_additive] lemma mem_carrier {s : submonoid M} {x : M} : x ∈ s.carrier ↔ x ∈ s := iff.rfl @[simp, to_additive] lemma mem_mk {s : set M} {x : M} (h_one) (h_mul) : x ∈ mk s h_one h_mul ↔ x ∈ s := iff.rfl @[simp, to_additive] lemma coe_set_mk {s : set M} (h_one) (h_mul) : (mk s h_one h_mul : set M) = s := rfl @[simp, to_additive] lemma mk_le_mk {s t : set M} (h_one) (h_mul) (h_one') (h_mul') : mk s h_one h_mul ≤ mk t h_one' h_mul' ↔ s ⊆ t := iff.rfl /-- Two submonoids are equal if they have the same elements. -/ @[ext, to_additive "Two `add_submonoid`s are equal if they have the same elements."] theorem ext {S T : submonoid M} (h : ∀ x, x ∈ S ↔ x ∈ T) : S = T := set_like.ext h /-- Copy a submonoid replacing `carrier` with a set that is equal to it. -/ @[to_additive "Copy an additive submonoid replacing `carrier` with a set that is equal to it."] protected def copy (S : submonoid M) (s : set M) (hs : s = S) : submonoid M := { carrier := s, one_mem' := hs.symm ▸ S.one_mem', mul_mem' := λ _ _, hs.symm ▸ S.mul_mem' } variable {S : submonoid M} @[simp, to_additive] lemma coe_copy {s : set M} (hs : s = S) : (S.copy s hs : set M) = s := rfl @[to_additive] lemma copy_eq {s : set M} (hs : s = S) : S.copy s hs = S := set_like.coe_injective hs variable (S) /-- A submonoid contains the monoid's 1. -/ @[to_additive "An `add_submonoid` contains the monoid's 0."] protected theorem one_mem : (1 : M) ∈ S := one_mem S /-- A submonoid is closed under multiplication. -/ @[to_additive "An `add_submonoid` is closed under addition."] protected theorem mul_mem {x y : M} : x ∈ S → y ∈ S → x * y ∈ S := mul_mem /-- The submonoid `M` of the monoid `M`. -/ @[to_additive "The additive submonoid `M` of the `add_monoid M`."] instance : has_top (submonoid M) := ⟨{ carrier := set.univ, one_mem' := set.mem_univ 1, mul_mem' := λ _ _ _ _, set.mem_univ _ }⟩ /-- The trivial submonoid `{1}` of an monoid `M`. -/ @[to_additive "The trivial `add_submonoid` `{0}` of an `add_monoid` `M`."] instance : has_bot (submonoid M) := ⟨{ carrier := {1}, one_mem' := set.mem_singleton 1, mul_mem' := λ a b ha hb, by { simp only [set.mem_singleton_iff] at *, rw [ha, hb, mul_one] }}⟩ @[to_additive] instance : inhabited (submonoid M) := ⟨⊥⟩ @[simp, to_additive] lemma mem_bot {x : M} : x ∈ (⊥ : submonoid M) ↔ x = 1 := set.mem_singleton_iff @[simp, to_additive] lemma mem_top (x : M) : x ∈ (⊤ : submonoid M) := set.mem_univ x @[simp, to_additive] lemma coe_top : ((⊤ : submonoid M) : set M) = set.univ := rfl @[simp, to_additive] lemma coe_bot : ((⊥ : submonoid M) : set M) = {1} := rfl /-- The inf of two submonoids is their intersection. -/ @[to_additive "The inf of two `add_submonoid`s is their intersection."] instance : has_inf (submonoid M) := ⟨λ S₁ S₂, { carrier := S₁ ∩ S₂, one_mem' := ⟨S₁.one_mem, S₂.one_mem⟩, mul_mem' := λ _ _ ⟨hx, hx'⟩ ⟨hy, hy'⟩, ⟨S₁.mul_mem hx hy, S₂.mul_mem hx' hy'⟩ }⟩ @[simp, to_additive] lemma coe_inf (p p' : submonoid M) : ((p ⊓ p' : submonoid M) : set M) = p ∩ p' := rfl @[simp, to_additive] lemma mem_inf {p p' : submonoid M} {x : M} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[to_additive] instance : has_Inf (submonoid M) := ⟨λ s, { carrier := ⋂ t ∈ s, ↑t, one_mem' := set.mem_bInter $ λ i h, i.one_mem, mul_mem' := λ x y hx hy, set.mem_bInter $ λ i h, i.mul_mem (by apply set.mem_Inter₂.1 hx i h) (by apply set.mem_Inter₂.1 hy i h) }⟩ @[simp, norm_cast, to_additive] lemma coe_Inf (S : set (submonoid M)) : ((Inf S : submonoid M) : set M) = ⋂ s ∈ S, ↑s := rfl @[to_additive] lemma mem_Inf {S : set (submonoid M)} {x : M} : x ∈ Inf S ↔ ∀ p ∈ S, x ∈ p := set.mem_Inter₂ @[to_additive] lemma mem_infi {ι : Sort*} {S : ι → submonoid M} {x : M} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [infi, mem_Inf, set.forall_range_iff] @[simp, norm_cast, to_additive] lemma coe_infi {ι : Sort*} {S : ι → submonoid M} : (↑(⨅ i, S i) : set M) = ⋂ i, S i := by simp only [infi, coe_Inf, set.bInter_range] /-- Submonoids of a monoid form a complete lattice. -/ @[to_additive "The `add_submonoid`s of an `add_monoid` form a complete lattice."] instance : complete_lattice (submonoid M) := { le := (≤), lt := (<), bot := (⊥), bot_le := λ S x hx, (mem_bot.1 hx).symm ▸ S.one_mem, top := (⊤), le_top := λ S x hx, mem_top x, inf := (⊓), Inf := has_Inf.Inf, le_inf := λ a b c ha hb x hx, ⟨ha hx, hb hx⟩, inf_le_left := λ a b x, and.left, inf_le_right := λ a b x, and.right, .. complete_lattice_of_Inf (submonoid M) $ λ s, is_glb.of_image (λ S T, show (S : set M) ≤ T ↔ S ≤ T, from set_like.coe_subset_coe) is_glb_binfi } @[simp, to_additive] lemma subsingleton_iff : subsingleton (submonoid M) ↔ subsingleton M := ⟨ λ h, by exactI ⟨λ x y, have ∀ i : M, i = 1 := λ i, mem_bot.mp $ subsingleton.elim (⊤ : submonoid M) ⊥ ▸ mem_top i, (this x).trans (this y).symm⟩, λ h, by exactI ⟨λ x y, submonoid.ext $ λ i, subsingleton.elim 1 i ▸ by simp [submonoid.one_mem]⟩⟩ @[simp, to_additive] lemma nontrivial_iff : nontrivial (submonoid M) ↔ nontrivial M := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans subsingleton_iff).trans not_nontrivial_iff_subsingleton.symm) @[to_additive] instance [subsingleton M] : unique (submonoid M) := ⟨⟨⊥⟩, λ a, @subsingleton.elim _ (subsingleton_iff.mpr ‹_›) a _⟩ @[to_additive] instance [nontrivial M] : nontrivial (submonoid M) := nontrivial_iff.mpr ‹_› /-- The `submonoid` generated by a set. -/ @[to_additive "The `add_submonoid` generated by a set"] def closure (s : set M) : submonoid M := Inf {S | s ⊆ S} @[to_additive] lemma mem_closure {x : M} : x ∈ closure s ↔ ∀ S : submonoid M, s ⊆ S → x ∈ S := mem_Inf /-- The submonoid generated by a set includes the set. -/ @[simp, to_additive "The `add_submonoid` generated by a set includes the set."] lemma subset_closure : s ⊆ closure s := λ x hx, mem_closure.2 $ λ S hS, hS hx @[to_additive] lemma not_mem_of_not_mem_closure {P : M} (hP : P ∉ closure s) : P ∉ s := λ h, hP (subset_closure h) variable {S} open set /-- A submonoid `S` includes `closure s` if and only if it includes `s`. -/ @[simp, to_additive "An additive submonoid `S` includes `closure s` if and only if it includes `s`"] lemma closure_le : closure s ≤ S ↔ s ⊆ S := ⟨subset.trans subset_closure, λ h, Inf_le h⟩ /-- Submonoid closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`. -/ @[to_additive "Additive submonoid closure of a set is monotone in its argument: if `s ⊆ t`, then `closure s ≤ closure t`"] lemma closure_mono ⦃s t : set M⦄ (h : s ⊆ t) : closure s ≤ closure t := closure_le.2 $ subset.trans h subset_closure @[to_additive] lemma closure_eq_of_le (h₁ : s ⊆ S) (h₂ : S ≤ closure s) : closure s = S := le_antisymm (closure_le.2 h₁) h₂ variable (S) /-- An induction principle for closure membership. If `p` holds for `1` and all elements of `s`, and is preserved under multiplication, then `p` holds for all elements of the closure of `s`. -/ @[elab_as_eliminator, to_additive "An induction principle for additive closure membership. If `p` holds for `0` and all elements of `s`, and is preserved under addition, then `p` holds for all elements of the additive closure of `s`."] lemma closure_induction {p : M → Prop} {x} (h : x ∈ closure s) (Hs : ∀ x ∈ s, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := (@closure_le _ _ _ ⟨p, Hmul, H1⟩).2 Hs h /-- A dependent version of `submonoid.closure_induction`. -/ @[elab_as_eliminator, to_additive "A dependent version of `add_submonoid.closure_induction`. "] lemma closure_induction' (s : set M) {p : Π x, x ∈ closure s → Prop} (Hs : ∀ x (h : x ∈ s), p x (subset_closure h)) (H1 : p 1 (one_mem _)) (Hmul : ∀ x hx y hy, p x hx → p y hy → p (x * y) (mul_mem hx hy)) {x} (hx : x ∈ closure s) : p x hx := begin refine exists.elim _ (λ (hx : x ∈ closure s) (hc : p x hx), hc), exact closure_induction hx (λ x hx, ⟨_, Hs x hx⟩) ⟨_, H1⟩ (λ x y ⟨hx', hx⟩ ⟨hy', hy⟩, ⟨_, Hmul _ _ _ _ hx hy⟩), end /-- An induction principle for closure membership for predicates with two arguments. -/ @[elab_as_eliminator, to_additive "An induction principle for additive closure membership for predicates with two arguments."] lemma closure_induction₂ {p : M → M → Prop} {x} {y : M} (hx : x ∈ closure s) (hy : y ∈ closure s) (Hs : ∀ (x ∈ s) (y ∈ s), p x y) (H1_left : ∀ x, p 1 x) (H1_right : ∀ x, p x 1) (Hmul_left : ∀ x y z, p x z → p y z → p (x * y) z) (Hmul_right : ∀ x y z, p z x → p z y → p z (x * y)) : p x y := closure_induction hx (λ x xs, closure_induction hy (Hs x xs) (H1_right x) (λ z y h₁ h₂, Hmul_right z _ _ h₁ h₂)) (H1_left y) (λ x z h₁ h₂, Hmul_left _ _ _ h₁ h₂) /-- If `s` is a dense set in a monoid `M`, `submonoid.closure s = ⊤`, then in order to prove that some predicate `p` holds for all `x : M` it suffices to verify `p x` for `x ∈ s`, verify `p 1`, and verify that `p x` and `p y` imply `p (x * y)`. -/ @[elab_as_eliminator, to_additive "If `s` is a dense set in an additive monoid `M`, `add_submonoid.closure s = ⊤`, then in order to prove that some predicate `p` holds for all `x : M` it suffices to verify `p x` for `x ∈ s`, verify `p 0`, and verify that `p x` and `p y` imply `p (x + y)`."] lemma dense_induction {p : M → Prop} (x : M) {s : set M} (hs : closure s = ⊤) (Hs : ∀ x ∈ s, p x) (H1 : p 1) (Hmul : ∀ x y, p x → p y → p (x * y)) : p x := have ∀ x ∈ closure s, p x, from λ x hx, closure_induction hx Hs H1 Hmul, by simpa [hs] using this x variable (M) /-- `closure` forms a Galois insertion with the coercion to set. -/ @[to_additive "`closure` forms a Galois insertion with the coercion to set."] protected def gi : galois_insertion (@closure M _) coe := { choice := λ s _, closure s, gc := λ s t, closure_le, le_l_u := λ s, subset_closure, choice_eq := λ s h, rfl } variable {M} /-- Closure of a submonoid `S` equals `S`. -/ @[simp, to_additive "Additive closure of an additive submonoid `S` equals `S`"] lemma closure_eq : closure (S : set M) = S := (submonoid.gi M).l_u_eq S @[simp, to_additive] lemma closure_empty : closure (∅ : set M) = ⊥ := (submonoid.gi M).gc.l_bot @[simp, to_additive] lemma closure_univ : closure (univ : set M) = ⊤ := @coe_top M _ ▸ closure_eq ⊤ @[to_additive] lemma closure_union (s t : set M) : closure (s ∪ t) = closure s ⊔ closure t := (submonoid.gi M).gc.l_sup @[to_additive] lemma closure_Union {ι} (s : ι → set M) : closure (⋃ i, s i) = ⨆ i, closure (s i) := (submonoid.gi M).gc.l_supr @[simp, to_additive] lemma closure_singleton_le_iff_mem (m : M) (p : submonoid M) : closure {m} ≤ p ↔ m ∈ p := by rw [closure_le, singleton_subset_iff, set_like.mem_coe] @[to_additive] lemma mem_supr {ι : Sort*} (p : ι → submonoid M) {m : M} : (m ∈ ⨆ i, p i) ↔ (∀ N, (∀ i, p i ≤ N) → m ∈ N) := begin rw [← closure_singleton_le_iff_mem, le_supr_iff], simp only [closure_singleton_le_iff_mem], end @[to_additive] lemma supr_eq_closure {ι : Sort*} (p : ι → submonoid M) : (⨆ i, p i) = submonoid.closure (⋃ i, (p i : set M)) := by simp_rw [submonoid.closure_Union, submonoid.closure_eq] @[to_additive] lemma disjoint_def {p₁ p₂ : submonoid M} : disjoint p₁ p₂ ↔ ∀ {x : M}, x ∈ p₁ → x ∈ p₂ → x = 1 := by simp_rw [disjoint_iff_inf_le, set_like.le_def, mem_inf, and_imp, mem_bot] @[to_additive] lemma disjoint_def' {p₁ p₂ : submonoid M} : disjoint p₁ p₂ ↔ ∀ {x y : M}, x ∈ p₁ → y ∈ p₂ → x = y → x = 1 := disjoint_def.trans ⟨λ h x y hx hy hxy, h hx $ hxy.symm ▸ hy, λ h x hx hx', h hx hx' rfl⟩ end submonoid namespace monoid_hom variables [mul_one_class N] open submonoid /-- The submonoid of elements `x : M` such that `f x = g x` -/ @[to_additive "The additive submonoid of elements `x : M` such that `f x = g x`"] def eq_mlocus (f g : M →* N) : submonoid M := { carrier := {x | f x = g x}, one_mem' := by rw [set.mem_set_of_eq, f.map_one, g.map_one], mul_mem' := λ x y (hx : _ = _) (hy : _ = _), by simp [*] } /-- If two monoid homomorphisms are equal on a set, then they are equal on its submonoid closure. -/ @[to_additive "If two monoid homomorphisms are equal on a set, then they are equal on its submonoid closure."] lemma eq_on_mclosure {f g : M →* N} {s : set M} (h : set.eq_on f g s) : set.eq_on f g (closure s) := show closure s ≤ f.eq_mlocus g, from closure_le.2 h @[to_additive] lemma eq_of_eq_on_mtop {f g : M →* N} (h : set.eq_on f g (⊤ : submonoid M)) : f = g := ext $ λ x, h trivial @[to_additive] lemma eq_of_eq_on_mdense {s : set M} (hs : closure s = ⊤) {f g : M →* N} (h : s.eq_on f g) : f = g := eq_of_eq_on_mtop $ hs ▸ eq_on_mclosure h end monoid_hom end non_assoc section assoc variables [monoid M] [monoid N] {s : set M} section is_unit /-- The submonoid consisting of the units of a monoid -/ @[to_additive "The additive submonoid consisting of the additive units of an additive monoid"] def is_unit.submonoid (M : Type*) [monoid M] : submonoid M := { carrier := set_of is_unit, one_mem' := by simp only [is_unit_one, set.mem_set_of_eq], mul_mem' := by { intros a b ha hb, rw set.mem_set_of_eq at *, exact is_unit.mul ha hb } } @[to_additive] lemma is_unit.mem_submonoid_iff {M : Type*} [monoid M] (a : M) : a ∈ is_unit.submonoid M ↔ is_unit a := begin change a ∈ set_of is_unit ↔ is_unit a, rw set.mem_set_of_eq end end is_unit namespace monoid_hom open submonoid /-- Let `s` be a subset of a monoid `M` such that the closure of `s` is the whole monoid. Then `monoid_hom.of_mclosure_eq_top_left` defines a monoid homomorphism from `M` asking for a proof of `f (x * y) = f x * f y` only for `x ∈ s`. -/ @[to_additive "/-- Let `s` be a subset of an additive monoid `M` such that the closure of `s` is the whole monoid. Then `add_monoid_hom.of_mclosure_eq_top_left` defines an additive monoid homomorphism from `M` asking for a proof of `f (x + y) = f x + f y` only for `x ∈ s`. -/"] def of_mclosure_eq_top_left {M N} [monoid M] [monoid N] {s : set M} (f : M → N) (hs : closure s = ⊤) (h1 : f 1 = 1) (hmul : ∀ (x ∈ s) y, f (x * y) = f x * f y) : M →* N := { to_fun := f, map_one' := h1, map_mul' := λ x, dense_induction x hs hmul (λ y, by rw [one_mul, h1, one_mul]) $ λ a b ha hb y, by rw [mul_assoc, ha, ha, hb, mul_assoc] } @[simp, norm_cast, to_additive] lemma coe_of_mclosure_eq_top_left (f : M → N) (hs : closure s = ⊤) (h1 hmul) : ⇑(of_mclosure_eq_top_left f hs h1 hmul) = f := rfl /-- Let `s` be a subset of a monoid `M` such that the closure of `s` is the whole monoid. Then `monoid_hom.of_mclosure_eq_top_right` defines a monoid homomorphism from `M` asking for a proof of `f (x * y) = f x * f y` only for `y ∈ s`. -/ @[to_additive "/-- Let `s` be a subset of an additive monoid `M` such that the closure of `s` is the whole monoid. Then `add_monoid_hom.of_mclosure_eq_top_right` defines an additive monoid homomorphism from `M` asking for a proof of `f (x + y) = f x + f y` only for `y ∈ s`. -/"] def of_mclosure_eq_top_right {M N} [monoid M] [monoid N] {s : set M} (f : M → N) (hs : closure s = ⊤) (h1 : f 1 = 1) (hmul : ∀ x (y ∈ s), f (x * y) = f x * f y) : M →* N := { to_fun := f, map_one' := h1, map_mul' := λ x y, dense_induction y hs (λ y hy x, hmul x y hy) (by simp [h1]) (λ y₁ y₂ h₁ h₂ x, by simp only [← mul_assoc, h₁, h₂]) x } @[simp, norm_cast, to_additive] lemma coe_of_mclosure_eq_top_right (f : M → N) (hs : closure s = ⊤) (h1 hmul) : ⇑(of_mclosure_eq_top_right f hs h1 hmul) = f := rfl end monoid_hom end assoc
d6b99306ff00cd141a658c31f9922d0f76acd486
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/CoreM.lean
4f77d46c5e93f46416e0efb492e4a24a168d5057
[ "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
6,269
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Util.RecDepth import Lean.Util.Trace import Lean.Data.Options import Lean.Environment import Lean.Exception import Lean.InternalExceptionId import Lean.Eval import Lean.MonadEnv import Lean.ResolveName namespace Lean namespace Core register_builtin_option maxHeartbeats : Nat := { defValue := 50000 descr := "maximum amount of heartbeats per command. A heartbeat is number of (small) memory allocations (in thousands), 0 means no limit" } def getMaxHeartbeats (opts : Options) : Nat := maxHeartbeats.get opts * 1000 structure State where env : Environment nextMacroScope : MacroScope := firstFrontendMacroScope + 1 ngen : NameGenerator := {} traceState : TraceState := {} deriving Inhabited structure Context where options : Options := {} currRecDepth : Nat := 0 maxRecDepth : Nat := 1000 ref : Syntax := Syntax.missing currNamespace : Name := Name.anonymous openDecls : List OpenDecl := [] initHeartbeats : Nat := 0 maxHeartbeats : Nat := getMaxHeartbeats options abbrev CoreM := ReaderT Context $ StateRefT State (EIO Exception) -- Make the compiler generate specialized `pure`/`bind` so we do not have to optimize through the -- whole monad stack at every use site. May eventually be covered by `deriving`. instance : Monad CoreM := let i := inferInstanceAs (Monad CoreM); { pure := i.pure, bind := i.bind } instance : Inhabited (CoreM α) where default := fun _ _ => throw arbitrary instance : MonadRef CoreM where getRef := return (← read).ref withRef ref x := withReader (fun ctx => { ctx with ref := ref }) x instance : MonadEnv CoreM where getEnv := return (← get).env modifyEnv f := modify fun s => { s with env := f s.env } instance : MonadOptions CoreM where getOptions := return (← read).options instance : MonadWithOptions CoreM where withOptions f x := withReader (fun ctx => { ctx with options := f ctx.options }) x instance : AddMessageContext CoreM where addMessageContext := addMessageContextPartial instance : MonadNameGenerator CoreM where getNGen := return (← get).ngen setNGen ngen := modify fun s => { s with ngen := ngen } instance : MonadRecDepth CoreM where withRecDepth d x := withReader (fun ctx => { ctx with currRecDepth := d }) x getRecDepth := return (← read).currRecDepth getMaxRecDepth := return (← read).maxRecDepth instance : MonadResolveName CoreM where getCurrNamespace := return (← read).currNamespace getOpenDecls := return (← read).openDecls @[inline] def liftIOCore (x : IO α) : CoreM α := do let ref ← getRef IO.toEIO (fun (err : IO.Error) => Exception.error ref (toString err)) x instance : MonadLift IO CoreM where monadLift := liftIOCore instance : MonadTrace CoreM where getTraceState := return (← get).traceState modifyTraceState f := modify fun s => { s with traceState := f s.traceState } /-- Restore backtrackable parts of the state. -/ def restore (b : State) : CoreM Unit := modify fun s => { s with env := b.env } private def mkFreshNameImp (n : Name) : CoreM Name := do let fresh ← modifyGet fun s => (s.nextMacroScope, { s with nextMacroScope := s.nextMacroScope + 1 }) return addMacroScope (← getEnv).mainModule n fresh def mkFreshUserName (n : Name) : CoreM Name := mkFreshNameImp n @[inline] def CoreM.run (x : CoreM α) (ctx : Context) (s : State) : EIO Exception (α × State) := (x ctx).run s @[inline] def CoreM.run' (x : CoreM α) (ctx : Context) (s : State) : EIO Exception α := Prod.fst <$> x.run ctx s @[inline] def CoreM.toIO (x : CoreM α) (ctx : Context) (s : State) : IO (α × State) := do match (← (x.run { ctx with initHeartbeats := (← IO.getNumHeartbeats) } s).toIO') with | Except.error (Exception.error _ msg) => do let e ← msg.toString; throw $ IO.userError e | Except.error (Exception.internal id _) => throw <| IO.userError <| "internal exception #" ++ toString id.idx | Except.ok a => return a instance [MetaEval α] : MetaEval (CoreM α) where eval env opts x _ := do let x : CoreM α := do try x finally printTraces let (a, s) ← x.toIO { maxRecDepth := maxRecDepth.get opts, options := opts } { env := env } MetaEval.eval s.env opts a (hideUnit := true) -- withIncRecDepth for a monad `m` such that `[MonadControlT CoreM n]` protected def withIncRecDepth [Monad m] [MonadControlT CoreM m] (x : m α) : m α := controlAt CoreM fun runInBase => withIncRecDepth (runInBase x) def checkMaxHeartbeatsCore (moduleName : String) (optionName : Name) (max : Nat) : CoreM Unit := do unless max == 0 do let numHeartbeats ← IO.getNumHeartbeats if numHeartbeats - (← read).initHeartbeats > max then throwError "(deterministic) timeout at '{moduleName}', maximum number of heartbeats ({max/1000}) has been reached (use 'set_option {optionName} <num>' to set the limit)" def checkMaxHeartbeats (moduleName : String) : CoreM Unit := do checkMaxHeartbeatsCore moduleName `maxHeartbeats (← read).maxHeartbeats private def withCurrHeartbeatsImp (x : CoreM α) : CoreM α := do let heartbeats ← IO.getNumHeartbeats withReader (fun ctx => { ctx with initHeartbeats := heartbeats }) x def withCurrHeartbeats [Monad m] [MonadControlT CoreM m] (x : m α) : m α := controlAt CoreM fun runInBase => withCurrHeartbeatsImp (runInBase x) end Core export Core (CoreM mkFreshUserName checkMaxHeartbeats withCurrHeartbeats) @[inline] def catchInternalId [Monad m] [MonadExcept Exception m] (id : InternalExceptionId) (x : m α) (h : Exception → m α) : m α := do try x catch ex => match ex with | Exception.error _ _ => throw ex | Exception.internal id' _ => if id == id' then h ex else throw ex @[inline] def catchInternalIds [Monad m] [MonadExcept Exception m] (ids : List InternalExceptionId) (x : m α) (h : Exception → m α) : m α := do try x catch ex => match ex with | Exception.error _ _ => throw ex | Exception.internal id _ => if ids.contains id then h ex else throw ex end Lean
13bd3471e6b11cf5fb6735b9bdbf269ff3b7c4b3
8b9f17008684d796c8022dab552e42f0cb6fb347
/library/algebra/ordered_ring.lean
dd9d5e91e69f1737a2110452b4ae1ae44364082f
[ "Apache-2.0" ]
permissive
chubbymaggie/lean
0d06ae25f9dd396306fb02190e89422ea94afd7b
d2c7b5c31928c98f545b16420d37842c43b4ae9a
refs/heads/master
1,611,313,622,901
1,430,266,839,000
1,430,267,083,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,947
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: algebra.ordered_ring Authors: Jeremy Avigad Here an "ordered_ring" is partially ordered ring, which is ordered with respect to both a weak order and an associated strict order. Our numeric structures (int, rat, and real) will be instances of "linear_ordered_comm_ring". This development is modeled after Isabelle's library. -/ import algebra.ordered_group algebra.ring open eq eq.ops namespace algebra variable {A : Type} definition absurd_a_lt_a {B : Type} {a : A} [s : strict_order A] (H : a < a) : B := absurd H (lt.irrefl a) structure ordered_semiring [class] (A : Type) extends has_mul A, has_zero A, has_lt A, -- TODO: remove hack for improving performance semiring A, ordered_cancel_comm_monoid A, zero_ne_one_class A := (mul_le_mul_of_nonneg_left: ∀a b c, le a b → le zero c → le (mul c a) (mul c b)) (mul_le_mul_of_nonneg_right: ∀a b c, le a b → le zero c → le (mul a c) (mul b c)) (mul_lt_mul_of_pos_left: ∀a b c, lt a b → lt zero c → lt (mul c a) (mul c b)) (mul_lt_mul_of_pos_right: ∀a b c, lt a b → lt zero c → lt (mul a c) (mul b c)) section variable [s : ordered_semiring A] variables (a b c d e : A) include s theorem mul_le_mul_of_nonneg_left {a b c : A} (Hab : a ≤ b) (Hc : 0 ≤ c) : c * a ≤ c * b := !ordered_semiring.mul_le_mul_of_nonneg_left Hab Hc theorem mul_le_mul_of_nonneg_right {a b c : A} (Hab : a ≤ b) (Hc : 0 ≤ c) : a * c ≤ b * c := !ordered_semiring.mul_le_mul_of_nonneg_right Hab Hc -- TODO: there are four variations, depending on which variables we assume to be nonneg theorem mul_le_mul {a b c d : A} (Hac : a ≤ c) (Hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : a * b ≤ c * d := calc a * b ≤ c * b : mul_le_mul_of_nonneg_right Hac nn_b ... ≤ c * d : mul_le_mul_of_nonneg_left Hbd nn_c theorem mul_nonneg {a b : A} (Ha : a ≥ 0) (Hb : b ≥ 0) : a * b ≥ 0 := begin have H : 0 * b ≤ a * b, from mul_le_mul_of_nonneg_right Ha Hb, rewrite zero_mul at H, exact H end theorem mul_nonpos_of_nonneg_of_nonpos {a b : A} (Ha : a ≥ 0) (Hb : b ≤ 0) : a * b ≤ 0 := begin have H : a * b ≤ a * 0, from mul_le_mul_of_nonneg_left Hb Ha, rewrite mul_zero at H, exact H end theorem mul_nonpos_of_nonpos_of_nonneg {a b : A} (Ha : a ≤ 0) (Hb : b ≥ 0) : a * b ≤ 0 := begin have H : a * b ≤ 0 * b, from mul_le_mul_of_nonneg_right Ha Hb, rewrite zero_mul at H, exact H end theorem mul_lt_mul_of_pos_left {a b c : A} (Hab : a < b) (Hc : 0 < c) : c * a < c * b := !ordered_semiring.mul_lt_mul_of_pos_left Hab Hc theorem mul_lt_mul_of_pos_right {a b c : A} (Hab : a < b) (Hc : 0 < c) : a * c < b * c := !ordered_semiring.mul_lt_mul_of_pos_right Hab Hc -- TODO: once again, there are variations theorem mul_lt_mul {a b c d : A} (Hac : a < c) (Hbd : b ≤ d) (pos_b : 0 < b) (nn_c : 0 ≤ c) : a * b < c * d := calc a * b < c * b : mul_lt_mul_of_pos_right Hac pos_b ... ≤ c * d : mul_le_mul_of_nonneg_left Hbd nn_c theorem mul_pos {a b : A} (Ha : a > 0) (Hb : b > 0) : a * b > 0 := begin have H : 0 * b < a * b, from mul_lt_mul_of_pos_right Ha Hb, rewrite zero_mul at H, exact H end theorem mul_neg_of_pos_of_neg {a b : A} (Ha : a > 0) (Hb : b < 0) : a * b < 0 := begin have H : a * b < a * 0, from mul_lt_mul_of_pos_left Hb Ha, rewrite mul_zero at H, exact H end theorem mul_neg_of_neg_of_pos {a b : A} (Ha : a < 0) (Hb : b > 0) : a * b < 0 := begin have H : a * b < 0 * b, from mul_lt_mul_of_pos_right Ha Hb, rewrite zero_mul at H, exact H end end structure linear_ordered_semiring [class] (A : Type) extends ordered_semiring A, linear_strong_order_pair A section variable [s : linear_ordered_semiring A] variables {a b c : A} include s theorem lt_of_mul_lt_mul_left (H : c * a < c * b) (Hc : c ≥ 0) : a < b := lt_of_not_le (assume H1 : b ≤ a, have H2 : c * b ≤ c * a, from mul_le_mul_of_nonneg_left H1 Hc, not_lt_of_le H2 H) theorem lt_of_mul_lt_mul_right (H : a * c < b * c) (Hc : c ≥ 0) : a < b := lt_of_not_le (assume H1 : b ≤ a, have H2 : b * c ≤ a * c, from mul_le_mul_of_nonneg_right H1 Hc, not_lt_of_le H2 H) theorem le_of_mul_le_mul_left (H : c * a ≤ c * b) (Hc : c > 0) : a ≤ b := le_of_not_lt (assume H1 : b < a, have H2 : c * b < c * a, from mul_lt_mul_of_pos_left H1 Hc, not_le_of_lt H2 H) theorem le_of_mul_le_mul_right (H : a * c ≤ b * c) (Hc : c > 0) : a ≤ b := le_of_not_lt (assume H1 : b < a, have H2 : b * c < a * c, from mul_lt_mul_of_pos_right H1 Hc, not_le_of_lt H2 H) theorem pos_of_mul_pos_left (H : 0 < a * b) (H1 : 0 ≤ a) : 0 < b := lt_of_not_le (assume H2 : b ≤ 0, have H3 : a * b ≤ 0, from mul_nonpos_of_nonneg_of_nonpos H1 H2, not_lt_of_le H3 H) theorem pos_of_mul_pos_right (H : 0 < a * b) (H1 : 0 ≤ b) : 0 < a := lt_of_not_le (assume H2 : a ≤ 0, have H3 : a * b ≤ 0, from mul_nonpos_of_nonpos_of_nonneg H2 H1, not_lt_of_le H3 H) end structure ordered_ring [class] (A : Type) extends ring A, ordered_comm_group A, zero_ne_one_class A := (mul_nonneg : ∀a b, le zero a → le zero b → le zero (mul a b)) (mul_pos : ∀a b, lt zero a → lt zero b → lt zero (mul a b)) theorem ordered_ring.mul_le_mul_of_nonneg_left [s : ordered_ring A] {a b c : A} (Hab : a ≤ b) (Hc : 0 ≤ c) : c * a ≤ c * b := have H1 : 0 ≤ b - a, from iff.elim_right !sub_nonneg_iff_le Hab, assert H2 : 0 ≤ c * (b - a), from ordered_ring.mul_nonneg _ _ Hc H1, begin rewrite mul_sub_left_distrib at H2, exact (iff.mp !sub_nonneg_iff_le H2) end theorem ordered_ring.mul_le_mul_of_nonneg_right [s : ordered_ring A] {a b c : A} (Hab : a ≤ b) (Hc : 0 ≤ c) : a * c ≤ b * c := have H1 : 0 ≤ b - a, from iff.elim_right !sub_nonneg_iff_le Hab, assert H2 : 0 ≤ (b - a) * c, from ordered_ring.mul_nonneg _ _ H1 Hc, begin rewrite mul_sub_right_distrib at H2, exact (iff.mp !sub_nonneg_iff_le H2) end theorem ordered_ring.mul_lt_mul_of_pos_left [s : ordered_ring A] {a b c : A} (Hab : a < b) (Hc : 0 < c) : c * a < c * b := have H1 : 0 < b - a, from iff.elim_right !sub_pos_iff_lt Hab, assert H2 : 0 < c * (b - a), from ordered_ring.mul_pos _ _ Hc H1, begin rewrite mul_sub_left_distrib at H2, exact (iff.mp !sub_pos_iff_lt H2) end theorem ordered_ring.mul_lt_mul_of_pos_right [s : ordered_ring A] {a b c : A} (Hab : a < b) (Hc : 0 < c) : a * c < b * c := have H1 : 0 < b - a, from iff.elim_right !sub_pos_iff_lt Hab, assert H2 : 0 < (b - a) * c, from ordered_ring.mul_pos _ _ H1 Hc, begin rewrite mul_sub_right_distrib at H2, exact (iff.mp !sub_pos_iff_lt H2) end definition ordered_ring.to_ordered_semiring [instance] [coercion] [reducible] [s : ordered_ring A] : ordered_semiring A := ⦃ ordered_semiring, s, mul_zero := mul_zero, zero_mul := zero_mul, add_left_cancel := @add.left_cancel A s, add_right_cancel := @add.right_cancel A s, le_of_add_le_add_left := @le_of_add_le_add_left A s, mul_le_mul_of_nonneg_left := @ordered_ring.mul_le_mul_of_nonneg_left A s, mul_le_mul_of_nonneg_right := @ordered_ring.mul_le_mul_of_nonneg_right A s, mul_lt_mul_of_pos_left := @ordered_ring.mul_lt_mul_of_pos_left A s, mul_lt_mul_of_pos_right := @ordered_ring.mul_lt_mul_of_pos_right A s ⦄ section variable [s : ordered_ring A] variables {a b c : A} include s theorem mul_le_mul_of_nonpos_left (H : b ≤ a) (Hc : c ≤ 0) : c * a ≤ c * b := have Hc' : -c ≥ 0, from iff.mp' !neg_nonneg_iff_nonpos Hc, assert H1 : -c * b ≤ -c * a, from mul_le_mul_of_nonneg_left H Hc', have H2 : -(c * b) ≤ -(c * a), begin rewrite [-*neg_mul_eq_neg_mul at H1], exact H1 end, iff.mp !neg_le_neg_iff_le H2 theorem mul_le_mul_of_nonpos_right (H : b ≤ a) (Hc : c ≤ 0) : a * c ≤ b * c := have Hc' : -c ≥ 0, from iff.mp' !neg_nonneg_iff_nonpos Hc, assert H1 : b * -c ≤ a * -c, from mul_le_mul_of_nonneg_right H Hc', have H2 : -(b * c) ≤ -(a * c), begin rewrite [-*neg_mul_eq_mul_neg at H1], exact H1 end, iff.mp !neg_le_neg_iff_le H2 theorem mul_nonneg_of_nonpos_of_nonpos (Ha : a ≤ 0) (Hb : b ≤ 0) : 0 ≤ a * b := begin have H : 0 * b ≤ a * b, from mul_le_mul_of_nonpos_right Ha Hb, rewrite zero_mul at H, exact H end theorem mul_lt_mul_of_neg_left (H : b < a) (Hc : c < 0) : c * a < c * b := have Hc' : -c > 0, from iff.mp' !neg_pos_iff_neg Hc, assert H1 : -c * b < -c * a, from mul_lt_mul_of_pos_left H Hc', have H2 : -(c * b) < -(c * a), begin rewrite [-*neg_mul_eq_neg_mul at H1], exact H1 end, iff.mp !neg_lt_neg_iff_lt H2 theorem mul_lt_mul_of_neg_right (H : b < a) (Hc : c < 0) : a * c < b * c := have Hc' : -c > 0, from iff.mp' !neg_pos_iff_neg Hc, assert H1 : b * -c < a * -c, from mul_lt_mul_of_pos_right H Hc', have H2 : -(b * c) < -(a * c), begin rewrite [-*neg_mul_eq_mul_neg at H1], exact H1 end, iff.mp !neg_lt_neg_iff_lt H2 theorem mul_pos_of_neg_of_neg (Ha : a < 0) (Hb : b < 0) : 0 < a * b := begin have H : 0 * b < a * b, from mul_lt_mul_of_neg_right Ha Hb, rewrite zero_mul at H, exact H end end -- TODO: we can eliminate mul_pos_of_pos, but now it is not worth the effort to redeclare the -- class instance structure linear_ordered_ring [class] (A : Type) extends ordered_ring A, linear_strong_order_pair A -- print fields linear_ordered_semiring definition linear_ordered_ring.to_linear_ordered_semiring [instance] [coercion] [reducible] [s : linear_ordered_ring A] : linear_ordered_semiring A := ⦃ linear_ordered_semiring, s, mul_zero := mul_zero, zero_mul := zero_mul, add_left_cancel := @add.left_cancel A s, add_right_cancel := @add.right_cancel A s, le_of_add_le_add_left := @le_of_add_le_add_left A s, mul_le_mul_of_nonneg_left := @mul_le_mul_of_nonneg_left A s, mul_le_mul_of_nonneg_right := @mul_le_mul_of_nonneg_right A s, mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left A s, mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right A s, le_total := linear_ordered_ring.le_total ⦄ structure linear_ordered_comm_ring [class] (A : Type) extends linear_ordered_ring A, comm_monoid A theorem linear_ordered_comm_ring.eq_zero_or_eq_zero_of_mul_eq_zero [s : linear_ordered_comm_ring A] {a b : A} (H : a * b = 0) : a = 0 ∨ b = 0 := lt.by_cases (assume Ha : 0 < a, lt.by_cases (assume Hb : 0 < b, begin have H1 : 0 < a * b, from mul_pos Ha Hb, rewrite H at H1, apply (absurd_a_lt_a H1) end) (assume Hb : 0 = b, or.inr (Hb⁻¹)) (assume Hb : 0 > b, begin have H1 : 0 > a * b, from mul_neg_of_pos_of_neg Ha Hb, rewrite H at H1, apply (absurd_a_lt_a H1) end)) (assume Ha : 0 = a, or.inl (Ha⁻¹)) (assume Ha : 0 > a, lt.by_cases (assume Hb : 0 < b, begin have H1 : 0 > a * b, from mul_neg_of_neg_of_pos Ha Hb, rewrite H at H1, apply (absurd_a_lt_a H1) end) (assume Hb : 0 = b, or.inr (Hb⁻¹)) (assume Hb : 0 > b, begin have H1 : 0 < a * b, from mul_pos_of_neg_of_neg Ha Hb, rewrite H at H1, apply (absurd_a_lt_a H1) end)) -- Linearity implies no zero divisors. Doesn't need commutativity. definition linear_ordered_comm_ring.to_integral_domain [instance] [coercion] [reducible] [s: linear_ordered_comm_ring A] : integral_domain A := ⦃ integral_domain, s, eq_zero_or_eq_zero_of_mul_eq_zero := @linear_ordered_comm_ring.eq_zero_or_eq_zero_of_mul_eq_zero A s ⦄ section variable [s : linear_ordered_ring A] variables (a b c : A) include s theorem mul_self_nonneg : a * a ≥ 0 := or.elim (le.total 0 a) (assume H : a ≥ 0, mul_nonneg H H) (assume H : a ≤ 0, mul_nonneg_of_nonpos_of_nonpos H H) theorem zero_le_one : 0 ≤ 1 := one_mul 1 ▸ mul_self_nonneg 1 theorem zero_lt_one : 0 < 1 := lt_of_le_of_ne zero_le_one zero_ne_one theorem pos_and_pos_or_neg_and_neg_of_mul_pos {a b : A} (Hab : a * b > 0) : (a > 0 ∧ b > 0) ∨ (a < 0 ∧ b < 0) := lt.by_cases (assume Ha : 0 < a, lt.by_cases (assume Hb : 0 < b, or.inl (and.intro Ha Hb)) (assume Hb : 0 = b, begin rewrite [-Hb at Hab, mul_zero at Hab], apply (absurd_a_lt_a Hab) end) (assume Hb : b < 0, absurd Hab (lt.asymm (mul_neg_of_pos_of_neg Ha Hb)))) (assume Ha : 0 = a, begin rewrite [-Ha at Hab, zero_mul at Hab], apply (absurd_a_lt_a Hab) end) (assume Ha : a < 0, lt.by_cases (assume Hb : 0 < b, absurd Hab (lt.asymm (mul_neg_of_neg_of_pos Ha Hb))) (assume Hb : 0 = b, begin rewrite [-Hb at Hab, mul_zero at Hab], apply (absurd_a_lt_a Hab) end) (assume Hb : b < 0, or.inr (and.intro Ha Hb))) theorem gt_of_mul_lt_mul_neg_left {a b c} (H : c * a < c * b) (Hc : c ≤ 0) : a > b := have nhc : -c ≥ 0, from neg_nonneg_of_nonpos Hc, have H2 : -(c * b) < -(c * a), from iff.mp' (neg_lt_neg_iff_lt _ _) H, have H3 : (-c) * b < (-c) * a, from calc (-c) * b = - (c * b) : neg_mul_eq_neg_mul ... < -(c * a) : H2 ... = (-c) * a : neg_mul_eq_neg_mul, lt_of_mul_lt_mul_left H3 nhc theorem zero_gt_neg_one : -1 < 0 := neg_zero ▸ (neg_lt_neg zero_lt_one) end /- TODO: Isabelle's library has all kinds of cancelation rules for the simplifier. Search on mult_le_cancel_right1 in Rings.thy. -/ structure decidable_linear_ordered_comm_ring [class] (A : Type) extends linear_ordered_comm_ring A, decidable_linear_ordered_comm_group A section variable [s : decidable_linear_ordered_comm_ring A] variables {a b c : A} include s definition sign (a : A) : A := lt.cases a 0 (-1) 0 1 theorem sign_of_neg (H : a < 0) : sign a = -1 := lt.cases_of_lt H theorem sign_zero : sign 0 = 0 := lt.cases_of_eq rfl theorem sign_of_pos (H : a > 0) : sign a = 1 := lt.cases_of_gt H theorem sign_one : sign 1 = 1 := sign_of_pos zero_lt_one theorem sign_neg_one : sign (-1) = -1 := sign_of_neg (neg_neg_of_pos zero_lt_one) theorem sign_sign (a : A) : sign (sign a) = sign a := lt.by_cases (assume H : a > 0, calc sign (sign a) = sign 1 : by rewrite (sign_of_pos H) ... = 1 : by rewrite sign_one ... = sign a : by rewrite (sign_of_pos H)) (assume H : 0 = a, calc sign (sign a) = sign (sign 0) : by rewrite H ... = sign 0 : by rewrite sign_zero at {1} ... = sign a : by rewrite -H) (assume H : a < 0, calc sign (sign a) = sign (-1) : by rewrite (sign_of_neg H) ... = -1 : by rewrite sign_neg_one ... = sign a : by rewrite (sign_of_neg H)) theorem pos_of_sign_eq_one (H : sign a = 1) : a > 0 := lt.by_cases (assume H1 : 0 < a, H1) (assume H1 : 0 = a, begin rewrite [-H1 at H, sign_zero at H], apply (absurd H zero_ne_one) end) (assume H1 : 0 > a, have H2 : -1 = 1, from (sign_of_neg H1)⁻¹ ⬝ H, absurd ((eq_zero_of_neg_eq H2)⁻¹) zero_ne_one) theorem eq_zero_of_sign_eq_zero (H : sign a = 0) : a = 0 := lt.by_cases (assume H1 : 0 < a, absurd (H⁻¹ ⬝ sign_of_pos H1) zero_ne_one) (assume H1 : 0 = a, H1⁻¹) (assume H1 : 0 > a, have H2 : 0 = -1, from H⁻¹ ⬝ sign_of_neg H1, have H3 : 1 = 0, from eq_neg_of_eq_neg H2 ⬝ neg_zero, absurd (H3⁻¹) zero_ne_one) theorem neg_of_sign_eq_neg_one (H : sign a = -1) : a < 0 := lt.by_cases (assume H1 : 0 < a, have H2 : -1 = 1, from H⁻¹ ⬝ (sign_of_pos H1), absurd ((eq_zero_of_neg_eq H2)⁻¹) zero_ne_one) (assume H1 : 0 = a, have H2 : 0 = -1, begin rewrite [-H1 at H, sign_zero at H], exact H end, have H3 : 1 = 0, from eq_neg_of_eq_neg H2 ⬝ neg_zero, absurd (H3⁻¹) zero_ne_one) (assume H1 : 0 > a, H1) theorem sign_neg (a : A) : sign (-a) = -(sign a) := lt.by_cases (assume H1 : 0 < a, calc sign (-a) = -1 : sign_of_neg (neg_neg_of_pos H1) ... = -(sign a) : by rewrite (sign_of_pos H1)) (assume H1 : 0 = a, calc sign (-a) = sign (-0) : by rewrite H1 ... = sign 0 : by rewrite neg_zero ... = 0 : by rewrite sign_zero ... = -0 : by rewrite neg_zero ... = -(sign 0) : by rewrite sign_zero ... = -(sign a) : by rewrite -H1) (assume H1 : 0 > a, calc sign (-a) = 1 : sign_of_pos (neg_pos_of_neg H1) ... = -(-1) : by rewrite neg_neg ... = -(sign a) : sign_of_neg H1) theorem sign_mul (a b : A) : sign (a * b) = sign a * sign b := lt.by_cases (assume z_lt_a : 0 < a, lt.by_cases (assume z_lt_b : 0 < b, by rewrite [sign_of_pos z_lt_a, sign_of_pos z_lt_b, sign_of_pos (mul_pos z_lt_a z_lt_b), one_mul]) (assume z_eq_b : 0 = b, by rewrite [-z_eq_b, mul_zero, *sign_zero, mul_zero]) (assume z_gt_b : 0 > b, by rewrite [sign_of_pos z_lt_a, sign_of_neg z_gt_b, sign_of_neg (mul_neg_of_pos_of_neg z_lt_a z_gt_b), one_mul])) (assume z_eq_a : 0 = a, by rewrite [-z_eq_a, zero_mul, *sign_zero, zero_mul]) (assume z_gt_a : 0 > a, lt.by_cases (assume z_lt_b : 0 < b, by rewrite [sign_of_neg z_gt_a, sign_of_pos z_lt_b, sign_of_neg (mul_neg_of_neg_of_pos z_gt_a z_lt_b), mul_one]) (assume z_eq_b : 0 = b, by rewrite [-z_eq_b, mul_zero, *sign_zero, mul_zero]) (assume z_gt_b : 0 > b, by rewrite [sign_of_neg z_gt_a, sign_of_neg z_gt_b, sign_of_pos (mul_pos_of_neg_of_neg z_gt_a z_gt_b), neg_mul_neg, one_mul])) theorem abs_eq_sign_mul (a : A) : abs a = sign a * a := lt.by_cases (assume H1 : 0 < a, calc abs a = a : abs_of_pos H1 ... = 1 * a : by rewrite one_mul ... = sign a * a : by rewrite (sign_of_pos H1)) (assume H1 : 0 = a, calc abs a = abs 0 : by rewrite H1 ... = 0 : by rewrite abs_zero ... = 0 * a : by rewrite zero_mul ... = sign 0 * a : by rewrite sign_zero ... = sign a * a : by rewrite H1) (assume H1 : a < 0, calc abs a = -a : abs_of_neg H1 ... = -1 * a : by rewrite neg_eq_neg_one_mul ... = sign a * a : by rewrite (sign_of_neg H1)) theorem eq_sign_mul_abs (a : A) : a = sign a * abs a := lt.by_cases (assume H1 : 0 < a, calc a = abs a : abs_of_pos H1 ... = 1 * abs a : by rewrite one_mul ... = sign a * abs a : by rewrite (sign_of_pos H1)) (assume H1 : 0 = a, calc a = 0 : H1⁻¹ ... = 0 * abs a : by rewrite zero_mul ... = sign 0 * abs a : by rewrite sign_zero ... = sign a * abs a : by rewrite H1) (assume H1 : a < 0, calc a = -(-a) : by rewrite neg_neg ... = -abs a : by rewrite (abs_of_neg H1) ... = -1 * abs a : by rewrite neg_eq_neg_one_mul ... = sign a * abs a : by rewrite (sign_of_neg H1)) theorem abs_dvd_iff_dvd (a b : A) : abs a ∣ b ↔ a ∣ b := abs.by_cases !iff.refl !neg_dvd_iff_dvd theorem dvd_abs_iff (a b : A) : a ∣ abs b ↔ a ∣ b := abs.by_cases !iff.refl !dvd_neg_iff_dvd theorem abs_mul (a b : A) : abs (a * b) = abs a * abs b := or.elim (le.total 0 a) (assume H1 : 0 ≤ a, or.elim (le.total 0 b) (assume H2 : 0 ≤ b, calc abs (a * b) = a * b : abs_of_nonneg (mul_nonneg H1 H2) ... = abs a * b : by rewrite (abs_of_nonneg H1) ... = abs a * abs b : by rewrite (abs_of_nonneg H2)) (assume H2 : b ≤ 0, calc abs (a * b) = -(a * b) : abs_of_nonpos (mul_nonpos_of_nonneg_of_nonpos H1 H2) ... = a * -b : by rewrite neg_mul_eq_mul_neg ... = abs a * -b : by rewrite (abs_of_nonneg H1) ... = abs a * abs b : by rewrite (abs_of_nonpos H2))) (assume H1 : a ≤ 0, or.elim (le.total 0 b) (assume H2 : 0 ≤ b, calc abs (a * b) = -(a * b) : abs_of_nonpos (mul_nonpos_of_nonpos_of_nonneg H1 H2) ... = -a * b : by rewrite neg_mul_eq_neg_mul ... = abs a * b : by rewrite (abs_of_nonpos H1) ... = abs a * abs b : by rewrite (abs_of_nonneg H2)) (assume H2 : b ≤ 0, calc abs (a * b) = a * b : abs_of_nonneg (mul_nonneg_of_nonpos_of_nonpos H1 H2) ... = -a * -b : by rewrite neg_mul_neg ... = abs a * -b : by rewrite (abs_of_nonpos H1) ... = abs a * abs b : by rewrite (abs_of_nonpos H2))) theorem abs_mul_self (a : A) : abs a * abs a = a * a := abs.by_cases rfl !neg_mul_neg end /- TODO: Multiplication and one, starting with mult_right_le_one_le. -/ end algebra
bbd18457e5105d75d5e3fcd464cac62de2eb4af3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/ring_theory/int/basic.lean
22faf207833029bd36b6779221e155cc0ebbe53c
[]
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
6,158
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker, Aaron Anderson -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.int.gcd import Mathlib.ring_theory.multiplicity import Mathlib.ring_theory.principal_ideal_domain import Mathlib.PostPort namespace Mathlib /-! # Divisibility over ℕ and ℤ This file collects results for the integers and natural numbers that use abstract algebra in their proofs or cases of ℕ and ℤ being examples of structures in abstract algebra. ## Main statements * `nat.prime_iff`: `nat.prime` coincides with the general definition of `prime` * `nat.irreducible_iff_prime`: a non-unit natural number is only divisible by `1` iff it is prime * `nat.factors_eq`: the multiset of elements of `nat.factors` is equal to the factors given by the `unique_factorization_monoid` instance * ℤ is a `normalization_monoid` * ℤ is a `gcd_monoid` ## Tags prime, irreducible, natural numbers, integers, normalization monoid, gcd monoid, greatest common divisor, prime factorization, prime factors, unique factorization, unique factors -/ theorem nat.prime_iff {p : ℕ} : nat.prime p ↔ prime p := sorry theorem nat.irreducible_iff_prime {p : ℕ} : irreducible p ↔ prime p := sorry namespace nat protected instance wf_dvd_monoid : wf_dvd_monoid ℕ := sorry protected instance unique_factorization_monoid : unique_factorization_monoid ℕ := unique_factorization_monoid.mk fun (_x : ℕ) => irreducible_iff_prime end nat namespace int protected instance normalization_monoid : normalization_monoid ℤ := normalization_monoid.mk (fun (a : ℤ) => ite (0 ≤ a) 1 (-1)) sorry sorry sorry theorem normalize_of_nonneg {z : ℤ} (h : 0 ≤ z) : coe_fn normalize z = z := sorry theorem normalize_of_neg {z : ℤ} (h : z < 0) : coe_fn normalize z = -z := sorry theorem normalize_coe_nat (n : ℕ) : coe_fn normalize ↑n = ↑n := normalize_of_nonneg (coe_nat_le_coe_nat_of_le (nat.zero_le n)) theorem coe_nat_abs_eq_normalize (z : ℤ) : ↑(nat_abs z) = coe_fn normalize z := sorry protected instance gcd_monoid : gcd_monoid ℤ := gcd_monoid.mk norm_unit sorry sorry sorry (fun (a b : ℤ) => ↑(gcd a b)) (fun (a b : ℤ) => ↑(lcm a b)) sorry sorry sorry sorry sorry sorry sorry theorem coe_gcd (i : ℤ) (j : ℤ) : ↑(gcd i j) = gcd i j := rfl theorem coe_lcm (i : ℤ) (j : ℤ) : ↑(lcm i j) = lcm i j := rfl theorem nat_abs_gcd (i : ℤ) (j : ℤ) : nat_abs (gcd i j) = gcd i j := rfl theorem nat_abs_lcm (i : ℤ) (j : ℤ) : nat_abs (lcm i j) = lcm i j := rfl theorem exists_unit_of_abs (a : ℤ) : ∃ (u : ℤ), ∃ (h : is_unit u), ↑(nat_abs a) = u * a := sorry theorem gcd_eq_one_iff_coprime {a : ℤ} {b : ℤ} : gcd a b = 1 ↔ is_coprime a b := sorry theorem sqr_of_gcd_eq_one {a : ℤ} {b : ℤ} {c : ℤ} (h : gcd a b = 1) (heq : a * b = c ^ bit0 1) : ∃ (a0 : ℤ), a = a0 ^ bit0 1 ∨ a = -a0 ^ bit0 1 := sorry theorem sqr_of_coprime {a : ℤ} {b : ℤ} {c : ℤ} (h : is_coprime a b) (heq : a * b = c ^ bit0 1) : ∃ (a0 : ℤ), a = a0 ^ bit0 1 ∨ a = -a0 ^ bit0 1 := sqr_of_gcd_eq_one (iff.mpr gcd_eq_one_iff_coprime h) heq end int theorem irreducible_iff_nat_prime (a : ℕ) : irreducible a ↔ nat.prime a := sorry theorem nat.prime_iff_prime {p : ℕ} : nat.prime p ↔ prime p := sorry theorem nat.prime_iff_prime_int {p : ℕ} : nat.prime p ↔ prime ↑p := sorry /-- Maps an associate class of integers consisting of `-n, n` to `n : ℕ` -/ def associates_int_equiv_nat : associates ℤ ≃ ℕ := equiv.mk (fun (z : associates ℤ) => int.nat_abs (associates.out z)) (fun (n : ℕ) => associates.mk ↑n) sorry sorry theorem int.prime.dvd_mul {m : ℤ} {n : ℤ} {p : ℕ} (hp : nat.prime p) (h : ↑p ∣ m * n) : p ∣ int.nat_abs m ∨ p ∣ int.nat_abs n := iff.mp (nat.prime.dvd_mul hp) (eq.mpr (id (Eq._oldrec (Eq.refl (p ∣ int.nat_abs m * int.nat_abs n)) (Eq.symm (int.nat_abs_mul m n)))) (iff.mp int.coe_nat_dvd_left h)) theorem int.prime.dvd_mul' {m : ℤ} {n : ℤ} {p : ℕ} (hp : nat.prime p) (h : ↑p ∣ m * n) : ↑p ∣ m ∨ ↑p ∣ n := eq.mpr (id (Eq._oldrec (Eq.refl (↑p ∣ m ∨ ↑p ∣ n)) (propext int.coe_nat_dvd_left))) (eq.mpr (id (Eq._oldrec (Eq.refl (p ∣ int.nat_abs m ∨ ↑p ∣ n)) (propext int.coe_nat_dvd_left))) (int.prime.dvd_mul hp h)) theorem int.prime.dvd_pow {n : ℤ} {k : ℕ} {p : ℕ} (hp : nat.prime p) (h : ↑p ∣ n ^ k) : p ∣ int.nat_abs n := nat.prime.dvd_of_dvd_pow hp (eq.mpr (id (Eq._oldrec (Eq.refl (p ∣ int.nat_abs n ^ k)) (Eq.symm (int.nat_abs_pow n k)))) (iff.mp int.coe_nat_dvd_left h)) theorem int.prime.dvd_pow' {n : ℤ} {k : ℕ} {p : ℕ} (hp : nat.prime p) (h : ↑p ∣ n ^ k) : ↑p ∣ n := eq.mpr (id (Eq._oldrec (Eq.refl (↑p ∣ n)) (propext int.coe_nat_dvd_left))) (int.prime.dvd_pow hp h) theorem prime_two_or_dvd_of_dvd_two_mul_pow_self_two {m : ℤ} {p : ℕ} (hp : nat.prime p) (h : ↑p ∣ bit0 1 * m ^ bit0 1) : p = bit0 1 ∨ p ∣ int.nat_abs m := sorry protected instance nat.unique_units : unique (units ℕ) := unique.mk { default := 1 } nat.units_eq_one theorem nat.factors_eq {n : ℕ} : unique_factorization_monoid.factors n = ↑(nat.factors n) := sorry theorem nat.factors_multiset_prod_of_irreducible {s : multiset ℕ} (h : ∀ (x : ℕ), x ∈ s → irreducible x) : unique_factorization_monoid.factors (multiset.prod s) = s := sorry namespace multiplicity theorem finite_int_iff_nat_abs_finite {a : ℤ} {b : ℤ} : finite a b ↔ finite (int.nat_abs a) (int.nat_abs b) := sorry theorem finite_int_iff {a : ℤ} {b : ℤ} : finite a b ↔ int.nat_abs a ≠ 1 ∧ b ≠ 0 := sorry protected instance decidable_nat : DecidableRel fun (a b : ℕ) => roption.dom (multiplicity a b) := fun (a b : ℕ) => decidable_of_iff (a ≠ 1 ∧ 0 < b) sorry protected instance decidable_int : DecidableRel fun (a b : ℤ) => roption.dom (multiplicity a b) := fun (a b : ℤ) => decidable_of_iff (int.nat_abs a ≠ 1 ∧ b ≠ 0) sorry
3b3bbeff260c96c842703ff2c12e76dcc3982aef
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/group_theory/p_group.lean
5b070e70c3d0aa5a12d33aa45d26b6442dfa5608
[ "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
10,187
lean
/- Copyright (c) 2018 . All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Thomas Browning -/ import group_theory.index import group_theory.group_action.conj_act import group_theory.perm.cycle_type import group_theory.quotient_group /-! # p-groups This file contains a proof that if `G` is a `p`-group acting on a finite set `α`, then the number of fixed points of the action is congruent mod `p` to the cardinality of `α`. It also contains proofs of some corollaries of this lemma about existence of fixed points. -/ open_locale big_operators open fintype mul_action variables (p : ℕ) (G : Type*) [group G] /-- A p-group is a group in which every element has prime power order -/ def is_p_group : Prop := ∀ g : G, ∃ k : ℕ, g ^ (p ^ k) = 1 variables {p} {G} namespace is_p_group lemma iff_order_of [hp : fact p.prime] : is_p_group p G ↔ ∀ g : G, ∃ k : ℕ, order_of g = p ^ k := forall_congr (λ g, ⟨λ ⟨k, hk⟩, exists_imp_exists (by exact λ j, Exists.snd) ((nat.dvd_prime_pow hp.out).mp (order_of_dvd_of_pow_eq_one hk)), exists_imp_exists (λ k hk, by rw [←hk, pow_order_of_eq_one])⟩) lemma of_card [fintype G] {n : ℕ} (hG : card G = p ^ n) : is_p_group p G := λ g, ⟨n, by rw [←hG, pow_card_eq_one]⟩ lemma of_bot : is_p_group p (⊥ : subgroup G) := of_card (subgroup.card_bot.trans (pow_zero p).symm) lemma iff_card [fact p.prime] [fintype G] : is_p_group p G ↔ ∃ n : ℕ, card G = p ^ n := begin have hG : 0 < card G := card_pos_iff.mpr has_one.nonempty, refine ⟨λ h, _, λ ⟨n, hn⟩, of_card hn⟩, suffices : ∀ q ∈ nat.factors (card G), q = p, { use (card G).factors.length, rw [←list.prod_repeat, ←list.eq_repeat_of_mem this, nat.prod_factors hG] }, intros q hq, obtain ⟨hq1, hq2⟩ := (nat.mem_factors hG).mp hq, haveI : fact q.prime := ⟨hq1⟩, obtain ⟨g, hg⟩ := equiv.perm.exists_prime_order_of_dvd_card q hq2, obtain ⟨k, hk⟩ := (iff_order_of.mp h) g, exact (hq1.pow_eq_iff.mp (hg.symm.trans hk).symm).1.symm, end section G_is_p_group variables (hG : is_p_group p G) include hG lemma of_injective {H : Type*} [group H] (ϕ : H →* G) (hϕ : function.injective ϕ) : is_p_group p H := begin simp_rw [is_p_group, ←hϕ.eq_iff, ϕ.map_pow, ϕ.map_one], exact λ h, hG (ϕ h), end lemma to_subgroup (H : subgroup G) : is_p_group p H := hG.of_injective H.subtype subtype.coe_injective lemma of_surjective {H : Type*} [group H] (ϕ : G →* H) (hϕ : function.surjective ϕ) : is_p_group p H := begin refine λ h, exists.elim (hϕ h) (λ g hg, exists_imp_exists (λ k hk, _) (hG g)), rw [←hg, ←ϕ.map_pow, hk, ϕ.map_one], end lemma to_quotient (H : subgroup G) [H.normal] : is_p_group p (quotient_group.quotient H) := hG.of_surjective (quotient_group.mk' H) quotient.surjective_quotient_mk' lemma of_equiv {H : Type*} [group H] (ϕ : G ≃* H) : is_p_group p H := hG.of_surjective ϕ.to_monoid_hom ϕ.surjective variables [hp : fact p.prime] include hp lemma index (H : subgroup G) [fintype (quotient_group.quotient H)] : ∃ n : ℕ, H.index = p ^ n := begin obtain ⟨n, hn⟩ := iff_card.mp (hG.to_quotient H.normal_core), obtain ⟨k, hk1, hk2⟩ := (nat.dvd_prime_pow hp.out).mp ((congr_arg _ (H.normal_core.index_eq_card.trans hn)).mp (subgroup.index_dvd_of_le H.normal_core_le)), exact ⟨k, hk2⟩, end variables {α : Type*} [mul_action G α] lemma card_orbit (a : α) [fintype (orbit G a)] : ∃ n : ℕ, card (orbit G a) = p ^ n := begin let ϕ := orbit_equiv_quotient_stabilizer G a, haveI := fintype.of_equiv (orbit G a) ϕ, rw [card_congr ϕ, ←subgroup.index_eq_card], exact hG.index (stabilizer G a), end variables (α) [fintype α] [fintype (fixed_points G α)] /-- If `G` is a `p`-group acting on a finite set `α`, then the number of fixed points of the action is congruent mod `p` to the cardinality of `α` -/ lemma card_modeq_card_fixed_points : card α ≡ card (fixed_points G α) [MOD p] := begin classical, calc card α = card (Σ y : quotient (orbit_rel G α), {x // quotient.mk' x = y}) : card_congr (equiv.sigma_preimage_equiv (@quotient.mk' _ (orbit_rel G α))).symm ... = ∑ a : quotient (orbit_rel G α), card {x // quotient.mk' x = a} : card_sigma _ ... ≡ ∑ a : fixed_points G α, 1 [MOD p] : _ ... = _ : by simp; refl, rw [←zmod.eq_iff_modeq_nat p, nat.cast_sum, nat.cast_sum], have key : ∀ x, card {y // (quotient.mk' y : quotient (orbit_rel G α)) = quotient.mk' x} = card (orbit G x) := λ x, by simp only [quotient.eq']; congr, refine eq.symm (finset.sum_bij_ne_zero (λ a _ _, quotient.mk' a.1) (λ _ _ _, finset.mem_univ _) (λ a₁ a₂ _ _ _ _ h, subtype.eq ((mem_fixed_points' α).mp a₂.2 a₁.1 (quotient.exact' h))) (λ b, quotient.induction_on' b (λ b _ hb, _)) (λ a ha _, by { rw [key, mem_fixed_points_iff_card_orbit_eq_one.mp a.2] })), obtain ⟨k, hk⟩ := hG.card_orbit b, have : k = 0 := nat.le_zero_iff.1 (nat.le_of_lt_succ (lt_of_not_ge (mt (pow_dvd_pow p) (by rwa [pow_one, ←hk, ←nat.modeq_zero_iff_dvd, ←zmod.eq_iff_modeq_nat, ←key])))), exact ⟨⟨b, mem_fixed_points_iff_card_orbit_eq_one.2 $ by rw [hk, this, pow_zero]⟩, finset.mem_univ _, (ne_of_eq_of_ne nat.cast_one one_ne_zero), rfl⟩, end /-- If a p-group acts on `α` and the cardinality of `α` is not a multiple of `p` then the action has a fixed point. -/ lemma nonempty_fixed_point_of_prime_not_dvd_card (hpα : ¬ p ∣ card α) : (fixed_points G α).nonempty := @set.nonempty_of_nonempty_subtype _ _ begin rw [←card_pos_iff, pos_iff_ne_zero], contrapose! hpα, rw [←nat.modeq_zero_iff_dvd, ←hpα], exact hG.card_modeq_card_fixed_points α, end /-- If a p-group acts on `α` and the cardinality of `α` is a multiple of `p`, and the action has one fixed point, then it has another fixed point. -/ lemma exists_fixed_point_of_prime_dvd_card_of_fixed_point (hpα : p ∣ card α) {a : α} (ha : a ∈ fixed_points G α) : ∃ b, b ∈ fixed_points G α ∧ a ≠ b := have hpf : p ∣ card (fixed_points G α) := nat.modeq_zero_iff_dvd.mp ((hG.card_modeq_card_fixed_points α).symm.trans hpα.modeq_zero_nat), have hα : 1 < card (fixed_points G α) := (fact.out p.prime).one_lt.trans_le (nat.le_of_dvd (card_pos_iff.2 ⟨⟨a, ha⟩⟩) hpf), let ⟨⟨b, hb⟩, hba⟩ := exists_ne_of_one_lt_card hα ⟨a, ha⟩ in ⟨b, hb, λ hab, hba (by simp_rw [hab])⟩ lemma center_nontrivial [nontrivial G] [fintype G] : nontrivial (subgroup.center G) := begin classical, have := (hG.of_equiv conj_act.to_conj_act).exists_fixed_point_of_prime_dvd_card_of_fixed_point G, rw conj_act.fixed_points_eq_center at this, obtain ⟨g, hg⟩ := this _ (subgroup.center G).one_mem, { exact ⟨⟨1, ⟨g, hg.1⟩, mt subtype.ext_iff.mp hg.2⟩⟩ }, { obtain ⟨n, hn⟩ := is_p_group.iff_card.mp hG, rw hn, apply dvd_pow_self, rintro rfl, exact (fintype.one_lt_card).ne' hn }, end lemma bot_lt_center [nontrivial G] [fintype G] : ⊥ < subgroup.center G := begin haveI := center_nontrivial hG, classical, exact bot_lt_iff_ne_bot.mpr ((subgroup.center G).one_lt_card_iff_ne_bot.mp fintype.one_lt_card), end end G_is_p_group lemma to_le {H K : subgroup G} (hK : is_p_group p K) (hHK : H ≤ K) : is_p_group p H := hK.of_injective (subgroup.inclusion hHK) (λ a b h, subtype.ext (show _, from subtype.ext_iff.mp h)) lemma to_inf_left {H K : subgroup G} (hH : is_p_group p H) : is_p_group p (H ⊓ K : subgroup G) := hH.to_le inf_le_left lemma to_inf_right {H K : subgroup G} (hK : is_p_group p K) : is_p_group p (H ⊓ K : subgroup G) := hK.to_le inf_le_right lemma map {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : G →* K) : is_p_group p (H.map ϕ) := begin rw [←H.subtype_range, monoid_hom.map_range], exact hH.of_surjective (ϕ.restrict H).range_restrict (ϕ.restrict H).range_restrict_surjective, end lemma comap_of_ker_is_p_group {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : K →* G) (hϕ : is_p_group p ϕ.ker) : is_p_group p (H.comap ϕ) := begin intro g, obtain ⟨j, hj⟩ := hH ⟨ϕ g.1, g.2⟩, rw [subtype.ext_iff, H.coe_pow, subtype.coe_mk, ←ϕ.map_pow] at hj, obtain ⟨k, hk⟩ := hϕ ⟨g.1 ^ p ^ j, hj⟩, rwa [subtype.ext_iff, ϕ.ker.coe_pow, subtype.coe_mk, ←pow_mul, ←pow_add] at hk, exact ⟨j + k, by rwa [subtype.ext_iff, (H.comap ϕ).coe_pow]⟩, end lemma comap_of_injective {H : subgroup G} (hH : is_p_group p H) {K : Type*} [group K] (ϕ : K →* G) (hϕ : function.injective ϕ) : is_p_group p (H.comap ϕ) := begin apply hH.comap_of_ker_is_p_group ϕ, rw ϕ.ker_eq_bot_iff.mpr hϕ, exact is_p_group.of_bot, end lemma to_sup_of_normal_right {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) [K.normal] : is_p_group p (H ⊔ K : subgroup G) := begin rw [←quotient_group.ker_mk K, ←subgroup.comap_map_eq], apply (hH.map (quotient_group.mk' K)).comap_of_ker_is_p_group, rwa quotient_group.ker_mk, end lemma to_sup_of_normal_left {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) [H.normal] : is_p_group p (H ⊔ K : subgroup G) := (congr_arg (λ H : subgroup G, is_p_group p H) sup_comm).mp (to_sup_of_normal_right hK hH) lemma to_sup_of_normal_right' {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) (hHK : H ≤ K.normalizer) : is_p_group p (H ⊔ K : subgroup G) := let hHK' := to_sup_of_normal_right (hH.of_equiv (subgroup.comap_subtype_equiv_of_le hHK).symm) (hK.of_equiv (subgroup.comap_subtype_equiv_of_le subgroup.le_normalizer).symm) in ((congr_arg (λ H : subgroup K.normalizer, is_p_group p H) (subgroup.sup_subgroup_of_eq hHK subgroup.le_normalizer)).mp hHK').of_equiv (subgroup.comap_subtype_equiv_of_le (sup_le hHK subgroup.le_normalizer)) lemma to_sup_of_normal_left' {H K : subgroup G} (hH : is_p_group p H) (hK : is_p_group p K) (hHK : K ≤ H.normalizer) : is_p_group p (H ⊔ K : subgroup G) := (congr_arg (λ H : subgroup G, is_p_group p H) sup_comm).mp (to_sup_of_normal_right' hK hH hHK) end is_p_group
07cec1719f8d82e0753f1eaec730dad502cb1209
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/pkg/frontend/Frontend/Main_with_eval.lean
37c7e32d7c855a8971107798ac77d37f021d7b79
[ "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
151
lean
import Frontend.Main #eval show IO _ from do let r ← main ["Frontend.Import1"] if r ≠ 0 then throw <| IO.userError "Messages were generated!"
4cd4ebf061e5c9edebd11db80fa69db9b711d0f7
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/meta2.lean
1aca9eba005aaa2e27646e4dcd7cbb3ccd2d8010
[ "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
18,266
lean
import Lean.Meta open Lean open Lean.Meta -- set_option trace.Meta true --set_option trace.Meta.isDefEq.step false -- set_option trace.Meta.isDefEq.delta false set_option trace.Meta.debug true def print (msg : MessageData) : MetaM Unit := do trace[Meta.debug] msg def checkM (x : MetaM Bool) : MetaM Unit := unless (← x) do throwError "check failed" def getAssignment (m : Expr) : MetaM Expr := do let v? ← getExprMVarAssignment? m.mvarId!; (match v? with | some v => pure v | none => throwError "metavariable is not assigned") def nat := mkConst `Nat def boolE := mkConst `Bool def succ := mkConst `Nat.succ def zero := mkConst `Nat.zero def add := mkConst `Nat.add def io := mkConst `IO def type := mkSort levelOne def boolFalse := mkConst `Bool.false def boolTrue := mkConst `Bool.true def tst1 : MetaM Unit := do print "----- tst1 -----"; let mvar <- mkFreshExprMVar nat; checkM $ isExprDefEq mvar (mkNatLit 10); checkM $ isExprDefEq mvar (mkNatLit 10); pure () #eval tst1 def tst2 : MetaM Unit := do print "----- tst2 -----"; let mvar <- mkFreshExprMVar nat; checkM $ isExprDefEq (mkApp succ mvar) (mkApp succ (mkNatLit 10)); checkM $ isExprDefEq mvar (mkNatLit 10); pure () #eval tst2 def tst3 : MetaM Unit := do print "----- tst3 -----"; let t := mkLambda `x BinderInfo.default nat $ mkBVar 0; let mvar ← mkFreshExprMVar (mkForall `x BinderInfo.default nat nat); lambdaTelescope t fun xs _ => do let x := xs[0]! checkM $ isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); pure (); let v ← getAssignment mvar; print v; pure () #eval tst3 def tst4 : MetaM Unit := do print "----- tst4 -----"; let t := mkLambda `x BinderInfo.default nat $ mkBVar 0; lambdaTelescope t fun xs _ => do let x := xs[0]! let mvar ← mkFreshExprMVar (mkForall `x BinderInfo.default nat nat); -- the following `isExprDefEq` fails because `x` is also in the context of `mvar` checkM $ not <$> isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); checkM $ approxDefEq $ isExprDefEq (mkApp mvar x) (mkAppN add #[x, mkAppN add #[mkNatLit 10, x]]); let v ← getAssignment mvar; print v; pure (); pure () #eval tst4 def mkAppC (c : Name) (xs : Array Expr) : MetaM Expr := do let r ← mkAppM c xs; check r; pure r def mkProd (a b : Expr) : MetaM Expr := mkAppC `Prod #[a, b] def mkPair (a b : Expr) : MetaM Expr := mkAppC `Prod.mk #[a, b] def mkFst (s : Expr) : MetaM Expr := mkAppC `Prod.fst #[s] def mkSnd (s : Expr) : MetaM Expr := mkAppC `Prod.snd #[s] def tst5 : MetaM Unit := do print "----- tst5 -----"; let p₁ ← mkPair (mkNatLit 1) (mkNatLit 2); let x ← mkFst p₁; print x; let v ← whnf x; print v; let v ← withTransparency TransparencyMode.reducible $ whnf x; print v; let x ← mkId x; print x; let prod ← mkProd nat nat; let m ← mkFreshExprMVar prod; let y ← mkFst m; checkM $ isExprDefEq y x; print y; let x ← mkProjection p₁ `fst; print x; let y ← mkProjection p₁ `snd; print y #eval tst5 def tst6 : MetaM Unit := do print "----- tst6 -----"; withLocalDeclD `x nat $ fun x => do let m ← mkFreshExprMVar nat; let t := mkAppN add #[m, mkNatLit 4]; let s := mkAppN add #[x, mkNatLit 3]; checkM $ not <$> isExprDefEq t s; let s := mkAppN add #[x, mkNatLit 6]; checkM $ isExprDefEq t s; let v ← getAssignment m; checkM $ pure $ v == (← mkAdd x (mkNatLit 2)) print v; let m ← mkFreshExprMVar nat; let t := mkAppN add #[m, mkNatLit 4]; let s := mkNatLit 3; checkM $ not <$> isExprDefEq t s; let s := mkNatLit 10; checkM $ isExprDefEq t s; let v ← getAssignment m; checkM $ pure $ v == mkNatLit 6; print v; pure () #eval tst6 def tst7 : MetaM Unit := do print "----- tst7 -----"; withLocalDeclD `x type $ fun x => do let m1 ← mkFreshExprMVar (← mkArrow type type); let m2 ← mkFreshExprMVar type; let t := mkApp io x; -- we need to use foApprox to solve `?m1 ?m2 =?= IO x` checkM $ not <$> isDefEq (mkApp m1 m2) t; checkM $ approxDefEq $ isDefEq (mkApp m1 m2) t; let v ← getAssignment m1; checkM $ pure $ v == io; let v ← getAssignment m2; checkM $ pure $ v == x; pure () #eval tst7 def tst9 : MetaM Unit := do print "----- tst9 -----"; let env ← getEnv; print (toString (← isReducible `Prod.fst)) print (toString (← isReducible `Add.add)) pure () #eval tst9 def tst10 : MetaM Unit := do print "----- tst10 -----"; let t ← withLocalDeclD `x nat $ fun x => do { let b := mkAppN add #[x, mkAppN add #[mkNatLit 2, mkNatLit 3]]; mkLambdaFVars #[x] b }; print t; let t ← reduce t; print t; pure () #eval tst10 def tst11 : MetaM Unit := do print "----- tst11 -----"; checkM $ isType nat; checkM $ isType (← mkArrow nat nat); checkM $ not <$> isType add; checkM $ not <$> isType (mkNatLit 1); withLocalDeclD `x nat fun x => do checkM $ not <$> isType x; checkM $ not <$> (mkLambdaFVars #[x] x >>= isType); checkM $ not <$> (mkLambdaFVars #[x] nat >>= isType); let t ← mkEq x (mkNatLit 0); let t ← mkForallFVars #[x] t (usedOnly := true); print t; checkM $ isType t; pure (); pure () #eval tst11 def tst12 : MetaM Unit := do print "----- tst12 -----"; withLocalDeclD `x nat $ fun x => do let t ← mkEqRefl x >>= mkLambdaFVars #[x]; print t; let type ← inferType t; print type; isProofQuick t >>= fun b => print (toString b); isProofQuick nat >>= fun b => print (toString b); isProofQuick type >>= fun b => print (toString b); pure (); pure () #eval tst12 def tst13 : MetaM Unit := do print "----- tst13 -----"; let m₁ ← mkFreshExprMVar (← mkArrow type type); let m₂ ← mkFreshExprMVar (mkApp m₁ nat); let t ← mkId m₂; print t; let r ← abstractMVars t; print r.expr; let (_, _, e) ← openAbstractMVarsResult r; print e; pure () def mkDecEq (type : Expr) : MetaM Expr := mkAppC `DecidableEq #[type] def mkStateM (σ : Expr) : MetaM Expr := mkAppC `StateM #[σ] def mkMonad (m : Expr) : MetaM Expr := mkAppC `Monad #[m] def mkMonadState (σ m : Expr) : MetaM Expr := mkAppC `MonadState #[σ, m] def mkAdd (a : Expr) : MetaM Expr := mkAppC `Add #[a] def mkToString (a : Expr) : MetaM Expr := mkAppC `ToString #[a] def tst14 : MetaM Unit := do print "----- tst14 -----"; let stateM ← mkStateM nat; print stateM; let monad ← mkMonad stateM; let globalInsts ← getGlobalInstancesIndex; let insts ← globalInsts.getUnify monad; print (insts.map (·.val)); pure () #eval tst14 def tst15 : MetaM Unit := do print "----- tst15 -----"; let inst ← _root_.mkAdd nat; let r ← synthInstance inst; print r; pure () #eval tst15 def tst16 : MetaM Unit := do print "----- tst16 -----"; let prod ← mkProd nat nat; let inst ← mkToString prod; print inst; let r ← synthInstance inst; print r; pure () #eval tst16 def tst17 : MetaM Unit := do print "----- tst17 -----"; let prod ← mkProd nat nat; let prod ← mkProd boolE prod; let inst ← mkToString prod; print inst; let r ← synthInstance inst; print r; pure () #eval tst17 def tst18 : MetaM Unit := do print "----- tst18 -----"; let decEqNat ← mkDecEq nat; let r ← synthInstance decEqNat; print r; pure () #eval tst18 def tst19 : MetaM Unit := do print "----- tst19 -----"; let stateM ← mkStateM nat; print stateM; let monad ← mkMonad stateM; print monad; let r ← synthInstance monad; print r; pure () #eval tst19 def tst20 : MetaM Unit := do print "----- tst20 -----"; let stateM ← mkStateM nat; print stateM; let monadState ← mkMonadState nat stateM; print monadState; let r ← synthInstance monadState; print r; pure () #eval tst20 def tst21 : MetaM Unit := do print "----- tst21 -----"; withLocalDeclD `x nat $ fun x => do withLocalDeclD `y nat $ fun y => do withLocalDeclD `z nat $ fun z => do let eq₁ ← mkEq x y; let eq₂ ← mkEq y z; withLocalDeclD `h₁ eq₁ $ fun h₁ => do withLocalDeclD `h₂ eq₂ $ fun h₂ => do let h ← mkEqTrans h₁ h₂; let h ← mkEqSymm h; let h ← mkCongrArg succ h; let h₂ ← mkEqRefl succ; let h ← mkCongr h₂ h; let t ← inferType h; check h; print h; print t; let h ← mkCongrFun h₂ x; let t ← inferType h; check h; print t; pure () #eval tst21 def tst22 : MetaM Unit := do print "----- tst22 -----"; withLocalDeclD `x nat $ fun x => do withLocalDeclD `y nat $ fun y => do let t ← mkAppC `Add.add #[x, y]; print t; let t ← mkAppC `Add.add #[y, x]; print t; let t ← mkAppC `ToString.toString #[x]; print t; pure () #eval tst22 def test1 : Nat := (fun x y => x + y) 0 1 def tst23 : MetaM Unit := do print "----- tst23 -----"; let cinfo ← getConstInfo `test1; let v := cinfo.value?.get!; print v; print v.headBeta #eval tst23 def tst26 : MetaM Unit := do print "----- tst26 -----"; let m1 ← mkFreshExprMVar (← mkArrow nat nat); let m2 ← mkFreshExprMVar nat; let m3 ← mkFreshExprMVar nat; checkM $ approxDefEq $ isDefEq (mkApp m1 m2) m3; checkM $ do { let b ← isExprMVarAssigned $ m1.mvarId!; pure (!b) }; checkM $ isExprMVarAssigned $ m3.mvarId!; pure () #eval tst26 section set_option trace.Meta.isDefEq true set_option trace.Meta.isDefEq.delta true set_option trace.Meta.isDefEq.assign true def tst27 : MetaM Unit := do print "----- tst27 -----"; let m ← mkFreshExprMVar nat; checkM $ isDefEq (mkNatLit 1) (mkApp (mkConst `Nat.succ) m); pure () #eval tst27 end def tst28 : MetaM Unit := do print "----- tst28 -----"; withLocalDeclD `x nat $ fun x => withLocalDeclD `y nat $ fun y => withLocalDeclD `z nat $ fun z => do let t1 ← mkAppM `Add.add #[x, y]; let t1 ← mkAppM `Add.add #[x, t1]; let t1 ← mkAppM `Add.add #[t1, t1]; let t2 ← mkAppM `Add.add #[z, y]; let t3 ← mkAppM `Eq #[t2, t1]; let t3 ← mkForallFVars #[z] t3; let m ← mkFreshExprMVar nat; let p ← mkAppM `Add.add #[x, m]; print t3; let r ← kabstract t3 p; print r; let p ← mkAppM `Add.add #[x, y]; let r ← kabstract t3 p; print r; pure () #eval tst28 def norm : Level → Level := @Lean.Level.normalize def tst29 : MetaM Unit := do print "----- tst29 -----"; let u := mkLevelParam `u; let v := mkLevelParam `v; let u1 := mkLevelSucc u; let m := mkLevelMax levelOne u1; print (norm m); checkM $ pure $ norm m == u1; let m := mkLevelMax u1 levelOne; print (norm m); checkM $ pure $ norm m == u1; let m := mkLevelMax (mkLevelMax levelOne (mkLevelSucc u1)) (mkLevelSucc levelOne); checkM $ pure $ norm m == mkLevelSucc u1; print m; print (norm m); let m := mkLevelMax (mkLevelMax (mkLevelSucc (mkLevelSucc u1)) (mkLevelSucc u1)) (mkLevelSucc levelOne); print m; print (norm m); checkM $ pure $ norm m == mkLevelSucc (mkLevelSucc u1); let m := mkLevelMax (mkLevelMax (mkLevelSucc v) (mkLevelSucc u1)) (mkLevelSucc levelOne); print m; print (norm m); pure () #eval tst29 def tst30 : MetaM Unit := do print "----- tst30 -----"; let m1 ← mkFreshExprMVar nat; let m2 ← mkFreshExprMVar (← mkArrow nat nat); withLocalDeclD `x nat $ fun x => do let t := mkApp succ $ mkApp m2 x; print t; checkM $ approxDefEq $ isDefEq m1 t; let r ← instantiateMVars m1; print r; let r ← instantiateMVars m2; print r; pure () #eval tst30 def tst31 : MetaM Unit := do print "----- tst31 -----"; let m ← mkFreshExprMVar nat; let t := mkLet `x nat zero m; print t; checkM $ isDefEq t m; pure () def tst32 : MetaM Unit := do print "----- tst32 -----"; withLocalDeclD `a nat $ fun a => do withLocalDeclD `b nat $ fun b => do let aeqb ← mkEq a b; withLocalDeclD `h2 aeqb $ fun h2 => do let t ← mkEq (mkApp2 add a a) a; print t; let motive := mkLambda `x BinderInfo.default nat (mkApp3 (mkConst `Eq [levelOne]) nat (mkApp2 add a (mkBVar 0)) a); withLocalDeclD `h1 t $ fun h1 => do let r ← mkEqNDRec motive h1 h2; print r; let rType ← inferType r >>= whnf; print rType; check r; pure () #eval tst32 def tst33 : MetaM Unit := do print "----- tst33 -----"; withLocalDeclD `a nat $ fun a => do withLocalDeclD `b nat $ fun b => do let aeqb ← mkEq a b; withLocalDeclD `h2 aeqb $ fun h2 => do let t ← mkEq (mkApp2 add a a) a; let motive := mkLambda `x BinderInfo.default nat $ mkLambda `h BinderInfo.default (mkApp3 (mkConst `Eq [levelOne]) nat a (mkBVar 0)) $ (mkApp3 (mkConst `Eq [levelOne]) nat (mkApp2 add a (mkBVar 1)) a); withLocalDeclD `h1 t $ fun h1 => do let r ← mkEqRec motive h1 h2; print r; let rType ← inferType r >>= whnf; print rType; check r; pure () #eval tst33 def tst34 : MetaM Unit := do print "----- tst34 -----"; let type := mkSort levelOne; withLocalDeclD `α type $ fun α => do let m ← mkFreshExprMVar type; let t ← mkLambdaFVars #[α] (← mkArrow m m); print t; pure () #eval tst34 def tst35 : MetaM Unit := do print "----- tst35 -----"; let type := mkSort levelOne; withLocalDeclD `α type $ fun α => do let m1 ← mkFreshExprMVar type; let m2 ← mkFreshExprMVar (← mkArrow nat type); let v := mkLambda `x BinderInfo.default nat m1; assignExprMVar m2.mvarId! v; let w := mkApp m2 zero; let t1 ← mkLambdaFVars #[α] (← mkArrow w w); print t1; let m3 ← mkFreshExprMVar type; let t2 ← mkLambdaFVars #[α] (← mkArrow (mkBVar 0) (mkBVar 1)); print t2; checkM $ isDefEq t1 t2; pure () #eval tst35 #check @Id def tst36 : MetaM Unit := do print "----- tst36 -----"; let type := mkSort levelOne; let m1 ← mkFreshExprMVar (← mkArrow type type); withLocalDeclD `α type $ fun α => do let m2 ← mkFreshExprMVar type; let t ← mkAppM `Id #[m2]; checkM $ approxDefEq $ isDefEq (mkApp m1 α) t; checkM $ approxDefEq $ isDefEq m1 (mkConst `Id [levelZero]); pure () #eval tst36 def tst37 : MetaM Unit := do print "----- tst37 -----"; let m1 ← mkFreshExprMVar (←mkArrow nat (←mkArrow type type)); let m2 ← mkFreshExprMVar (←mkArrow nat type); withLocalDeclD `v nat $ fun v => do let lhs := mkApp2 m1 v (mkApp m2 v); let rhs ← mkAppM `StateM #[nat, nat]; print lhs; print rhs; checkM $ approxDefEq $ isDefEq lhs rhs; pure () #eval tst37 def tst38 : MetaM Unit := do print "----- tst38 -----"; let m1 ← mkFreshExprMVar nat; withLocalDeclD `x nat $ fun x => do let m2 ← mkFreshExprMVar type; withLocalDeclD `y m2 $ fun y => do let m3 ← mkFreshExprMVar (←mkArrow m2 nat); let rhs := mkApp m3 y; checkM $ approxDefEq $ isDefEq m2 nat; print m2; checkM $ getAssignment m2 >>= fun v => pure $ v == nat; checkM $ approxDefEq $ isDefEq m1 rhs; print m2; checkM $ getAssignment m2 >>= fun v => pure $ v == nat; pure () set_option pp.all true set_option trace.Meta.isDefEq true set_option trace.Meta.isDefEq.delta true set_option trace.Meta.isDefEq.assign true #eval tst38 def tst39 : MetaM Unit := do print "----- tst39 -----"; withLocalDeclD `α type $ fun α => withLocalDeclD `β type $ fun β => do let p ← mkProd α β; let t ← mkForallFVars #[α, β] p; print t; let e ← instantiateForall t #[nat, boolE]; print e; pure () #eval tst39 def tst40 : MetaM Unit := do print "----- tst40 -----"; withLocalDeclD `α type $ fun α => withLocalDeclD `β type $ fun β => withLocalDeclD `a α $ fun a => withLocalDeclD `b β $ fun b => do let p ← mkProd α β; let t1 ← mkForallFVars #[α, β] p; let t2 ← mkForallFVars #[α, β, a, b] p; print t1; print $ toString $ t1.bindingBody!.hasLooseBVarInExplicitDomain 0 false; print $ toString $ t1.bindingBody!.hasLooseBVarInExplicitDomain 0 true; print $ toString $ t2.bindingBody!.hasLooseBVarInExplicitDomain 0 false; print $ t1.inferImplicit 2 false; checkM $ pure $ ((t1.inferImplicit 2 false).bindingInfo! == BinderInfo.default); checkM $ pure $ ((t1.inferImplicit 2 false).bindingBody!.bindingInfo! == BinderInfo.default); print $ t1.inferImplicit 2 true; checkM $ pure $ ((t1.inferImplicit 2 true).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t1.inferImplicit 2 true).bindingBody!.bindingInfo! == BinderInfo.implicit); print t2; print $ t2.inferImplicit 2 false; checkM $ pure $ ((t2.inferImplicit 2 false).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t2.inferImplicit 2 false).bindingBody!.bindingInfo! == BinderInfo.implicit); print $ t2.inferImplicit 1 false; checkM $ pure $ ((t2.inferImplicit 1 false).bindingInfo! == BinderInfo.implicit); checkM $ pure $ ((t2.inferImplicit 1 false).bindingBody!.bindingInfo! == BinderInfo.default); pure () #eval tst40 universe u structure A (α : Type u) := (x y : α) structure B (α : Type u) := (z : α) structure C (α : Type u) extends A α, B α := (w : Bool) def mkA (x y : Expr) : MetaM Expr := mkAppC `A.mk #[x, y] def mkB (z : Expr) : MetaM Expr := mkAppC `B.mk #[z] def mkC (x y z w : Expr) : MetaM Expr := do let a ← mkA x y; let b ← mkB z; mkAppC `C.mk #[a, b, w] def tst41 : MetaM Unit := do print "----- tst41 -----"; let c ← mkC (mkNatLit 1) (mkNatLit 2) (mkNatLit 3) boolTrue; print c; let x ← mkProjection c `x; check x; print x; let y ← mkProjection c `y; check y; print y; let z ← mkProjection c `z; check z; print z; let w ← mkProjection c `w; check w; print w; pure () set_option trace.Meta.isDefEq false set_option trace.Meta.isDefEq.delta false set_option trace.Meta.isDefEq.assign false #eval tst41 set_option pp.all false def tst42 : MetaM Unit := do print "----- tst42 -----"; let t ← mkListLit nat [mkRawNatLit 1, mkRawNatLit 2]; print t; check t; let t ← mkArrayLit nat [mkRawNatLit 1, mkRawNatLit 2]; print t; check t; (match t.arrayLit? with | some (_, xs) => do checkM $ pure $ xs.length == 2; (match (xs.get! 0).natLit?, (xs.get! 1).natLit? with | some 1, some 2 => pure () | _, _ => throwError "nat lits expected") | none => throwError "array lit expected") #eval tst42
bcf6188294221db5a42b13ec9ba3897f7e274084
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/polynomial/dickson.lean
76e01282385bd0a2679c0a81faca5b7f78c92a74
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
10,686
lean
/- Copyright (c) 2021 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer -/ import algebra.char_p.invertible import data.zmod.basic import field_theory.finite.basic import ring_theory.localization.fraction_ring import ring_theory.polynomial.chebyshev /-! # Dickson polynomials The (generalised) Dickson polynomials are a family of polynomials indexed by `ℕ × ℕ`, with coefficients in a commutative ring `R` depending on an element `a∈R`. More precisely, the they satisfy the recursion `dickson k a (n + 2) = X * (dickson k a n + 1) - a * (dickson k a n)` with starting values `dickson k a 0 = 3 - k` and `dickson k a 1 = X`. In the literature, `dickson k a n` is called the `n`-th Dickson polynomial of the `k`-th kind associated to the parameter `a : R`. They are closely related to the Chebyshev polynomials in the case that `a=1`. When `a=0` they are just the family of monomials `X ^ n`. ## Main definition * `polynomial.dickson`: the generalised Dickson polynomials. ## Main statements * `polynomial.dickson_one_one_mul`, the `(m * n)`-th Dickson polynomial of the first kind for parameter `1 : R` is the composition of the `m`-th and `n`-th Dickson polynomials of the first kind for `1 : R`. * `polynomial.dickson_one_one_char_p`, for a prime number `p`, the `p`-th Dickson polynomial of the first kind associated to parameter `1 : R` is congruent to `X ^ p` modulo `p`. ## References * [R. Lidl, G. L. Mullen and G. Turnwald, _Dickson polynomials_][MR1237403] ## TODO * Redefine `dickson` in terms of `linear_recurrence`. * Show that `dickson 2 1` is equal to the characteristic polynomial of the adjacency matrix of a type A Dynkin diagram. * Prove that the adjacency matrices of simply laced Dynkin diagrams are precisely the adjacency matrices of simple connected graphs which annihilate `dickson 2 1`. -/ noncomputable theory namespace polynomial open_locale polynomial variables {R S : Type*} [comm_ring R] [comm_ring S] (k : ℕ) (a : R) /-- `dickson` is the `n`the (generalised) Dickson polynomial of the `k`-th kind associated to the element `a ∈ R`. -/ noncomputable def dickson : ℕ → R[X] | 0 := 3 - k | 1 := X | (n + 2) := X * dickson (n + 1) - (C a) * dickson n @[simp] lemma dickson_zero : dickson k a 0 = 3 - k := rfl @[simp] lemma dickson_one : dickson k a 1 = X := rfl lemma dickson_two : dickson k a 2 = X ^ 2 - C a * (3 - k) := by simp only [dickson, sq] @[simp] lemma dickson_add_two (n : ℕ) : dickson k a (n + 2) = X * dickson k a (n + 1) - C a * dickson k a n := by rw dickson lemma dickson_of_two_le {n : ℕ} (h : 2 ≤ n) : dickson k a n = X * dickson k a (n - 1) - C a * dickson k a (n - 2) := begin obtain ⟨n, rfl⟩ := nat.exists_eq_add_of_le h, rw add_comm, exact dickson_add_two k a n end variables {R S k a} lemma map_dickson (f : R →+* S) : ∀ (n : ℕ), map f (dickson k a n) = dickson k (f a) n | 0 := by simp only [dickson_zero, polynomial.map_sub, polynomial.map_nat_cast, bit1, bit0, polynomial.map_add, polynomial.map_one] | 1 := by simp only [dickson_one, map_X] | (n + 2) := begin simp only [dickson_add_two, polynomial.map_sub, polynomial.map_mul, map_X, map_C], rw [map_dickson, map_dickson] end variable {R} @[simp] lemma dickson_two_zero : ∀ (n : ℕ), dickson 2 (0 : R) n = X ^ n | 0 := by { simp only [dickson_zero, pow_zero], norm_num } | 1 := by simp only [dickson_one, pow_one] | (n + 2) := begin simp only [dickson_add_two, C_0, zero_mul, sub_zero], rw [dickson_two_zero, pow_add X (n + 1) 1, mul_comm, pow_one] end section dickson /-! ### A Lambda structure on `polynomial ℤ` Mathlib doesn't currently know what a Lambda ring is. But once it does, we can endow `polynomial ℤ` with a Lambda structure in terms of the `dickson 1 1` polynomials defined below. There is exactly one other Lambda structure on `polynomial ℤ` in terms of binomial polynomials. -/ variables {R} lemma dickson_one_one_eval_add_inv (x y : R) (h : x * y = 1) : ∀ n, (dickson 1 (1 : R) n).eval (x + y) = x ^ n + y ^ n | 0 := by { simp only [bit0, eval_one, eval_add, pow_zero, dickson_zero], norm_num } | 1 := by simp only [eval_X, dickson_one, pow_one] | (n + 2) := begin simp only [eval_sub, eval_mul, dickson_one_one_eval_add_inv, eval_X, dickson_add_two, C_1, eval_one], conv_lhs { simp only [pow_succ, add_mul, mul_add, h, ← mul_assoc, mul_comm y x, one_mul] }, ring_exp end variables (R) lemma dickson_one_one_eq_chebyshev_T [invertible (2 : R)] : ∀ n, dickson 1 (1 : R) n = 2 * (chebyshev.T R n).comp (C (⅟2) * X) | 0 := by { simp only [chebyshev.T_zero, mul_one, one_comp, dickson_zero], norm_num } | 1 := by rw [dickson_one, chebyshev.T_one, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul, mul_inv_of_self, C_1, one_mul] | (n + 2) := begin simp only [dickson_add_two, chebyshev.T_add_two, dickson_one_one_eq_chebyshev_T (n + 1), dickson_one_one_eq_chebyshev_T n, sub_comp, mul_comp, add_comp, X_comp, bit0_comp, one_comp], simp only [← C_1, ← C_bit0, ← mul_assoc, ← C_mul, mul_inv_of_self], rw [C_1, one_mul], ring end lemma chebyshev_T_eq_dickson_one_one [invertible (2 : R)] (n : ℕ) : chebyshev.T R n = C (⅟2) * (dickson 1 1 n).comp (2 * X) := begin rw dickson_one_one_eq_chebyshev_T, simp only [comp_assoc, mul_comp, C_comp, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul], rw [inv_of_mul_self, C_1, one_mul, one_mul, comp_X] end /-- The `(m * n)`-th Dickson polynomial of the first kind is the composition of the `m`-th and `n`-th. -/ lemma dickson_one_one_mul (m n : ℕ) : dickson 1 (1 : R) (m * n) = (dickson 1 1 m).comp (dickson 1 1 n) := begin have h : (1 : R) = int.cast_ring_hom R (1), simp only [ring_hom.eq_int_cast, int.cast_one], rw h, simp only [← map_dickson (int.cast_ring_hom R), ← map_comp], congr' 1, apply map_injective (int.cast_ring_hom ℚ) int.cast_injective, simp only [map_dickson, map_comp, ring_hom.eq_int_cast, int.cast_one, dickson_one_one_eq_chebyshev_T, chebyshev.T_mul, two_mul, ← add_comp], simp only [← two_mul, ← comp_assoc], apply eval₂_congr rfl rfl, rw [comp_assoc], apply eval₂_congr rfl _ rfl, rw [mul_comp, C_comp, X_comp, ← mul_assoc, ← C_1, ← C_bit0, ← C_mul, inv_of_mul_self, C_1, one_mul] end lemma dickson_one_one_comp_comm (m n : ℕ) : (dickson 1 (1 : R) m).comp (dickson 1 1 n) = (dickson 1 1 n).comp (dickson 1 1 m) := by rw [← dickson_one_one_mul, mul_comm, dickson_one_one_mul] lemma dickson_one_one_zmod_p (p : ℕ) [fact p.prime] : dickson 1 (1 : zmod p) p = X ^ p := begin -- Recall that `dickson_eval_add_inv` characterises `dickson 1 1 p` -- as a polynomial that maps `x + x⁻¹` to `x ^ p + (x⁻¹) ^ p`. -- Since `X ^ p` also satisfies this property in characteristic `p`, -- we can use a variant on `polynomial.funext` to conclude that these polynomials are equal. -- For this argument, we need an arbitrary infinite field of characteristic `p`. obtain ⟨K, _, _, H⟩ : ∃ (K : Type) (_ : field K), by exactI ∃ (_ : char_p K p), infinite K, { let K := fraction_ring (polynomial (zmod p)), let f : zmod p →+* K := (algebra_map _ (fraction_ring _)).comp C, haveI : char_p K p, { rw ← f.char_p_iff_char_p, apply_instance }, haveI : infinite K := infinite.of_injective (algebra_map (polynomial (zmod p)) (fraction_ring (polynomial (zmod p)))) (is_fraction_ring.injective _ _), refine ⟨K, _, _, _⟩; apply_instance }, resetI, apply map_injective (zmod.cast_hom (dvd_refl p) K) (ring_hom.injective _), rw [map_dickson, polynomial.map_pow, map_X], apply eq_of_infinite_eval_eq, -- The two polynomials agree on all `x` of the form `x = y + y⁻¹`. apply @set.infinite.mono _ {x : K | ∃ y, x = y + y⁻¹ ∧ y ≠ 0}, { rintro _ ⟨x, rfl, hx⟩, simp only [eval_X, eval_pow, set.mem_set_of_eq, @add_pow_char K _ p, dickson_one_one_eval_add_inv _ _ (mul_inv_cancel hx), inv_pow₀, zmod.cast_hom_apply, zmod.cast_one'] }, -- Now we need to show that the set of such `x` is infinite. -- If the set is finite, then we will show that `K` is also finite. { intro h, rw ← set.infinite_univ_iff at H, apply H, -- To each `x` of the form `x = y + y⁻¹` -- we `bind` the set of `y` that solve the equation `x = y + y⁻¹`. -- For every `x`, that set is finite (since it is governed by a quadratic equation). -- For the moment, we claim that all these sets together cover `K`. suffices : (set.univ : set K) = {x : K | ∃ (y : K), x = y + y⁻¹ ∧ y ≠ 0} >>= (λ x, {y | x = y + y⁻¹ ∨ y = 0}), { rw this, clear this, refine h.bUnion (λ x hx, _), -- The following quadratic polynomial has as solutions the `y` for which `x = y + y⁻¹`. let φ : K[X] := X ^ 2 - C x * X + 1, have hφ : φ ≠ 0, { intro H, have : φ.eval 0 = 0, by rw [H, eval_zero], simpa [eval_X, eval_one, eval_pow, eval_sub, sub_zero, eval_add, eval_mul, mul_zero, sq, zero_add, one_ne_zero] }, classical, convert (φ.roots ∪ {0}).to_finset.finite_to_set using 1, ext1 y, simp only [multiset.mem_to_finset, set.mem_set_of_eq, finset.mem_coe, multiset.mem_union, mem_roots hφ, is_root, eval_add, eval_sub, eval_pow, eval_mul, eval_X, eval_C, eval_one, multiset.mem_singleton], by_cases hy : y = 0, { simp only [hy, eq_self_iff_true, or_true] }, apply or_congr _ iff.rfl, rw [← mul_left_inj' hy, eq_comm, ← sub_eq_zero, add_mul, inv_mul_cancel hy], apply eq_iff_eq_cancel_right.mpr, ring }, -- Finally, we prove the claim that our finite union of finite sets covers all of `K`. { apply (set.eq_univ_of_forall _).symm, intro x, simp only [exists_prop, set.mem_Union, set.bind_def, ne.def, set.mem_set_of_eq], by_cases hx : x = 0, { simp only [hx, and_true, eq_self_iff_true, inv_zero, or_true], exact ⟨_, 1, rfl, one_ne_zero⟩ }, { simp only [hx, or_false, exists_eq_right], exact ⟨_, rfl, hx⟩ } } } end lemma dickson_one_one_char_p (p : ℕ) [fact p.prime] [char_p R p] : dickson 1 (1 : R) p = X ^ p := begin have h : (1 : R) = zmod.cast_hom (dvd_refl p) R (1), simp only [zmod.cast_hom_apply, zmod.cast_one'], rw [h, ← map_dickson (zmod.cast_hom (dvd_refl p) R), dickson_one_one_zmod_p, polynomial.map_pow, map_X] end end dickson end polynomial
4f9b539722a45c28846950def5202cc66a4c79f9
ecdf4e083eb363cd3a0d6880399f86e2cd7f5adb
/src/group_theory/mathieu_group.lean
73cda7cbf5000c6e17c8aa74edf1da433fa24bcb
[]
no_license
fpvandoorn/formalabstracts
29aa71772da418f18994c38379e2192a6ef361f7
cea2f9f96d89ee1187d1b01e33f22305cdfe4d59
refs/heads/master
1,609,476,761,601
1,558,130,287,000
1,558,130,287,000
97,261,457
0
2
null
1,550,879,230,000
1,500,056,313,000
Lean
UTF-8
Lean
false
false
4,115
lean
import group_theory.basic import data.finset group_theory.group_action open finset fintype universes u v local attribute [instance, priority 0] classical.prop_decidable variables {α : Type u} {β : Type v} {t k v : ℕ} namespace mathieu_group /-- A Steiner system $S(t,k,v)$, where $t < k < v$ are positive integers is a finite set $X$ of cardinality $v$, a collection of $k$ element subsets of $X$ (called blocks), such that each $t$ element subset of $X$ is contained in a unique block. -/ structure steiner_system (t k v : ℕ) := (X : Type u) (blocks : finset (finset X)) (h₁ : fintype X) (h₂ : card X = v) (h₃ : ∀ b ∈ blocks, card b = k) (h₄ : ∀ x : finset X, card x = t → ∃! b, b ∈ blocks ∧ x ⊂ b) /-- The coercion from steiner systems to types. -/ instance : has_coe_to_sort (steiner_system t k v) := ⟨Type u, λ s, s.X⟩ /-- The underlying set of a steiner system is finite. -/ instance steiner_system_fintype (s : steiner_system t k v) : fintype s := s.h₁ def nonempty_steiner_system {s : steiner_system t k v} (h : v > 0) : nonempty s := by { rw ← fintype.card_pos_iff, rw ←s.h₂ at h, exact h } /-- The set of all isomorphisms of between two steiner systems consists of all equivalences of the underlying sets which are block-preserving. -/ def steiner_system_isomorphism {t₁ k₁ v₁ : ℕ} {t₂ k₂ v₂ : ℕ} (s₁ : steiner_system t₁ k₁ v₁) (s₂ : steiner_system t₂ k₂ v₂) := {f : s₁ ≃ s₂ | ∀ b ∈ s₁.blocks, finset.image f b ∈ s₂.blocks } /-- The automorphism set $\mathrm{Aut}(s)$ of a steiner system $s$ is the set of isomorphisms from $s$ to $s$. -/ def Aut {t k v : ℕ}(s : steiner_system t k v) := steiner_system_isomorphism s s /-- The automorphism set of a steiner system can be equipped with a group structure with group identity the identity function, multiplication is function composition, inverses are function inverses. -/ instance {s : steiner_system t k v} : group (Aut s) := { one := ⟨equiv.refl s, omitted⟩ , mul := fun f g, ⟨f.1.trans g.1, omitted⟩, inv := fun f, ⟨ equiv.symm f.1, omitted⟩, one_mul := omitted, mul_one := omitted, mul_left_inv := omitted, mul_assoc := omitted, } /-- The Steiner system $S(5,8,24)$ exists and is unique up to isomorphism.-/ lemma is_unique_s_5_8_24 : ∃ x: steiner_system 5 8 24, ∀ y : steiner_system 5 8 24, nonempty $ steiner_system_isomorphism x y := omitted /-- Using the axiom of choice, we can pick a representative of the isomorphism class of $S(5,8,24)$.-/ noncomputable def s_5_8_24 : steiner_system 5 8 24 := classical.some is_unique_s_5_8_24 /-- The Steiner system $S(5,6,12)$ exists and is unique up to isomorphism.-/ lemma is_unique_s_5_6_12 : ∃ x : steiner_system 5 6 12, ∀ y : steiner_system 5 6 12, nonempty $ steiner_system_isomorphism x y := omitted /-- Using the axiom of choice, we can pick a representative of the isomorphism class of $S(5,6,12)$.-/ noncomputable def s_5_6_12 : steiner_system 5 6 12 := classical.some is_unique_s_5_6_12 /-- The automorphism group of a steiner system acts on the underlying set through the evaluation action.-/ def evaluation {t k v : ℕ} (s : steiner_system t k v) : Aut(s) → s → s := λ f x, f.1 x /-- The evaluation action of the automorphism group satisfies the properties of a monoid action. -/ instance evaluation_action {t k v : ℕ} (s : steiner_system t k v) : mul_action (Aut s) s := { smul := evaluation s, one_smul := omitted, mul_smul := omitted } /- *TODO(Kody) : move these to mathlib?* -/ /-- The two point stabilizer of a group is the set of all group elements which fix two distinct points of the group via the group action. -/ def two_pt_stabilizer [monoid α] (f : mul_action α β) {x : β × β} (h : x.1 ≠ x.2) : set α := { y : α | y • x.1 = x.1 ∧ y • x.2 = x.2} /-- The two point stabilizer of a group is a subgroup of the group. -/ instance is_subgroup_two_pt_stabilizer (α) [group α] (f : mul_action α β) {x : β × β} (h : x.1 ≠ x.2) : is_subgroup (two_pt_stabilizer f h) := omitted end mathieu_group
e340eb21ece7a67cb280a3b00a939153590db4f9
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/laurent_series.lean
0435cc96b3c9366fb4500e9dce194f543c846e67
[ "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
8,138
lean
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import ring_theory.hahn_series import ring_theory.localization /-! # Laurent Series ## Main Definitions * Defines `laurent_series` as an abbreviation for `hahn_series ℤ`. * Provides a coercion `power_series R` into `laurent_series R` given by `hahn_series.of_power_series`. * Defines `laurent_series.power_series_part` * Defines the localization map `laurent_series.of_power_series_localization` which evaluates to `hahn_series.of_power_series`. -/ open hahn_series open_locale big_operators classical noncomputable theory universe u /-- A `laurent_series` is implemented as a `hahn_series` with value group `ℤ`. -/ abbreviation laurent_series (R : Type*) [has_zero R] := hahn_series ℤ R variables {R : Type u} namespace laurent_series section semiring variable [semiring R] instance : has_coe (power_series R) (laurent_series R) := ⟨hahn_series.of_power_series ℤ R⟩ lemma coe_power_series (x : power_series R) : (x : laurent_series R) = hahn_series.of_power_series ℤ R x := rfl @[simp] lemma coeff_coe_power_series (x : power_series R) (n : ℕ) : hahn_series.coeff (x : laurent_series R) n = power_series.coeff R n x := by rw [← int.nat_cast_eq_coe_nat, coe_power_series, of_power_series_apply_coeff] /-- This is a power series that can be multiplied by an integer power of `X` to give our Laurent series. If the Laurent series is nonzero, `power_series_part` has a nonzero constant term. -/ def power_series_part (x : laurent_series R) : power_series R := power_series.mk (λ n, x.coeff (x.order + n)) @[simp] lemma power_series_part_coeff (x : laurent_series R) (n : ℕ) : power_series.coeff R n x.power_series_part = x.coeff (x.order + n) := power_series.coeff_mk _ _ @[simp] lemma power_series_part_zero : power_series_part (0 : laurent_series R) = 0 := by { ext, simp } @[simp] lemma power_series_part_eq_zero (x : laurent_series R) : x.power_series_part = 0 ↔ x = 0 := begin split, { contrapose!, intro h, rw [power_series.ext_iff, not_forall], refine ⟨0, _⟩, simp [coeff_order_ne_zero h] }, { rintro rfl, simp } end @[simp] lemma single_order_mul_power_series_part (x : laurent_series R) : (single x.order 1 : laurent_series R) * x.power_series_part = x := begin ext n, rw [← sub_add_cancel n x.order, single_mul_coeff_add, sub_add_cancel, one_mul], by_cases h : x.order ≤ n, { rw [int.eq_nat_abs_of_zero_le (sub_nonneg_of_le h), coeff_coe_power_series, power_series_part_coeff, ← int.eq_nat_abs_of_zero_le (sub_nonneg_of_le h), add_sub_cancel'_right] }, { rw [coe_power_series, of_power_series_apply, emb_domain_notin_range], { contrapose! h, exact order_le_of_coeff_ne_zero h.symm }, { contrapose! h, simp only [set.mem_range, rel_embedding.coe_fn_mk, function.embedding.coe_fn_mk, int.nat_cast_eq_coe_nat] at h, obtain ⟨m, hm⟩ := h, rw [← sub_nonneg, ← hm], exact int.zero_le_of_nat _ } } end lemma of_power_series_power_series_part (x : laurent_series R) : of_power_series ℤ R x.power_series_part = single (-x.order) 1 * x := begin refine eq.trans _ (congr rfl x.single_order_mul_power_series_part), rw [← mul_assoc, single_mul_single, neg_add_self, mul_one, ← C_apply, C_one, one_mul, coe_power_series], end end semiring instance [comm_semiring R] : algebra (power_series R) (laurent_series R) := (hahn_series.of_power_series ℤ R).to_algebra @[simp] lemma coe_algebra_map [comm_semiring R] : ⇑(algebra_map (power_series R) (laurent_series R)) = hahn_series.of_power_series ℤ R := rfl /-- The localization map from power series to Laurent series. -/ @[simps] instance of_power_series_localization [comm_ring R] : is_localization (submonoid.powers (power_series.X : power_series R)) (laurent_series R) := { map_units := (begin rintro ⟨_, n, rfl⟩, refine ⟨⟨single (n : ℤ) 1, single (-n : ℤ) 1, _, _⟩, _⟩, { simp only [single_mul_single, mul_one, add_right_neg], refl }, { simp only [single_mul_single, mul_one, add_left_neg], refl }, { simp } end), surj := (begin intro z, by_cases h : 0 ≤ z.order, { refine ⟨⟨power_series.X ^ (int.nat_abs z.order) * power_series_part z, 1⟩, _⟩, simp only [ring_hom.map_one, mul_one, ring_hom.map_mul, coe_algebra_map, of_power_series_X_pow, submonoid.coe_one, int.nat_cast_eq_coe_nat], rw [int.nat_abs_of_nonneg h, ← coe_power_series, single_order_mul_power_series_part] }, { refine ⟨⟨power_series_part z, power_series.X ^ (int.nat_abs z.order), ⟨_, rfl⟩⟩, _⟩, simp only [coe_algebra_map, of_power_series_power_series_part], rw [mul_comm _ z], refine congr rfl _, rw [subtype.coe_mk, of_power_series_X_pow, int.nat_cast_eq_coe_nat, int.of_nat_nat_abs_of_nonpos], exact le_of_not_ge h } end), eq_iff_exists := (begin intros x y, rw [coe_algebra_map, of_power_series_injective.eq_iff], split, { rintro rfl, exact ⟨1, rfl⟩ }, { rintro ⟨⟨_, n, rfl⟩, hc⟩, rw [← sub_eq_zero, ← sub_mul, power_series.ext_iff] at hc, rw [← sub_eq_zero, power_series.ext_iff], intro m, have h := hc (m + n), rw [linear_map.map_zero, subtype.coe_mk, power_series.X_pow_eq, power_series.monomial, power_series.coeff, finsupp.single_add, mv_power_series.coeff_add_mul_monomial, mul_one] at h, exact h } end) } instance {K : Type u} [field K] : is_fraction_ring (power_series K) (laurent_series K) := is_localization.of_le (submonoid.powers (power_series.X : power_series K)) _ (powers_le_non_zero_divisors_of_no_zero_divisors power_series.X_ne_zero) (λ f hf, is_unit_of_mem_non_zero_divisors $ map_mem_non_zero_divisors _ hahn_series.of_power_series_injective hf) end laurent_series namespace power_series open laurent_series variables [semiring R] (f g : power_series R) @[simp, norm_cast] lemma coe_zero : ((0 : power_series R) : laurent_series R) = 0 := (of_power_series ℤ R).map_zero @[simp, norm_cast] lemma coe_one : ((1 : power_series R) : laurent_series R) = 1 := (of_power_series ℤ R).map_one @[simp, norm_cast] lemma coe_add : ((f + g : power_series R) : laurent_series R) = f + g := (of_power_series ℤ R).map_add _ _ @[simp, norm_cast] lemma coe_mul : ((f * g : power_series R) : laurent_series R) = f * g := (of_power_series ℤ R).map_mul _ _ lemma coeff_coe (i : ℤ) : ((f : power_series R) : laurent_series R).coeff i = if i < 0 then 0 else power_series.coeff R i.nat_abs f := begin cases i, { rw [int.nat_abs_of_nat_core, int.of_nat_eq_coe, coeff_coe_power_series, if_neg (int.coe_nat_nonneg _).not_lt] }, { rw [coe_power_series, of_power_series_apply, emb_domain_notin_image_support, if_pos (int.neg_succ_lt_zero _)], simp only [not_exists, rel_embedding.coe_fn_mk, set.mem_image, not_and, function.embedding.coe_fn_mk, ne.def, to_power_series_symm_apply_coeff, mem_support, int.nat_cast_eq_coe_nat, int.coe_nat_eq, implies_true_iff, not_false_iff] } end @[simp, norm_cast] lemma coe_C (r : R) : ((C R r : power_series R) : laurent_series R) = hahn_series.C r := of_power_series_C _ @[simp] lemma coe_X : ((X : power_series R) : laurent_series R) = single 1 1 := of_power_series_X @[simp, norm_cast] lemma coe_smul {S : Type*} [semiring S] [module R S] (r : R) (x : power_series S) : ((r • x : power_series S) : laurent_series S) = r • x := by { ext, simp [coeff_coe, coeff_smul, smul_ite] } @[simp, norm_cast] lemma coe_bit0 : ((bit0 f : power_series R) : laurent_series R) = bit0 f := (of_power_series ℤ R).map_bit0 _ @[simp, norm_cast] lemma coe_bit1 : ((bit1 f : power_series R) : laurent_series R) = bit1 f := (of_power_series ℤ R).map_bit1 _ @[simp, norm_cast] lemma coe_pow (n : ℕ) : ((f ^ n : power_series R) : laurent_series R) = f ^ n := (of_power_series ℤ R).map_pow _ _ end power_series
0df65c6ce400e23f4fe4c586693438339107cb20
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/category/Module/products.lean
1db567c7430491eb95607423359ebef7bb8791d4
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
1,948
lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import linear_algebra.pi import algebra.category.Module.basic /-! # The concrete products in the category of modules are products in the categorical sense. -/ open category_theory open category_theory.limits universes u v w namespace Module variables {R : Type u} [ring R] variables {ι : Type v} (Z : ι → Module.{max v w} R) /-- The product cone induced by the concrete product. -/ def product_cone : fan Z := fan.mk (Module.of R (Π i : ι, Z i)) (λ i, (linear_map.proj i : (Π i : ι, Z i) →ₗ[R] Z i)) /-- The concrete product cone is limiting. -/ def product_cone_is_limit : is_limit (product_cone Z) := { lift := λ s, (linear_map.pi (λ j, s.π.app ⟨j⟩) : s.X →ₗ[R] (Π i : ι, Z i)), fac' := λ s j, by { cases j, tidy, }, uniq' := λ s m w, by { ext x i, exact linear_map.congr_fun (w ⟨i⟩) x, }, } -- While we could use this to construct a `has_products (Module R)` instance, -- we already have `has_limits (Module R)` in `algebra.category.Module.limits`. variables [has_product Z] /-- The categorical product of a family of objects in `Module` agrees with the usual module-theoretical product. -/ noncomputable def pi_iso_pi : ∏ Z ≅ Module.of R (Π i, Z i) := limit.iso_limit_cone ⟨_, product_cone_is_limit Z⟩ -- We now show this isomorphism commutes with the inclusion of the kernel into the source. @[simp, elementwise] lemma pi_iso_pi_inv_kernel_ι (i : ι) : (pi_iso_pi Z).inv ≫ pi.π Z i = (linear_map.proj i : (Π i : ι, Z i) →ₗ[R] Z i) := limit.iso_limit_cone_inv_π _ _ @[simp, elementwise] lemma pi_iso_pi_hom_ker_subtype (i : ι) : (pi_iso_pi Z).hom ≫ (linear_map.proj i : (Π i : ι, Z i) →ₗ[R] Z i) = pi.π Z i := is_limit.cone_point_unique_up_to_iso_inv_comp _ (limit.is_limit _) (discrete.mk i) end Module
e30202e29956fc7a5acb918d3a2badf0cb115d20
07c6143268cfb72beccd1cc35735d424ebcb187b
/src/tactic/lint/default.lean
f7a1e9f952473d5594566b61183992865d9d3da6
[ "Apache-2.0" ]
permissive
khoek/mathlib
bc49a842910af13a3c372748310e86467d1dc766
aa55f8b50354b3e11ba64792dcb06cccb2d8ee28
refs/heads/master
1,588,232,063,837
1,587,304,803,000
1,587,304,803,000
176,688,517
0
0
Apache-2.0
1,553,070,585,000
1,553,070,585,000
null
UTF-8
Lean
false
false
4,039
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import tactic.lint.frontend tactic.lint.simp tactic.lint.type_classes tactic.lint.misc open tactic add_tactic_doc { name := "linting commands", category := doc_category.cmd, decl_names := [`lint_cmd, `lint_mathlib_cmd, `lint_all_cmd, `list_linters], tags := ["linting"], description := "User commands to spot common mistakes in the code * `#lint`: check all declarations in the current file * `#lint_mathlib`: check all declarations in mathlib (so excluding core or other projects, and also excluding the current file) * `#lint_all`: check all declarations in the environment (the current file and all imported files) The following linters are run by default: 1. `unused_arguments` checks for unused arguments in declarations. 2. `def_lemma` checks whether a declaration is incorrectly marked as a def/lemma. 3. `dup_namespce` checks whether a namespace is duplicated in the name of a declaration. 4. `ge_or_gt` checks whether ≥/> is used in the declaration. 5. `instance_priority` checks that instances that always apply have priority below default. 6. `doc_blame` checks for missing doc strings on definitions and constants. 7. `has_inhabited_instance` checks whether every type has an associated `inhabited` instance. 8. `impossible_instance` checks for instances that can never fire. 9. `incorrect_type_class_argument` checks for arguments in [square brackets] that are not classes. 10. `dangerous_instance` checks for instances that generate type-class problems with metavariables. 11. `fails_quickly` tests that type-class inference ends (relatively) quickly when applied to variables. 12. `has_coe_variable` tests that there are no instances of type `has_coe α t` for a variable `α`. 13. `inhabited_nonempty` checks for `inhabited` instance arguments that should be changed to `nonempty`. 14. `simp_nf` checks that the left-hand side of simp lemmas is in simp-normal form. 15. `simp_var_head` checks that there are no variables as head symbol of left-hand sides of simp lemmas. 16. `simp_comm` checks that no commutativity lemmas (such as `add_comm`) are marked simp. 17. `decidable_classical` checks for `decidable` hypotheses that are used in the proof of a proposition but not in the statement, and could be removed using `classical`. 18. `has_coe_to_fun` checks that every type that coerces to a function has a direct `has_coe_to_fun` instance. Another linter, `doc_blame_thm`, checks for missing doc strings on lemmas and theorems. This is not run by default. The command `#list_linters` prints a list of the names of all available linters. You can append a `*` to any command (e.g. `#lint_mathlib*`) to omit the slow tests (4). You can append a `-` to any command (e.g. `#lint_mathlib-`) to run a silent lint that suppresses the output of passing checks. A silent lint will fail if any test fails. You can append a sequence of linter names to any command to run extra tests, in addition to the default ones. e.g. `#lint doc_blame_thm` will run all default tests and `doc_blame_thm`. You can append `only name1 name2 ...` to any command to run a subset of linters, e.g. `#lint only unused_arguments` You can add custom linters by defining a term of type `linter` in the `linter` namespace. A linter defined with the name `linter.my_new_check` can be run with `#lint my_new_check` or `lint only my_new_check`. If you add the attribute `@[linter]` to `linter.my_new_check` it will run by default. Adding the attribute `@[nolint doc_blame unused_arguments]` to a declaration omits it from only the specified linter checks." } /-- The default linters used in mathlib CI. -/ meta def mathlib_linters : list name := by do ls ← get_checks tt [] ff, let ls := ls.map (λ ⟨n, _⟩, `linter ++ n), exact (reflect ls)
8a17589c8379ec007856015acd4d61382aca9ed5
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/eq21.lean
141a9c3571a599b4fa95879bbb1666ce6ad5ec5b
[ "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
413
lean
inductive formula | eqf : nat → nat → formula | impf : formula → formula → formula namespace formula definition denote : formula → Prop | (eqf n1 n2) := n1 = n2 | (impf f1 f2) := denote f1 → denote f2 theorem denote_eqf (n1 n2 : nat) : denote (eqf n1 n2) = (n1 = n2) := rfl theorem denote_impf (f1 f2 : formula) : denote (impf f1 f2) = (denote f1 → denote f2) := rfl end formula
3d6bae12c27d81976b97e724d41bab138a5a77e3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/padics/padic_integers.lean
df485c83a582946a367baf6105c3ef59c3b8d486
[]
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
18,345
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, Mario Carneiro, Johan Commelin -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.int.modeq import Mathlib.data.zmod.basic import Mathlib.linear_algebra.adic_completion import Mathlib.data.padics.padic_numbers import Mathlib.ring_theory.discrete_valuation_ring import Mathlib.topology.metric_space.cau_seq_filter import Mathlib.PostPort namespace Mathlib /-! # p-adic integers This file defines the p-adic integers `ℤ_p` as the subtype of `ℚ_p` with norm `≤ 1`. We show that `ℤ_p` * is complete * is nonarchimedean * is a normed ring * is a local ring * is a discrete valuation ring The relation between `ℤ_[p]` and `zmod p` is established in another file. ## Important definitions * `padic_int` : the type of p-adic numbers ## Notation We introduce the notation `ℤ_[p]` for the p-adic integers. ## Implementation notes Much, but not all, of this file assumes that `p` is prime. This assumption is inferred automatically by taking `[fact (nat.prime p)] as a type class argument. Coercions into `ℤ_p` are set up to work with the `norm_cast` tactic. ## References * [F. Q. Gouêva, *p-adic numbers*][gouvea1997] * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/P-adic_number> ## Tags p-adic, p adic, padic, p-adic integer -/ /-- The p-adic integers ℤ_p are the p-adic numbers with norm ≤ 1. -/ def padic_int (p : ℕ) [fact (nat.prime p)] := Subtype fun (x : padic p) => norm x ≤ 1 namespace padic_int /-! ### Ring structure and coercion to `ℚ_[p]` -/ protected instance padic.has_coe {p : ℕ} [fact (nat.prime p)] : has_coe (padic_int p) (padic p) := has_coe.mk subtype.val theorem ext {p : ℕ} [fact (nat.prime p)] {x : padic_int p} {y : padic_int p} : ↑x = ↑y → x = y := iff.mpr subtype.ext_iff_val /-- Addition on ℤ_p is inherited from ℚ_p. -/ protected instance has_add {p : ℕ} [fact (nat.prime p)] : Add (padic_int p) := { add := fun (_x : padic_int p) => sorry } /-- Multiplication on ℤ_p is inherited from ℚ_p. -/ protected instance has_mul {p : ℕ} [fact (nat.prime p)] : Mul (padic_int p) := { mul := fun (_x : padic_int p) => sorry } /-- Negation on ℤ_p is inherited from ℚ_p. -/ protected instance has_neg {p : ℕ} [fact (nat.prime p)] : Neg (padic_int p) := { neg := fun (_x : padic_int p) => sorry } /-- Subtraction on ℤ_p is inherited from ℚ_p. -/ protected instance has_sub {p : ℕ} [fact (nat.prime p)] : Sub (padic_int p) := { sub := fun (_x : padic_int p) => sorry } /-- Zero on ℤ_p is inherited from ℚ_p. -/ protected instance has_zero {p : ℕ} [fact (nat.prime p)] : HasZero (padic_int p) := { zero := { val := 0, property := sorry } } protected instance inhabited {p : ℕ} [fact (nat.prime p)] : Inhabited (padic_int p) := { default := 0 } /-- One on ℤ_p is inherited from ℚ_p. -/ protected instance has_one {p : ℕ} [fact (nat.prime p)] : HasOne (padic_int p) := { one := { val := 1, property := sorry } } @[simp] theorem mk_zero {p : ℕ} [fact (nat.prime p)] {h : norm 0 ≤ 1} : { val := 0, property := h } = 0 := rfl @[simp] theorem val_eq_coe {p : ℕ} [fact (nat.prime p)] (z : padic_int p) : subtype.val z = ↑z := rfl @[simp] theorem coe_add {p : ℕ} [fact (nat.prime p)] (z1 : padic_int p) (z2 : padic_int p) : ↑(z1 + z2) = ↑z1 + ↑z2 := sorry @[simp] theorem coe_mul {p : ℕ} [fact (nat.prime p)] (z1 : padic_int p) (z2 : padic_int p) : ↑(z1 * z2) = ↑z1 * ↑z2 := sorry @[simp] theorem coe_neg {p : ℕ} [fact (nat.prime p)] (z1 : padic_int p) : ↑(-z1) = -↑z1 := subtype.cases_on z1 fun (z1_val : padic p) (z1_property : norm z1_val ≤ 1) => idRhs (↑(-{ val := z1_val, property := z1_property }) = ↑(-{ val := z1_val, property := z1_property })) rfl @[simp] theorem coe_sub {p : ℕ} [fact (nat.prime p)] (z1 : padic_int p) (z2 : padic_int p) : ↑(z1 - z2) = ↑z1 - ↑z2 := sorry @[simp] theorem coe_one {p : ℕ} [fact (nat.prime p)] : ↑1 = 1 := rfl @[simp] theorem coe_coe {p : ℕ} [fact (nat.prime p)] (n : ℕ) : ↑↑n = ↑n := sorry @[simp] theorem coe_coe_int {p : ℕ} [fact (nat.prime p)] (z : ℤ) : ↑↑z = ↑z := sorry @[simp] theorem coe_zero {p : ℕ} [fact (nat.prime p)] : ↑0 = 0 := rfl protected instance ring {p : ℕ} [fact (nat.prime p)] : ring (padic_int p) := ring.mk Add.add sorry 0 sorry sorry Neg.neg Sub.sub sorry sorry Mul.mul sorry 1 sorry sorry sorry sorry /-- The coercion from ℤ[p] to ℚ[p] as a ring homomorphism. -/ def coe.ring_hom {p : ℕ} [fact (nat.prime p)] : padic_int p →+* padic p := ring_hom.mk coe sorry coe_mul sorry coe_add @[simp] theorem coe_pow {p : ℕ} [fact (nat.prime p)] (x : padic_int p) (n : ℕ) : ↑(x ^ n) = ↑x ^ n := ring_hom.map_pow coe.ring_hom x n @[simp] theorem mk_coe {p : ℕ} [fact (nat.prime p)] (k : padic_int p) : { val := ↑k, property := subtype.property k } = k := sorry /-- The inverse of a p-adic integer with norm equal to 1 is also a p-adic integer. Otherwise, the inverse is defined to be 0. -/ def inv {p : ℕ} [fact (nat.prime p)] : padic_int p → padic_int p := sorry protected instance char_zero {p : ℕ} [fact (nat.prime p)] : char_zero (padic_int p) := char_zero.mk fun (m n : ℕ) (h : ↑m = ↑n) => nat.cast_injective ((fun (this : ↑m = ↑n) => this) (eq.mp ((fun (a a_1 : padic p) (e_1 : a = a_1) (ᾰ ᾰ_1 : padic p) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (↑↑m) (↑m) (coe_coe m) (↑↑n) (↑n) (coe_coe n)) (eq.mp (Eq._oldrec (Eq.refl (↑m = ↑n)) (propext subtype.ext_iff)) h))) @[simp] theorem coe_int_eq {p : ℕ} [fact (nat.prime p)] (z1 : ℤ) (z2 : ℤ) : ↑z1 = ↑z2 ↔ z1 = z2 := sorry /-- A sequence of integers that is Cauchy with respect to the `p`-adic norm converges to a `p`-adic integer. -/ def of_int_seq {p : ℕ} [fact (nat.prime p)] (seq : ℕ → ℤ) (h : is_cau_seq (padic_norm p) fun (n : ℕ) => ↑(seq n)) : padic_int p := { val := quotient.mk { val := fun (n : ℕ) => ↑(seq n), property := h }, property := sorry } end padic_int namespace padic_int /-! ### Instances We now show that `ℤ_[p]` is a * complete metric space * normed ring * integral domain -/ protected instance metric_space (p : ℕ) [fact (nat.prime p)] : metric_space (padic_int p) := subtype.metric_space protected instance complete_space (p : ℕ) [fact (nat.prime p)] : complete_space (padic_int p) := sorry protected instance has_norm (p : ℕ) [fact (nat.prime p)] : has_norm (padic_int p) := has_norm.mk fun (z : padic_int p) => norm ↑z protected theorem mul_comm {p : ℕ} [fact (nat.prime p)] (z1 : padic_int p) (z2 : padic_int p) : z1 * z2 = z2 * z1 := sorry protected theorem zero_ne_one {p : ℕ} [fact (nat.prime p)] : 0 ≠ 1 := (fun (this : { val := 0, property := has_zero._proof_1 } ≠ { val := 1, property := has_one._proof_1 }) => this) (mt (iff.mp subtype.ext_iff_val) zero_ne_one) protected theorem eq_zero_or_eq_zero_of_mul_eq_zero {p : ℕ} [fact (nat.prime p)] (a : padic_int p) (b : padic_int p) : a * b = 0 → a = 0 ∨ b = 0 := sorry theorem norm_def {p : ℕ} [fact (nat.prime p)] {z : padic_int p} : norm z = norm ↑z := rfl protected instance normed_comm_ring (p : ℕ) [fact (nat.prime p)] : normed_comm_ring (padic_int p) := normed_comm_ring.mk padic_int.mul_comm protected instance norm_one_class (p : ℕ) [fact (nat.prime p)] : norm_one_class (padic_int p) := norm_one_class.mk (Eq.trans norm_def norm_one) protected instance is_absolute_value (p : ℕ) [fact (nat.prime p)] : is_absolute_value fun (z : padic_int p) => norm z := is_absolute_value.mk norm_nonneg (fun (_x : padic_int p) => sorry) (fun (_x : padic_int p) => sorry) fun (_x _x_1 : padic_int p) => eq.mpr (id ((fun (a a_1 : ℝ) (e_1 : a = a_1) (ᾰ ᾰ_1 : ℝ) (e_2 : ᾰ = ᾰ_1) => congr (congr_arg Eq e_1) e_2) (norm (_x * _x_1)) (norm ↑_x * norm ↑_x_1) (Eq.trans (Eq.trans norm_def ((fun (ᾰ ᾰ_1 : padic p) (e_2 : ᾰ = ᾰ_1) => congr_arg norm e_2) (↑(_x * _x_1)) (↑_x * ↑_x_1) (coe_mul _x _x_1))) (padic_norm_e.mul ↑_x ↑_x_1)) (norm _x * norm _x_1) (norm ↑_x * norm ↑_x_1) ((fun (ᾰ ᾰ_1 : ℝ) (e_2 : ᾰ = ᾰ_1) (ᾰ_2 ᾰ_3 : ℝ) (e_3 : ᾰ_2 = ᾰ_3) => congr (congr_arg Mul.mul e_2) e_3) (norm _x) (norm ↑_x) norm_def (norm _x_1) (norm ↑_x_1) norm_def))) (Eq.refl (norm ↑_x * norm ↑_x_1)) protected instance integral_domain {p : ℕ} [fact (nat.prime p)] : integral_domain (padic_int p) := integral_domain.mk ring.add sorry ring.zero sorry sorry ring.neg ring.sub sorry sorry ring.mul sorry ring.one sorry sorry sorry sorry sorry sorry sorry end padic_int namespace padic_int /-! ### Norm -/ theorem norm_le_one {p : ℕ} [fact (nat.prime p)] (z : padic_int p) : norm z ≤ 1 := subtype.cases_on z fun (z_val : padic p) (z_property : norm z_val ≤ 1) => idRhs (norm z_val ≤ 1) z_property @[simp] theorem norm_mul {p : ℕ} [fact (nat.prime p)] (z1 : padic_int p) (z2 : padic_int p) : norm (z1 * z2) = norm z1 * norm z2 := sorry @[simp] theorem norm_pow {p : ℕ} [fact (nat.prime p)] (z : padic_int p) (n : ℕ) : norm (z ^ n) = norm z ^ n := sorry theorem nonarchimedean {p : ℕ} [fact (nat.prime p)] (q : padic_int p) (r : padic_int p) : norm (q + r) ≤ max (norm q) (norm r) := sorry theorem norm_add_eq_max_of_ne {p : ℕ} [fact (nat.prime p)] {q : padic_int p} {r : padic_int p} : norm q ≠ norm r → norm (q + r) = max (norm q) (norm r) := sorry theorem norm_eq_of_norm_add_lt_right {p : ℕ} [fact (nat.prime p)] {z1 : padic_int p} {z2 : padic_int p} (h : norm (z1 + z2) < norm z2) : norm z1 = norm z2 := sorry theorem norm_eq_of_norm_add_lt_left {p : ℕ} [fact (nat.prime p)] {z1 : padic_int p} {z2 : padic_int p} (h : norm (z1 + z2) < norm z1) : norm z1 = norm z2 := sorry @[simp] theorem padic_norm_e_of_padic_int {p : ℕ} [fact (nat.prime p)] (z : padic_int p) : norm ↑z = norm z := sorry theorem norm_int_cast_eq_padic_norm {p : ℕ} [fact (nat.prime p)] (z : ℤ) : norm ↑z = norm ↑z := sorry @[simp] theorem norm_eq_padic_norm {p : ℕ} [fact (nat.prime p)] {q : padic p} (hq : norm q ≤ 1) : norm { val := q, property := hq } = norm q := rfl @[simp] theorem norm_p {p : ℕ} [fact (nat.prime p)] : norm ↑p = (↑p⁻¹) := sorry @[simp] theorem norm_p_pow {p : ℕ} [fact (nat.prime p)] (n : ℕ) : norm (↑p ^ n) = ↑p ^ (-↑n) := sorry protected instance complete (p : ℕ) [fact (nat.prime p)] : cau_seq.is_complete (padic_int p) norm := cau_seq.is_complete.mk sorry end padic_int namespace padic_int theorem exists_pow_neg_lt (p : ℕ) [hp_prime : fact (nat.prime p)] {ε : ℝ} (hε : 0 < ε) : ∃ (k : ℕ), ↑p ^ (-↑k) < ε := sorry theorem exists_pow_neg_lt_rat (p : ℕ) [hp_prime : fact (nat.prime p)] {ε : ℚ} (hε : 0 < ε) : ∃ (k : ℕ), ↑p ^ (-↑k) < ε := sorry theorem norm_int_lt_one_iff_dvd {p : ℕ} [hp_prime : fact (nat.prime p)] (k : ℤ) : norm ↑k < 1 ↔ ↑p ∣ k := (fun (this : norm ↑k < 1 ↔ ↑p ∣ k) => eq.mpr (id (Eq._oldrec (Eq.refl (norm ↑k < 1 ↔ ↑p ∣ k)) (norm_int_cast_eq_padic_norm k))) this) (padic_norm_e.norm_int_lt_one_iff_dvd k) theorem norm_int_le_pow_iff_dvd {p : ℕ} [hp_prime : fact (nat.prime p)] {k : ℤ} {n : ℕ} : norm ↑k ≤ ↑p ^ (-↑n) ↔ ↑p ^ n ∣ k := sorry /-! ### Valuation on `ℤ_[p]` -/ /-- `padic_int.valuation` lifts the p-adic valuation on `ℚ` to `ℤ_[p]`. -/ def valuation {p : ℕ} [hp_prime : fact (nat.prime p)] (x : padic_int p) : ℤ := padic.valuation ↑x theorem norm_eq_pow_val {p : ℕ} [hp_prime : fact (nat.prime p)] {x : padic_int p} (hx : x ≠ 0) : norm x = ↑p ^ (-valuation x) := sorry @[simp] theorem valuation_zero {p : ℕ} [hp_prime : fact (nat.prime p)] : valuation 0 = 0 := padic.valuation_zero @[simp] theorem valuation_one {p : ℕ} [hp_prime : fact (nat.prime p)] : valuation 1 = 0 := padic.valuation_one @[simp] theorem valuation_p {p : ℕ} [hp_prime : fact (nat.prime p)] : valuation ↑p = 1 := sorry theorem valuation_nonneg {p : ℕ} [hp_prime : fact (nat.prime p)] (x : padic_int p) : 0 ≤ valuation x := sorry @[simp] theorem valuation_p_pow_mul {p : ℕ} [hp_prime : fact (nat.prime p)] (n : ℕ) (c : padic_int p) (hc : c ≠ 0) : valuation (↑p ^ n * c) = ↑n + valuation c := sorry /-! ### Units of `ℤ_[p]` -/ theorem mul_inv {p : ℕ} [hp_prime : fact (nat.prime p)] {z : padic_int p} : norm z = 1 → z * inv z = 1 := sorry theorem inv_mul {p : ℕ} [hp_prime : fact (nat.prime p)] {z : padic_int p} (hz : norm z = 1) : inv z * z = 1 := eq.mpr (id (Eq._oldrec (Eq.refl (inv z * z = 1)) (mul_comm (inv z) z))) (eq.mpr (id (Eq._oldrec (Eq.refl (z * inv z = 1)) (mul_inv hz))) (Eq.refl 1)) theorem is_unit_iff {p : ℕ} [hp_prime : fact (nat.prime p)] {z : padic_int p} : is_unit z ↔ norm z = 1 := sorry theorem norm_lt_one_add {p : ℕ} [hp_prime : fact (nat.prime p)] {z1 : padic_int p} {z2 : padic_int p} (hz1 : norm z1 < 1) (hz2 : norm z2 < 1) : norm (z1 + z2) < 1 := lt_of_le_of_lt (nonarchimedean z1 z2) (max_lt hz1 hz2) theorem norm_lt_one_mul {p : ℕ} [hp_prime : fact (nat.prime p)] {z1 : padic_int p} {z2 : padic_int p} (hz2 : norm z2 < 1) : norm (z1 * z2) < 1 := sorry @[simp] theorem mem_nonunits {p : ℕ} [hp_prime : fact (nat.prime p)] {z : padic_int p} : z ∈ nonunits (padic_int p) ↔ norm z < 1 := sorry /-- A `p`-adic number `u` with `∥u∥ = 1` is a unit of `ℤ_[p]`. -/ def mk_units {p : ℕ} [hp_prime : fact (nat.prime p)] {u : padic p} (h : norm u = 1) : units (padic_int p) := let z : padic_int p := { val := u, property := sorry }; units.mk z (inv z) (mul_inv h) (inv_mul h) @[simp] theorem mk_units_eq {p : ℕ} [hp_prime : fact (nat.prime p)] {u : padic p} (h : norm u = 1) : ↑↑(mk_units h) = u := rfl @[simp] theorem norm_units {p : ℕ} [hp_prime : fact (nat.prime p)] (u : units (padic_int p)) : norm ↑u = 1 := iff.mp is_unit_iff (eq.mpr (id (propext ((fun {M : Type} (u : units M) => iff_true_intro (is_unit_unit u)) u))) trivial) /-- `unit_coeff hx` is the unit `u` in the unique representation `x = u * p ^ n`. See `unit_coeff_spec`. -/ def unit_coeff {p : ℕ} [hp_prime : fact (nat.prime p)] {x : padic_int p} (hx : x ≠ 0) : units (padic_int p) := let u : padic p := ↑x * ↑p ^ (-valuation x); (fun (hu : norm u = 1) => mk_units hu) sorry @[simp] theorem unit_coeff_coe {p : ℕ} [hp_prime : fact (nat.prime p)] {x : padic_int p} (hx : x ≠ 0) : ↑(unit_coeff hx) = ↑x * ↑p ^ (-valuation x) := rfl theorem unit_coeff_spec {p : ℕ} [hp_prime : fact (nat.prime p)] {x : padic_int p} (hx : x ≠ 0) : x = ↑(unit_coeff hx) * ↑p ^ int.nat_abs (valuation x) := sorry /-! ### Various characterizations of open unit balls -/ theorem norm_le_pow_iff_le_valuation {p : ℕ} [hp_prime : fact (nat.prime p)] (x : padic_int p) (hx : x ≠ 0) (n : ℕ) : norm x ≤ ↑p ^ (-↑n) ↔ ↑n ≤ valuation x := sorry theorem mem_span_pow_iff_le_valuation {p : ℕ} [hp_prime : fact (nat.prime p)] (x : padic_int p) (hx : x ≠ 0) (n : ℕ) : x ∈ ideal.span (singleton (↑p ^ n)) ↔ ↑n ≤ valuation x := sorry theorem norm_le_pow_iff_mem_span_pow {p : ℕ} [hp_prime : fact (nat.prime p)] (x : padic_int p) (n : ℕ) : norm x ≤ ↑p ^ (-↑n) ↔ x ∈ ideal.span (singleton (↑p ^ n)) := sorry theorem norm_le_pow_iff_norm_lt_pow_add_one {p : ℕ} [hp_prime : fact (nat.prime p)] (x : padic_int p) (n : ℤ) : norm x ≤ ↑p ^ n ↔ norm x < ↑p ^ (n + 1) := sorry theorem norm_lt_pow_iff_norm_le_pow_sub_one {p : ℕ} [hp_prime : fact (nat.prime p)] (x : padic_int p) (n : ℤ) : norm x < ↑p ^ n ↔ norm x ≤ ↑p ^ (n - 1) := sorry theorem norm_lt_one_iff_dvd {p : ℕ} [hp_prime : fact (nat.prime p)] (x : padic_int p) : norm x < 1 ↔ ↑p ∣ x := sorry @[simp] theorem pow_p_dvd_int_iff {p : ℕ} [hp_prime : fact (nat.prime p)] (n : ℕ) (a : ℤ) : ↑p ^ n ∣ ↑a ↔ ↑p ^ n ∣ a := sorry /-! ### Discrete valuation ring -/ protected instance local_ring {p : ℕ} [hp_prime : fact (nat.prime p)] : local_ring (padic_int p) := local_of_nonunits_ideal zero_ne_one fun (x y : padic_int p) => eq.mpr (id (imp_congr_eq (propext mem_nonunits) (imp_congr_eq (propext mem_nonunits) (propext mem_nonunits)))) norm_lt_one_add theorem p_nonnunit {p : ℕ} [hp_prime : fact (nat.prime p)] : ↑p ∈ nonunits (padic_int p) := sorry theorem maximal_ideal_eq_span_p {p : ℕ} [hp_prime : fact (nat.prime p)] : local_ring.maximal_ideal (padic_int p) = ideal.span (singleton ↑p) := sorry theorem prime_p {p : ℕ} [hp_prime : fact (nat.prime p)] : prime ↑p := sorry theorem irreducible_p {p : ℕ} [hp_prime : fact (nat.prime p)] : irreducible ↑p := irreducible_of_prime prime_p protected instance discrete_valuation_ring {p : ℕ} [hp_prime : fact (nat.prime p)] : discrete_valuation_ring (padic_int p) := discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization (Exists.intro ↑p { left := irreducible_p, right := fun (x : padic_int p) (hx : x ≠ 0) => Exists.intro (int.nat_abs (valuation x)) (Exists.intro (unit_coeff hx) (eq.mpr (id (Eq._oldrec (Eq.refl (↑p ^ int.nat_abs (valuation x) * ↑(unit_coeff hx) = x)) (mul_comm (↑p ^ int.nat_abs (valuation x)) ↑(unit_coeff hx)))) (eq.mpr (id (Eq._oldrec (Eq.refl (↑(unit_coeff hx) * ↑p ^ int.nat_abs (valuation x) = x)) (Eq.symm (unit_coeff_spec hx)))) (Eq.refl x)))) }) theorem ideal_eq_span_pow_p {p : ℕ} [hp_prime : fact (nat.prime p)] {s : ideal (padic_int p)} (hs : s ≠ ⊥) : ∃ (n : ℕ), s = ideal.span (singleton (↑p ^ n)) := discrete_valuation_ring.ideal_eq_span_pow_irreducible hs irreducible_p
31849d6935741688b780aff9936126d24d962916
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/ctx.lean
803df0215f2ec55997f39ea3af04bf83b29ab885
[ "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
135
lean
open prod definition foo {A B : Type} (a1 a2 a3 a4 a5 a6 a7 a8 a9 a10 : nat) (b1 b2 b3 : bool) (h : A = B) (p1 p2 : A × B) : nat := _
4ae82d39110ab04232708bbdccd0206e33d04307
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/measure_theory/group/arithmetic.lean
cc22009185d5abf4283681d723fd72ea06dded3d
[ "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
28,178
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import measure_theory.measure.measure_space /-! # Typeclasses for measurability of operations In this file we define classes `has_measurable_mul` etc and prove dot-style lemmas (`measurable.mul`, `ae_measurable.mul` etc). For binary operations we define two typeclasses: - `has_measurable_mul` says that both left and right multiplication are measurable; - `has_measurable_mul₂` says that `λ p : α × α, p.1 * p.2` is measurable, and similarly for other binary operations. The reason for introducing these classes is that in case of topological space `α` equipped with the Borel `σ`-algebra, instances for `has_measurable_mul₂` etc require `α` to have a second countable topology. We define separate classes for `has_measurable_div`/`has_measurable_sub` because on some types (e.g., `ℕ`, `ℝ≥0∞`) division and/or subtraction are not defined as `a * b⁻¹` / `a + (-b)`. For instances relating, e.g., `has_continuous_mul` to `has_measurable_mul` see file `measure_theory.borel_space`. ## Implementation notes For the heuristics of `@[to_additive]` it is important that the type with a multiplication (or another multiplicative operations) is the first (implicit) argument of all declarations. ## Tags measurable function, arithmetic operator ## Todo * Uniformize the treatment of `pow` and `smul`. * Use `@[to_additive]` to send `has_measurable_pow` to `has_measurable_smul₂`. * This might require changing the definition (swapping the arguments in the function that is in the conclusion of `measurable_smul`.) -/ universes u v open_locale big_operators pointwise open measure_theory /-! ### Binary operations: `(+)`, `(*)`, `(-)`, `(/)` -/ /-- We say that a type `has_measurable_add` if `((+) c)` and `(+ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (+)` see `has_measurable_add₂`. -/ class has_measurable_add (M : Type*) [measurable_space M] [has_add M] : Prop := (measurable_const_add : ∀ c : M, measurable ((+) c)) (measurable_add_const : ∀ c : M, measurable (+ c)) /-- We say that a type `has_measurable_add` if `uncurry (+)` is a measurable functions. For a typeclass assuming measurability of `((+) c)` and `(+ c)` see `has_measurable_add`. -/ class has_measurable_add₂ (M : Type*) [measurable_space M] [has_add M] : Prop := (measurable_add : measurable (λ p : M × M, p.1 + p.2)) export has_measurable_add₂ (measurable_add) has_measurable_add (measurable_const_add measurable_add_const) /-- We say that a type `has_measurable_mul` if `((*) c)` and `(* c)` are measurable functions. For a typeclass assuming measurability of `uncurry (*)` see `has_measurable_mul₂`. -/ @[to_additive] class has_measurable_mul (M : Type*) [measurable_space M] [has_mul M] : Prop := (measurable_const_mul : ∀ c : M, measurable ((*) c)) (measurable_mul_const : ∀ c : M, measurable (* c)) /-- We say that a type `has_measurable_mul` if `uncurry (*)` is a measurable functions. For a typeclass assuming measurability of `((*) c)` and `(* c)` see `has_measurable_mul`. -/ @[to_additive has_measurable_add₂] class has_measurable_mul₂ (M : Type*) [measurable_space M] [has_mul M] : Prop := (measurable_mul : measurable (λ p : M × M, p.1 * p.2)) export has_measurable_mul₂ (measurable_mul) has_measurable_mul (measurable_const_mul measurable_mul_const) section mul variables {M α : Type*} [measurable_space M] [has_mul M] [measurable_space α] @[to_additive, measurability] lemma measurable.const_mul [has_measurable_mul M] {f : α → M} (hf : measurable f) (c : M) : measurable (λ x, c * f x) := (measurable_const_mul c).comp hf @[to_additive, measurability] lemma ae_measurable.const_mul [has_measurable_mul M] {f : α → M} {μ : measure α} (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c * f x) μ := (has_measurable_mul.measurable_const_mul c).comp_ae_measurable hf @[to_additive, measurability] lemma measurable.mul_const [has_measurable_mul M] {f : α → M} (hf : measurable f) (c : M) : measurable (λ x, f x * c) := (measurable_mul_const c).comp hf @[to_additive, measurability] lemma ae_measurable.mul_const [has_measurable_mul M] {f : α → M} {μ : measure α} (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, f x * c) μ := (measurable_mul_const c).comp_ae_measurable hf @[to_additive, measurability] lemma measurable.mul' [has_measurable_mul₂ M] {f g : α → M} (hf : measurable f) (hg : measurable g) : measurable (f * g) := measurable_mul.comp (hf.prod_mk hg) @[to_additive, measurability] lemma measurable.mul [has_measurable_mul₂ M] {f g : α → M} (hf : measurable f) (hg : measurable g) : measurable (λ a, f a * g a) := measurable_mul.comp (hf.prod_mk hg) @[to_additive, measurability] lemma ae_measurable.mul' [has_measurable_mul₂ M] {μ : measure α} {f g : α → M} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f * g) μ := measurable_mul.comp_ae_measurable (hf.prod_mk hg) @[to_additive, measurability] lemma ae_measurable.mul [has_measurable_mul₂ M] {μ : measure α} {f g : α → M} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a * g a) μ := measurable_mul.comp_ae_measurable (hf.prod_mk hg) @[priority 100, to_additive] instance has_measurable_mul₂.to_has_measurable_mul [has_measurable_mul₂ M] : has_measurable_mul M := ⟨λ c, measurable_const.mul measurable_id, λ c, measurable_id.mul measurable_const⟩ attribute [measurability] measurable.add' measurable.add ae_measurable.add ae_measurable.add' measurable.const_add ae_measurable.const_add measurable.add_const ae_measurable.add_const end mul /-- This class assumes that the map `β × γ → β` given by `(x, y) ↦ x ^ y` is measurable. -/ class has_measurable_pow (β γ : Type*) [measurable_space β] [measurable_space γ] [has_pow β γ] := (measurable_pow : measurable (λ p : β × γ, p.1 ^ p.2)) export has_measurable_pow (measurable_pow) instance has_measurable_mul.has_measurable_pow (M : Type*) [monoid M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_pow M ℕ := ⟨begin haveI : measurable_singleton_class ℕ := ⟨λ _, trivial⟩, refine measurable_from_prod_encodable (λ n, _), induction n with n ih, { simp [pow_zero, measurable_one] }, { simp only [pow_succ], exact measurable_id.mul ih } end⟩ section pow variables {β γ α : Type*} [measurable_space β] [measurable_space γ] [has_pow β γ] [has_measurable_pow β γ] [measurable_space α] @[measurability] lemma measurable.pow {f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : measurable (λ x, f x ^ g x) := measurable_pow.comp (hf.prod_mk hg) @[measurability] lemma ae_measurable.pow {μ : measure α} {f : α → β} {g : α → γ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, f x ^ g x) μ := measurable_pow.comp_ae_measurable (hf.prod_mk hg) @[measurability] lemma measurable.pow_const {f : α → β} (hf : measurable f) (c : γ) : measurable (λ x, f x ^ c) := hf.pow measurable_const @[measurability] lemma ae_measurable.pow_const {μ : measure α} {f : α → β} (hf : ae_measurable f μ) (c : γ) : ae_measurable (λ x, f x ^ c) μ := hf.pow ae_measurable_const @[measurability] lemma measurable.const_pow {f : α → γ} (hf : measurable f) (c : β) : measurable (λ x, c ^ f x) := measurable_const.pow hf @[measurability] lemma ae_measurable.const_pow {μ : measure α} {f : α → γ} (hf : ae_measurable f μ) (c : β) : ae_measurable (λ x, c ^ f x) μ := ae_measurable_const.pow hf end pow /-- We say that a type `has_measurable_sub` if `(λ x, c - x)` and `(λ x, x - c)` are measurable functions. For a typeclass assuming measurability of `uncurry (-)` see `has_measurable_sub₂`. -/ class has_measurable_sub (G : Type*) [measurable_space G] [has_sub G] : Prop := (measurable_const_sub : ∀ c : G, measurable (λ x, c - x)) (measurable_sub_const : ∀ c : G, measurable (λ x, x - c)) /-- We say that a type `has_measurable_sub` if `uncurry (-)` is a measurable functions. For a typeclass assuming measurability of `((-) c)` and `(- c)` see `has_measurable_sub`. -/ class has_measurable_sub₂ (G : Type*) [measurable_space G] [has_sub G] : Prop := (measurable_sub : measurable (λ p : G × G, p.1 - p.2)) export has_measurable_sub₂ (measurable_sub) /-- We say that a type `has_measurable_div` if `((/) c)` and `(/ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (/)` see `has_measurable_div₂`. -/ @[to_additive] class has_measurable_div (G₀: Type*) [measurable_space G₀] [has_div G₀] : Prop := (measurable_const_div : ∀ c : G₀, measurable ((/) c)) (measurable_div_const : ∀ c : G₀, measurable (/ c)) /-- We say that a type `has_measurable_div` if `uncurry (/)` is a measurable functions. For a typeclass assuming measurability of `((/) c)` and `(/ c)` see `has_measurable_div`. -/ @[to_additive has_measurable_sub₂] class has_measurable_div₂ (G₀: Type*) [measurable_space G₀] [has_div G₀] : Prop := (measurable_div : measurable (λ p : G₀× G₀, p.1 / p.2)) export has_measurable_div₂ (measurable_div) section div variables {G α : Type*} [measurable_space G] [has_div G] [measurable_space α] @[to_additive, measurability] lemma measurable.const_div [has_measurable_div G] {f : α → G} (hf : measurable f) (c : G) : measurable (λ x, c / f x) := (has_measurable_div.measurable_const_div c).comp hf @[to_additive, measurability] lemma ae_measurable.const_div [has_measurable_div G] {f : α → G} {μ : measure α} (hf : ae_measurable f μ) (c : G) : ae_measurable (λ x, c / f x) μ := (has_measurable_div.measurable_const_div c).comp_ae_measurable hf @[to_additive, measurability] lemma measurable.div_const [has_measurable_div G] {f : α → G} (hf : measurable f) (c : G) : measurable (λ x, f x / c) := (has_measurable_div.measurable_div_const c).comp hf @[to_additive, measurability] lemma ae_measurable.div_const [has_measurable_div G] {f : α → G} {μ : measure α} (hf : ae_measurable f μ) (c : G) : ae_measurable (λ x, f x / c) μ := (has_measurable_div.measurable_div_const c).comp_ae_measurable hf @[to_additive, measurability] lemma measurable.div' [has_measurable_div₂ G] {f g : α → G} (hf : measurable f) (hg : measurable g) : measurable (f / g) := measurable_div.comp (hf.prod_mk hg) @[to_additive, measurability] lemma measurable.div [has_measurable_div₂ G] {f g : α → G} (hf : measurable f) (hg : measurable g) : measurable (λ a, f a / g a) := measurable_div.comp (hf.prod_mk hg) @[to_additive, measurability] lemma ae_measurable.div' [has_measurable_div₂ G] {f g : α → G} {μ : measure α} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f / g) μ := measurable_div.comp_ae_measurable (hf.prod_mk hg) @[to_additive, measurability] lemma ae_measurable.div [has_measurable_div₂ G] {f g : α → G} {μ : measure α} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a / g a) μ := measurable_div.comp_ae_measurable (hf.prod_mk hg) @[priority 100, to_additive] instance has_measurable_div₂.to_has_measurable_div [has_measurable_div₂ G] : has_measurable_div G := ⟨λ c, measurable_const.div measurable_id, λ c, measurable_id.div measurable_const⟩ attribute [measurability] measurable.sub measurable.sub' ae_measurable.sub ae_measurable.sub' measurable.const_sub ae_measurable.const_sub measurable.sub_const ae_measurable.sub_const @[measurability] lemma measurable_set_eq_fun {E} [measurable_space E] [add_group E] [measurable_singleton_class E] [has_measurable_sub₂ E] {f g : α → E} (hf : measurable f) (hg : measurable g) : measurable_set {x | f x = g x} := begin suffices h_set_eq : {x : α | f x = g x} = {x | (f-g) x = (0 : E)}, { rw h_set_eq, exact (hf.sub hg) measurable_set_eq, }, ext, simp_rw [set.mem_set_of_eq, pi.sub_apply, sub_eq_zero], end lemma ae_eq_trim_of_measurable {α E} {m m0 : measurable_space α} {μ : measure α} [measurable_space E] [add_group E] [measurable_singleton_class E] [has_measurable_sub₂ E] (hm : m ≤ m0) {f g : α → E} (hf : @measurable _ _ m _ f) (hg : @measurable _ _ m _ g) (hfg : f =ᵐ[μ] g) : f =ᶠ[@measure.ae α m (μ.trim hm)] g := begin rwa [filter.eventually_eq, ae_iff, trim_measurable_set_eq hm _], exact (@measurable_set.compl α _ m (@measurable_set_eq_fun α m E _ _ _ _ _ _ hf hg)), end end div /-- We say that a type `has_measurable_neg` if `x ↦ -x` is a measurable function. -/ class has_measurable_neg (G : Type*) [has_neg G] [measurable_space G] : Prop := (measurable_neg : measurable (has_neg.neg : G → G)) /-- We say that a type `has_measurable_inv` if `x ↦ x⁻¹` is a measurable function. -/ @[to_additive] class has_measurable_inv (G : Type*) [has_inv G] [measurable_space G] : Prop := (measurable_inv : measurable (has_inv.inv : G → G)) export has_measurable_inv (measurable_inv) has_measurable_neg (measurable_neg) @[priority 100, to_additive] instance has_measurable_div_of_mul_inv (G : Type*) [measurable_space G] [div_inv_monoid G] [has_measurable_mul G] [has_measurable_inv G] : has_measurable_div G := { measurable_const_div := λ c, by { convert (measurable_inv.const_mul c), ext1, apply div_eq_mul_inv }, measurable_div_const := λ c, by { convert (measurable_id.mul_const c⁻¹), ext1, apply div_eq_mul_inv } } section inv variables {G α : Type*} [has_inv G] [measurable_space G] [has_measurable_inv G] [measurable_space α] @[to_additive, measurability] lemma measurable.inv {f : α → G} (hf : measurable f) : measurable (λ x, (f x)⁻¹) := measurable_inv.comp hf @[to_additive, measurability] lemma ae_measurable.inv {f : α → G} {μ : measure α} (hf : ae_measurable f μ) : ae_measurable (λ x, (f x)⁻¹) μ := measurable_inv.comp_ae_measurable hf attribute [measurability] measurable.neg ae_measurable.neg @[to_additive] lemma measurable_set.inv {s : set G} (hs : measurable_set s) : measurable_set s⁻¹ := measurable_inv hs @[simp, to_additive] lemma measurable_inv_iff {G : Type*} [group G] [measurable_space G] [has_measurable_inv G] {f : α → G} : measurable (λ x, (f x)⁻¹) ↔ measurable f := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ @[simp, to_additive] lemma ae_measurable_inv_iff {G : Type*} [group G] [measurable_space G] [has_measurable_inv G] {f : α → G} {μ : measure α} : ae_measurable (λ x, (f x)⁻¹) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_inv] using h.inv, λ h, h.inv⟩ @[simp] lemma measurable_inv_iff₀ {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [has_measurable_inv G₀] {f : α → G₀} : measurable (λ x, (f x)⁻¹) ↔ measurable f := ⟨λ h, by simpa only [inv_inv₀] using h.inv, λ h, h.inv⟩ @[simp] lemma ae_measurable_inv_iff₀ {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [has_measurable_inv G₀] {f : α → G₀} {μ : measure α} : ae_measurable (λ x, (f x)⁻¹) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_inv₀] using h.inv, λ h, h.inv⟩ end inv /- There is something extremely strange here: copy-pasting the proof of this lemma in the proof of `has_measurable_zpow` fails, while `pp.all` does not show any difference in the goal. Keep it as a separate lemmas as a workaround. -/ private lemma has_measurable_zpow_aux (G : Type u) [div_inv_monoid G] [measurable_space G] [has_measurable_mul₂ G] [has_measurable_inv G] (k : ℕ) : measurable (λ (x : G), x ^(-[1+ k])) := begin simp_rw [zpow_neg_succ_of_nat], exact (measurable_id.pow_const (k + 1)).inv end instance has_measurable_zpow (G : Type u) [div_inv_monoid G] [measurable_space G] [has_measurable_mul₂ G] [has_measurable_inv G] : has_measurable_pow G ℤ := begin letI : measurable_singleton_class ℤ := ⟨λ _, trivial⟩, constructor, refine measurable_from_prod_encodable (λ n, _), dsimp, apply int.cases_on n, { simpa using measurable_id.pow_const }, { exact has_measurable_zpow_aux G } end @[priority 100, to_additive] instance has_measurable_div₂_of_mul_inv (G : Type*) [measurable_space G] [div_inv_monoid G] [has_measurable_mul₂ G] [has_measurable_inv G] : has_measurable_div₂ G := ⟨by { simp only [div_eq_mul_inv], exact measurable_fst.mul measurable_snd.inv }⟩ /-- We say that the action of `M` on `α` `has_measurable_vadd` if for each `c` the map `x ↦ c +ᵥ x` is a measurable function and for each `x` the map `c ↦ c +ᵥ x` is a measurable function. -/ class has_measurable_vadd (M α : Type*) [has_vadd M α] [measurable_space M] [measurable_space α] : Prop := (measurable_const_vadd : ∀ c : M, measurable ((+ᵥ) c : α → α)) (measurable_vadd_const : ∀ x : α, measurable (λ c : M, c +ᵥ x)) /-- We say that the action of `M` on `α` `has_measurable_smul` if for each `c` the map `x ↦ c • x` is a measurable function and for each `x` the map `c ↦ c • x` is a measurable function. -/ @[to_additive] class has_measurable_smul (M α : Type*) [has_scalar M α] [measurable_space M] [measurable_space α] : Prop := (measurable_const_smul : ∀ c : M, measurable ((•) c : α → α)) (measurable_smul_const : ∀ x : α, measurable (λ c : M, c • x)) /-- We say that the action of `M` on `α` `has_measurable_vadd₂` if the map `(c, x) ↦ c +ᵥ x` is a measurable function. -/ class has_measurable_vadd₂ (M α : Type*) [has_vadd M α] [measurable_space M] [measurable_space α] : Prop := (measurable_vadd : measurable (function.uncurry (+ᵥ) : M × α → α)) /-- We say that the action of `M` on `α` `has_measurable_smul₂` if the map `(c, x) ↦ c • x` is a measurable function. -/ @[to_additive has_measurable_vadd₂] class has_measurable_smul₂ (M α : Type*) [has_scalar M α] [measurable_space M] [measurable_space α] : Prop := (measurable_smul : measurable (function.uncurry (•) : M × α → α)) export has_measurable_smul (measurable_const_smul measurable_smul_const) has_measurable_smul₂ (measurable_smul) export has_measurable_vadd (measurable_const_vadd measurable_vadd_const) has_measurable_vadd₂ (measurable_vadd) @[to_additive] instance has_measurable_smul_of_mul (M : Type*) [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_smul M M := ⟨measurable_id.const_mul, measurable_id.mul_const⟩ @[to_additive] instance has_measurable_smul₂_of_mul (M : Type*) [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_smul₂ M M := ⟨measurable_mul⟩ section smul variables {M β α : Type*} [measurable_space M] [measurable_space β] [has_scalar M β] [measurable_space α] @[measurability, to_additive] lemma measurable.smul [has_measurable_smul₂ M β] {f : α → M} {g : α → β} (hf : measurable f) (hg : measurable g) : measurable (λ x, f x • g x) := measurable_smul.comp (hf.prod_mk hg) @[measurability, to_additive] lemma ae_measurable.smul [has_measurable_smul₂ M β] {f : α → M} {g : α → β} {μ : measure α} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, f x • g x) μ := has_measurable_smul₂.measurable_smul.comp_ae_measurable (hf.prod_mk hg) @[priority 100, to_additive] instance has_measurable_smul₂.to_has_measurable_smul [has_measurable_smul₂ M β] : has_measurable_smul M β := ⟨λ c, measurable_const.smul measurable_id, λ y, measurable_id.smul measurable_const⟩ variables [has_measurable_smul M β] {μ : measure α} @[measurability, to_additive] lemma measurable.smul_const {f : α → M} (hf : measurable f) (y : β) : measurable (λ x, f x • y) := (has_measurable_smul.measurable_smul_const y).comp hf @[measurability, to_additive] lemma ae_measurable.smul_const {f : α → M} (hf : ae_measurable f μ) (y : β) : ae_measurable (λ x, f x • y) μ := (has_measurable_smul.measurable_smul_const y).comp_ae_measurable hf @[measurability, to_additive] lemma measurable.const_smul' {f : α → β} (hf : measurable f) (c : M) : measurable (λ x, c • f x) := (has_measurable_smul.measurable_const_smul c).comp hf @[measurability, to_additive] lemma measurable.const_smul {f : α → β} (hf : measurable f) (c : M) : measurable (c • f) := hf.const_smul' c @[measurability, to_additive] lemma ae_measurable.const_smul' {f : α → β} (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c • f x) μ := (has_measurable_smul.measurable_const_smul c).comp_ae_measurable hf @[measurability, to_additive] lemma ae_measurable.const_smul {f : α → β} (hf : ae_measurable f μ) (c : M) : ae_measurable (c • f) μ := hf.const_smul' c end smul section mul_action variables {M β α : Type*} [measurable_space M] [measurable_space β] [monoid M] [mul_action M β] [has_measurable_smul M β] [measurable_space α] {f : α → β} {μ : measure α} variables {G : Type*} [group G] [measurable_space G] [mul_action G β] [has_measurable_smul G β] @[to_additive] lemma measurable_const_smul_iff (c : G) : measurable (λ x, c • f x) ↔ measurable f := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ @[to_additive] lemma ae_measurable_const_smul_iff (c : G) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := ⟨λ h, by simpa only [inv_smul_smul] using h.const_smul' c⁻¹, λ h, h.const_smul c⟩ @[to_additive] instance : measurable_space (units M) := measurable_space.comap (coe : units M → M) ‹_› @[to_additive] instance units.has_measurable_smul : has_measurable_smul (units M) β := { measurable_const_smul := λ c, (measurable_const_smul (c : M) : _), measurable_smul_const := λ x, (measurable_smul_const x : measurable (λ c : M, c • x)).comp measurable_space.le_map_comap, } @[to_additive] lemma is_unit.measurable_const_smul_iff {c : M} (hc : is_unit c) : measurable (λ x, c • f x) ↔ measurable f := let ⟨u, hu⟩ := hc in hu ▸ measurable_const_smul_iff u @[to_additive] lemma is_unit.ae_measurable_const_smul_iff {c : M} (hc : is_unit c) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := let ⟨u, hu⟩ := hc in hu ▸ ae_measurable_const_smul_iff u variables {G₀ : Type*} [group_with_zero G₀] [measurable_space G₀] [mul_action G₀ β] [has_measurable_smul G₀ β] lemma measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : measurable (λ x, c • f x) ↔ measurable f := (is_unit.mk0 c hc).measurable_const_smul_iff lemma ae_measurable_const_smul_iff₀ {c : G₀} (hc : c ≠ 0) : ae_measurable (λ x, c • f x) μ ↔ ae_measurable f μ := (is_unit.mk0 c hc).ae_measurable_const_smul_iff end mul_action /-! ### Opposite monoid -/ section opposite open opposite instance {α : Type*} [h : measurable_space α] : measurable_space αᵒᵖ := measurable_space.map op h lemma measurable_op {α : Type*} [measurable_space α] : measurable (op : α → αᵒᵖ) := λ s, id lemma measurable_unop {α : Type*} [measurable_space α] : measurable (unop : αᵒᵖ → α) := λ s, id instance {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_mul Mᵒᵖ := ⟨λ c, measurable_op.comp (measurable_unop.mul_const _), λ c, measurable_op.comp (measurable_unop.const_mul _)⟩ instance {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_mul₂ Mᵒᵖ := ⟨measurable_op.comp ((measurable_unop.comp measurable_snd).mul (measurable_unop.comp measurable_fst))⟩ instance has_measurable_smul_opposite_of_mul {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul M] : has_measurable_smul Mᵒᵖ M := ⟨λ c, measurable_mul_const (unop c), λ x, measurable_unop.const_mul x⟩ instance has_measurable_smul₂_opposite_of_mul {M : Type*} [has_mul M] [measurable_space M] [has_measurable_mul₂ M] : has_measurable_smul₂ Mᵒᵖ M := ⟨measurable_snd.mul (measurable_unop.comp measurable_fst)⟩ end opposite /-! ### Big operators: `∏` and `∑` -/ section monoid variables {M α : Type*} [monoid M] [measurable_space M] [has_measurable_mul₂ M] [measurable_space α] @[to_additive, measurability] lemma list.measurable_prod' (l : list (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable l.prod := begin induction l with f l ihl, { exact measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[to_additive, measurability] lemma list.ae_measurable_prod' {μ : measure α} (l : list (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable l.prod μ := begin induction l with f l ihl, { exact ae_measurable_one }, rw [list.forall_mem_cons] at hl, rw [list.prod_cons], exact hl.1.mul (ihl hl.2) end @[to_additive, measurability] lemma list.measurable_prod (l : list (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable (λ x, (l.map (λ f : α → M, f x)).prod) := by simpa only [← pi.list_prod_apply] using l.measurable_prod' hl @[to_additive, measurability] lemma list.ae_measurable_prod {μ : measure α} (l : list (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable (λ x, (l.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.list_prod_apply] using l.ae_measurable_prod' hl end monoid section comm_monoid variables {M ι α : Type*} [comm_monoid M] [measurable_space M] [has_measurable_mul₂ M] [measurable_space α] @[to_additive, measurability] lemma multiset.measurable_prod' (l : multiset (α → M)) (hl : ∀ f ∈ l, measurable f) : measurable l.prod := by { rcases l with ⟨l⟩, simpa using l.measurable_prod' (by simpa using hl) } @[to_additive, measurability] lemma multiset.ae_measurable_prod' {μ : measure α} (l : multiset (α → M)) (hl : ∀ f ∈ l, ae_measurable f μ) : ae_measurable l.prod μ := by { rcases l with ⟨l⟩, simpa using l.ae_measurable_prod' (by simpa using hl) } @[to_additive, measurability] lemma multiset.measurable_prod (s : multiset (α → M)) (hs : ∀ f ∈ s, measurable f) : measurable (λ x, (s.map (λ f : α → M, f x)).prod) := by simpa only [← pi.multiset_prod_apply] using s.measurable_prod' hs @[to_additive, measurability] lemma multiset.ae_measurable_prod {μ : measure α} (s : multiset (α → M)) (hs : ∀ f ∈ s, ae_measurable f μ) : ae_measurable (λ x, (s.map (λ f : α → M, f x)).prod) μ := by simpa only [← pi.multiset_prod_apply] using s.ae_measurable_prod' hs @[to_additive, measurability] lemma finset.measurable_prod' {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, measurable (f i)) : measurable (∏ i in s, f i) := finset.prod_induction _ _ (λ _ _, measurable.mul) (@measurable_one M _ _ _ _) hf @[to_additive, measurability] lemma finset.measurable_prod {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, measurable (f i)) : measurable (λ a, ∏ i in s, f i a) := by simpa only [← finset.prod_apply] using s.measurable_prod' hf @[to_additive, measurability] lemma finset.ae_measurable_prod' {μ : measure α} {f : ι → α → M} (s : finset ι) (hf : ∀i ∈ s, ae_measurable (f i) μ) : ae_measurable (∏ i in s, f i) μ := multiset.ae_measurable_prod' _ $ λ g hg, let ⟨i, hi, hg⟩ := multiset.mem_map.1 hg in (hg ▸ hf _ hi) @[to_additive, measurability] lemma finset.ae_measurable_prod {f : ι → α → M} {μ : measure α} (s : finset ι) (hf : ∀i ∈ s, ae_measurable (f i) μ) : ae_measurable (λ a, ∏ i in s, f i a) μ := by simpa only [← finset.prod_apply] using s.ae_measurable_prod' hf end comm_monoid attribute [measurability] list.measurable_sum' list.ae_measurable_sum' list.measurable_sum list.ae_measurable_sum multiset.measurable_sum' multiset.ae_measurable_sum' multiset.measurable_sum multiset.ae_measurable_sum finset.measurable_sum' finset.ae_measurable_sum' finset.measurable_sum finset.ae_measurable_sum
1ed167dd4e0b445bc45e97d8b941af429fe77014
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/pkg/user_ext/UserExt/Tst1.lean
9c15cbfac203c0dbf4ab8c3c7372620565c1c19f
[ "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
185
lean
import UserExt.FooExt import UserExt.BlaExt set_option trace.myDebug true insert_foo hello set_option trace.myDebug false insert_foo world show_foo_set insert_bla abc show_bla_set
5919e966a5c0f12aaf872d7bf2bf5f1530970c64
3c9dc4ea6cc92e02634ef557110bde9eae393338
/src/Lean/Elab/Log.lean
5a3288bc58c04f8bf01060d9e69edc05b462f231
[ "Apache-2.0" ]
permissive
shingtaklam1324/lean4
3d7efe0c8743a4e33d3c6f4adbe1300df2e71492
351285a2e8ad0cef37af05851cfabf31edfb5970
refs/heads/master
1,676,827,679,740
1,610,462,623,000
1,610,552,340,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,088
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Elab.Util import Lean.Util.Sorry import Lean.Elab.Exception namespace Lean.Elab class MonadFileMap (m : Type → Type) where getFileMap : m FileMap export MonadFileMap (getFileMap) class MonadLog (m : Type → Type) extends MonadFileMap m where getRef : m Syntax getFileName : m String logMessage : Message → m Unit export MonadLog (getFileName logMessage) instance (m n) [MonadLog m] [MonadLift m n] : MonadLog n := { getRef := liftM (MonadLog.getRef : m _), getFileMap := liftM (getFileMap : m _), getFileName := liftM (getFileName : m _), logMessage := fun msg => liftM (logMessage msg : m _ ) } variables {m : Type → Type} [Monad m] [MonadLog m] [AddMessageContext m] def getRefPos : m String.Pos := do let ref ← MonadLog.getRef return ref.getPos.getD 0 def getRefPosition : m Position := do let fileMap ← getFileMap return fileMap.toPosition (← getRefPos) def logAt (ref : Syntax) (msgData : MessageData) (severity : MessageSeverity := MessageSeverity.error): m Unit := unless severity == MessageSeverity.error && msgData.hasSyntheticSorry do let ref := replaceRef ref (← MonadLog.getRef) let pos := ref.getPos.getD 0 let endPos := ref.getTailPos.getD pos let fileMap ← getFileMap let msgData ← addMessageContext msgData logMessage { fileName := (← getFileName), pos := fileMap.toPosition pos, endPos := fileMap.toPosition endPos, data := msgData, severity := severity } def logErrorAt (ref : Syntax) (msgData : MessageData) : m Unit := logAt ref msgData MessageSeverity.error def logWarningAt (ref : Syntax) (msgData : MessageData) : m Unit := logAt ref msgData MessageSeverity.warning def logInfoAt (ref : Syntax) (msgData : MessageData) : m Unit := logAt ref msgData MessageSeverity.information def log (msgData : MessageData) (severity : MessageSeverity := MessageSeverity.error): m Unit := do let ref ← MonadLog.getRef logAt ref msgData severity def logError (msgData : MessageData) : m Unit := log msgData MessageSeverity.error def logWarning (msgData : MessageData) : m Unit := log msgData MessageSeverity.warning def logInfo (msgData : MessageData) : m Unit := log msgData MessageSeverity.information def logException [MonadLiftT IO m] (ex : Exception) : m Unit := do match ex with | Exception.error ref msg => logErrorAt ref msg | Exception.internal id _ => unless isAbortExceptionId id do let name ← id.getName logError m!"internal exception: {name}" def logTrace (cls : Name) (msgData : MessageData) : m Unit := do logInfo (MessageData.tagged cls msgData) @[inline] def trace [MonadOptions m] (cls : Name) (msg : Unit → MessageData) : m Unit := do if checkTraceOption (← getOptions) cls then logTrace cls (msg ()) def logDbgTrace [MonadOptions m] (msg : MessageData) : m Unit := do trace `Elab.debug fun _ => msg end Lean.Elab
85b577320f46bbab1aaabeb447e2f9710cba1bbf
4fa161becb8ce7378a709f5992a594764699e268
/src/analysis/normed_space/banach.lean
82497290c86c08950eb34dcef8c9ce7fbb5bc82d
[ "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
12,025
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.metric_space.baire import analysis.normed_space.operator_norm /-! # Banach open mapping theorem This file contains the Banach open mapping theorem, i.e., the fact that a bijective bounded linear map between Banach spaces has a bounded inverse. -/ open function metric set filter finset open_locale classical topological_space big_operators variables {𝕜 : Type*} [nondiscrete_normed_field 𝕜] {E : Type*} [normed_group E] [normed_space 𝕜 E] {F : Type*} [normed_group F] [normed_space 𝕜 F] (f : E →L[𝕜] F) include 𝕜 variable [complete_space F] /-- First step of the proof of the Banach open mapping theorem (using completeness of `F`): by Baire's theorem, there exists a ball in `E` whose image closure has nonempty interior. Rescaling everything, it follows that any `y ∈ F` is arbitrarily well approached by images of elements of norm at most `C * ∥y∥`. For further use, we will only need such an element whose image is within distance `∥y∥/2` of `y`, to apply an iterative process. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] lemma exists_approx_preimage_norm_le (surj : surjective f) : ∃C ≥ 0, ∀y, ∃x, dist (f x) y ≤ 1/2 * ∥y∥ ∧ ∥x∥ ≤ C * ∥y∥ := begin have A : (⋃n:ℕ, closure (f '' (ball 0 n))) = univ, { refine subset.antisymm (subset_univ _) (λy hy, _), rcases surj y with ⟨x, hx⟩, rcases exists_nat_gt (∥x∥) with ⟨n, hn⟩, refine mem_Union.2 ⟨n, subset_closure _⟩, refine (mem_image _ _ _).2 ⟨x, ⟨_, hx⟩⟩, rwa [mem_ball, dist_eq_norm, sub_zero] }, have : ∃(n:ℕ) y ε, 0 < ε ∧ ball y ε ⊆ closure (f '' (ball 0 n)) := nonempty_interior_of_Union_of_closed (λn, is_closed_closure) A, rcases this with ⟨n, a, ε, ⟨εpos, H⟩⟩, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, refine ⟨(ε/2)⁻¹ * ∥c∥ * 2 * n, _, λy, _⟩, { refine mul_nonneg (mul_nonneg (mul_nonneg _ (norm_nonneg _)) (by norm_num)) _, refine inv_nonneg.2 (div_nonneg' (le_of_lt εpos) (by norm_num)), exact nat.cast_nonneg n }, { by_cases hy : y = 0, { use 0, simp [hy] }, { rcases rescale_to_shell hc (half_pos εpos) hy with ⟨d, hd, ydle, leyd, dinv⟩, let δ := ∥d∥ * ∥y∥/4, have δpos : 0 < δ := div_pos (mul_pos (norm_pos_iff.2 hd) (norm_pos_iff.2 hy)) (by norm_num), have : a + d • y ∈ ball a ε, by simp [dist_eq_norm, lt_of_le_of_lt ydle (half_lt_self εpos)], rcases metric.mem_closure_iff.1 (H this) _ δpos with ⟨z₁, z₁im, h₁⟩, rcases (mem_image _ _ _).1 z₁im with ⟨x₁, hx₁, xz₁⟩, rw ← xz₁ at h₁, rw [mem_ball, dist_eq_norm, sub_zero] at hx₁, have : a ∈ ball a ε, by { simp, exact εpos }, rcases metric.mem_closure_iff.1 (H this) _ δpos with ⟨z₂, z₂im, h₂⟩, rcases (mem_image _ _ _).1 z₂im with ⟨x₂, hx₂, xz₂⟩, rw ← xz₂ at h₂, rw [mem_ball, dist_eq_norm, sub_zero] at hx₂, let x := x₁ - x₂, have I : ∥f x - d • y∥ ≤ 2 * δ := calc ∥f x - d • y∥ = ∥f x₁ - (a + d • y) - (f x₂ - a)∥ : by { congr' 1, simp only [x, f.map_sub], abel } ... ≤ ∥f x₁ - (a + d • y)∥ + ∥f x₂ - a∥ : norm_sub_le _ _ ... ≤ δ + δ : begin apply add_le_add, { rw [← dist_eq_norm, dist_comm], exact le_of_lt h₁ }, { rw [← dist_eq_norm, dist_comm], exact le_of_lt h₂ } end ... = 2 * δ : (two_mul _).symm, have J : ∥f (d⁻¹ • x) - y∥ ≤ 1/2 * ∥y∥ := calc ∥f (d⁻¹ • x) - y∥ = ∥d⁻¹ • f x - (d⁻¹ * d) • y∥ : by rwa [f.map_smul _, inv_mul_cancel, one_smul] ... = ∥d⁻¹ • (f x - d • y)∥ : by rw [mul_smul, smul_sub] ... = ∥d∥⁻¹ * ∥f x - d • y∥ : by rw [norm_smul, normed_field.norm_inv] ... ≤ ∥d∥⁻¹ * (2 * δ) : begin apply mul_le_mul_of_nonneg_left I, rw inv_nonneg, exact norm_nonneg _ end ... = (∥d∥⁻¹ * ∥d∥) * ∥y∥ /2 : by { simp only [δ], ring } ... = ∥y∥/2 : by { rw [inv_mul_cancel, one_mul], simp [norm_eq_zero, hd] } ... = (1/2) * ∥y∥ : by ring, rw ← dist_eq_norm at J, have 𝕜 : ∥d⁻¹ • x∥ ≤ (ε / 2)⁻¹ * ∥c∥ * 2 * ↑n * ∥y∥ := calc ∥d⁻¹ • x∥ = ∥d∥⁻¹ * ∥x₁ - x₂∥ : by rw [norm_smul, normed_field.norm_inv] ... ≤ ((ε / 2)⁻¹ * ∥c∥ * ∥y∥) * (n + n) : begin refine mul_le_mul dinv _ (norm_nonneg _) _, { exact le_trans (norm_sub_le _ _) (add_le_add (le_of_lt hx₁) (le_of_lt hx₂)) }, { apply mul_nonneg (mul_nonneg _ (norm_nonneg _)) (norm_nonneg _), exact inv_nonneg.2 (le_of_lt (half_pos εpos)) } end ... = (ε / 2)⁻¹ * ∥c∥ * 2 * ↑n * ∥y∥ : by ring, exact ⟨d⁻¹ • x, J, 𝕜⟩ } }, end variable [complete_space E] /-- The Banach open mapping theorem: if a bounded linear map between Banach spaces is onto, then any point has a preimage with controlled norm. -/ @[nolint ge_or_gt] -- see Note [nolint_ge] theorem exists_preimage_norm_le (surj : surjective f) : ∃C > 0, ∀y, ∃x, f x = y ∧ ∥x∥ ≤ C * ∥y∥ := begin obtain ⟨C, C0, hC⟩ := exists_approx_preimage_norm_le f surj, /- Second step of the proof: starting from `y`, we want an exact preimage of `y`. Let `g y` be the approximate preimage of `y` given by the first step, and `h y = y - f(g y)` the part that has no preimage yet. We will iterate this process, taking the approximate preimage of `h y`, leaving only `h^2 y` without preimage yet, and so on. Let `u n` be the approximate preimage of `h^n y`. Then `u` is a converging series, and by design the sum of the series is a preimage of `y`. This uses completeness of `E`. -/ choose g hg using hC, let h := λy, y - f (g y), have hle : ∀y, ∥h y∥ ≤ (1/2) * ∥y∥, { assume y, rw [← dist_eq_norm, dist_comm], exact (hg y).1 }, refine ⟨2 * C + 1, by linarith, λy, _⟩, have hnle : ∀n:ℕ, ∥(h^[n]) y∥ ≤ (1/2)^n * ∥y∥, { assume n, induction n with n IH, { simp only [one_div_eq_inv, nat.nat_zero_eq_zero, one_mul, iterate_zero_apply, pow_zero] }, { rw [iterate_succ'], apply le_trans (hle _) _, rw [pow_succ, mul_assoc], apply mul_le_mul_of_nonneg_left IH, norm_num } }, let u := λn, g((h^[n]) y), have ule : ∀n, ∥u n∥ ≤ (1/2)^n * (C * ∥y∥), { assume n, apply le_trans (hg _).2 _, calc C * ∥(h^[n]) y∥ ≤ C * ((1/2)^n * ∥y∥) : mul_le_mul_of_nonneg_left (hnle n) C0 ... = (1 / 2) ^ n * (C * ∥y∥) : by ring }, have sNu : summable (λn, ∥u n∥), { refine summable_of_nonneg_of_le (λn, norm_nonneg _) ule _, exact summable.mul_right _ (summable_geometric_of_lt_1 (by norm_num) (by norm_num)) }, have su : summable u := summable_of_summable_norm sNu, let x := tsum u, have x_ineq : ∥x∥ ≤ (2 * C + 1) * ∥y∥ := calc ∥x∥ ≤ (∑'n, ∥u n∥) : norm_tsum_le_tsum_norm sNu ... ≤ (∑'n, (1/2)^n * (C * ∥y∥)) : tsum_le_tsum ule sNu (summable.mul_right _ summable_geometric_two) ... = (∑'n, (1/2)^n) * (C * ∥y∥) : by { rw tsum_mul_right, exact summable_geometric_two } ... = 2 * (C * ∥y∥) : by rw tsum_geometric_two ... = 2 * C * ∥y∥ + 0 : by rw [add_zero, mul_assoc] ... ≤ 2 * C * ∥y∥ + ∥y∥ : add_le_add (le_refl _) (norm_nonneg _) ... = (2 * C + 1) * ∥y∥ : by ring, have fsumeq : ∀n:ℕ, f(∑ i in finset.range n, u i) = y - (h^[n]) y, { assume n, induction n with n IH, { simp [f.map_zero] }, { rw [sum_range_succ, f.map_add, IH, iterate_succ'], simp [u, h, sub_eq_add_neg, add_comm, add_left_comm] } }, have : tendsto (λn, ∑ i in range n, u i) at_top (𝓝 x) := su.has_sum.tendsto_sum_nat, have L₁ : tendsto (λn, f(∑ i in range n, u i)) at_top (𝓝 (f x)) := (f.continuous.tendsto _).comp this, simp only [fsumeq] at L₁, have L₂ : tendsto (λn, y - (h^[n]) y) at_top (𝓝 (y - 0)), { refine tendsto_const_nhds.sub _, rw tendsto_iff_norm_tendsto_zero, simp only [sub_zero], refine squeeze_zero (λ_, norm_nonneg _) hnle _, have : 0 = 0 * ∥y∥, by rw zero_mul, rw this, refine tendsto.mul _ tendsto_const_nhds, exact tendsto_pow_at_top_nhds_0_of_lt_1 (by norm_num) (by norm_num) }, have feq : f x = y - 0, { apply tendsto_nhds_unique _ L₁ L₂, simp }, rw sub_zero at feq, exact ⟨x, feq, x_ineq⟩ end /-- The Banach open mapping theorem: a surjective bounded linear map between Banach spaces is open. -/ theorem open_mapping (surj : surjective f) : is_open_map f := begin assume s hs, rcases exists_preimage_norm_le f surj with ⟨C, Cpos, hC⟩, refine is_open_iff.2 (λy yfs, _), rcases mem_image_iff_bex.1 yfs with ⟨x, xs, fxy⟩, rcases is_open_iff.1 hs x xs with ⟨ε, εpos, hε⟩, refine ⟨ε/C, div_pos εpos Cpos, λz hz, _⟩, rcases hC (z-y) with ⟨w, wim, wnorm⟩, have : f (x + w) = z, by { rw [f.map_add, wim, fxy, add_sub_cancel'_right] }, rw ← this, have : x + w ∈ ball x ε := calc dist (x+w) x = ∥w∥ : by { rw dist_eq_norm, simp } ... ≤ C * ∥z - y∥ : wnorm ... < C * (ε/C) : begin apply mul_lt_mul_of_pos_left _ Cpos, rwa [mem_ball, dist_eq_norm] at hz, end ... = ε : mul_div_cancel' _ (ne_of_gt Cpos), exact set.mem_image_of_mem _ (hε this) end namespace linear_equiv /-- If a bounded linear map is a bijection, then its inverse is also a bounded linear map. -/ theorem continuous_symm (e : E ≃ₗ[𝕜] F) (h : continuous e) : continuous e.symm := begin intros s hs, rw [← e.image_eq_preimage], rw [← e.coe_coe] at h ⊢, exact open_mapping ⟨↑e, h⟩ e.surjective s hs end /-- Associating to a linear equivalence between Banach spaces a continuous linear equivalence when the direct map is continuous, thanks to the Banach open mapping theorem that ensures that the inverse map is also continuous. -/ def to_continuous_linear_equiv_of_continuous (e : E ≃ₗ[𝕜] F) (h : continuous e) : E ≃L[𝕜] F := { continuous_to_fun := h, continuous_inv_fun := e.continuous_symm h, ..e } @[simp] lemma coe_fn_to_continuous_linear_equiv_of_continuous (e : E ≃ₗ[𝕜] F) (h : continuous e) : ⇑(e.to_continuous_linear_equiv_of_continuous h) = e := rfl @[simp] lemma coe_fn_to_continuous_linear_equiv_of_continuous_symm (e : E ≃ₗ[𝕜] F) (h : continuous e) : ⇑(e.to_continuous_linear_equiv_of_continuous h).symm = e.symm := rfl end linear_equiv namespace continuous_linear_equiv /-- Convert a bijective continuous linear map `f : E →L[𝕜] F` between two Banach spaces to a continuous linear equivalence. -/ noncomputable def of_bijective (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) : E ≃L[𝕜] F := (linear_equiv.of_bijective ↑f hinj hsurj).to_continuous_linear_equiv_of_continuous f.continuous @[simp] lemma coe_fn_of_bijective (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) : ⇑(of_bijective f hinj hsurj) = f := rfl @[simp] lemma of_bijective_symm_apply_apply (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) (x : E) : (of_bijective f hinj hsurj).symm (f x) = x := (of_bijective f hinj hsurj).symm_apply_apply x @[simp] lemma of_bijective_apply_symm_apply (f : E →L[𝕜] F) (hinj : f.ker = ⊥) (hsurj : f.range = ⊤) (y : F) : f ((of_bijective f hinj hsurj).symm y) = y := (of_bijective f hinj hsurj).apply_symm_apply y end continuous_linear_equiv
0a3955c8a26c99567d0854dcab65af8f89d38fad
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/algebraic_geometry/Scheme.lean
ebbad3a6575d985c6f0f0bb0f52a57e5b156ece2
[ "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
3,757
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebraic_geometry.Spec /-! # The category of schemes A scheme is a locally ringed space such that every point is contained in some open set where there is an isomorphism of presheaves between the restriction to that open set, and the structure sheaf of `Spec R`, for some commutative ring `R`. A morphism of schemes is just a morphism of the underlying locally ringed spaces. -/ open topological_space open category_theory open Top open opposite namespace algebraic_geometry /-- We define `Scheme` as a `X : LocallyRingedSpace`, along with a proof that every point has an open neighbourhood `U` so that that the restriction of `X` to `U` is isomorphic, as a space with a presheaf of commutative rings, to `Spec.PresheafedSpace R` for some `R : CommRing`. (Note we're not asking in the definition that this is an isomorphism as locally ringed spaces, although that is a consequence.) -/ structure Scheme extends X : LocallyRingedSpace := (local_affine : ∀ x : X, ∃ (U : open_nhds x) (R : CommRing), nonempty (X.to_PresheafedSpace.restrict _ U.open_embedding ≅ Spec.PresheafedSpace R)) -- PROJECT -- In fact, we can make the isomorphism `i` above an isomorphism in `LocallyRingedSpace`. -- However this is a consequence of the above definition, and not necessary for defining schemes. -- We haven't done this yet because we haven't shown that you can restrict a `LocallyRingedSpace` -- along an open embedding. -- We can do this already for `SheafedSpace` (as above), but we need to know that -- the stalks of the restriction are still local rings, which we follow if we knew that -- the stalks didn't change. -- This will follow if we define cofinal functors, and show precomposing with a cofinal functor -- doesn't change colimits, because open neighbourhoods of `x` within `U` are cofinal in -- all open neighbourhoods of `x`. namespace Scheme /-- Every `Scheme` is a `LocallyRingedSpace`. -/ -- (This parent projection is apparently not automatically generated because -- we used the `extends X : LocallyRingedSpace` syntax.) def to_LocallyRingedSpace (S : Scheme) : LocallyRingedSpace := { ..S } /-- `Spec R` as a `Scheme`. -/ noncomputable def Spec (R : CommRing) : Scheme := { local_affine := λ x, ⟨⟨⊤, trivial⟩, R, ⟨(Spec.PresheafedSpace R).restrict_top_iso⟩⟩, .. Spec.LocallyRingedSpace R } /-- The empty scheme, as `Spec 0`. -/ noncomputable def empty : Scheme := Spec (CommRing.of punit) noncomputable instance : has_emptyc Scheme := ⟨empty⟩ noncomputable instance : inhabited Scheme := ⟨∅⟩ /-- Schemes are a full subcategory of locally ringed spaces. -/ instance : category Scheme := induced_category.category Scheme.to_LocallyRingedSpace /-- The global sections, notated Gamma. -/ def Γ : Schemeᵒᵖ ⥤ CommRing := (induced_functor Scheme.to_LocallyRingedSpace).op ⋙ LocallyRingedSpace.Γ lemma Γ_def : Γ = (induced_functor Scheme.to_LocallyRingedSpace).op ⋙ LocallyRingedSpace.Γ := rfl @[simp] lemma Γ_obj (X : Schemeᵒᵖ) : Γ.obj X = (unop X).presheaf.obj (op ⊤) := rfl lemma Γ_obj_op (X : Scheme) : Γ.obj (op X) = X.presheaf.obj (op ⊤) := rfl @[simp] lemma Γ_map {X Y : Schemeᵒᵖ} (f : X ⟶ Y) : Γ.map f = f.unop.1.c.app (op ⊤) ≫ (unop Y).presheaf.map (opens.le_map_top _ _).op := rfl lemma Γ_map_op {X Y : Scheme} (f : X ⟶ Y) : Γ.map f.op = f.1.c.app (op ⊤) ≫ X.presheaf.map (opens.le_map_top _ _).op := rfl -- PROJECTS: -- 1. Make `Spec` a functor. -- 2. Construct `Spec ≫ Γ ≅ functor.id _`. -- 3. Adjunction between `Γ` and `Spec`. -- end Scheme end algebraic_geometry
b84a78b74009ae2dde4ff28d186bf853d446ab67
9028d228ac200bbefe3a711342514dd4e4458bff
/src/algebra/archimedean.lean
5887a1a4740e287333dc616c330908934a8651a3
[ "Apache-2.0" ]
permissive
mcncm/mathlib
8d25099344d9d2bee62822cb9ed43aa3e09fa05e
fde3d78cadeec5ef827b16ae55664ef115e66f57
refs/heads/master
1,672,743,316,277
1,602,618,514,000
1,602,618,514,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,791
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro Archimedean groups and fields. -/ import algebra.field_power import data.rat variables {α : Type*} /-- An ordered additive commutative monoid is called `archimedean` if for any two elements `x`, `y` such that `0 < y` there exists a natural number `n` such that `x ≤ n •ℕ y`. -/ class archimedean (α) [ordered_add_comm_monoid α] : Prop := (arch : ∀ (x : α) {y}, 0 < y → ∃ n : ℕ, x ≤ n •ℕ y) namespace decidable_linear_ordered_add_comm_group variables [decidable_linear_ordered_add_comm_group α] [archimedean α] /-- An archimedean decidable linearly ordered `add_comm_group` has a version of the floor: for `a > 0`, any `g` in the group lies between some two consecutive multiples of `a`. -/ lemma exists_int_smul_near_of_pos {a : α} (ha : 0 < a) (g : α) : ∃ k, k •ℤ a ≤ g ∧ g < (k + 1) •ℤ a := begin let s : set ℤ := {n : ℤ | n •ℤ a ≤ g}, obtain ⟨k, hk : -g ≤ k •ℕ a⟩ := archimedean.arch (-g) ha, have h_ne : s.nonempty := ⟨-k, by simpa using neg_le_neg hk⟩, obtain ⟨k, hk⟩ := archimedean.arch g ha, have h_bdd : ∀ n ∈ s, n ≤ (k : ℤ) := λ n hn, (gsmul_le_gsmul_iff ha).mp (le_trans hn hk : n •ℤ a ≤ k •ℤ a), obtain ⟨m, hm, hm'⟩ := int.exists_greatest_of_bdd ⟨k, h_bdd⟩ h_ne, refine ⟨m, hm, _⟩, by_contra H, linarith [hm' _ $ not_lt.mp H] end lemma exists_int_smul_near_of_pos' {a : α} (ha : 0 < a) (g : α) : ∃ k, 0 ≤ g - k •ℤ a ∧ g - k •ℤ a < a := begin obtain ⟨k, h1, h2⟩ := exists_int_smul_near_of_pos ha g, refine ⟨k, sub_nonneg.mpr h1, _⟩, have : g < k •ℤ a + 1 •ℤ a, by rwa ← add_gsmul a k 1, simpa [sub_lt_iff_lt_add'] end end decidable_linear_ordered_add_comm_group theorem exists_nat_gt [linear_ordered_semiring α] [archimedean α] (x : α) : ∃ n : ℕ, x < n := let ⟨n, h⟩ := archimedean.arch x zero_lt_one in ⟨n+1, lt_of_le_of_lt (by rwa ← nsmul_one) (nat.cast_lt.2 (nat.lt_succ_self _))⟩ theorem exists_nat_ge [linear_ordered_semiring α] [archimedean α] (x : α) : ∃ n : ℕ, x ≤ n := (exists_nat_gt x).imp $ λ n, le_of_lt lemma add_one_pow_unbounded_of_pos [linear_ordered_semiring α] [archimedean α] (x : α) {y : α} (hy : 0 < y) : ∃ n : ℕ, x < (y + 1) ^ n := let ⟨n, h⟩ := archimedean.arch x hy in ⟨n, calc x ≤ n •ℕ y : h ... < 1 + n •ℕ y : lt_one_add _ ... ≤ (1 + y) ^ n : one_add_mul_le_pow' (mul_nonneg (le_of_lt hy) (le_of_lt hy)) (le_of_lt $ lt_trans hy (lt_one_add y)) _ ... = (y + 1) ^ n : by rw [add_comm]⟩ section linear_ordered_ring variables [linear_ordered_ring α] [archimedean α] lemma pow_unbounded_of_one_lt (x : α) {y : α} (hy1 : 1 < y) : ∃ n : ℕ, x < y ^ n := sub_add_cancel y 1 ▸ add_one_pow_unbounded_of_pos _ (sub_pos.2 hy1) /-- Every x greater than or equal to 1 is between two successive natural-number powers of every y greater than one. -/ lemma exists_nat_pow_near {x : α} {y : α} (hx : 1 ≤ x) (hy : 1 < y) : ∃ n : ℕ, y ^ n ≤ x ∧ x < y ^ (n + 1) := have h : ∃ n : ℕ, x < y ^ n, from pow_unbounded_of_one_lt _ hy, by classical; exact let n := nat.find h in have hn : x < y ^ n, from nat.find_spec h, have hnp : 0 < n, from nat.pos_iff_ne_zero.2 (λ hn0, by rw [hn0, pow_zero] at hn; exact (not_le_of_gt hn hx)), have hnsp : nat.pred n + 1 = n, from nat.succ_pred_eq_of_pos hnp, have hltn : nat.pred n < n, from nat.pred_lt (ne_of_gt hnp), ⟨nat.pred n, le_of_not_lt (nat.find_min h hltn), by rwa hnsp⟩ theorem exists_int_gt (x : α) : ∃ n : ℤ, x < n := let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa ← coe_coe⟩ theorem exists_int_lt (x : α) : ∃ n : ℤ, (n : α) < x := let ⟨n, h⟩ := exists_int_gt (-x) in ⟨-n, by rw int.cast_neg; exact neg_lt.1 h⟩ theorem exists_floor (x : α) : ∃ (fl : ℤ), ∀ (z : ℤ), z ≤ fl ↔ (z : α) ≤ x := begin haveI := classical.prop_decidable, have : ∃ (ub : ℤ), (ub:α) ≤ x ∧ ∀ (z : ℤ), (z:α) ≤ x → z ≤ ub := int.exists_greatest_of_bdd (let ⟨n, hn⟩ := exists_int_gt x in ⟨n, λ z h', int.cast_le.1 $ le_trans h' $ le_of_lt hn⟩) (let ⟨n, hn⟩ := exists_int_lt x in ⟨n, le_of_lt hn⟩), refine this.imp (λ fl h z, _), cases h with h₁ h₂, exact ⟨λ h, le_trans (int.cast_le.2 h) h₁, h₂ z⟩, end end linear_ordered_ring section linear_ordered_field /-- Every positive x is between two successive integer powers of another y greater than one. This is the same as `exists_int_pow_near'`, but with ≤ and < the other way around. -/ lemma exists_int_pow_near [discrete_linear_ordered_field α] [archimedean α] {x : α} {y : α} (hx : 0 < x) (hy : 1 < y) : ∃ n : ℤ, y ^ n ≤ x ∧ x < y ^ (n + 1) := by classical; exact let ⟨N, hN⟩ := pow_unbounded_of_one_lt x⁻¹ hy in have he: ∃ m : ℤ, y ^ m ≤ x, from ⟨-N, le_of_lt (by rw [(fpow_neg y (↑N))]; exact (inv_lt hx (lt_trans (inv_pos.2 hx) hN)).1 hN)⟩, let ⟨M, hM⟩ := pow_unbounded_of_one_lt x hy in have hb: ∃ b : ℤ, ∀ m, y ^ m ≤ x → m ≤ b, from ⟨M, λ m hm, le_of_not_lt (λ hlt, not_lt_of_ge (fpow_le_of_le (le_of_lt hy) (le_of_lt hlt)) (lt_of_le_of_lt hm hM))⟩, let ⟨n, hn₁, hn₂⟩ := int.exists_greatest_of_bdd hb he in ⟨n, hn₁, lt_of_not_ge (λ hge, not_le_of_gt (int.lt_succ _) (hn₂ _ hge))⟩ /-- Every positive x is between two successive integer powers of another y greater than one. This is the same as `exists_int_pow_near`, but with ≤ and < the other way around. -/ lemma exists_int_pow_near' [discrete_linear_ordered_field α] [archimedean α] {x : α} {y : α} (hx : 0 < x) (hy : 1 < y) : ∃ n : ℤ, y ^ n < x ∧ x ≤ y ^ (n + 1) := let ⟨m, hle, hlt⟩ := exists_int_pow_near (inv_pos.2 hx) hy in have hyp : 0 < y, from lt_trans zero_lt_one hy, ⟨-(m+1), by rwa [fpow_neg, inv_lt (fpow_pos_of_pos hyp _) hx], by rwa [neg_add, neg_add_cancel_right, fpow_neg, le_inv hx (fpow_pos_of_pos hyp _)]⟩ variables [linear_ordered_field α] [floor_ring α] lemma sub_floor_div_mul_nonneg (x : α) {y : α} (hy : 0 < y) : 0 ≤ x - ⌊x / y⌋ * y := begin conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm}, rw ← sub_mul, exact mul_nonneg (sub_nonneg.2 (floor_le _)) (le_of_lt hy) end lemma sub_floor_div_mul_lt (x : α) {y : α} (hy : 0 < y) : x - ⌊x / y⌋ * y < y := sub_lt_iff_lt_add.2 begin conv in y {rw ← one_mul y}, conv in x {rw ← div_mul_cancel x (ne_of_lt hy).symm}, rw ← add_mul, exact (mul_lt_mul_right hy).2 (by rw add_comm; exact lt_floor_add_one _), end end linear_ordered_field instance : archimedean ℕ := ⟨λ n m m0, ⟨n, by simpa only [mul_one, nat.nsmul_eq_mul] using nat.mul_le_mul_left n m0⟩⟩ instance : archimedean ℤ := ⟨λ n m m0, ⟨n.to_nat, le_trans (int.le_to_nat _) $ by simpa only [nsmul_eq_mul, int.nat_cast_eq_coe_nat, zero_add, mul_one] using mul_le_mul_of_nonneg_left (int.add_one_le_iff.2 m0) (int.coe_zero_le n.to_nat)⟩⟩ /-- A linear ordered archimedean ring is a floor ring. This is not an `instance` because in some cases we have a computable `floor` function. -/ noncomputable def archimedean.floor_ring (α) [linear_ordered_ring α] [archimedean α] : floor_ring α := { floor := λ x, classical.some (exists_floor x), le_floor := λ z x, classical.some_spec (exists_floor x) z } section linear_ordered_field variables [linear_ordered_field α] theorem archimedean_iff_nat_lt : archimedean α ↔ ∀ x : α, ∃ n : ℕ, x < n := ⟨@exists_nat_gt α _, λ H, ⟨λ x y y0, (H (x / y)).imp $ λ n h, le_of_lt $ by rwa [div_lt_iff y0, ← nsmul_eq_mul] at h⟩⟩ theorem archimedean_iff_nat_le : archimedean α ↔ ∀ x : α, ∃ n : ℕ, x ≤ n := archimedean_iff_nat_lt.trans ⟨λ H x, (H x).imp $ λ _, le_of_lt, λ H x, let ⟨n, h⟩ := H x in ⟨n+1, lt_of_le_of_lt h (nat.cast_lt.2 (lt_add_one _))⟩⟩ theorem exists_rat_gt [archimedean α] (x : α) : ∃ q : ℚ, x < q := let ⟨n, h⟩ := exists_nat_gt x in ⟨n, by rwa rat.cast_coe_nat⟩ theorem archimedean_iff_rat_lt : archimedean α ↔ ∀ x : α, ∃ q : ℚ, x < q := ⟨@exists_rat_gt α _, λ H, archimedean_iff_nat_lt.2 $ λ x, let ⟨q, h⟩ := H x in ⟨nat_ceil q, lt_of_lt_of_le h $ by simpa only [rat.cast_coe_nat] using (@rat.cast_le α _ _ _).2 (le_nat_ceil _)⟩⟩ theorem archimedean_iff_rat_le : archimedean α ↔ ∀ x : α, ∃ q : ℚ, x ≤ q := archimedean_iff_rat_lt.trans ⟨λ H x, (H x).imp $ λ _, le_of_lt, λ H x, let ⟨n, h⟩ := H x in ⟨n+1, lt_of_le_of_lt h (rat.cast_lt.2 (lt_add_one _))⟩⟩ variable [archimedean α] theorem exists_rat_lt (x : α) : ∃ q : ℚ, (q : α) < x := let ⟨n, h⟩ := exists_int_lt x in ⟨n, by rwa rat.cast_coe_int⟩ theorem exists_rat_btwn {x y : α} (h : x < y) : ∃ q : ℚ, x < q ∧ (q:α) < y := begin cases exists_nat_gt (y - x)⁻¹ with n nh, cases exists_floor (x * n) with z zh, refine ⟨(z + 1 : ℤ) / n, _⟩, have n0 := nat.cast_pos.1 (lt_trans (inv_pos.2 (sub_pos.2 h)) nh), have n0' := (@nat.cast_pos α _ _).2 n0, rw [rat.cast_div_of_ne_zero, rat.cast_coe_nat, rat.cast_coe_int, div_lt_iff n0'], refine ⟨(lt_div_iff n0').2 $ (lt_iff_lt_of_le_iff_le (zh _)).1 (lt_add_one _), _⟩, rw [int.cast_add, int.cast_one], refine lt_of_le_of_lt (add_le_add_right ((zh _).1 (le_refl _)) _) _, rwa [← lt_sub_iff_add_lt', ← sub_mul, ← div_lt_iff' (sub_pos.2 h), one_div], { rw [rat.coe_int_denom, nat.cast_one], exact one_ne_zero }, { intro H, rw [rat.coe_nat_num, ← coe_coe, nat.cast_eq_zero] at H, subst H, cases n0 }, { rw [rat.coe_nat_denom, nat.cast_one], exact one_ne_zero } end theorem exists_nat_one_div_lt {ε : α} (hε : 0 < ε) : ∃ n : ℕ, 1 / (n + 1: α) < ε := begin cases exists_nat_gt (1/ε) with n hn, use n, rw [div_lt_iff, ← div_lt_iff' hε], { apply hn.trans, simp [zero_lt_one] }, { exact n.cast_add_one_pos } end theorem exists_pos_rat_lt {x : α} (x0 : 0 < x) : ∃ q : ℚ, 0 < q ∧ (q : α) < x := by simpa only [rat.cast_pos] using exists_rat_btwn x0 include α @[simp] theorem rat.cast_floor (x : ℚ) : by haveI := archimedean.floor_ring α; exact ⌊(x:α)⌋ = ⌊x⌋ := begin haveI := archimedean.floor_ring α, apply le_antisymm, { rw [le_floor, ← @rat.cast_le α, rat.cast_coe_int], apply floor_le }, { rw [le_floor, ← rat.cast_coe_int, rat.cast_le], apply floor_le } end end linear_ordered_field section variables [discrete_linear_ordered_field α] /-- `round` rounds a number to the nearest integer. `round (1 / 2) = 1` -/ def round [floor_ring α] (x : α) : ℤ := ⌊x + 1 / 2⌋ lemma abs_sub_round [floor_ring α] (x : α) : abs (x - round x) ≤ 1 / 2 := begin rw [round, abs_sub_le_iff], have := floor_le (x + 1 / 2), have := lt_floor_add_one (x + 1 / 2), split; linarith end variable [archimedean α] theorem exists_rat_near (x : α) {ε : α} (ε0 : 0 < ε) : ∃ q : ℚ, abs (x - q) < ε := let ⟨q, h₁, h₂⟩ := exists_rat_btwn $ lt_trans ((sub_lt_self_iff x).2 ε0) ((lt_add_iff_pos_left x).2 ε0) in ⟨q, abs_sub_lt_iff.2 ⟨sub_lt.1 h₁, sub_lt_iff_lt_add.2 h₂⟩⟩ instance : archimedean ℚ := archimedean_iff_rat_le.2 $ λ q, ⟨q, by rw rat.cast_id⟩ @[simp] theorem rat.cast_round (x : ℚ) : by haveI := archimedean.floor_ring α; exact round (x:α) = round x := have ((x + (1 : ℚ) / (2 : ℚ) : ℚ) : α) = x + 1 / 2, by simp, by rw [round, round, ← this, rat.cast_floor] end
bdfb408b54340f03fff7aba69fe3052267385af5
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/topology/locally_constant/basic.lean
3a46a0fce005e1c5a4d5fc84ec3800f186b0aee2
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
19,430
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 topology.subset_properties import topology.connected import topology.continuous_function.basic import algebra.indicator_function import tactic.tfae import tactic.fin_cases /-! # Locally constant functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file sets up the theory of locally constant function from a topological space to a type. ## Main definitions and constructions * `is_locally_constant f` : a map `f : X → Y` where `X` is a topological space is locally constant if every set in `Y` has an open preimage. * `locally_constant X Y` : the type of locally constant maps from `X` to `Y` * `locally_constant.map` : push-forward of locally constant maps * `locally_constant.comap` : pull-back of locally constant maps -/ variables {X Y Z α : Type*} [topological_space X] open set filter open_locale topology /-- A function between topological spaces is locally constant if the preimage of any set is open. -/ def is_locally_constant (f : X → Y) : Prop := ∀ s : set Y, is_open (f ⁻¹' s) namespace is_locally_constant protected lemma tfae (f : X → Y) : tfae [is_locally_constant f, ∀ x, ∀ᶠ x' in 𝓝 x, f x' = f x, ∀ x, is_open {x' | f x' = f x}, ∀ y, is_open (f ⁻¹' {y}), ∀ x, ∃ (U : set X) (hU : is_open U) (hx : x ∈ U), ∀ x' ∈ U, f x' = f x] := begin tfae_have : 1 → 4, from λ h y, h {y}, tfae_have : 4 → 3, from λ h x, h (f x), tfae_have : 3 → 2, from λ h x, is_open.mem_nhds (h x) rfl, tfae_have : 2 → 5, { intros h x, rcases mem_nhds_iff.1 (h x) with ⟨U, eq, hU, hx⟩, exact ⟨U, hU, hx, eq⟩ }, tfae_have : 5 → 1, { intros h s, refine is_open_iff_forall_mem_open.2 (λ x hx, _), rcases h x with ⟨U, hU, hxU, eq⟩, exact ⟨U, λ x' hx', mem_preimage.2 $ (eq x' hx').symm ▸ hx, hU, hxU⟩ }, tfae_finish end @[nontriviality] lemma of_discrete [discrete_topology X] (f : X → Y) : is_locally_constant f := λ s, is_open_discrete _ lemma is_open_fiber {f : X → Y} (hf : is_locally_constant f) (y : Y) : is_open {x | f x = y} := hf {y} lemma is_closed_fiber {f : X → Y} (hf : is_locally_constant f) (y : Y) : is_closed {x | f x = y} := ⟨hf {y}ᶜ⟩ lemma is_clopen_fiber {f : X → Y} (hf : is_locally_constant f) (y : Y) : is_clopen {x | f x = y} := ⟨is_open_fiber hf _, is_closed_fiber hf _⟩ lemma iff_exists_open (f : X → Y) : is_locally_constant f ↔ ∀ x, ∃ (U : set X) (hU : is_open U) (hx : x ∈ U), ∀ x' ∈ U, f x' = f x := (is_locally_constant.tfae f).out 0 4 lemma iff_eventually_eq (f : X → Y) : is_locally_constant f ↔ ∀ x, ∀ᶠ y in 𝓝 x, f y = f x := (is_locally_constant.tfae f).out 0 1 lemma exists_open {f : X → Y} (hf : is_locally_constant f) (x : X) : ∃ (U : set X) (hU : is_open U) (hx : x ∈ U), ∀ x' ∈ U, f x' = f x := (iff_exists_open f).1 hf x protected lemma eventually_eq {f : X → Y} (hf : is_locally_constant f) (x : X) : ∀ᶠ y in 𝓝 x, f y = f x := (iff_eventually_eq f).1 hf x protected lemma continuous [topological_space Y] {f : X → Y} (hf : is_locally_constant f) : continuous f := ⟨λ U hU, hf _⟩ lemma iff_continuous {_ : topological_space Y} [discrete_topology Y] (f : X → Y) : is_locally_constant f ↔ continuous f := ⟨is_locally_constant.continuous, λ h s, h.is_open_preimage s (is_open_discrete _)⟩ lemma of_constant (f : X → Y) (h : ∀ x y, f x = f y) : is_locally_constant f := (iff_eventually_eq f).2 $ λ x, eventually_of_forall $ λ x', h _ _ lemma const (y : Y) : is_locally_constant (function.const X y) := of_constant _ $ λ _ _, rfl lemma comp {f : X → Y} (hf : is_locally_constant f) (g : Y → Z) : is_locally_constant (g ∘ f) := λ s, by { rw set.preimage_comp, exact hf _ } lemma prod_mk {Y'} {f : X → Y} {f' : X → Y'} (hf : is_locally_constant f) (hf' : is_locally_constant f') : is_locally_constant (λ x, (f x, f' x)) := (iff_eventually_eq _).2 $ λ x, (hf.eventually_eq x).mp $ (hf'.eventually_eq x).mono $ λ x' hf' hf, prod.ext hf hf' lemma comp₂ {Y₁ Y₂ Z : Type*} {f : X → Y₁} {g : X → Y₂} (hf : is_locally_constant f) (hg : is_locally_constant g) (h : Y₁ → Y₂ → Z) : is_locally_constant (λ x, h (f x) (g x)) := (hf.prod_mk hg).comp (λ x : Y₁ × Y₂, h x.1 x.2) lemma comp_continuous [topological_space Y] {g : Y → Z} {f : X → Y} (hg : is_locally_constant g) (hf : continuous f) : is_locally_constant (g ∘ f) := λ s, by { rw set.preimage_comp, exact hf.is_open_preimage _ (hg _) } /-- A locally constant function is constant on any preconnected set. -/ lemma apply_eq_of_is_preconnected {f : X → Y} (hf : is_locally_constant f) {s : set X} (hs : is_preconnected s) {x y : X} (hx : x ∈ s) (hy : y ∈ s) : f x = f y := begin let U := f ⁻¹' {f y}, suffices : x ∉ Uᶜ, from not_not.1 this, intro hxV, specialize hs U Uᶜ (hf {f y}) (hf {f y}ᶜ) _ ⟨y, ⟨hy, rfl⟩⟩ ⟨x, ⟨hx, hxV⟩⟩, { simp only [union_compl_self, subset_univ] }, { simpa only [inter_empty, not_nonempty_empty, inter_compl_self] using hs } end lemma apply_eq_of_preconnected_space [preconnected_space X] {f : X → Y} (hf : is_locally_constant f) (x y : X) : f x = f y := hf.apply_eq_of_is_preconnected is_preconnected_univ trivial trivial lemma eq_const [preconnected_space X] {f : X → Y} (hf : is_locally_constant f) (x : X) : f = function.const X (f x) := funext $ λ y, hf.apply_eq_of_preconnected_space y x lemma exists_eq_const [preconnected_space X] [nonempty Y] {f : X → Y} (hf : is_locally_constant f) : ∃ y, f = function.const X y := begin casesI is_empty_or_nonempty X, { exact ⟨classical.arbitrary Y, funext $ h.elim⟩ }, { exact ⟨f (classical.arbitrary X), hf.eq_const _⟩ }, end lemma iff_is_const [preconnected_space X] {f : X → Y} : is_locally_constant f ↔ ∀ x y, f x = f y := ⟨λ h x y, h.apply_eq_of_is_preconnected is_preconnected_univ trivial trivial, of_constant _⟩ lemma range_finite [compact_space X] {f : X → Y} (hf : is_locally_constant f) : (set.range f).finite := begin letI : topological_space Y := ⊥, haveI := discrete_topology_bot Y, rw @iff_continuous X Y ‹_› ‹_› at hf, exact (is_compact_range hf).finite_of_discrete end @[to_additive] lemma one [has_one Y] : is_locally_constant (1 : X → Y) := const 1 @[to_additive] lemma inv [has_inv Y] ⦃f : X → Y⦄ (hf : is_locally_constant f) : is_locally_constant f⁻¹ := hf.comp (λ x, x⁻¹) @[to_additive] lemma mul [has_mul Y] ⦃f g : X → Y⦄ (hf : is_locally_constant f) (hg : is_locally_constant g) : is_locally_constant (f * g) := hf.comp₂ hg (*) @[to_additive] lemma div [has_div Y] ⦃f g : X → Y⦄ (hf : is_locally_constant f) (hg : is_locally_constant g) : is_locally_constant (f / g) := hf.comp₂ hg (/) /-- If a composition of a function `f` followed by an injection `g` is locally constant, then the locally constant property descends to `f`. -/ lemma desc {α β : Type*} (f : X → α) (g : α → β) (h : is_locally_constant (g ∘ f)) (inj : function.injective g) : is_locally_constant f := begin rw (is_locally_constant.tfae f).out 0 3, intros a, have : f ⁻¹' {a} = (g ∘ f) ⁻¹' { g a }, { ext x, simp only [mem_singleton_iff, function.comp_app, mem_preimage], exact ⟨λ h, by rw h, λ h, inj h⟩ }, rw this, apply h, end lemma of_constant_on_connected_components [locally_connected_space X] {f : X → Y} (h : ∀ x, ∀ y ∈ connected_component x, f y = f x) : is_locally_constant f := begin rw iff_exists_open, exact λ x, ⟨connected_component x, is_open_connected_component, mem_connected_component, h x⟩, end lemma of_constant_on_preconnected_clopens [locally_connected_space X] {f : X → Y} (h : ∀ U : set X, is_preconnected U → is_clopen U → ∀ x ∈ U, ∀ y ∈ U, f y = f x) : is_locally_constant f := of_constant_on_connected_components (λ x, h (connected_component x) is_preconnected_connected_component is_clopen_connected_component x mem_connected_component) end is_locally_constant /-- A (bundled) locally constant function from a topological space `X` to a type `Y`. -/ @[protect_proj] structure locally_constant (X Y : Type*) [topological_space X] := (to_fun : X → Y) (is_locally_constant : is_locally_constant to_fun) namespace locally_constant instance [inhabited Y] : inhabited (locally_constant X Y) := ⟨⟨_, is_locally_constant.const default⟩⟩ instance : has_coe_to_fun (locally_constant X Y) (λ _, X → Y) := ⟨locally_constant.to_fun⟩ initialize_simps_projections locally_constant (to_fun → apply) @[simp] lemma to_fun_eq_coe (f : locally_constant X Y) : f.to_fun = f := rfl @[simp] lemma coe_mk (f : X → Y) (h) : ⇑(⟨f, h⟩ : locally_constant X Y) = f := rfl theorem congr_fun {f g : locally_constant X Y} (h : f = g) (x : X) : f x = g x := congr_arg (λ h : locally_constant X Y, h x) h theorem congr_arg (f : locally_constant X Y) {x y : X} (h : x = y) : f x = f y := congr_arg (λ x : X, f x) h theorem coe_injective : @function.injective (locally_constant X Y) (X → Y) coe_fn | ⟨f, hf⟩ ⟨g, hg⟩ h := have f = g, from h, by subst f @[simp, norm_cast] theorem coe_inj {f g : locally_constant X Y} : (f : X → Y) = g ↔ f = g := coe_injective.eq_iff @[ext] theorem ext ⦃f g : locally_constant X Y⦄ (h : ∀ x, f x = g x) : f = g := coe_injective (funext h) theorem ext_iff {f g : locally_constant X Y} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ section codomain_topological_space variables [topological_space Y] (f : locally_constant X Y) protected lemma continuous : continuous f := f.is_locally_constant.continuous /-- We can turn a locally-constant function into a bundled `continuous_map`. -/ def to_continuous_map : C(X, Y) := ⟨f, f.continuous⟩ /-- As a shorthand, `locally_constant.to_continuous_map` is available as a coercion -/ instance : has_coe (locally_constant X Y) C(X, Y) := ⟨to_continuous_map⟩ @[simp] lemma to_continuous_map_eq_coe : f.to_continuous_map = f := rfl @[simp] lemma coe_continuous_map : ((f : C(X, Y)) : X → Y) = (f : X → Y) := rfl lemma to_continuous_map_injective : function.injective (to_continuous_map : locally_constant X Y → C(X, Y)) := λ _ _ h, ext (continuous_map.congr_fun h) end codomain_topological_space /-- The constant locally constant function on `X` with value `y : Y`. -/ def const (X : Type*) {Y : Type*} [topological_space X] (y : Y) : locally_constant X Y := ⟨function.const X y, is_locally_constant.const _⟩ @[simp] lemma coe_const (y : Y) : (const X y : X → Y) = function.const X y := rfl /-- The locally constant function to `fin 2` associated to a clopen set. -/ def of_clopen {X : Type*} [topological_space X] {U : set X} [∀ x, decidable (x ∈ U)] (hU : is_clopen U) : locally_constant X (fin 2) := { to_fun := λ x, if x ∈ U then 0 else 1, is_locally_constant := begin rw (is_locally_constant.tfae (λ x, if x ∈ U then (0 : fin 2) else 1)).out 0 3, intros e, fin_cases e, { convert hU.1 using 1, ext, simp only [mem_singleton_iff, fin.one_eq_zero_iff, mem_preimage, ite_eq_left_iff, nat.succ_succ_ne_one], tauto }, { rw ← is_closed_compl_iff, convert hU.2, ext, simp } end } @[simp] lemma of_clopen_fiber_zero {X : Type*} [topological_space X] {U : set X} [∀ x, decidable (x ∈ U)] (hU : is_clopen U) : of_clopen hU ⁻¹' ({0} : set (fin 2)) = U := begin ext, simp only [of_clopen, mem_singleton_iff, fin.one_eq_zero_iff, coe_mk, mem_preimage, ite_eq_left_iff, nat.succ_succ_ne_one], tauto, end @[simp] lemma of_clopen_fiber_one {X : Type*} [topological_space X] {U : set X} [∀ x, decidable (x ∈ U)] (hU : is_clopen U) : of_clopen hU ⁻¹' ({1} : set (fin 2)) = Uᶜ := begin ext, simp only [of_clopen, mem_singleton_iff, coe_mk, fin.zero_eq_one_iff, mem_preimage, ite_eq_right_iff, mem_compl_iff, nat.succ_succ_ne_one], tauto, end lemma locally_constant_eq_of_fiber_zero_eq {X : Type*} [topological_space X] (f g : locally_constant X (fin 2)) (h : f ⁻¹' ({0} : set (fin 2)) = g ⁻¹' {0}) : f = g := begin simp only [set.ext_iff, mem_singleton_iff, mem_preimage] at h, ext1 x, exact fin.fin_two_eq_of_eq_zero_iff (h x) end lemma range_finite [compact_space X] (f : locally_constant X Y) : (set.range f).finite := f.is_locally_constant.range_finite lemma apply_eq_of_is_preconnected (f : locally_constant X Y) {s : set X} (hs : is_preconnected s) {x y : X} (hx : x ∈ s) (hy : y ∈ s) : f x = f y := f.is_locally_constant.apply_eq_of_is_preconnected hs hx hy lemma apply_eq_of_preconnected_space [preconnected_space X] (f : locally_constant X Y) (x y : X) : f x = f y := f.is_locally_constant.apply_eq_of_is_preconnected is_preconnected_univ trivial trivial lemma eq_const [preconnected_space X] (f : locally_constant X Y) (x : X) : f = const X (f x) := ext $ λ y, apply_eq_of_preconnected_space f _ _ lemma exists_eq_const [preconnected_space X] [nonempty Y] (f : locally_constant X Y) : ∃ y, f = const X y := begin rcases classical.em (nonempty X) with ⟨⟨x⟩⟩|hX, { exact ⟨f x, f.eq_const x⟩ }, { exact ⟨classical.arbitrary Y, ext $ λ x, (hX ⟨x⟩).elim⟩ } end /-- Push forward of locally constant maps under any map, by post-composition. -/ def map (f : Y → Z) : locally_constant X Y → locally_constant X Z := λ g, ⟨f ∘ g, λ s, by { rw set.preimage_comp, apply g.is_locally_constant }⟩ @[simp] lemma map_apply (f : Y → Z) (g : locally_constant X Y) : ⇑(map f g) = f ∘ g := rfl @[simp] lemma map_id : @map X Y Y _ id = id := by { ext, refl } @[simp] lemma map_comp {Y₁ Y₂ Y₃ : Type*} (g : Y₂ → Y₃) (f : Y₁ → Y₂) : @map X _ _ _ g ∘ map f = map (g ∘ f) := by { ext, refl } /-- Given a locally constant function to `α → β`, construct a family of locally constant functions with values in β indexed by α. -/ def flip {X α β : Type*} [topological_space X] (f : locally_constant X (α → β)) (a : α) : locally_constant X β := f.map (λ f, f a) /-- If α is finite, this constructs a locally constant function to `α → β` given a family of locally constant functions with values in β indexed by α. -/ def unflip {X α β : Type*} [fintype α] [topological_space X] (f : α → locally_constant X β) : locally_constant X (α → β) := { to_fun := λ x a, f a x, is_locally_constant := begin rw (is_locally_constant.tfae (λ x a, f a x)).out 0 3, intros g, have : (λ (x : X) (a : α), f a x) ⁻¹' {g} = ⋂ (a : α), (f a) ⁻¹' {g a}, by tidy, rw this, apply is_open_Inter, intros a, apply (f a).is_locally_constant, end } @[simp] lemma unflip_flip {X α β : Type*} [fintype α] [topological_space X] (f : locally_constant X (α → β)) : unflip f.flip = f := by { ext, refl } @[simp] lemma flip_unflip {X α β : Type*} [fintype α] [topological_space X] (f : α → locally_constant X β) : (unflip f).flip = f := by { ext, refl } section comap open_locale classical variables [topological_space Y] /-- Pull back of locally constant maps under any map, by pre-composition. This definition only makes sense if `f` is continuous, in which case it sends locally constant functions to their precomposition with `f`. See also `locally_constant.coe_comap`. -/ noncomputable def comap (f : X → Y) : locally_constant Y Z → locally_constant X Z := if hf : continuous f then λ g, ⟨g ∘ f, g.is_locally_constant.comp_continuous hf⟩ else begin by_cases H : nonempty X, { introsI g, exact const X (g $ f $ classical.arbitrary X) }, { intro g, refine ⟨λ x, (H ⟨x⟩).elim, _⟩, intro s, rw is_open_iff_nhds, intro x, exact (H ⟨x⟩).elim } end @[simp] lemma coe_comap (f : X → Y) (g : locally_constant Y Z) (hf : continuous f) : ⇑(comap f g) = g ∘ f := by { rw [comap, dif_pos hf], refl } @[simp] lemma comap_id : @comap X X Z _ _ id = id := by { ext, simp only [continuous_id, id.def, function.comp.right_id, coe_comap] } lemma comap_comp [topological_space Z] (f : X → Y) (g : Y → Z) (hf : continuous f) (hg : continuous g) : @comap _ _ α _ _ f ∘ comap g = comap (g ∘ f) := by { ext, simp only [hf, hg, hg.comp hf, coe_comap] } lemma comap_const (f : X → Y) (y : Y) (h : ∀ x, f x = y) : (comap f : locally_constant Y Z → locally_constant X Z) = λ g, ⟨λ x, g y, is_locally_constant.const _⟩ := begin ext, rw coe_comap, { simp only [h, coe_mk, function.comp_app] }, { rw show f = λ x, y, by ext; apply h, exact continuous_const } end end comap section desc /-- If a locally constant function factors through an injection, then it factors through a locally constant function. -/ def desc {X α β : Type*} [topological_space X] {g : α → β} (f : X → α) (h : locally_constant X β) (cond : g ∘ f = h) (inj : function.injective g) : locally_constant X α := { to_fun := f, is_locally_constant := is_locally_constant.desc _ g (by { rw cond, exact h.2 }) inj } @[simp] lemma coe_desc {X α β : Type*} [topological_space X] (f : X → α) (g : α → β) (h : locally_constant X β) (cond : g ∘ f = h) (inj : function.injective g) : ⇑(desc f h cond inj) = f := rfl end desc section indicator variables {R : Type*} [has_one R] {U : set X} (f : locally_constant X R) open_locale classical /-- Given a clopen set `U` and a locally constant function `f`, `locally_constant.mul_indicator` returns the locally constant function that is `f` on `U` and `1` otherwise. -/ @[to_additive /-" Given a clopen set `U` and a locally constant function `f`, `locally_constant.indicator` returns the locally constant function that is `f` on `U` and `0` otherwise. "-/, simps] noncomputable def mul_indicator (hU : is_clopen U) : locally_constant X R := { to_fun := set.mul_indicator U f, is_locally_constant := begin rw is_locally_constant.iff_exists_open, rintros x, obtain ⟨V, hV, hx, h'⟩ := (is_locally_constant.iff_exists_open _).1 f.is_locally_constant x, by_cases x ∈ U, { refine ⟨U ∩ V, is_open.inter hU.1 hV, set.mem_inter h hx, _⟩, rintros y hy, rw set.mem_inter_iff at hy, rw [set.mul_indicator_of_mem hy.1, set.mul_indicator_of_mem h], apply h' y hy.2, }, { rw ←set.mem_compl_iff at h, refine ⟨Uᶜ, (is_clopen.compl hU).1, h, _⟩, rintros y hy, rw set.mem_compl_iff at h, rw set.mem_compl_iff at hy, simp [h, hy], }, end, } variables (a : X) @[to_additive] theorem mul_indicator_apply_eq_if (hU : is_clopen U) : mul_indicator f hU a = if a ∈ U then f a else 1 := set.mul_indicator_apply U f a variables {a} @[to_additive] theorem mul_indicator_of_mem (hU : is_clopen U) (h : a ∈ U) : f.mul_indicator hU a = f a := by{ rw mul_indicator_apply, apply set.mul_indicator_of_mem h, } @[to_additive] theorem mul_indicator_of_not_mem (hU : is_clopen U) (h : a ∉ U) : f.mul_indicator hU a = 1 := by{ rw mul_indicator_apply, apply set.mul_indicator_of_not_mem h, } end indicator end locally_constant
89d7f588d417c16b5e69b1abb645249f0ce10f1e
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/control/bitraversable/instances_auto.lean
bf83fdda7d051dfa46f01c764312e8c92566fd97
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
5,099
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author(s): Simon Hudon -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.control.bitraversable.lemmas import Mathlib.control.traversable.lemmas import Mathlib.PostPort universes u u_1 u_2 namespace Mathlib /-! # bitraversable instances ## Instances * prod * sum * const * flip * bicompl * bicompr ## References * Hackage: <https://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Bitraversable.html> ## Tags traversable bitraversable functor bifunctor applicative -/ def prod.bitraverse {F : Type u → Type u} [Applicative F] {α : Type u_1} {α' : Type u} {β : Type u_2} {β' : Type u} (f : α → F α') (f' : β → F β') : α × β → F (α' × β') := sorry protected instance prod.bitraversable : bitraversable Prod := bitraversable.mk prod.bitraverse protected instance prod.is_lawful_bitraversable : is_lawful_bitraversable Prod := is_lawful_bitraversable.mk sorry sorry sorry sorry def sum.bitraverse {F : Type u → Type u} [Applicative F] {α : Type u_1} {α' : Type u} {β : Type u_2} {β' : Type u} (f : α → F α') (f' : β → F β') : α ⊕ β → F (α' ⊕ β') := sorry protected instance sum.bitraversable : bitraversable sum := bitraversable.mk sum.bitraverse protected instance sum.is_lawful_bitraversable : is_lawful_bitraversable sum := is_lawful_bitraversable.mk sorry sorry sorry sorry def const.bitraverse {F : Type u → Type u} [Applicative F] {α : Type u_1} {α' : Type u} {β : Type u_2} {β' : Type u} (f : α → F α') (f' : β → F β') : functor.const α β → F (functor.const α' β') := f protected instance bitraversable.const : bitraversable functor.const := bitraversable.mk const.bitraverse protected instance is_lawful_bitraversable.const : is_lawful_bitraversable functor.const := is_lawful_bitraversable.mk sorry sorry sorry sorry def flip.bitraverse {t : Type u → Type u → Type u} [bitraversable t] {F : Type u → Type u} [Applicative F] {α : Type u} {α' : Type u} {β : Type u} {β' : Type u} (f : α → F α') (f' : β → F β') : flip t α β → F (flip t α' β') := bitraverse f' f protected instance bitraversable.flip {t : Type u → Type u → Type u} [bitraversable t] : bitraversable (flip t) := bitraversable.mk flip.bitraverse protected instance is_lawful_bitraversable.flip {t : Type u → Type u → Type u} [bitraversable t] [is_lawful_bitraversable t] : is_lawful_bitraversable (flip t) := is_lawful_bitraversable.mk sorry sorry sorry sorry protected instance bitraversable.traversable {t : Type u → Type u → Type u} [bitraversable t] {α : Type u} : traversable (t α) := traversable.mk bitraversable.tsnd protected instance bitraversable.is_lawful_traversable {t : Type u → Type u → Type u} [bitraversable t] [is_lawful_bitraversable t] {α : Type u} : is_lawful_traversable (t α) := is_lawful_traversable.mk sorry sorry sorry sorry def bicompl.bitraverse {t : Type u → Type u → Type u} [bitraversable t] (F : Type u → Type u) (G : Type u → Type u) [traversable F] [traversable G] {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u} {α' : Type u} {β' : Type u} (f : α → m β) (f' : α' → m β') : function.bicompl t F G α α' → m (function.bicompl t F G β β') := bitraverse (traverse f) (traverse f') protected instance function.bicompl.bitraversable {t : Type u → Type u → Type u} [bitraversable t] (F : Type u → Type u) (G : Type u → Type u) [traversable F] [traversable G] : bitraversable (function.bicompl t F G) := bitraversable.mk (bicompl.bitraverse F G) protected instance function.bicompl.is_lawful_bitraversable {t : Type u → Type u → Type u} [bitraversable t] (F : Type u → Type u) (G : Type u → Type u) [traversable F] [traversable G] [is_lawful_traversable F] [is_lawful_traversable G] [is_lawful_bitraversable t] : is_lawful_bitraversable (function.bicompl t F G) := is_lawful_bitraversable.mk sorry sorry sorry sorry def bicompr.bitraverse {t : Type u → Type u → Type u} [bitraversable t] (F : Type u → Type u) [traversable F] {m : Type u → Type u} [Applicative m] {α : Type u} {β : Type u} {α' : Type u} {β' : Type u} (f : α → m β) (f' : α' → m β') : function.bicompr F t α α' → m (function.bicompr F t β β') := traverse (bitraverse f f') protected instance function.bicompr.bitraversable {t : Type u → Type u → Type u} [bitraversable t] (F : Type u → Type u) [traversable F] : bitraversable (function.bicompr F t) := bitraversable.mk (bicompr.bitraverse F) protected instance function.bicompr.is_lawful_bitraversable {t : Type u → Type u → Type u} [bitraversable t] (F : Type u → Type u) [traversable F] [is_lawful_traversable F] [is_lawful_bitraversable t] : is_lawful_bitraversable (function.bicompr F t) := is_lawful_bitraversable.mk sorry sorry sorry sorry end Mathlib
a9b18a1ee554bc88f360f66f447b006ce8fa9b5c
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Lean/Compiler/IR/ElimDeadBranches.lean
1e73e0d09a5dd7b54043be343eb9672187d3b4a4
[ "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
9,643
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.Option import Init.Data.Nat import Init.Lean.Compiler.IR.Format import Init.Lean.Compiler.IR.Basic import Init.Lean.Compiler.IR.CompilerM namespace Lean namespace IR namespace UnreachableBranches /-- Value used in the abstract interpreter -/ inductive Value | bot -- undefined | top -- any value | ctor (i : CtorInfo) (vs : Array Value) | choice (vs : List Value) namespace Value instance : Inhabited Value := ⟨top⟩ protected partial def beq : Value → Value → Bool | bot, bot => true | top, top => true | ctor i₁ vs₁, ctor i₂ vs₂ => i₁ == i₂ && Array.isEqv vs₁ vs₂ beq | choice vs₁, choice vs₂ => vs₁.all (fun v₁ => vs₂.any $ fun v₂ => beq v₁ v₂) && vs₂.all (fun v₂ => vs₁.any $ fun v₁ => beq v₁ v₂) | _, _ => false instance : HasBeq Value := ⟨Value.beq⟩ partial def addChoice (merge : Value → Value → Value) : List Value → Value → List Value | [], v => [v] | v₁@(ctor i₁ vs₁) :: cs, v₂@(ctor i₂ vs₂) => if i₁ == i₂ then merge v₁ v₂ :: cs else v₁ :: addChoice cs v₂ | _, _ => panic! "invalid addChoice" partial def merge : Value → Value → Value | bot, v => v | v, bot => v | top, _ => top | _, top => top | v₁@(ctor i₁ vs₁), v₂@(ctor i₂ vs₂) => if i₁ == i₂ then ctor i₁ $ vs₁.size.fold (fun i r => r.push (merge (vs₁.get! i) (vs₂.get! i))) #[] else choice [v₁, v₂] | choice vs₁, choice vs₂ => choice $ vs₁.foldl (addChoice merge) vs₂ | choice vs, v => choice $ addChoice merge vs v | v, choice vs => choice $ addChoice merge vs v protected partial def format : Value → Format | top => "top" | bot => "bot" | choice vs => fmt "@" ++ @List.format _ ⟨format⟩ vs | ctor i vs => fmt "#" ++ if vs.isEmpty then fmt i.name else Format.paren (fmt i.name ++ @formatArray _ ⟨format⟩ vs) instance : HasFormat Value := ⟨Value.format⟩ instance : HasToString Value := ⟨Format.pretty ∘ Value.format⟩ /- Make sure constructors of recursive inductive datatypes can only occur once in each path. We use this function this function to implement a simple widening operation for our abstract interpreter. -/ partial def truncate (env : Environment) : Value → NameSet → Value | ctor i vs, found => let I := i.name.getPrefix; if found.contains I then top else let cont (found' : NameSet) : Value := ctor i (vs.map $ fun v => truncate v found'); match env.find? I with | some (ConstantInfo.inductInfo d) => if d.isRec then cont (found.insert I) else cont found | _ => cont found | choice vs, found => let newVs := vs.map $ fun v => truncate v found; if newVs.elem top then top else choice newVs | v, _ => v /- Widening operator that guarantees termination in our abstract interpreter. -/ def widening (env : Environment) (v₁ v₂ : Value) : Value := truncate env (merge v₁ v₂) {} end Value abbrev FunctionSummaries := SMap FunId Value def mkFunctionSummariesExtension : IO (SimplePersistentEnvExtension (FunId × Value) FunctionSummaries) := registerSimplePersistentEnvExtension { name := `unreachBranchesFunSummary, addImportedFn := fun as => let cache : FunctionSummaries := mkStateFromImportedEntries (fun s (p : FunId × Value) => s.insert p.1 p.2) {} as; cache.switch, addEntryFn := fun s ⟨e, n⟩ => s.insert e n } @[init mkFunctionSummariesExtension] constant functionSummariesExt : SimplePersistentEnvExtension (FunId × Value) FunctionSummaries := arbitrary _ def addFunctionSummary (env : Environment) (fid : FunId) (v : Value) : Environment := functionSummariesExt.addEntry env (fid, v) def getFunctionSummary? (env : Environment) (fid : FunId) : Option Value := (functionSummariesExt.getState env).find? fid abbrev Assignment := HashMap VarId Value structure InterpContext := (currFnIdx : Nat := 0) (decls : Array Decl) (env : Environment) (lctx : LocalContext := {}) structure InterpState := (assignments : Array Assignment) (funVals : PArray Value) -- we take snapshots during fixpoint computations abbrev M := ReaderT InterpContext (StateM InterpState) open Value def findVarValue (x : VarId) : M Value := do ctx ← read; s ← get; let assignment := s.assignments.get! ctx.currFnIdx; pure $ assignment.findD x bot def findArgValue (arg : Arg) : M Value := match arg with | Arg.var x => findVarValue x | _ => pure top def updateVarAssignment (x : VarId) (v : Value) : M Unit := do v' ← findVarValue x; ctx ← read; modify $ fun s => { assignments := s.assignments.modify ctx.currFnIdx $ fun a => a.insert x (merge v v'), .. s } partial def projValue : Value → Nat → Value | ctor _ vs, i => vs.getD i bot | choice vs, i => vs.foldl (fun r v => merge r (projValue v i)) bot | v, _ => v def interpExpr : Expr → M Value | Expr.ctor i ys => ctor i <$> ys.mapM (fun y => findArgValue y) | Expr.proj i x => do v ← findVarValue x; pure $ projValue v i | Expr.fap fid ys => do ctx ← read; match getFunctionSummary? ctx.env fid with | some v => pure v | none => do s ← get; match ctx.decls.findIdx? (fun decl => decl.name == fid) with | some idx => pure $ s.funVals.get! idx | none => pure top | _ => pure top partial def containsCtor : Value → CtorInfo → Bool | top, _ => true | ctor i _, j => i == j | choice vs, j => vs.any $ fun v => containsCtor v j | _, _ => false def updateCurrFnSummary (v : Value) : M Unit := do ctx ← read; let currFnIdx := ctx.currFnIdx; modify $ fun s => { funVals := s.funVals.modify currFnIdx (fun v' => widening ctx.env v v'), .. s } /-- Return true if the assignment of at least one parameter has been updated. -/ def updateJPParamsAssignment (ys : Array Param) (xs : Array Arg) : M Bool := do ctx ← read; let currFnIdx := ctx.currFnIdx; ys.size.foldM (fun i r => do let y := ys.get! i; let x := xs.get! i; yVal ← findVarValue y.x; xVal ← findArgValue x; let newVal := merge yVal xVal; if newVal == yVal then pure r else do modify $ fun s => { assignments := s.assignments.modify currFnIdx $ fun a => a.insert y.x newVal, .. s }; pure true) false partial def interpFnBody : FnBody → M Unit | FnBody.vdecl x _ e b => do v ← interpExpr e; updateVarAssignment x v; interpFnBody b | FnBody.jdecl j ys v b => adaptReader (fun (ctx : InterpContext) => { lctx := ctx.lctx.addJP j ys v, .. ctx }) $ interpFnBody b | FnBody.case _ x _ alts => do v ← findVarValue x; alts.forM $ fun alt => match alt with | Alt.ctor i b => when (containsCtor v i) $ interpFnBody b | Alt.default b => interpFnBody b | FnBody.ret x => do v ← findArgValue x; -- dbgTrace ("ret " ++ toString v) $ fun _ => updateCurrFnSummary v | FnBody.jmp j xs => do ctx ← read; let ys := (ctx.lctx.getJPParams j).get!; updated ← updateJPParamsAssignment ys xs; when updated $ interpFnBody $ (ctx.lctx.getJPBody j).get! | e => unless (e.isTerminal) $ interpFnBody e.body def inferStep : M Bool := do ctx ← read; modify $ fun s => { assignments := ctx.decls.map $ fun _ => {}, .. s }; ctx.decls.size.foldM (fun idx modified => do match ctx.decls.get! idx with | Decl.fdecl fid ys _ b => do s ← get; -- dbgTrace (">> " ++ toString fid) $ fun _ => let currVals := s.funVals.get! idx; adaptReader (fun (ctx : InterpContext) => { currFnIdx := idx, .. ctx }) $ do ys.forM $ fun y => updateVarAssignment y.x top; interpFnBody b; s ← get; let newVals := s.funVals.get! idx; pure (modified || currVals != newVals) | Decl.extern _ _ _ _ => pure modified) false partial def inferMain : Unit → M Unit | _ => do modified ← inferStep; if modified then inferMain () else pure () partial def elimDeadAux (assignment : Assignment) : FnBody → FnBody | FnBody.vdecl x t e b => FnBody.vdecl x t e (elimDeadAux b) | FnBody.jdecl j ys v b => FnBody.jdecl j ys (elimDeadAux v) (elimDeadAux b) | FnBody.case tid x xType alts => let v := assignment.findD x bot; let alts := alts.map $ fun alt => match alt with | Alt.ctor i b => Alt.ctor i $ if containsCtor v i then elimDeadAux b else FnBody.unreachable | Alt.default b => Alt.default (elimDeadAux b); FnBody.case tid x xType alts | e => if e.isTerminal then e else let (instr, b) := e.split; let b := elimDeadAux b; instr.setBody b partial def elimDead (assignment : Assignment) : Decl → Decl | Decl.fdecl fid ys t b => Decl.fdecl fid ys t $ elimDeadAux assignment b | other => other end UnreachableBranches open UnreachableBranches def elimDeadBranches (decls : Array Decl) : CompilerM (Array Decl) := do s ← get; let env := s.env; let assignments : Array Assignment := decls.map $ fun _ => {}; let funVals := mkPArray decls.size Value.bot; let ctx : InterpContext := { decls := decls, env := env }; let s : InterpState := { assignments := assignments, funVals := funVals }; let (_, s) := (inferMain () ctx).run s; let funVals := s.funVals; let assignments := s.assignments; modify $ fun s => let env := decls.size.fold (fun i env => -- dbgTrace (">> " ++ toString (decls.get! i).name ++ " " ++ toString (funVals.get! i)) $ fun _ => addFunctionSummary env (decls.get! i).name (funVals.get! i)) s.env; { env := env, .. s }; pure $ decls.mapIdx $ fun i decl => elimDead (assignments.get! i) decl end IR end Lean
452743222031097129b65f2f6283ac069bce23ba
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/logic/unique.lean
13c2b7cb7b2463b174f46286cbc67f1245738015
[ "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
6,332
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import tactic.basic import logic.is_empty /-! # Types with a unique term In this file we define a typeclass `unique`, which expresses that a type has a unique term. In other words, a type that is `inhabited` and a `subsingleton`. ## Main declaration * `unique`: a typeclass that expresses that a type has a unique term. ## Main statements * `unique.mk'`: an inhabited subsingleton type is `unique`. This can not be an instance because it would lead to loops in typeclass inference. * `function.surjective.unique`: if the domain of a surjective function is `unique`, then its codomain is `unique` as well. * `function.injective.subsingleton`: if the codomain of an injective function is `subsingleton`, then its domain is `subsingleton` as well. * `function.injective.unique`: if the codomain of an injective function is `subsingleton` and its domain is `inhabited`, then its domain is `unique`. ## Implementation details The typeclass `unique α` is implemented as a type, rather than a `Prop`-valued predicate, for good definitional properties of the default term. -/ universes u v w variables {α : Sort u} {β : Sort v} {γ : Sort w} /-- `unique α` expresses that `α` is a type with a unique term `default α`. This is implemented as a type, rather than a `Prop`-valued predicate, for good definitional properties of the default term. -/ @[ext] structure unique (α : Sort u) extends inhabited α := (uniq : ∀ a:α, a = default) attribute [class] unique /-- Given an explicit `a : α` with `[subsingleton α]`, we can construct a `[unique α]` instance. This is a def because the typeclass search cannot arbitrarily invent the `a : α` term. Nevertheless, these instances are all equivalent by `unique.subsingleton.unique`. See note [reducible non-instances]. -/ @[reducible] def unique_of_subsingleton {α : Sort*} [subsingleton α] (a : α) : unique α := { default := a, uniq := λ _, subsingleton.elim _ _ } instance punit.unique : unique punit.{u} := { default := punit.star, uniq := λ x, punit_eq x _ } /-- Every provable proposition is unique, as all proofs are equal. -/ def unique_prop {p : Prop} (h : p) : unique p := { default := h, uniq := λ x, rfl } instance : unique true := unique_prop trivial lemma fin.eq_zero : ∀ n : fin 1, n = 0 | ⟨n, hn⟩ := fin.eq_of_veq (nat.eq_zero_of_le_zero (nat.le_of_lt_succ hn)) instance {n : ℕ} : inhabited (fin n.succ) := ⟨0⟩ instance inhabited_fin_one_add (n : ℕ) : inhabited (fin (1 + n)) := ⟨⟨0, nat.zero_lt_one_add n⟩⟩ @[simp] lemma fin.default_eq_zero (n : ℕ) : default (fin n.succ) = 0 := rfl instance fin.unique : unique (fin 1) := { uniq := fin.eq_zero, .. fin.inhabited } namespace unique open function section variables [unique α] @[priority 100] -- see Note [lower instance priority] instance : inhabited α := to_inhabited ‹unique α› lemma eq_default (a : α) : a = default α := uniq _ a lemma default_eq (a : α) : default α = a := (uniq _ a).symm @[priority 100] -- see Note [lower instance priority] instance : subsingleton α := subsingleton_of_forall_eq _ eq_default lemma forall_iff {p : α → Prop} : (∀ a, p a) ↔ p (default α) := ⟨λ h, h _, λ h x, by rwa [unique.eq_default x]⟩ lemma exists_iff {p : α → Prop} : Exists p ↔ p (default α) := ⟨λ ⟨a, ha⟩, eq_default a ▸ ha, exists.intro (default α)⟩ end @[ext] protected lemma subsingleton_unique' : ∀ (h₁ h₂ : unique α), h₁ = h₂ | ⟨⟨x⟩, h⟩ ⟨⟨y⟩, _⟩ := by congr; rw [h x, h y] instance subsingleton_unique : subsingleton (unique α) := ⟨unique.subsingleton_unique'⟩ /-- Construct `unique` from `inhabited` and `subsingleton`. Making this an instance would create a loop in the class inheritance graph. -/ def mk' (α : Sort u) [h₁ : inhabited α] [subsingleton α] : unique α := { uniq := λ x, subsingleton.elim _ _, .. h₁ } end unique @[simp] lemma pi.default_def {β : Π a : α, Sort v} [Π a, inhabited (β a)] : default (Π a, β a) = λ a, default (β a) := rfl lemma pi.default_apply {β : Π a : α, Sort v} [Π a, inhabited (β a)] (a : α) : default (Π a, β a) a = default (β a) := rfl instance pi.unique {β : Π a : α, Sort v} [Π a, unique (β a)] : unique (Π a, β a) := { uniq := λ f, funext $ λ x, unique.eq_default _, .. pi.inhabited α } /-- There is a unique function on an empty domain. -/ instance pi.unique_of_is_empty [is_empty α] (β : Π a : α, Sort v) : unique (Π a, β a) := { default := is_empty_elim, uniq := λ f, funext is_empty_elim } namespace function variable {f : α → β} /-- If the domain of a surjective function is a singleton, then the codomain is a singleton as well. -/ protected def surjective.unique (hf : surjective f) [unique α] : unique β := { default := f (default _), uniq := λ b, let ⟨a, ha⟩ := hf b in ha ▸ congr_arg f (unique.eq_default _) } /-- If the codomain of an injective function is a subsingleton, then the domain is a subsingleton as well. -/ protected lemma injective.subsingleton (hf : injective f) [subsingleton β] : subsingleton α := ⟨λ x y, hf $ subsingleton.elim _ _⟩ /-- If `α` is inhabited and admits an injective map to a subsingleton type, then `α` is `unique`. -/ protected def injective.unique [inhabited α] [subsingleton β] (hf : injective f) : unique α := @unique.mk' _ _ hf.subsingleton end function namespace option /-- `option α` is a `subsingleton` if and only if `α` is empty. -/ lemma subsingleton_iff_is_empty {α} : subsingleton (option α) ↔ is_empty α := ⟨λ h, ⟨λ x, option.no_confusion $ @subsingleton.elim _ h x none⟩, λ h, ⟨λ x y, option.cases_on x (option.cases_on y rfl (λ x, h.elim x)) (λ x, h.elim x)⟩⟩ instance {α} [is_empty α] : unique (option α) := @unique.mk' _ _ (subsingleton_iff_is_empty.2 ‹_›) end option section subtype instance unique.subtype_eq (y : α) : unique {x // x = y} := { default := ⟨y, rfl⟩, uniq := λ ⟨x, hx⟩, by simpa using hx } instance unique.subtype_eq' (y : α) : unique {x // y = x} := { default := ⟨y, rfl⟩, uniq := λ ⟨x, hx⟩, by simpa using hx.symm } end subtype
b7e9540957b29c5f7c852d992c483f11b8ccfbe9
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/order/pilex.lean
6bb4a86552666ca59c87bf3d328b8554d87c2da0
[]
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,507
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.algebra.ordered_pi import Mathlib.order.well_founded import Mathlib.algebra.order_functions import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-- The lexicographic relation on `Π i : ι, β i`, where `ι` is ordered by `r`, and each `β i` is ordered by `s`. -/ def pi.lex {ι : Type u_1} {β : ι → Type u_2} (r : ι → ι → Prop) (s : {i : ι} → β i → β i → Prop) (x : (i : ι) → β i) (y : (i : ι) → β i) := ∃ (i : ι), (∀ (j : ι), r j i → x j = y j) ∧ s (x i) (y i) /-- The cartesian product of an indexed family, equipped with the lexicographic order. -/ def pilex (α : Type u_1) (β : α → Type u_2) := (a : α) → β a protected instance pilex.has_lt {ι : Type u_1} {β : ι → Type u_2} [HasLess ι] [(a : ι) → HasLess (β a)] : HasLess (pilex ι β) := { Less := pi.lex Less fun (_x : ι) => Less } protected instance pilex.inhabited {ι : Type u_1} {β : ι → Type u_2} [(a : ι) → Inhabited (β a)] : Inhabited (pilex ι β) := eq.mpr sorry (pi.inhabited ι) protected instance pilex.partial_order {ι : Type u_1} {β : ι → Type u_2} [linear_order ι] [(a : ι) → partial_order (β a)] : partial_order (pilex ι β) := (fun (lt_not_symm : ∀ {x y : pilex ι β}, ¬(x < y ∧ y < x)) => partial_order.mk (fun (x y : pilex ι β) => x < y ∨ x = y) Less sorry sorry sorry) sorry /-- `pilex` is a linear order if the original order is well-founded. This cannot be an instance, since it depends on the well-foundedness of `<`. -/ protected def pilex.linear_order {ι : Type u_1} {β : ι → Type u_2} [linear_order ι] (wf : well_founded Less) [(a : ι) → linear_order (β a)] : linear_order (pilex ι β) := linear_order.mk partial_order.le partial_order.lt sorry sorry sorry sorry (classical.dec_rel LessEq) Mathlib.decidable_eq_of_decidable_le Mathlib.decidable_lt_of_decidable_le protected instance pilex.ordered_add_comm_group {ι : Type u_1} {β : ι → Type u_2} [linear_order ι] [(a : ι) → ordered_add_comm_group (β a)] : ordered_add_comm_group (pilex ι β) := ordered_add_comm_group.mk add_comm_group.add sorry add_comm_group.zero sorry sorry add_comm_group.neg add_comm_group.sub sorry sorry partial_order.le partial_order.lt sorry sorry sorry sorry
36eb429957ca0885148e9172adbbd767f3fd71f2
c5b07d17b3c9fb19e4b302465d237fd1d988c14f
/src/sampler.lean
c0a985a18ba650f39ad2a7f3e8ca67d4099659a0
[ "MIT" ]
permissive
skaslev/papers
acaec61602b28c33d6115e53913b2002136aa29b
f15b379f3c43bbd0a37ac7bb75f4278f7e901389
refs/heads/master
1,665,505,770,318
1,660,378,602,000
1,660,378,602,000
14,101,547
0
1
MIT
1,595,414,522,000
1,383,542,702,000
Lean
UTF-8
Lean
false
false
3,309
lean
import system.io import functors.generating class sampler (A : Type) := (gen : io A) def gen (A) [sampler A] := @sampler.gen A def genₛ {A} (s : sampler A) := @sampler.gen A s instance : functor sampler := {map := λ A B f s, ⟨do x <- genₛ s, return $ f x⟩} instance {n} : sampler (fin n) := {gen := do i <- io.rand 0 n, dite (i < n) (λ h, return ⟨i, h⟩) (λ h, failure)} def gen_fseq (n A) [sampler A] : io (fseq n A) := nat.rec_on n (return fin.elim0) (λ n ih, do x <- gen A, xs <- ih, return $ fseq.cons_iso.f (x, xs)) def gen_fseqₛ (n) {A} (s : sampler A) : io (fseq n A) := @gen_fseq n A s instance {n A} [sampler A] : sampler (fseq n A) := {gen := gen_fseq n A} instance zero_sampler : sampler 0 := {gen := failure} instance one_sampler : sampler 1 := {gen := return ()} instance : sampler bool := {gen := do x <- io.rand 0 2, return $ x ≠ 0} namespace sample def sized_ogf (c A) [sampler A] (size : ℕ) : sampler (ogf c A) := {gen := do shape <- gen (fin (c size)), data <- gen (fseq size A), return ⟨size, (shape, data)⟩} def sized_ogf_iso {f : Type → Type} {c A} [sampler A] (i : f A ≃ ogf c A) (size : ℕ) : sampler (f A) := i.g <$> sized_ogf c A size def sized_ogf₁ (c) (size : ℕ) : sampler (ogf c 1) := sized_ogf c 1 size def sized_ogf_iso₁ {A c} (i : A ≃ ogf c 1) (size : ℕ) : sampler A := sized_ogf_iso (iso.const_iso.inv ⋆ i) size def sized_shape (c) (size : ℕ) : sampler (shape c) := ogf.shape_iso.f <$> sized_ogf₁ c size def reverse_impl {A} : list A → list A → list A | acc [] := acc | acc (x::xs) := reverse_impl (x::acc) xs def reverse {A} : list A → list A := reverse_impl [] def prefix_sum_impl (c : ℕ → ℕ) : ℕ → list ℕ | 0 := [c 0] | (n+1) := let ih := prefix_sum_impl n in (c (n+1) + ih.head) :: ih def prefix_sum (c : ℕ → ℕ) (n : ℕ) : ℕ × list ℕ := let ps := prefix_sum_impl c n in (ps.head, reverse ps) def bounded_ogf (c A) [sampler A] (max_size : ℕ) : sampler (ogf c A) := let (num_shapes, ps) := prefix_sum c max_size in {gen := do shape <- gen (fin num_shapes), let size := list.find_index (λ x, shape.1 < x) ps, genₛ (sized_ogf c A size)} def bounded_ogf_iso {f : Type → Type} {c A} [sampler A] (i : f A ≃ ogf c A) (max_size : ℕ): sampler (f A) := i.g <$> bounded_ogf c A max_size def bounded_ogf₁ (c) (max_size : ℕ) : sampler (ogf c 1) := bounded_ogf c 1 max_size def bounded_ogf_iso₁ {A c} (i : A ≃ ogf c 1) (max_size : ℕ) : sampler A := bounded_ogf_iso (iso.const_iso.inv ⋆ i) max_size def bounded_shape (c) (max_size : ℕ) : sampler (shape c) := ogf.shape_iso.f <$> bounded_ogf₁ c max_size end sample namespace X def sized_ogf (f A) [has_ogf f] [sampler A] (size : ℕ) : sampler (f A) := (@has_ogf.iso f _ _).g <$> sample.sized_ogf (has_ogf.cf f) A size def sized_ogf₁ (A) [has_ogf₁ A] (size : ℕ): sampler A := (@has_ogf₁.iso A _).g <$> sample.sized_ogf₁ (has_ogf₁.cf A) size def bounded_ogf (f A) [has_ogf f] [sampler A] (max_size : ℕ) : sampler (f A) := (@has_ogf.iso f _ _).g <$> sample.bounded_ogf (has_ogf.cf f) A max_size def bounded_ogf₁ (A) [has_ogf₁ A] (max_size : ℕ) : sampler A := (@has_ogf₁.iso A _).g <$> sample.bounded_ogf₁ (has_ogf₁.cf A) max_size end X
5e7d34fc5272c451cfef0a1c7ebaaaba8298af49
e0b0b1648286e442507eb62344760d5cd8d13f2d
/tests/compiler/rbmap_library.lean
cba7ac5c54168f016e9aa1637b80d079ef9f9c60
[ "Apache-2.0" ]
permissive
MULXCODE/lean4
743ed389e05e26e09c6a11d24607ad5a697db39b
4675817a9e89824eca37192364cd47a4027c6437
refs/heads/master
1,682,231,879,857
1,620,423,501,000
1,620,423,501,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
2,291
lean
import Std open Std def check (b : Bool) : IO Unit := do unless b do IO.println "ERROR" def sz {α β : Type} {cmp : α → α → Ordering} (m : RBMap α β cmp) : Nat := m.fold (fun sz _ _ => sz+1) 0 def depth {α β : Type} {cmp : α → α → Ordering} (m : RBMap α β cmp) : Nat := m.depth Nat.max def tst1 : IO Unit := do let Map := RBMap String Nat compare let m : Map := {} let m := m.insert "hello" 0 let m := m.insert "world" 1 check (m.find? "hello" == some 0) check (m.find? "world" == some 1) let m := m.erase "hello" check (m.find? "hello" == none) check (m.find? "world" == some 1) pure () def tst2 : IO Unit := do let Map := RBMap Nat Nat compare let m : Map := {} let n : Nat := 10000 let mut m := n.fold (fun i (m : Map) => m.insert i (i*10)) m check (m.all (fun k v => v == k*10)) check (sz m == n) IO.println (">> " ++ toString (depth m) ++ ", " ++ toString (sz m)) for i in [:n/2] do m := m.erase (2*i) check (m.all (fun k v => v == k*10)) check (sz m == n / 2) IO.println (">> " ++ toString (depth m) ++ ", " ++ toString (sz m)) pure () abbrev Map := RBMap Nat Nat compare def mkRandMap (max : Nat) : Nat → Map → Array (Nat × Nat) → IO (Map × Array (Nat × Nat)) | 0, m, a => pure (m, a) | n+1, m, a => do let k ← IO.rand 0 max let v ← IO.rand 0 max if m.find? k == none then do let m := m.insert k v let a := a.push (k, v) mkRandMap max n m a else mkRandMap max n m a def tst3 (seed : Nat) (n : Nat) (max : Nat) : IO Unit := do IO.setRandSeed seed let mut (m, a) ← mkRandMap max n {} Array.empty check (sz m == a.size) check (a.all (fun ⟨k, v⟩ => m.find? k == some v)) IO.println ("tst3 size: " ++ toString a.size) let mut i := 0 for (k, b) in a do if i % 2 == 0 then m := m.erase k i := i + 1 check (sz m == a.size / 2) let i := 0 for (k, v) in a do if i % 2 == 1 then check (m.find? k == some v) i := i + 1 IO.println ("tst3 after, depth: " ++ toString (depth m) ++ ", size: " ++ toString (sz m)) pure () def main (xs : List String) : IO Unit := tst1 *> tst2 *> tst3 1 1000 20000 *> tst3 2 1000 40000 *> tst3 3 100 4000 *> tst3 4 5000 100000 *> tst3 5 1000 40000 *> pure ()
03a40c0bad34e4698ef61618f61c355ccccec12e
36c7a18fd72e5b57229bd8ba36493daf536a19ce
/library/data/real/bigops.lean
10d6d3b66b13160889d07811ac64c42c796e480b
[ "Apache-2.0" ]
permissive
YHVHvx/lean
732bf0fb7a298cd7fe0f15d82f8e248c11db49e9
038369533e0136dd395dc252084d3c1853accbf2
refs/heads/master
1,610,701,080,210
1,449,128,595,000
1,449,128,595,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,950
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Finite products and sums on the reals. -/ import data.real.division algebra.group_bigops algebra.group_set_bigops open list namespace real open [classes] algebra local attribute real.ordered_ring [instance] local attribute real.comm_ring [instance] variables {A : Type} [deceqA : decidable_eq A] /- Prodl -/ definition Prodl (l : list A) (f : A → real) : real := algebra.Prodl l f notation `∏` binders `←` l, r:(scoped f, Prodl l f) := r theorem Prodl_nil (f : A → real) : Prodl [] f = 1 := algebra.Prodl_nil f theorem Prodl_cons (f : A → real) (a : A) (l : list A) : Prodl (a::l) f = f a * Prodl l f := algebra.Prodl_cons f a l theorem Prodl_append (l₁ l₂ : list A) (f : A → real) : Prodl (l₁++l₂) f = Prodl l₁ f * Prodl l₂ f := algebra.Prodl_append l₁ l₂ f theorem Prodl_mul (l : list A) (f g : A → real) : Prodl l (λx, f x * g x) = Prodl l f * Prodl l g := algebra.Prodl_mul l f g section deceqA include deceqA theorem Prodl_insert_of_mem (f : A → real) {a : A} {l : list A} (H : a ∈ l) : Prodl (insert a l) f = Prodl l f := algebra.Prodl_insert_of_mem f H theorem Prodl_insert_of_not_mem (f : A → real) {a : A} {l : list A} (H : a ∉ l) : Prodl (insert a l) f = f a * Prodl l f := algebra.Prodl_insert_of_not_mem f H theorem Prodl_union {l₁ l₂ : list A} (f : A → real) (d : disjoint l₁ l₂) : Prodl (union l₁ l₂) f = Prodl l₁ f * Prodl l₂ f := algebra.Prodl_union f d theorem Prodl_one (l : list A) : Prodl l (λ x, 1) = 1 := algebra.Prodl_one l end deceqA /- Prod over finset -/ namespace finset open finset definition Prod (s : finset A) (f : A → real) : real := algebra.finset.Prod s f notation `∏` binders `∈` s, r:(scoped f, Prod s f) := r theorem Prod_empty (f : A → real) : Prod ∅ f = 1 := algebra.finset.Prod_empty f theorem Prod_mul (s : finset A) (f g : A → real) : Prod s (λx, f x * g x) = Prod s f * Prod s g := algebra.finset.Prod_mul s f g section deceqA include deceqA theorem Prod_insert_of_mem (f : A → real) {a : A} {s : finset A} (H : a ∈ s) : Prod (insert a s) f = Prod s f := algebra.finset.Prod_insert_of_mem f H theorem Prod_insert_of_not_mem (f : A → real) {a : A} {s : finset A} (H : a ∉ s) : Prod (insert a s) f = f a * Prod s f := algebra.finset.Prod_insert_of_not_mem f H theorem Prod_union (f : A → real) {s₁ s₂ : finset A} (disj : s₁ ∩ s₂ = ∅) : Prod (s₁ ∪ s₂) f = Prod s₁ f * Prod s₂ f := algebra.finset.Prod_union f disj theorem Prod_ext {s : finset A} {f g : A → real} (H : ∀x, x ∈ s → f x = g x) : Prod s f = Prod s g := algebra.finset.Prod_ext H theorem Prod_one (s : finset A) : Prod s (λ x, 1) = 1 := algebra.finset.Prod_one s end deceqA end finset /- Prod over set -/ namespace set open set noncomputable definition Prod (s : set A) (f : A → real) : real := algebra.set.Prod s f notation `∏` binders `∈` s, r:(scoped f, Prod s f) := r theorem Prod_empty (f : A → real) : Prod ∅ f = 1 := algebra.set.Prod_empty f theorem Prod_of_not_finite {s : set A} (nfins : ¬ finite s) (f : A → real) : Prod s f = 1 := algebra.set.Prod_of_not_finite nfins f theorem Prod_mul (s : set A) (f g : A → real) : Prod s (λx, f x * g x) = Prod s f * Prod s g := algebra.set.Prod_mul s f g theorem Prod_insert_of_mem (f : A → real) {a : A} {s : set A} (H : a ∈ s) : Prod (insert a s) f = Prod s f := algebra.set.Prod_insert_of_mem f H theorem Prod_insert_of_not_mem (f : A → real) {a : A} {s : set A} [fins : finite s] (H : a ∉ s) : Prod (insert a s) f = f a * Prod s f := algebra.set.Prod_insert_of_not_mem f H theorem Prod_union (f : A → real) {s₁ s₂ : set A} [fins₁ : finite s₁] [fins₂ : finite s₂] (disj : s₁ ∩ s₂ = ∅) : Prod (s₁ ∪ s₂) f = Prod s₁ f * Prod s₂ f := algebra.set.Prod_union f disj theorem Prod_ext {s : set A} {f g : A → real} (H : ∀x, x ∈ s → f x = g x) : Prod s f = Prod s g := algebra.set.Prod_ext H theorem Prod_one (s : set A) : Prod s (λ x, 1) = 1 := algebra.set.Prod_one s end set /- Suml -/ definition Suml (l : list A) (f : A → real) : real := algebra.Suml l f notation `∑` binders `←` l, r:(scoped f, Suml l f) := r theorem Suml_nil (f : A → real) : Suml [] f = 0 := algebra.Suml_nil f theorem Suml_cons (f : A → real) (a : A) (l : list A) : Suml (a::l) f = f a + Suml l f := algebra.Suml_cons f a l theorem Suml_append (l₁ l₂ : list A) (f : A → real) : Suml (l₁++l₂) f = Suml l₁ f + Suml l₂ f := algebra.Suml_append l₁ l₂ f theorem Suml_add (l : list A) (f g : A → real) : Suml l (λx, f x + g x) = Suml l f + Suml l g := algebra.Suml_add l f g section deceqA include deceqA theorem Suml_insert_of_mem (f : A → real) {a : A} {l : list A} (H : a ∈ l) : Suml (insert a l) f = Suml l f := algebra.Suml_insert_of_mem f H theorem Suml_insert_of_not_mem (f : A → real) {a : A} {l : list A} (H : a ∉ l) : Suml (insert a l) f = f a + Suml l f := algebra.Suml_insert_of_not_mem f H theorem Suml_union {l₁ l₂ : list A} (f : A → real) (d : disjoint l₁ l₂) : Suml (union l₁ l₂) f = Suml l₁ f + Suml l₂ f := algebra.Suml_union f d theorem Suml_zero (l : list A) : Suml l (λ x, 0) = 0 := algebra.Suml_zero l end deceqA /- Sum over a finset -/ namespace finset open finset definition Sum (s : finset A) (f : A → real) : real := algebra.finset.Sum s f notation `∑` binders `∈` s, r:(scoped f, Sum s f) := r theorem Sum_empty (f : A → real) : Sum ∅ f = 0 := algebra.finset.Sum_empty f theorem Sum_add (s : finset A) (f g : A → real) : Sum s (λx, f x + g x) = Sum s f + Sum s g := algebra.finset.Sum_add s f g section deceqA include deceqA theorem Sum_insert_of_mem (f : A → real) {a : A} {s : finset A} (H : a ∈ s) : Sum (insert a s) f = Sum s f := algebra.finset.Sum_insert_of_mem f H theorem Sum_insert_of_not_mem (f : A → real) {a : A} {s : finset A} (H : a ∉ s) : Sum (insert a s) f = f a + Sum s f := algebra.finset.Sum_insert_of_not_mem f H theorem Sum_union (f : A → real) {s₁ s₂ : finset A} (disj : s₁ ∩ s₂ = ∅) : Sum (s₁ ∪ s₂) f = Sum s₁ f + Sum s₂ f := algebra.finset.Sum_union f disj theorem Sum_ext {s : finset A} {f g : A → real} (H : ∀x, x ∈ s → f x = g x) : Sum s f = Sum s g := algebra.finset.Sum_ext H theorem Sum_zero (s : finset A) : Sum s (λ x, 0) = 0 := algebra.finset.Sum_zero s end deceqA end finset /- Sum over a set -/ namespace set open set noncomputable definition Sum (s : set A) (f : A → real) : real := algebra.set.Sum s f notation `∏` binders `∈` s, r:(scoped f, Sum s f) := r theorem Sum_empty (f : A → real) : Sum ∅ f = 0 := algebra.set.Sum_empty f theorem Sum_of_not_finite {s : set A} (nfins : ¬ finite s) (f : A → real) : Sum s f = 0 := algebra.set.Sum_of_not_finite nfins f theorem Sum_add (s : set A) (f g : A → real) : Sum s (λx, f x + g x) = Sum s f + Sum s g := algebra.set.Sum_add s f g theorem Sum_insert_of_mem (f : A → real) {a : A} {s : set A} (H : a ∈ s) : Sum (insert a s) f = Sum s f := algebra.set.Sum_insert_of_mem f H theorem Sum_insert_of_not_mem (f : A → real) {a : A} {s : set A} [fins : finite s] (H : a ∉ s) : Sum (insert a s) f = f a + Sum s f := algebra.set.Sum_insert_of_not_mem f H theorem Sum_union (f : A → real) {s₁ s₂ : set A} [fins₁ : finite s₁] [fins₂ : finite s₂] (disj : s₁ ∩ s₂ = ∅) : Sum (s₁ ∪ s₂) f = Sum s₁ f + Sum s₂ f := algebra.set.Sum_union f disj theorem Sum_ext {s : set A} {f g : A → real} (H : ∀x, x ∈ s → f x = g x) : Sum s f = Sum s g := algebra.set.Sum_ext H theorem Sum_zero (s : set A) : Sum s (λ x, 0) = 0 := algebra.set.Sum_zero s end set end real
7ecc1eb45f4e18107500edf8d0cfdc211db0b40e
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/record2.lean
34a47978d9fde6c6795475cdf94ccb0dc9ca1ad3
[ "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
283
lean
import logic data.unit set_option pp.universes true section parameter (A : Type) section parameter (B : Type) structure point := mk :: (x : A) (y : B) check point check point.mk check point.x end check point check point.mk check point.x end
10fd234eb083ac394a39173f27ee60ff40412dff
30b012bb72d640ec30c8fdd4c45fdfa67beb012c
/group_theory/subgroup.lean
9b076b885a9bc23796136102aea92dbc84603aa8
[ "Apache-2.0" ]
permissive
kckennylau/mathlib
21fb810b701b10d6606d9002a4004f7672262e83
47b3477e20ffb5a06588dd3abb01fe0fe3205646
refs/heads/master
1,634,976,409,281
1,542,042,832,000
1,542,319,733,000
109,560,458
0
0
Apache-2.0
1,542,369,208,000
1,509,867,494,000
Lean
UTF-8
Lean
false
false
18,485
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, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro -/ import group_theory.submonoid open set function variables {α : Type*} {β : Type*} {a a₁ a₂ b c: α} section group variables [group α] [add_group β] @[to_additive injective_add] lemma injective_mul {a : α} : injective ((*) a) := assume a₁ a₂ h, have a⁻¹ * a * a₁ = a⁻¹ * a * a₂, by rw [mul_assoc, mul_assoc, h], by rwa [inv_mul_self, one_mul, one_mul] at this /-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/ class is_subgroup (s : set α) extends is_submonoid s : Prop := (inv_mem {a} : a ∈ s → a⁻¹ ∈ s) /-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/ class is_add_subgroup (s : set β) extends is_add_submonoid s : Prop := (neg_mem {a} : a ∈ s → -a ∈ s) attribute [to_additive is_add_subgroup] is_subgroup attribute [to_additive is_add_subgroup.to_is_add_submonoid] is_subgroup.to_is_submonoid attribute [to_additive is_add_subgroup.neg_mem] is_subgroup.inv_mem attribute [to_additive is_add_subgroup.mk] is_subgroup.mk instance additive.is_add_subgroup (s : set α) [is_subgroup s] : @is_add_subgroup (additive α) _ s := ⟨@is_subgroup.inv_mem _ _ _ _⟩ theorem additive.is_add_subgroup_iff {s : set α} : @is_add_subgroup (additive α) _ s ↔ is_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk α _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by resetI; apply_instance⟩ instance multiplicative.is_subgroup (s : set β) [is_add_subgroup s] : @is_subgroup (multiplicative β) _ s := ⟨@is_add_subgroup.neg_mem _ _ _ _⟩ theorem multiplicative.is_subgroup_iff {s : set β} : @is_subgroup (multiplicative β) _ s ↔ is_add_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk β _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by resetI; apply_instance⟩ instance subtype.group {s : set α} [is_subgroup s] : group s := by subtype_instance instance subtype.add_group {s : set β} [is_add_subgroup s] : add_group s := by subtype_instance attribute [to_additive subtype.add_group] subtype.group @[simp, to_additive is_add_subgroup.coe_neg] lemma is_subgroup.coe_inv {s : set α} [is_subgroup s] (a : s) : ((a⁻¹ : s) : α) = a⁻¹ := rfl @[simp] lemma is_subgroup.coe_gpow {s : set α} [is_subgroup s] (a : s) (n : ℤ) : ((a ^ n : s) : α) = a ^ n := by induction n; simp [is_submonoid.coe_pow a] @[simp] lemma is_add_subgroup.gsmul_coe {β : Type*} [add_group β] {s : set β} [is_add_subgroup s] (a : s) (n : ℤ) : ((gsmul n a : s) : β) = gsmul n a := by induction n; simp [is_add_submonoid.smul_coe a] attribute [to_additive is_add_subgroup.gsmul_coe] is_subgroup.coe_gpow theorem is_subgroup.of_div (s : set α) (one_mem : (1:α) ∈ s) (div_mem : ∀{a b:α}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s): is_subgroup s := have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from assume a ha, have 1 * a⁻¹ ∈ s, from div_mem one_mem ha, by simpa, { inv_mem := inv_mem, mul_mem := assume a b ha hb, have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb), by simpa, one_mem := one_mem } theorem is_add_subgroup.of_sub (s : set β) (zero_mem : (0:β) ∈ s) (sub_mem : ∀{a b:β}, a ∈ s → b ∈ s → a - b ∈ s): is_add_subgroup s := multiplicative.is_subgroup_iff.1 $ @is_subgroup.of_div (multiplicative β) _ _ zero_mem @sub_mem def gpowers (x : α) : set α := {y | ∃i:ℤ, x^i = y} def gmultiples (x : β) : set β := {y | ∃i:ℤ, gsmul i x = y} attribute [to_additive gmultiples] gpowers instance gpowers.is_subgroup (x : α) : is_subgroup (gpowers x) := { one_mem := ⟨(0:ℤ), by simp⟩, mul_mem := assume x₁ x₂ ⟨i₁, h₁⟩ ⟨i₂, h₂⟩, ⟨i₁ + i₂, by simp [gpow_add, *]⟩, inv_mem := assume x₀ ⟨i, h⟩, ⟨-i, by simp [h.symm]⟩ } instance gmultiples.is_add_subgroup (x : β) : is_add_subgroup (gmultiples x) := multiplicative.is_subgroup_iff.1 $ gpowers.is_subgroup _ attribute [to_additive gmultiples.is_add_subgroup] gpowers.is_subgroup lemma is_subgroup.gpow_mem {a : α} {s : set α} [is_subgroup s] (h : a ∈ s) : ∀{i:ℤ}, a ^ i ∈ s | (n : ℕ) := is_submonoid.pow_mem h | -[1+ n] := is_subgroup.inv_mem (is_submonoid.pow_mem h) lemma is_add_subgroup.gsmul_mem {a : β} {s : set β} [is_add_subgroup s] : a ∈ s → ∀{i:ℤ}, gsmul i a ∈ s := @is_subgroup.gpow_mem (multiplicative β) _ _ _ _ lemma mem_gpowers {a : α} : a ∈ gpowers a := ⟨1, by simp⟩ lemma mem_gmultiples {a : β} : a ∈ gmultiples a := ⟨1, by simp⟩ attribute [to_additive mem_gmultiples] mem_gpowers end group namespace is_subgroup open is_submonoid variables [group α] (s : set α) [is_subgroup s] @[to_additive is_add_subgroup.neg_mem_iff] lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s := ⟨λ h, by simpa using inv_mem h, inv_mem⟩ @[to_additive is_add_subgroup.add_mem_cancel_left] lemma mul_mem_cancel_left (h : a ∈ s) : b * a ∈ s ↔ b ∈ s := ⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩ @[to_additive is_add_subgroup.add_mem_cancel_right] lemma mul_mem_cancel_right (h : a ∈ s) : a * b ∈ s ↔ b ∈ s := ⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩ end is_subgroup theorem is_add_subgroup.sub_mem {α} [add_group α] (s : set α) [is_add_subgroup s] (a b : α) (ha : a ∈ s) (hb : b ∈ s) : a - b ∈ s := is_add_submonoid.add_mem ha (is_add_subgroup.neg_mem hb) namespace group open is_submonoid is_subgroup variables [group α] {s : set α} inductive in_closure (s : set α) : α → Prop | basic {a : α} : a ∈ s → in_closure a | one : in_closure 1 | inv {a : α} : in_closure a → in_closure a⁻¹ | mul {a b : α} : in_closure a → in_closure b → in_closure (a * b) /-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ def closure (s : set α) : set α := {a | in_closure s a } lemma mem_closure {a : α} : a ∈ s → a ∈ closure s := in_closure.basic instance closure.is_subgroup (s : set α) : is_subgroup (closure s) := { one_mem := in_closure.one s, mul_mem := assume a b, in_closure.mul, inv_mem := assume a, in_closure.inv } theorem subset_closure {s : set α} : s ⊆ closure s := λ a, mem_closure theorem closure_subset {s t : set α} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff] lemma closure_subset_iff (s t : set α) [is_subgroup t] : closure s ⊆ t ↔ s ⊆ t := ⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset h ha⟩ theorem gpowers_eq_closure {a : α} : gpowers a = closure {a} := subset.antisymm (assume x h, match x, h with _, ⟨i, rfl⟩ := gpow_mem (mem_closure $ by simp) end) (closure_subset $ by simp [mem_gpowers]) end group namespace add_group open is_add_submonoid is_add_subgroup variables [add_group α] {s : set α} /-- `add_group.closure s` is the additive subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ def closure (s : set α) : set α := @group.closure (multiplicative α) _ s attribute [to_additive add_group.closure] group.closure lemma mem_closure {a : α} : a ∈ s → a ∈ closure s := group.mem_closure attribute [to_additive add_group.mem_closure] group.mem_closure instance closure.is_add_subgroup (s : set α) : is_add_subgroup (closure s) := multiplicative.is_subgroup_iff.1 $ group.closure.is_subgroup _ attribute [to_additive add_group.closure.is_add_subgroup] group.closure.is_subgroup attribute [to_additive add_group.subset_closure] group.subset_closure theorem closure_subset {s t : set α} [is_add_subgroup t] : s ⊆ t → closure s ⊆ t := group.closure_subset attribute [to_additive add_group.closure_subset] group.closure_subset attribute [to_additive add_group.closure_subset_iff] group.closure_subset_iff theorem gmultiples_eq_closure {a : α} : gmultiples a = closure {a} := group.gpowers_eq_closure attribute [to_additive add_group.gmultiples_eq_closure] group.gpowers_eq_closure @[elab_as_eliminator] theorem in_closure.rec_on {C : α → Prop} {a : α} (H : a ∈ closure s) (H1 : ∀ {a : α}, a ∈ s → C a) (H2 : C 0) (H3 : ∀ {a : α}, a ∈ closure s → C a → C (-a)) (H4 : ∀ {a b : α}, a ∈ closure s → b ∈ closure s → C a → C b → C (a + b)) : C a := group.in_closure.rec_on H (λ _, H1) H2 (λ _, H3) (λ _ _, H4) end add_group class normal_subgroup [group α] (s : set α) extends is_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g * n * g⁻¹ ∈ s) class normal_add_subgroup [add_group α] (s : set α) extends is_add_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : α, g + n - g ∈ s) attribute [to_additive normal_add_subgroup] normal_subgroup attribute [to_additive normal_add_subgroup.to_is_add_subgroup] normal_subgroup.to_is_subgroup attribute [to_additive normal_add_subgroup.normal] normal_subgroup.normal attribute [to_additive normal_add_subgroup.mk] normal_subgroup.mk @[to_additive normal_add_subgroup_of_add_comm_group] lemma normal_subgroup_of_comm_group [comm_group α] (s : set α) [hs : is_subgroup s] : normal_subgroup s := { normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul], ..hs } instance additive.normal_add_subgroup [group α] (s : set α) [normal_subgroup s] : @normal_add_subgroup (additive α) _ s := ⟨@normal_subgroup.normal _ _ _ _⟩ theorem additive.normal_add_subgroup_iff [group α] {s : set α} : @normal_add_subgroup (additive α) _ s ↔ normal_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_subgroup.mk α _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂, λ h, by resetI; apply_instance⟩ instance multiplicative.normal_subgroup [add_group α] (s : set α) [normal_add_subgroup s] : @normal_subgroup (multiplicative α) _ s := ⟨@normal_add_subgroup.normal _ _ _ _⟩ theorem multiplicative.normal_subgroup_iff [add_group α] {s : set α} : @normal_subgroup (multiplicative α) _ s ↔ normal_add_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_add_subgroup.mk α _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂, λ h, by resetI; apply_instance⟩ namespace is_subgroup variable [group α] -- Normal subgroup properties lemma mem_norm_comm {s : set α} [normal_subgroup s] {a b : α} (hab : a * b ∈ s) : b * a ∈ s := have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹, by simp at h; exact h lemma mem_norm_comm_iff {s : set α} [normal_subgroup s] {a b : α} : a * b ∈ s ↔ b * a ∈ s := ⟨mem_norm_comm, mem_norm_comm⟩ /-- The trivial subgroup -/ def trivial (α : Type*) [group α] : set α := {1} @[simp] lemma mem_trivial [group α] {g : α} : g ∈ trivial α ↔ g = 1 := mem_singleton_iff instance trivial_normal : normal_subgroup (trivial α) := by refine {..}; simp [trivial] {contextual := tt} lemma trivial_eq_closure : trivial α = group.closure ∅ := subset.antisymm (by simp [set.subset_def, is_submonoid.one_mem]) (group.closure_subset $ by simp) instance univ_subgroup : normal_subgroup (@univ α) := by refine {..}; simp def center (α : Type*) [group α] : set α := {z | ∀ g, g * z = z * g} lemma mem_center {a : α} : a ∈ center α ↔ ∀g, g * a = a * g := iff.rfl instance center_normal : normal_subgroup (center α) := { one_mem := by simp [center], mul_mem := assume a b ha hb g, by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc], inv_mem := assume a ha g, calc g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g] ... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp, normal := assume n ha g h, calc h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc] ... = g * g⁻¹ * n * h : by rw ha h; simp ... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] } end is_subgroup namespace is_add_subgroup variable [add_group α] attribute [to_additive is_add_subgroup.mem_norm_comm] is_subgroup.mem_norm_comm attribute [to_additive is_add_subgroup.mem_norm_comm_iff] is_subgroup.mem_norm_comm_iff /-- The trivial subgroup -/ def trivial (α : Type*) [add_group α] : set α := {0} attribute [to_additive is_add_subgroup.trivial] is_subgroup.trivial attribute [to_additive is_add_subgroup.mem_trivial] is_subgroup.mem_trivial instance trivial_normal : normal_add_subgroup (trivial α) := multiplicative.normal_subgroup_iff.1 is_subgroup.trivial_normal attribute [to_additive is_add_subgroup.trivial_normal] is_subgroup.trivial_normal attribute [to_additive is_add_subgroup.trivial_eq_closure] is_subgroup.trivial_eq_closure instance univ_add_subgroup : normal_add_subgroup (@univ α) := multiplicative.normal_subgroup_iff.1 is_subgroup.univ_subgroup attribute [to_additive is_add_subgroup.univ_add_subgroup] is_subgroup.univ_subgroup def center (α : Type*) [add_group α] : set α := {z | ∀ g, g + z = z + g} attribute [to_additive is_add_subgroup.center] is_subgroup.center attribute [to_additive is_add_subgroup.mem_center] is_subgroup.mem_center instance center_normal : normal_add_subgroup (center α) := multiplicative.normal_subgroup_iff.1 is_subgroup.center_normal end is_add_subgroup -- Homomorphism subgroups namespace is_group_hom open is_submonoid is_subgroup variables [group α] [group β] @[to_additive is_add_group_hom.ker] def ker (f : α → β) [is_group_hom f] : set α := preimage f (trivial β) attribute [to_additive is_add_group_hom.ker.equations._eqn_1] ker.equations._eqn_1 @[to_additive is_add_group_hom.mem_ker] lemma mem_ker (f : α → β) [is_group_hom f] {x : α} : x ∈ ker f ↔ f x = 1 := mem_trivial @[to_additive is_add_group_hom.zero_ker_neg] lemma one_ker_inv (f : α → β) [is_group_hom f] {a b : α} (h : f (a * b⁻¹) = 1) : f a = f b := begin rw [mul f, inv f] at h, rw [←inv_inv (f b), eq_inv_of_mul_eq_one h] end @[to_additive is_add_group_hom.neg_ker_zero] lemma inv_ker_one (f : α → β) [is_group_hom f] {a b : α} (h : f a = f b) : f (a * b⁻¹) = 1 := have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv], by rwa [←inv f, ←mul f] at this @[to_additive is_add_group_hom.zero_iff_ker_neg] lemma one_iff_ker_inv (f : α → β) [is_group_hom f] (a b : α) : f a = f b ↔ f (a * b⁻¹) = 1 := ⟨inv_ker_one f, one_ker_inv f⟩ @[to_additive is_add_group_hom.neg_iff_ker] lemma inv_iff_ker (f : α → β) [w : is_group_hom f] (a b : α) : f a = f b ↔ a * b⁻¹ ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv _ _ _ instance image_subgroup (f : α → β) [is_group_hom f] (s : set α) [is_subgroup s] : is_subgroup (f '' s) := { mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩, ⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, mul f]⟩, one_mem := ⟨1, one_mem s, one f⟩, inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw inv f; simp *⟩ } attribute [to_additive is_add_group_hom.image_add_subgroup._match_1] is_group_hom.image_subgroup._match_1 attribute [to_additive is_add_group_hom.image_add_subgroup._match_2] is_group_hom.image_subgroup._match_2 attribute [to_additive is_add_group_hom.image_add_subgroup._match_3] is_group_hom.image_subgroup._match_3 attribute [to_additive is_add_group_hom.image_add_subgroup] is_group_hom.image_subgroup attribute [to_additive is_add_group_hom.image_add_subgroup._match_1.equations._eqn_1] is_group_hom.image_subgroup._match_1.equations._eqn_1 attribute [to_additive is_add_group_hom.image_add_subgroup._match_2.equations._eqn_1] is_group_hom.image_subgroup._match_2.equations._eqn_1 attribute [to_additive is_add_group_hom.image_add_subgroup._match_3.equations._eqn_1] is_group_hom.image_subgroup._match_3.equations._eqn_1 attribute [to_additive is_add_group_hom.image_add_subgroup.equations._eqn_1] is_group_hom.image_subgroup.equations._eqn_1 instance range_subgroup (f : α → β) [is_group_hom f] : is_subgroup (set.range f) := @set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ attribute [to_additive is_add_group_hom.range_add_subgroup] is_group_hom.range_subgroup attribute [to_additive is_add_group_hom.range_add_subgroup.equations._eqn_1] is_group_hom.range_subgroup.equations._eqn_1 local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal instance preimage (f : α → β) [is_group_hom f] (s : set β) [is_subgroup s] : is_subgroup (f ⁻¹' s) := by refine {..}; simp [mul f, one f, inv f, @inv_mem β _ s] {contextual:=tt} attribute [to_additive is_add_group_hom.preimage] is_group_hom.preimage attribute [to_additive is_add_group_hom.preimage.equations._eqn_1] is_group_hom.preimage.equations._eqn_1 instance preimage_normal (f : α → β) [is_group_hom f] (s : set β) [normal_subgroup s] : normal_subgroup (f ⁻¹' s) := ⟨by simp [mul f, inv f] {contextual:=tt}⟩ attribute [to_additive is_add_group_hom.preimage_normal] is_group_hom.preimage_normal attribute [to_additive is_add_group_hom.preimage_normal.equations._eqn_1] is_group_hom.preimage_normal.equations._eqn_1 instance normal_subgroup_ker (f : α → β) [is_group_hom f] : normal_subgroup (ker f) := is_group_hom.preimage_normal f (trivial β) attribute [to_additive is_add_group_hom.normal_subgroup_ker] is_group_hom.normal_subgroup_ker attribute [to_additive is_add_group_hom.normal_subgroup_ker.equations._eqn_1] is_group_hom.normal_subgroup_ker.equations._eqn_1 lemma inj_of_trivial_ker (f : α → β) [is_group_hom f] (h : ker f = trivial α) : function.injective f := begin intros a₁ a₂ hfa, simp [ext_iff, ker, is_subgroup.trivial] at h, have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa, rw [eq_inv_of_mul_eq_one ha, inv_inv a₂] end lemma trivial_ker_of_inj (f : α → β) [is_group_hom f] (h : function.injective f) : ker f = trivial α := set.ext $ assume x, iff.intro (assume hx, suffices f x = f 1, by simpa using h this, by simp [one f]; rwa [mem_ker] at hx) (by simp [mem_ker, is_group_hom.one f] {contextual := tt}) lemma inj_iff_trivial_ker (f : α → β) [is_group_hom f] : function.injective f ↔ ker f = trivial α := ⟨trivial_ker_of_inj f, inj_of_trivial_ker f⟩ end is_group_hom
434259818357ba097b34fbd6f480fe2e9841faba
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/data/list/sort.lean
4a1d23d70838e7ccab97725a32e3f5980d6ec6e8
[ "Apache-2.0" ]
permissive
vaibhavkarve/mathlib
a574aaf68c0a431a47fa82ce0637f0f769826bfe
17f8340912468f49bdc30acdb9a9fa02eeb0473a
refs/heads/master
1,621,263,802,637
1,585,399,588,000
1,585,399,588,000
250,833,447
0
0
Apache-2.0
1,585,410,341,000
1,585,410,341,000
null
UTF-8
Lean
false
false
11,453
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad Insertion sort and merge sort. -/ import data.list.perm open list.perm namespace list section sorted universe variable uu variables {α : Type uu} {r : α → α → Prop} /-- `sorted r l` is the same as `pairwise r l`, preferred in the case that `r` is a `<` or `≤`-like relation (transitive and antisymmetric or asymmetric) -/ def sorted := @pairwise @[simp] theorem sorted_nil : sorted r [] := pairwise.nil theorem sorted_of_sorted_cons {a : α} {l : list α} : sorted r (a :: l) → sorted r l := pairwise_of_pairwise_cons theorem sorted.tail {r : α → α → Prop} : Π {l : list α}, sorted r l → sorted r l.tail | [] h := h | (hd :: tl) h := sorted_of_sorted_cons h theorem rel_of_sorted_cons {a : α} {l : list α} : sorted r (a :: l) → ∀ b ∈ l, r a b := rel_of_pairwise_cons @[simp] theorem sorted_cons {a : α} {l : list α} : sorted r (a :: l) ↔ (∀ b ∈ l, r a b) ∧ sorted r l := pairwise_cons theorem eq_of_sorted_of_perm [is_antisymm α r] {l₁ l₂ : list α} (p : l₁ ~ l₂) (s₁ : sorted r l₁) (s₂ : sorted r l₂) : l₁ = l₂ := begin induction s₁ with a l₁ h₁ s₁ IH generalizing l₂, { rw eq_nil_of_perm_nil p }, { have : a ∈ l₂ := perm_subset p (mem_cons_self _ _), rcases mem_split this with ⟨u₂, v₂, rfl⟩, have p' := (perm_cons a).1 (p.trans perm_middle), have := IH p' (pairwise_of_sublist (by simp) s₂), subst l₁, change a::u₂ ++ v₂ = u₂ ++ ([a] ++ v₂), rw ← append_assoc, congr, have : ∀ (x : α) (h : x ∈ u₂), x = a := λ x m, antisymm ((pairwise_append.1 s₂).2.2 _ m a (mem_cons_self _ _)) (h₁ _ (by simp [m])), rw [(@eq_repeat _ a (length u₂ + 1) (a::u₂)).2, (@eq_repeat _ a (length u₂ + 1) (u₂++[a])).2]; split; simp [iff_true_intro this, or_comm] } end @[simp] theorem sorted_singleton (a : α) : sorted r [a] := pairwise_singleton _ _ lemma nth_le_of_sorted_of_le [is_refl α r] {l : list α} (h : l.sorted r) {a b : ℕ} {ha : a < l.length} {hb : b < l.length} (hab : a ≤ b) : r (l.nth_le a ha) (l.nth_le b hb) := begin cases eq_or_lt_of_le hab with H H, { induction H, exact refl _ }, { exact list.pairwise_iff_nth_le.1 h a b hb H } end end sorted /- sorting procedures -/ section sort universe variable uu parameters {α : Type uu} (r : α → α → Prop) [decidable_rel r] local infix ` ≼ ` : 50 := r /- insertion sort -/ section insertion_sort /-- `ordered_insert a l` inserts `a` into `l` at such that `ordered_insert a l` is sorted if `l` is. -/ @[simp] def ordered_insert (a : α) : list α → list α | [] := [a] | (b :: l) := if a ≼ b then a :: b :: l else b :: ordered_insert l /-- `insertion_sort l` returns `l` sorted using the insertion sort algorithm. -/ @[simp] def insertion_sort : list α → list α | [] := [] | (b :: l) := ordered_insert b (insertion_sort l) @[simp] lemma ordered_insert_nil (a : α) : [].ordered_insert r a = [a] := rfl theorem ordered_insert_length : Π (L : list α) (a : α), (L.ordered_insert r a).length = L.length + 1 | [] a := rfl | (hd :: tl) a := by { dsimp [ordered_insert], split_ifs; simp [ordered_insert_length], } section correctness open perm theorem perm_ordered_insert (a) : ∀ l : list α, ordered_insert a l ~ a :: l | [] := perm.refl _ | (b :: l) := by by_cases a ≼ b; [simp [ordered_insert, h], simpa [ordered_insert, h] using (perm.skip _ (perm_ordered_insert l)).trans (perm.swap _ _ _)] theorem ordered_insert_count [decidable_eq α] (L : list α) (a b : α) : count a (L.ordered_insert r b) = count a L + if (a = b) then 1 else 0 := begin rw [perm_count (L.perm_ordered_insert r b), count_cons], split_ifs; simp only [nat.succ_eq_add_one, add_zero], end theorem perm_insertion_sort : ∀ l : list α, insertion_sort l ~ l | [] := perm.nil | (b :: l) := by simpa [insertion_sort] using (perm_ordered_insert _ _ _).trans (perm.skip b (perm_insertion_sort l)) section total_and_transitive variables [is_total α r] [is_trans α r] theorem sorted_ordered_insert (a : α) : ∀ l, sorted r l → sorted r (ordered_insert a l) | [] h := sorted_singleton a | (b :: l) h := begin by_cases h' : a ≼ b, { simpa [ordered_insert, h', h] using λ b' bm, trans h' (rel_of_sorted_cons h _ bm) }, { suffices : ∀ (b' : α), b' ∈ ordered_insert r a l → r b b', { simpa [ordered_insert, h', sorted_ordered_insert l (sorted_of_sorted_cons h)] }, intros b' bm, cases (show b' = a ∨ b' ∈ l, by simpa using perm_subset (perm_ordered_insert _ _ _) bm) with be bm, { subst b', exact (total_of r _ _).resolve_left h' }, { exact rel_of_sorted_cons h _ bm } } end theorem sorted_insertion_sort : ∀ l, sorted r (insertion_sort l) | [] := sorted_nil | (a :: l) := sorted_ordered_insert a _ (sorted_insertion_sort l) end total_and_transitive end correctness end insertion_sort /- merge sort -/ section merge_sort -- TODO(Jeremy): observation: if instead we write (a :: (split l).1, b :: (split l).2), the -- equation compiler can't prove the third equation /-- Split `l` into two lists of approximately equal length. split [1, 2, 3, 4, 5] = ([1, 3, 5], [2, 4]) -/ @[simp] def split : list α → list α × list α | [] := ([], []) | (a :: l) := let (l₁, l₂) := split l in (a :: l₂, l₁) theorem split_cons_of_eq (a : α) {l l₁ l₂ : list α} (h : split l = (l₁, l₂)) : split (a :: l) = (a :: l₂, l₁) := by rw [split, h]; refl theorem length_split_le : ∀ {l l₁ l₂ : list α}, split l = (l₁, l₂) → length l₁ ≤ length l ∧ length l₂ ≤ length l | [] ._ ._ rfl := ⟨nat.le_refl 0, nat.le_refl 0⟩ | (a::l) l₁' l₂' h := begin cases e : split l with l₁ l₂, injection (split_cons_of_eq _ e).symm.trans h, substs l₁' l₂', cases length_split_le e with h₁ h₂, exact ⟨nat.succ_le_succ h₂, nat.le_succ_of_le h₁⟩ end theorem length_split_lt {a b} {l l₁ l₂ : list α} (h : split (a::b::l) = (l₁, l₂)) : length l₁ < length (a::b::l) ∧ length l₂ < length (a::b::l) := begin cases e : split l with l₁' l₂', injection (split_cons_of_eq _ (split_cons_of_eq _ e)).symm.trans h, substs l₁ l₂, cases length_split_le e with h₁ h₂, exact ⟨nat.succ_le_succ (nat.succ_le_succ h₁), nat.succ_le_succ (nat.succ_le_succ h₂)⟩ end theorem perm_split : ∀ {l l₁ l₂ : list α}, split l = (l₁, l₂) → l ~ l₁ ++ l₂ | [] ._ ._ rfl := perm.refl _ | (a::l) l₁' l₂' h := begin cases e : split l with l₁ l₂, injection (split_cons_of_eq _ e).symm.trans h, substs l₁' l₂', exact perm.skip a ((perm_split e).trans perm_app_comm), end /-- Merge two sorted lists into one in linear time. merge [1, 2, 4, 5] [0, 1, 3, 4] = [0, 1, 1, 2, 3, 4, 4, 5] -/ def merge : list α → list α → list α | [] l' := l' | l [] := l | (a :: l) (b :: l') := if a ≼ b then a :: merge l (b :: l') else b :: merge (a :: l) l' using_well_founded wf_tacs include r /-- Implementation of a merge sort algorithm to sort a list. -/ def merge_sort : list α → list α | [] := [] | [a] := [a] | (a::b::l) := begin cases e : split (a::b::l) with l₁ l₂, cases length_split_lt e with h₁ h₂, exact merge r (merge_sort l₁) (merge_sort l₂) end using_well_founded { rel_tac := λ_ _, `[exact ⟨_, inv_image.wf length nat.lt_wf⟩], dec_tac := tactic.assumption } theorem merge_sort_cons_cons {a b} {l l₁ l₂ : list α} (h : split (a::b::l) = (l₁, l₂)) : merge_sort (a::b::l) = merge (merge_sort l₁) (merge_sort l₂) := begin suffices : ∀ (L : list α) h1, @@and.rec (λ a a (_ : length l₁ < length l + 1 + 1 ∧ length l₂ < length l + 1 + 1), L) h1 h1 = L, { simp [merge_sort, h], apply this }, intros, cases h1, refl end section correctness theorem perm_merge : ∀ (l l' : list α), merge l l' ~ l ++ l' | [] [] := perm.nil | [] (b :: l') := by simp [merge] | (a :: l) [] := by simp [merge] | (a :: l) (b :: l') := begin by_cases a ≼ b, { simpa [merge, h] using skip _ (perm_merge _ _) }, { suffices : b :: merge r (a :: l) l' ~ a :: (l ++ b :: l'), {simpa [merge, h]}, exact (skip _ (perm_merge _ _)).trans ((swap _ _ _).trans (skip _ perm_middle.symm)) } end using_well_founded wf_tacs theorem perm_merge_sort : ∀ l : list α, merge_sort l ~ l | [] := perm.refl _ | [a] := perm.refl _ | (a::b::l) := begin cases e : split (a::b::l) with l₁ l₂, cases length_split_lt e with h₁ h₂, rw [merge_sort_cons_cons r e], apply (perm_merge r _ _).trans, exact (perm_app (perm_merge_sort l₁) (perm_merge_sort l₂)).trans (perm_split e).symm end using_well_founded { rel_tac := λ_ _, `[exact ⟨_, inv_image.wf length nat.lt_wf⟩], dec_tac := tactic.assumption } @[simp] lemma length_merge_sort (l : list α) : (merge_sort l).length = l.length := perm_length (perm_merge_sort _) section total_and_transitive variables [is_total α r] [is_trans α r] theorem sorted_merge : ∀ {l l' : list α}, sorted r l → sorted r l' → sorted r (merge l l') | [] [] h₁ h₂ := sorted_nil | [] (b :: l') h₁ h₂ := by simpa [merge] using h₂ | (a :: l) [] h₁ h₂ := by simpa [merge] using h₁ | (a :: l) (b :: l') h₁ h₂ := begin by_cases a ≼ b, { suffices : ∀ (b' : α) (_ : b' ∈ merge r l (b :: l')), r a b', { simpa [merge, h, sorted_merge (sorted_of_sorted_cons h₁) h₂] }, intros b' bm, rcases (show b' = b ∨ b' ∈ l ∨ b' ∈ l', by simpa [or.left_comm] using perm_subset (perm_merge _ _ _) bm) with be | bl | bl', { subst b', assumption }, { exact rel_of_sorted_cons h₁ _ bl }, { exact trans h (rel_of_sorted_cons h₂ _ bl') } }, { suffices : ∀ (b' : α) (_ : b' ∈ merge r (a :: l) l'), r b b', { simpa [merge, h, sorted_merge h₁ (sorted_of_sorted_cons h₂)] }, intros b' bm, have ba : b ≼ a := (total_of r _ _).resolve_left h, rcases (show b' = a ∨ b' ∈ l ∨ b' ∈ l', by simpa using perm_subset (perm_merge _ _ _) bm) with be | bl | bl', { subst b', assumption }, { exact trans ba (rel_of_sorted_cons h₁ _ bl) }, { exact rel_of_sorted_cons h₂ _ bl' } } end using_well_founded wf_tacs theorem sorted_merge_sort : ∀ l : list α, sorted r (merge_sort l) | [] := sorted_nil | [a] := sorted_singleton _ | (a::b::l) := begin cases e : split (a::b::l) with l₁ l₂, cases length_split_lt e with h₁ h₂, rw [merge_sort_cons_cons r e], exact sorted_merge r (sorted_merge_sort l₁) (sorted_merge_sort l₂) end using_well_founded { rel_tac := λ_ _, `[exact ⟨_, inv_image.wf length nat.lt_wf⟩], dec_tac := tactic.assumption } theorem merge_sort_eq_self [is_antisymm α r] {l : list α} : sorted r l → merge_sort l = l := eq_of_sorted_of_perm (perm_merge_sort _) (sorted_merge_sort _) end total_and_transitive end correctness end merge_sort end sort /- try them out! -/ --#eval insertion_sort (λ m n : ℕ, m ≤ n) [5, 27, 221, 95, 17, 43, 7, 2, 98, 567, 23, 12] --#eval merge_sort (λ m n : ℕ, m ≤ n) [5, 27, 221, 95, 17, 43, 7, 2, 98, 567, 23, 12] end list
ba3013bf010ed11fd9c12e6fc5f683386fea07bf
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/init/meta/vm_auto.lean
cdac47395612efba74ab0c59d7f65054addc55ee
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
1,805
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.meta.tactic import Mathlib.Lean3Lib.init.control.option import Mathlib.Lean3Lib.init.meta.mk_dec_eq_instance universes l namespace Mathlib inductive vm_obj_kind where | simple : vm_obj_kind | constructor : vm_obj_kind | closure : vm_obj_kind | native_closure : vm_obj_kind | mpz : vm_obj_kind | name : vm_obj_kind | level : vm_obj_kind | expr : vm_obj_kind | declaration : vm_obj_kind | environment : vm_obj_kind | tactic_state : vm_obj_kind | format : vm_obj_kind | options : vm_obj_kind | other : vm_obj_kind namespace vm_obj /-- For simple and constructor vm_obj's, it returns the constructor tag/index. Return 0 otherwise. -/ /-- For closure vm_obj's, it returns the internal function index. -/ /-- For constructor vm_obj's, it returns the data stored in the object. For closure vm_obj's, it returns the local arguments captured by the closure. -/ /-- For simple and mpz vm_obj's -/ /-- For name vm_obj's, it returns the name wrapped by the vm_obj. -/ /-- For level vm_obj's, it returns the universe level wrapped by the vm_obj. -/ /-- For expr vm_obj's, it returns the expression wrapped by the vm_obj. -/ /-- For declaration vm_obj's, it returns the declaration wrapped by the vm_obj. -/ /-- For environment vm_obj's, it returns the environment wrapped by the vm_obj. -/ /-- For tactic_state vm_obj's, it returns the tactic_state object wrapped by the vm_obj. -/ /-- For format vm_obj's, it returns the format object wrapped by the vm_obj. -/ end vm_obj inductive vm_decl_kind where | bytecode : vm_decl_kind | builtin : vm_decl_kind | cfun : vm_decl_kind end Mathlib
791475d59de938631c858b7909700501e7779f00
9dc8cecdf3c4634764a18254e94d43da07142918
/src/set_theory/cardinal/ordinal.lean
7503ee6a185adf1c363cd26442099692247f058e
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
47,886
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, Floris van Doorn -/ import data.finsupp.multiset import order.bounded import set_theory.ordinal.principal import tactic.linarith /-! # Cardinals and ordinals Relationships between cardinals and ordinals, properties of cardinals that are proved using ordinals. ## Main definitions * The function `cardinal.aleph'` gives the cardinals listed by their ordinal index, and is the inverse of `cardinal.aleph_idx`. `aleph' n = n`, `aleph' ω = ℵ₀`, `aleph' (ω + 1) = succ ℵ₀`, etc. It is an order isomorphism between ordinals and cardinals. * The function `cardinal.aleph` gives the infinite cardinals listed by their ordinal index. `aleph 0 = ℵ₀`, `aleph 1 = succ ℵ₀` is the first uncountable cardinal, and so on. * The function `cardinal.beth` enumerates the Beth cardinals. `beth 0 = ℵ₀`, `beth (succ o) = 2 ^ beth o`, and for a limit ordinal `o`, `beth o` is the supremum of `beth a` for `a < o`. ## Main Statements * `cardinal.mul_eq_max` and `cardinal.add_eq_max` state that the product (resp. sum) of two infinite cardinals is just their maximum. Several variations around this fact are also given. * `cardinal.mk_list_eq_mk` : when `α` is infinite, `α` and `list α` have the same cardinality. * simp lemmas for inequalities between `bit0 a` and `bit1 b` are registered, making `simp` able to prove inequalities about numeral cardinals. ## Tags cardinal arithmetic (for infinite cardinals) -/ noncomputable theory open function cardinal set equiv order open_locale classical cardinal ordinal universes u v w namespace cardinal section using_ordinals open ordinal theorem ord_is_limit {c} (co : ℵ₀ ≤ c) : (ord c).is_limit := begin refine ⟨λ h, aleph_0_ne_zero _, λ a, lt_imp_lt_of_le_imp_le (λ h, _)⟩, { rw [←ordinal.le_zero, ord_le] at h, simpa only [card_zero, nonpos_iff_eq_zero] using co.trans h }, { rw ord_le at h ⊢, rwa [←@add_one_of_aleph_0_le (card a), ←card_succ], rw [←ord_le, ←le_succ_of_is_limit, ord_le], { exact co.trans h }, { rw ord_aleph_0, exact omega_is_limit } } end /-! ### Aleph cardinals -/ /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) In this definition, we register additionally that this function is an initial segment, i.e., it is order preserving and its range is an initial segment of the ordinals. For the basic function version, see `aleph_idx`. For an upgraded version stating that the range is everything, see `aleph_idx.rel_iso`. -/ def aleph_idx.initial_seg : @initial_seg cardinal ordinal (<) (<) := @rel_embedding.collapse cardinal ordinal (<) (<) _ cardinal.ord.order_embedding.lt_embedding /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ω = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) For an upgraded version stating that the range is everything, see `aleph_idx.rel_iso`. -/ def aleph_idx : cardinal → ordinal := aleph_idx.initial_seg @[simp] theorem aleph_idx.initial_seg_coe : (aleph_idx.initial_seg : cardinal → ordinal) = aleph_idx := rfl @[simp] theorem aleph_idx_lt {a b} : aleph_idx a < aleph_idx b ↔ a < b := aleph_idx.initial_seg.to_rel_embedding.map_rel_iff @[simp] theorem aleph_idx_le {a b} : aleph_idx a ≤ aleph_idx b ↔ a ≤ b := by rw [← not_lt, ← not_lt, aleph_idx_lt] theorem aleph_idx.init {a b} : b < aleph_idx a → ∃ c, aleph_idx c = b := aleph_idx.initial_seg.init _ _ /-- The `aleph'` index function, which gives the ordinal index of a cardinal. (The `aleph'` part is because unlike `aleph` this counts also the finite stages. So `aleph_idx n = n`, `aleph_idx ℵ₀ = ω`, `aleph_idx ℵ₁ = ω + 1` and so on.) In this version, we register additionally that this function is an order isomorphism between cardinals and ordinals. For the basic function version, see `aleph_idx`. -/ def aleph_idx.rel_iso : @rel_iso cardinal.{u} ordinal.{u} (<) (<) := @rel_iso.of_surjective cardinal.{u} ordinal.{u} (<) (<) aleph_idx.initial_seg.{u} $ (initial_seg.eq_or_principal aleph_idx.initial_seg.{u}).resolve_right $ λ ⟨o, e⟩, begin have : ∀ c, aleph_idx c < o := λ c, (e _).2 ⟨_, rfl⟩, refine ordinal.induction_on o _ this, introsI α r _ h, let s := ⨆ a, inv_fun aleph_idx (ordinal.typein r a), apply (lt_succ s).not_le, have I : injective aleph_idx := aleph_idx.initial_seg.to_embedding.injective, simpa only [typein_enum, left_inverse_inv_fun I (succ s)] using le_csupr (cardinal.bdd_above_range.{u u} (λ a : α, inv_fun aleph_idx (ordinal.typein r a))) (ordinal.enum r _ (h (succ s))) end @[simp] theorem aleph_idx.rel_iso_coe : (aleph_idx.rel_iso : cardinal → ordinal) = aleph_idx := rfl @[simp] theorem type_cardinal : @type cardinal (<) _ = ordinal.univ.{u (u+1)} := by rw ordinal.univ_id; exact quotient.sound ⟨aleph_idx.rel_iso⟩ @[simp] theorem mk_cardinal : #cardinal = univ.{u (u+1)} := by simpa only [card_type, card_univ] using congr_arg card type_cardinal /-- The `aleph'` function gives the cardinals listed by their ordinal index, and is the inverse of `aleph_idx`. `aleph' n = n`, `aleph' ω = ω`, `aleph' (ω + 1) = succ ℵ₀`, etc. In this version, we register additionally that this function is an order isomorphism between ordinals and cardinals. For the basic function version, see `aleph'`. -/ def aleph'.rel_iso := cardinal.aleph_idx.rel_iso.symm /-- The `aleph'` function gives the cardinals listed by their ordinal index, and is the inverse of `aleph_idx`. `aleph' n = n`, `aleph' ω = ω`, `aleph' (ω + 1) = succ ℵ₀`, etc. -/ def aleph' : ordinal → cardinal := aleph'.rel_iso @[simp] theorem aleph'.rel_iso_coe : (aleph'.rel_iso : ordinal → cardinal) = aleph' := rfl @[simp] theorem aleph'_lt {o₁ o₂ : ordinal} : aleph' o₁ < aleph' o₂ ↔ o₁ < o₂ := aleph'.rel_iso.map_rel_iff @[simp] theorem aleph'_le {o₁ o₂ : ordinal} : aleph' o₁ ≤ aleph' o₂ ↔ o₁ ≤ o₂ := le_iff_le_iff_lt_iff_lt.2 aleph'_lt @[simp] theorem aleph'_aleph_idx (c : cardinal) : aleph' c.aleph_idx = c := cardinal.aleph_idx.rel_iso.to_equiv.symm_apply_apply c @[simp] theorem aleph_idx_aleph' (o : ordinal) : (aleph' o).aleph_idx = o := cardinal.aleph_idx.rel_iso.to_equiv.apply_symm_apply o @[simp] theorem aleph'_zero : aleph' 0 = 0 := by { rw [← nonpos_iff_eq_zero, ← aleph'_aleph_idx 0, aleph'_le], apply ordinal.zero_le } @[simp] theorem aleph'_succ {o : ordinal} : aleph' (succ o) = succ (aleph' o) := begin apply (succ_le_of_lt $ aleph'_lt.2 $ lt_succ o).antisymm' (cardinal.aleph_idx_le.1 $ _), rw [aleph_idx_aleph', succ_le_iff, ← aleph'_lt, aleph'_aleph_idx], apply lt_succ end @[simp] theorem aleph'_nat : ∀ n : ℕ, aleph' n = n | 0 := aleph'_zero | (n+1) := show aleph' (succ n) = n.succ, by rw [aleph'_succ, aleph'_nat, nat_succ] theorem aleph'_le_of_limit {o : ordinal} (l : o.is_limit) {c} : aleph' o ≤ c ↔ ∀ o' < o, aleph' o' ≤ c := ⟨λ h o' h', (aleph'_le.2 $ h'.le).trans h, λ h, begin rw [←aleph'_aleph_idx c, aleph'_le, limit_le l], intros x h', rw [←aleph'_le, aleph'_aleph_idx], exact h _ h' end⟩ theorem aleph'_limit {o : ordinal} (ho : is_limit o) : aleph' o = ⨆ a : Iio o, aleph' a := begin refine le_antisymm _ (csupr_le' (λ i, aleph'_le.2 (le_of_lt i.2))), rw aleph'_le_of_limit ho, exact λ a ha, le_csupr (bdd_above_of_small _) (⟨a, ha⟩ : Iio o) end @[simp] theorem aleph'_omega : aleph' ω = ℵ₀ := eq_of_forall_ge_iff $ λ c, begin simp only [aleph'_le_of_limit omega_is_limit, lt_omega, exists_imp_distrib, aleph_0_le], exact forall_swap.trans (forall_congr $ λ n, by simp only [forall_eq, aleph'_nat]), end /-- `aleph'` and `aleph_idx` form an equivalence between `ordinal` and `cardinal` -/ @[simp] def aleph'_equiv : ordinal ≃ cardinal := ⟨aleph', aleph_idx, aleph_idx_aleph', aleph'_aleph_idx⟩ /-- The `aleph` function gives the infinite cardinals listed by their ordinal index. `aleph 0 = ℵ₀`, `aleph 1 = succ ℵ₀` is the first uncountable cardinal, and so on. -/ def aleph (o : ordinal) : cardinal := aleph' (ω + o) @[simp] theorem aleph_lt {o₁ o₂ : ordinal} : aleph o₁ < aleph o₂ ↔ o₁ < o₂ := aleph'_lt.trans (add_lt_add_iff_left _) @[simp] theorem aleph_le {o₁ o₂ : ordinal} : aleph o₁ ≤ aleph o₂ ↔ o₁ ≤ o₂ := le_iff_le_iff_lt_iff_lt.2 aleph_lt @[simp] theorem max_aleph_eq (o₁ o₂ : ordinal) : max (aleph o₁) (aleph o₂) = aleph (max o₁ o₂) := begin cases le_total (aleph o₁) (aleph o₂) with h h, { rw [max_eq_right h, max_eq_right (aleph_le.1 h)] }, { rw [max_eq_left h, max_eq_left (aleph_le.1 h)] } end @[simp] theorem aleph_succ {o : ordinal} : aleph (succ o) = succ (aleph o) := by rw [aleph, add_succ, aleph'_succ, aleph] @[simp] theorem aleph_zero : aleph 0 = ℵ₀ := by rw [aleph, add_zero, aleph'_omega] theorem aleph_limit {o : ordinal} (ho : is_limit o) : aleph o = ⨆ a : Iio o, aleph a := begin apply le_antisymm _ (csupr_le' _), { rw [aleph, aleph'_limit (ho.add _)], refine csupr_mono' (bdd_above_of_small _) _, rintro ⟨i, hi⟩, cases lt_or_le i ω, { rcases lt_omega.1 h with ⟨n, rfl⟩, use ⟨0, ho.pos⟩, simpa using (nat_lt_aleph_0 n).le }, { exact ⟨⟨_, (sub_lt_of_le h).2 hi⟩, aleph'_le.2 (le_add_sub _ _)⟩ } }, { exact λ i, aleph_le.2 (le_of_lt i.2) } end theorem aleph_0_le_aleph' {o : ordinal} : ℵ₀ ≤ aleph' o ↔ ω ≤ o := by rw [← aleph'_omega, aleph'_le] theorem aleph_0_le_aleph (o : ordinal) : ℵ₀ ≤ aleph o := by { rw [aleph, aleph_0_le_aleph'], apply ordinal.le_add_right } theorem aleph'_pos {o : ordinal} (ho : 0 < o) : 0 < aleph' o := by rwa [←aleph'_zero, aleph'_lt] theorem aleph_pos (o : ordinal) : 0 < aleph o := aleph_0_pos.trans_le (aleph_0_le_aleph o) @[simp] theorem aleph_to_nat (o : ordinal) : (aleph o).to_nat = 0 := to_nat_apply_of_aleph_0_le $ aleph_0_le_aleph o @[simp] theorem aleph_to_part_enat (o : ordinal) : (aleph o).to_part_enat = ⊤ := to_part_enat_apply_of_aleph_0_le $ aleph_0_le_aleph o instance nonempty_out_aleph (o : ordinal) : nonempty (aleph o).ord.out.α := begin rw [out_nonempty_iff_ne_zero, ←ord_zero], exact λ h, (ord_injective h).not_gt (aleph_pos o) end theorem ord_aleph_is_limit (o : ordinal) : is_limit (aleph o).ord := ord_is_limit $ aleph_0_le_aleph _ instance (o : ordinal) : no_max_order (aleph o).ord.out.α := out_no_max_of_succ_lt (ord_aleph_is_limit o).2 theorem exists_aleph {c : cardinal} : ℵ₀ ≤ c ↔ ∃ o, c = aleph o := ⟨λ h, ⟨aleph_idx c - ω, by { rw [aleph, ordinal.add_sub_cancel_of_le, aleph'_aleph_idx], rwa [← aleph_0_le_aleph', aleph'_aleph_idx] }⟩, λ ⟨o, e⟩, e.symm ▸ aleph_0_le_aleph _⟩ theorem aleph'_is_normal : is_normal (ord ∘ aleph') := ⟨λ o, ord_lt_ord.2 $ aleph'_lt.2 $ lt_succ o, λ o l a, by simp only [ord_le, aleph'_le_of_limit l]⟩ theorem aleph_is_normal : is_normal (ord ∘ aleph) := aleph'_is_normal.trans $ add_is_normal ω theorem succ_aleph_0 : succ ℵ₀ = aleph 1 := by rw [←aleph_zero, ←aleph_succ, ordinal.succ_zero] lemma aleph_0_lt_aleph_one : ℵ₀ < aleph 1 := by { rw ←succ_aleph_0, apply lt_succ } lemma countable_iff_lt_aleph_one {α : Type*} (s : set α) : s.countable ↔ #s < aleph 1 := by rw [←succ_aleph_0, lt_succ_iff, le_aleph_0_iff_set_countable] /-- Ordinals that are cardinals are unbounded. -/ theorem ord_card_unbounded : unbounded (<) {b : ordinal | b.card.ord = b} := unbounded_lt_iff.2 $ λ a, ⟨_, ⟨(by { dsimp, rw card_ord }), (lt_ord_succ_card a).le⟩⟩ theorem eq_aleph'_of_eq_card_ord {o : ordinal} (ho : o.card.ord = o) : ∃ a, (aleph' a).ord = o := ⟨cardinal.aleph_idx.rel_iso o.card, by simpa using ho⟩ /-- `ord ∘ aleph'` enumerates the ordinals that are cardinals. -/ theorem ord_aleph'_eq_enum_card : ord ∘ aleph' = enum_ord {b : ordinal | b.card.ord = b} := begin rw [←eq_enum_ord _ ord_card_unbounded, range_eq_iff], exact ⟨aleph'_is_normal.strict_mono, ⟨(λ a, (by { dsimp, rw card_ord })), λ b hb, eq_aleph'_of_eq_card_ord hb⟩⟩ end /-- Infinite ordinals that are cardinals are unbounded. -/ theorem ord_card_unbounded' : unbounded (<) {b : ordinal | b.card.ord = b ∧ ω ≤ b} := (unbounded_lt_inter_le ω).2 ord_card_unbounded theorem eq_aleph_of_eq_card_ord {o : ordinal} (ho : o.card.ord = o) (ho' : ω ≤ o) : ∃ a, (aleph a).ord = o := begin cases eq_aleph'_of_eq_card_ord ho with a ha, use a - ω, unfold aleph, rwa ordinal.add_sub_cancel_of_le, rwa [←aleph_0_le_aleph', ←ord_le_ord, ha, ord_aleph_0] end /-- `ord ∘ aleph` enumerates the infinite ordinals that are cardinals. -/ theorem ord_aleph_eq_enum_card : ord ∘ aleph = enum_ord {b : ordinal | b.card.ord = b ∧ ω ≤ b} := begin rw ←eq_enum_ord _ ord_card_unbounded', use aleph_is_normal.strict_mono, rw range_eq_iff, refine ⟨(λ a, ⟨_, _⟩), λ b hb, eq_aleph_of_eq_card_ord hb.1 hb.2⟩, { rw card_ord }, { rw [←ord_aleph_0, ord_le_ord], exact aleph_0_le_aleph _ } end /-! ### Beth cardinals -/ /-- Beth numbers are defined so that `beth 0 = ℵ₀`, `beth (succ o) = 2 ^ (beth o)`, and when `o` is a limit ordinal, `beth o` is the supremum of `beth o'` for `o' < o`. Assuming the generalized continuum hypothesis, which is undecidable in ZFC, `beth o = aleph o` for every `o`. -/ def beth (o : ordinal.{u}) : cardinal.{u} := limit_rec_on o aleph_0 (λ _ x, 2 ^ x) (λ a ha IH, ⨆ b : Iio a, IH b.1 b.2) @[simp] theorem beth_zero : beth 0 = aleph_0 := limit_rec_on_zero _ _ _ @[simp] theorem beth_succ (o : ordinal) : beth (succ o) = 2 ^ beth o := limit_rec_on_succ _ _ _ _ theorem beth_limit {o : ordinal} : is_limit o → beth o = ⨆ a : Iio o, beth a := limit_rec_on_limit _ _ _ _ theorem beth_strict_mono : strict_mono beth := begin intros a b, induction b using ordinal.induction with b IH generalizing a, intro h, rcases zero_or_succ_or_limit b with rfl | ⟨c, rfl⟩ | hb, { exact (ordinal.not_lt_zero a h).elim }, { rw lt_succ_iff at h, rw beth_succ, apply lt_of_le_of_lt _ (cantor _), rcases eq_or_lt_of_le h with rfl | h, { refl }, exact (IH c (lt_succ c) h).le }, { apply (cantor _).trans_le, rw [beth_limit hb, ←beth_succ], exact le_csupr (bdd_above_of_small _) (⟨_, hb.succ_lt h⟩ : Iio b) } end @[simp] theorem beth_lt {o₁ o₂ : ordinal} : beth o₁ < beth o₂ ↔ o₁ < o₂ := beth_strict_mono.lt_iff_lt @[simp] theorem beth_le {o₁ o₂ : ordinal} : beth o₁ ≤ beth o₂ ↔ o₁ ≤ o₂ := beth_strict_mono.le_iff_le theorem aleph_le_beth (o : ordinal) : aleph o ≤ beth o := begin apply limit_rec_on o, { simp }, { intros o h, rw [aleph_succ, beth_succ, succ_le_iff], exact (cantor _).trans_le (power_le_power_left two_ne_zero' h) }, { intros o ho IH, rw [aleph_limit ho, beth_limit ho], exact csupr_mono (bdd_above_of_small _) (λ x, IH x.1 x.2) } end theorem aleph_0_le_beth (o : ordinal) : ℵ₀ ≤ beth o := (aleph_0_le_aleph o).trans $ aleph_le_beth o theorem beth_pos (o : ordinal) : 0 < beth o := aleph_0_pos.trans_le $ aleph_0_le_beth o theorem beth_ne_zero (o : ordinal) : beth o ≠ 0 := (beth_pos o).ne' /-! ### Properties of `mul` -/ /-- If `α` is an infinite type, then `α × α` and `α` have the same cardinality. -/ theorem mul_eq_self {c : cardinal} (h : ℵ₀ ≤ c) : c * c = c := begin refine le_antisymm _ (by simpa only [mul_one] using mul_le_mul_left' (one_le_aleph_0.trans h) c), -- the only nontrivial part is `c * c ≤ c`. We prove it inductively. refine acc.rec_on (cardinal.lt_wf.apply c) (λ c _, quotient.induction_on c $ λ α IH ol, _) h, -- consider the minimal well-order `r` on `α` (a type with cardinality `c`). rcases ord_eq α with ⟨r, wo, e⟩, resetI, letI := linear_order_of_STO r, haveI : is_well_order α (<) := wo, -- Define an order `s` on `α × α` by writing `(a, b) < (c, d)` if `max a b < max c d`, or -- the max are equal and `a < c`, or the max are equal and `a = c` and `b < d`. let g : α × α → α := λ p, max p.1 p.2, let f : α × α ↪ ordinal × (α × α) := ⟨λ p:α×α, (typein (<) (g p), p), λ p q, congr_arg prod.snd⟩, let s := f ⁻¹'o (prod.lex (<) (prod.lex (<) (<))), -- this is a well order on `α × α`. haveI : is_well_order _ s := (rel_embedding.preimage _ _).is_well_order, /- it suffices to show that this well order is smaller than `r` if it were larger, then `r` would be a strict prefix of `s`. It would be contained in `β × β` for some `β` of cardinality `< c`. By the inductive assumption, this set has the same cardinality as `β` (or it is finite if `β` is finite), so it is `< c`, which is a contradiction. -/ suffices : type s ≤ type r, {exact card_le_card this}, refine le_of_forall_lt (λ o h, _), rcases typein_surj s h with ⟨p, rfl⟩, rw [← e, lt_ord], refine lt_of_le_of_lt (_ : _ ≤ card (succ (typein (<) (g p))) * card (succ (typein (<) (g p)))) _, { have : {q | s q p} ⊆ insert (g p) {x | x < g p} ×ˢ insert (g p) {x | x < g p}, { intros q h, simp only [s, embedding.coe_fn_mk, order.preimage, typein_lt_typein, prod.lex_def, typein_inj] at h, exact max_le_iff.1 (le_iff_lt_or_eq.2 $ h.imp_right and.left) }, suffices H : (insert (g p) {x | r x (g p)} : set α) ≃ ({x | r x (g p)} ⊕ punit), { exact ⟨(set.embedding_of_subset _ _ this).trans ((equiv.set.prod _ _).trans (H.prod_congr H)).to_embedding⟩ }, refine (equiv.set.insert _).trans ((equiv.refl _).sum_congr punit_equiv_punit), apply @irrefl _ r }, cases lt_or_le (card (succ (typein (<) (g p)))) ℵ₀ with qo qo, { exact (mul_lt_aleph_0 qo qo).trans_le ol }, { suffices, {exact (IH _ this qo).trans_lt this}, rw ← lt_ord, apply (ord_is_limit ol).2, rw [mk_def, e], apply typein_lt_type } end end using_ordinals /-- If `α` and `β` are infinite types, then the cardinality of `α × β` is the maximum of the cardinalities of `α` and `β`. -/ theorem mul_eq_max {a b : cardinal} (ha : ℵ₀ ≤ a) (hb : ℵ₀ ≤ b) : a * b = max a b := le_antisymm (mul_eq_self (ha.trans (le_max_left a b)) ▸ mul_le_mul' (le_max_left _ _) (le_max_right _ _)) $ max_le (by simpa only [mul_one] using mul_le_mul_left' (one_le_aleph_0.trans hb) a) (by simpa only [one_mul] using mul_le_mul_right' (one_le_aleph_0.trans ha) b) @[simp] theorem mul_mk_eq_max {α β : Type*} [infinite α] [infinite β] : #α * #β = max (#α) (#β) := mul_eq_max (aleph_0_le_mk α) (aleph_0_le_mk β) @[simp] theorem aleph_mul_aleph (o₁ o₂ : ordinal) : aleph o₁ * aleph o₂ = aleph (max o₁ o₂) := by rw [cardinal.mul_eq_max (aleph_0_le_aleph o₁) (aleph_0_le_aleph o₂), max_aleph_eq] @[simp] theorem aleph_0_mul_eq {a : cardinal} (ha : ℵ₀ ≤ a) : ℵ₀ * a = a := (mul_eq_max le_rfl ha).trans (max_eq_right ha) @[simp] theorem mul_aleph_0_eq {a : cardinal} (ha : ℵ₀ ≤ a) : a * ℵ₀ = a := (mul_eq_max ha le_rfl).trans (max_eq_left ha) @[simp] theorem aleph_0_mul_mk_eq {α : Type*} [infinite α] : ℵ₀ * #α = #α := aleph_0_mul_eq (aleph_0_le_mk α) @[simp] theorem mk_mul_aleph_0_eq {α : Type*} [infinite α] : #α * ℵ₀ = #α := mul_aleph_0_eq (aleph_0_le_mk α) @[simp] theorem aleph_0_mul_aleph (o : ordinal) : ℵ₀ * aleph o = aleph o := aleph_0_mul_eq (aleph_0_le_aleph o) @[simp] theorem aleph_mul_aleph_0 (o : ordinal) : aleph o * ℵ₀ = aleph o := mul_aleph_0_eq (aleph_0_le_aleph o) theorem mul_lt_of_lt {a b c : cardinal} (hc : ℵ₀ ≤ c) (h1 : a < c) (h2 : b < c) : a * b < c := (mul_le_mul' (le_max_left a b) (le_max_right a b)).trans_lt $ (lt_or_le (max a b) ℵ₀).elim (λ h, (mul_lt_aleph_0 h h).trans_le hc) (λ h, by { rw mul_eq_self h, exact max_lt h1 h2 }) lemma mul_le_max_of_aleph_0_le_left {a b : cardinal} (h : ℵ₀ ≤ a) : a * b ≤ max a b := begin convert mul_le_mul' (le_max_left a b) (le_max_right a b), rw mul_eq_self, refine h.trans (le_max_left a b) end lemma mul_eq_max_of_aleph_0_le_left {a b : cardinal} (h : ℵ₀ ≤ a) (h' : b ≠ 0) : a * b = max a b := begin cases le_or_lt ℵ₀ b with hb hb, { exact mul_eq_max h hb }, refine (mul_le_max_of_aleph_0_le_left h).antisymm _, have : b ≤ a, from hb.le.trans h, rw [max_eq_left this], convert mul_le_mul_left' (one_le_iff_ne_zero.mpr h') _, rw [mul_one], end lemma mul_eq_max_of_aleph_0_le_right {a b : cardinal} (h' : a ≠ 0) (h : ℵ₀ ≤ b) : a * b = max a b := begin rw [mul_comm, max_comm], exact mul_eq_max_of_aleph_0_le_left h h' end lemma mul_eq_max' {a b : cardinal} (h : ℵ₀ ≤ a * b) : a * b = max a b := begin rcases aleph_0_le_mul_iff.mp h with ⟨ha, hb, ha' | hb'⟩, { exact mul_eq_max_of_aleph_0_le_left ha' hb }, { exact mul_eq_max_of_aleph_0_le_right ha hb' } end theorem mul_le_max (a b : cardinal) : a * b ≤ max (max a b) ℵ₀ := begin rcases eq_or_ne a 0 with rfl | ha0, { simp }, rcases eq_or_ne b 0 with rfl | hb0, { simp }, cases le_or_lt ℵ₀ a with ha ha, { rw [mul_eq_max_of_aleph_0_le_left ha hb0], exact le_max_left _ _ }, { cases le_or_lt ℵ₀ b with hb hb, { rw [mul_comm, mul_eq_max_of_aleph_0_le_left hb ha0, max_comm], exact le_max_left _ _ }, { exact le_max_of_le_right (mul_lt_aleph_0 ha hb).le } } end lemma mul_eq_left {a b : cardinal} (ha : ℵ₀ ≤ a) (hb : b ≤ a) (hb' : b ≠ 0) : a * b = a := by { rw [mul_eq_max_of_aleph_0_le_left ha hb', max_eq_left hb] } lemma mul_eq_right {a b : cardinal} (hb : ℵ₀ ≤ b) (ha : a ≤ b) (ha' : a ≠ 0) : a * b = b := by { rw [mul_comm, mul_eq_left hb ha ha'] } lemma le_mul_left {a b : cardinal} (h : b ≠ 0) : a ≤ b * a := by { convert mul_le_mul_right' (one_le_iff_ne_zero.mpr h) _, rw [one_mul] } lemma le_mul_right {a b : cardinal} (h : b ≠ 0) : a ≤ a * b := by { rw [mul_comm], exact le_mul_left h } lemma mul_eq_left_iff {a b : cardinal} : a * b = a ↔ ((max ℵ₀ b ≤ a ∧ b ≠ 0) ∨ b = 1 ∨ a = 0) := begin rw max_le_iff, refine ⟨λ h, _, _⟩, { cases le_or_lt ℵ₀ a with ha ha, { have : a ≠ 0, { rintro rfl, exact ha.not_lt aleph_0_pos }, left, use ha, { rw ←not_lt, exact λ hb, ne_of_gt (hb.trans_le (le_mul_left this)) h }, { rintro rfl, apply this, rw mul_zero at h, exact h.symm }}, right, by_cases h2a : a = 0, { exact or.inr h2a }, have hb : b ≠ 0, { rintro rfl, apply h2a, rw mul_zero at h, exact h.symm }, left, rw [←h, mul_lt_aleph_0_iff, lt_aleph_0, lt_aleph_0] at ha, rcases ha with rfl|rfl|⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩, contradiction, contradiction, rw ←ne at h2a, rw ←one_le_iff_ne_zero at h2a hb, norm_cast at h2a hb h ⊢, apply le_antisymm _ hb, rw ←not_lt, apply λ h2b, ne_of_gt _ h, conv_lhs { rw ←mul_one n }, rwa mul_lt_mul_left, apply nat.lt_of_succ_le h2a }, { rintro (⟨⟨ha, hab⟩, hb⟩|rfl|rfl), { rw [mul_eq_max_of_aleph_0_le_left ha hb, max_eq_left hab] }, all_goals { simp }} end /-! ### Properties of `add` -/ /-- If `α` is an infinite type, then `α ⊕ α` and `α` have the same cardinality. -/ theorem add_eq_self {c : cardinal} (h : ℵ₀ ≤ c) : c + c = c := le_antisymm (by simpa only [nat.cast_bit0, nat.cast_one, mul_eq_self h, two_mul] using mul_le_mul_right' ((nat_lt_aleph_0 2).le.trans h) c) (self_le_add_left c c) /-- If `α` is an infinite type, then the cardinality of `α ⊕ β` is the maximum of the cardinalities of `α` and `β`. -/ theorem add_eq_max {a b : cardinal} (ha : ℵ₀ ≤ a) : a + b = max a b := le_antisymm (add_eq_self (ha.trans (le_max_left a b)) ▸ add_le_add (le_max_left _ _) (le_max_right _ _)) $ max_le (self_le_add_right _ _) (self_le_add_left _ _) theorem add_eq_max' {a b : cardinal} (ha : ℵ₀ ≤ b) : a + b = max a b := by rw [add_comm, max_comm, add_eq_max ha] @[simp] theorem add_mk_eq_max {α β : Type*} [infinite α] : #α + #β = max (#α) (#β) := add_eq_max (aleph_0_le_mk α) @[simp] theorem add_mk_eq_max' {α β : Type*} [infinite β] : #α + #β = max (#α) (#β) := add_eq_max' (aleph_0_le_mk β) theorem add_le_max (a b : cardinal) : a + b ≤ max (max a b) ℵ₀ := begin cases le_or_lt ℵ₀ a with ha ha, { rw [add_eq_max ha], exact le_max_left _ _ }, { cases le_or_lt ℵ₀ b with hb hb, { rw [add_comm, add_eq_max hb, max_comm], exact le_max_left _ _ }, { exact le_max_of_le_right (add_lt_aleph_0 ha hb).le } } end theorem add_le_of_le {a b c : cardinal} (hc : ℵ₀ ≤ c) (h1 : a ≤ c) (h2 : b ≤ c) : a + b ≤ c := (add_le_add h1 h2).trans $ le_of_eq $ add_eq_self hc theorem add_lt_of_lt {a b c : cardinal} (hc : ℵ₀ ≤ c) (h1 : a < c) (h2 : b < c) : a + b < c := (add_le_add (le_max_left a b) (le_max_right a b)).trans_lt $ (lt_or_le (max a b) ℵ₀).elim (λ h, (add_lt_aleph_0 h h).trans_le hc) (λ h, by rw add_eq_self h; exact max_lt h1 h2) lemma eq_of_add_eq_of_aleph_0_le {a b c : cardinal} (h : a + b = c) (ha : a < c) (hc : ℵ₀ ≤ c) : b = c := begin apply le_antisymm, { rw [← h], apply self_le_add_left }, rw[← not_lt], intro hb, have : a + b < c := add_lt_of_lt hc ha hb, simpa [h, lt_irrefl] using this end lemma add_eq_left {a b : cardinal} (ha : ℵ₀ ≤ a) (hb : b ≤ a) : a + b = a := by { rw [add_eq_max ha, max_eq_left hb] } lemma add_eq_right {a b : cardinal} (hb : ℵ₀ ≤ b) (ha : a ≤ b) : a + b = b := by { rw [add_comm, add_eq_left hb ha] } lemma add_eq_left_iff {a b : cardinal} : a + b = a ↔ (max ℵ₀ b ≤ a ∨ b = 0) := begin rw max_le_iff, refine ⟨λ h, _, _⟩, { cases (le_or_lt ℵ₀ a) with ha ha, { left, use ha, rw ←not_lt, apply λ hb, ne_of_gt _ h, exact hb.trans_le (self_le_add_left b a) }, right, rw [←h, add_lt_aleph_0_iff, lt_aleph_0, lt_aleph_0] at ha, rcases ha with ⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩, norm_cast at h ⊢, rw [←add_right_inj, h, add_zero] }, { rintro (⟨h1, h2⟩|h3), { rw [add_eq_max h1, max_eq_left h2] }, { rw [h3, add_zero] } } end lemma add_eq_right_iff {a b : cardinal} : a + b = b ↔ (max ℵ₀ a ≤ b ∨ a = 0) := by { rw [add_comm, add_eq_left_iff] } lemma add_one_eq {a : cardinal} (ha : ℵ₀ ≤ a) : a + 1 = a := add_eq_left ha (one_le_aleph_0.trans ha) @[simp] lemma mk_add_one_eq {α : Type*} [infinite α] : #α + 1 = #α := add_one_eq (aleph_0_le_mk α) protected lemma eq_of_add_eq_add_left {a b c : cardinal} (h : a + b = a + c) (ha : a < ℵ₀) : b = c := begin cases le_or_lt ℵ₀ b with hb hb, { have : a < b := ha.trans_le hb, rw [add_eq_right hb this.le, eq_comm] at h, rw [eq_of_add_eq_of_aleph_0_le h this hb] }, { have hc : c < ℵ₀, { rw ←not_le, intro hc, apply lt_irrefl ℵ₀, apply (hc.trans (self_le_add_left _ a)).trans_lt, rw ←h, apply add_lt_aleph_0 ha hb }, rw lt_aleph_0 at *, rcases ha with ⟨n, rfl⟩, rcases hb with ⟨m, rfl⟩, rcases hc with ⟨k, rfl⟩, norm_cast at h ⊢, apply add_left_cancel h } end protected lemma eq_of_add_eq_add_right {a b c : cardinal} (h : a + b = c + b) (hb : b < ℵ₀) : a = c := by { rw [add_comm a b, add_comm c b] at h, exact cardinal.eq_of_add_eq_add_left h hb } @[simp] theorem aleph_add_aleph (o₁ o₂ : ordinal) : aleph o₁ + aleph o₂ = aleph (max o₁ o₂) := by rw [cardinal.add_eq_max (aleph_0_le_aleph o₁), max_aleph_eq] theorem principal_add_ord {c : cardinal} (hc : ℵ₀ ≤ c) : ordinal.principal (+) c.ord := λ a b ha hb, by { rw [lt_ord, ordinal.card_add] at *, exact add_lt_of_lt hc ha hb } theorem principal_add_aleph (o : ordinal) : ordinal.principal (+) (aleph o).ord := principal_add_ord $ aleph_0_le_aleph o /-! ### Properties about power -/ theorem pow_le {κ μ : cardinal.{u}} (H1 : ℵ₀ ≤ κ) (H2 : μ < ℵ₀) : κ ^ μ ≤ κ := let ⟨n, H3⟩ := lt_aleph_0.1 H2 in H3.symm ▸ (quotient.induction_on κ (λ α H1, nat.rec_on n (lt_of_lt_of_le (by { rw [nat.cast_zero, power_zero], exact one_lt_aleph_0 }) H1).le (λ n ih, trans_rel_left _ (by { rw [nat.cast_succ, power_add, power_one], exact mul_le_mul_right' ih _ }) (mul_eq_self H1))) H1) theorem pow_eq {κ μ : cardinal.{u}} (H1 : ℵ₀ ≤ κ) (H2 : 1 ≤ μ) (H3 : μ < ℵ₀) : κ ^ μ = κ := (pow_le H1 H3).antisymm $ self_le_power κ H2 lemma power_self_eq {c : cardinal} (h : ℵ₀ ≤ c) : c ^ c = 2 ^ c := begin apply ((power_le_power_right $ (cantor c).le).trans _).antisymm, { convert power_le_power_right ((nat_lt_aleph_0 2).le.trans h), apply nat.cast_two.symm }, { rw [←power_mul, mul_eq_self h] } end lemma prod_eq_two_power {ι : Type u} [infinite ι] {c : ι → cardinal.{v}} (h₁ : ∀ i, 2 ≤ c i) (h₂ : ∀ i, lift.{u} (c i) ≤ lift.{v} (#ι)) : prod c = 2 ^ lift.{v} (#ι) := begin rw [← lift_id' (prod c), lift_prod, ← lift_two_power], apply le_antisymm, { refine (prod_le_prod _ _ h₂).trans_eq _, rw [prod_const, lift_lift, ← lift_power, power_self_eq (aleph_0_le_mk ι), lift_umax.{u v}] }, { rw [← prod_const', lift_prod], refine prod_le_prod _ _ (λ i, _), rw [lift_two, ← lift_two.{u v}, lift_le], exact h₁ i } end lemma power_eq_two_power {c₁ c₂ : cardinal} (h₁ : ℵ₀ ≤ c₁) (h₂ : 2 ≤ c₂) (h₂' : c₂ ≤ c₁) : c₂ ^ c₁ = 2 ^ c₁ := le_antisymm (power_self_eq h₁ ▸ power_le_power_right h₂') (power_le_power_right h₂) lemma nat_power_eq {c : cardinal.{u}} (h : ℵ₀ ≤ c) {n : ℕ} (hn : 2 ≤ n) : (n : cardinal.{u}) ^ c = 2 ^ c := power_eq_two_power h (by assumption_mod_cast) ((nat_lt_aleph_0 n).le.trans h) lemma power_nat_le {c : cardinal.{u}} {n : ℕ} (h : ℵ₀ ≤ c) : c ^ n ≤ c := pow_le h (nat_lt_aleph_0 n) lemma power_nat_eq {c : cardinal.{u}} {n : ℕ} (h1 : ℵ₀ ≤ c) (h2 : 1 ≤ n) : c ^ n = c := pow_eq h1 (by exact_mod_cast h2) (nat_lt_aleph_0 n) lemma power_nat_le_max {c : cardinal.{u}} {n : ℕ} : c ^ (n : cardinal.{u}) ≤ max c ℵ₀ := begin cases le_or_lt ℵ₀ c with hc hc, { exact le_max_of_le_left (power_nat_le hc) }, { exact le_max_of_le_right ((power_lt_aleph_0 hc (nat_lt_aleph_0 _)).le) } end lemma powerlt_aleph_0 {c : cardinal} (h : ℵ₀ ≤ c) : c ^< ℵ₀ = c := begin apply le_antisymm, { rw powerlt_le, intro c', rw lt_aleph_0, rintro ⟨n, rfl⟩, apply power_nat_le h }, convert le_powerlt c one_lt_aleph_0, rw power_one end lemma powerlt_aleph_0_le (c : cardinal) : c ^< ℵ₀ ≤ max c ℵ₀ := begin cases le_or_lt ℵ₀ c, { rw powerlt_aleph_0 h, apply le_max_left }, rw powerlt_le, exact λ c' hc', (power_lt_aleph_0 h hc').le.trans (le_max_right _ _) end /-! ### Computing cardinality of various types -/ @[simp] theorem mk_list_eq_mk (α : Type u) [infinite α] : #(list α) = #α := have H1 : ℵ₀ ≤ #α := aleph_0_le_mk α, eq.symm $ le_antisymm ⟨⟨λ x, [x], λ x y H, (list.cons.inj H).1⟩⟩ $ calc #(list α) = sum (λ n : ℕ, #α ^ (n : cardinal.{u})) : mk_list_eq_sum_pow α ... ≤ sum (λ n : ℕ, #α) : sum_le_sum _ _ $ λ n, pow_le H1 $ nat_lt_aleph_0 n ... = #α : by simp [H1] theorem mk_list_eq_aleph_0 (α : Type u) [countable α] [nonempty α] : #(list α) = ℵ₀ := mk_le_aleph_0.antisymm (aleph_0_le_mk _) theorem mk_list_eq_max_mk_aleph_0 (α : Type u) [nonempty α] : #(list α) = max (#α) ℵ₀ := begin casesI finite_or_infinite α, { rw [mk_list_eq_aleph_0, eq_comm, max_eq_right], exact mk_le_aleph_0 }, { rw [mk_list_eq_mk, eq_comm, max_eq_left], exact aleph_0_le_mk α } end theorem mk_list_le_max (α : Type u) : #(list α) ≤ max ℵ₀ (#α) := begin casesI finite_or_infinite α, { exact mk_le_aleph_0.trans (le_max_left _ _) }, { rw mk_list_eq_mk, apply le_max_right } end @[simp] theorem mk_finset_of_infinite (α : Type u) [infinite α] : #(finset α) = #α := eq.symm $ le_antisymm (mk_le_of_injective (λ x y, finset.singleton_inj.1)) $ calc #(finset α) ≤ #(list α) : mk_le_of_surjective list.to_finset_surjective ... = #α : mk_list_eq_mk α @[simp] lemma mk_finsupp_lift_of_infinite (α : Type u) (β : Type v) [infinite α] [has_zero β] [nontrivial β] : #(α →₀ β) = max (lift.{v} (#α)) (lift.{u} (#β)) := begin apply le_antisymm, { calc #(α →₀ β) ≤ # (finset (α × β)) : mk_le_of_injective (finsupp.graph_injective α β) ... = #(α × β) : mk_finset_of_infinite _ ... = max (lift.{v} (#α)) (lift.{u} (#β)) : by rw [mk_prod, mul_eq_max_of_aleph_0_le_left]; simp }, { apply max_le; rw [←lift_id (# (α →₀ β)), ←lift_umax], { cases exists_ne (0 : β) with b hb, exact lift_mk_le.{u (max u v) v}.2 ⟨⟨_, finsupp.single_left_injective hb⟩⟩ }, { inhabit α, exact lift_mk_le.{v (max u v) u}.2 ⟨⟨_, finsupp.single_injective default⟩⟩ } } end lemma mk_finsupp_of_infinite (α β : Type u) [infinite α] [has_zero β] [nontrivial β] : #(α →₀ β) = max (#α) (#β) := by simp @[simp] lemma mk_finsupp_lift_of_infinite' (α : Type u) (β : Type v) [nonempty α] [has_zero β] [infinite β] : #(α →₀ β) = max (lift.{v} (#α)) (lift.{u} (#β)) := begin casesI fintype_or_infinite α, { rw mk_finsupp_lift_of_fintype, have : ℵ₀ ≤ (#β).lift := aleph_0_le_lift.2 (aleph_0_le_mk β), rw [max_eq_right (le_trans _ this), power_nat_eq this], exacts [fintype.card_pos, lift_le_aleph_0.2 (lt_aleph_0_of_finite _).le] }, { apply mk_finsupp_lift_of_infinite }, end lemma mk_finsupp_of_infinite' (α β : Type u) [nonempty α] [has_zero β] [infinite β] : #(α →₀ β) = max (#α) (#β) := by simp lemma mk_finsupp_nat (α : Type u) [nonempty α] : #(α →₀ ℕ) = max (#α) ℵ₀ := by simp @[simp] lemma mk_multiset_of_nonempty (α : Type u) [nonempty α] : #(multiset α) = max (#α) ℵ₀ := multiset.to_finsupp.to_equiv.cardinal_eq.trans (mk_finsupp_nat α) lemma mk_multiset_of_infinite (α : Type u) [infinite α] : #(multiset α) = #α := by simp @[simp] lemma mk_multiset_of_is_empty (α : Type u) [is_empty α] : #(multiset α) = 1 := multiset.to_finsupp.to_equiv.cardinal_eq.trans (by simp) lemma mk_multiset_of_countable (α : Type u) [countable α] [nonempty α] : #(multiset α) = ℵ₀ := multiset.to_finsupp.to_equiv.cardinal_eq.trans (by simp) lemma mk_bounded_set_le_of_infinite (α : Type u) [infinite α] (c : cardinal) : #{t : set α // #t ≤ c} ≤ #α ^ c := begin refine le_trans _ (by rw [←add_one_eq (aleph_0_le_mk α)]), induction c using cardinal.induction_on with β, fapply mk_le_of_surjective, { intro f, use sum.inl ⁻¹' range f, refine le_trans (mk_preimage_of_injective _ _ (λ x y, sum.inl.inj)) _, apply mk_range_le }, rintro ⟨s, ⟨g⟩⟩, use λ y, if h : ∃(x : s), g x = y then sum.inl (classical.some h).val else sum.inr ⟨⟩, apply subtype.eq, ext, split, { rintro ⟨y, h⟩, dsimp only at h, by_cases h' : ∃ (z : s), g z = y, { rw [dif_pos h'] at h, cases sum.inl.inj h, exact (classical.some h').2 }, { rw [dif_neg h'] at h, cases h }}, { intro h, have : ∃(z : s), g z = g ⟨x, h⟩, exact ⟨⟨x, h⟩, rfl⟩, use g ⟨x, h⟩, dsimp only, rw [dif_pos this], congr', suffices : classical.some this = ⟨x, h⟩, exact congr_arg subtype.val this, apply g.2, exact classical.some_spec this } end lemma mk_bounded_set_le (α : Type u) (c : cardinal) : #{t : set α // #t ≤ c} ≤ max (#α) ℵ₀ ^ c := begin transitivity #{t : set (ulift.{u} ℕ ⊕ α) // #t ≤ c}, { refine ⟨embedding.subtype_map _ _⟩, apply embedding.image, use sum.inr, apply sum.inr.inj, intros s hs, exact mk_image_le.trans hs }, apply (mk_bounded_set_le_of_infinite (ulift.{u} ℕ ⊕ α) c).trans, rw [max_comm, ←add_eq_max]; refl end lemma mk_bounded_subset_le {α : Type u} (s : set α) (c : cardinal.{u}) : #{t : set α // t ⊆ s ∧ #t ≤ c} ≤ max (#s) ℵ₀ ^ c := begin refine le_trans _ (mk_bounded_set_le s c), refine ⟨embedding.cod_restrict _ _ _⟩, use λ t, coe ⁻¹' t.1, { rintros ⟨t, ht1, ht2⟩ ⟨t', h1t', h2t'⟩ h, apply subtype.eq, dsimp only at h ⊢, refine (preimage_eq_preimage' _ _).1 h; rw [subtype.range_coe]; assumption }, rintro ⟨t, h1t, h2t⟩, exact (mk_preimage_of_injective _ _ subtype.val_injective).trans h2t end /-! ### Properties of `compl` -/ lemma mk_compl_of_infinite {α : Type*} [infinite α] (s : set α) (h2 : #s < #α) : #(sᶜ : set α) = #α := by { refine eq_of_add_eq_of_aleph_0_le _ h2 (aleph_0_le_mk α), exact mk_sum_compl s } lemma mk_compl_finset_of_infinite {α : Type*} [infinite α] (s : finset α) : #((↑s)ᶜ : set α) = #α := by { apply mk_compl_of_infinite, exact (finset_card_lt_aleph_0 s).trans_le (aleph_0_le_mk α) } lemma mk_compl_eq_mk_compl_infinite {α : Type*} [infinite α] {s t : set α} (hs : #s < #α) (ht : #t < #α) : #(sᶜ : set α) = #(tᶜ : set α) := by { rw [mk_compl_of_infinite s hs, mk_compl_of_infinite t ht] } lemma mk_compl_eq_mk_compl_finite_lift {α : Type u} {β : Type v} [finite α] {s : set α} {t : set β} (h1 : lift.{max v w} (#α) = lift.{max u w} (#β)) (h2 : lift.{max v w} (#s) = lift.{max u w} (#t)) : lift.{max v w} (#(sᶜ : set α)) = lift.{max u w} (#(tᶜ : set β)) := begin casesI nonempty_fintype α, rcases lift_mk_eq.1 h1 with ⟨e⟩, letI : fintype β := fintype.of_equiv α e, replace h1 : fintype.card α = fintype.card β := (fintype.of_equiv_card _).symm, classical, lift s to finset α using s.to_finite, lift t to finset β using t.to_finite, simp only [finset.coe_sort_coe, mk_coe_finset, lift_nat_cast, nat.cast_inj] at h2, simp only [← finset.coe_compl, finset.coe_sort_coe, mk_coe_finset, finset.card_compl, lift_nat_cast, nat.cast_inj, h1, h2] end lemma mk_compl_eq_mk_compl_finite {α β : Type u} [finite α] {s : set α} {t : set β} (h1 : #α = #β) (h : #s = #t) : #(sᶜ : set α) = #(tᶜ : set β) := by { rw ← lift_inj, apply mk_compl_eq_mk_compl_finite_lift; rwa [lift_inj] } lemma mk_compl_eq_mk_compl_finite_same {α : Type*} [finite α] {s t : set α} (h : #s = #t) : #(sᶜ : set α) = #(tᶜ : set α) := mk_compl_eq_mk_compl_finite rfl h /-! ### Extending an injection to an equiv -/ theorem extend_function {α β : Type*} {s : set α} (f : s ↪ β) (h : nonempty ((sᶜ : set α) ≃ ((range f)ᶜ : set β))) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin intros, have := h, cases this with g, let h : α ≃ β := (set.sum_compl (s : set α)).symm.trans ((sum_congr (equiv.of_injective f f.2) g).trans (set.sum_compl (range f))), refine ⟨h, _⟩, rintro ⟨x, hx⟩, simp [set.sum_compl_symm_apply_of_mem, hx] end theorem extend_function_finite {α β : Type*} [finite α] {s : set α} (f : s ↪ β) (h : nonempty (α ≃ β)) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin apply extend_function f, cases id h with g, rw [← lift_mk_eq] at h, rw [←lift_mk_eq, mk_compl_eq_mk_compl_finite_lift h], rw [mk_range_eq_lift], exact f.2 end theorem extend_function_of_lt {α β : Type*} {s : set α} (f : s ↪ β) (hs : #s < #α) (h : nonempty (α ≃ β)) : ∃ (g : α ≃ β), ∀ x : s, g x = f x := begin casesI fintype_or_infinite α, { exact extend_function_finite f h }, { apply extend_function f, cases id h with g, haveI := infinite.of_injective _ g.injective, rw [← lift_mk_eq'] at h ⊢, rwa [mk_compl_of_infinite s hs, mk_compl_of_infinite], rwa [← lift_lt, mk_range_eq_of_injective f.injective, ← h, lift_lt] }, end section bit /-! This section proves inequalities for `bit0` and `bit1`, enabling `simp` to solve inequalities for numeral cardinals. The complexity of the resulting algorithm is not good, as in some cases `simp` reduces an inequality to a disjunction of two situations, depending on whether a cardinal is finite or infinite. Since the evaluation of the branches is not lazy, this is bad. It is good enough for practical situations, though. For specific numbers, these inequalities could also be deduced from the corresponding inequalities of natural numbers using `norm_cast`: ``` example : (37 : cardinal) < 42 := by { norm_cast, norm_num } ``` -/ lemma bit0_ne_zero (a : cardinal) : ¬bit0 a = 0 ↔ ¬a = 0 := by simp [bit0] @[simp] lemma bit1_ne_zero (a : cardinal) : ¬bit1 a = 0 := by simp [bit1] @[simp] lemma zero_lt_bit0 (a : cardinal) : 0 < bit0 a ↔ 0 < a := by { rw ←not_iff_not, simp [bit0], } @[simp] lemma zero_lt_bit1 (a : cardinal) : 0 < bit1 a := zero_lt_one.trans_le (self_le_add_left _ _) @[simp] lemma one_le_bit0 (a : cardinal) : 1 ≤ bit0 a ↔ 0 < a := ⟨λ h, (zero_lt_bit0 a).mp (zero_lt_one.trans_le h), λ h, (one_le_iff_pos.mpr h).trans (self_le_add_left a a)⟩ @[simp] lemma one_le_bit1 (a : cardinal) : 1 ≤ bit1 a := self_le_add_left _ _ theorem bit0_eq_self {c : cardinal} (h : ℵ₀ ≤ c) : bit0 c = c := add_eq_self h @[simp] theorem bit0_lt_aleph_0 {c : cardinal} : bit0 c < ℵ₀ ↔ c < ℵ₀ := by simp [bit0, add_lt_aleph_0_iff] @[simp] theorem aleph_0_le_bit0 {c : cardinal} : ℵ₀ ≤ bit0 c ↔ ℵ₀ ≤ c := by { rw ←not_iff_not, simp } @[simp] theorem bit1_eq_self_iff {c : cardinal} : bit1 c = c ↔ ℵ₀ ≤ c := begin by_cases h : ℵ₀ ≤ c, { simp only [bit1, bit0_eq_self h, h, eq_self_iff_true, add_one_of_aleph_0_le] }, { refine iff_of_false (ne_of_gt _) h, rcases lt_aleph_0.1 (not_le.1 h) with ⟨n, rfl⟩, norm_cast, dsimp [bit1, bit0], linarith } end @[simp] theorem bit1_lt_aleph_0 {c : cardinal} : bit1 c < ℵ₀ ↔ c < ℵ₀ := by simp [bit1, bit0, add_lt_aleph_0_iff, one_lt_aleph_0] @[simp] theorem aleph_0_le_bit1 {c : cardinal} : ℵ₀ ≤ bit1 c ↔ ℵ₀ ≤ c := by { rw ←not_iff_not, simp } @[simp] lemma bit0_le_bit0 {a b : cardinal} : bit0 a ≤ bit0 b ↔ a ≤ b := begin cases le_or_lt ℵ₀ a with ha ha; cases le_or_lt ℵ₀ b with hb hb, { rw [bit0_eq_self ha, bit0_eq_self hb] }, { rw bit0_eq_self ha, refine iff_of_false (λ h, _) (hb.trans_le ha).not_le, have A : bit0 b < ℵ₀, by simpa using hb, exact lt_irrefl _ ((A.trans_le ha).trans_le h) }, { rw bit0_eq_self hb, exact iff_of_true ((bit0_lt_aleph_0.2 ha).le.trans hb) (ha.le.trans hb) }, { rcases lt_aleph_0.1 ha with ⟨m, rfl⟩, rcases lt_aleph_0.1 hb with ⟨n, rfl⟩, norm_cast, exact bit0_le_bit0 } end @[simp] lemma bit0_le_bit1 {a b : cardinal} : bit0 a ≤ bit1 b ↔ a ≤ b := begin cases le_or_lt ℵ₀ a with ha ha; cases le_or_lt ℵ₀ b with hb hb, { rw [bit0_eq_self ha, bit1_eq_self_iff.2 hb] }, { rw bit0_eq_self ha, refine iff_of_false (λ h, _) (hb.trans_le ha).not_le, have A : bit1 b < ℵ₀, by simpa using hb, exact lt_irrefl _ ((A.trans_le ha).trans_le h) }, { rw bit1_eq_self_iff.2 hb, exact iff_of_true ((bit0_lt_aleph_0.2 ha).le.trans hb) (ha.le.trans hb) }, { rcases lt_aleph_0.1 ha with ⟨m, rfl⟩, rcases lt_aleph_0.1 hb with ⟨n, rfl⟩, norm_cast, exact nat.bit0_le_bit1_iff } end @[simp] lemma bit1_le_bit1 {a b : cardinal} : bit1 a ≤ bit1 b ↔ a ≤ b := ⟨λ h, bit0_le_bit1.1 ((self_le_add_right (bit0 a) 1).trans h), λ h, (add_le_add_right (add_le_add_left h a) 1).trans (add_le_add_right (add_le_add_right h b) 1)⟩ @[simp] lemma bit1_le_bit0 {a b : cardinal} : bit1 a ≤ bit0 b ↔ (a < b ∨ (a ≤ b ∧ ℵ₀ ≤ a)) := begin cases le_or_lt ℵ₀ a with ha ha; cases le_or_lt ℵ₀ b with hb hb, { simp only [bit1_eq_self_iff.mpr ha, bit0_eq_self hb, ha, and_true], refine ⟨λ h, or.inr h, λ h, _⟩, cases h, { exact le_of_lt h }, { exact h } }, { rw bit1_eq_self_iff.2 ha, refine iff_of_false (λ h, _) (λ h, _), { have A : bit0 b < ℵ₀, by simpa using hb, exact lt_irrefl _ ((A.trans_le ha).trans_le h) }, { exact not_le_of_lt (hb.trans_le ha) (h.elim le_of_lt and.left) } }, { rw bit0_eq_self hb, exact iff_of_true ((bit1_lt_aleph_0.2 ha).le.trans hb) (or.inl $ ha.trans_le hb) }, { rcases lt_aleph_0.1 ha with ⟨m, rfl⟩, rcases lt_aleph_0.1 hb with ⟨n, rfl⟩, norm_cast, simp [not_le.mpr ha] } end @[simp] lemma bit0_lt_bit0 {a b : cardinal} : bit0 a < bit0 b ↔ a < b := begin cases le_or_lt ℵ₀ a with ha ha; cases le_or_lt ℵ₀ b with hb hb, { rw [bit0_eq_self ha, bit0_eq_self hb] }, { rw bit0_eq_self ha, refine iff_of_false (λ h, _) (hb.le.trans ha).not_lt, have A : bit0 b < ℵ₀, by simpa using hb, exact lt_irrefl _ ((A.trans_le ha).trans h) }, { rw bit0_eq_self hb, exact iff_of_true ((bit0_lt_aleph_0.2 ha).trans_le hb) (ha.trans_le hb) }, { rcases lt_aleph_0.1 ha with ⟨m, rfl⟩, rcases lt_aleph_0.1 hb with ⟨n, rfl⟩, norm_cast, exact bit0_lt_bit0 } end @[simp] lemma bit1_lt_bit0 {a b : cardinal} : bit1 a < bit0 b ↔ a < b := begin cases le_or_lt ℵ₀ a with ha ha; cases le_or_lt ℵ₀ b with hb hb, { rw [bit1_eq_self_iff.2 ha, bit0_eq_self hb] }, { rw bit1_eq_self_iff.2 ha, refine iff_of_false (λ h, _) (hb.le.trans ha).not_lt, have A : bit0 b < ℵ₀, by simpa using hb, exact lt_irrefl _ ((A.trans_le ha).trans h) }, { rw bit0_eq_self hb, exact iff_of_true ((bit1_lt_aleph_0.2 ha).trans_le hb) (ha.trans_le hb) }, { rcases lt_aleph_0.1 ha with ⟨m, rfl⟩, rcases lt_aleph_0.1 hb with ⟨n, rfl⟩, norm_cast, exact nat.bit1_lt_bit0_iff } end @[simp] lemma bit1_lt_bit1 {a b : cardinal} : bit1 a < bit1 b ↔ a < b := begin cases le_or_lt ℵ₀ a with ha ha; cases le_or_lt ℵ₀ b with hb hb, { rw [bit1_eq_self_iff.2 ha, bit1_eq_self_iff.2 hb] }, { rw bit1_eq_self_iff.2 ha, refine iff_of_false (λ h, _) (hb.le.trans ha).not_lt, have A : bit1 b < ℵ₀, by simpa using hb, exact lt_irrefl _ ((A.trans_le ha).trans h) }, { rw bit1_eq_self_iff.2 hb, exact iff_of_true ((bit1_lt_aleph_0.2 ha).trans_le hb) (ha.trans_le hb) }, { rcases lt_aleph_0.1 ha with ⟨m, rfl⟩, rcases lt_aleph_0.1 hb with ⟨n, rfl⟩, norm_cast, exact bit1_lt_bit1 } end @[simp] lemma bit0_lt_bit1 {a b : cardinal} : bit0 a < bit1 b ↔ (a < b ∨ (a ≤ b ∧ a < ℵ₀)) := begin cases le_or_lt ℵ₀ a with ha ha; cases le_or_lt ℵ₀ b with hb hb, { simp [bit0_eq_self ha, bit1_eq_self_iff.2 hb, not_lt.mpr ha] }, { rw bit0_eq_self ha, refine iff_of_false (λ h, _) (λ h, _), { have A : bit1 b < ℵ₀, by simpa using hb, exact lt_irrefl _ ((A.trans_le ha).trans h) }, { exact (hb.trans_le ha).not_le (h.elim le_of_lt and.left) } }, { rw [bit1_eq_self_iff.2 hb], exact iff_of_true ((bit0_lt_aleph_0.2 ha).trans_le hb) (or.inl $ ha.trans_le hb) }, { rcases lt_aleph_0.1 ha with ⟨m, rfl⟩, rcases lt_aleph_0.1 hb with ⟨n, rfl⟩, norm_cast, simp only [ha, and_true, nat.bit0_lt_bit1_iff, or_iff_right_of_imp le_of_lt] } end lemma one_lt_two : (1 : cardinal) < 2 := -- This strategy works generally to prove inequalities between numerals in `cardinality`. by { norm_cast, norm_num } @[simp] lemma one_lt_bit0 {a : cardinal} : 1 < bit0 a ↔ 0 < a := by simp [←bit1_zero] @[simp] lemma one_lt_bit1 (a : cardinal) : 1 < bit1 a ↔ 0 < a := by simp [←bit1_zero] end bit end cardinal
82b1d703f3ee984e90f31c505397375bb6540708
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/lie/submodule.lean
c4794edc3829dbc62b9db48b74443d500375e2cd
[ "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
36,744
lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import algebra.lie.subalgebra import ring_theory.noetherian /-! # Lie submodules of a Lie algebra In this file we define Lie submodules and Lie ideals, we construct the lattice structure on Lie submodules and we use it to define various important operations, notably the Lie span of a subset of a Lie module. ## Main definitions * `lie_submodule` * `lie_submodule.well_founded_of_noetherian` * `lie_submodule.lie_span` * `lie_submodule.map` * `lie_submodule.comap` * `lie_ideal` * `lie_ideal.map` * `lie_ideal.comap` ## Tags lie algebra, lie submodule, lie ideal, lattice structure -/ universes u v w w₁ w₂ section lie_submodule variables (R : Type u) (L : Type v) (M : Type w) variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] variables [lie_ring_module L M] [lie_module R L M] set_option old_structure_cmd true /-- A Lie submodule of a Lie module is a submodule that is closed under the Lie bracket. This is a sufficient condition for the subset itself to form a Lie module. -/ structure lie_submodule extends submodule R M := (lie_mem : ∀ {x : L} {m : M}, m ∈ carrier → ⁅x, m⁆ ∈ carrier) attribute [nolint doc_blame] lie_submodule.to_submodule namespace lie_submodule variables {R L M} (N N' : lie_submodule R L M) instance : set_like (lie_submodule R L M) M := { coe := carrier, coe_injective' := λ N O h, by cases N; cases O; congr' } instance : add_subgroup_class (lie_submodule R L M) M := { add_mem := λ N, N.add_mem', zero_mem := λ N, N.zero_mem', neg_mem := λ N x hx, show -x ∈ N.to_submodule, from neg_mem hx } /-- The zero module is a Lie submodule of any Lie module. -/ instance : has_zero (lie_submodule R L M) := ⟨{ lie_mem := λ x m h, by { rw ((submodule.mem_bot R).1 h), apply lie_zero, }, ..(0 : submodule R M)}⟩ instance : inhabited (lie_submodule R L M) := ⟨0⟩ instance coe_submodule : has_coe (lie_submodule R L M) (submodule R M) := ⟨to_submodule⟩ @[simp] lemma to_submodule_eq_coe : N.to_submodule = N := rfl @[norm_cast] lemma coe_to_submodule : ((N : submodule R M) : set M) = N := rfl @[simp] lemma mem_carrier {x : M} : x ∈ N.carrier ↔ x ∈ (N : set M) := iff.rfl @[simp] lemma mem_mk_iff (S : set M) (h₁ h₂ h₃ h₄) {x : M} : x ∈ (⟨S, h₁, h₂, h₃, h₄⟩ : lie_submodule R L M) ↔ x ∈ S := iff.rfl @[simp] lemma mem_coe_submodule {x : M} : x ∈ (N : submodule R M) ↔ x ∈ N := iff.rfl lemma mem_coe {x : M} : x ∈ (N : set M) ↔ x ∈ N := iff.rfl @[simp] protected lemma zero_mem : (0 : M) ∈ N := zero_mem N @[simp] lemma mk_eq_zero {x} (h : x ∈ N) : (⟨x, h⟩ : N) = 0 ↔ x = 0 := subtype.ext_iff_val @[simp] lemma coe_to_set_mk (S : set M) (h₁ h₂ h₃ h₄) : ((⟨S, h₁, h₂, h₃, h₄⟩ : lie_submodule R L M) : set M) = S := rfl @[simp] lemma coe_to_submodule_mk (p : submodule R M) (h) : (({lie_mem := h, ..p} : lie_submodule R L M) : submodule R M) = p := by { cases p, refl, } lemma coe_submodule_injective : function.injective (to_submodule : lie_submodule R L M → submodule R M) := λ x y h, by { cases x, cases y, congr, injection h } @[ext] lemma ext (h : ∀ m, m ∈ N ↔ m ∈ N') : N = N' := set_like.ext h @[simp] lemma coe_to_submodule_eq_iff : (N : submodule R M) = (N' : submodule R M) ↔ N = N' := coe_submodule_injective.eq_iff /-- Copy of a lie_submodule with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (s : set M) (hs : s = ↑N) : lie_submodule R L M := { carrier := s, zero_mem' := hs.symm ▸ N.zero_mem', add_mem' := hs.symm ▸ N.add_mem', smul_mem' := hs.symm ▸ N.smul_mem', lie_mem := hs.symm ▸ N.lie_mem, } @[simp] lemma coe_copy (S : lie_submodule R L M) (s : set M) (hs : s = ↑S) : (S.copy s hs : set M) = s := rfl lemma copy_eq (S : lie_submodule R L M) (s : set M) (hs : s = ↑S) : S.copy s hs = S := coe_submodule_injective (set_like.coe_injective hs) instance : lie_ring_module L N := { bracket := λ (x : L) (m : N), ⟨⁅x, m.val⁆, N.lie_mem m.property⟩, add_lie := by { intros x y m, apply set_coe.ext, apply add_lie, }, lie_add := by { intros x m n, apply set_coe.ext, apply lie_add, }, leibniz_lie := by { intros x y m, apply set_coe.ext, apply leibniz_lie, }, } instance module' {S : Type*} [semiring S] [has_scalar S R] [module S M] [is_scalar_tower S R M] : module S N := N.to_submodule.module' instance : module R N := N.to_submodule.module instance {S : Type*} [semiring S] [has_scalar S R] [has_scalar Sᵐᵒᵖ R] [module S M] [module Sᵐᵒᵖ M] [is_scalar_tower S R M] [is_scalar_tower Sᵐᵒᵖ R M] [is_central_scalar S M] : is_central_scalar S N := N.to_submodule.is_central_scalar instance : lie_module R L N := { lie_smul := by { intros t x y, apply set_coe.ext, apply lie_smul, }, smul_lie := by { intros t x y, apply set_coe.ext, apply smul_lie, }, } @[simp, norm_cast] lemma coe_zero : ((0 : N) : M) = (0 : M) := rfl @[simp, norm_cast] lemma coe_add (m m' : N) : (↑(m + m') : M) = (m : M) + (m' : M) := rfl @[simp, norm_cast] lemma coe_neg (m : N) : (↑(-m) : M) = -(m : M) := rfl @[simp, norm_cast] lemma coe_sub (m m' : N) : (↑(m - m') : M) = (m : M) - (m' : M) := rfl @[simp, norm_cast] lemma coe_smul (t : R) (m : N) : (↑(t • m) : M) = t • (m : M) := rfl @[simp, norm_cast] lemma coe_bracket (x : L) (m : N) : (↑⁅x, m⁆ : M) = ⁅x, ↑m⁆ := rfl end lie_submodule section lie_ideal variables (L) /-- An ideal of a Lie algebra is a Lie submodule of the Lie algebra as a Lie module over itself. -/ abbreviation lie_ideal := lie_submodule R L L lemma lie_mem_right (I : lie_ideal R L) (x y : L) (h : y ∈ I) : ⁅x, y⁆ ∈ I := I.lie_mem h lemma lie_mem_left (I : lie_ideal R L) (x y : L) (h : x ∈ I) : ⁅x, y⁆ ∈ I := by { rw [←lie_skew, ←neg_lie], apply lie_mem_right, assumption, } /-- An ideal of a Lie algebra is a Lie subalgebra. -/ def lie_ideal_subalgebra (I : lie_ideal R L) : lie_subalgebra R L := { lie_mem' := by { intros x y hx hy, apply lie_mem_right, exact hy, }, ..I.to_submodule, } instance : has_coe (lie_ideal R L) (lie_subalgebra R L) := ⟨λ I, lie_ideal_subalgebra R L I⟩ @[norm_cast] lemma lie_ideal.coe_to_subalgebra (I : lie_ideal R L) : ((I : lie_subalgebra R L) : set L) = I := rfl @[norm_cast] lemma lie_ideal.coe_to_lie_subalgebra_to_submodule (I : lie_ideal R L) : ((I : lie_subalgebra R L) : submodule R L) = I := rfl end lie_ideal variables {R M} lemma submodule.exists_lie_submodule_coe_eq_iff (p : submodule R M) : (∃ (N : lie_submodule R L M), ↑N = p) ↔ ∀ (x : L) (m : M), m ∈ p → ⁅x, m⁆ ∈ p := begin split, { rintros ⟨N, rfl⟩, exact N.lie_mem, }, { intros h, use { lie_mem := h, ..p }, exact lie_submodule.coe_to_submodule_mk p _, }, end namespace lie_subalgebra variables {L} (K : lie_subalgebra R L) /-- Given a Lie subalgebra `K ⊆ L`, if we view `L` as a `K`-module by restriction, it contains a distinguished Lie submodule for the action of `K`, namely `K` itself. -/ def to_lie_submodule : lie_submodule R K L := { lie_mem := λ x y hy, K.lie_mem x.property hy, .. (K : submodule R L) } @[simp] lemma coe_to_lie_submodule : (K.to_lie_submodule : submodule R L) = K := by { rcases K with ⟨⟨⟩⟩, refl, } variables {K} @[simp] lemma mem_to_lie_submodule (x : L) : x ∈ K.to_lie_submodule ↔ x ∈ K := iff.rfl lemma exists_lie_ideal_coe_eq_iff : (∃ (I : lie_ideal R L), ↑I = K) ↔ ∀ (x y : L), y ∈ K → ⁅x, y⁆ ∈ K := begin simp only [← coe_to_submodule_eq_iff, lie_ideal.coe_to_lie_subalgebra_to_submodule, submodule.exists_lie_submodule_coe_eq_iff L], exact iff.rfl, end lemma exists_nested_lie_ideal_coe_eq_iff {K' : lie_subalgebra R L} (h : K ≤ K') : (∃ (I : lie_ideal R K'), ↑I = of_le h) ↔ ∀ (x y : L), x ∈ K' → y ∈ K → ⁅x, y⁆ ∈ K := begin simp only [exists_lie_ideal_coe_eq_iff, coe_bracket, mem_of_le], split, { intros h' x y hx hy, exact h' ⟨x, hx⟩ ⟨y, h hy⟩ hy, }, { rintros h' ⟨x, hx⟩ ⟨y, hy⟩ hy', exact h' x y hx hy', }, end end lie_subalgebra end lie_submodule namespace lie_submodule variables {R : Type u} {L : Type v} {M : Type w} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M] variables [lie_ring_module L M] [lie_module R L M] variables (N N' : lie_submodule R L M) (I J : lie_ideal R L) section lattice_structure open set lemma coe_injective : function.injective (coe : lie_submodule R L M → set M) := set_like.coe_injective @[simp, norm_cast] lemma coe_submodule_le_coe_submodule : (N : submodule R M) ≤ N' ↔ N ≤ N' := iff.rfl instance : has_bot (lie_submodule R L M) := ⟨0⟩ @[simp] lemma bot_coe : ((⊥ : lie_submodule R L M) : set M) = {0} := rfl @[simp] lemma bot_coe_submodule : ((⊥ : lie_submodule R L M) : submodule R M) = ⊥ := rfl @[simp] lemma mem_bot (x : M) : x ∈ (⊥ : lie_submodule R L M) ↔ x = 0 := mem_singleton_iff instance : has_top (lie_submodule R L M) := ⟨{ lie_mem := λ x m h, mem_univ ⁅x, m⁆, ..(⊤ : submodule R M) }⟩ @[simp] lemma top_coe : ((⊤ : lie_submodule R L M) : set M) = univ := rfl @[simp] lemma top_coe_submodule : ((⊤ : lie_submodule R L M) : submodule R M) = ⊤ := rfl @[simp] lemma mem_top (x : M) : x ∈ (⊤ : lie_submodule R L M) := mem_univ x instance : has_inf (lie_submodule R L M) := ⟨λ N N', { lie_mem := λ x m h, mem_inter (N.lie_mem h.1) (N'.lie_mem h.2), ..(N ⊓ N' : submodule R M) }⟩ instance : has_Inf (lie_submodule R L M) := ⟨λ S, { lie_mem := λ x m h, by { simp only [submodule.mem_carrier, mem_Inter, submodule.Inf_coe, mem_set_of_eq, forall_apply_eq_imp_iff₂, exists_imp_distrib] at *, intros N hN, apply N.lie_mem (h N hN), }, ..Inf {(s : submodule R M) | s ∈ S} }⟩ @[simp] theorem inf_coe : (↑(N ⊓ N') : set M) = N ∩ N' := rfl @[simp] lemma Inf_coe_to_submodule (S : set (lie_submodule R L M)) : (↑(Inf S) : submodule R M) = Inf {(s : submodule R M) | s ∈ S} := rfl @[simp] lemma Inf_coe (S : set (lie_submodule R L M)) : (↑(Inf S) : set M) = ⋂ s ∈ S, (s : set M) := begin rw [← lie_submodule.coe_to_submodule, Inf_coe_to_submodule, submodule.Inf_coe], ext m, simpa only [mem_Inter, mem_set_of_eq, forall_apply_eq_imp_iff₂, exists_imp_distrib], end lemma Inf_glb (S : set (lie_submodule R L M)) : is_glb S (Inf S) := begin have h : ∀ (N N' : lie_submodule R L M), (N : set M) ≤ N' ↔ N ≤ N', { intros, refl }, apply is_glb.of_image h, simp only [Inf_coe], exact is_glb_binfi end /-- The set of Lie submodules of a Lie module form a complete lattice. We provide explicit values for the fields `bot`, `top`, `inf` to get more convenient definitions than we would otherwise obtain from `complete_lattice_of_Inf`. -/ instance : complete_lattice (lie_submodule R L M) := { le := (≤), lt := (<), bot := ⊥, bot_le := λ N _ h, by { rw mem_bot at h, rw h, exact N.zero_mem', }, top := ⊤, le_top := λ _ _ _, trivial, inf := (⊓), le_inf := λ N₁ N₂ N₃ h₁₂ h₁₃ m hm, ⟨h₁₂ hm, h₁₃ hm⟩, inf_le_left := λ _ _ _, and.left, inf_le_right := λ _ _ _, and.right, ..set_like.partial_order, ..complete_lattice_of_Inf _ Inf_glb } instance : add_comm_monoid (lie_submodule R L M) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm, } @[simp] lemma add_eq_sup : N + N' = N ⊔ N' := rfl @[norm_cast, simp] lemma sup_coe_to_submodule : (↑(N ⊔ N') : submodule R M) = (N : submodule R M) ⊔ (N' : submodule R M) := begin have aux : ∀ (x : L) m, m ∈ (N ⊔ N' : submodule R M) → ⁅x,m⁆ ∈ (N ⊔ N' : submodule R M), { simp only [submodule.mem_sup], rintro x m ⟨y, hy, z, hz, rfl⟩, refine ⟨⁅x, y⁆, N.lie_mem hy, ⁅x, z⁆, N'.lie_mem hz, (lie_add _ _ _).symm⟩ }, refine le_antisymm (Inf_le ⟨{ lie_mem := aux, ..(N ⊔ N' : submodule R M) }, _⟩) _, { simp only [exists_prop, and_true, mem_set_of_eq, eq_self_iff_true, coe_to_submodule_mk, ← coe_submodule_le_coe_submodule, and_self, le_sup_left, le_sup_right] }, { simp, }, end @[norm_cast, simp] lemma inf_coe_to_submodule : (↑(N ⊓ N') : submodule R M) = (N : submodule R M) ⊓ (N' : submodule R M) := rfl @[simp] lemma mem_inf (x : M) : x ∈ N ⊓ N' ↔ x ∈ N ∧ x ∈ N' := by rw [← mem_coe_submodule, ← mem_coe_submodule, ← mem_coe_submodule, inf_coe_to_submodule, submodule.mem_inf] lemma mem_sup (x : M) : x ∈ N ⊔ N' ↔ ∃ (y ∈ N) (z ∈ N'), y + z = x := by { rw [← mem_coe_submodule, sup_coe_to_submodule, submodule.mem_sup], exact iff.rfl, } lemma eq_bot_iff : N = ⊥ ↔ ∀ (m : M), m ∈ N → m = 0 := by { rw eq_bot_iff, exact iff.rfl, } -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_bot : subsingleton (lie_submodule R L ↥(⊥ : lie_submodule R L M)) := begin apply subsingleton_of_bot_eq_top, ext ⟨x, hx⟩, change x ∈ ⊥ at hx, rw submodule.mem_bot at hx, subst hx, simp only [true_iff, eq_self_iff_true, submodule.mk_eq_zero, lie_submodule.mem_bot], end instance : is_modular_lattice (lie_submodule R L M) := { sup_inf_le_assoc_of_le := λ N₁ N₂ N₃, by { simp only [← coe_submodule_le_coe_submodule, sup_coe_to_submodule, inf_coe_to_submodule], exact is_modular_lattice.sup_inf_le_assoc_of_le ↑N₂, }, } variables (R L M) lemma well_founded_of_noetherian [is_noetherian R M] : well_founded ((>) : lie_submodule R L M → lie_submodule R L M → Prop) := let f : ((>) : lie_submodule R L M → lie_submodule R L M → Prop) →r ((>) : submodule R M → submodule R M → Prop) := { to_fun := coe, map_rel' := λ N N' h, h, } in rel_hom_class.well_founded f (is_noetherian_iff_well_founded.mp infer_instance) @[simp] lemma subsingleton_iff : subsingleton (lie_submodule R L M) ↔ subsingleton M := have h : subsingleton (lie_submodule R L M) ↔ subsingleton (submodule R M), { rw [← subsingleton_iff_bot_eq_top, ← subsingleton_iff_bot_eq_top, ← coe_to_submodule_eq_iff, top_coe_submodule, bot_coe_submodule], }, h.trans $ submodule.subsingleton_iff R @[simp] lemma nontrivial_iff : nontrivial (lie_submodule R L M) ↔ nontrivial M := not_iff_not.mp ( (not_nontrivial_iff_subsingleton.trans $ subsingleton_iff R L M).trans not_nontrivial_iff_subsingleton.symm) instance [nontrivial M] : nontrivial (lie_submodule R L M) := (nontrivial_iff R L M).mpr ‹_› lemma nontrivial_iff_ne_bot {N : lie_submodule R L M} : nontrivial N ↔ N ≠ ⊥ := begin split; contrapose!, { rintros rfl ⟨⟨m₁, h₁ : m₁ ∈ (⊥ : lie_submodule R L M)⟩, ⟨m₂, h₂ : m₂ ∈ (⊥ : lie_submodule R L M)⟩, h₁₂⟩, simpa [(lie_submodule.mem_bot _).mp h₁, (lie_submodule.mem_bot _).mp h₂] using h₁₂, }, { rw [not_nontrivial_iff_subsingleton, lie_submodule.eq_bot_iff], rintros ⟨h⟩ m hm, simpa using h ⟨m, hm⟩ ⟨_, N.zero_mem⟩, }, end variables {R L M} section inclusion_maps /-- The inclusion of a Lie submodule into its ambient space is a morphism of Lie modules. -/ def incl : N →ₗ⁅R,L⁆ M := { map_lie' := λ x m, rfl, ..submodule.subtype (N : submodule R M) } @[simp] lemma incl_coe : (N.incl : N →ₗ[R] M) = (N : submodule R M).subtype := rfl @[simp] lemma incl_apply (m : N) : N.incl m = m := rfl lemma incl_eq_val : (N.incl : N → M) = subtype.val := rfl variables {N N'} (h : N ≤ N') /-- Given two nested Lie submodules `N ⊆ N'`, the inclusion `N ↪ N'` is a morphism of Lie modules.-/ def hom_of_le : N →ₗ⁅R,L⁆ N' := { map_lie' := λ x m, rfl, ..submodule.of_le (show N.to_submodule ≤ N'.to_submodule, from h) } @[simp] lemma coe_hom_of_le (m : N) : (hom_of_le h m : M) = m := rfl lemma hom_of_le_apply (m : N) : hom_of_le h m = ⟨m.1, h m.2⟩ := rfl lemma hom_of_le_injective : function.injective (hom_of_le h) := λ x y, by simp only [hom_of_le_apply, imp_self, subtype.mk_eq_mk, set_like.coe_eq_coe, subtype.val_eq_coe] end inclusion_maps section lie_span variables (R L) (s : set M) /-- The `lie_span` of a set `s ⊆ M` is the smallest Lie submodule of `M` that contains `s`. -/ def lie_span : lie_submodule R L M := Inf {N | s ⊆ N} variables {R L s} lemma mem_lie_span {x : M} : x ∈ lie_span R L s ↔ ∀ N : lie_submodule R L M, s ⊆ N → x ∈ N := by { change x ∈ (lie_span R L s : set M) ↔ _, erw Inf_coe, exact mem_Inter₂, } lemma subset_lie_span : s ⊆ lie_span R L s := by { intros m hm, erw mem_lie_span, intros N hN, exact hN hm, } lemma submodule_span_le_lie_span : submodule.span R s ≤ lie_span R L s := by { rw submodule.span_le, apply subset_lie_span, } lemma lie_span_le {N} : lie_span R L s ≤ N ↔ s ⊆ N := begin split, { exact subset.trans subset_lie_span, }, { intros hs m hm, rw mem_lie_span at hm, exact hm _ hs, }, end lemma lie_span_mono {t : set M} (h : s ⊆ t) : lie_span R L s ≤ lie_span R L t := by { rw lie_span_le, exact subset.trans h subset_lie_span, } lemma lie_span_eq : lie_span R L (N : set M) = N := le_antisymm (lie_span_le.mpr rfl.subset) subset_lie_span lemma coe_lie_span_submodule_eq_iff {p : submodule R M} : (lie_span R L (p : set M) : submodule R M) = p ↔ ∃ (N : lie_submodule R L M), ↑N = p := begin rw p.exists_lie_submodule_coe_eq_iff L, split; intros h, { intros x m hm, rw [← h, mem_coe_submodule], exact lie_mem _ (subset_lie_span hm), }, { rw [← coe_to_submodule_mk p h, coe_to_submodule, coe_to_submodule_eq_iff, lie_span_eq], }, end variables (R L M) /-- `lie_span` forms a Galois insertion with the coercion from `lie_submodule` to `set`. -/ protected def gi : galois_insertion (lie_span R L : set M → lie_submodule R L M) coe := { choice := λ s _, lie_span R L s, gc := λ s t, lie_span_le, le_l_u := λ s, subset_lie_span, choice_eq := λ s h, rfl } @[simp] lemma span_empty : lie_span R L (∅ : set M) = ⊥ := (lie_submodule.gi R L M).gc.l_bot @[simp] lemma span_univ : lie_span R L (set.univ : set M) = ⊤ := eq_top_iff.2 $ set_like.le_def.2 $ subset_lie_span lemma lie_span_eq_bot_iff : lie_span R L s = ⊥ ↔ ∀ (m ∈ s), m = (0 : M) := by rw [_root_.eq_bot_iff, lie_span_le, bot_coe, subset_singleton_iff] variables {M} lemma span_union (s t : set M) : lie_span R L (s ∪ t) = lie_span R L s ⊔ lie_span R L t := (lie_submodule.gi R L M).gc.l_sup lemma span_Union {ι} (s : ι → set M) : lie_span R L (⋃ i, s i) = ⨆ i, lie_span R L (s i) := (lie_submodule.gi R L M).gc.l_supr end lie_span end lattice_structure end lie_submodule section lie_submodule_map_and_comap variables {R : Type u} {L : Type v} {L' : Type w₂} {M : Type w} {M' : Type w₁} variables [comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L'] variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] variables [add_comm_group M'] [module R M'] [lie_ring_module L M'] [lie_module R L M'] namespace lie_submodule variables (f : M →ₗ⁅R,L⁆ M') (N N₂ : lie_submodule R L M) (N' : lie_submodule R L M') /-- A morphism of Lie modules `f : M → M'` pushes forward Lie submodules of `M` to Lie submodules of `M'`. -/ def map : lie_submodule R L M' := { lie_mem := λ x m' h, by { rcases h with ⟨m, hm, hfm⟩, use ⁅x, m⁆, split, { apply N.lie_mem hm, }, { norm_cast at hfm, simp [hfm], }, }, ..(N : submodule R M).map (f : M →ₗ[R] M') } @[simp] lemma coe_submodule_map : (N.map f : submodule R M') = (N : submodule R M).map (f : M →ₗ[R] M') := rfl /-- A morphism of Lie modules `f : M → M'` pulls back Lie submodules of `M'` to Lie submodules of `M`. -/ def comap : lie_submodule R L M := { lie_mem := λ x m h, by { suffices : ⁅x, f m⁆ ∈ N', { simp [this], }, apply N'.lie_mem h, }, ..(N' : submodule R M').comap (f : M →ₗ[R] M') } @[simp] lemma coe_submodule_comap : (N'.comap f : submodule R M) = (N' : submodule R M').comap (f : M →ₗ[R] M') := rfl variables {f N N₂ N'} lemma map_le_iff_le_comap : map f N ≤ N' ↔ N ≤ comap f N' := set.image_subset_iff variables (f) lemma gc_map_comap : galois_connection (map f) (comap f) := λ N N', map_le_iff_le_comap variables {f} @[simp] lemma map_sup : (N ⊔ N₂).map f = N.map f ⊔ N₂.map f := (gc_map_comap f).l_sup lemma mem_map (m' : M') : m' ∈ N.map f ↔ ∃ m, m ∈ N ∧ f m = m' := submodule.mem_map @[simp] lemma mem_comap {m : M} : m ∈ comap f N' ↔ f m ∈ N' := iff.rfl end lie_submodule namespace lie_ideal variables (f : L →ₗ⁅R⁆ L') (I I₂ : lie_ideal R L) (J : lie_ideal R L') @[simp] lemma top_coe_lie_subalgebra : ((⊤ : lie_ideal R L) : lie_subalgebra R L) = ⊤ := rfl /-- A morphism of Lie algebras `f : L → L'` pushes forward Lie ideals of `L` to Lie ideals of `L'`. Note that unlike `lie_submodule.map`, we must take the `lie_span` of the image. Mathematically this is because although `f` makes `L'` into a Lie module over `L`, in general the `L` submodules of `L'` are not the same as the ideals of `L'`. -/ def map : lie_ideal R L' := lie_submodule.lie_span R L' $ (I : submodule R L).map (f : L →ₗ[R] L') /-- A morphism of Lie algebras `f : L → L'` pulls back Lie ideals of `L'` to Lie ideals of `L`. Note that `f` makes `L'` into a Lie module over `L` (turning `f` into a morphism of Lie modules) and so this is a special case of `lie_submodule.comap` but we do not exploit this fact. -/ def comap : lie_ideal R L := { lie_mem := λ x y h, by { suffices : ⁅f x, f y⁆ ∈ J, { simp [this], }, apply J.lie_mem h, }, ..(J : submodule R L').comap (f : L →ₗ[R] L') } @[simp] lemma map_coe_submodule (h : ↑(map f I) = f '' I) : (map f I : submodule R L') = (I : submodule R L).map (f : L →ₗ[R] L') := by { rw [set_like.ext'_iff, lie_submodule.coe_to_submodule, h, submodule.map_coe], refl, } @[simp] lemma comap_coe_submodule : (comap f J : submodule R L) = (J : submodule R L').comap (f : L →ₗ[R] L') := rfl lemma map_le : map f I ≤ J ↔ f '' I ⊆ J := lie_submodule.lie_span_le variables {f I I₂ J} lemma mem_map {x : L} (hx : x ∈ I) : f x ∈ map f I := by { apply lie_submodule.subset_lie_span, use x, exact ⟨hx, rfl⟩, } @[simp] lemma mem_comap {x : L} : x ∈ comap f J ↔ f x ∈ J := iff.rfl lemma map_le_iff_le_comap : map f I ≤ J ↔ I ≤ comap f J := by { rw map_le, exact set.image_subset_iff, } variables (f) lemma gc_map_comap : galois_connection (map f) (comap f) := λ I I', map_le_iff_le_comap variables {f} @[simp] lemma map_sup : (I ⊔ I₂).map f = I.map f ⊔ I₂.map f := (gc_map_comap f).l_sup lemma map_comap_le : map f (comap f J) ≤ J := by { rw map_le_iff_le_comap, exact le_rfl, } /-- See also `lie_ideal.map_comap_eq`. -/ lemma comap_map_le : I ≤ comap f (map f I) := by { rw ← map_le_iff_le_comap, exact le_rfl, } @[mono] lemma map_mono : monotone (map f) := λ I₁ I₂ h, by { rw set_like.le_def at h, apply lie_submodule.lie_span_mono (set.image_subset ⇑f h), } @[mono] lemma comap_mono : monotone (comap f) := λ J₁ J₂ h, by { rw ← set_like.coe_subset_coe at h ⊢, exact set.preimage_mono h, } lemma map_of_image (h : f '' I = J) : I.map f = J := begin apply le_antisymm, { erw [lie_submodule.lie_span_le, submodule.map_coe, h], }, { rw [← set_like.coe_subset_coe, ← h], exact lie_submodule.subset_lie_span, }, end /-- Note that this is not a special case of `lie_submodule.subsingleton_of_bot`. Indeed, given `I : lie_ideal R L`, in general the two lattices `lie_ideal R I` and `lie_submodule R L I` are different (though the latter does naturally inject into the former). In other words, in general, ideals of `I`, regarded as a Lie algebra in its own right, are not the same as ideals of `L` contained in `I`. -/ -- TODO[gh-6025]: make this an instance once safe to do so lemma subsingleton_of_bot : subsingleton (lie_ideal R ↥(⊥ : lie_ideal R L)) := begin apply subsingleton_of_bot_eq_top, ext ⟨x, hx⟩, change x ∈ ⊥ at hx, rw submodule.mem_bot at hx, subst hx, simp only [true_iff, eq_self_iff_true, submodule.mk_eq_zero, lie_submodule.mem_bot], end end lie_ideal namespace lie_hom variables (f : L →ₗ⁅R⁆ L') (I : lie_ideal R L) (J : lie_ideal R L') /-- The kernel of a morphism of Lie algebras, as an ideal in the domain. -/ def ker : lie_ideal R L := lie_ideal.comap f ⊥ /-- The range of a morphism of Lie algebras as an ideal in the codomain. -/ def ideal_range : lie_ideal R L' := lie_submodule.lie_span R L' f.range lemma ideal_range_eq_lie_span_range : f.ideal_range = lie_submodule.lie_span R L' f.range := rfl lemma ideal_range_eq_map : f.ideal_range = lie_ideal.map f ⊤ := by { ext, simp only [ideal_range, range_eq_map], refl } /-- The condition that the image of a morphism of Lie algebras is an ideal. -/ def is_ideal_morphism : Prop := (f.ideal_range : lie_subalgebra R L') = f.range @[simp] lemma is_ideal_morphism_def : f.is_ideal_morphism ↔ (f.ideal_range : lie_subalgebra R L') = f.range := iff.rfl lemma is_ideal_morphism_iff : f.is_ideal_morphism ↔ ∀ (x : L') (y : L), ∃ (z : L), ⁅x, f y⁆ = f z := begin simp only [is_ideal_morphism_def, ideal_range_eq_lie_span_range, ← lie_subalgebra.coe_to_submodule_eq_iff, ← f.range.coe_to_submodule, lie_ideal.coe_to_lie_subalgebra_to_submodule, lie_submodule.coe_lie_span_submodule_eq_iff, lie_subalgebra.mem_coe_submodule, mem_range, exists_imp_distrib, submodule.exists_lie_submodule_coe_eq_iff], split, { intros h x y, obtain ⟨z, hz⟩ := h x (f y) y rfl, use z, exact hz.symm, }, { intros h x y z hz, obtain ⟨w, hw⟩ := h x z, use w, rw [← hw, hz], }, end lemma range_subset_ideal_range : (f.range : set L') ⊆ f.ideal_range := lie_submodule.subset_lie_span lemma map_le_ideal_range : I.map f ≤ f.ideal_range := begin rw f.ideal_range_eq_map, exact lie_ideal.map_mono le_top, end lemma ker_le_comap : f.ker ≤ J.comap f := lie_ideal.comap_mono bot_le @[simp] lemma ker_coe_submodule : (ker f : submodule R L) = (f : L →ₗ[R] L').ker := rfl @[simp] lemma mem_ker {x : L} : x ∈ ker f ↔ f x = 0 := show x ∈ (f.ker : submodule R L) ↔ _, by simp only [ker_coe_submodule, linear_map.mem_ker, coe_to_linear_map] lemma mem_ideal_range {x : L} : f x ∈ ideal_range f := begin rw ideal_range_eq_map, exact lie_ideal.mem_map (lie_submodule.mem_top x) end @[simp] lemma mem_ideal_range_iff (h : is_ideal_morphism f) {y : L'} : y ∈ ideal_range f ↔ ∃ (x : L), f x = y := begin rw f.is_ideal_morphism_def at h, rw [← lie_submodule.mem_coe, ← lie_ideal.coe_to_subalgebra, h, f.range_coe, set.mem_range], end lemma le_ker_iff : I ≤ f.ker ↔ ∀ x, x ∈ I → f x = 0 := begin split; intros h x hx, { specialize h hx, rw mem_ker at h, exact h, }, { rw mem_ker, apply h x hx, }, end lemma ker_eq_bot : f.ker = ⊥ ↔ function.injective f := by rw [← lie_submodule.coe_to_submodule_eq_iff, ker_coe_submodule, lie_submodule.bot_coe_submodule, linear_map.ker_eq_bot, coe_to_linear_map] @[simp] lemma range_coe_submodule : (f.range : submodule R L') = (f : L →ₗ[R] L').range := rfl lemma range_eq_top : f.range = ⊤ ↔ function.surjective f := begin rw [← lie_subalgebra.coe_to_submodule_eq_iff, range_coe_submodule, lie_subalgebra.top_coe_submodule], exact linear_map.range_eq_top, end @[simp] lemma ideal_range_eq_top_of_surjective (h : function.surjective f) : f.ideal_range = ⊤ := begin rw ← f.range_eq_top at h, rw [ideal_range_eq_lie_span_range, h, ← lie_subalgebra.coe_to_submodule, ← lie_submodule.coe_to_submodule_eq_iff, lie_submodule.top_coe_submodule, lie_subalgebra.top_coe_submodule, lie_submodule.coe_lie_span_submodule_eq_iff], use ⊤, exact lie_submodule.top_coe_submodule, end lemma is_ideal_morphism_of_surjective (h : function.surjective f) : f.is_ideal_morphism := by rw [is_ideal_morphism_def, f.ideal_range_eq_top_of_surjective h, f.range_eq_top.mpr h, lie_ideal.top_coe_lie_subalgebra] end lie_hom namespace lie_ideal variables {f : L →ₗ⁅R⁆ L'} {I : lie_ideal R L} {J : lie_ideal R L'} @[simp] lemma map_eq_bot_iff : I.map f = ⊥ ↔ I ≤ f.ker := by { rw ← le_bot_iff, exact lie_ideal.map_le_iff_le_comap } lemma coe_map_of_surjective (h : function.surjective f) : (I.map f : submodule R L') = (I : submodule R L).map (f : L →ₗ[R] L') := begin let J : lie_ideal R L' := { lie_mem := λ x y hy, begin have hy' : ∃ (x : L), x ∈ I ∧ f x = y, { simpa [hy], }, obtain ⟨z₂, hz₂, rfl⟩ := hy', obtain ⟨z₁, rfl⟩ := h x, simp only [lie_hom.coe_to_linear_map, set_like.mem_coe, set.mem_image, lie_submodule.mem_coe_submodule, submodule.mem_carrier, submodule.map_coe], use ⁅z₁, z₂⁆, exact ⟨I.lie_mem hz₂, f.map_lie z₁ z₂⟩, end, ..(I : submodule R L).map (f : L →ₗ[R] L'), }, erw lie_submodule.coe_lie_span_submodule_eq_iff, use J, apply lie_submodule.coe_to_submodule_mk, end lemma mem_map_of_surjective {y : L'} (h₁ : function.surjective f) (h₂ : y ∈ I.map f) : ∃ (x : I), f x = y := begin rw [← lie_submodule.mem_coe_submodule, coe_map_of_surjective h₁, submodule.mem_map] at h₂, obtain ⟨x, hx, rfl⟩ := h₂, use ⟨x, hx⟩, refl, end lemma bot_of_map_eq_bot {I : lie_ideal R L} (h₁ : function.injective f) (h₂ : I.map f = ⊥) : I = ⊥ := begin rw ← f.ker_eq_bot at h₁, change comap f ⊥ = ⊥ at h₁, rw [eq_bot_iff, map_le_iff_le_comap, h₁] at h₂, rw eq_bot_iff, exact h₂, end /-- Given two nested Lie ideals `I₁ ⊆ I₂`, the inclusion `I₁ ↪ I₂` is a morphism of Lie algebras. -/ def hom_of_le {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) : I₁ →ₗ⁅R⁆ I₂ := { map_lie' := λ x y, rfl, ..submodule.of_le (show I₁.to_submodule ≤ I₂.to_submodule, from h), } @[simp] lemma coe_hom_of_le {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) (x : I₁) : (hom_of_le h x : L) = x := rfl lemma hom_of_le_apply {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) (x : I₁) : hom_of_le h x = ⟨x.1, h x.2⟩ := rfl lemma hom_of_le_injective {I₁ I₂ : lie_ideal R L} (h : I₁ ≤ I₂) : function.injective (hom_of_le h) := λ x y, by simp only [hom_of_le_apply, imp_self, subtype.mk_eq_mk, set_like.coe_eq_coe, subtype.val_eq_coe] @[simp] lemma map_sup_ker_eq_map : lie_ideal.map f (I ⊔ f.ker) = lie_ideal.map f I := begin suffices : lie_ideal.map f (I ⊔ f.ker) ≤ lie_ideal.map f I, { exact le_antisymm this (lie_ideal.map_mono le_sup_left), }, apply lie_submodule.lie_span_mono, rintros x ⟨y, hy₁, hy₂⟩, rw ← hy₂, erw lie_submodule.mem_sup at hy₁, obtain ⟨z₁, hz₁, z₂, hz₂, hy⟩ := hy₁, rw ← hy, rw [f.coe_to_linear_map, f.map_add, f.mem_ker.mp hz₂, add_zero], exact ⟨z₁, hz₁, rfl⟩, end @[simp] lemma map_comap_eq (h : f.is_ideal_morphism) : map f (comap f J) = f.ideal_range ⊓ J := begin apply le_antisymm, { rw le_inf_iff, exact ⟨f.map_le_ideal_range _, map_comap_le⟩, }, { rw f.is_ideal_morphism_def at h, rw [← set_like.coe_subset_coe, lie_submodule.inf_coe, ← coe_to_subalgebra, h], rintros y ⟨⟨x, h₁⟩, h₂⟩, rw ← h₁ at h₂ ⊢, exact mem_map h₂, }, end @[simp] lemma comap_map_eq (h : ↑(map f I) = f '' I) : comap f (map f I) = I ⊔ f.ker := by rw [← lie_submodule.coe_to_submodule_eq_iff, comap_coe_submodule, I.map_coe_submodule f h, lie_submodule.sup_coe_to_submodule, f.ker_coe_submodule, submodule.comap_map_eq] variables (f I J) /-- Regarding an ideal `I` as a subalgebra, the inclusion map into its ambient space is a morphism of Lie algebras. -/ def incl : I →ₗ⁅R⁆ L := (I : lie_subalgebra R L).incl @[simp] lemma incl_range : I.incl.range = I := (I : lie_subalgebra R L).incl_range @[simp] lemma incl_apply (x : I) : I.incl x = x := rfl @[simp] lemma incl_coe : (I.incl : I →ₗ[R] L) = (I : submodule R L).subtype := rfl @[simp] lemma comap_incl_self : comap I.incl I = ⊤ := by { rw ← lie_submodule.coe_to_submodule_eq_iff, exact submodule.comap_subtype_self _, } @[simp] lemma ker_incl : I.incl.ker = ⊥ := by rw [← lie_submodule.coe_to_submodule_eq_iff, I.incl.ker_coe_submodule, lie_submodule.bot_coe_submodule, incl_coe, submodule.ker_subtype] @[simp] lemma incl_ideal_range : I.incl.ideal_range = I := begin rw [lie_hom.ideal_range_eq_lie_span_range, ← lie_subalgebra.coe_to_submodule, ← lie_submodule.coe_to_submodule_eq_iff, incl_range, coe_to_lie_subalgebra_to_submodule, lie_submodule.coe_lie_span_submodule_eq_iff], use I, end lemma incl_is_ideal_morphism : I.incl.is_ideal_morphism := begin rw [I.incl.is_ideal_morphism_def, incl_ideal_range], exact (I : lie_subalgebra R L).incl_range.symm, end end lie_ideal end lie_submodule_map_and_comap namespace lie_module_hom variables {R : Type u} {L : Type v} {M : Type w} {N : Type w₁} variables [comm_ring R] [lie_ring L] [lie_algebra R L] variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] variables [add_comm_group N] [module R N] [lie_ring_module L N] [lie_module R L N] variables (f : M →ₗ⁅R,L⁆ N) /-- The kernel of a morphism of Lie algebras, as an ideal in the domain. -/ def ker : lie_submodule R L M := lie_submodule.comap f ⊥ @[simp] lemma ker_coe_submodule : (f.ker : submodule R M) = (f : M →ₗ[R] N).ker := rfl lemma ker_eq_bot : f.ker = ⊥ ↔ function.injective f := by rw [← lie_submodule.coe_to_submodule_eq_iff, ker_coe_submodule, lie_submodule.bot_coe_submodule, linear_map.ker_eq_bot, coe_to_linear_map] variables {f} @[simp] lemma mem_ker (m : M) : m ∈ f.ker ↔ f m = 0 := iff.rfl @[simp] lemma ker_id : (lie_module_hom.id : M →ₗ⁅R,L⁆ M).ker = ⊥ := rfl @[simp] lemma comp_ker_incl : f.comp f.ker.incl = 0 := by { ext ⟨m, hm⟩, exact (mem_ker m).mp hm, } lemma le_ker_iff_map (M' : lie_submodule R L M) : M' ≤ f.ker ↔ lie_submodule.map f M' = ⊥ := by rw [ker, eq_bot_iff, lie_submodule.map_le_iff_le_comap] variables (f) /-- The range of a morphism of Lie modules `f : M → N` is a Lie submodule of `N`. See Note [range copy pattern]. -/ def range : lie_submodule R L N := (lie_submodule.map f ⊤).copy (set.range f) set.image_univ.symm @[simp] lemma coe_range : (f.range : set N) = set.range f := rfl @[simp] lemma coe_submodule_range : (f.range : submodule R N) = (f : M →ₗ[R] N).range := rfl @[simp] lemma mem_range (n : N) : n ∈ f.range ↔ ∃ m, f m = n := iff.rfl lemma map_top : lie_submodule.map f ⊤ = f.range := by { ext, simp [lie_submodule.mem_map], } end lie_module_hom namespace lie_submodule variables {R : Type u} {L : Type v} {M : Type w} variables [comm_ring R] [lie_ring L] [lie_algebra R L] variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M] variables (N : lie_submodule R L M) @[simp] lemma ker_incl : N.incl.ker = ⊥ := by simp [← lie_submodule.coe_to_submodule_eq_iff] @[simp] lemma range_incl : N.incl.range = N := by simp [← lie_submodule.coe_to_submodule_eq_iff] @[simp] lemma comap_incl_self : comap N.incl N = ⊤ := by simp [← lie_submodule.coe_to_submodule_eq_iff] end lie_submodule section top_equiv variables {R : Type u} {L : Type v} variables [comm_ring R] [lie_ring L] [lie_algebra R L] /-- The natural equivalence between the 'top' Lie subalgebra and the enclosing Lie algebra. This is the Lie subalgebra version of `submodule.top_equiv`. -/ def lie_subalgebra.top_equiv : (⊤ : lie_subalgebra R L) ≃ₗ⁅R⁆ L := { inv_fun := λ x, ⟨x, set.mem_univ x⟩, left_inv := λ x, by { ext, refl, }, right_inv := λ x, rfl, ..(⊤ : lie_subalgebra R L).incl, } @[simp] lemma lie_subalgebra.top_equiv_apply (x : (⊤ : lie_subalgebra R L)) : lie_subalgebra.top_equiv x = x := rfl /-- The natural equivalence between the 'top' Lie ideal and the enclosing Lie algebra. This is the Lie ideal version of `submodule.top_equiv`. -/ def lie_ideal.top_equiv : (⊤ : lie_ideal R L) ≃ₗ⁅R⁆ L := lie_subalgebra.top_equiv @[simp] lemma lie_ideal.top_equiv_apply (x : (⊤ : lie_ideal R L)) : lie_ideal.top_equiv x = x := rfl end top_equiv
ec6e8d207ab7e438d58e981521e3f57a56c5df05
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/set_theory/cardinal/finite.lean
9e98d2bb67b27577bf8e47e9dede10361158d4b0
[ "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
7,120
lean
/- Copyright (c) 2021 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import data.zmod.defs import set_theory.cardinal.basic /-! # Finite Cardinality Functions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Main Definitions * `nat.card α` is the cardinality of `α` as a natural number. If `α` is infinite, `nat.card α = 0`. * `part_enat.card α` is the cardinality of `α` as an extended natural number (`part ℕ` implementation). If `α` is infinite, `part_enat.card α = ⊤`. -/ open cardinal noncomputable theory open_locale big_operators variables {α β : Type*} namespace nat /-- `nat.card α` is the cardinality of `α` as a natural number. If `α` is infinite, `nat.card α = 0`. -/ protected def card (α : Type*) : ℕ := (mk α).to_nat @[simp] lemma card_eq_fintype_card [fintype α] : nat.card α = fintype.card α := mk_to_nat_eq_card @[simp] lemma card_eq_zero_of_infinite [infinite α] : nat.card α = 0 := mk_to_nat_of_infinite lemma finite_of_card_ne_zero (h : nat.card α ≠ 0) : finite α := not_infinite_iff_finite.mp $ h ∘ @nat.card_eq_zero_of_infinite α lemma card_congr (f : α ≃ β) : nat.card α = nat.card β := cardinal.to_nat_congr f lemma card_eq_of_bijective (f : α → β) (hf : function.bijective f) : nat.card α = nat.card β := card_congr (equiv.of_bijective f hf) lemma card_eq_of_equiv_fin {α : Type*} {n : ℕ} (f : α ≃ fin n) : nat.card α = n := by simpa using card_congr f /-- If the cardinality is positive, that means it is a finite type, so there is an equivalence between `α` and `fin (nat.card α)`. See also `finite.equiv_fin`. -/ def equiv_fin_of_card_pos {α : Type*} (h : nat.card α ≠ 0) : α ≃ fin (nat.card α) := begin casesI fintype_or_infinite α, { simpa using fintype.equiv_fin α }, { simpa using h }, end lemma card_of_subsingleton (a : α) [subsingleton α] : nat.card α = 1 := begin letI := fintype.of_subsingleton a, rw [card_eq_fintype_card, fintype.card_of_subsingleton a] end @[simp] lemma card_unique [unique α] : nat.card α = 1 := card_of_subsingleton default lemma card_eq_one_iff_unique : nat.card α = 1 ↔ subsingleton α ∧ nonempty α := cardinal.to_nat_eq_one_iff_unique lemma card_eq_two_iff : nat.card α = 2 ↔ ∃ x y : α, x ≠ y ∧ {x, y} = @set.univ α := (to_nat_eq_iff two_ne_zero).trans $ iff.trans (by rw [nat.cast_two]) mk_eq_two_iff lemma card_eq_two_iff' (x : α) : nat.card α = 2 ↔ ∃! y, y ≠ x := (to_nat_eq_iff two_ne_zero).trans $ iff.trans (by rw [nat.cast_two]) (mk_eq_two_iff' x) theorem card_of_is_empty [is_empty α] : nat.card α = 0 := by simp @[simp] lemma card_prod (α β : Type*) : nat.card (α × β) = nat.card α * nat.card β := by simp only [nat.card, mk_prod, to_nat_mul, to_nat_lift] @[simp] lemma card_ulift (α : Type*) : nat.card (ulift α) = nat.card α := card_congr equiv.ulift @[simp] lemma card_plift (α : Type*) : nat.card (plift α) = nat.card α := card_congr equiv.plift lemma card_pi {β : α → Type*} [fintype α] : nat.card (Π a, β a) = ∏ a, nat.card (β a) := by simp_rw [nat.card, mk_pi, prod_eq_of_fintype, to_nat_lift, to_nat_finset_prod] lemma card_fun [finite α] : nat.card (α → β) = nat.card β ^ nat.card α := begin haveI := fintype.of_finite α, rw [nat.card_pi, finset.prod_const, finset.card_univ, ←nat.card_eq_fintype_card], end @[simp] lemma card_zmod (n : ℕ) : nat.card (zmod n) = n := begin cases n, { exact nat.card_eq_zero_of_infinite }, { rw [nat.card_eq_fintype_card, zmod.card] }, end end nat namespace part_enat /-- `part_enat.card α` is the cardinality of `α` as an extended natural number. If `α` is infinite, `part_enat.card α = ⊤`. -/ def card (α : Type*) : part_enat := (mk α).to_part_enat @[simp] lemma card_eq_coe_fintype_card [fintype α] : card α = fintype.card α := mk_to_part_enat_eq_coe_card @[simp] lemma card_eq_top_of_infinite [infinite α] : card α = ⊤ := mk_to_part_enat_of_infinite lemma card_congr {α : Type*} {β : Type*} (f : α ≃ β) : part_enat.card α = part_enat.card β := cardinal.to_part_enat_congr f lemma card_ulift (α : Type*) : card (ulift α) = card α := card_congr equiv.ulift @[simp] lemma card_plift (α : Type*) : card (plift α) = card α := card_congr equiv.plift lemma card_image_of_inj_on {α : Type*} {β : Type*} {f : α → β} {s : set α} (h : set.inj_on f s) : card (f '' s) = card s := card_congr (equiv.set.image_of_inj_on f s h).symm lemma card_image_of_injective {α : Type*} {β : Type*} (f : α → β) (s : set α) (h : function.injective f) : card (f '' s) = card s := card_image_of_inj_on (set.inj_on_of_injective h s) -- Should I keep the 6 following lemmas ? @[simp] lemma _root_.cardinal.coe_nat_le_to_part_enat_iff {n : ℕ} {c : cardinal} : ↑n ≤ to_part_enat c ↔ ↑n ≤ c := by rw [← to_part_enat_cast n, to_part_enat_le_iff_le_of_le_aleph_0 (le_of_lt (nat_lt_aleph_0 n))] @[simp] lemma _root_.cardinal.to_part_enat_le_coe_nat_iff {c : cardinal} {n : ℕ} : to_part_enat c ≤ n ↔ c ≤ n := by rw [← to_part_enat_cast n, to_part_enat_le_iff_le_of_lt_aleph_0 (nat_lt_aleph_0 n)] @[simp] lemma _root_.cardinal.coe_nat_eq_to_part_enat_iff {n : ℕ} {c : cardinal} : ↑n = to_part_enat c ↔ ↑n = c := by rw [le_antisymm_iff, le_antisymm_iff, cardinal.coe_nat_le_to_part_enat_iff, cardinal.to_part_enat_le_coe_nat_iff] @[simp] lemma _root_.cardinal.to_part_enat_eq_coe_nat_iff {c : cardinal} {n : ℕ} : to_part_enat c = n ↔ c = n:= by rw [eq_comm, cardinal.coe_nat_eq_to_part_enat_iff, eq_comm] @[simp] lemma _root_.cardinal.coe_nat_lt_coe_iff_lt {n : ℕ} {c : cardinal} : ↑n < to_part_enat c ↔ ↑n < c := by simp only [← not_le, cardinal.to_part_enat_le_coe_nat_iff] @[simp] lemma _root_.cardinal.lt_coe_nat_iff_lt {n : ℕ} {c : cardinal} : to_part_enat c < n ↔ c < n := by simp only [← not_le, cardinal.coe_nat_le_to_part_enat_iff] lemma card_eq_zero_iff_empty (α : Type*) : card α = 0 ↔ is_empty α := begin rw ← cardinal.mk_eq_zero_iff, conv_rhs { rw ← nat.cast_zero }, rw ← cardinal.to_part_enat_eq_coe_nat_iff, simp only [part_enat.card, nat.cast_zero] end lemma card_le_one_iff_subsingleton (α : Type*) : card α ≤ 1 ↔ subsingleton α := begin rw ← le_one_iff_subsingleton, conv_rhs { rw ← nat.cast_one}, rw ← cardinal.to_part_enat_le_coe_nat_iff, simp only [part_enat.card, nat.cast_one] end lemma one_lt_card_iff_nontrivial (α : Type*) : 1 < card α ↔ nontrivial α := begin rw ← one_lt_iff_nontrivial, conv_rhs { rw ← nat.cast_one}, rw ← cardinal.coe_nat_lt_coe_iff_lt, simp only [part_enat.card, nat.cast_one] end lemma is_finite_of_card {α : Type*} {n : ℕ} (hα : part_enat.card α = n) : finite α := begin apply or.resolve_right (finite_or_infinite α), intro h, resetI, apply part_enat.coe_ne_top n, rw ← hα, exact part_enat.card_eq_top_of_infinite, end end part_enat
2ea8e919600e8e8bf00dfaeb8c977e2f98c67bd2
8e2026ac8a0660b5a490dfb895599fb445bb77a0
/library/init/category/applicative.lean
a4d264fa3050b814e969d16824120b37410e2115
[ "Apache-2.0" ]
permissive
pcmoritz/lean
6a8575115a724af933678d829b4f791a0cb55beb
35eba0107e4cc8a52778259bb5392300267bfc29
refs/heads/master
1,607,896,326,092
1,490,752,175,000
1,490,752,175,000
86,612,290
0
0
null
1,490,809,641,000
1,490,809,641,000
null
UTF-8
Lean
false
false
3,038
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ prelude import init.category.functor open function universes u v section set_option auto_param.check_exists false class applicative (f : Type u → Type v) extends functor f := (pure : Π {α : Type u}, α → f α) (seq : Π {α β : Type u}, f (α → β) → f α → f β) (infixr ` <$> `:100 := map) (infixl ` <*> `:60 := seq) (map := λ _ _ x y, pure x <*> y) -- ` <* ` (seq_left : Π {α β : Type u}, f α → f β → f α := λ α β a b, seq (map (const β) a) b) (seq_left_eq : ∀ {α β : Type u} (a : f α) (b : f β), seq_left a b = seq (map (const β) a) b . control_laws_tac) -- ` *> ` (seq_right : Π {α β : Type u}, f α → f β → f β := λ α β a b, seq (map (const α id) a) b) (seq_right_eq : ∀ {α β : Type u} (a : f α) (b : f β), seq_right a b = seq (map (const α id) a) b . control_laws_tac) -- applicative laws (pure_seq_eq_map : ∀ {α β : Type u} (g : α → β) (x : f α), pure g <*> x = g <$> x) -- . control_laws_tac) (map_pure : ∀ {α β : Type u} (g : α → β) (x : α), g <$> pure x = pure (g x)) (seq_pure : ∀ {α β : Type u} (g : f (α → β)) (x : α), g <*> pure x = (λ g : α → β, g x) <$> g) (seq_assoc : ∀ {α β γ : Type u} (x : f α) (g : f (α → β)) (h : f (β → γ)), h <*> (g <*> x) = (@comp α β γ <$> h) <*> g <*> x) -- defaulted functor law (map_comp := λ α β γ g h x, calc (h ∘ g) <$> x = pure (h ∘ g) <*> x : eq.symm $ pure_seq_eq_map _ _ ... = (comp h <$> pure g) <*> x : eq.rec rfl $ map_pure (comp h) g ... = pure (@comp α β γ h) <*> pure g <*> x : eq.rec rfl $ eq.symm $ pure_seq_eq_map (comp h) (pure g) ... = (@comp α β γ <$> pure h) <*> pure g <*> x : eq.rec rfl $ map_pure (@comp α β γ) h ... = pure h <*> (pure g <*> x) : eq.symm $ seq_assoc _ _ _ ... = h <$> (pure g <*> x) : pure_seq_eq_map _ _ ... = h <$> g <$> x : congr_arg _ $ pure_seq_eq_map _ _) end section variables {f : Type u → Type v} [applicative f] {α β : Type u} @[inline] def pure : α → f α := applicative.pure f @[inline] def seq_app : f (α → β) → f α → f β := applicative.seq /-- Sequence actions, discarding the first value. -/ @[inline] def seq_left : f α → f β → f α := applicative.seq_left /-- Sequence actions, discarding the second value. -/ @[inline] def seq_right : f α → f β → f β := applicative.seq_right infixl ` <*> `:60 := seq_app infixl ` <* `:60 := seq_left infixl ` *> `:60 := seq_right end -- applicative "law" derivable from other laws theorem applicative.pure_id_seq {α β : Type u} {f : Type u → Type v} [applicative f] (x : f α) : pure id <*> x = x := eq.trans (applicative.pure_seq_eq_map _ _) (functor.id_map _)
4ae5cc408213b71cdc8d6325a1ca0d18c9532585
e8d6a03af236c510516281c3b076aa984c212a1f
/lean4/ModelCount/ModelCount/Iteg.lean
961584fb35a7f73065eb0649b486dd19f2f823d9
[ "MIT" ]
permissive
minsungc/model-counting
9918a1afb2ef4cc91e6be906956b4ae2b82d10be
02f650741bddb6a94a0729889dfeb8e9b0e8521a
refs/heads/main
1,686,421,950,907
1,624,283,093,000
1,624,283,093,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
12,401
lean
import ModelCount.Lib /- itegs-/ inductive IteElt | Tr : IteElt | Fls : IteElt | Var (i : Nat) : IteElt | Ite (c t f : Nat) : IteElt deriving Repr instance : Inhabited IteElt := ⟨IteElt.Tr⟩ open IteElt /-- `IteEltBounded x b` says that if `x` is an ite clause, then the branches are less than `b`-/ def IteEltBounded : IteElt → Nat → Prop | Tr, _ => True | Fls, _ => True | Var i, _ => True | Ite c t f, b => t < b ∧ f < b -- No need to bound i, since the default value of an `IteElt` is `Tr` def Iteg := { I : Array IteElt // ∀ i, IteEltBounded I[i] i } -- coerce to an array instance itegCoe (n : Nat) : CoeHead Iteg (Array IteElt) where coe v := v.val -- TODO: a hack to fix an annoying dependcy; find a better way theorem match_aux (i : IteElt) (a b : α) (c : Nat → α) (d : Nat → Nat → Nat → α) : (match h:i with | Tr => a | Fls => b | Var j => c j | Ite c t f => d c t f) = (match i with | Tr => a | Fls => b | Var j => c j | Ite c t f => d c t f) := by cases i <;> rfl /- semantics: the `eval` function -/ def Assignment : Type := Nat → Bool namespace Iteg def evalF (I : Iteg) (v : Assignment) (m : Nat) (eval_rec : (∀ i, i < m → Bool)) : Bool := match h:I.val[m] with | Tr => true | Fls => false | Var k => v k | Ite c t f => have bdd : IteEltBounded (Ite c t f) m by { rw ←h; apply I.property } cond (v c) (eval_rec _ bdd.1) (eval_rec _ bdd.2) /-- `I.eval v j` evaluated line `j` of iteg `I` with assignment `v` -/ noncomputable def eval (I : Iteg) (v : Assignment) : Nat → Bool := WellFounded.fix Nat.ltWf (evalF I v) theorem eval_eq (I : Iteg) (v : Assignment) (j : Nat) : eval I v j = match I.val[j] with | Tr => true | Fls => false | Var k => v k | Ite c t f => cond (v c) (eval I v t) (eval I v f) := by apply Eq.trans $ WellFounded.fixEq Nat.ltWf (evalF I v) j apply Eq.trans _ (match_aux _ _ _ _ _); rfl @[simp] theorem eval_Tr {I : Iteg} {j : Nat} (h : I.val[j] = Tr) (v : Assignment) : I.eval v j = true := by rw [eval_eq, h] @[simp] theorem eval_Fls {I : Iteg} {j : Nat} (h : I.val[j] = Fls) (v : Assignment) : I.eval v j = false := by rw [eval_eq, h] @[simp] theorem eval_Var {I : Iteg} {j k : Nat} (h : I.val[j] = Var k) (v : Assignment) : I.eval v j = v k := by rw [eval_eq, h] @[simp] theorem eval_Ite {I : Iteg} {j c t f : Nat} (h : I.val[j] = Ite c t f) (v : Assignment) : I.eval v j = cond (v c) (I.eval v t) (I.eval v f) := by rw [eval_eq, h] end Iteg namespace Iteg open Finset /- the `vars` function -/ def varsF (I : Iteg) (m : Nat) (vars_rec : (∀ i, i < m → Finset Nat)) : Finset Nat := match h:I.val[m] with | Tr => ∅ | Fls => ∅ | Var j => singleton j | Ite c t f => have bdd : IteEltBounded (Ite c t f) m by { rw ←h; apply I.property } singleton c ∪ (vars_rec _ bdd.1) ∪ (vars_rec _ bdd.2) /-- `I.vars j` returns the `Finset` of variables that line `j` iteg `I` depends on. -/ noncomputable def vars (I : Iteg) : Nat → Finset Nat := WellFounded.fix Nat.ltWf (varsF I) theorem vars_eq (I : Iteg) (j : Nat) : vars I j = match I.val[j] with | Tr => ∅ | Fls => ∅ | Var k => singleton k | Ite c t f => singleton c ∪ vars I t ∪ vars I f := by apply Eq.trans $ WellFounded.fixEq Nat.ltWf (varsF I) j apply Eq.trans _ (match_aux _ _ _ _ _); rfl @[simp] theorem vars_Tr {I : Iteg} {j : Nat} (h : I.val[j] = Tr) : I.vars j = (∅ : Finset Nat) := by rw [vars_eq, h] @[simp] theorem var_Fls {I : Iteg} {j : Nat} (h : I.val[j] = Fls) : I.vars j = (∅ : Finset Nat) := by rw [vars_eq, h] @[simp] theorem vars_Var {I : Iteg} {j k : Nat} (h : I.val[j] = Var k) : I.vars j = singleton k := by rw [vars_eq, h] @[simp] theorem vars_Ite {I : Iteg} {j c t f : Nat} (h : I.val[j] = Ite c t f) : I.vars j = singleton c ∪ I.vars t ∪ I.vars f := by rw [vars_eq, h] /- the `count` function -/ def countF (I : Iteg) (numVars : Nat) (m : Nat) (count_rec : ∀ i, i < m → Nat) : Nat := match h:I.val[m] with | Tr => 2 ^ numVars | Fls => 0 | Var j => 2 ^ (numVars - 1) | Ite c t f => have bdd : IteEltBounded (Ite c t f) m by { rw ←h; apply I.property } (count_rec _ bdd.1 + count_rec _ bdd.2) / 2 /-- `I.count I j` counts the number of models of line `j` of iteg `I`, assuming `numVars` variables in total. -/ noncomputable def count (I : Iteg) (numVars : Nat) : Nat → Nat := WellFounded.fix Nat.ltWf (countF I numVars) theorem count_eq (I : Iteg) (numVars : Nat) (j : Nat) : count I numVars j = match I.val[j] with | Tr => 2 ^ numVars | Fls => 0 | Var k => 2 ^ (numVars - 1) | Ite c t f => (count I numVars t + count I numVars f) / 2 := by apply Eq.trans $ WellFounded.fixEq Nat.ltWf (countF I numVars) j apply Eq.trans _ (match_aux _ _ _ _ _); rfl @[simp] theorem count_Tr {I : Iteg} {j : Nat} (h : I.val[j] = Tr) (numVars : Nat) : I.count numVars j = 2 ^ numVars := by rw [count_eq, h] @[simp] theorem count_Fls {I : Iteg} {j : Nat} (h : I.val[j] = Fls) (numVars : Nat) : I.count numVars j = 0 := by rw [count_eq, h] @[simp] theorem count_Var {I : Iteg} {j k : Nat} (h : I.val[j] = Var k) (numVars : Nat) : I.count numVars j = 2 ^ (numVars - 1) := by rw [count_eq, h] @[simp] theorem count_Ite {I : Iteg} {j c t f : Nat} (h : I.val[j] = Ite c t f) (numVars : Nat) : I.count numVars j = (count I numVars t + count I numVars f) / 2 := by rw [count_eq, h] /- free itegs -/ def free (I : Iteg) : Prop := ∀ j, match I.val[j] with | Tr => True | Fls => True | Var k => True | Ite c t f => c ∉ I.vars t ∧ c ∉ I.vars f /- counting models assignments -/ def defined_on (v : Assignment) (s : Finset Nat) := ∀ n, ¬ n ∈ s → v n = false theorem finite_defined_on (s : Finset Nat) : Set.finite (fun v : Assignment => defined_on v s) := sorry def models (s : Finset Nat) : Finset Assignment := ⟨_, finite_defined_on s⟩ @[simp] theorem card_models (s : Finset Nat) : card (models s) = 2 ^ card s := sorry /- auxiliary lemmas -/ theorem card_models_Var {s : Finset Nat} {k : Nat} (h : k ∈ s) : card {v ∈ models s | v k = True} = 2 ^ (card s - 1) := sorry /- this is the main combinatorial lemma behind the counting algorithm -/ theorem card_models_Ite {I : Iteg} {s : Finset Nat} {c : Nat} (h₀ : c ∈ s) (h₁ : c ∉ I.vars t) (h₂ : c ∉ I.vars f) : card {v ∈ models s | cond (v c) (eval I v t) (eval I v f) = true} = (card {v ∈ models s | eval I v t = true} + card {v ∈ models s | eval I v f = true}) / 2 := sorry /- the main theorem -/ theorem card_models_Iteg (I : Iteg) (v : Assignment) (s : Finset Nat) (Ifree : free I): ∀ j, I.vars j ⊆ s → card {v ∈ models s | I.eval v j = true} = I.count (card s) j := by intro j induction j using Nat.CompleteInductionOn with | ind j ih => rw count_eq cases h:I.val[j] with | Tr => simp [h] | Fls => simp [h] | Var k => simp [vars_Var h, eval_Var h] apply card_models_Var | Ite c t f => have bdd : IteEltBounded (Ite c t f) j by { rw ←h; apply I.property } have _ := Ifree j rw h at this have free: c ∉ I.vars t ∧ c ∉ I.vars f from this simp [vars_Ite h, eval_Ite h] intro h' have h₀ : c ∈ s from singleton_subset.mp (subset_trans (subset_union_left _ _) h') have h₁ : vars I t ⊆ s from subset_trans (subset_union_left _ _) (subset_trans (subset_union_right _ _) h') have h₂ : vars I f ⊆ s from subset_trans (subset_union_right _ _) (subset_trans (subset_union_right _ _) h') have h₃ : card {v ∈ models s | I.eval v t = true} = count I (card s) t from ih _ bdd.1 h₁ have h₄ : card {v ∈ models s | I.eval v f = true} = count I (card s) f from ih _ bdd.2 h₂ rw [←h₃, ←h₄] apply card_models_Ite h₀ free.1 free.2 /- The array-based algorithm. -/ def countModelsStep (I : Array IteElt) (numVars : Nat) (i : Nat) (O : Array Nat) := O.push $ match I[i] with | Tr => 2 ^ numVars | Fls => 0 | Var k => 2^ (numVars - 1) | Ite c t f => (O[t] + O[f]) / 2 def countModels (I : Array IteElt) (numVars : Nat) : Array Nat := do let mut O : Array Nat := #[] for i in [:I.size] do O := countModelsStep I numVars i O return O /- The array-based algorithm is equal to the recursive description. -/ @[simp] theorem size_countModelsStep (I : Array IteElt) (numVars : Nat) (i : Nat) (O : Array Nat) : Array.size (countModelsStep I numVars i O) = O.size + 1 := by simp [countModelsStep] @[simp] theorem get_countModelsStep_of_lt (I : Array IteElt) (numVars : Nat) (O : Array Nat) (i j : Nat) (hi : i < O.size) : (countModelsStep I numVars j O)[i] = O[i] := by simp [countModelsStep]; rw [Array.get_push_of_lt O _ hi] @[simp] theorem get_countModelsStep_size (I : Array IteElt) (numVars : Nat) (O : Array Nat) : (countModelsStep I numVars i O)[O.size] = match I[i] with | Tr => 2 ^ numVars | Fls => 0 | Var k => 2^ (numVars - 1) | Ite c t f => (O[t] + O[f]) / 2 := by simp [countModelsStep] def countModelsRec (I : Array IteElt) (numVars : Nat) (n j : Nat) (init : Array Nat) : Array Nat := Std.Range.forIn.loop (m := Id) [:I.size] (fun i (O : Array Nat) => ForInStep.yield $ countModelsStep I numVars i O) n j init theorem countModels_eq_countModelsRec (I : Array IteElt) (numVars : Nat) : countModels I numVars = countModelsRec I numVars I.size 0 #[] := rfl theorem countModelsRecSucc (I : Array IteElt) (numVars : Nat) (n j : Nat) (init : Array Nat) : countModelsRec I numVars (n + 1) j init = if j ≥ Array.size I then init else countModelsRec I numVars n (j + 1) (countModelsStep I numVars j init) := by simp [countModelsRec] theorem countModelsRec_prop (I : Iteg) (numVars : Nat) : ∀ n j (init : Array Nat), init.size = j → (∀ i, i < j → init[i] = I.count numVars i) → n + j ≤ I.val.size → let O := countModelsRec I.val numVars n j init O.size = n + j ∧ ∀ i, i < n + j → O[i] = I.count numVars i | 0, j, init, h₁, h₂, _ => by simp [countModelsRec] exact ⟨h₁, fun i hi => h₂ _ hi⟩ | n+1, j, init, h₁, h₂, h₃ => by simp [countModelsRecSucc] let O := countModelsStep I.val numVars j init have h₄ : Array.size O = j + 1 by simp [h₁] have h₅ : ∀ i, i < j + 1 → O[i] = count I numVars i by intros i hi cases Nat.le_iff_lt_or_eq.mp $ Nat.lt_succ_iff_le.mp hi with | inl h' => rw [←h₂ _ h', get_countModelsStep_of_lt]; try rfl rw [h₁]; apply h' | inr h' => rw [h', ←h₁, get_countModelsStep_size, count_eq, h₁] cases ieq: I.val[j] with | Ite c t f => have bdd : IteEltBounded (Ite c t f) j by { rw ←ieq; apply I.property } simp [h₂ _ bdd.1, h₂ _ bdd.2] | _ => rfl have h₆ : n + (j + 1) = n + 1 + j by rw [Nat.add_assoc, Nat.add_comm j] have h₇ : n + (j + 1) ≤ I.val.size by rw [h₆]; exact h₃ let ih := countModelsRec_prop I numVars n (j+1) (countModelsStep I.val numVars j init) h₄ h₅ h₇ have h₇ : ¬ j ≥ I.val.size by apply Nat.not_le_of_lt apply Nat.lt_of_lt_of_le _ h₃ rw [Nat.add_comm (n + 1), ←Nat.add_assoc] apply Nat.lt_succ_of_le apply Nat.le_add_right rw [ifNeg h₇, ←h₆] exact ih theorem countModels_eq_count (I : Iteg) (numVars : Nat) {j} (hj : j < I.val.size) : (countModels I.val numVars)[j] = I.count numVars j := (countModelsRec_prop I numVars I.val.size 0 #[] Array.size_empty (fun _ hi => absurd hi (Nat.not_lt_zero _)) (Nat.le_refl _)).2 _ hj end Iteg
013e28cb5ebf9dc702d0dd6f929402c3b73aef4d
618003631150032a5676f229d13a079ac875ff77
/src/data/string/basic.lean
4e1ca3d56ce9ba2ce8bfcf5579bd4376641cd084
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
2,528
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Supplementary theorems about the `string` type. -/ import data.list.basic import data.char namespace string def ltb : iterator → iterator → bool | s₁ s₂ := begin cases s₂.has_next, {exact ff}, cases h₁ : s₁.has_next, {exact tt}, exact if s₁.curr = s₂.curr then have s₁.next.2.length < s₁.2.length, from match s₁, h₁ with ⟨_, a::l⟩, h := nat.lt_succ_self _ end, ltb s₁.next s₂.next else s₁.curr < s₂.curr, end using_well_founded {rel_tac := λ _ _, `[exact ⟨_, measure_wf (λ s, s.1.2.length)⟩]} instance has_lt' : has_lt string := ⟨λ s₁ s₂, ltb s₁.mk_iterator s₂.mk_iterator⟩ instance decidable_lt : @decidable_rel string (<) := by apply_instance -- short-circuit type class inference @[simp] theorem lt_iff_to_list_lt : ∀ {s₁ s₂ : string}, s₁ < s₂ ↔ s₁.to_list < s₂.to_list | ⟨i₁⟩ ⟨i₂⟩ := suffices ∀ {p₁ p₂ s₁ s₂}, ltb ⟨p₁, s₁⟩ ⟨p₂, s₂⟩ ↔ s₁ < s₂, from this, begin intros, induction s₁ with a s₁ IH generalizing p₁ p₂ s₂; cases s₂ with b s₂; rw ltb; simp [iterator.has_next], { exact iff_of_false bool.ff_ne_tt (lt_irrefl _) }, { exact iff_of_true rfl list.lex.nil }, { exact iff_of_false bool.ff_ne_tt (not_lt_of_lt list.lex.nil) }, { dsimp [iterator.has_next, iterator.curr, iterator.next], split_ifs, { subst b, exact IH.trans list.lex.cons_iff.symm }, { simp, refine ⟨list.lex.rel, λ e, _⟩, cases e, {cases h rfl}, assumption } } end instance has_le : has_le string := ⟨λ s₁ s₂, ¬ s₂ < s₁⟩ instance decidable_le : @decidable_rel string (≤) := by apply_instance -- short-circuit type class inference @[simp] theorem le_iff_to_list_le {s₁ s₂ : string} : s₁ ≤ s₂ ↔ s₁.to_list ≤ s₂.to_list := (not_congr lt_iff_to_list_lt).trans not_lt theorem to_list_inj : ∀ {s₁ s₂}, to_list s₁ = to_list s₂ ↔ s₁ = s₂ | ⟨s₁⟩ ⟨s₂⟩ := ⟨congr_arg _, congr_arg _⟩ instance : decidable_linear_order string := by refine_struct { lt := (<), le := (≤), decidable_lt := by apply_instance, decidable_le := string.decidable_le, decidable_eq := by apply_instance, .. }; { simp only [le_iff_to_list_le, lt_iff_to_list_lt, ← to_list_inj], introv, apply_field } end string
d4234199e2f8a7b7e2345e3bab50b94b8fdd68ec
78269ad0b3c342b20786f60690708b6e328132b0
/src/library_dev/algebra/lattice/zorn.lean
8e0c81bbe7fc738646afa931d076047fca3d793c
[]
no_license
dselsam/library_dev
e74f46010fee9c7b66eaa704654cad0fcd2eefca
1b4e34e7fb067ea5211714d6d3ecef5132fc8218
refs/heads/master
1,610,372,841,675
1,497,014,421,000
1,497,014,421,000
86,526,137
0
0
null
1,490,752,133,000
1,490,752,132,000
null
UTF-8
Lean
false
false
8,656
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 Zorn's lemmas. Ported from Isabelle/HOL (written by Jacques D. Fleuriot, Tobias Nipkow, and Christian Sternagel). -/ import ...data.set noncomputable theory universes u open set classical local attribute [instance] decidable_inhabited local attribute [instance] prop_decidable namespace zorn section chain parameters {α : Type u} {r : α → α → Prop} local infix ` ≺ `:50 := r def chain (c : set α) := pairwise_on c (λx y, x ≺ y ∨ y ≺ x) lemma chain_insert {c : set α} {a : α} (hc : chain c) (ha : ∀b∈c, b ≠ a → a ≺ b ∨ b ≺ a) : chain (insert a c) := forall_insert_of_forall (take x hx, forall_insert_of_forall (hc x hx) (take hneq, (ha x hx hneq).symm)) (forall_insert_of_forall (take x hx hneq, ha x hx $ take h', hneq h'.symm) (take h, (h rfl).rec _)) def super_chain (c₁ c₂ : set α) := chain c₂ ∧ c₁ ⊂ c₂ def is_max_chain (c : set α) := chain c ∧ ¬ (∃c', super_chain c c') def succ_chain (c : set α) := if h : ∃c', chain c ∧ super_chain c c' then some h else c lemma succ_spec {c : set α} (h : ∃c', chain c ∧ super_chain c c') : super_chain c (succ_chain c) := let ⟨c', hc'⟩ := h in have chain c ∧ super_chain c (some h), from @some_spec _ (λc', chain c ∧ super_chain c c') _, by simp [succ_chain, dif_pos, h, this.right] lemma chain_succ {c : set α} (hc : chain c) : chain (succ_chain c) := if h : ∃c', chain c ∧ super_chain c c' then (succ_spec h).left else by simp [succ_chain, dif_neg, h]; exact hc lemma super_of_not_max {c : set α} (hc₁ : chain c) (hc₂ : ¬ is_max_chain c) : super_chain c (succ_chain c) := begin simp [is_max_chain, not_and_iff, not_not_iff] at hc₂, have ∃c', super_chain c c', from hc₂.neg_resolve_left hc₁, let ⟨c', hc'⟩ := this in show super_chain c (succ_chain c), from succ_spec ⟨c', hc₁, hc'⟩ end lemma succ_increasing {c : set α} : c ⊆ succ_chain c := if h : ∃c', chain c ∧ super_chain c c' then have super_chain c (succ_chain c), from succ_spec h, this.right.left else by simp [succ_chain, dif_neg, h, subset.refl] inductive chain_closure : set α → Prop | succ : ∀{s}, chain_closure s → chain_closure (succ_chain s) | union : ∀{s}, (∀a∈s, chain_closure a) → chain_closure (⋃₀ s) lemma chain_closure_empty : chain_closure ∅ := have chain_closure (⋃₀ ∅), from chain_closure.union $ take a h, h.rec _, by simp at this; assumption lemma chain_closure_closure : chain_closure (⋃₀ {s | chain_closure s}) := chain_closure.union $ take s hs, hs variables {c c₁ c₂ c₃ : set α} private lemma chain_closure_succ_total_aux (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : ∀{c₃}, chain_closure c₃ → c₃ ⊆ c₂ → c₂ = c₃ ∨ succ_chain c₃ ⊆ c₂) : c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁ := begin induction hc₁, case _root_.zorn.chain_closure.succ c₃ hc₃ ih { cases ih with ih ih, { note h := h hc₃ ih, cases h with h h, { exact (or.inr $ h ▸ subset.refl _) }, { exact (or.inl h) } }, { exact (or.inr $ subset.trans ih succ_increasing) } }, case _root_.zorn.chain_closure.union s hs ih { exact (or_of_not_implies $ take hn, sUnion_subset $ take a ha, have a ⊆ c₂ ∨ succ_chain c₂ ⊆ a, from ih a ha, this.resolve_right $ take h, hn $ subset.trans h $ subset_sUnion_of_mem ha) } end private lemma chain_closure_succ_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h : c₁ ⊆ c₂) : c₂ = c₁ ∨ succ_chain c₁ ⊆ c₂ := begin induction hc₂ generalizing c₁ hc₁ h, case _root_.zorn.chain_closure.succ c₂ hc₂ ih { note h₁ : c₁ ⊆ c₂ ∨ @succ_chain α r c₂ ⊆ c₁ := (chain_closure_succ_total_aux hc₁ hc₂ $ take c₁, ih), cases h₁ with h₁ h₁, { note h₂ := ih hc₁ h₁, cases h₂ with h₂ h₂, { exact (or.inr $ h₂ ▸ subset.refl _) }, { exact (or.inr $ subset.trans h₂ succ_increasing) } }, { exact (or.inl $ subset.antisymm h₁ h) } }, case _root_.zorn.chain_closure.union s hs ih { apply or.imp (take h', subset.antisymm h' h) id, apply classical.by_contradiction, simp [not_or_iff, sUnion_subset_iff, not_forall_iff_exists_not, not_implies_iff_and_not], intro h, cases h with h₁ h₂, cases h₂ with c₃ h₂, cases h₂ with h₂ hc₃, note h := chain_closure_succ_total_aux hc₁ (hs c₃ hc₃) (take c₄, ih _ hc₃), cases h with h h, { note h' := ih c₃ hc₃ hc₁ h, cases h' with h' h', { exact (h₂ $ h' ▸ subset.refl _) }, { exact (h₁ $ subset.trans h' $ subset_sUnion_of_mem hc₃) } }, { exact (h₂ $ subset.trans succ_increasing h) } } end lemma chain_closure_total (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) : c₁ ⊆ c₂ ∨ c₂ ⊆ c₁ := have c₁ ⊆ c₂ ∨ succ_chain c₂ ⊆ c₁, from chain_closure_succ_total_aux hc₁ hc₂ $ take c₃ hc₃, chain_closure_succ_total hc₃ hc₂, or.imp_right (suppose succ_chain c₂ ⊆ c₁, subset.trans succ_increasing this) this lemma chain_closure_succ_fixpoint (hc₁ : chain_closure c₁) (hc₂ : chain_closure c₂) (h_eq : succ_chain c₂ = c₂) : c₁ ⊆ c₂ := begin induction hc₁, case _root_.zorn.chain_closure.succ c₁ hc₁ h { exact or.elim (chain_closure_succ_total hc₁ hc₂ h) (take h, h ▸ h_eq.symm ▸ subset.refl c₂) id }, case _root_.zorn.chain_closure.union s hs ih { exact (sUnion_subset $ take c₁ hc₁, ih c₁ hc₁) } end lemma chain_closure_succ_fixpoint_iff (hc : chain_closure c) : succ_chain c = c ↔ c = ⋃₀ {c | chain_closure c} := ⟨take h, subset.antisymm (subset_sUnion_of_mem hc) (chain_closure_succ_fixpoint chain_closure_closure hc h), suppose c = ⋃₀{c : set α | chain_closure c}, subset.antisymm (calc succ_chain c ⊆ ⋃₀{c : set α | chain_closure c} : subset_sUnion_of_mem $ chain_closure.succ hc ... = c : this.symm) succ_increasing⟩ lemma chain_chain_closure (hc : chain_closure c) : chain c := begin induction hc, case _root_.zorn.chain_closure.succ c hc h { exact chain_succ h }, case _root_.zorn.chain_closure.union s hs h { note h : ∀c∈s, zorn.chain c := h, exact take c₁ ⟨t₁, ht₁, (hc₁ : c₁ ∈ t₁)⟩ c₂ ⟨t₂, ht₂, (hc₂ : c₂ ∈ t₂)⟩ hneq, have t₁ ⊆ t₂ ∨ t₂ ⊆ t₁, from chain_closure_total (hs _ ht₁) (hs _ ht₂), or.elim this (suppose t₁ ⊆ t₂, h t₂ ht₂ c₁ (this hc₁) c₂ hc₂ hneq) (suppose t₂ ⊆ t₁, h t₁ ht₁ c₁ hc₁ c₂ (this hc₂) hneq) } end def max_chain := (⋃₀ {c | chain_closure c}) /-- Hausdorff's maximality principle There exists a maximal totally ordered subset of `α`. Note that we do not require `α` to be partially ordered by `r`. -/ lemma max_chain_spec : is_max_chain max_chain := classical.by_contradiction $ suppose ¬ is_max_chain (⋃₀ {c | chain_closure c}), have super_chain (⋃₀ {c | chain_closure c}) (succ_chain (⋃₀ {c | chain_closure c})), from super_of_not_max (chain_chain_closure chain_closure_closure) this, let ⟨h₁, h₂, (h₃ : (⋃₀ {c | chain_closure c}) ≠ succ_chain (⋃₀ {c | chain_closure c}))⟩ := this in have succ_chain (⋃₀ {c | chain_closure c}) = (⋃₀ {c | chain_closure c}), from (chain_closure_succ_fixpoint_iff chain_closure_closure).mpr rfl, h₃ this.symm /-- Zorn's lemma If every chain has an upper bound, then there is a maximal element -/ lemma zorn (h : ∀c, chain c → ∃ub, ∀a∈c, a ≺ ub) (trans : ∀{a b c}, a ≺ b → b ≺ c → a ≺ c) : ∃m, ∀a, m ≺ a → a ≺ m := have ∃ub, ∀a∈max_chain, a ≺ ub, from h _ $ max_chain_spec.left, let ⟨ub, (hub : ∀a∈max_chain, a ≺ ub)⟩ := this in ⟨ub, take a ha, have chain (insert a max_chain), from chain_insert max_chain_spec.left $ take b hb _, or.inr $ trans (hub b hb) ha, have a ∈ max_chain, from classical.by_contradiction $ assume h : a ∉ max_chain, max_chain_spec.right $ ⟨insert a max_chain, this, ssubset_insert h⟩, hub a this⟩ end chain lemma zorn_weak_order {α : Type u} [weak_order α] (h : ∀c:set α, @chain α (≤) c → ∃ub, ∀a∈c, a ≤ ub) : ∃m:α, ∀a, m ≤ a → a = m := let ⟨m, hm⟩ := @zorn α (≤) h (take a b c, le_trans) in ⟨m, take a ha, le_antisymm (hm a ha) ha⟩ end zorn
894d5766df4d1ed6d8864f22d0afce245a3b2aa7
7571914d3f4d9677288f35ab1a53a2ad70a62bd7
/library/init/data/nat/lemmas.lean
0c21d5fd200d951e89099d5c093de24672a43292
[ "Apache-2.0" ]
permissive
picrin/lean-1
a395fed5287995f09a15a190bb24609919a0727f
b50597228b42a7eaa01bf8cb7a4fb1a98e7a8aab
refs/heads/master
1,610,757,735,162
1,502,008,413,000
1,502,008,413,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
50,506
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad -/ prelude import init.data.nat.basic init.data.nat.div init.data.nat.pow init.meta init.algebra.functions universes u namespace nat attribute [pre_smt] nat_zero_eq_zero protected lemma zero_add : ∀ n : ℕ, 0 + n = n | 0 := rfl | (n+1) := congr_arg succ (zero_add n) lemma succ_add : ∀ n m : ℕ, (succ n) + m = succ (n + m) | n 0 := rfl | n (m+1) := congr_arg succ (succ_add n m) lemma add_succ (n m : ℕ) : n + succ m = succ (n + m) := rfl protected lemma add_zero (n : ℕ) : n + 0 = n := rfl lemma add_one (n : ℕ) : n + 1 = succ n := rfl lemma succ_eq_add_one (n : ℕ) : succ n = n + 1 := rfl protected lemma add_comm : ∀ n m : ℕ, n + m = m + n | n 0 := eq.symm (nat.zero_add n) | n (m+1) := suffices succ (n + m) = succ (m + n), from eq.symm (succ_add m n) ▸ this, congr_arg succ (add_comm n m) protected lemma add_assoc : ∀ n m k : ℕ, (n + m) + k = n + (m + k) | n m 0 := rfl | n m (succ k) := by rw [add_succ, add_succ, add_assoc] protected lemma add_left_comm : ∀ (n m k : ℕ), n + (m + k) = m + (n + k) := left_comm nat.add nat.add_comm nat.add_assoc protected lemma add_left_cancel : ∀ {n m k : ℕ}, n + m = n + k → m = k | 0 m k := by simp [nat.zero_add] {contextual := tt} | (succ n) m k := λ h, have n+m = n+k, by simp [succ_add] at h; injection h, add_left_cancel this protected lemma add_right_cancel {n m k : ℕ} (h : n + m = k + m) : n = k := have m + n = m + k, by rwa [nat.add_comm n m, nat.add_comm k m] at h, nat.add_left_cancel this lemma succ_ne_zero (n : ℕ) : succ n ≠ 0 := assume h, nat.no_confusion h lemma succ_ne_self : ∀ n : ℕ, succ n ≠ n | 0 h := absurd h (nat.succ_ne_zero 0) | (n+1) h := succ_ne_self n (nat.no_confusion h (λ h, h)) protected lemma one_ne_zero : 1 ≠ (0 : ℕ) := assume h, nat.no_confusion h protected lemma zero_ne_one : 0 ≠ (1 : ℕ) := assume h, nat.no_confusion h instance : zero_ne_one_class ℕ := { zero := 0, one := 1, zero_ne_one := nat.zero_ne_one } lemma eq_zero_of_add_eq_zero_right : ∀ {n m : ℕ}, n + m = 0 → n = 0 | 0 m := by simp [nat.zero_add] | (n+1) m := λ h, begin exfalso, rw [add_one, succ_add] at h, apply succ_ne_zero _ h end lemma eq_zero_of_add_eq_zero_left {n m : ℕ} (h : n + m = 0) : m = 0 := @eq_zero_of_add_eq_zero_right m n (nat.add_comm n m ▸ h) @[simp] lemma pred_zero : pred 0 = 0 := rfl @[simp] lemma pred_succ (n : ℕ) : pred (succ n) = n := rfl protected lemma mul_zero (n : ℕ) : n * 0 = 0 := rfl lemma mul_succ (n m : ℕ) : n * succ m = n * m + n := rfl protected theorem zero_mul : ∀ (n : ℕ), 0 * n = 0 | 0 := rfl | (succ n) := by rw [mul_succ, zero_mul] private meta def sort_add := `[simp [nat.add_assoc, nat.add_comm, nat.add_left_comm]] lemma succ_mul : ∀ (n m : ℕ), (succ n) * m = (n * m) + m | n 0 := rfl | n (succ m) := begin simp [mul_succ, add_succ, succ_mul n m], sort_add end protected lemma right_distrib : ∀ (n m k : ℕ), (n + m) * k = n * k + m * k | n m 0 := rfl | n m (succ k) := begin simp [mul_succ, right_distrib n m k], sort_add end protected lemma left_distrib : ∀ (n m k : ℕ), n * (m + k) = n * m + n * k | 0 m k := by simp [nat.zero_mul] | (succ n) m k := begin simp [succ_mul, left_distrib n m k], sort_add end protected lemma mul_comm : ∀ (n m : ℕ), n * m = m * n | n 0 := by rw [nat.zero_mul, nat.mul_zero] | n (succ m) := by simp [mul_succ, succ_mul, mul_comm n m] protected lemma mul_assoc : ∀ (n m k : ℕ), (n * m) * k = n * (m * k) | n m 0 := rfl | n m (succ k) := by simp [mul_succ, nat.left_distrib, mul_assoc n m k] protected lemma mul_one : ∀ (n : ℕ), n * 1 = n := nat.zero_add protected lemma one_mul (n : ℕ) : 1 * n = n := by rw [nat.mul_comm, nat.mul_one] instance : comm_semiring nat := {add := nat.add, add_assoc := nat.add_assoc, zero := nat.zero, zero_add := nat.zero_add, add_zero := nat.add_zero, add_comm := nat.add_comm, mul := nat.mul, mul_assoc := nat.mul_assoc, one := nat.succ nat.zero, one_mul := nat.one_mul, mul_one := nat.mul_one, left_distrib := nat.left_distrib, right_distrib := nat.right_distrib, zero_mul := nat.zero_mul, mul_zero := nat.mul_zero, mul_comm := nat.mul_comm} /- properties of inequality -/ protected lemma le_of_eq {n m : ℕ} (p : n = m) : n ≤ m := p ▸ less_than_or_equal.refl n lemma le_succ_of_le {n m : ℕ} (h : n ≤ m) : n ≤ succ m := nat.le_trans h (le_succ m) lemma le_of_succ_le {n m : ℕ} (h : succ n ≤ m) : n ≤ m := nat.le_trans (le_succ n) h protected lemma le_of_lt {n m : ℕ} (h : n < m) : n ≤ m := le_of_succ_le h def lt.step {n m : ℕ} : n < m → n < succ m := less_than_or_equal.step lemma eq_zero_or_pos (n : ℕ) : n = 0 ∨ n > 0 := by {cases n, exact or.inl rfl, exact or.inr (succ_pos _)} protected lemma pos_of_ne_zero {n : nat} : n ≠ 0 → n > 0 := or.resolve_left (eq_zero_or_pos n) protected lemma lt_trans {n m k : ℕ} (h₁ : n < m) : m < k → n < k := nat.le_trans (less_than_or_equal.step h₁) protected lemma lt_of_le_of_lt {n m k : ℕ} (h₁ : n ≤ m) : m < k → n < k := nat.le_trans (succ_le_succ h₁) def lt.base (n : ℕ) : n < succ n := nat.le_refl (succ n) lemma lt_succ_self (n : ℕ) : n < succ n := lt.base n protected lemma le_antisymm {n m : ℕ} (h₁ : n ≤ m) : m ≤ n → n = m := less_than_or_equal.cases_on h₁ (λ a, rfl) (λ a b c, absurd (nat.lt_of_le_of_lt b c) (nat.lt_irrefl n)) protected lemma lt_or_ge : ∀ (a b : ℕ), a < b ∨ a ≥ b | a 0 := or.inr (zero_le a) | a (b+1) := match lt_or_ge a b with | or.inl h := or.inl (le_succ_of_le h) | or.inr h := match nat.eq_or_lt_of_le h with | or.inl h1 := or.inl (h1 ▸ lt_succ_self b) | or.inr h1 := or.inr h1 end end protected lemma le_total {m n : ℕ} : m ≤ n ∨ n ≤ m := or.imp_left nat.le_of_lt (nat.lt_or_ge m n) protected lemma lt_of_le_and_ne {m n : ℕ} (h1 : m ≤ n) : m ≠ n → m < n := or.resolve_right (or.swap (nat.eq_or_lt_of_le h1)) protected lemma lt_iff_le_not_le {m n : ℕ} : m < n ↔ (m ≤ n ∧ ¬ n ≤ m) := ⟨λ hmn, ⟨nat.le_of_lt hmn, λ hnm, nat.lt_irrefl _ (nat.lt_of_le_of_lt hnm hmn)⟩, λ ⟨hmn, hnm⟩, nat.lt_of_le_and_ne hmn (λ heq, hnm (heq ▸ nat.le_refl _))⟩ instance : linear_order ℕ := { le := nat.less_than_or_equal, le_refl := @nat.le_refl, le_trans := @nat.le_trans, le_antisymm := @nat.le_antisymm, le_total := @nat.le_total, lt := nat.lt, lt_iff_le_not_le := @nat.lt_iff_le_not_le } lemma eq_zero_of_le_zero {n : nat} (h : n ≤ 0) : n = 0 := le_antisymm h (zero_le _) lemma succ_lt_succ {a b : ℕ} : a < b → succ a < succ b := succ_le_succ lemma lt_of_succ_lt {a b : ℕ} : succ a < b → a < b := le_of_succ_le lemma lt_of_succ_lt_succ {a b : ℕ} : succ a < succ b → a < b := le_of_succ_le_succ lemma pred_lt_pred : ∀ {n m : ℕ}, n ≠ 0 → m ≠ 0 → n < m → pred n < pred m | 0 _ h₁ h₂ h := absurd rfl h₁ | _ 0 h₁ h₂ h := absurd rfl h₂ | (succ n) (succ m) _ _ h := lt_of_succ_lt_succ h lemma lt_of_succ_le {a b : ℕ} (h : succ a ≤ b) : a < b := h lemma succ_le_of_lt {a b : ℕ} (h : a < b) : succ a ≤ b := h lemma le_add_right : ∀ (n k : ℕ), n ≤ n + k | n 0 := nat.le_refl n | n (k+1) := le_succ_of_le (le_add_right n k) lemma le_add_left (n m : ℕ): n ≤ m + n := nat.add_comm n m ▸ le_add_right n m lemma le.dest : ∀ {n m : ℕ}, n ≤ m → ∃ k, n + k = m | n ._ (less_than_or_equal.refl ._) := ⟨0, rfl⟩ | n ._ (@less_than_or_equal.step ._ m h) := match le.dest h with | ⟨w, hw⟩ := ⟨succ w, hw ▸ add_succ n w⟩ end lemma le.intro {n m k : ℕ} (h : n + k = m) : n ≤ m := h ▸ le_add_right n k protected lemma add_le_add_left {n m : ℕ} (h : n ≤ m) (k : ℕ) : k + n ≤ k + m := match le.dest h with | ⟨w, hw⟩ := @le.intro _ _ w begin rw [nat.add_assoc, hw] end end protected lemma add_le_add_right {n m : ℕ} (h : n ≤ m) (k : ℕ) : n + k ≤ m + k := begin rw [nat.add_comm n k, nat.add_comm m k], apply nat.add_le_add_left h end protected lemma le_of_add_le_add_left {k n m : ℕ} (h : k + n ≤ k + m) : n ≤ m := match le.dest h with | ⟨w, hw⟩ := @le.intro _ _ w begin dsimp at hw, rw [nat.add_assoc] at hw, apply nat.add_left_cancel hw end end protected lemma le_of_add_le_add_right {k n m : ℕ} : n + k ≤ m + k → n ≤ m := begin rw [nat.add_comm _ k, nat.add_comm _ k], apply nat.le_of_add_le_add_left end protected lemma add_le_add_iff_le_right (k n m : ℕ) : n + k ≤ m + k ↔ n ≤ m := ⟨ nat.le_of_add_le_add_right , assume h, nat.add_le_add_right h _ ⟩ protected theorem lt_of_add_lt_add_left {k n m : ℕ} (h : k + n < k + m) : n < m := let h' := nat.le_of_lt h in nat.lt_of_le_and_ne (nat.le_of_add_le_add_left h') (λ heq, nat.lt_irrefl (k + m) begin rw heq at h, assumption end) protected lemma add_lt_add_left {n m : ℕ} (h : n < m) (k : ℕ) : k + n < k + m := lt_of_succ_le (add_succ k n ▸ nat.add_le_add_left (succ_le_of_lt h) k) protected lemma add_lt_add_right {n m : ℕ} (h : n < m) (k : ℕ) : n + k < m + k := nat.add_comm k m ▸ nat.add_comm k n ▸ nat.add_lt_add_left h k protected lemma lt_add_of_pos_right {n k : ℕ} (h : k > 0) : n < n + k := nat.add_lt_add_left h n protected lemma lt_add_of_pos_left {n k : ℕ} (h : k > 0) : n < k + n := by rw add_comm; exact nat.lt_add_of_pos_right h protected lemma zero_lt_one : 0 < (1:nat) := zero_lt_succ 0 lemma mul_le_mul_left {n m : ℕ} (k : ℕ) (h : n ≤ m) : k * n ≤ k * m := match le.dest h with | ⟨l, hl⟩ := have k * n + k * l = k * m, by rw [← left_distrib, hl], le.intro this end lemma mul_le_mul_right {n m : ℕ} (k : ℕ) (h : n ≤ m) : n * k ≤ m * k := mul_comm k m ▸ mul_comm k n ▸ mul_le_mul_left k h protected lemma mul_lt_mul_of_pos_left {n m k : ℕ} (h : n < m) (hk : k > 0) : k * n < k * m := nat.lt_of_lt_of_le (nat.lt_add_of_pos_right hk) (mul_succ k n ▸ nat.mul_le_mul_left k (succ_le_of_lt h)) protected lemma mul_lt_mul_of_pos_right {n m k : ℕ} (h : n < m) (hk : k > 0) : n * k < m * k := mul_comm k m ▸ mul_comm k n ▸ nat.mul_lt_mul_of_pos_left h hk instance : decidable_linear_ordered_semiring nat := { nat.comm_semiring with add_left_cancel := @nat.add_left_cancel, add_right_cancel := @nat.add_right_cancel, lt := nat.lt, le := nat.le, le_refl := nat.le_refl, le_trans := @nat.le_trans, le_antisymm := @nat.le_antisymm, le_total := @nat.le_total, lt_of_add_lt_add_left := @nat.lt_of_add_lt_add_left, lt_iff_le_not_le := @lt_iff_le_not_le _ _, add_lt_add_left := @nat.add_lt_add_left, add_le_add_left := @nat.add_le_add_left, le_of_add_le_add_left := @nat.le_of_add_le_add_left, zero_lt_one := zero_lt_succ 0, mul_le_mul_of_nonneg_left := assume a b c h₁ h₂, nat.mul_le_mul_left c h₁, mul_le_mul_of_nonneg_right := assume a b c h₁ h₂, nat.mul_le_mul_right c h₁, mul_lt_mul_of_pos_left := @nat.mul_lt_mul_of_pos_left, mul_lt_mul_of_pos_right := @nat.mul_lt_mul_of_pos_right, decidable_lt := nat.decidable_lt, decidable_le := nat.decidable_le, decidable_eq := nat.decidable_eq } -- all the fields are already included in the decidable_linear_ordered_semiring instance instance : decidable_linear_ordered_cancel_comm_monoid ℕ := { nat.decidable_linear_ordered_semiring with add_left_cancel := @nat.add_left_cancel } lemma le_of_lt_succ {m n : nat} : m < succ n → m ≤ n := le_of_succ_le_succ theorem eq_of_mul_eq_mul_left {m k n : ℕ} (Hn : n > 0) (H : n * m = n * k) : m = k := le_antisymm (le_of_mul_le_mul_left (le_of_eq H) Hn) (le_of_mul_le_mul_left (le_of_eq H.symm) Hn) theorem eq_of_mul_eq_mul_right {n m k : ℕ} (Hm : m > 0) (H : n * m = k * m) : n = k := by rw [mul_comm n m, mul_comm k m] at H; exact eq_of_mul_eq_mul_left Hm H /- sub properties -/ @[simp] protected lemma zero_sub : ∀ a : ℕ, 0 - a = 0 | 0 := rfl | (a+1) := congr_arg pred (zero_sub a) lemma sub_lt_succ (a b : ℕ) : a - b < succ a := lt_succ_of_le (sub_le a b) protected theorem sub_le_sub_right {n m : ℕ} (h : n ≤ m) : ∀ k, n - k ≤ m - k | 0 := h | (succ z) := pred_le_pred (sub_le_sub_right z) /- bit0/bit1 properties -/ protected lemma bit0_succ_eq (n : ℕ) : bit0 (succ n) = succ (succ (bit0 n)) := show succ (succ n + n) = succ (succ (n + n)), from congr_arg succ (succ_add n n) protected lemma bit1_eq_succ_bit0 (n : ℕ) : bit1 n = succ (bit0 n) := rfl protected lemma bit1_succ_eq (n : ℕ) : bit1 (succ n) = succ (succ (bit1 n)) := eq.trans (nat.bit1_eq_succ_bit0 (succ n)) (congr_arg succ (nat.bit0_succ_eq n)) protected lemma bit0_ne_zero : ∀ {n : ℕ}, n ≠ 0 → bit0 n ≠ 0 | 0 h := absurd rfl h | (n+1) h := succ_ne_zero _ protected lemma bit1_ne_zero (n : ℕ) : bit1 n ≠ 0 := show succ (n + n) ≠ 0, from succ_ne_zero (n + n) protected lemma bit1_ne_one : ∀ {n : ℕ}, n ≠ 0 → bit1 n ≠ 1 | 0 h h1 := absurd rfl h | (n+1) h h1 := nat.no_confusion h1 (λ h2, absurd h2 (succ_ne_zero _)) protected lemma bit0_ne_one : ∀ n : ℕ, bit0 n ≠ 1 | 0 h := absurd h (ne.symm nat.one_ne_zero) | (n+1) h := have h1 : succ (succ (n + n)) = 1, from succ_add n n ▸ h, nat.no_confusion h1 (λ h2, absurd h2 (succ_ne_zero (n + n))) protected lemma add_self_ne_one : ∀ (n : ℕ), n + n ≠ 1 | 0 h := nat.no_confusion h | (n+1) h := have h1 : succ (succ (n + n)) = 1, from succ_add n n ▸ h, nat.no_confusion h1 (λ h2, absurd h2 (nat.succ_ne_zero (n + n))) protected lemma bit1_ne_bit0 : ∀ (n m : ℕ), bit1 n ≠ bit0 m | 0 m h := absurd h (ne.symm (nat.add_self_ne_one m)) | (n+1) 0 h := have h1 : succ (bit0 (succ n)) = 0, from h, absurd h1 (nat.succ_ne_zero _) | (n+1) (m+1) h := have h1 : succ (succ (bit1 n)) = succ (succ (bit0 m)), from nat.bit0_succ_eq m ▸ nat.bit1_succ_eq n ▸ h, have h2 : bit1 n = bit0 m, from nat.no_confusion h1 (λ h2', nat.no_confusion h2' (λ h2'', h2'')), absurd h2 (bit1_ne_bit0 n m) protected lemma bit0_ne_bit1 : ∀ (n m : ℕ), bit0 n ≠ bit1 m := λ n m : nat, ne.symm (nat.bit1_ne_bit0 m n) protected lemma bit0_inj : ∀ {n m : ℕ}, bit0 n = bit0 m → n = m | 0 0 h := rfl | 0 (m+1) h := by contradiction | (n+1) 0 h := by contradiction | (n+1) (m+1) h := have succ (succ (n + n)) = succ (succ (m + m)), begin unfold bit0 at h, simp [add_one, add_succ, succ_add] at h, exact h end, have n + n = m + m, by repeat {injection this with this}, have n = m, from bit0_inj this, by rw this protected lemma bit1_inj : ∀ {n m : ℕ}, bit1 n = bit1 m → n = m := λ n m h, have succ (bit0 n) = succ (bit0 m), begin simp [nat.bit1_eq_succ_bit0] at h, assumption end, have bit0 n = bit0 m, by injection this, nat.bit0_inj this protected lemma bit0_ne {n m : ℕ} : n ≠ m → bit0 n ≠ bit0 m := λ h₁ h₂, absurd (nat.bit0_inj h₂) h₁ protected lemma bit1_ne {n m : ℕ} : n ≠ m → bit1 n ≠ bit1 m := λ h₁ h₂, absurd (nat.bit1_inj h₂) h₁ protected lemma zero_ne_bit0 {n : ℕ} : n ≠ 0 → 0 ≠ bit0 n := λ h, ne.symm (nat.bit0_ne_zero h) protected lemma zero_ne_bit1 (n : ℕ) : 0 ≠ bit1 n := ne.symm (nat.bit1_ne_zero n) protected lemma one_ne_bit0 (n : ℕ) : 1 ≠ bit0 n := ne.symm (nat.bit0_ne_one n) protected lemma one_ne_bit1 {n : ℕ} : n ≠ 0 → 1 ≠ bit1 n := λ h, ne.symm (nat.bit1_ne_one h) protected lemma zero_lt_bit1 (n : nat) : 0 < bit1 n := zero_lt_succ _ protected lemma zero_lt_bit0 : ∀ {n : nat}, n ≠ 0 → 0 < bit0 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit0_succ_eq, apply zero_lt_succ end protected lemma one_lt_bit1 : ∀ {n : nat}, n ≠ 0 → 1 < bit1 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit1_succ_eq, apply succ_lt_succ, apply zero_lt_succ end protected lemma one_lt_bit0 : ∀ {n : nat}, n ≠ 0 → 1 < bit0 n | 0 h := by contradiction | (succ n) h := begin rw nat.bit0_succ_eq, apply succ_lt_succ, apply zero_lt_succ end protected lemma bit0_lt {n m : nat} (h : n < m) : bit0 n < bit0 m := add_lt_add h h protected lemma bit1_lt {n m : nat} (h : n < m) : bit1 n < bit1 m := succ_lt_succ (add_lt_add h h) protected lemma bit0_lt_bit1 {n m : nat} (h : n ≤ m) : bit0 n < bit1 m := lt_succ_of_le (add_le_add h h) protected lemma bit1_lt_bit0 : ∀ {n m : nat}, n < m → bit1 n < bit0 m | n 0 h := absurd h (not_lt_zero _) | n (succ m) h := have n ≤ m, from le_of_lt_succ h, have succ (n + n) ≤ succ (m + m), from succ_le_succ (add_le_add this this), have succ (n + n) ≤ succ m + m, {rw succ_add, assumption}, show succ (n + n) < succ (succ m + m), from lt_succ_of_le this protected lemma one_le_bit1 (n : ℕ) : 1 ≤ bit1 n := show 1 ≤ succ (bit0 n), from succ_le_succ (zero_le (bit0 n)) protected lemma one_le_bit0 : ∀ (n : ℕ), n ≠ 0 → 1 ≤ bit0 n | 0 h := absurd rfl h | (n+1) h := suffices 1 ≤ succ (succ (bit0 n)), from eq.symm (nat.bit0_succ_eq n) ▸ this, succ_le_succ (zero_le (succ (bit0 n))) /- Extra instances to short-circuit type class resolution -/ instance : add_comm_monoid nat := by apply_instance instance : add_monoid nat := by apply_instance instance : monoid nat := by apply_instance instance : comm_monoid nat := by apply_instance instance : comm_semigroup nat := by apply_instance instance : semigroup nat := by apply_instance instance : add_comm_semigroup nat := by apply_instance instance : add_semigroup nat := by apply_instance instance : distrib nat := by apply_instance instance : semiring nat := by apply_instance instance : ordered_semiring nat := by apply_instance /- subtraction -/ @[simp] protected theorem sub_zero (n : ℕ) : n - 0 = n := rfl theorem sub_succ (n m : ℕ) : n - succ m = pred (n - m) := rfl theorem succ_sub_succ (n m : ℕ) : succ n - succ m = n - m := succ_sub_succ_eq_sub n m protected theorem sub_self : ∀ (n : ℕ), n - n = 0 | 0 := by rw nat.sub_zero | (succ n) := by rw [succ_sub_succ, sub_self n] /- TODO(Leo): remove the following ematch annotations as soon as we have arithmetic theory in the smt_stactic -/ @[ematch_lhs] protected theorem add_sub_add_right : ∀ (n k m : ℕ), (n + k) - (m + k) = n - m | n 0 m := by rw [add_zero, add_zero] | n (succ k) m := by rw [add_succ, add_succ, succ_sub_succ, add_sub_add_right n k m] @[ematch_lhs] protected theorem add_sub_add_left (k n m : ℕ) : (k + n) - (k + m) = n - m := by rw [add_comm k n, add_comm k m, nat.add_sub_add_right] @[ematch_lhs] protected theorem add_sub_cancel (n m : ℕ) : n + m - m = n := suffices n + m - (0 + m) = n, from by rwa [zero_add] at this, by rw [nat.add_sub_add_right, nat.sub_zero] @[ematch_lhs] protected theorem add_sub_cancel_left (n m : ℕ) : n + m - n = m := show n + m - (n + 0) = m, from by rw [nat.add_sub_add_left, nat.sub_zero] protected theorem sub_sub : ∀ (n m k : ℕ), n - m - k = n - (m + k) | n m 0 := by rw [add_zero, nat.sub_zero] | n m (succ k) := by rw [add_succ, nat.sub_succ, nat.sub_succ, sub_sub n m k] theorem le_of_le_of_sub_le_sub_right {n m k : ℕ} (h₀ : k ≤ m) (h₁ : n - k ≤ m - k) : n ≤ m := begin revert k m, induction n with n ; intros k m h₀ h₁, { apply zero_le }, { cases k with k, { apply h₁ }, cases m with m, { cases not_succ_le_zero _ h₀ }, { simp [succ_sub_succ] at h₁, apply succ_le_succ, apply ih_1 _ h₁, apply le_of_succ_le_succ h₀ }, } end protected theorem sub_le_sub_right_iff (n m k : ℕ) (h : k ≤ m) : n - k ≤ m - k ↔ n ≤ m := ⟨ le_of_le_of_sub_le_sub_right h , assume h, nat.sub_le_sub_right h k ⟩ theorem sub_self_add (n m : ℕ) : n - (n + m) = 0 := show (n + 0) - (n + m) = 0, from by rw [nat.add_sub_add_left, nat.zero_sub] theorem add_le_to_le_sub (x : ℕ) {y k : ℕ} (h : k ≤ y) : x + k ≤ y ↔ x ≤ y - k := by rw [← nat.add_sub_cancel x k, nat.sub_le_sub_right_iff _ _ _ h, nat.add_sub_cancel] lemma sub_lt_of_pos_le (a b : ℕ) (h₀ : 0 < a) (h₁ : a ≤ b) : b - a < b := begin apply sub_lt _ h₀, apply lt_of_lt_of_le h₀ h₁ end theorem sub_one (n : ℕ) : n - 1 = pred n := rfl theorem succ_sub_one (n : ℕ) : succ n - 1 = n := rfl theorem succ_pred_eq_of_pos : ∀ {n : ℕ}, n > 0 → succ (pred n) = n | 0 h := absurd h (lt_irrefl 0) | (succ k) h := rfl theorem sub_eq_zero_of_le {n m : ℕ} (h : n ≤ m) : n - m = 0 := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, sub_self_add]) protected theorem le_of_sub_eq_zero : ∀{n m : ℕ}, n - m = 0 → n ≤ m | n 0 H := begin rw [nat.sub_zero] at H, simp [H] end | 0 (m+1) H := zero_le _ | (n+1) (m+1) H := add_le_add_right (le_of_sub_eq_zero begin simp [nat.add_sub_add_right] at H, exact H end) _ protected theorem sub_eq_zero_iff_le {n m : ℕ} : n - m = 0 ↔ n ≤ m := ⟨nat.le_of_sub_eq_zero, nat.sub_eq_zero_of_le⟩ theorem add_sub_of_le {n m : ℕ} (h : n ≤ m) : n + (m - n) = m := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, nat.add_sub_cancel_left]) protected theorem sub_add_cancel {n m : ℕ} (h : n ≥ m) : n - m + m = n := by rw [add_comm, add_sub_of_le h] protected theorem add_sub_assoc {m k : ℕ} (h : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) := exists.elim (nat.le.dest h) (assume l, assume hl : k + l = m, by rw [← hl, nat.add_sub_cancel_left, add_comm k, ← add_assoc, nat.add_sub_cancel]) protected lemma sub_eq_iff_eq_add {a b c : ℕ} (ab : b ≤ a) : a - b = c ↔ a = c + b := ⟨assume c_eq, begin rw [c_eq.symm, nat.sub_add_cancel ab] end, assume a_eq, begin rw [a_eq, nat.add_sub_cancel] end⟩ protected lemma lt_of_sub_eq_succ {m n l : ℕ} (H : m - n = nat.succ l) : n < m := lt_of_not_ge (assume (H' : n ≥ m), begin simp [nat.sub_eq_zero_of_le H'] at H, contradiction end) @[simp] lemma zero_min (a : ℕ) : min 0 a = 0 := min_eq_left (zero_le a) @[simp] lemma min_zero (a : ℕ) : min a 0 = 0 := min_eq_right (zero_le a) -- Distribute succ over min theorem min_succ_succ (x y : ℕ) : min (succ x) (succ y) = succ (min x y) := have f : x ≤ y → min (succ x) (succ y) = succ (min x y), from λp, calc min (succ x) (succ y) = succ x : if_pos (succ_le_succ p) ... = succ (min x y) : congr_arg succ (eq.symm (if_pos p)), have g : ¬ (x ≤ y) → min (succ x) (succ y) = succ (min x y), from λp, calc min (succ x) (succ y) = succ y : if_neg (λeq, p (pred_le_pred eq)) ... = succ (min x y) : congr_arg succ (eq.symm (if_neg p)), decidable.by_cases f g theorem sub_eq_sub_min (n m : ℕ) : n - m = n - min n m := if h : n ≥ m then by rewrite [min_eq_right h] else by rewrite [sub_eq_zero_of_le (le_of_not_ge h), min_eq_left (le_of_not_ge h), nat.sub_self] @[simp] theorem sub_add_min_cancel (n m : ℕ) : n - m + min n m = n := by rw [sub_eq_sub_min, nat.sub_add_cancel (min_le_left n m)] /- TODO(Leo): sub + inequalities -/ protected def strong_rec_on {p : nat → Sort u} (n : nat) (h : ∀ n, (∀ m, m < n → p m) → p n) : p n := suffices ∀ n m, m < n → p m, from this (succ n) n (lt_succ_self _), begin intros n, induction n with n ih, {intros m h₁, exact absurd h₁ (not_lt_zero _)}, {intros m h₁, apply or.by_cases (lt_or_eq_of_le (le_of_lt_succ h₁)), {intros, apply ih, assumption}, {intros, subst m, apply h _ ih}} end protected lemma strong_induction_on {p : nat → Prop} (n : nat) (h : ∀ n, (∀ m, m < n → p m) → p n) : p n := nat.strong_rec_on n h protected lemma case_strong_induction_on {p : nat → Prop} (a : nat) (hz : p 0) (hi : ∀ n, (∀ m, m ≤ n → p m) → p (succ n)) : p a := nat.strong_induction_on a $ λ n, match n with | 0 := λ _, hz | (n+1) := λ h₁, hi n (λ m h₂, h₁ _ (lt_succ_of_le h₂)) end /- mod -/ lemma mod_def (x y : nat) : x % y = if 0 < y ∧ y ≤ x then (x - y) % y else x := by have h := mod_def_aux x y; rwa [dif_eq_if] at h @[simp] lemma mod_zero (a : nat) : a % 0 = a := begin rw mod_def, have h : ¬ (0 < 0 ∧ 0 ≤ a), simp [lt_irrefl], simp [if_neg, h] end lemma mod_eq_of_lt {a b : nat} (h : a < b) : a % b = a := begin rw mod_def, have h' : ¬(0 < b ∧ b ≤ a), simp [not_le_of_gt h], simp [if_neg, h'] end @[simp] lemma zero_mod (b : nat) : 0 % b = 0 := begin rw mod_def, have h : ¬(0 < b ∧ b ≤ 0), {intro hn, cases hn with l r, exact absurd (lt_of_lt_of_le l r) (lt_irrefl 0)}, simp [if_neg, h] end lemma mod_eq_sub_mod {a b : nat} (h : a ≥ b) : a % b = (a - b) % b := or.elim (eq_zero_or_pos b) (λb0, by rw [b0, nat.sub_zero]) (λh₂, by rw [mod_def, if_pos (and.intro h₂ h)]) lemma mod_lt (x : nat) {y : nat} (h : y > 0) : x % y < y := begin induction x using nat.case_strong_induction_on with x ih, {rw zero_mod, assumption}, {apply or.elim (decidable.em (succ x < y)), {intro h₁, rwa [mod_eq_of_lt h₁]}, {intro h₁, have h₁ : succ x % y = (succ x - y) % y, {exact mod_eq_sub_mod (le_of_not_gt h₁)}, have this : succ x - y ≤ x, {exact le_of_lt_succ (sub_lt (succ_pos x) h)}, have h₂ : (succ x - y) % y < y, {exact ih _ this}, rwa [← h₁] at h₂}} end @[simp] theorem mod_self (n : nat) : n % n = 0 := by rw [mod_eq_sub_mod (le_refl _), nat.sub_self, zero_mod] @[simp] lemma mod_one (n : ℕ) : n % 1 = 0 := have n % 1 < 1, from (mod_lt n) (succ_pos 0), eq_zero_of_le_zero (le_of_lt_succ this) lemma mod_two_eq_zero_or_one (n : ℕ) : n % 2 = 0 ∨ n % 2 = 1 := match n % 2, @nat.mod_lt n 2 dec_trivial with | 0, _ := or.inl rfl | 1, _ := or.inr rfl | k+2, h := absurd h dec_trivial end /- div & mod -/ lemma div_def (x y : nat) : x / y = if 0 < y ∧ y ≤ x then (x - y) / y + 1 else 0 := by have h := div_def_aux x y; rwa dif_eq_if at h lemma mod_add_div (m k : ℕ) : m % k + k * (m / k) = m := begin apply nat.strong_induction_on m, clear m, intros m IH, cases decidable.em (0 < k ∧ k ≤ m) with h h', -- 0 < k ∧ k ≤ m { have h' : m - k < m, { apply nat.sub_lt _ h.left, apply lt_of_lt_of_le h.left h.right }, rw [div_def, mod_def, if_pos h, if_pos h], simp [left_distrib, IH _ h'], rw [← nat.add_sub_assoc h.right, nat.add_sub_cancel_left] }, -- ¬ (0 < k ∧ k ≤ m) { rw [div_def, mod_def, if_neg h', if_neg h'], simp }, end /- div -/ @[simp] protected lemma div_one (n : ℕ) : n / 1 = n := have n % 1 + 1 * (n / 1) = n, from mod_add_div _ _, by simp [mod_one] at this; assumption @[simp] protected lemma div_zero (n : ℕ) : n / 0 = 0 := begin rw [div_def], simp [lt_irrefl] end @[simp] protected lemma zero_div (b : ℕ) : 0 / b = 0 := eq.trans (div_def 0 b) $ if_neg (and.rec not_le_of_gt) protected lemma div_le_of_le_mul {m n : ℕ} : ∀ {k}, m ≤ k * n → m / k ≤ n | 0 h := by simp [nat.div_zero]; apply zero_le | (succ k) h := suffices succ k * (m / succ k) ≤ succ k * n, from le_of_mul_le_mul_left this (zero_lt_succ _), calc succ k * (m / succ k) ≤ m % succ k + succ k * (m / succ k) : le_add_left _ _ ... = m : by rw mod_add_div ... ≤ succ k * n : h protected lemma div_le_self : ∀ (m n : ℕ), m / n ≤ m | m 0 := by simp [nat.div_zero]; apply zero_le | m (succ n) := have m ≤ succ n * m, from calc m = 1 * m : by simp ... ≤ succ n * m : mul_le_mul_right _ (succ_le_succ (zero_le _)), nat.div_le_of_le_mul this lemma div_eq_sub_div {a b : nat} (h₁ : b > 0) (h₂ : a ≥ b) : a / b = (a - b) / b + 1 := begin rw [div_def a, if_pos], split ; assumption end lemma div_eq_of_lt {a b : ℕ} (h₀ : a < b) : a / b = 0 := begin rw [div_def a, if_neg], intro h₁, apply not_le_of_gt h₀ h₁.right end -- this is a Galois connection -- f x ≤ y ↔ x ≤ g y -- with -- f x = x * k -- g y = y / k theorem le_div_iff_mul_le (x y : ℕ) {k : ℕ} (Hk : k > 0) : x ≤ y / k ↔ x * k ≤ y := begin -- Hk is needed because, despite div being made total, y / 0 := 0 -- x * 0 ≤ y ↔ x ≤ y / 0 -- ↔ 0 ≤ y ↔ x ≤ 0 -- ↔ true ↔ x = 0 -- ↔ x = 0 revert x, apply nat.strong_induction_on y _, clear y, intros y IH x, cases lt_or_ge y k with h h, -- base case: y < k { rw [div_eq_of_lt h], cases x with x, { simp [zero_mul, zero_le] }, { simp [succ_mul, not_succ_le_zero], apply not_le_of_gt, apply lt_of_lt_of_le h, apply le_add_right } }, -- step: k ≤ y { rw [div_eq_sub_div Hk h], cases x with x, { simp [zero_mul, zero_le] }, { have Hlt : y - k < y, { apply sub_lt_of_pos_le ; assumption }, rw [ ← add_one , nat.add_le_add_iff_le_right , IH (y - k) Hlt x , add_one , succ_mul, add_le_to_le_sub _ h ] } } end theorem div_lt_iff_lt_mul (x y : ℕ) {k : ℕ} (Hk : k > 0) : x / k < y ↔ x < y * k := begin simp [lt_iff_not_ge], apply not_iff_not_of_iff, apply le_div_iff_mul_le _ _ Hk end def iterate {A : Type} (op : A → A) : ℕ → A → A | 0 := λ a, a | (succ k) := λ a, op (iterate k a) notation f`^[`n`]` := iterate f n /- successor and predecessor -/ theorem add_one_ne_zero (n : ℕ) : n + 1 ≠ 0 := succ_ne_zero _ theorem eq_zero_or_eq_succ_pred (n : ℕ) : n = 0 ∨ n = succ (pred n) := by cases n; simp theorem exists_eq_succ_of_ne_zero {n : ℕ} (H : n ≠ 0) : ∃k : ℕ, n = succ k := ⟨_, (eq_zero_or_eq_succ_pred _).resolve_left H⟩ theorem succ_inj {n m : ℕ} (H : succ n = succ m) : n = m := nat.succ.inj_arrow H id theorem discriminate {B : Sort u} {n : ℕ} (H1: n = 0 → B) (H2 : ∀m, n = succ m → B) : B := by ginduction n with h; [exact H1 h, exact H2 _ h] theorem one_succ_zero : 1 = succ 0 := rfl theorem two_step_induction {P : ℕ → Sort u} (H1 : P 0) (H2 : P 1) (H3 : ∀ (n : ℕ) (IH1 : P n) (IH2 : P (succ n)), P (succ (succ n))) : Π (a : ℕ), P a | 0 := H1 | 1 := H2 | (succ (succ n)) := H3 _ (two_step_induction _) (two_step_induction _) theorem sub_induction {P : ℕ → ℕ → Sort u} (H1 : ∀m, P 0 m) (H2 : ∀n, P (succ n) 0) (H3 : ∀n m, P n m → P (succ n) (succ m)) : Π (n m : ℕ), P n m | 0 m := H1 _ | (succ n) 0 := H2 _ | (succ n) (succ m) := H3 _ _ (sub_induction n m) /- addition -/ theorem succ_add_eq_succ_add (n m : ℕ) : succ n + m = n + succ m := by simp [succ_add, add_succ] theorem one_add (n : ℕ) : 1 + n = succ n := by simp protected theorem add_right_comm : ∀ (n m k : ℕ), n + m + k = n + k + m := right_comm nat.add nat.add_comm nat.add_assoc theorem eq_zero_of_add_eq_zero {n m : ℕ} (H : n + m = 0) : n = 0 ∧ m = 0 := ⟨nat.eq_zero_of_add_eq_zero_right H, nat.eq_zero_of_add_eq_zero_left H⟩ theorem eq_zero_of_mul_eq_zero : ∀ {n m : ℕ}, n * m = 0 → n = 0 ∨ m = 0 | 0 m := λ h, or.inl rfl | (succ n) m := begin rw succ_mul, intro h, exact or.inr (eq_zero_of_add_eq_zero_left h) end /- properties of inequality -/ theorem le_succ_of_pred_le {n m : ℕ} : pred n ≤ m → n ≤ succ m := nat.cases_on n less_than_or_equal.step (λ a, succ_le_succ) theorem le_lt_antisymm {n m : ℕ} (h₁ : n ≤ m) (h₂ : m < n) : false := nat.lt_irrefl n (nat.lt_of_le_of_lt h₁ h₂) theorem lt_le_antisymm {n m : ℕ} (h₁ : n < m) (h₂ : m ≤ n) : false := le_lt_antisymm h₂ h₁ protected theorem nat.lt_asymm {n m : ℕ} (h₁ : n < m) : ¬ m < n := le_lt_antisymm (nat.le_of_lt h₁) protected def lt_ge_by_cases {a b : ℕ} {C : Sort u} (h₁ : a < b → C) (h₂ : a ≥ b → C) : C := decidable.by_cases h₁ (λ h, h₂ (or.elim (nat.lt_or_ge a b) (λ a, absurd a h) (λ a, a))) protected def lt_by_cases {a b : ℕ} {C : Sort u} (h₁ : a < b → C) (h₂ : a = b → C) (h₃ : b < a → C) : C := nat.lt_ge_by_cases h₁ (λ h₁, nat.lt_ge_by_cases h₃ (λ h, h₂ (nat.le_antisymm h h₁))) protected theorem lt_trichotomy (a b : ℕ) : a < b ∨ a = b ∨ b < a := nat.lt_by_cases (λ h, or.inl h) (λ h, or.inr (or.inl h)) (λ h, or.inr (or.inr h)) protected theorem eq_or_lt_of_not_lt {a b : ℕ} (hnlt : ¬ a < b) : a = b ∨ b < a := (nat.lt_trichotomy a b).resolve_left hnlt theorem lt_succ_of_lt {a b : nat} (h : a < b) : a < succ b := le_succ_of_le h def one_pos := nat.zero_lt_one theorem mul_self_le_mul_self {n m : ℕ} (h : n ≤ m) : n * n ≤ m * m := mul_le_mul h h (zero_le _) (zero_le _) theorem mul_self_lt_mul_self : Π {n m : ℕ}, n < m → n * n < m * m | 0 m h := mul_pos h h | (succ n) m h := mul_lt_mul h (le_of_lt h) (succ_pos _) (zero_le _) theorem mul_self_le_mul_self_iff {n m : ℕ} : n ≤ m ↔ n * n ≤ m * m := ⟨mul_self_le_mul_self, λh, decidable.by_contradiction $ λhn, not_lt_of_ge h $ mul_self_lt_mul_self $ lt_of_not_ge hn⟩ theorem mul_self_lt_mul_self_iff {n m : ℕ} : n < m ↔ n * n < m * m := iff.trans (lt_iff_not_ge _ _) $ iff.trans (not_iff_not_of_iff mul_self_le_mul_self_iff) $ iff.symm (lt_iff_not_ge _ _) theorem le_mul_self : Π (n : ℕ), n ≤ n * n | 0 := le_refl _ | (n+1) := let t := mul_le_mul_left (n+1) (succ_pos n) in by simp at t; exact t /- subtraction -/ protected theorem sub_le_sub_left {n m : ℕ} (k) (h : n ≤ m) : k - m ≤ k - n := by induction h; [refl, exact le_trans (pred_le _) ih_1] theorem succ_sub_sub_succ (n m k : ℕ) : succ n - m - succ k = n - m - k := by rw [nat.sub_sub, nat.sub_sub, add_succ, succ_sub_succ] protected theorem sub.right_comm (m n k : ℕ) : m - n - k = m - k - n := by rw [nat.sub_sub, nat.sub_sub, add_comm] theorem mul_pred_left : ∀ (n m : ℕ), pred n * m = n * m - m | 0 m := by simp [nat.zero_sub, pred_zero, zero_mul] | (succ n) m := by rw [pred_succ, succ_mul, nat.add_sub_cancel] theorem mul_pred_right (n m : ℕ) : n * pred m = n * m - n := by rw [mul_comm, mul_pred_left, mul_comm] protected theorem mul_sub_right_distrib : ∀ (n m k : ℕ), (n - m) * k = n * k - m * k | n 0 k := by simp [nat.sub_zero] | n (succ m) k := by rw [nat.sub_succ, mul_pred_left, mul_sub_right_distrib, succ_mul, nat.sub_sub] protected theorem mul_sub_left_distrib (n m k : ℕ) : n * (m - k) = n * m - n * k := by rw [mul_comm, nat.mul_sub_right_distrib, mul_comm m n, mul_comm n k] protected theorem mul_self_sub_mul_self_eq (a b : nat) : a * a - b * b = (a + b) * (a - b) := by rw [nat.mul_sub_left_distrib, right_distrib, right_distrib, mul_comm b a, add_comm (a*a) (a*b), nat.add_sub_add_left] theorem succ_mul_succ_eq (a b : nat) : succ a * succ b = a*b + a + b + 1 := begin rw [← add_one, ← add_one], simp [right_distrib, left_distrib] end theorem succ_sub {m n : ℕ} (h : m ≥ n) : succ m - n = succ (m - n) := exists.elim (nat.le.dest h) (assume k, assume hk : n + k = m, by rw [← hk, nat.add_sub_cancel_left, ← add_succ, nat.add_sub_cancel_left]) protected theorem sub_pos_of_lt {m n : ℕ} (h : m < n) : n - m > 0 := have 0 + m < n - m + m, begin rw [zero_add, nat.sub_add_cancel (le_of_lt h)], exact h end, lt_of_add_lt_add_right this protected theorem sub_sub_self {n m : ℕ} (h : m ≤ n) : n - (n - m) = m := (nat.sub_eq_iff_eq_add (nat.sub_le _ _)).2 (eq.symm (add_sub_of_le h)) protected theorem sub_add_comm {n m k : ℕ} (h : k ≤ n) : n + m - k = n - k + m := (nat.sub_eq_iff_eq_add (nat.le_trans h (nat.le_add_right _ _))).2 (by rwa [nat.add_right_comm, nat.sub_add_cancel]) theorem sub_one_sub_lt {n i} (h : i < n) : n - 1 - i < n := begin rw nat.sub_sub, apply nat.sub_lt, apply lt_of_lt_of_le (nat.zero_lt_succ _) h, rw add_comm, apply nat.zero_lt_succ end theorem pred_inj : ∀ {a b : nat}, a > 0 → b > 0 → nat.pred a = nat.pred b → a = b | (succ a) (succ b) ha hb h := have a = b, from h, by rw this | (succ a) 0 ha hb h := absurd hb (lt_irrefl _) | 0 (succ b) ha hb h := absurd ha (lt_irrefl _) | 0 0 ha hb h := rfl /- find -/ section find parameter {p : ℕ → Prop} private def lbp (m n : ℕ) : Prop := m = n + 1 ∧ ∀ k ≤ n, ¬p k parameters [decidable_pred p] (H : ∃n, p n) private def wf_lbp : well_founded lbp := ⟨let ⟨n, pn⟩ := H in suffices ∀m k, n ≤ k + m → acc lbp k, from λa, this _ _ (nat.le_add_left _ _), λm, nat.rec_on m (λk kn, ⟨_, λy r, match y, r with ._, ⟨rfl, a⟩ := absurd pn (a _ kn) end⟩) (λm IH k kn, ⟨_, λy r, match y, r with ._, ⟨rfl, a⟩ := IH _ (by rw nat.add_right_comm; exact kn) end⟩)⟩ protected def find_x : {n // p n ∧ ∀m < n, ¬p m} := @well_founded.fix _ (λk, (∀n < k, ¬p n) → {n // p n ∧ ∀m < n, ¬p m}) lbp wf_lbp (λm IH al, if pm : p m then ⟨m, pm, al⟩ else have ∀ n ≤ m, ¬p n, from λn h, or.elim (lt_or_eq_of_le h) (al n) (λe, by rw e; exact pm), IH _ ⟨rfl, this⟩ (λn h, this n $ nat.le_of_succ_le_succ h)) 0 (λn h, absurd h (nat.not_lt_zero _)) protected def find : ℕ := nat.find_x.1 protected theorem find_spec : p nat.find := nat.find_x.2.left protected theorem find_min : ∀ {m : ℕ}, m < nat.find → ¬p m := nat.find_x.2.right protected theorem find_min' {m : ℕ} (h : p m) : nat.find ≤ m := le_of_not_gt (λ l, find_min l h) end find /- mod -/ theorem mod_le (x y : ℕ) : x % y ≤ x := or.elim (lt_or_ge x y) (λxlty, by rw mod_eq_of_lt xlty; refl) (λylex, or.elim (eq_zero_or_pos y) (λy0, by rw [y0, mod_zero]; refl) (λypos, le_trans (le_of_lt (mod_lt _ ypos)) ylex)) @[simp] theorem add_mod_right (x z : ℕ) : (x + z) % z = x % z := by rw [mod_eq_sub_mod (nat.le_add_left _ _), nat.add_sub_cancel] @[simp] theorem add_mod_left (x z : ℕ) : (x + z) % x = z % x := by rw [add_comm, add_mod_right] @[simp] theorem add_mul_mod_self_left (x y z : ℕ) : (x + y * z) % y = x % y := by {induction z with z ih, simp, rw[mul_succ, ← add_assoc, add_mod_right, ih]} @[simp] theorem add_mul_mod_self_right (x y z : ℕ) : (x + y * z) % z = x % z := by rw [mul_comm, add_mul_mod_self_left] @[simp] theorem mul_mod_right (m n : ℕ) : (m * n) % m = 0 := by rw [← zero_add (m*n), add_mul_mod_self_left, zero_mod] @[simp] theorem mul_mod_left (m n : ℕ) : (m * n) % n = 0 := by rw [mul_comm, mul_mod_right] theorem mul_mod_mul_left (z x y : ℕ) : (z * x) % (z * y) = z * (x % y) := if y0 : y = 0 then by rw [y0, mul_zero, mod_zero, mod_zero] else if z0 : z = 0 then by rw [z0, zero_mul, zero_mul, zero_mul, mod_zero] else x.strong_induction_on $ λn IH, have y0 : y > 0, from nat.pos_of_ne_zero y0, have z0 : z > 0, from nat.pos_of_ne_zero z0, or.elim (le_or_gt y n) (λyn, by rw [ mod_eq_sub_mod yn, mod_eq_sub_mod (mul_le_mul_left z yn), ← nat.mul_sub_left_distrib]; exact IH _ (sub_lt (lt_of_lt_of_le y0 yn) y0)) (λyn, by rw [mod_eq_of_lt yn, mod_eq_of_lt (mul_lt_mul_of_pos_left yn z0)]) theorem mul_mod_mul_right (z x y : ℕ) : (x * z) % (y * z) = (x % y) * z := by rw [mul_comm x z, mul_comm y z, mul_comm (x % y) z]; apply mul_mod_mul_left theorem cond_to_bool_mod_two (x : ℕ) [d : decidable (x % 2 = 1)] : cond (@to_bool (x % 2 = 1) d) 1 0 = x % 2 := begin cases d with h h ; unfold decidable.to_bool cond, { cases mod_two_eq_zero_or_one x with h' h', rw h', cases h h' }, { rw h }, end theorem sub_mul_mod (x k n : ℕ) (h₁ : n*k ≤ x) : (x - n*k) % n = x % n := begin induction k with k, { simp }, { have h₂ : n * k ≤ x, { rw [mul_succ] at h₁, apply nat.le_trans _ h₁, apply le_add_right _ n }, have h₄ : x - n * k ≥ n, { apply @nat.le_of_add_le_add_right (n*k), rw [nat.sub_add_cancel h₂], simp [mul_succ] at h₁, simp [h₁] }, rw [mul_succ, ← nat.sub_sub, ← mod_eq_sub_mod h₄, ih_1 h₂] } end /- div -/ theorem sub_mul_div (x n p : ℕ) (h₁ : n*p ≤ x) : (x - n*p) / n = x / n - p := begin cases eq_zero_or_pos n with h₀ h₀, { rw [h₀, nat.div_zero, nat.div_zero, nat.zero_sub] }, { induction p with p, { simp }, { have h₂ : n*p ≤ x, { transitivity, { apply nat.mul_le_mul_left, apply le_succ }, { apply h₁ } }, have h₃ : x - n * p ≥ n, { apply le_of_add_le_add_right, rw [nat.sub_add_cancel h₂, add_comm], rw [mul_succ] at h₁, apply h₁ }, rw [sub_succ, ← ih_1 h₂], rw [@div_eq_sub_div (x - n*p) _ h₀ h₃], simp [add_one, pred_succ, mul_succ, nat.sub_sub] } } end theorem div_mul_le_self : ∀ (m n : ℕ), m / n * n ≤ m | m 0 := by simp; apply zero_le | m (succ n) := (le_div_iff_mul_le _ _ (nat.succ_pos _)).1 (le_refl _) @[simp] theorem add_div_right (x : ℕ) {z : ℕ} (H : z > 0) : (x + z) / z = succ (x / z) := by rw [div_eq_sub_div H (nat.le_add_left _ _), nat.add_sub_cancel] @[simp] theorem add_div_left (x : ℕ) {z : ℕ} (H : z > 0) : (z + x) / z = succ (x / z) := by rw [add_comm, add_div_right x H] @[simp] theorem mul_div_right (n : ℕ) {m : ℕ} (H : m > 0) : m * n / m = n := by {induction n; simp [*, mul_succ, -mul_comm]} @[simp] theorem mul_div_left (m : ℕ) {n : ℕ} (H : n > 0) : m * n / n = m := by rw [mul_comm, mul_div_right _ H] protected theorem div_self {n : ℕ} (H : n > 0) : n / n = 1 := let t := add_div_right 0 H in by rwa [zero_add, nat.zero_div] at t theorem add_mul_div_left (x z : ℕ) {y : ℕ} (H : y > 0) : (x + y * z) / y = x / y + z := by {induction z with z ih, simp, rw [mul_succ, ← add_assoc, add_div_right _ H, ih]} theorem add_mul_div_right (x y : ℕ) {z : ℕ} (H : z > 0) : (x + y * z) / z = x / z + y := by rw [mul_comm, add_mul_div_left _ _ H] protected theorem mul_div_cancel (m : ℕ) {n : ℕ} (H : n > 0) : m * n / n = m := let t := add_mul_div_right 0 m H in by rwa [zero_add, nat.zero_div, zero_add] at t protected theorem mul_div_cancel_left (m : ℕ) {n : ℕ} (H : n > 0) : n * m / n = m := by rw [mul_comm, nat.mul_div_cancel _ H] protected theorem div_eq_of_eq_mul_left {m n k : ℕ} (H1 : n > 0) (H2 : m = k * n) : m / n = k := by rw [H2, nat.mul_div_cancel _ H1] protected theorem div_eq_of_eq_mul_right {m n k : ℕ} (H1 : n > 0) (H2 : m = n * k) : m / n = k := by rw [H2, nat.mul_div_cancel_left _ H1] protected theorem div_eq_of_lt_le {m n k : ℕ} (lo : k * n ≤ m) (hi : m < succ k * n) : m / n = k := have npos : n > 0, from (eq_zero_or_pos _).resolve_left $ λ hn, by rw [hn, mul_zero] at hi lo; exact absurd lo (not_le_of_gt hi), le_antisymm (le_of_lt_succ ((nat.div_lt_iff_lt_mul _ _ npos).2 hi)) ((nat.le_div_iff_mul_le _ _ npos).2 lo) theorem mul_sub_div (x n p : ℕ) (h₁ : x < n*p) : (n * p - succ x) / n = p - succ (x / n) := begin have npos : n > 0 := (eq_zero_or_pos _).resolve_left (λ n0, by rw [n0, zero_mul] at h₁; exact not_lt_zero _ h₁), apply nat.div_eq_of_lt_le, { rw [nat.mul_sub_right_distrib, mul_comm], apply nat.sub_le_sub_left, exact (div_lt_iff_lt_mul _ _ npos).1 (lt_succ_self _) }, { change succ (pred (n * p - x)) ≤ (succ (pred (p - x / n))) * n, rw [succ_pred_eq_of_pos (nat.sub_pos_of_lt h₁), succ_pred_eq_of_pos (nat.sub_pos_of_lt _)], { rw [nat.mul_sub_right_distrib, mul_comm], apply nat.sub_le_sub_left, apply div_mul_le_self }, { apply (div_lt_iff_lt_mul _ _ npos).2, rwa mul_comm } } end protected theorem div_div_eq_div_mul (m n k : ℕ) : m / n / k = m / (n * k) := begin cases eq_zero_or_pos k with k0 kpos, {rw [k0, mul_zero, nat.div_zero, nat.div_zero]}, cases eq_zero_or_pos n with n0 npos, {rw [n0, zero_mul, nat.div_zero, nat.zero_div]}, apply le_antisymm, { apply (le_div_iff_mul_le _ _ (mul_pos npos kpos)).2, rw [mul_comm n k, ← mul_assoc], apply (le_div_iff_mul_le _ _ npos).1, apply (le_div_iff_mul_le _ _ kpos).1, refl }, { apply (le_div_iff_mul_le _ _ kpos).2, apply (le_div_iff_mul_le _ _ npos).2, rw [mul_assoc, mul_comm n k], apply (le_div_iff_mul_le _ _ (mul_pos kpos npos)).1, refl } end protected theorem mul_div_mul {m : ℕ} (n k : ℕ) (H : m > 0) : m * n / (m * k) = n / k := by rw [← nat.div_div_eq_div_mul, nat.mul_div_cancel_left _ H] /- dvd -/ protected theorem dvd_add_iff_right {k m n : ℕ} (h : k ∣ m) : k ∣ n ↔ k ∣ m + n := ⟨dvd_add h, dvd.elim h $ λd hd, match m, hd with | ._, rfl := λh₂, dvd.elim h₂ $ λe he, ⟨e - d, by rw [nat.mul_sub_left_distrib, ← he, nat.add_sub_cancel_left]⟩ end⟩ protected theorem dvd_add_iff_left {k m n : ℕ} (h : k ∣ n) : k ∣ m ↔ k ∣ m + n := by rw add_comm; exact nat.dvd_add_iff_right h theorem dvd_sub {k m n : ℕ} (H : n ≤ m) (h₁ : k ∣ m) (h₂ : k ∣ n) : k ∣ m - n := (nat.dvd_add_iff_left h₂).2 $ by rw nat.sub_add_cancel H; exact h₁ theorem dvd_mod_iff {k m n : ℕ} (h : k ∣ n) : k ∣ m % n ↔ k ∣ m := let t := @nat.dvd_add_iff_left _ (m % n) _ (dvd_trans h (dvd_mul_right n (m / n))) in by rwa mod_add_div at t theorem le_of_dvd {m n : ℕ} (h : n > 0) : m ∣ n → m ≤ n := λ⟨k, e⟩, by { revert h, rw e, refine k.cases_on _ _, exact λhn, absurd hn (lt_irrefl _), exact λk _, let t := mul_le_mul_left m (succ_pos k) in by rwa mul_one at t } theorem dvd_antisymm : Π {m n : ℕ}, m ∣ n → n ∣ m → m = n | m 0 h₁ h₂ := eq_zero_of_zero_dvd h₂ | 0 n h₁ h₂ := (eq_zero_of_zero_dvd h₁).symm | (succ m) (succ n) h₁ h₂ := le_antisymm (le_of_dvd (succ_pos _) h₁) (le_of_dvd (succ_pos _) h₂) theorem pos_of_dvd_of_pos {m n : ℕ} (H1 : m ∣ n) (H2 : n > 0) : m > 0 := nat.pos_of_ne_zero $ λm0, by rw m0 at H1; rw eq_zero_of_zero_dvd H1 at H2; exact lt_irrefl _ H2 theorem eq_one_of_dvd_one {n : ℕ} (H : n ∣ 1) : n = 1 := le_antisymm (le_of_dvd dec_trivial H) (pos_of_dvd_of_pos H dec_trivial) theorem dvd_of_mod_eq_zero {m n : ℕ} (H : n % m = 0) : m ∣ n := dvd.intro (n / m) $ let t := mod_add_div n m in by simp [H] at t; exact t theorem mod_eq_zero_of_dvd {m n : ℕ} (H : m ∣ n) : n % m = 0 := dvd.elim H (λ z H1, by rw [H1, mul_mod_right]) theorem dvd_iff_mod_eq_zero (m n : ℕ) : m ∣ n ↔ n % m = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ instance decidable_dvd : @decidable_rel ℕ (∣) := λm n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem mul_div_cancel' {m n : ℕ} (H : n ∣ m) : n * (m / n) = m := let t := mod_add_div m n in by rwa [mod_eq_zero_of_dvd H, zero_add] at t protected theorem div_mul_cancel {m n : ℕ} (H : n ∣ m) : m / n * n = m := by rw [mul_comm, nat.mul_div_cancel' H] protected theorem mul_div_assoc (m : ℕ) {n k : ℕ} (H : k ∣ n) : m * n / k = m * (n / k) := or.elim (eq_zero_or_pos k) (λh, by rw [h, nat.div_zero, nat.div_zero, mul_zero]) (λh, have m * n / k = m * (n / k * k) / k, by rw nat.div_mul_cancel H, by rw[this, ← mul_assoc, nat.mul_div_cancel _ h]) theorem dvd_of_mul_dvd_mul_left {m n k : ℕ} (kpos : k > 0) (H : k * m ∣ k * n) : m ∣ n := dvd.elim H (λl H1, by rw mul_assoc at H1; exact ⟨_, eq_of_mul_eq_mul_left kpos H1⟩) theorem dvd_of_mul_dvd_mul_right {m n k : ℕ} (kpos : k > 0) (H : m * k ∣ n * k) : m ∣ n := by rw [mul_comm m k, mul_comm n k] at H; exact dvd_of_mul_dvd_mul_left kpos H /- pow -/ @[simp] theorem pow_one (b : ℕ) : b^1 = b := by simp [pow_succ] theorem pow_le_pow_of_le_left {x y : ℕ} (H : x ≤ y) : ∀ i, x^i ≤ y^i | 0 := le_refl _ | (succ i) := mul_le_mul (pow_le_pow_of_le_left i) H (zero_le _) (zero_le _) theorem pow_le_pow_of_le_right {x : ℕ} (H : x > 0) {i} : ∀ {j}, i ≤ j → x^i ≤ x^j | 0 h := by rw eq_zero_of_le_zero h; apply le_refl | (succ j) h := (lt_or_eq_of_le h).elim (λhl, by rw [pow_succ, ← mul_one (x^i)]; exact mul_le_mul (pow_le_pow_of_le_right $ le_of_lt_succ hl) H (zero_le _) (zero_le _)) (λe, by rw e; refl) theorem pos_pow_of_pos {b : ℕ} (n : ℕ) (h : 0 < b) : 0 < b^n := pow_le_pow_of_le_right h (zero_le _) theorem zero_pow {n : ℕ} (h : 0 < n) : 0^n = 0 := by rw [← succ_pred_eq_of_pos h, pow_succ, mul_zero] theorem pow_lt_pow_of_lt_left {x y : ℕ} (H : x < y) {i} (h : i > 0) : x^i < y^i := begin cases i with i, { exact absurd h (not_lt_zero _) }, rw [pow_succ, pow_succ], exact mul_lt_mul' (pow_le_pow_of_le_left (le_of_lt H) _) H (zero_le _) (pos_pow_of_pos _ $ lt_of_le_of_lt (zero_le _) H) end theorem pow_lt_pow_of_lt_right {x : ℕ} (H : x > 1) {i j} (h : i < j) : x^i < x^j := begin have xpos := lt_of_succ_lt H, refine lt_of_lt_of_le _ (pow_le_pow_of_le_right xpos h), rw [← mul_one (x^i), pow_succ], exact nat.mul_lt_mul_of_pos_left H (pos_pow_of_pos _ xpos) end /- mod / div / pow -/ theorem mod_pow_succ {b : ℕ} (b_pos : b > 0) (w m : ℕ) : m % (b^succ w) = b * (m/b % b^w) + m % b := begin apply nat.strong_induction_on m, clear m, intros p IH, cases lt_or_ge p (b^succ w) with h₁ h₁, -- base case: p < b^succ w { have h₂ : p / b < b^w, { rw [div_lt_iff_lt_mul p _ b_pos], simp [pow_succ] at h₁, simp [h₁] }, rw [mod_eq_of_lt h₁, mod_eq_of_lt h₂], simp [mod_add_div] }, -- step: p ≥ b^succ w { -- Generate condiition for induction principal have h₂ : p - b^succ w < p, { apply sub_lt_of_pos_le _ _ (pos_pow_of_pos _ b_pos) h₁ }, -- Apply induction rw [mod_eq_sub_mod h₁, IH _ h₂], -- Normalize goal and h1 simp [pow_succ], simp [ge, pow_succ] at h₁, -- Pull subtraction outside mod and div rw [sub_mul_mod _ _ _ h₁, sub_mul_div _ _ _ h₁], -- Cancel subtraction inside mod b^w have p_b_ge : b^w ≤ p / b, { rw [le_div_iff_mul_le _ _ b_pos], simp [h₁] }, rw [eq.symm (mod_eq_sub_mod p_b_ge)] } end end nat
6ecbbebbff9519ed8bbc9e84954716e1c51ab0ba
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/ring_theory/integral_domain.lean
b38239823e1c8416f2e6ab8054c02e08ad511d02
[ "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
7,317
lean
/- Copyright (c) 2020 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Chris Hughes -/ import data.fintype.card import data.polynomial.ring_division import group_theory.specific_groups.cyclic import algebra.geom_sum /-! # Integral domains Assorted theorems about integral domains. ## Main theorems * `is_cyclic_of_subgroup_is_domain` : A finite subgroup of the units of an integral domain is cyclic. * `field_of_is_domain` : A finite integral domain is a field. ## Tags integral domain, finite integral domain, finite field -/ section open finset polynomial function open_locale big_operators nat variables {R : Type*} {G : Type*} section ring variables [ring R] [is_domain R] [fintype R] lemma mul_right_bijective₀ (a : R) (ha : a ≠ 0) : bijective (λ b, a * b) := fintype.injective_iff_bijective.1 $ mul_right_injective₀ ha lemma mul_left_bijective₀ (a : R) (ha : a ≠ 0) : bijective (λ b, b * a) := fintype.injective_iff_bijective.1 $ mul_left_injective₀ ha /-- Every finite domain is a division ring. TODO: Prove Wedderburn's little theorem, which shows a finite domain is in fact commutative, hence a field. -/ def division_ring_of_is_domain (R : Type*) [ring R] [is_domain R] [decidable_eq R] [fintype R] : division_ring R := { inv := λ a, if h : a = 0 then 0 else fintype.bij_inv (mul_right_bijective₀ a h) 1, mul_inv_cancel := λ a ha, show a * dite _ _ _ = _, by { rw dif_neg ha, exact fintype.right_inverse_bij_inv _ _ }, inv_zero := dif_pos rfl, ..show nontrivial R, by apply_instance, ..‹ring R› } end ring variables [comm_ring R] [is_domain R] [group G] [fintype G] lemma card_nth_roots_subgroup_units (f : G →* R) (hf : injective f) {n : ℕ} (hn : 0 < n) (g₀ : G) : ({g ∈ univ | g ^ n = g₀} : finset G).card ≤ (nth_roots n (f g₀)).card := begin haveI : decidable_eq R := classical.dec_eq _, refine le_trans _ (nth_roots n (f g₀)).to_finset_card_le, apply card_le_card_of_inj_on f, { intros g hg, rw [sep_def, mem_filter] at hg, rw [multiset.mem_to_finset, mem_nth_roots hn, ← f.map_pow, hg.2] }, { intros, apply hf, assumption } end /-- A finite subgroup of the unit group of an integral domain is cyclic. -/ lemma is_cyclic_of_subgroup_is_domain (f : G →* R) (hf : injective f) : is_cyclic G := begin classical, apply is_cyclic_of_card_pow_eq_one_le, intros n hn, convert (le_trans (card_nth_roots_subgroup_units f hf hn 1) (card_nth_roots n (f 1))) end /-- The unit group of a finite integral domain is cyclic. To support `units ℤ` and other infinite monoids with finite groups of units, this requires only `fintype (units R)` rather than deducing it from `fintype R`. -/ instance [fintype (units R)] : is_cyclic (units R) := is_cyclic_of_subgroup_is_domain (units.coe_hom R) $ units.ext /-- Every finite integral domain is a field. -/ def field_of_is_domain [decidable_eq R] [fintype R] : field R := { ..division_ring_of_is_domain R, ..‹comm_ring R› } section variables (S : subgroup (units R)) [fintype S] /-- A finite subgroup of the units of an integral domain is cyclic. -/ instance subgroup_units_cyclic : is_cyclic S := begin refine is_cyclic_of_subgroup_is_domain ⟨(coe : S → R), _, _⟩ (units.ext.comp subtype.val_injective), { simp }, { intros, simp }, end end lemma card_fiber_eq_of_mem_range {H : Type*} [group H] [decidable_eq H] (f : G →* H) {x y : H} (hx : x ∈ set.range f) (hy : y ∈ set.range f) : (univ.filter $ λ g, f g = x).card = (univ.filter $ λ g, f g = y).card := begin rcases hx with ⟨x, rfl⟩, rcases hy with ⟨y, rfl⟩, refine card_congr (λ g _, g * x⁻¹ * y) _ _ (λ g hg, ⟨g * y⁻¹ * x, _⟩), { simp only [mem_filter, one_mul, monoid_hom.map_mul, mem_univ, mul_right_inv, eq_self_iff_true, monoid_hom.map_mul_inv, and_self, forall_true_iff] {contextual := tt} }, { simp only [mul_left_inj, imp_self, forall_2_true_iff], }, { simp only [true_and, mem_filter, mem_univ] at hg, simp only [hg, mem_filter, one_mul, monoid_hom.map_mul, mem_univ, mul_right_inv, eq_self_iff_true, exists_prop_of_true, monoid_hom.map_mul_inv, and_self, mul_inv_cancel_right, inv_mul_cancel_right], } end /-- In an integral domain, a sum indexed by a nontrivial homomorphism from a finite group is zero. -/ lemma sum_hom_units_eq_zero (f : G →* R) (hf : f ≠ 1) : ∑ g : G, f g = 0 := begin classical, obtain ⟨x, hx⟩ : ∃ x : monoid_hom.range f.to_hom_units, ∀ y : monoid_hom.range f.to_hom_units, y ∈ submonoid.powers x, from is_cyclic.exists_monoid_generator, have hx1 : x ≠ 1, { rintro rfl, apply hf, ext g, rw [monoid_hom.one_apply], cases hx ⟨f.to_hom_units g, g, rfl⟩ with n hn, rwa [subtype.ext_iff, units.ext_iff, subtype.coe_mk, monoid_hom.coe_to_hom_units, one_pow, eq_comm] at hn, }, replace hx1 : (x : R) - 1 ≠ 0, from λ h, hx1 (subtype.eq (units.ext (sub_eq_zero.1 h))), let c := (univ.filter (λ g, f.to_hom_units g = 1)).card, calc ∑ g : G, f g = ∑ g : G, f.to_hom_units g : rfl ... = ∑ u : units R in univ.image f.to_hom_units, (univ.filter (λ g, f.to_hom_units g = u)).card • u : sum_comp (coe : units R → R) f.to_hom_units ... = ∑ u : units R in univ.image f.to_hom_units, c • u : sum_congr rfl (λ u hu, congr_arg2 _ _ rfl) -- remaining goal 1, proven below ... = ∑ b : monoid_hom.range f.to_hom_units, c • ↑b : finset.sum_subtype _ (by simp ) _ ... = c • ∑ b : monoid_hom.range f.to_hom_units, (b : R) : smul_sum.symm ... = c • 0 : congr_arg2 _ rfl _ -- remaining goal 2, proven below ... = 0 : smul_zero _, { -- remaining goal 1 show (univ.filter (λ (g : G), f.to_hom_units g = u)).card = c, apply card_fiber_eq_of_mem_range f.to_hom_units, { simpa only [mem_image, mem_univ, exists_prop_of_true, set.mem_range] using hu, }, { exact ⟨1, f.to_hom_units.map_one⟩ } }, -- remaining goal 2 show ∑ b : monoid_hom.range f.to_hom_units, (b : R) = 0, calc ∑ b : monoid_hom.range f.to_hom_units, (b : R) = ∑ n in range (order_of x), x ^ n : eq.symm $ sum_bij (λ n _, x ^ n) (by simp only [mem_univ, forall_true_iff]) (by simp only [implies_true_iff, eq_self_iff_true, subgroup.coe_pow, units.coe_pow, coe_coe]) (λ m n hm hn, pow_injective_of_lt_order_of _ (by simpa only [mem_range] using hm) (by simpa only [mem_range] using hn)) (λ b hb, let ⟨n, hn⟩ := hx b in ⟨n % order_of x, mem_range.2 (nat.mod_lt _ (order_of_pos _)), by rw [← pow_eq_mod_order_of, hn]⟩) ... = 0 : _, rw [← mul_left_inj' hx1, zero_mul, ← geom_sum, geom_sum_mul, coe_coe], norm_cast, simp [pow_order_of_eq_one], end /-- In an integral domain, a sum indexed by a homomorphism from a finite group is zero, unless the homomorphism is trivial, in which case the sum is equal to the cardinality of the group. -/ lemma sum_hom_units (f : G →* R) [decidable (f = 1)] : ∑ g : G, f g = if f = 1 then fintype.card G else 0 := begin split_ifs with h h, { simp [h, card_univ] }, { exact sum_hom_units_eq_zero f h } end end
77d2fe80f0825bd04528d3f71f37a0856dd0487a
f3be49eddff7edf577d3d3666e314d995f7a6357
/TBA/Exercises/Exercise6.lean
06d43cec920db1f1f1aa670eae06a10aa87b7c40
[]
no_license
IPDSnelting/tba-2021
8b930bcd2f4aae44a2ddc86e72b77f84e6d46e82
b6390e55b768423d3266969e81d19290129c5914
refs/heads/master
1,686,754,693,583
1,625,135,602,000
1,625,136,365,000
355,124,341
50
7
null
1,625,133,762,000
1,617,699,824,000
Lean
UTF-8
Lean
false
false
9,450
lean
/- Even more induction! -/ variable (r : α → α → Prop) -- The reflexive transitive closure of `r` as an inductive predicate inductive RTC : α → α → Prop where -- Notice how declaring `r` as a `variable` instead of as a parameter instead of declaring it -- directly as a parameter of `RTC` means we don't have to write `RTC r a a` inside the -- declaration of `RTC`. This also works with recursive `def`s! | refl : RTC a a | trans : r a b → RTC b c → RTC a c -- We have arbitrarily chosen a "left-biased" definition of `RTC.trans`, but can easily show the -- mirror version by induction on the predicate theorem RTC.trans' : RTC r a b → r b c → RTC r a c := by intros hab hbc induction hab with | refl => _ -- `a/b/c` in the constructor `RTC.trans` are marked as *implicit* because we didn't specify them -- explicitly. -- Just like in other contexts, we can use `@` to specify/match implicit parameters in `induction`. | @trans a a' b haa' ha'b ih => open Nat -- By the way, we can leave out `:= fun p1 ... => match p1, ... with` at `def` def double : Nat → Nat | zero => 0 | succ n => succ (succ (double n)) theorem double.inj : double n = double m → n = m := by intro h -- Try to finish this proof. You might find that the inductive case is impossible to solve! -- Do you see a different approach? If not, read on! induction n with -- The issue with the above approach is that our inductive hypothesis is not sufficiently general! -- When we begin induction, we have already fixed (introduced) a particular `m`, but for the inductive -- step we need the inductive hypothesis for a *different* m. -- We could avoid this by carefully introducing `m` (and `h`, which depends on it) only after `induction`: -- ``` -- theorem double.inj : ∀ m, double n = double m → n = m := by -- induction n with -- | zero => intro m h; ... -- ... -- ``` -- `induction` even allows us to apply a tactic before *each* case: -- ``` -- theorem double.inj : ∀ m, double n = double m → n = m := by -- induction n with -- intro m h -- | zero => ... -- ... -- ``` -- However, it turns out that we do not have to change the theorem statement at all: if we simply say -- ``` -- induction n generalizing m with -- ``` -- then `induction` will automatically `revert` (yes, that's also a tactic) and re`intro`duce the variable(s) -- before/after induction for us! So add `generalizing m` above, see how the inductive hypothesis is -- affected, and then go finish that proof! /- Partial & dependent maps -/ -- *Partial maps* are a useful data type for the semantics project and many other topics. -- They map *some* keys of one type to values of another type. abbrev Map (α β : Type) := α → Option β -- We express partiality via the `Option` type, which either holds `some b` for `b : β`, or `none`. -- Ctrl+click it for the whole definition. namespace Map def empty : Map α β := fun k => none -- If we wanted a partial map for programming, we might choose a more efficient implementation such -- as a search tree or a hash map. If, on the other hand, we are only interested in using it in a -- formalization, a simple function like above is usually the simpler solution. For example, a -- simple typing context `Γ` can be formalized as a partial map from variable names to their types. -- The function-based definition makes defining operations such as a map update quite easy: /-- Set the entry `k` of the map `m` to the value `v`. All other entries are unchanged. -/ def update [DecidableEq α] (m : Map α β) (k : α) (v : Option β) : Map α β := _ -- A `scoped` notation is activated only when opening/inside the current namespace scoped notation:max m "[" k " ↦ " v "]" => update m k v theorem apply_update [DecidableEq α] (m : Map α β) : m[k ↦ v] k = v := by -- hint: use function extensionality (`apply funext`) theorem update_self [DecidableEq α] (m : Map α β) : m[k ↦ m k] = m := by end Map -- One interesting generalization of partial maps we can express in Lean are *dependent maps* where -- the *type* of the value may depend on the key: abbrev DepMap (α : Type) (β : α → Type) := (k : α) → Option (β k) namespace DepMap def empty : DepMap α β := fun k => none -- If we try to define `update` as above, it turns out that we run into a type error! -- You may want to use the "dependent if" `if h : p then t else e` that makes a *proof* of -- the condition `p` available in each branch: `h : p` in the `then` branch and `h : ¬p` in the -- `else` branch. You should then be able to use rewriting (e.g. `▸`) to fix the type error. def update [DecidableEq α] (m : DepMap α β) (k : α) (v : Option (β k)) : DepMap α β := _ local notation:max m "[" k " ↦ " v "]" => update m k v -- This one should be as before... theorem apply_update [DecidableEq α] (m : DepMap α β) : m[k ↦ v] k = v := by -- ...but this one is where the fun starts: try replicating the corresponding `Map` proof... theorem update_self [DecidableEq α] (m : DepMap α β) : m[k ↦ m k] = m := by -- and you should end up with an unsolved goal containing a subterm of the shape `(_ : a = b) ▸ c`. This -- is the rewrite from `update`; the proof is elided as `_` by default because, as we said in week 1, Lean -- considers all proofs of a proposition as equal, so we really don't care what proof is displayed there. -- So how do we get rid of the `▸`? We know it is something like a match on `Eq.refl`; more formally, -- both `▸` and such a match compile down to an application of `Eq`'s *recursor* (week 3). -- We know matches/recursors reduce ("go away") when applied to a matching constructor application, -- i.e. for `▸` we have `(rfl ▸ c) ≡ c`. -- So why didn't `simp` reduce away `(_ : a = b) ▸` if it works for `rfl` and all proofs are the same? -- Well, all proofs of a *single* proposition are the same, but `rfl` is not a proof of `a = b` unless -- `a` and `b` are in fact the same term! Thus the general way to get rid of `(_ : a = b) ▸` is to -- first rewrite the goal with a proof of the very equality `a = b`. After that, `simp`, or definitional -- equality in general, will get rid of the `▸`. -- Now, for technical reasons we should use `rw` instead of `simp` itself to do this rewrite. The short -- answer as to why that is is that `simp` tries to be *too clever* in this case: it will rewrite `a = b` -- on both sides of the `▸` individually, which usually makes it more flexible (week 4, slide pages 17 & 20), -- but in this case unfortunately leads to a type-incorrect proof. The "naive" strategy of `rw`, which will -- simply replace all `a` with `b` everywhere simultaneously by applying the `Eq` recursor once at the root, -- turns out to be the better approach in this case. -- Phew, that was a lot of typing (in the theoretic sense and on my keyboard). If you can't get the proof to -- work, don't worry about it, we will not bother you with this kind of "esoteric" proof again. If, on the -- other hand, you are interested in this kind of strong dependent typing, we may have an interesting variant -- of the semantics project to offer you next week! end DepMap open List Nat /- Insertion Sort -/ -- We want to implement insertion sort in Lean and show that the resulting `List` is indeed sorted. -- To that end, we first assume that the type `α` is of the type class `LE`, meaning that we can use -- the symbol `≤` (\le) as notation. -- We also assume (notice that cool dot notation) that this relation is decidable: variable [LE α] [DecidableRel ((· ≤ ·) : α → α → Prop)] -- First, we want to define a predicate that holds if a list is sorted. -- The predicate should have three constructors: -- The empty list `[]` and the single element list `[a]` are sorted, -- and we can add `a` to the front of a sorted list `b :: l`, if `a ≤ b`.s inductive Sorted : List α → Prop where -- The main ingredient to insertion sort is a function `insertInOrder` which inserts -- a given element `a` before the first entry `x` of a list for which `a ≤ x` holds. -- Define that function by recursion on the list. Remember that `≤` is decidable. def insertInOrder (a : α) (xs : List α) : List α := _ -- Now, see whether the function actually does what it should do. #eval insertInOrder 4 [1, 3, 4, 6, 7] #eval insertInOrder 4 [1, 2, 3] -- Defining `insertionSort` itself is now an easy recursion. def insertionSort (xs : List α) : List α := _ -- Let's test the sorting algorithm next. #eval insertionSort [6, 2, 4, 4, 1, 3, 64] #eval insertionSort [1, 2, 3] #eval insertionSort (repeat (fun xs => xs.length :: xs) 500 []) -- Now we want to move on to actually verify that the algorithm does what it claims to do! -- To prove this, we don't need the relation to be transitive, but we need to assume the following property: variable (antisymm : ∀ {x y : α}, ¬ x ≤ y → (y ≤ x)) -- Okay, now prove the statement itself! -- Hints: -- * You might at one point have the choice to either apply induction on a list or on a witness of `Sorted`. -- Choose wisely. -- * Remember the tactic `byCases` from the fifth exercise! theorem sorted_insertionSort (as : List α) : Sorted (insertionSort as) := _ -- Here's a "soft" question: Have we now fully verified that `insertionSort` is a sorting algorithm? -- What other property would be an obvious one to verify?
9f6d290b050256bde02e30d41a041d02be81f6f7
9dc8cecdf3c4634764a18254e94d43da07142918
/src/analysis/special_functions/gaussian.lean
f270da52503f38c7b824ba271bd6b399444d0ad4
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
7,655
lean
/- Copyright (c) 2022 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.special_functions.gamma import analysis.special_functions.polar_coord /-! # Gaussian integral We prove the formula `∫ x, exp (-b * x^2) = sqrt (π / b)`, in `integral_gaussian`. -/ noncomputable theory open real set measure_theory filter asymptotics open_locale real topological_space lemma exp_neg_mul_sq_is_o_exp_neg {b : ℝ} (hb : 0 < b) : (λ x:ℝ, exp (-b * x^2)) =o[at_top] (λ x:ℝ, exp (-x)) := begin have A : (λ (x : ℝ), -x - -b * x ^ 2) = (λ x, x * (b * x + (- 1))), by { ext x, ring }, rw [is_o_exp_comp_exp_comp, A], apply tendsto.at_top_mul_at_top tendsto_id, apply tendsto_at_top_add_const_right at_top (-1 : ℝ), exact tendsto.const_mul_at_top hb tendsto_id, end lemma rpow_mul_exp_neg_mul_sq_is_o_exp_neg {b : ℝ} (hb : 0 < b) (s : ℝ) : (λ x:ℝ, x ^ s * exp (-b * x^2)) =o[at_top] (λ x:ℝ, exp (-(1/2) * x)) := begin apply ((is_O_refl (λ x:ℝ, x ^ s) at_top).mul_is_o (exp_neg_mul_sq_is_o_exp_neg hb)).trans, convert Gamma_integrand_is_o s, simp_rw [mul_comm], end lemma integrable_on_rpow_mul_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) {s : ℝ} (hs : -1 < s) : integrable_on (λ x:ℝ, x ^ s * exp (-b * x^2)) (Ioi 0) := begin rw [← Ioc_union_Ioi_eq_Ioi (zero_le_one : (0 : ℝ) ≤ 1), integrable_on_union], split, { rw [←integrable_on_Icc_iff_integrable_on_Ioc], refine integrable_on.mul_continuous_on _ _ is_compact_Icc, { refine (interval_integrable_iff_integrable_Icc_of_le zero_le_one).mp _, exact interval_integral.interval_integrable_rpow' hs }, { exact (continuous_exp.comp (continuous_const.mul (continuous_pow 2))).continuous_on } }, { have B : (0 : ℝ) < 1/2, by norm_num, apply integrable_of_is_O_exp_neg B _ (is_o.is_O (rpow_mul_exp_neg_mul_sq_is_o_exp_neg hb _)), assume x hx, have N : x ≠ 0, { refine (zero_lt_one.trans_le _).ne', exact hx }, apply ((continuous_at_rpow_const _ _ (or.inl N)).mul _).continuous_within_at, exact (continuous_exp.comp (continuous_const.mul (continuous_pow 2))).continuous_at }, end lemma integrable_rpow_mul_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) {s : ℝ} (hs : -1 < s) : integrable (λ x:ℝ, x ^ s * exp (-b * x^2)) := begin rw [← integrable_on_univ, ← @Iio_union_Ici _ _ (0 : ℝ), integrable_on_union, integrable_on_Ici_iff_integrable_on_Ioi], refine ⟨_, integrable_on_rpow_mul_exp_neg_mul_sq hb hs⟩, rw ← (measure.measure_preserving_neg (volume : measure ℝ)).integrable_on_comp_preimage ((homeomorph.neg ℝ).to_measurable_equiv.measurable_embedding), simp only [function.comp, neg_sq, neg_preimage, preimage_neg_Iio, neg_neg, neg_zero], apply integrable.mono' (integrable_on_rpow_mul_exp_neg_mul_sq hb hs), { apply measurable.ae_strongly_measurable, exact (measurable_id'.neg.pow measurable_const).mul ((measurable_id'.pow measurable_const).const_mul (-b)).exp }, { have : measurable_set (Ioi (0 : ℝ)) := measurable_set_Ioi, filter_upwards [ae_restrict_mem this] with x hx, have h'x : 0 ≤ x := le_of_lt hx, rw [real.norm_eq_abs, abs_mul, abs_of_nonneg (exp_pos _).le], apply mul_le_mul_of_nonneg_right _ (exp_pos _).le, simpa [abs_of_nonneg h'x] using abs_rpow_le_abs_rpow (-x) s } end lemma integrable_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) : integrable (λ x:ℝ, exp (-b * x^2)) := begin have A : (-1 : ℝ) < 0, by norm_num, convert integrable_rpow_mul_exp_neg_mul_sq hb A, simp, end lemma integrable_exp_neg_mul_sq_iff {b : ℝ} : integrable (λ x:ℝ, exp (-b * x^2)) ↔ 0 < b := begin refine ⟨λ h, _, integrable_exp_neg_mul_sq⟩, by_contra' hb, have : ∫⁻ x:ℝ, 1 ≤ ∫⁻ x:ℝ, ∥exp (-b * x^2)∥₊, { apply lintegral_mono (λ x, _), simp only [neg_mul, ennreal.one_le_coe_iff, ← to_nnreal_one, to_nnreal_le_iff_le_coe, real.norm_of_nonneg (exp_pos _).le, coe_nnnorm, one_le_exp_iff, right.nonneg_neg_iff], exact mul_nonpos_of_nonpos_of_nonneg hb (sq_nonneg _) }, simpa using this.trans_lt h.2, end lemma integrable_mul_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) : integrable (λ x:ℝ, x * exp (-b * x^2)) := begin have A : (-1 : ℝ) < 1, by norm_num, convert integrable_rpow_mul_exp_neg_mul_sq hb A, simp, end lemma integral_mul_exp_neg_mul_sq {b : ℝ} (hb : 0 < b) : ∫ r in Ioi 0, r * exp (-b * r ^ 2) = (2 * b)⁻¹ := begin have I : integrable (λ x, x * exp (-b * x^2)) := integrable_mul_exp_neg_mul_sq hb, refine tendsto_nhds_unique (interval_integral_tendsto_integral_Ioi _ I.integrable_on filter.tendsto_id) _, have A : ∀ x, has_deriv_at (λ x, - (2 * b)⁻¹ * exp (-b * x^2)) (x * exp (- b * x^2)) x, { assume x, convert (((has_deriv_at_pow 2 x)).const_mul (-b)).exp.const_mul (- (2 * b)⁻¹) using 1, field_simp [hb.ne'], ring }, have : ∀ (y : ℝ), ∫ x in 0..(id y), x * exp (- b * x^2) = (- (2 * b)⁻¹ * exp (-b * y^2)) - (- (2 * b)⁻¹ * exp (-b * 0^2)) := λ y, interval_integral.integral_eq_sub_of_has_deriv_at (λ x hx, A x) I.interval_integrable, simp_rw [this], have L : tendsto (λ (x : ℝ), (2 * b)⁻¹ - (2 * b)⁻¹ * exp (-b * x ^ 2)) at_top (𝓝 ((2 * b)⁻¹ - (2 * b)⁻¹ * 0)), { refine tendsto_const_nhds.sub _, apply tendsto.const_mul, apply tendsto_exp_at_bot.comp, exact tendsto.neg_const_mul_at_top (neg_lt_zero.2 hb) (tendsto_pow_at_top two_ne_zero) }, simpa using L, end theorem integral_gaussian (b : ℝ) : ∫ x, exp (-b * x^2) = sqrt (π / b) := begin /- First we deal with the crazy case where `b ≤ 0`: then both sides vanish. -/ rcases le_or_lt b 0 with hb|hb, { rw [integral_undef, sqrt_eq_zero_of_nonpos], { exact div_nonpos_of_nonneg_of_nonpos pi_pos.le hb }, { simpa only [not_lt, integrable_exp_neg_mul_sq_iff] using hb } }, /- Assume now `b > 0`. We will show that the squares of the sides coincide. -/ refine (sq_eq_sq _ (sqrt_nonneg _)).1 _, { exact integral_nonneg (λ x, (exp_pos _).le) }, /- We compute `(∫ exp(-b x^2))^2` as an integral over `ℝ^2`, and then make a polar change of coordinates. We are left with `∫ r * exp (-b r^2)`, which has been computed in `integral_mul_exp_neg_mul_sq` using the fact that this function has an obvious primitive. -/ calc (∫ x, real.exp (-b * x^2)) ^ 2 = ∫ p : ℝ × ℝ, exp (-b * p.1 ^ 2) * exp (-b * p.2 ^ 2) : by { rw [pow_two, ← integral_prod_mul], refl } ... = ∫ p : ℝ × ℝ, real.exp (- b * (p.1 ^ 2 + p.2^2)) : by { congr, ext p, simp only [← real.exp_add, neg_add_rev, real.exp_eq_exp], ring } ... = ∫ p in polar_coord.target, p.1 * exp (- b * ((p.1 * cos p.2) ^ 2 + (p.1 * sin p.2)^2)) : (integral_comp_polar_coord_symm (λ p, exp (- b * (p.1^2 + p.2^2)))).symm ... = (∫ r in Ioi (0 : ℝ), r * exp (-b * r^2)) * (∫ θ in Ioo (-π) π, 1) : begin rw ← set_integral_prod_mul, congr' with p, rw mul_one, congr, conv_rhs { rw [← one_mul (p.1^2), ← sin_sq_add_cos_sq p.2], }, ring_exp, end ... = π / b : begin have : 0 ≤ π + π, by linarith [real.pi_pos], simp only [integral_const, measure.restrict_apply', measurable_set_Ioo, univ_inter, this, sub_neg_eq_add, algebra.id.smul_eq_mul, mul_one, volume_Ioo, two_mul, ennreal.to_real_of_real, integral_mul_exp_neg_mul_sq hb, one_mul], field_simp [hb.ne'], ring, end ... = (sqrt (π / b)) ^ 2 : by { rw sq_sqrt, exact div_nonneg pi_pos.le hb.le } end
45a24fa38fbaf26f25542438fb3d017f561f1d0b
a8c03ed21a1bd6fc45901943b79dd6574ea3f0c2
/resolution.lean
00c601c2cd564960ac6c2edcde54808b1cf428ae
[]
no_license
gebner/resolution.lean
716c355fbb5204e5c4d0c5a7f3f3cc825892a2bf
c6fafe06fba1cfad73db68f2aa474b29fe892a2b
refs/heads/master
1,601,111,444,528
1,475,256,701,000
1,475,256,701,000
67,711,151
0
0
null
null
null
null
UTF-8
Lean
false
false
1,954
lean
import clause prover_state utils open tactic monad variable gt : expr → expr → bool variables (ac1 ac2 : active_cls) variables (c1 c2 : clause) variables (i1 i2 : nat) -- c1 : ... → ¬a → ... -- c2 : ... → a → ... meta def try_resolve : tactic clause := do qf1 ← c1↣open_metan c1↣num_quants, qf2 ← c2↣open_metan c2↣num_quants, unify (qf1↣1↣get_lit i1)↣formula (qf2↣1↣get_lit i2)↣formula, qf1i ← qf1↣1↣inst_mvars, guard $ clause.is_maximal gt qf1i i1, op1 ← qf1↣1↣open_constn i1, op2 ← qf2↣1↣open_constn c2↣num_lits, a_in_op2 ← (op2↣2↣nth i2)↣to_monad, clause.meta_closure (qf1.2 ++ qf2.2) $ (op1↣1↣inst (op2↣1↣close_const a_in_op2)↣proof)↣close_constn (op1↣2 ++ op2↣2↣remove i2) meta def try_add_resolvent : resolution_prover unit := do c' ← ↑(try_resolve gt ac1↣c ac2↣c i1 i2), add_inferred c' [ac1, ac2] meta def maybe_add_resolvent : resolution_prover unit := try_add_resolvent gt ac1 ac2 i1 i2 <|> return () meta def resolution_left_inf : inference := take given, do active : rb_map name active_cls ← get_active, sequence' $ do given_i ← given↣selected, guard $ clause.literal.is_neg (given↣c↣get_lit given_i), other ← rb_map.values active, other_i ← other↣selected, guard $ clause.literal.is_pos (other↣c↣get_lit other_i), [maybe_add_resolvent gt other given other_i given_i] meta def resolution_right_inf : inference := take given, do active : rb_map name active_cls ← get_active, sequence' $ do given_i ← given↣selected, guard $ clause.literal.is_pos (given↣c↣get_lit given_i), other ← rb_map.values active, other_i ← other↣selected, guard $ clause.literal.is_neg (other↣c↣get_lit other_i), [maybe_add_resolvent gt given other given_i other_i] meta def resolution_inf : inference := take given, do gt ← get_term_order, resolution_left_inf gt given >> resolution_right_inf gt given
1fa95a015928d25c91ad1f1d1cc20a5d697bcf94
271e26e338b0c14544a889c31c30b39c989f2e0f
/stage0/src/Init/Lean/Elab/Exception.lean
3bcc81f81dca33fa96c90ca693538d6b9b1567c9
[ "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
771
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Lean.Meta namespace Lean namespace Elab abbrev Exception := Message def mkMessageCore (fileName : String) (fileMap : FileMap) (msgData : MessageData) (severity : MessageSeverity) (pos : String.Pos) : Message := let pos := fileMap.toPosition pos; { fileName := fileName, pos := pos, data := msgData, severity := severity } def mkExceptionCore (fileName : String) (fileMap : FileMap) (msgData : MessageData) (pos : String.Pos) : Exception := let pos := fileMap.toPosition pos; { fileName := fileName, pos := pos, data := msgData, severity := MessageSeverity.error } end Elab end Lean
104d6f1237eaf843e6b5704b0960a71eb0142873
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category/bifunctor.lean
26ac2b1ab0432b24c359ec26286d63e82bd70fc3
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
4,352
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon Functors with two arguments -/ import logic.function data.sum category.basic category.functor tactic.basic universes u₀ u₁ u₂ v₀ v₁ v₂ open function class bifunctor (F : Type u₀ → Type u₁ → Type u₂) := (bimap : Π {α α' β β'}, (α → α') → (β → β') → F α β → F α' β') export bifunctor ( bimap ) class is_lawful_bifunctor (F : Type u₀ → Type u₁ → Type u₂) [bifunctor F] := (id_bimap : Π {α β} (x : F α β), bimap id id x = x) (bimap_bimap : Π {α₀ α₁ α₂ β₀ β₁ β₂} (f : α₀ → α₁) (f' : α₁ → α₂) (g : β₀ → β₁) (g' : β₁ → β₂) (x : F α₀ β₀), bimap f' g' (bimap f g x) = bimap (f' ∘ f) (g' ∘ g) x) export is_lawful_bifunctor (id_bimap bimap_bimap) attribute [higher_order bimap_id_id] id_bimap attribute [higher_order bimap_comp_bimap] bimap_bimap export is_lawful_bifunctor (bimap_id_id bimap_comp_bimap) variables {F : Type u₀ → Type u₁ → Type u₂} [bifunctor F] namespace bifunctor @[reducible] def fst {α α' β} (f : α → α') : F α β → F α' β := bimap f id @[reducible] def snd {α β β'} (f : β → β') : F α β → F α β' := bimap id f variable [is_lawful_bifunctor F] @[higher_order fst_id] lemma id_fst : Π {α β} (x : F α β), fst id x = x := @id_bimap _ _ _ @[higher_order snd_id] lemma id_snd : Π {α β} (x : F α β), snd id x = x := @id_bimap _ _ _ @[higher_order fst_comp_fst] lemma comp_fst {α₀ α₁ α₂ β} (f : α₀ → α₁) (f' : α₁ → α₂) (x : F α₀ β) : fst f' (fst f x) = fst (f' ∘ f) x := by simp [fst,bimap_bimap] @[higher_order fst_comp_snd] lemma fst_snd {α₀ α₁ β₀ β₁} (f : α₀ → α₁) (f' : β₀ → β₁) (x : F α₀ β₀) : fst f (snd f' x) = bimap f f' x := by simp [fst,bimap_bimap] @[higher_order snd_comp_fst] lemma snd_fst {α₀ α₁ β₀ β₁} (f : α₀ → α₁) (f' : β₀ → β₁) (x : F α₀ β₀) : snd f' (fst f x) = bimap f f' x := by simp [snd,bimap_bimap] @[higher_order snd_comp_snd] lemma comp_snd {α β₀ β₁ β₂} (g : β₀ → β₁) (g' : β₁ → β₂) (x : F α β₀) : snd g' (snd g x) = snd (g' ∘ g) x := by simp [snd,bimap_bimap] attribute [functor_norm] bimap_bimap comp_snd comp_fst snd_comp_snd snd_comp_fst fst_comp_snd fst_comp_fst bimap_comp_bimap bimap_id_id fst_id snd_id end bifunctor open functor instance : bifunctor prod := { bimap := @prod.map } instance : is_lawful_bifunctor prod := by refine { .. }; intros; cases x; refl instance bifunctor.const : bifunctor const := { bimap := (λ α α' β β f _, f) } instance is_lawful_bifunctor.const : is_lawful_bifunctor const := by refine { .. }; intros; refl instance bifunctor.flip : bifunctor (flip F) := { bimap := (λ α α' β β' f f' x, (bimap f' f x : F β' α')) } instance is_lawful_bifunctor.flip [is_lawful_bifunctor F] : is_lawful_bifunctor (flip F) := by refine { .. }; intros; simp [bimap] with functor_norm instance : bifunctor sum := { bimap := @sum.map } instance : is_lawful_bifunctor sum := by refine { .. }; intros; cases x; refl open bifunctor functor @[priority 0] instance bifunctor.functor {α} : functor (F α) := { map := λ _ _, snd } @[priority 0] instance bifunctor.is_lawful_functor [is_lawful_bifunctor F] {α} : is_lawful_functor (F α) := by refine {..}; intros; simp [functor.map] with functor_norm section bicompl variables (G : Type* → Type u₀) (H : Type* → Type u₁) [functor G] [functor H] instance : bifunctor (bicompl F G H) := { bimap := λ α α' β β' f f' x, (bimap (map f) (map f') x : F (G α') (H β')) } instance [is_lawful_functor G] [is_lawful_functor H] [is_lawful_bifunctor F] : is_lawful_bifunctor (bicompl F G H) := by constructor; intros; simp [bimap,map_id,map_comp_map] with functor_norm end bicompl section bicompr variables (G : Type u₂ → Type*) [functor G] instance : bifunctor (bicompr G F) := { bimap := λ α α' β β' f f' x, (map (bimap f f') x : G (F α' β')) } instance [is_lawful_functor G] [is_lawful_bifunctor F] : is_lawful_bifunctor (bicompr G F) := by constructor; intros; simp [bimap] with functor_norm end bicompr
062d1bf24ae9f46af62da2323f7754be7d59843e
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/08_Building_Theories_and_Proofs.org.8.lean
b82677a9394c879a71a1ad23344844cb848c3ff0
[]
no_license
cjmazey/lean-tutorial
ba559a49f82aa6c5848b9bf17b7389bf7f4ba645
381f61c9fcac56d01d959ae0fa6e376f2c4e3b34
refs/heads/master
1,610,286,098,832
1,447,124,923,000
1,447,124,923,000
43,082,433
0
0
null
null
null
null
UTF-8
Lean
false
false
693
lean
import standard structure Semigroup : Type := (carrier : Type) (mul : carrier → carrier → carrier) (mul_assoc : ∀ a b c : carrier, mul (mul a b) c = mul a (mul b c)) notation a `*` b := Semigroup.mul _ a b attribute Semigroup.carrier [coercion] structure morphism (S1 S2 : Semigroup) : Type := (mor : S1 → S2) (resp_mul : ∀ a b : S1, mor (a * b) = (mor a) * (mor b)) attribute morphism.mor [coercion] -- BEGIN theorem test (S1 S2 : Semigroup) (f : morphism S1 S2) (a : S1) : f (a * a * a) = f a * f a * f a := calc f (a * a * a) = f (a * a) * f a : morphism.resp_mul f ... = f a * f a * f a : morphism.resp_mul f set_option pp.coercions true check test print coercions -- END
494ba3f87abb3db3645466c525a34f6620c03f36
618003631150032a5676f229d13a079ac875ff77
/src/data/num/bitwise.lean
a89e892064278a0178c170f8da5802fdf48af6a0
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
7,576
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro Bitwise operations using binary representation of integers. -/ import data.num.basic import data.bitvec namespace pos_num def lor : pos_num → pos_num → pos_num | 1 (bit0 q) := bit1 q | 1 q := q | (bit0 p) 1 := bit1 p | p 1 := p | (bit0 p) (bit0 q) := bit0 (lor p q) | (bit0 p) (bit1 q) := bit1 (lor p q) | (bit1 p) (bit0 q) := bit1 (lor p q) | (bit1 p) (bit1 q) := bit1 (lor p q) def land : pos_num → pos_num → num | 1 (bit0 q) := 0 | 1 _ := 1 | (bit0 p) 1 := 0 | _ 1 := 1 | (bit0 p) (bit0 q) := num.bit0 (land p q) | (bit0 p) (bit1 q) := num.bit0 (land p q) | (bit1 p) (bit0 q) := num.bit0 (land p q) | (bit1 p) (bit1 q) := num.bit1 (land p q) def ldiff : pos_num → pos_num → num | 1 (bit0 q) := 1 | 1 _ := 0 | (bit0 p) 1 := num.pos (bit0 p) | (bit1 p) 1 := num.pos (bit0 p) | (bit0 p) (bit0 q) := num.bit0 (ldiff p q) | (bit0 p) (bit1 q) := num.bit0 (ldiff p q) | (bit1 p) (bit0 q) := num.bit1 (ldiff p q) | (bit1 p) (bit1 q) := num.bit0 (ldiff p q) def lxor : pos_num → pos_num → num | 1 1 := 0 | 1 (bit0 q) := num.pos (bit1 q) | 1 (bit1 q) := num.pos (bit0 q) | (bit0 p) 1 := num.pos (bit1 p) | (bit1 p) 1 := num.pos (bit0 p) | (bit0 p) (bit0 q) := num.bit0 (lxor p q) | (bit0 p) (bit1 q) := num.bit1 (lxor p q) | (bit1 p) (bit0 q) := num.bit1 (lxor p q) | (bit1 p) (bit1 q) := num.bit0 (lxor p q) def test_bit : pos_num → nat → bool | 1 0 := tt | 1 (n+1) := ff | (bit0 p) 0 := ff | (bit0 p) (n+1) := test_bit p n | (bit1 p) 0 := tt | (bit1 p) (n+1) := test_bit p n def one_bits : pos_num → nat → list nat | 1 d := [d] | (bit0 p) d := one_bits p (d+1) | (bit1 p) d := d :: one_bits p (d+1) def shiftl (p : pos_num) : nat → pos_num | 0 := p | (n+1) := bit0 (shiftl n) def shiftr : pos_num → nat → num | p 0 := num.pos p | 1 (n+1) := 0 | (bit0 p) (n+1) := shiftr p n | (bit1 p) (n+1) := shiftr p n end pos_num namespace num def lor : num → num → num | 0 q := q | p 0 := p | (pos p) (pos q) := pos (p.lor q) def land : num → num → num | 0 q := 0 | p 0 := 0 | (pos p) (pos q) := p.land q def ldiff : num → num → num | 0 q := 0 | p 0 := p | (pos p) (pos q) := p.ldiff q def lxor : num → num → num | 0 q := q | p 0 := p | (pos p) (pos q) := p.lxor q def shiftl : num → nat → num | 0 n := 0 | (pos p) n := pos (p.shiftl n) def shiftr : num → nat → num | 0 n := 0 | (pos p) n := p.shiftr n def test_bit : num → nat → bool | 0 n := ff | (pos p) n := p.test_bit n def one_bits : num → list nat | 0 := [] | (pos p) := p.one_bits 0 end num /-- See `snum`. -/ @[derive has_reflect, derive decidable_eq] inductive nzsnum : Type | msb : bool → nzsnum | bit : bool → nzsnum → nzsnum /-- Alternative representation of integers using a sign bit at the end. The convention on sign here is to have the argument to `msb` denote the sign of the MSB itself, with all higher bits set to the negation of this sign. The result is interpreted in two's complement. 13 = ..0001101(base 2) = nz (bit1 (bit0 (bit1 (msb tt)))) -13 = ..1110011(base 2) = nz (bit1 (bit1 (bit0 (msb ff)))) As with `num`, a special case must be added for zero, which has no msb, but by two's complement symmetry there is a second special case for -1. Here the `bool` field indicates the sign of the number. 0 = ..0000000(base 2) = zero ff -1 = ..1111111(base 2) = zero tt -/ @[derive has_reflect, derive decidable_eq] inductive snum : Type | zero : bool → snum | nz : nzsnum → snum instance : has_coe nzsnum snum := ⟨snum.nz⟩ instance : has_zero snum := ⟨snum.zero ff⟩ instance : has_one nzsnum := ⟨nzsnum.msb tt⟩ instance : has_one snum := ⟨snum.nz 1⟩ instance : inhabited nzsnum := ⟨1⟩ instance : inhabited snum := ⟨0⟩ /- The snum representation uses a bit string, essentially a list of 0 (ff) and 1 (tt) bits, and the negation of the MSB is sign-extended to all higher bits. -/ namespace nzsnum notation a :: b := bit a b def sign : nzsnum → bool | (msb b) := bnot b | (b :: p) := sign p @[pattern] def not : nzsnum → nzsnum | (msb b) := msb (bnot b) | (b :: p) := bnot b :: not p prefix ~ := not def bit0 : nzsnum → nzsnum := bit ff def bit1 : nzsnum → nzsnum := bit tt def head : nzsnum → bool | (msb b) := b | (b :: p) := b def tail : nzsnum → snum | (msb b) := snum.zero (bnot b) | (b :: p) := p end nzsnum namespace snum open nzsnum def sign : snum → bool | (zero z) := z | (nz p) := p.sign @[pattern] def not : snum → snum | (zero z) := zero (bnot z) | (nz p) := ~p prefix ~ := not @[pattern] def bit : bool → snum → snum | b (zero z) := if b = z then zero b else msb b | b (nz p) := p.bit b notation a :: b := bit a b def bit0 : snum → snum := bit ff def bit1 : snum → snum := bit tt theorem bit_zero (b) : b :: zero b = zero b := by cases b; refl theorem bit_one (b) : b :: zero (bnot b) = msb b := by cases b; refl end snum namespace nzsnum open snum def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p : nzsnum, C p | (msb b) := by rw ←bit_one; exact s b (snum.zero (bnot b)) (z (bnot b)) | (bit b p) := s b p (drec' p) end nzsnum namespace snum open nzsnum def head : snum → bool | (zero z) := z | (nz p) := p.head def tail : snum → snum | (zero z) := zero z | (nz p) := p.tail def drec' {C : snum → Sort*} (z : Π b, C (snum.zero b)) (s : Π b p, C p → C (b :: p)) : Π p, C p | (zero b) := z b | (nz p) := p.drec' z s def rec' {α} (z : bool → α) (s : bool → snum → α → α) : snum → α := drec' z s def test_bit : nat → snum → bool | 0 p := head p | (n+1) p := test_bit n (tail p) def succ : snum → snum := rec' (λ b, cond b 0 1) (λb p succp, cond b (ff :: succp) (tt :: p)) def pred : snum → snum := rec' (λ b, cond b (~1) ~0) (λb p predp, cond b (ff :: p) (tt :: predp)) protected def neg (n : snum) : snum := succ ~n instance : has_neg snum := ⟨snum.neg⟩ -- First bit is 0 or 1 (tt), second bit is 0 or -1 (tt) def czadd : bool → bool → snum → snum | ff ff p := p | ff tt p := pred p | tt ff p := succ p | tt tt p := p end snum namespace snum def bits : snum → Π n, vector bool n | p 0 := vector.nil | p (n+1) := head p :: bits (tail p) n def cadd : snum → snum → bool → snum := rec' (λ a p c, czadd c a p) $ λa p IH, rec' (λb c, czadd c b (a :: p)) $ λb q _ c, bitvec.xor3 a b c :: IH q (bitvec.carry a b c) protected def add (a b : snum) : snum := cadd a b ff instance : has_add snum := ⟨snum.add⟩ protected def sub (a b : snum) : snum := a + -b instance : has_sub snum := ⟨snum.sub⟩ protected def mul (a : snum) : snum → snum := rec' (λ b, cond b (-a) 0) $ λb q IH, cond b (bit0 IH + a) (bit0 IH) instance : has_mul snum := ⟨snum.mul⟩ end snum
428b07ca8d7aadffdf355c8291658850dcd34088
624f6f2ae8b3b1adc5f8f67a365c51d5126be45a
/src/Init/Data/ByteArray/Basic.lean
b99f4f3a28c5d1ff7bf8a3373e7bb1b2e5c73325
[ "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
1,643
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 -/ prelude import Init.Data.Array.Basic import Init.Data.UInt import Init.Data.Option.Basic universes u structure ByteArray := (data : Array UInt8) attribute [extern "lean_byte_array_mk"] ByteArray.mk attribute [extern "lean_byte_array_data"] ByteArray.data namespace ByteArray @[extern "lean_mk_empty_byte_array"] def mkEmpty (c : @& Nat) : ByteArray := { data := #[] } def empty : ByteArray := mkEmpty 0 instance : Inhabited ByteArray := ⟨empty⟩ @[extern "lean_byte_array_push"] def push : ByteArray → UInt8 → ByteArray | ⟨bs⟩, b => ⟨bs.push b⟩ @[extern "lean_byte_array_size"] def size : (@& ByteArray) → Nat | ⟨bs⟩ => bs.size @[extern "lean_byte_array_get"] def get! : (@& ByteArray) → (@& Nat) → UInt8 | ⟨bs⟩, i => bs.get! i @[extern "lean_byte_array_set"] def set! : ByteArray → (@& Nat) → UInt8 → ByteArray | ⟨bs⟩, i, b => ⟨bs.set! i b⟩ def isEmpty (s : ByteArray) : Bool := s.size == 0 partial def toListAux (bs : ByteArray) : Nat → List UInt8 → List UInt8 | i, r => if i < bs.size then toListAux (i+1) (bs.get! i :: r) else r.reverse def toList (bs : ByteArray) : List UInt8 := toListAux bs 0 [] end ByteArray def List.toByteArrayAux : List UInt8 → ByteArray → ByteArray | [], r => r | b::bs, r => List.toByteArrayAux bs (r.push b) def List.toByteArray (bs : List UInt8) : ByteArray := bs.toByteArrayAux ByteArray.empty instance : HasToString ByteArray := ⟨fun bs => bs.toList.toString⟩
872a63864c07dbb7f68197b499a6e93c2da6605f
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/algebra/ordered_ring.lean
1e2cb09c2ecd82afc11c31915a632889bca658b5
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
16,384
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ prelude import init.algebra.ordered_group init.algebra.ring /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ set_option default_priority 100 set_option old_structure_cmd true universe u class ordered_semiring (α : Type u) extends semiring α, ordered_cancel_comm_monoid α := (mul_le_mul_of_nonneg_left: ∀ a b c : α, a ≤ b → 0 ≤ c → c * a ≤ c * b) (mul_le_mul_of_nonneg_right: ∀ a b c : α, a ≤ b → 0 ≤ c → a * c ≤ b * c) (mul_lt_mul_of_pos_left: ∀ a b c : α, a < b → 0 < c → c * a < c * b) (mul_lt_mul_of_pos_right: ∀ a b c : α, a < b → 0 < c → a * c < b * c) variable {α : Type u} section ordered_semiring variable [ordered_semiring α] lemma mul_le_mul_of_nonneg_left {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := ordered_semiring.mul_le_mul_of_nonneg_left a b c h₁ h₂ lemma mul_le_mul_of_nonneg_right {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := ordered_semiring.mul_le_mul_of_nonneg_right a b c h₁ h₂ lemma mul_lt_mul_of_pos_left {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b := ordered_semiring.mul_lt_mul_of_pos_left a b c h₁ h₂ lemma mul_lt_mul_of_pos_right {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c := ordered_semiring.mul_lt_mul_of_pos_right a b c h₁ h₂ -- TODO: there are four variations, depending on which variables we assume to be nonneg lemma mul_le_mul {a b c d : α} (hac : a ≤ c) (hbd : b ≤ d) (nn_b : 0 ≤ b) (nn_c : 0 ≤ c) : a * b ≤ c * d := calc a * b ≤ c * b : mul_le_mul_of_nonneg_right hac nn_b ... ≤ c * d : mul_le_mul_of_nonneg_left hbd nn_c lemma mul_nonneg {a b : α} (ha : a ≥ 0) (hb : b ≥ 0) : a * b ≥ 0 := have h : 0 * b ≤ a * b, from mul_le_mul_of_nonneg_right ha hb, by rwa [zero_mul] at h lemma mul_nonpos_of_nonneg_of_nonpos {a b : α} (ha : a ≥ 0) (hb : b ≤ 0) : a * b ≤ 0 := have h : a * b ≤ a * 0, from mul_le_mul_of_nonneg_left hb ha, by rwa mul_zero at h lemma mul_nonpos_of_nonpos_of_nonneg {a b : α} (ha : a ≤ 0) (hb : b ≥ 0) : a * b ≤ 0 := have h : a * b ≤ 0 * b, from mul_le_mul_of_nonneg_right ha hb, by rwa zero_mul at h lemma mul_lt_mul {a b c d : α} (hac : a < c) (hbd : b ≤ d) (pos_b : 0 < b) (nn_c : 0 ≤ c) : a * b < c * d := calc a * b < c * b : mul_lt_mul_of_pos_right hac pos_b ... ≤ c * d : mul_le_mul_of_nonneg_left hbd nn_c lemma mul_lt_mul' {a b c d : α} (h1 : a ≤ c) (h2 : b < d) (h3 : b ≥ 0) (h4 : c > 0) : a * b < c * d := calc a * b ≤ c * b : mul_le_mul_of_nonneg_right h1 h3 ... < c * d : mul_lt_mul_of_pos_left h2 h4 lemma mul_pos {a b : α} (ha : a > 0) (hb : b > 0) : a * b > 0 := have h : 0 * b < a * b, from mul_lt_mul_of_pos_right ha hb, by rwa zero_mul at h lemma mul_neg_of_pos_of_neg {a b : α} (ha : a > 0) (hb : b < 0) : a * b < 0 := have h : a * b < a * 0, from mul_lt_mul_of_pos_left hb ha, by rwa mul_zero at h lemma mul_neg_of_neg_of_pos {a b : α} (ha : a < 0) (hb : b > 0) : a * b < 0 := have h : a * b < 0 * b, from mul_lt_mul_of_pos_right ha hb, by rwa zero_mul at h lemma mul_self_le_mul_self {a b : α} (h1 : 0 ≤ a) (h2 : a ≤ b) : a * a ≤ b * b := mul_le_mul h2 h2 h1 (le_trans h1 h2) lemma mul_self_lt_mul_self {a b : α} (h1 : 0 ≤ a) (h2 : a < b) : a * a < b * b := mul_lt_mul' (le_of_lt h2) h2 h1 (lt_of_le_of_lt h1 h2) end ordered_semiring class linear_ordered_semiring (α : Type u) extends ordered_semiring α, linear_order α := (zero_lt_one : zero < one) section linear_ordered_semiring variable [linear_ordered_semiring α] lemma zero_lt_one : 0 < (1:α) := linear_ordered_semiring.zero_lt_one α lemma zero_le_one : 0 ≤ (1:α) := le_of_lt zero_lt_one lemma lt_of_mul_lt_mul_left {a b c : α} (h : c * a < c * b) (hc : c ≥ 0) : a < b := lt_of_not_ge (assume h1 : b ≤ a, have h2 : c * b ≤ c * a, from mul_le_mul_of_nonneg_left h1 hc, not_lt_of_ge h2 h) lemma lt_of_mul_lt_mul_right {a b c : α} (h : a * c < b * c) (hc : c ≥ 0) : a < b := lt_of_not_ge (assume h1 : b ≤ a, have h2 : b * c ≤ a * c, from mul_le_mul_of_nonneg_right h1 hc, not_lt_of_ge h2 h) lemma le_of_mul_le_mul_left {a b c : α} (h : c * a ≤ c * b) (hc : c > 0) : a ≤ b := le_of_not_gt (assume h1 : b < a, have h2 : c * b < c * a, from mul_lt_mul_of_pos_left h1 hc, not_le_of_gt h2 h) lemma le_of_mul_le_mul_right {a b c : α} (h : a * c ≤ b * c) (hc : c > 0) : a ≤ b := le_of_not_gt (assume h1 : b < a, have h2 : b * c < a * c, from mul_lt_mul_of_pos_right h1 hc, not_le_of_gt h2 h) lemma pos_of_mul_pos_left {a b : α} (h : 0 < a * b) (h1 : 0 ≤ a) : 0 < b := lt_of_not_ge (assume h2 : b ≤ 0, have h3 : a * b ≤ 0, from mul_nonpos_of_nonneg_of_nonpos h1 h2, not_lt_of_ge h3 h) lemma pos_of_mul_pos_right {a b : α} (h : 0 < a * b) (h1 : 0 ≤ b) : 0 < a := lt_of_not_ge (assume h2 : a ≤ 0, have h3 : a * b ≤ 0, from mul_nonpos_of_nonpos_of_nonneg h2 h1, not_lt_of_ge h3 h) lemma nonneg_of_mul_nonneg_left {a b : α} (h : 0 ≤ a * b) (h1 : 0 < a) : 0 ≤ b := le_of_not_gt (assume h2 : b < 0, not_le_of_gt (mul_neg_of_pos_of_neg h1 h2) h) lemma nonneg_of_mul_nonneg_right {a b : α} (h : 0 ≤ a * b) (h1 : 0 < b) : 0 ≤ a := le_of_not_gt (assume h2 : a < 0, not_le_of_gt (mul_neg_of_neg_of_pos h2 h1) h) lemma neg_of_mul_neg_left {a b : α} (h : a * b < 0) (h1 : 0 ≤ a) : b < 0 := lt_of_not_ge (assume h2 : b ≥ 0, not_lt_of_ge (mul_nonneg h1 h2) h) lemma neg_of_mul_neg_right {a b : α} (h : a * b < 0) (h1 : 0 ≤ b) : a < 0 := lt_of_not_ge (assume h2 : a ≥ 0, not_lt_of_ge (mul_nonneg h2 h1) h) lemma nonpos_of_mul_nonpos_left {a b : α} (h : a * b ≤ 0) (h1 : 0 < a) : b ≤ 0 := le_of_not_gt (assume h2 : b > 0, not_le_of_gt (mul_pos h1 h2) h) lemma nonpos_of_mul_nonpos_right {a b : α} (h : a * b ≤ 0) (h1 : 0 < b) : a ≤ 0 := le_of_not_gt (assume h2 : a > 0, not_le_of_gt (mul_pos h2 h1) h) end linear_ordered_semiring class decidable_linear_ordered_semiring (α : Type u) extends linear_ordered_semiring α, decidable_linear_order α class ordered_ring (α : Type u) extends ring α, ordered_comm_group α, zero_ne_one_class α := (mul_nonneg : ∀ a b : α, 0 ≤ a → 0 ≤ b → 0 ≤ a * b) (mul_pos : ∀ a b : α, 0 < a → 0 < b → 0 < a * b) lemma ordered_ring.mul_le_mul_of_nonneg_left [s : ordered_ring α] {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : c * a ≤ c * b := have 0 ≤ b - a, from sub_nonneg_of_le h₁, have 0 ≤ c * (b - a), from ordered_ring.mul_nonneg c (b - a) h₂ this, begin rw mul_sub_left_distrib at this, apply le_of_sub_nonneg this end lemma ordered_ring.mul_le_mul_of_nonneg_right [s : ordered_ring α] {a b c : α} (h₁ : a ≤ b) (h₂ : 0 ≤ c) : a * c ≤ b * c := have 0 ≤ b - a, from sub_nonneg_of_le h₁, have 0 ≤ (b - a) * c, from ordered_ring.mul_nonneg (b - a) c this h₂, begin rw mul_sub_right_distrib at this, apply le_of_sub_nonneg this end lemma ordered_ring.mul_lt_mul_of_pos_left [s : ordered_ring α] {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : c * a < c * b := have 0 < b - a, from sub_pos_of_lt h₁, have 0 < c * (b - a), from ordered_ring.mul_pos c (b - a) h₂ this, begin rw mul_sub_left_distrib at this, apply lt_of_sub_pos this end lemma ordered_ring.mul_lt_mul_of_pos_right [s : ordered_ring α] {a b c : α} (h₁ : a < b) (h₂ : 0 < c) : a * c < b * c := have 0 < b - a, from sub_pos_of_lt h₁, have 0 < (b - a) * c, from ordered_ring.mul_pos (b - a) c this h₂, begin rw mul_sub_right_distrib at this, apply lt_of_sub_pos this end instance ordered_ring.to_ordered_semiring [s : ordered_ring α] : ordered_semiring α := { s with mul_zero := mul_zero, zero_mul := zero_mul, add_left_cancel := @add_left_cancel α _, add_right_cancel := @add_right_cancel α _, le_of_add_le_add_left := @le_of_add_le_add_left α _, mul_le_mul_of_nonneg_left := @ordered_ring.mul_le_mul_of_nonneg_left α _, mul_le_mul_of_nonneg_right := @ordered_ring.mul_le_mul_of_nonneg_right α _, mul_lt_mul_of_pos_left := @ordered_ring.mul_lt_mul_of_pos_left α _, mul_lt_mul_of_pos_right := @ordered_ring.mul_lt_mul_of_pos_right α _, lt_of_add_lt_add_left := @lt_of_add_lt_add_left α _} section ordered_ring variable [ordered_ring α] lemma mul_le_mul_of_nonpos_left {a b c : α} (h : b ≤ a) (hc : c ≤ 0) : c * a ≤ c * b := have -c ≥ 0, from neg_nonneg_of_nonpos hc, have -c * b ≤ -c * a, from mul_le_mul_of_nonneg_left h this, have -(c * b) ≤ -(c * a), by rwa [← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul] at this, le_of_neg_le_neg this lemma mul_le_mul_of_nonpos_right {a b c : α} (h : b ≤ a) (hc : c ≤ 0) : a * c ≤ b * c := have -c ≥ 0, from neg_nonneg_of_nonpos hc, have b * -c ≤ a * -c, from mul_le_mul_of_nonneg_right h this, have -(b * c) ≤ -(a * c), by rwa [← neg_mul_eq_mul_neg, ← neg_mul_eq_mul_neg] at this, le_of_neg_le_neg this lemma mul_nonneg_of_nonpos_of_nonpos {a b : α} (ha : a ≤ 0) (hb : b ≤ 0) : 0 ≤ a * b := have 0 * b ≤ a * b, from mul_le_mul_of_nonpos_right ha hb, by rwa zero_mul at this lemma mul_lt_mul_of_neg_left {a b c : α} (h : b < a) (hc : c < 0) : c * a < c * b := have -c > 0, from neg_pos_of_neg hc, have -c * b < -c * a, from mul_lt_mul_of_pos_left h this, have -(c * b) < -(c * a), by rwa [← neg_mul_eq_neg_mul, ← neg_mul_eq_neg_mul] at this, lt_of_neg_lt_neg this lemma mul_lt_mul_of_neg_right {a b c : α} (h : b < a) (hc : c < 0) : a * c < b * c := have -c > 0, from neg_pos_of_neg hc, have b * -c < a * -c, from mul_lt_mul_of_pos_right h this, have -(b * c) < -(a * c), by rwa [← neg_mul_eq_mul_neg, ← neg_mul_eq_mul_neg] at this, lt_of_neg_lt_neg this lemma mul_pos_of_neg_of_neg {a b : α} (ha : a < 0) (hb : b < 0) : 0 < a * b := have 0 * b < a * b, from mul_lt_mul_of_neg_right ha hb, by rwa zero_mul at this end ordered_ring class linear_ordered_ring (α : Type u) extends ordered_ring α, linear_order α := (zero_lt_one : zero < one) instance linear_ordered_ring.to_linear_ordered_semiring [s : linear_ordered_ring α] : linear_ordered_semiring α := { s with mul_zero := mul_zero, zero_mul := zero_mul, add_left_cancel := @add_left_cancel α _, add_right_cancel := @add_right_cancel α _, le_of_add_le_add_left := @le_of_add_le_add_left α _, mul_le_mul_of_nonneg_left := @mul_le_mul_of_nonneg_left α _, mul_le_mul_of_nonneg_right := @mul_le_mul_of_nonneg_right α _, mul_lt_mul_of_pos_left := @mul_lt_mul_of_pos_left α _, mul_lt_mul_of_pos_right := @mul_lt_mul_of_pos_right α _, le_total := linear_ordered_ring.le_total, lt_of_add_lt_add_left := @lt_of_add_lt_add_left α _ } section linear_ordered_ring variable [linear_ordered_ring α] lemma mul_self_nonneg (a : α) : a * a ≥ 0 := or.elim (le_total 0 a) (assume h : a ≥ 0, mul_nonneg h h) (assume h : a ≤ 0, mul_nonneg_of_nonpos_of_nonpos h h) lemma pos_and_pos_or_neg_and_neg_of_mul_pos {a b : α} (hab : a * b > 0) : (a > 0 ∧ b > 0) ∨ (a < 0 ∧ b < 0) := match lt_trichotomy 0 a with | or.inl hlt₁ := match lt_trichotomy 0 b with | or.inl hlt₂ := or.inl ⟨hlt₁, hlt₂⟩ | or.inr (or.inl heq₂) := begin rw [← heq₂, mul_zero] at hab, exact absurd hab (lt_irrefl _) end | or.inr (or.inr hgt₂) := absurd hab (lt_asymm (mul_neg_of_pos_of_neg hlt₁ hgt₂)) end | or.inr (or.inl heq₁) := begin rw [← heq₁, zero_mul] at hab, exact absurd hab (lt_irrefl _) end | or.inr (or.inr hgt₁) := match lt_trichotomy 0 b with | or.inl hlt₂ := absurd hab (lt_asymm (mul_neg_of_neg_of_pos hgt₁ hlt₂)) | or.inr (or.inl heq₂) := begin rw [← heq₂, mul_zero] at hab, exact absurd hab (lt_irrefl _) end | or.inr (or.inr hgt₂) := or.inr ⟨hgt₁, hgt₂⟩ end end lemma gt_of_mul_lt_mul_neg_left {a b c : α} (h : c * a < c * b) (hc : c ≤ 0) : a > b := have nhc : -c ≥ 0, from neg_nonneg_of_nonpos hc, have h2 : -(c * b) < -(c * a), from neg_lt_neg h, have h3 : (-c) * b < (-c) * a, from calc (-c) * b = - (c * b) : by rewrite neg_mul_eq_neg_mul ... < -(c * a) : h2 ... = (-c) * a : by rewrite neg_mul_eq_neg_mul, lt_of_mul_lt_mul_left h3 nhc lemma zero_gt_neg_one : -1 < (0:α) := begin have this := neg_lt_neg (@zero_lt_one α _), rwa neg_zero at this end lemma le_of_mul_le_of_ge_one {a b c : α} (h : a * c ≤ b) (hb : b ≥ 0) (hc : c ≥ 1) : a ≤ b := have h' : a * c ≤ b * c, from calc a * c ≤ b : h ... = b * 1 : by rewrite mul_one ... ≤ b * c : mul_le_mul_of_nonneg_left hc hb, le_of_mul_le_mul_right h' (lt_of_lt_of_le zero_lt_one hc) lemma nonneg_le_nonneg_of_squares_le {a b : α} (hb : b ≥ 0) (h : a * a ≤ b * b) : a ≤ b := le_of_not_gt (λhab, not_le_of_gt (mul_self_lt_mul_self hb hab) h) lemma mul_self_le_mul_self_iff {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a ≤ b ↔ a * a ≤ b * b := ⟨mul_self_le_mul_self h1, nonneg_le_nonneg_of_squares_le h2⟩ lemma mul_self_lt_mul_self_iff {a b : α} (h1 : 0 ≤ a) (h2 : 0 ≤ b) : a < b ↔ a * a < b * b := iff.trans (lt_iff_not_ge _ _) $ iff.trans (not_iff_not_of_iff $ mul_self_le_mul_self_iff h2 h1) $ iff.symm (lt_iff_not_ge _ _) end linear_ordered_ring class linear_ordered_comm_ring (α : Type u) extends linear_ordered_ring α, comm_monoid α lemma linear_ordered_comm_ring.eq_zero_or_eq_zero_of_mul_eq_zero [s : linear_ordered_comm_ring α] {a b : α} (h : a * b = 0) : a = 0 ∨ b = 0 := match lt_trichotomy 0 a with | or.inl hlt₁ := match lt_trichotomy 0 b with | or.inl hlt₂ := have 0 < a * b, from mul_pos hlt₁ hlt₂, begin rw h at this, exact absurd this (lt_irrefl _) end | or.inr (or.inl heq₂) := or.inr heq₂.symm | or.inr (or.inr hgt₂) := have 0 > a * b, from mul_neg_of_pos_of_neg hlt₁ hgt₂, begin rw h at this, exact absurd this (lt_irrefl _) end end | or.inr (or.inl heq₁) := or.inl heq₁.symm | or.inr (or.inr hgt₁) := match lt_trichotomy 0 b with | or.inl hlt₂ := have 0 > a * b, from mul_neg_of_neg_of_pos hgt₁ hlt₂, begin rw h at this, exact absurd this (lt_irrefl _) end | or.inr (or.inl heq₂) := or.inr heq₂.symm | or.inr (or.inr hgt₂) := have 0 < a * b, from mul_pos_of_neg_of_neg hgt₁ hgt₂, begin rw h at this, exact absurd this (lt_irrefl _) end end end instance linear_ordered_comm_ring.to_integral_domain [s: linear_ordered_comm_ring α] : integral_domain α := {s with eq_zero_or_eq_zero_of_mul_eq_zero := @linear_ordered_comm_ring.eq_zero_or_eq_zero_of_mul_eq_zero α s } class decidable_linear_ordered_comm_ring (α : Type u) extends linear_ordered_comm_ring α, decidable_linear_ordered_comm_group α instance decidable_linear_ordered_comm_ring.to_decidable_linear_ordered_semiring [d : decidable_linear_ordered_comm_ring α] : decidable_linear_ordered_semiring α := let s : linear_ordered_semiring α := @linear_ordered_ring.to_linear_ordered_semiring α _ in {d with zero_mul := @linear_ordered_semiring.zero_mul α s, mul_zero := @linear_ordered_semiring.mul_zero α s, add_left_cancel := @linear_ordered_semiring.add_left_cancel α s, add_right_cancel := @linear_ordered_semiring.add_right_cancel α s, le_of_add_le_add_left := @linear_ordered_semiring.le_of_add_le_add_left α s, lt_of_add_lt_add_left := @linear_ordered_semiring.lt_of_add_lt_add_left α s, mul_le_mul_of_nonneg_left := @linear_ordered_semiring.mul_le_mul_of_nonneg_left α s, mul_le_mul_of_nonneg_right := @linear_ordered_semiring.mul_le_mul_of_nonneg_right α s, mul_lt_mul_of_pos_left := @linear_ordered_semiring.mul_lt_mul_of_pos_left α s, mul_lt_mul_of_pos_right := @linear_ordered_semiring.mul_lt_mul_of_pos_right α s}
8cd516809b164a0094d56972727f3988a537a4ed
b7f22e51856f4989b970961f794f1c435f9b8f78
/library/data/nat/sub.lean
b850b62085c024d4dcac02ec32d1dfd62f38356e
[ "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
15,944
lean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Jeremy Avigad Subtraction on the natural numbers, as well as min, max, and distance. -/ import .order open eq.ops namespace nat /- subtraction -/ protected theorem sub_zero [simp] (n : ℕ) : n - 0 = n := rfl theorem sub_succ [simp] (n m : ℕ) : n - succ m = pred (n - m) := rfl protected theorem zero_sub [simp] (n : ℕ) : 0 - n = 0 := nat.induction_on n (by simp) (by simp) theorem succ_sub_succ [simp] (n m : ℕ) : succ n - succ m = n - m := succ_sub_succ_eq_sub n m protected theorem sub_self [simp] (n : ℕ) : n - n = 0 := nat.induction_on n (by simp) (by simp) local attribute nat.add_succ [simp] protected theorem add_sub_add_right [simp] (n k m : ℕ) : (n + k) - (m + k) = n - m := nat.induction_on k (by simp) (by simp) protected theorem add_sub_add_left [simp] (k n m : ℕ) : (k + n) - (k + m) = n - m := nat.induction_on k (by simp) (by simp) protected theorem add_sub_cancel [simp] (n m : ℕ) : n + m - m = n := nat.induction_on m (by simp) (by simp) protected theorem add_sub_cancel_left [simp] (n m : ℕ) : n + m - n = m := nat.induction_on n (by simp) (by simp) protected theorem sub_sub [simp] (n m k : ℕ) : (: n - m - k :) = (: n - (m + k) :) := nat.induction_on k (by simp) (by simp) theorem succ_sub_sub_succ [simp] (n m k : ℕ) : succ n - m - succ k = n - m - k := by simp theorem sub_self_add [simp] (n m : ℕ) : n - (n + m) = 0 := by inst_simp protected theorem sub.right_comm (m n k : ℕ) : m - n - k = m - k - n := by simp theorem sub_one (n : ℕ) : n - 1 = pred n := rfl theorem succ_sub_one [simp] (n : ℕ) : succ n - 1 = n := rfl local attribute nat.succ_mul nat.mul_succ [simp] /- interaction with multiplication -/ theorem mul_pred_left [simp] (n m : ℕ) : pred n * m = n * m - m := nat.induction_on n (by simp) (by simp) theorem mul_pred_right [simp] (n m : ℕ) : n * pred m = n * m - n := by inst_simp protected theorem mul_sub_right_distrib [simp] (n m k : ℕ) : (n - m) * k = n * k - m * k := nat.induction_on m (by simp) (by simp) protected theorem mul_sub_left_distrib [simp] (n m k : ℕ) : n * (m - k) = n * m - n * k := by inst_simp protected theorem mul_self_sub_mul_self_eq (a b : nat) : a * a - b * b = (a + b) * (a - b) := by rewrite [nat.mul_sub_left_distrib, *right_distrib, mul.comm b a, add.comm (a*a) (a*b), nat.add_sub_add_left] local attribute succ_eq_add_one right_distrib left_distrib [simp] theorem succ_mul_succ_eq (a : nat) : succ a * succ a = a*a + a + a + 1 := by simp /- interaction with inequalities -/ theorem succ_sub {m n : ℕ} : m ≥ n → succ m - n = succ (m - n) := sub_induction n m (take k, assume H : 0 ≤ k, rfl) (take k, assume H : succ k ≤ 0, absurd H !not_succ_le_zero) (take k l, assume IH : k ≤ l → succ l - k = succ (l - k), take H : succ k ≤ succ l, calc succ (succ l) - succ k = succ l - k : succ_sub_succ ... = succ (l - k) : IH (le_of_succ_le_succ H) ... = succ (succ l - succ k) : succ_sub_succ) theorem sub_eq_zero_of_le {n m : ℕ} (H : n ≤ m) : n - m = 0 := obtain (k : ℕ) (Hk : n + k = m), from le.elim H, Hk ▸ !sub_self_add theorem add_sub_of_le {n m : ℕ} : n ≤ m → n + (m - n) = m := sub_induction n m (take k, assume H : 0 ≤ k, calc 0 + (k - 0) = k - 0 : zero_add ... = k : nat.sub_zero) (take k, assume H : succ k ≤ 0, absurd H !not_succ_le_zero) (take k l, assume IH : k ≤ l → k + (l - k) = l, take H : succ k ≤ succ l, calc succ k + (succ l - succ k) = succ k + (l - k) : succ_sub_succ ... = succ (k + (l - k)) : succ_add ... = succ l : IH (le_of_succ_le_succ H)) theorem add_sub_of_ge {n m : ℕ} (H : n ≥ m) : n + (m - n) = n := calc n + (m - n) = n + 0 : sub_eq_zero_of_le H ... = n : add_zero protected theorem sub_add_cancel {n m : ℕ} : n ≥ m → n - m + m = n := !add.comm ▸ !add_sub_of_le theorem sub_add_of_le {n m : ℕ} : n ≤ m → n - m + m = m := !add.comm ▸ add_sub_of_ge theorem sub.cases {P : ℕ → Prop} {n m : ℕ} (H1 : n ≤ m → P 0) (H2 : ∀k, m + k = n -> P k) : P (n - m) := or.elim !le.total (assume H3 : n ≤ m, (sub_eq_zero_of_le H3)⁻¹ ▸ (H1 H3)) (assume H3 : m ≤ n, H2 (n - m) (add_sub_of_le H3)) theorem exists_sub_eq_of_le {n m : ℕ} (H : n ≤ m) : ∃k, m - k = n := obtain (k : ℕ) (Hk : n + k = m), from le.elim H, exists.intro k (calc m - k = n + k - k : by rewrite Hk ... = n : nat.add_sub_cancel) protected theorem add_sub_assoc {m k : ℕ} (H : k ≤ m) (n : ℕ) : n + m - k = n + (m - k) := have l1 : k ≤ m → n + m - k = n + (m - k), from sub_induction k m (by simp) (take k : ℕ, assume H : succ k ≤ 0, absurd H !not_succ_le_zero) (take k m, assume IH : k ≤ m → n + m - k = n + (m - k), take H : succ k ≤ succ m, calc n + succ m - succ k = succ (n + m) - succ k : add_succ ... = n + m - k : succ_sub_succ ... = n + (m - k) : IH (le_of_succ_le_succ H) ... = n + (succ m - succ k) : succ_sub_succ), l1 H theorem le_of_sub_eq_zero {n m : ℕ} : n - m = 0 → n ≤ m := sub.cases (assume H1 : n ≤ m, assume H2 : 0 = 0, H1) (take k : ℕ, assume H1 : m + k = n, assume H2 : k = 0, have H3 : n = m, from !add_zero ▸ H2 ▸ H1⁻¹, H3 ▸ !le.refl) theorem sub_sub.cases {P : ℕ → ℕ → Prop} {n m : ℕ} (H1 : ∀k, n = m + k -> P k 0) (H2 : ∀k, m = n + k → P 0 k) : P (n - m) (m - n) := or.elim !le.total (assume H3 : n ≤ m, (sub_eq_zero_of_le H3)⁻¹ ▸ (H2 (m - n) (add_sub_of_le H3)⁻¹)) (assume H3 : m ≤ n, (sub_eq_zero_of_le H3)⁻¹ ▸ (H1 (n - m) (add_sub_of_le H3)⁻¹)) protected theorem sub_eq_of_add_eq {n m k : ℕ} (H : n + m = k) : k - n = m := have H2 : k - n + n = m + n, from calc k - n + n = k : nat.sub_add_cancel (le.intro H) ... = n + m : H⁻¹ ... = m + n : !add.comm, add.right_cancel H2 protected theorem eq_sub_of_add_eq {a b c : ℕ} (H : a + c = b) : a = b - c := (nat.sub_eq_of_add_eq (!add.comm ▸ H))⁻¹ protected theorem sub_eq_of_eq_add {a b c : ℕ} (H : a = c + b) : a - b = c := nat.sub_eq_of_add_eq (!add.comm ▸ H⁻¹) protected theorem sub_le_sub_right {n m : ℕ} (H : n ≤ m) (k : ℕ) : n - k ≤ m - k := obtain (l : ℕ) (Hl : n + l = m), from le.elim H, or.elim !le.total (assume H2 : n ≤ k, (sub_eq_zero_of_le H2)⁻¹ ▸ !zero_le) (assume H2 : k ≤ n, have H3 : n - k + l = m - k, from calc n - k + l = l + (n - k) : by simp ... = l + n - k : nat.add_sub_assoc H2 l ... = m - k : by simp, le.intro H3) protected theorem sub_le_sub_left {n m : ℕ} (H : n ≤ m) (k : ℕ) : k - m ≤ k - n := obtain (l : ℕ) (Hl : n + l = m), from le.elim H, sub.cases (assume H2 : k ≤ m, !zero_le) (take m' : ℕ, assume Hm : m + m' = k, have H3 : n ≤ k, from le.trans H (le.intro Hm), have H4 : m' + l + n = k - n + n, by simp, le.intro (add.right_cancel H4)) protected theorem sub_pos_of_lt {m n : ℕ} (H : m < n) : n - m > 0 := have H1 : n = n - m + m, from (nat.sub_add_cancel (le_of_lt H))⁻¹, have H2 : 0 + m < n - m + m, begin rewrite [zero_add, -H1], exact H end, !lt_of_add_lt_add_right H2 protected theorem lt_of_sub_pos {m n : ℕ} (H : n - m > 0) : m < n := lt_of_not_ge (take H1 : m ≥ n, have H2 : n - m = 0, from sub_eq_zero_of_le H1, !lt.irrefl (H2 ▸ H)) protected theorem lt_of_sub_lt_sub_right {n m k : ℕ} (H : n - k < m - k) : n < m := lt_of_not_ge (assume H1 : m ≤ n, have H2 : m - k ≤ n - k, from nat.sub_le_sub_right H1 _, not_le_of_gt H H2) protected theorem lt_of_sub_lt_sub_left {n m k : ℕ} (H : n - m < n - k) : k < m := lt_of_not_ge (assume H1 : m ≤ k, have H2 : n - k ≤ n - m, from nat.sub_le_sub_left H1 _, not_le_of_gt H H2) protected theorem sub_lt_sub_add_sub (n m k : ℕ) : n - k ≤ (n - m) + (m - k) := sub.cases (assume H : n ≤ m, (zero_add (m - k))⁻¹ ▸ nat.sub_le_sub_right H k) (take mn : ℕ, assume Hmn : m + mn = n, sub.cases (assume H : m ≤ k, have H2 : n - k ≤ n - m, from nat.sub_le_sub_left H n, have H3 : n - k ≤ mn, from nat.sub_eq_of_add_eq Hmn ▸ H2, show n - k ≤ mn + 0, begin rewrite add_zero, assumption end) (take km : ℕ, assume Hkm : k + km = m, have H : k + (mn + km) = n, from calc k + (mn + km) = k + km + mn : by simp ... = m + mn : Hkm ... = n : Hmn, have H2 : n - k = mn + km, from nat.sub_eq_of_add_eq H, H2 ▸ !le.refl)) protected theorem sub_lt_self {m n : ℕ} (H1 : m > 0) (H2 : n > 0) : m - n < m := calc m - n = succ (pred m) - n : succ_pred_of_pos H1 ... = succ (pred m) - succ (pred n) : succ_pred_of_pos H2 ... = pred m - pred n : succ_sub_succ ... ≤ pred m : sub_le ... < succ (pred m) : lt_succ_self ... = m : succ_pred_of_pos H1 protected theorem le_sub_of_add_le {m n k : ℕ} (H : m + k ≤ n) : m ≤ n - k := calc m = m + k - k : nat.add_sub_cancel ... ≤ n - k : nat.sub_le_sub_right H k protected theorem lt_sub_of_add_lt {m n k : ℕ} (H : m + k < n) (H2 : k ≤ n) : m < n - k := lt_of_succ_le (nat.le_sub_of_add_le (calc succ m + k = succ (m + k) : succ_add_eq_succ_add ... ≤ n : succ_le_of_lt H)) protected theorem sub_lt_of_lt_add {v n m : nat} (h₁ : v < n + m) (h₂ : n ≤ v) : v - n < m := have succ v ≤ n + m, from succ_le_of_lt h₁, have succ (v - n) ≤ m, from calc succ (v - n) = succ v - n : succ_sub h₂ ... ≤ n + m - n : nat.sub_le_sub_right this n ... = m : nat.add_sub_cancel_left, lt_of_succ_le this /- distance -/ definition dist [reducible] (n m : ℕ) := (n - m) + (m - n) theorem dist.comm (n m : ℕ) : dist n m = dist m n := by simp theorem dist_self (n : ℕ) : dist n n = 0 := by simp theorem eq_of_dist_eq_zero {n m : ℕ} (H : dist n m = 0) : n = m := have H2 : n - m = 0, from eq_zero_of_add_eq_zero_right H, have H3 : n ≤ m, from le_of_sub_eq_zero H2, have H4 : m - n = 0, from eq_zero_of_add_eq_zero_left H, have H5 : m ≤ n, from le_of_sub_eq_zero H4, le.antisymm H3 H5 theorem dist_eq_zero {n m : ℕ} (H : n = m) : dist n m = 0 := by substvars; rewrite [↑dist, *nat.sub_self, add_zero] theorem dist_eq_sub_of_le {n m : ℕ} (H : n ≤ m) : dist n m = m - n := calc dist n m = 0 + (m - n) : {sub_eq_zero_of_le H} ... = m - n : zero_add theorem dist_eq_sub_of_lt {n m : ℕ} (H : n < m) : dist n m = m - n := dist_eq_sub_of_le (le_of_lt H) theorem dist_eq_sub_of_ge {n m : ℕ} (H : n ≥ m) : dist n m = n - m := !dist.comm ▸ dist_eq_sub_of_le H theorem dist_eq_sub_of_gt {n m : ℕ} (H : n > m) : dist n m = n - m := dist_eq_sub_of_ge (le_of_lt H) theorem dist_zero_right (n : ℕ) : dist n 0 = n := dist_eq_sub_of_ge !zero_le ⬝ !nat.sub_zero theorem dist_zero_left (n : ℕ) : dist 0 n = n := dist_eq_sub_of_le !zero_le ⬝ !nat.sub_zero theorem dist.intro {n m k : ℕ} (H : n + m = k) : dist k n = m := calc dist k n = k - n : dist_eq_sub_of_ge (le.intro H) ... = m : nat.sub_eq_of_add_eq H theorem dist_add_add_right (n k m : ℕ) : dist (n + k) (m + k) = dist n m := calc dist (n + k) (m + k) = ((n+k) - (m+k)) + ((m+k)-(n+k)) : rfl ... = (n - m) + ((m + k) - (n + k)) : nat.add_sub_add_right ... = (n - m) + (m - n) : nat.add_sub_add_right theorem dist_add_add_left (k n m : ℕ) : dist (k + n) (k + m) = dist n m := begin rewrite [add.comm k n, add.comm k m]; apply dist_add_add_right end theorem dist_add_eq_of_ge {n m : ℕ} (H : n ≥ m) : dist n m + m = n := calc dist n m + m = n - m + m : {dist_eq_sub_of_ge H} ... = n : nat.sub_add_cancel H theorem dist_eq_intro {n m k l : ℕ} (H : n + m = k + l) : dist n k = dist l m := calc dist n k = dist (n + m) (k + m) : dist_add_add_right ... = dist (k + l) (k + m) : H ... = dist l m : dist_add_add_left theorem dist_sub_eq_dist_add_left {n m : ℕ} (H : n ≥ m) (k : ℕ) : dist (n - m) k = dist n (k + m) := have H2 : n - m + (k + m) = k + n, from calc n - m + (k + m) = n - m + m + k : by simp ... = n + k : nat.sub_add_cancel H ... = k + n : by simp, dist_eq_intro H2 theorem dist_sub_eq_dist_add_right {k m : ℕ} (H : k ≥ m) (n : ℕ) : dist n (k - m) = dist (n + m) k := dist.comm (k - m) n ▸ dist.comm k (n + m) ▸ dist_sub_eq_dist_add_left H n theorem dist.triangle_inequality (n m k : ℕ) : dist n k ≤ dist n m + dist m k := have (n - m) + (m - k) + ((k - m) + (m - n)) = (n - m) + (m - n) + ((m - k) + (k - m)), by simp, this ▸ add_le_add !nat.sub_lt_sub_add_sub !nat.sub_lt_sub_add_sub theorem dist_add_add_le_add_dist_dist (n m k l : ℕ) : dist (n + m) (k + l) ≤ dist n k + dist m l := have H : dist (n + m) (k + m) + dist (k + m) (k + l) = dist n k + dist m l, by rewrite [dist_add_add_left, dist_add_add_right], by rewrite -H; apply dist.triangle_inequality theorem dist_mul_right (n k m : ℕ) : dist (n * k) (m * k) = dist n m * k := have ∀ n m, dist n m = n - m + (m - n), from take n m, rfl, by rewrite [this, this n m, right_distrib, *nat.mul_sub_right_distrib] theorem dist_mul_left (k n m : ℕ) : dist (k * n) (k * m) = k * dist n m := begin rewrite [mul.comm k n, mul.comm k m, dist_mul_right, mul.comm] end theorem dist_mul_dist (n m k l : ℕ) : dist n m * dist k l = dist (n * k + m * l) (n * l + m * k) := have aux : ∀k l, k ≥ l → dist n m * dist k l = dist (n * k + m * l) (n * l + m * k), from take k l : ℕ, assume H : k ≥ l, have H2 : m * k ≥ m * l, from !mul_le_mul_left H, have H3 : n * l + m * k ≥ m * l, from le.trans H2 !le_add_left, calc dist n m * dist k l = dist n m * (k - l) : dist_eq_sub_of_ge H ... = dist (n * (k - l)) (m * (k - l)) : dist_mul_right ... = dist (n * k - n * l) (m * k - m * l) : by rewrite [*nat.mul_sub_left_distrib] ... = dist (n * k) (m * k - m * l + n * l) : dist_sub_eq_dist_add_left (!mul_le_mul_left H) ... = dist (n * k) (n * l + (m * k - m * l)) : add.comm ... = dist (n * k) (n * l + m * k - m * l) : nat.add_sub_assoc H2 (n * l) ... = dist (n * k + m * l) (n * l + m * k) : dist_sub_eq_dist_add_right H3 _, or.elim !le.total (assume H : k ≤ l, !dist.comm ▸ !dist.comm ▸ aux l k H) (assume H : l ≤ k, aux k l H) lemma dist_eq_max_sub_min {i j : nat} : dist i j = (max i j) - min i j := or.elim (lt_or_ge i j) (suppose i < j, by rewrite [max_eq_right_of_lt this, min_eq_left_of_lt this, dist_eq_sub_of_lt this]) (suppose i ≥ j, by rewrite [max_eq_left this , min_eq_right this, dist_eq_sub_of_ge this]) lemma dist_succ {i j : nat} : dist (succ i) (succ j) = dist i j := by rewrite [↑dist, *succ_sub_succ] lemma dist_le_max {i j : nat} : dist i j ≤ max i j := begin rewrite dist_eq_max_sub_min, apply sub_le end lemma dist_pos_of_ne {i j : nat} : i ≠ j → dist i j > 0 := assume Pne, lt.by_cases (suppose i < j, begin rewrite [dist_eq_sub_of_lt this], apply nat.sub_pos_of_lt this end) (suppose i = j, by contradiction) (suppose i > j, begin rewrite [dist_eq_sub_of_gt this], apply nat.sub_pos_of_lt this end) end nat
4213a9fcc59d23a43af5b95a8f8972c0c25fdc0c
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/specialize.lean
c8f65f76692b59439ccfdecbf819d478ada95601
[ "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
617
lean
open tactic lemma test1 (x y z : Prop) (f : x → y → z) (xp : x) (yp : y) : z := begin specialize (f xp yp), assumption end lemma test2 (B C : Prop) (f : forall (A : Prop), A → C) (x : B) : C := begin specialize f _ x, exact f, end lemma test3 (B C : Prop) (f : forall {A : Prop}, A → C) (x : B) : C := begin specialize (f x), exact f, end lemma test4 (B C : Prop) (f : forall {A : Prop}, A → C) (x : B) : C := begin specialize (@f _ x), exact f, end def test5 (X : Type) [has_add X] (f : forall {A : Type} [has_add A], A → A → A) (x : X) : X := begin specialize (f x x), assumption end
40105eb3c4b6d9c20503bb479060f563bfcc0c54
22e97a5d648fc451e25a06c668dc03ac7ed7bc25
/src/topology/algebra/multilinear.lean
8288ac095d1d4b21da0083902b845b7a3fcc0c5d
[ "Apache-2.0" ]
permissive
keeferrowan/mathlib
f2818da875dbc7780830d09bd4c526b0764a4e50
aad2dfc40e8e6a7e258287a7c1580318e865817e
refs/heads/master
1,661,736,426,952
1,590,438,032,000
1,590,438,032,000
266,892,663
0
0
Apache-2.0
1,590,445,835,000
1,590,445,835,000
null
UTF-8
Lean
false
false
11,888
lean
/- Copyright (c) 2020 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import topology.algebra.module import linear_algebra.multilinear /-! # Continuous multilinear maps We define continuous multilinear maps as maps from `Π(i : ι), M₁ i` to `M₂` which are multilinear and continuous, by extending the space of multilinear maps with a continuity assumption. Here, `M₁ i` and `M₂` are modules over a ring `R`, and `ι` is an arbitrary type, and all these spaces are also topological spaces. ## Main definitions * `continuous_multilinear_map R M₁ M₂` is the space of continuous multilinear maps from `Π(i : ι), M₁ i` to `M₂`. We show that it is an `R`-module. ## Implementation notes We mostly follow the API of multilinear maps. ## Notation We introduce the notation `M [×n]→L[R] M'` for the space of continuous `n`-multilinear maps from `M^n` to `M'`. This is a particular case of the general notion (where we allow varying dependent types as the arguments of our continuous multilinear maps), but arguably the most important one, especially when defining iterated derivatives. -/ open function fin set universes u v w w₁ w₂ w₃ w₄ variables {R : Type u} {ι : Type v} {n : ℕ} {M : fin n.succ → Type w} {M₁ : ι → Type w₁} {M₂ : Type w₂} {M₃ : Type w₃} {M₄ : Type w₄} [decidable_eq ι] /-- Continuous multilinear maps over the ring `R`, from `Πi, M₁ i` to `M₂` where `M₁ i` and `M₂` are modules over `R` with a topological structure. In applications, there will be compatibility conditions between the algebraic and the topological structures, but this is not needed for the definition. -/ structure continuous_multilinear_map (R : Type u) {ι : Type v} (M₁ : ι → Type w₁) (M₂ : Type w₂) [decidable_eq ι] [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, module R (M₁ i)] [module R M₂] [∀i, topological_space (M₁ i)] [topological_space M₂] extends multilinear_map R M₁ M₂ := (cont : continuous to_fun) notation M `[×`:25 n `]→L[`:25 R `] ` M' := continuous_multilinear_map R (λ (i : fin n), M) M' namespace continuous_multilinear_map section ring variables [ring R] [∀i, add_comm_group (M i)] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [add_comm_group M₃] [add_comm_group M₄] [∀i, module R (M i)] [∀i, module R (M₁ i)] [module R M₂] [module R M₃] [module R M₄] [∀i, topological_space (M i)] [∀i, topological_space (M₁ i)] [topological_space M₂] [topological_space M₃] [topological_space M₄] (f f' : continuous_multilinear_map R M₁ M₂) instance : has_coe_to_fun (continuous_multilinear_map R M₁ M₂) := ⟨_, λ f, f.to_multilinear_map.to_fun⟩ @[ext] theorem ext {f f' : continuous_multilinear_map R M₁ M₂} (H : ∀ x, f x = f' x) : f = f' := by { cases f, cases f', congr, ext x, exact H x } @[simp] lemma map_add (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x + y)) = f (update m i x) + f (update m i y) := f.add m i x y @[simp] lemma map_smul (m : Πi, M₁ i) (i : ι) (c : R) (x : M₁ i) : f (update m i (c • x)) = c • f (update m i x) := f.smul m i c x @[simp] lemma map_sub (m : Πi, M₁ i) (i : ι) (x y : M₁ i) : f (update m i (x - y)) = f (update m i x) - f (update m i y) := f.to_multilinear_map.map_sub _ _ _ _ lemma map_coord_zero {m : Πi, M₁ i} (i : ι) (h : m i = 0) : f m = 0 := f.to_multilinear_map.map_coord_zero i h @[simp] lemma map_zero [nonempty ι] : f 0 = 0 := f.to_multilinear_map.map_zero instance : has_zero (continuous_multilinear_map R M₁ M₂) := ⟨{ cont := continuous_const, ..(0 : multilinear_map R M₁ M₂) }⟩ instance : inhabited (continuous_multilinear_map R M₁ M₂) := ⟨0⟩ @[simp] lemma zero_apply (m : Πi, M₁ i) : (0 : continuous_multilinear_map R M₁ M₂) m = 0 := rfl section topological_add_group variable [topological_add_group M₂] instance : has_add (continuous_multilinear_map R M₁ M₂) := ⟨λ f f', {cont := f.cont.add f'.cont, ..(f.to_multilinear_map + f'.to_multilinear_map)}⟩ @[simp] lemma add_apply (m : Πi, M₁ i) : (f + f') m = f m + f' m := rfl instance : has_neg (continuous_multilinear_map R M₁ M₂) := ⟨λ f, {cont := f.cont.neg, ..(-f.to_multilinear_map)}⟩ @[simp] lemma neg_apply (m : Πi, M₁ i) : (-f) m = - (f m) := rfl instance : add_comm_group (continuous_multilinear_map R M₁ M₂) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp [add_comm, add_left_comm] @[simp] lemma sub_apply (m : Πi, M₁ i) : (f - f') m = f m - f' m := rfl @[simp] lemma sum_apply {α : Type*} (f : α → continuous_multilinear_map R M₁ M₂) (m : Πi, M₁ i) : ∀ {s : finset α}, (s.sum f) m = s.sum (λ a, f a m) := begin classical, apply finset.induction, { rw finset.sum_empty, simp }, { assume a s has H, rw finset.sum_insert has, simp [H, has] } end end topological_add_group /-- If `f` is a continuous multilinear map, then `f.to_continuous_linear_map m i` is the continuous linear map obtained by fixing all coordinates but `i` equal to those of `m`, and varying the `i`-th coordinate. -/ def to_continuous_linear_map (m : Πi, M₁ i) (i : ι) : M₁ i →L[R] M₂ := { cont := f.cont.comp continuous_update, ..(f.to_multilinear_map.to_linear_map m i) } /-- The cartesian product of two continuous multilinear maps, as a continuous multilinear map. -/ def prod (f : continuous_multilinear_map R M₁ M₂) (g : continuous_multilinear_map R M₁ M₃) : continuous_multilinear_map R M₁ (M₂ × M₃) := { cont := f.cont.prod_mk g.cont, .. f.to_multilinear_map.prod g.to_multilinear_map } @[simp] lemma prod_apply (f : continuous_multilinear_map R M₁ M₂) (g : continuous_multilinear_map R M₁ M₃) (m : Πi, M₁ i) : (f.prod g) m = (f m, g m) := rfl /- If `R` and `M₃` are implicit in the next definition, Lean is never able to inder them, even given `g` and `f`. Therefore, we make them explicit. -/ variables (R M₃) /-- If `g` is continuous multilinear and `f` is continuous linear, then `g (f m₁, ..., f mₙ)` is again a continuous multilinear map, that we call `g.comp_continuous_linear_map f`. -/ def comp_continuous_linear_map (g : continuous_multilinear_map R (λ (i : ι), M₃) M₄) (f : M₂ →L[R] M₃) : continuous_multilinear_map R (λ (i : ι), M₂) M₄ := { cont := g.cont.comp $ continuous_pi $ λj, f.cont.comp $ continuous_apply _, .. g.to_multilinear_map.comp_linear_map f.to_linear_map } variables {R M₃} /-- In the specific case of continuous multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the additivity of a multilinear map along the first variable. -/ lemma cons_add (f : continuous_multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (x y : M 0) : f (cons (x+y) m) = f (cons x m) + f (cons y m) := f.to_multilinear_map.cons_add m x y /-- In the specific case of continuous multilinear maps on spaces indexed by `fin (n+1)`, where one can build an element of `Π(i : fin (n+1)), M i` using `cons`, one can express directly the multiplicativity of a multilinear map along the first variable. -/ lemma cons_smul (f : continuous_multilinear_map R M M₂) (m : Π(i : fin n), M i.succ) (c : R) (x : M 0) : f (cons (c • x) m) = c • f (cons x m) := f.to_multilinear_map.cons_smul m c x lemma map_piecewise_add (m m' : Πi, M₁ i) (t : finset ι) : f (t.piecewise (m + m') m') = t.powerset.sum (λ s, f (s.piecewise m m')) := f.to_multilinear_map.map_piecewise_add _ _ _ /-- Additivity of a continuous multilinear map along all coordinates at the same time, writing `f (m + m')` as the sum of `f (s.piecewise m m')` over all sets `s`. -/ lemma map_add_univ [fintype ι] (m m' : Πi, M₁ i) : f (m + m') = (finset.univ : finset (finset ι)).sum (λ s, f (s.piecewise m m')) := f.to_multilinear_map.map_add_univ _ _ section apply_sum open fintype finset variables {α : ι → Type*} [fintype ι] (g : Π i, α i → M₁ i) (A : Π i, finset (α i)) /-- If `f` is continuous multilinear, then `f (Σ_{j₁ ∈ A₁} g₁ j₁, ..., Σ_{jₙ ∈ Aₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions with `r 1 ∈ A₁`, ..., `r n ∈ Aₙ`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum_finset : f (λ i, (A i).sum (g i)) = (pi_finset A).sum (λ r, f (λ i, g i (r i))) := f.to_multilinear_map.map_sum_finset _ _ /-- If `f` is continuous multilinear, then `f (Σ_{j₁} g₁ j₁, ..., Σ_{jₙ} gₙ jₙ)` is the sum of `f (g₁ (r 1), ..., gₙ (r n))` where `r` ranges over all functions `r`. This follows from multilinearity by expanding successively with respect to each coordinate. -/ lemma map_sum [∀ i, fintype (α i)] : f (λ i, finset.univ.sum (g i)) = finset.univ.sum (λ (r : Π i, α i), f (λ i, g i (r i))) := f.to_multilinear_map.map_sum _ end apply_sum end ring section comm_ring variables [comm_ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [∀i, module R (M₁ i)] [module R M₂] [∀i, topological_space (M₁ i)] [topological_space M₂] (f : continuous_multilinear_map R M₁ M₂) lemma map_piecewise_smul (c : ι → R) (m : Πi, M₁ i) (s : finset ι) : f (s.piecewise (λ i, c i • m i) m) = s.prod c • f m := f.to_multilinear_map.map_piecewise_smul _ _ _ /-- Multiplicativity of a continuous multilinear map along all coordinates at the same time, writing `f (λ i, c i • m i)` as `univ.prod c • f m`. -/ lemma map_smul_univ [fintype ι] (c : ι → R) (m : Πi, M₁ i) : f (λ i, c i • m i) = finset.univ.prod c • f m := f.to_multilinear_map.map_smul_univ _ _ variables [topological_space R] [topological_module R M₂] instance : has_scalar R (continuous_multilinear_map R M₁ M₂) := ⟨λ c f, { cont := continuous.smul continuous_const f.cont, .. c • f.to_multilinear_map }⟩ @[simp] lemma smul_apply (c : R) (m : Πi, M₁ i) : (c • f) m = c • f m := rfl /-- The space of continuous multilinear maps is a module over `R`, for the pointwise addition and scalar multiplication. -/ instance [topological_add_group M₂] : module R (continuous_multilinear_map R M₁ M₂) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] /-- Linear map version of the map `to_multilinear_map` associating to a continuous multilinear map the corresponding multilinear map. -/ def to_multilinear_map_linear [topological_add_group M₂] : (continuous_multilinear_map R M₁ M₂) →ₗ[R] (multilinear_map R M₁ M₂) := { to_fun := λ f, f.to_multilinear_map, add := λ f g, rfl, smul := λ c f, rfl } end comm_ring end continuous_multilinear_map namespace continuous_linear_map variables [ring R] [∀i, add_comm_group (M₁ i)] [add_comm_group M₂] [add_comm_group M₃] [∀i, module R (M₁ i)] [module R M₂] [module R M₃] [∀i, topological_space (M₁ i)] [topological_space M₂] [topological_space M₃] /-- Composing a continuous multilinear map with a continuous linear map gives again a continuous multilinear map. -/ def comp_continuous_multilinear_map (g : M₂ →L[R] M₃) (f : continuous_multilinear_map R M₁ M₂) : continuous_multilinear_map R M₁ M₃ := { cont := g.cont.comp f.cont, .. g.to_linear_map.comp_multilinear_map f.to_multilinear_map } @[simp] lemma comp_continuous_multilinear_map_coe (g : M₂ →L[R] M₃) (f : continuous_multilinear_map R M₁ M₂) : ((g.comp_continuous_multilinear_map f) : (Πi, M₁ i) → M₃) = (g : M₂ → M₃) ∘ (f : (Πi, M₁ i) → M₂) := by { ext m, refl } end continuous_linear_map
6385861fc43c1c3bd0c4cba73ef48aa601bc2d37
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/inline_bug.lean
16701507462f520c30f41d79d6c93aeb214524a0
[ "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
98
lean
@[inline] def o (n : ℕ) := prod.mk n n set_option trace.compiler.inline true def f := (o 1).fst
b36aee42dd64484b97aa5e0c80b62c68bd528fdb
491068d2ad28831e7dade8d6dff871c3e49d9431
/library/theories/analysis/real_limit.lean
c11c7f6a5df204e43c6201cc075d58b8f520dfe0
[ "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
33,937
lean
/- Copyright (c) 2015 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Robert Y. Lewis Instantiates the reals as a metric space, and expresses completeness, sup, and inf in a manner that is less constructive, but more convenient, than the way it is done in data.real.complete. The definitions here are noncomputable, for various reasons: (1) We rely on the nonconstructive definition of abs. (2) The theory of the reals uses the "some" operator e.g. to define the ceiling function. This can't be defined constructively as an operation on the quotient, because such a function is not continuous. (3) We use "forall" and "exists" to say that a series converges, rather than carrying around rates of convergence explicitly. We then use "some" whenever we need to extract information, such as the limit. These could be avoided in a constructive theory of analysis, but here we will not follow that route. -/ import .metric_space data.real.complete open real eq.ops classical noncomputable theory namespace real /- the reals form a metric space -/ protected definition to_metric_space [instance] : metric_space ℝ := ⦃ metric_space, dist := λ x y, abs (x - y), dist_self := λ x, abstract by rewrite [sub_self, abs_zero] end, eq_of_dist_eq_zero := @eq_of_abs_sub_eq_zero, dist_comm := abs_sub, dist_triangle := abs_sub_le ⦄ open nat definition converges_to_seq (X : ℕ → ℝ) (y : ℝ) : Prop := ∀ ⦃ε : ℝ⦄, ε > 0 → ∃ N : ℕ, ∀ {n}, n ≥ N → abs (X n - y) < ε proposition converges_to_seq.intro {X : ℕ → ℝ} {y : ℝ} (H : ∀ ⦃ε : ℝ⦄, ε > 0 → ∃ N : ℕ, ∀ {n}, n ≥ N → abs (X n - y) ≤ ε) : converges_to_seq X y := metric_space.converges_to_seq.intro H notation X `⟶` y `in` `ℕ` := converges_to_seq X y definition converges_seq [class] (X : ℕ → ℝ) : Prop := ∃ y, X ⟶ y in ℕ definition limit_seq (X : ℕ → ℝ) [H : converges_seq X] : ℝ := some H proposition converges_to_limit_seq (X : ℕ → ℝ) [H : converges_seq X] : (X ⟶ limit_seq X in ℕ) := some_spec H proposition converges_to_seq_unique {X : ℕ → ℝ} {y₁ y₂ : ℝ} (H₁ : X ⟶ y₁ in ℕ) (H₂ : X ⟶ y₂ in ℕ) : y₁ = y₂ := metric_space.converges_to_seq_unique H₁ H₂ proposition eq_limit_of_converges_to_seq {X : ℕ → ℝ} (y : ℝ) (H : X ⟶ y in ℕ) : y = @limit_seq X (exists.intro y H) := converges_to_seq_unique H (@converges_to_limit_seq X (exists.intro y H)) proposition converges_to_seq_constant (y : ℝ) : (λn, y) ⟶ y in ℕ := metric_space.converges_to_seq_constant y proposition converges_to_seq_offset {X : ℕ → ℝ} {y : ℝ} (k : ℕ) (H : X ⟶ y in ℕ) : (λ n, X (n + k)) ⟶ y in ℕ := metric_space.converges_to_seq_offset k H proposition converges_to_seq_offset_left {X : ℕ → ℝ} {y : ℝ} (k : ℕ) (H : X ⟶ y in ℕ) : (λ n, X (k + n)) ⟶ y in ℕ := metric_space.converges_to_seq_offset_left k H proposition converges_to_set_offset_succ {X : ℕ → ℝ} {y : ℝ} (H : X ⟶ y in ℕ) : (λ n, X (succ n)) ⟶ y in ℕ := metric_space.converges_to_seq_offset_succ H proposition converges_to_seq_of_converges_to_seq_offset {X : ℕ → ℝ} {y : ℝ} {k : ℕ} (H : (λ n, X (n + k)) ⟶ y in ℕ) : X ⟶ y in ℕ := metric_space.converges_to_seq_of_converges_to_seq_offset H proposition converges_to_seq_of_converges_to_seq_offset_left {X : ℕ → ℝ} {y : ℝ} {k : ℕ} (H : (λ n, X (k + n)) ⟶ y in ℕ) : X ⟶ y in ℕ := metric_space.converges_to_seq_of_converges_to_seq_offset_left H proposition converges_to_seq_of_converges_to_seq_offset_succ {X : ℕ → ℝ} {y : ℝ} (H : (λ n, X (succ n)) ⟶ y in ℕ) : X ⟶ y in ℕ := metric_space.converges_to_seq_of_converges_to_seq_offset_succ H proposition converges_to_seq_offset_iff (X : ℕ → ℝ) (y : ℝ) (k : ℕ) : ((λ n, X (n + k)) ⟶ y in ℕ) ↔ (X ⟶ y in ℕ) := metric_space.converges_to_seq_offset_iff X y k proposition converges_to_seq_offset_left_iff (X : ℕ → ℝ) (y : ℝ) (k : ℕ) : ((λ n, X (k + n)) ⟶ y in ℕ) ↔ (X ⟶ y in ℕ) := metric_space.converges_to_seq_offset_left_iff X y k proposition converges_to_seq_offset_succ_iff (X : ℕ → ℝ) (y : ℝ) : ((λ n, X (succ n)) ⟶ y in ℕ) ↔ (X ⟶ y in ℕ) := metric_space.converges_to_seq_offset_succ_iff X y /- the completeness of the reals, "translated" from data.real.complete -/ definition cauchy (X : ℕ → ℝ) := metric_space.cauchy X section open pnat subtype private definition pnat.succ (n : ℕ) : ℕ+ := tag (succ n) !succ_pos private definition r_seq_of (X : ℕ → ℝ) : r_seq := λ n, X (elt_of n) private lemma rate_of_cauchy_aux {X : ℕ → ℝ} (H : cauchy X) : ∀ k : ℕ+, ∃ N : ℕ+, ∀ m n : ℕ+, m ≥ N → n ≥ N → abs (X (elt_of m) - X (elt_of n)) ≤ of_rat k⁻¹ := take k : ℕ+, have H1 : (rat.gt k⁻¹ (rat.of_num 0)), from !inv_pos, have H2 : (of_rat k⁻¹ > of_rat (rat.of_num 0)), from !of_rat_lt_of_rat_of_lt H1, obtain (N : ℕ) (H : ∀ m n, m ≥ N → n ≥ N → abs (X m - X n) < of_rat k⁻¹), from H _ H2, exists.intro (pnat.succ N) (take m n : ℕ+, assume Hm : m ≥ (pnat.succ N), assume Hn : n ≥ (pnat.succ N), have Hm' : elt_of m ≥ N, from nat.le.trans !le_succ Hm, have Hn' : elt_of n ≥ N, from nat.le.trans !le_succ Hn, show abs (X (elt_of m) - X (elt_of n)) ≤ of_rat k⁻¹, from le_of_lt (H _ _ Hm' Hn')) private definition rate_of_cauchy {X : ℕ → ℝ} (H : cauchy X) (k : ℕ+) : ℕ+ := some (rate_of_cauchy_aux H k) private lemma cauchy_with_rate_of_cauchy {X : ℕ → ℝ} (H : cauchy X) : cauchy_with_rate (r_seq_of X) (rate_of_cauchy H) := take k : ℕ+, some_spec (rate_of_cauchy_aux H k) private lemma converges_to_with_rate_of_cauchy {X : ℕ → ℝ} (H : cauchy X) : ∃ l Nb, converges_to_with_rate (r_seq_of X) l Nb := begin apply exists.intro, apply exists.intro, apply converges_to_with_rate_of_cauchy_with_rate, exact cauchy_with_rate_of_cauchy H end theorem converges_seq_of_cauchy {X : ℕ → ℝ} (H : cauchy X) : converges_seq X := obtain l Nb (conv : converges_to_with_rate (r_seq_of X) l Nb), from converges_to_with_rate_of_cauchy H, exists.intro l (take ε : ℝ, suppose ε > 0, obtain (k' : ℕ) (Hn : 1 / succ k' < ε), from archimedean_small `ε > 0`, let k : ℕ+ := tag (succ k') !succ_pos, N : ℕ+ := Nb k in have Hk : real.of_rat k⁻¹ < ε, by rewrite [↑pnat.inv, of_rat_divide]; exact Hn, exists.intro (elt_of N) (take n : ℕ, assume Hn : n ≥ elt_of N, let n' : ℕ+ := tag n (nat.lt_of_lt_of_le (has_property N) Hn) in have abs (X n - l) ≤ real.of_rat k⁻¹, from conv k n' Hn, show abs (X n - l) < ε, from lt_of_le_of_lt this Hk)) open set private definition exists_is_sup {X : set ℝ} (H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → x ≤ b)) : ∃ y, is_sup X y := let x := some (and.left H), b := some (and.right H) in exists_is_sup_of_inh_of_bdd X x (some_spec (and.left H)) b (some_spec (and.right H)) private definition sup_aux {X : set ℝ} (H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → x ≤ b)) := some (exists_is_sup H) private definition sup_aux_spec {X : set ℝ} (H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → x ≤ b)) : is_sup X (sup_aux H) := some_spec (exists_is_sup H) definition sup (X : set ℝ) : ℝ := if H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → x ≤ b) then sup_aux H else 0 proposition le_sup {x : ℝ} {X : set ℝ} (Hx : x ∈ X) {b : ℝ} (Hb : ∀ x, x ∈ X → x ≤ b) : x ≤ sup X := have H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → x ≤ b), from and.intro (exists.intro x Hx) (exists.intro b Hb), by+ rewrite [↑sup, dif_pos H]; exact and.left (sup_aux_spec H) x Hx proposition sup_le {X : set ℝ} (HX : ∃ x, x ∈ X) {b : ℝ} (Hb : ∀ x, x ∈ X → x ≤ b) : sup X ≤ b := have H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → x ≤ b), from and.intro HX (exists.intro b Hb), by+ rewrite [↑sup, dif_pos H]; exact and.right (sup_aux_spec H) b Hb proposition exists_mem_and_lt_of_lt_sup {X : set ℝ} (HX : ∃ x, x ∈ X) {b : ℝ} (Hb : b < sup X) : ∃ x, x ∈ X ∧ b < x := have ¬ ∀ x, x ∈ X → x ≤ b, from assume H, not_le_of_gt Hb (sup_le HX H), obtain x (Hx : ¬ (x ∈ X → x ≤ b)), from exists_not_of_not_forall this, exists.intro x (have x ∈ X ∧ ¬ x ≤ b, by rewrite [-not_implies_iff_and_not]; apply Hx, and.intro (and.left this) (lt_of_not_ge (and.right this))) private definition exists_is_inf {X : set ℝ} (H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → b ≤ x)) : ∃ y, is_inf X y := let x := some (and.left H), b := some (and.right H) in exists_is_inf_of_inh_of_bdd X x (some_spec (and.left H)) b (some_spec (and.right H)) private definition inf_aux {X : set ℝ} (H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → b ≤ x)) := some (exists_is_inf H) private definition inf_aux_spec {X : set ℝ} (H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → b ≤ x)) : is_inf X (inf_aux H) := some_spec (exists_is_inf H) definition inf (X : set ℝ) : ℝ := if H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → b ≤ x) then inf_aux H else 0 proposition inf_le {x : ℝ} {X : set ℝ} (Hx : x ∈ X) {b : ℝ} (Hb : ∀ x, x ∈ X → b ≤ x) : inf X ≤ x := have H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → b ≤ x), from and.intro (exists.intro x Hx) (exists.intro b Hb), by+ rewrite [↑inf, dif_pos H]; exact and.left (inf_aux_spec H) x Hx proposition le_inf {X : set ℝ} (HX : ∃ x, x ∈ X) {b : ℝ} (Hb : ∀ x, x ∈ X → b ≤ x) : b ≤ inf X := have H : (∃ x, x ∈ X) ∧ (∃ b, ∀ x, x ∈ X → b ≤ x), from and.intro HX (exists.intro b Hb), by+ rewrite [↑inf, dif_pos H]; exact and.right (inf_aux_spec H) b Hb proposition exists_mem_and_lt_of_inf_lt {X : set ℝ} (HX : ∃ x, x ∈ X) {b : ℝ} (Hb : inf X < b) : ∃ x, x ∈ X ∧ x < b := have ¬ ∀ x, x ∈ X → b ≤ x, from assume H, not_le_of_gt Hb (le_inf HX H), obtain x (Hx : ¬ (x ∈ X → b ≤ x)), from exists_not_of_not_forall this, exists.intro x (have x ∈ X ∧ ¬ b ≤ x, by rewrite [-not_implies_iff_and_not]; apply Hx, and.intro (and.left this) (lt_of_not_ge (and.right this))) -- TODO: is there a better place to put this? proposition image_neg_eq (X : set ℝ) : (λ x, -x) '[X] = {x | -x ∈ X} := set.ext (take x, iff.intro (assume H, obtain y [(Hy₁ : y ∈ X) (Hy₂ : -y = x)], from H, show -x ∈ X, by rewrite [-Hy₂, neg_neg]; exact Hy₁) (assume H : -x ∈ X, exists.intro (-x) (and.intro H !neg_neg))) proposition sup_neg {X : set ℝ} (nonempty_X : ∃ x, x ∈ X) {b : ℝ} (Hb : ∀ x, x ∈ X → b ≤ x) : sup {x | -x ∈ X} = - inf X := let negX := {x | -x ∈ X} in have nonempty_negX : ∃ x, x ∈ negX, from obtain x Hx, from nonempty_X, have -(-x) ∈ X, by rewrite neg_neg; apply Hx, exists.intro (-x) this, have H₁ : ∀ x, x ∈ negX → x ≤ - inf X, from take x, assume H, have inf X ≤ -x, from inf_le H Hb, show x ≤ - inf X, from le_neg_of_le_neg this, have H₂ : ∀ x, x ∈ X → -sup negX ≤ x, from take x, assume H, have -(-x) ∈ X, by rewrite neg_neg; apply H, have -x ≤ sup negX, from le_sup this H₁, show -sup negX ≤ x, from !neg_le_of_neg_le this, eq_of_le_of_ge (show sup negX ≤ - inf X, from sup_le nonempty_negX H₁) (show -inf X ≤ sup negX, from !neg_le_of_neg_le (le_inf nonempty_X H₂)) proposition inf_neg {X : set ℝ} (nonempty_X : ∃ x, x ∈ X) {b : ℝ} (Hb : ∀ x, x ∈ X → x ≤ b) : inf {x | -x ∈ X} = - sup X := let negX := {x | -x ∈ X} in have nonempty_negX : ∃ x, x ∈ negX, from obtain x Hx, from nonempty_X, have -(-x) ∈ X, by rewrite neg_neg; apply Hx, exists.intro (-x) this, have Hb' : ∀ x, x ∈ negX → -b ≤ x, from take x, assume H, !neg_le_of_neg_le (Hb _ H), have HX : X = {x | -x ∈ negX}, from set.ext (take x, by rewrite [↑set_of, ↑mem, +neg_neg]), show inf {x | -x ∈ X} = - sup X, using HX Hb' nonempty_negX, by rewrite [HX at {2}, sup_neg nonempty_negX Hb', neg_neg] end /- limits under pointwise operations -/ section limit_operations open nat variables {X Y : ℕ → ℝ} variables {x y : ℝ} proposition add_converges_to_seq (HX : X ⟶ x in ℕ) (HY : Y ⟶ y in ℕ) : (λ n, X n + Y n) ⟶ x + y in ℕ := take ε, suppose ε > 0, have e2pos : ε / 2 > 0, from div_pos_of_pos_of_pos `ε > 0` two_pos, obtain N₁ (HN₁ : ∀ {n}, n ≥ N₁ → abs (X n - x) < ε / 2), from HX e2pos, obtain N₂ (HN₂ : ∀ {n}, n ≥ N₂ → abs (Y n - y) < ε / 2), from HY e2pos, let N := nat.max N₁ N₂ in exists.intro N (take n, suppose n ≥ N, have ngtN₁ : n ≥ N₁, from nat.le.trans !nat.le_max_left `n ≥ N`, have ngtN₂ : n ≥ N₂, from nat.le.trans !nat.le_max_right `n ≥ N`, show abs ((X n + Y n) - (x + y)) < ε, from calc abs ((X n + Y n) - (x + y)) = abs ((X n - x) + (Y n - y)) : by rewrite [sub_add_eq_sub_sub, *sub_eq_add_neg, *add.assoc, add.left_comm (-x)] ... ≤ abs (X n - x) + abs (Y n - y) : abs_add_le_abs_add_abs ... < ε / 2 + ε / 2 : add_lt_add (HN₁ ngtN₁) (HN₂ ngtN₂) ... = ε : add_halves) private lemma mul_left_converges_to_seq_of_pos {c : ℝ} (cnz : c ≠ 0) (HX : X ⟶ x in ℕ) : (λ n, c * X n) ⟶ c * x in ℕ := take ε, suppose ε > 0, have abscpos : abs c > 0, from abs_pos_of_ne_zero cnz, have epos : ε / abs c > 0, from div_pos_of_pos_of_pos `ε > 0` abscpos, obtain N (HN : ∀ {n}, n ≥ N → abs (X n - x) < ε / abs c), from HX epos, exists.intro N (take n, suppose n ≥ N, have H : abs (X n - x) < ε / abs c, from HN this, show abs (c * X n - c * x) < ε, from calc abs (c * X n - c * x) = abs c * abs (X n - x) : by rewrite [-mul_sub_left_distrib, abs_mul] ... < abs c * (ε / abs c) : mul_lt_mul_of_pos_left H abscpos ... = ε : mul_div_cancel' (ne_of_gt abscpos)) proposition mul_left_converges_to_seq (c : ℝ) (HX : X ⟶ x in ℕ) : (λ n, c * X n) ⟶ c * x in ℕ := by_cases (assume cz : c = 0, have (λ n, c * X n) = (λ n, 0), from funext (take x, by rewrite [cz, zero_mul]), by+ rewrite [this, cz, zero_mul]; apply converges_to_seq_constant) (suppose c ≠ 0, mul_left_converges_to_seq_of_pos this HX) proposition mul_right_converges_to_seq (c : ℝ) (HX : X ⟶ x in ℕ) : (λ n, X n * c) ⟶ x * c in ℕ := have (λ n, X n * c) = (λ n, c * X n), from funext (take x, !mul.comm), by+ rewrite [this, mul.comm]; apply mul_left_converges_to_seq c HX -- TODO: converges_to_seq_div, converges_to_seq_mul_left_iff, etc. proposition neg_converges_to_seq (HX : X ⟶ x in ℕ) : (λ n, - X n) ⟶ - x in ℕ := take ε, suppose ε > 0, obtain N (HN : ∀ {n}, n ≥ N → abs (X n - x) < ε), from HX this, exists.intro N (take n, suppose n ≥ N, show abs (- X n - (- x)) < ε, by rewrite [-neg_neg_sub_neg, *neg_neg, abs_neg]; exact HN `n ≥ N`) proposition neg_converges_to_seq_iff (X : ℕ → ℝ) : ((λ n, - X n) ⟶ - x in ℕ) ↔ (X ⟶ x in ℕ) := have aux : X = λ n, (- (- X n)), from funext (take n, by rewrite neg_neg), iff.intro (assume H : (λ n, -X n)⟶ -x in ℕ, show X ⟶ x in ℕ, by+ rewrite [aux, -neg_neg x]; exact neg_converges_to_seq H) neg_converges_to_seq proposition abs_converges_to_seq_zero (HX : X ⟶ 0 in ℕ) : (λ n, abs (X n)) ⟶ 0 in ℕ := take ε, suppose ε > 0, obtain N (HN : ∀ n, n ≥ N → abs (X n - 0) < ε), from HX `ε > 0`, exists.intro N (take n, assume Hn : n ≥ N, have abs (X n) < ε, from (!sub_zero ▸ HN n Hn), show abs (abs (X n) - 0) < ε, using this, by rewrite [sub_zero, abs_of_nonneg !abs_nonneg]; apply this) proposition converges_to_seq_zero_of_abs_converges_to_seq_zero (HX : (λ n, abs (X n)) ⟶ 0 in ℕ) : X ⟶ 0 in ℕ := take ε, suppose ε > 0, obtain N (HN : ∀ n, n ≥ N → abs (abs (X n) - 0) < ε), from HX `ε > 0`, exists.intro (N : ℕ) (take n : ℕ, assume Hn : n ≥ N, have HN' : abs (abs (X n) - 0) < ε, from HN n Hn, have abs (X n) < ε, by+ rewrite [real.sub_zero at HN', abs_of_nonneg !abs_nonneg at HN']; apply HN', show abs (X n - 0) < ε, using this, by rewrite sub_zero; apply this) proposition abs_converges_to_seq_zero_iff (X : ℕ → ℝ) : ((λ n, abs (X n)) ⟶ 0 in ℕ) ↔ (X ⟶ 0 in ℕ) := iff.intro converges_to_seq_zero_of_abs_converges_to_seq_zero abs_converges_to_seq_zero -- TODO: products of two sequences, converges_seq, limit_seq end limit_operations /- monotone sequences -/ section monotone_sequences open nat set variable {X : ℕ → ℝ} definition nondecreasing (X : ℕ → ℝ) : Prop := ∀ ⦃i j⦄, i ≤ j → X i ≤ X j proposition nondecreasing_of_forall_le_succ (H : ∀ i, X i ≤ X (succ i)) : nondecreasing X := take i j, suppose i ≤ j, have ∀ n, X i ≤ X (i + n), from take n, nat.induction_on n (by rewrite nat.add_zero; apply le.refl) (take n, assume ih, le.trans ih (H (i + n))), have X i ≤ X (i + (j - i)), from !this, by+ rewrite [add_sub_of_le `i ≤ j` at this]; exact this proposition converges_to_seq_sup_of_nondecreasing (nondecX : nondecreasing X) {b : ℝ} (Hb : ∀ i, X i ≤ b) : X ⟶ sup (X '[univ]) in ℕ := let sX := sup (X '[univ]) in have Xle : ∀ i, X i ≤ sX, from take i, have ∀ x, x ∈ X '[univ] → x ≤ b, from (take x, assume H, obtain i [H' (Hi : X i = x)], from H, by rewrite -Hi; exact Hb i), show X i ≤ sX, from le_sup (mem_image_of_mem X !mem_univ) this, have exX : ∃ x, x ∈ X '[univ], from exists.intro (X 0) (mem_image_of_mem X !mem_univ), take ε, assume epos : ε > 0, have sX - ε < sX, from !sub_lt_of_pos epos, obtain x' [(H₁x' : x' ∈ X '[univ]) (H₂x' : sX - ε < x')], from exists_mem_and_lt_of_lt_sup exX this, obtain i [H' (Hi : X i = x')], from H₁x', have Hi' : ∀ j, j ≥ i → sX - ε < X j, from take j, assume Hj, lt_of_lt_of_le (Hi⁻¹ ▸ H₂x') (nondecX Hj), exists.intro i (take j, assume Hj : j ≥ i, have X j - sX ≤ 0, from sub_nonpos_of_le (Xle j), have eq₁ : abs (X j - sX) = sX - X j, using this, by rewrite [abs_of_nonpos this, neg_sub], have sX - ε < X j, from lt_of_lt_of_le (Hi⁻¹ ▸ H₂x') (nondecX Hj), have sX < X j + ε, from lt_add_of_sub_lt_right this, have sX - X j < ε, from sub_lt_left_of_lt_add this, show (abs (X j - sX)) < ε, using eq₁ this, by rewrite eq₁; exact this) definition nonincreasing (X : ℕ → ℝ) : Prop := ∀ ⦃i j⦄, i ≤ j → X i ≥ X j proposition nodecreasing_of_nonincreasing_neg (nonincX : nonincreasing (λ n, - X n)) : nondecreasing (λ n, X n) := take i j, suppose i ≤ j, show X i ≤ X j, from le_of_neg_le_neg (nonincX this) proposition noincreasing_neg_of_nondecreasing (nondecX : nondecreasing X) : nonincreasing (λ n, - X n) := take i j, suppose i ≤ j, show - X i ≥ - X j, from neg_le_neg (nondecX this) proposition nonincreasing_neg_iff (X : ℕ → ℝ) : nonincreasing (λ n, - X n) ↔ nondecreasing X := iff.intro nodecreasing_of_nonincreasing_neg noincreasing_neg_of_nondecreasing proposition nonincreasing_of_nondecreasing_neg (nondecX : nondecreasing (λ n, - X n)) : nonincreasing (λ n, X n) := take i j, suppose i ≤ j, show X i ≥ X j, from le_of_neg_le_neg (nondecX this) proposition nodecreasing_neg_of_nonincreasing (nonincX : nonincreasing X) : nondecreasing (λ n, - X n) := take i j, suppose i ≤ j, show - X i ≤ - X j, from neg_le_neg (nonincX this) proposition nondecreasing_neg_iff (X : ℕ → ℝ) : nondecreasing (λ n, - X n) ↔ nonincreasing X := iff.intro nonincreasing_of_nondecreasing_neg nodecreasing_neg_of_nonincreasing proposition nonincreasing_of_forall_succ_le (H : ∀ i, X (succ i) ≤ X i) : nonincreasing X := begin rewrite -nondecreasing_neg_iff, show nondecreasing (λ n : ℕ, - X n), from nondecreasing_of_forall_le_succ (take i, neg_le_neg (H i)) end proposition converges_to_seq_inf_of_nonincreasing (nonincX : nonincreasing X) {b : ℝ} (Hb : ∀ i, b ≤ X i) : X ⟶ inf (X '[univ]) in ℕ := have H₁ : ∃ x, x ∈ X '[univ], from exists.intro (X 0) (mem_image_of_mem X !mem_univ), have H₂ : ∀ x, x ∈ X '[univ] → b ≤ x, from (take x, assume H, obtain i [Hi₁ (Hi₂ : X i = x)], from H, show b ≤ x, by rewrite -Hi₂; apply Hb i), have H₃ : {x : ℝ | -x ∈ X '[univ]} = {x : ℝ | x ∈ (λ n, -X n) '[univ]}, from calc {x : ℝ | -x ∈ X '[univ]} = (λ y, -y) '[X '[univ]] : !image_neg_eq⁻¹ ... = {x : ℝ | x ∈ (λ n, -X n) '[univ]} : !image_compose⁻¹, have H₄ : ∀ i, - X i ≤ - b, from take i, neg_le_neg (Hb i), begin+ rewrite [-neg_converges_to_seq_iff, -sup_neg H₁ H₂, H₃, -nondecreasing_neg_iff at nonincX], apply converges_to_seq_sup_of_nondecreasing nonincX H₄ end end monotone_sequences section xn open nat set theorem pow_converges_to_seq_zero {x : ℝ} (H : abs x < 1) : (λ n, x^n) ⟶ 0 in ℕ := suffices H' : (λ n, (abs x)^n) ⟶ 0 in ℕ, from have (λ n, (abs x)^n) = (λ n, abs (x^n)), from funext (take n, !abs_pow⁻¹), using this, by rewrite this at H'; exact converges_to_seq_zero_of_abs_converges_to_seq_zero H', let aX := (λ n, (abs x)^n), iaX := inf (aX '[univ]), asX := (λ n, (abs x)^(succ n)) in have noninc_aX : nonincreasing aX, from nonincreasing_of_forall_succ_le (take i, have (abs x) * (abs x)^i ≤ 1 * (abs x)^i, from mul_le_mul_of_nonneg_right (le_of_lt H) (!pow_nonneg_of_nonneg !abs_nonneg), show (abs x) * (abs x)^i ≤ (abs x)^i, from !one_mul ▸ this), have bdd_aX : ∀ i, 0 ≤ aX i, from take i, !pow_nonneg_of_nonneg !abs_nonneg, have aXconv : aX ⟶ iaX in ℕ, from converges_to_seq_inf_of_nonincreasing noninc_aX bdd_aX, have asXconv : asX ⟶ iaX in ℕ, from metric_space.converges_to_seq_offset_succ aXconv, have asXconv' : asX ⟶ (abs x) * iaX in ℕ, from mul_left_converges_to_seq (abs x) aXconv, have iaX = (abs x) * iaX, from converges_to_seq_unique asXconv asXconv', have iaX = 0, from eq_zero_of_mul_eq_self_left (ne_of_lt H) this⁻¹, show aX ⟶ 0 in ℕ, from this ▸ aXconv end xn section continuous -- this definition should be inherited from metric_space once a migrate is done. definition continuous (f : ℝ → ℝ) := ∀ x : ℝ, ∀ ⦃ε : ℝ⦄, ε > 0 → ∃ δ : ℝ, δ > 0 ∧ ∀ x' : ℝ, abs (x - x') < δ → abs (f x - f x') < ε theorem pos_on_nbhd_of_cts_of_pos {f : ℝ → ℝ} (Hf : continuous f) {b : ℝ} (Hb : f b > 0) : ∃ δ : ℝ, δ > 0 ∧ ∀ y, abs (b - y) < δ → f y > 0 := begin let Hcont := Hf b Hb, cases Hcont with δ Hδ, existsi δ, split, exact and.left Hδ, intro y Hy, let Hy' := and.right Hδ y Hy, let Hlt := sub_lt_of_abs_sub_lt_right Hy', rewrite sub_self at Hlt, assumption end theorem neg_on_nbhd_of_cts_of_neg {f : ℝ → ℝ} (Hf : continuous f) {b : ℝ} (Hb : f b < 0) : ∃ δ : ℝ, δ > 0 ∧ ∀ y, abs (b - y) < δ → f y < 0 := begin let Hcont := Hf b (neg_pos_of_neg Hb), cases Hcont with δ Hδ, existsi δ, split, exact and.left Hδ, intro y Hy, let Hy' := and.right Hδ y Hy, let Hlt := sub_lt_of_abs_sub_lt_left Hy', let Hlt' := lt_add_of_sub_lt_right Hlt, rewrite [-sub_eq_add_neg at Hlt', sub_self at Hlt'], assumption end theorem neg_continuous_of_continuous {f : ℝ → ℝ} (Hcon : continuous f) : continuous (λ x, - f x) := begin intros x ε Hε, cases Hcon x Hε with δ Hδ, cases Hδ with Hδ₁ Hδ₂, existsi δ, split, assumption, intros x' Hx', let HD := Hδ₂ x' Hx', rewrite [-abs_neg, neg_neg_sub_neg], assumption end theorem translate_continuous_of_continuous {f : ℝ → ℝ} (Hcon : continuous f) (a : ℝ) : continuous (λ x, (f x) + a) := begin intros x ε Hε, cases Hcon x Hε with δ Hδ, cases Hδ with Hδ₁ Hδ₂, existsi δ, split, assumption, intros x' Hx', rewrite [add_sub_comm, sub_self, add_zero], apply Hδ₂, assumption end end continuous section inter_val open set private definition inter_sup (a b : ℝ) (f : ℝ → ℝ) := sup {x | x < b ∧ f x < 0} section parameters {f : ℝ → ℝ} (Hf : continuous f) {a b : ℝ} (Hab : a < b) (Ha : f a < 0) (Hb : f b > 0) include Hf Ha Hb Hab private theorem Hinh : ∃ x, x ∈ {x | x < b ∧ f x < 0} := exists.intro a (and.intro Hab Ha) private theorem Hmem : ∀ x, x ∈ {x | x < b ∧ f x < 0} → x ≤ b := λ x Hx, le_of_lt (and.left Hx) private theorem Hsupleb : inter_sup a b f ≤ b := sup_le (Hinh) Hmem local notation 2 := of_num 1 + of_num 1 private theorem ex_delta_lt {x : ℝ} (Hx : f x < 0) (Hxb : x < b) : ∃ δ : ℝ, δ > 0 ∧ x + δ < b ∧ f (x + δ) < 0 := begin let Hcont := neg_on_nbhd_of_cts_of_neg Hf Hx, cases Hcont with δ Hδ, {cases em (x + δ < b) with Haδ Haδ, existsi δ / 2, split, {exact div_pos_of_pos_of_pos (and.left Hδ) two_pos}, split, {apply lt.trans, apply add_lt_add_left, exact div_two_lt_of_pos (and.left Hδ), exact Haδ}, {apply and.right Hδ, rewrite [sub_add_eq_sub_sub, sub_self, zero_sub, abs_neg, abs_of_pos (div_pos_of_pos_of_pos (and.left Hδ) two_pos)], exact div_two_lt_of_pos (and.left Hδ)}, existsi (b - x) / 2, split, {apply div_pos_of_pos_of_pos, exact sub_pos_of_lt Hxb, exact two_pos}, split, {apply add_midpoint Hxb}, {apply and.right Hδ, rewrite [sub_add_eq_sub_sub, sub_self, zero_sub, abs_neg, abs_of_pos (div_pos_of_pos_of_pos (sub_pos_of_lt Hxb) two_pos)], apply lt_of_lt_of_le, apply div_two_lt_of_pos (sub_pos_of_lt Hxb), apply sub_left_le_of_le_add, apply le_of_not_gt Haδ}} end private lemma sup_near_b {δ : ℝ} (Hpos : 0 < δ) (Hgeb : inter_sup a b f + δ / 2 ≥ b) : abs (inter_sup a b f - b) < δ := begin apply abs_lt_of_lt_of_neg_lt, apply sub_lt_left_of_lt_add, apply lt_of_le_of_lt, apply Hsupleb, apply lt_add_of_pos_right Hpos, rewrite neg_sub, apply sub_lt_left_of_lt_add, apply lt_of_le_of_lt, apply Hgeb, apply add_lt_add_left, apply div_two_lt_of_pos Hpos end private lemma delta_of_lt (Hflt : f (inter_sup a b f) < 0) : ∃ δ : ℝ, δ > 0 ∧ inter_sup a b f + δ < b ∧ f (inter_sup a b f + δ) < 0 := if Hlt : inter_sup a b f < b then ex_delta_lt Hflt Hlt else begin let Heq := eq_of_le_of_ge Hsupleb (le_of_not_gt Hlt), apply absurd Hflt, apply not_lt_of_ge, apply le_of_lt, rewrite Heq, exact Hb end private theorem sup_fn_interval_aux1 : f (inter_sup a b f) ≥ 0 := have ¬ f (inter_sup a b f) < 0, from (suppose f (inter_sup a b f) < 0, obtain δ Hδ, from delta_of_lt this, have inter_sup a b f + δ ∈ {x | x < b ∧ f x < 0}, from and.intro (and.left (and.right Hδ)) (and.right (and.right Hδ)), have ¬ inter_sup a b f < inter_sup a b f + δ, from not_lt_of_ge (le_sup this Hmem), show false, from this (lt_add_of_pos_right (and.left Hδ))), le_of_not_gt this private theorem sup_fn_interval_aux2 : f (inter_sup a b f) ≤ 0 := have ¬ f (inter_sup a b f) > 0, from (assume Hfsup : f (inter_sup a b f) > 0, obtain δ Hδ, from pos_on_nbhd_of_cts_of_pos Hf Hfsup, have ∀ x, x ∈ {x | x < b ∧ f x < 0} → x ≤ inter_sup a b f - δ / 2, from (take x, assume Hxset : x ∈ {x | x < b ∧ f x < 0}, have ¬ x > inter_sup a b f - δ / 2, from (assume Hngt, have Habs : abs (inter_sup a b f - x) < δ, begin apply abs_lt_of_lt_of_neg_lt, apply sub_lt_of_sub_lt, apply gt.trans, exact Hngt, apply sub_lt_sub_left, exact div_two_lt_of_pos (and.left Hδ), rewrite neg_sub, apply lt_of_le_of_lt, rotate 1, apply and.left Hδ, apply sub_nonpos_of_le, apply le_sup, exact Hxset, exact Hmem end, have f x > 0, from and.right Hδ x Habs, show false, from (not_lt_of_gt this) (and.right Hxset)), le_of_not_gt this), have Hle : inter_sup a b f ≤ inter_sup a b f - δ / 2, from sup_le Hinh this, show false, from not_le_of_gt (sub_lt_of_pos _ (div_pos_of_pos_of_pos (and.left Hδ) (two_pos))) Hle), le_of_not_gt this private theorem sup_fn_interval : f (inter_sup a b f) = 0 := eq_of_le_of_ge sup_fn_interval_aux2 sup_fn_interval_aux1 private theorem intermediate_value_incr_aux2 : ∃ δ : ℝ, δ > 0 ∧ a + δ < b ∧ f (a + δ) < 0 := begin let Hcont := neg_on_nbhd_of_cts_of_neg Hf Ha, cases Hcont with δ Hδ, {cases em (a + δ < b) with Haδ Haδ, existsi δ / 2, split, {exact div_pos_of_pos_of_pos (and.left Hδ) two_pos}, split, {apply lt.trans, apply add_lt_add_left, exact div_two_lt_of_pos (and.left Hδ), exact Haδ}, {apply and.right Hδ, rewrite [sub_add_eq_sub_sub, sub_self, zero_sub, abs_neg, abs_of_pos (div_pos_of_pos_of_pos (and.left Hδ) two_pos)], exact div_two_lt_of_pos (and.left Hδ)}, existsi (b - a) / 2, split, {apply div_pos_of_pos_of_pos, exact sub_pos_of_lt Hab, exact two_pos}, split, {apply add_midpoint Hab}, {apply and.right Hδ, rewrite [sub_add_eq_sub_sub, sub_self, zero_sub, abs_neg, abs_of_pos (div_pos_of_pos_of_pos (sub_pos_of_lt Hab) two_pos)], apply lt_of_lt_of_le, apply div_two_lt_of_pos (sub_pos_of_lt Hab), apply sub_left_le_of_le_add, apply le_of_not_gt Haδ}} end theorem intermediate_value_incr_zero : ∃ c, a < c ∧ c < b ∧ f c = 0 := begin existsi inter_sup a b f, split, {cases intermediate_value_incr_aux2 with δ Hδ, apply lt_of_lt_of_le, apply lt_add_of_pos_right, exact and.left Hδ, apply le_sup, exact and.right Hδ, intro x Hx, apply le_of_lt, exact and.left Hx}, split, {cases pos_on_nbhd_of_cts_of_pos Hf Hb with δ Hδ, apply lt_of_le_of_lt, rotate 1, apply sub_lt_of_pos, exact and.left Hδ, rotate_right 1, apply sup_le, exact exists.intro a (and.intro Hab Ha), intro x Hx, apply le_of_not_gt, intro Hxgt, have Hxgt' : b - x < δ, from sub_lt_of_sub_lt Hxgt, rewrite -(abs_of_pos (sub_pos_of_lt (and.left Hx))) at Hxgt', let Hxgt'' := and.right Hδ _ Hxgt', exact not_lt_of_ge (le_of_lt Hxgt'') (and.right Hx)}, {exact sup_fn_interval} end end theorem intermediate_value_decr_zero {f : ℝ → ℝ} (Hf : continuous f) {a b : ℝ} (Hab : a < b) (Ha : f a > 0) (Hb : f b < 0) : ∃ c, a < c ∧ c < b ∧ f c = 0 := begin have Ha' : - f a < 0, from neg_neg_of_pos Ha, have Hb' : - f b > 0, from neg_pos_of_neg Hb, have Hcon : continuous (λ x, - f x), from neg_continuous_of_continuous Hf, let Hiv := intermediate_value_incr_zero Hcon Hab Ha' Hb', cases Hiv with c Hc, existsi c, split, exact and.left Hc, split, exact and.left (and.right Hc), apply eq_zero_of_neg_eq_zero, apply and.right (and.right Hc) end theorem intermediate_value_incr {f : ℝ → ℝ} (Hf : continuous f) {a b : ℝ} (Hab : a < b) {v : ℝ} (Hav : f a < v) (Hbv : f b > v) : ∃ c, a < c ∧ c < b ∧ f c = v := have Hav' : f a - v < 0, from sub_neg_of_lt Hav, have Hbv' : f b - v > 0, from sub_pos_of_lt Hbv, have Hcon : continuous (λ x, f x - v), from translate_continuous_of_continuous Hf _, have Hiv : ∃ c, a < c ∧ c < b ∧ f c - v = 0, from intermediate_value_incr_zero Hcon Hab Hav' Hbv', obtain c Hc, from Hiv, exists.intro c (and.intro (and.left Hc) (and.intro (and.left (and.right Hc)) (eq_of_sub_eq_zero (and.right (and.right Hc))))) theorem intermediate_value_decr {f : ℝ → ℝ} (Hf : continuous f) {a b : ℝ} (Hab : a < b) {v : ℝ} (Hav : f a > v) (Hbv : f b < v) : ∃ c, a < c ∧ c < b ∧ f c = v := have Hav' : f a - v > 0, from sub_pos_of_lt Hav, have Hbv' : f b - v < 0, from sub_neg_of_lt Hbv, have Hcon : continuous (λ x, f x - v), from translate_continuous_of_continuous Hf _, have Hiv : ∃ c, a < c ∧ c < b ∧ f c - v = 0, from intermediate_value_decr_zero Hcon Hab Hav' Hbv', obtain c Hc, from Hiv, exists.intro c (and.intro (and.left Hc) (and.intro (and.left (and.right Hc)) (eq_of_sub_eq_zero (and.right (and.right Hc))))) end inter_val /- proposition converges_to_at_unique {f : M → N} {y₁ y₂ : N} {x : M} (H₁ : f ⟶ y₁ '[at x]) (H₂ : f ⟶ y₂ '[at x]) : y₁ = y₂ := eq_of_forall_dist_le (take ε, suppose ε > 0, have e2pos : ε / 2 > 0, from div_pos_of_pos_of_pos `ε > 0` two_pos, obtain δ₁ [(δ₁pos : δ₁ > 0) (Hδ₁ : ∀ x', x ≠ x' ∧ dist x x' < δ₁ → dist (f x') y₁ < ε / 2)], from H₁ e2pos, obtain δ₂ [(δ₂pos : δ₂ > 0) (Hδ₂ : ∀ x', x ≠ x' ∧ dist x x' < δ₂ → dist (f x') y₂ < ε / 2)], from H₂ e2pos, let δ := min δ₁ δ₂ in have δ > 0, from lt_min δ₁pos δ₂pos, -/ end real
8f4a5e2addf54758bc7f53432781943bbf6d2a41
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/archive/100-theorems-list/83_friendship_graphs.lean
bf2f99f99942fcdc8007a16a7d531256d096f10f
[ "Apache-2.0" ]
permissive
waynemunro/mathlib
e3fd4ff49f4cb43d4a8ded59d17be407bc5ee552
065a70810b5480d584033f7bbf8e0409480c2118
refs/heads/master
1,693,417,182,397
1,634,644,781,000
1,634,644,781,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,779
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson, Jalex Stark, Kyle Miller -/ import combinatorics.simple_graph.adj_matrix import linear_algebra.matrix.charpoly.coeff import data.int.modeq import data.zmod.basic import tactic.interval_cases /-! # The Friendship Theorem ## Definitions and Statement - A `friendship` graph is one in which any two distinct vertices have exactly one neighbor in common - A `politician`, at least in the context of this problem, is a vertex in a graph which is adjacent to every other vertex. - The friendship theorem (Erdős, Rényi, Sós 1966) states that every finite friendship graph has a politician. ## Proof outline The proof revolves around the theory of adjacency matrices, although some steps could equivalently be phrased in terms of counting walks. - Assume `G` is a finite friendship graph. - First we show that any two nonadjacent vertices have the same degree - Assume for contradiction that `G` does not have a politician. - Conclude from the last two points that `G` is `d`-regular for some `d : ℕ`. - Show that `G` has `d ^ 2 - d + 1` vertices. - By casework, show that if `d = 0, 1, 2`, then `G` has a politician. - If `3 ≤ d`, let `p` be a prime factor of `d - 1`. - If `A` is the adjacency matrix of `G` with entries in `ℤ/pℤ`, we show that `A ^ p` has trace `1`. - This gives a contradiction, as `A` has trace `0`, and thus `A ^ p` has trace `0`. ## References - [P. Erdős, A. Rényi, V. Sós, *On A Problem of Graph Theory*][erdosrenyisos] - [C. Huneke, *The Friendship Theorem*][huneke2002] -/ open_locale classical big_operators noncomputable theory open finset simple_graph matrix universes u v variables {V : Type u} {R : Type v} [semiring R] section friendship_def variables (G : simple_graph V) /-- This property of a graph is the hypothesis of the friendship theorem: every pair of nonadjacent vertices has exactly one common friend, a vertex to which both are adjacent. -/ def friendship [fintype V] : Prop := ∀ ⦃v w : V⦄, v ≠ w → fintype.card (G.common_neighbors v w) = 1 /-- A politician is a vertex that is adjacent to all other vertices. -/ def exists_politician : Prop := ∃ (v : V), ∀ (w : V), v ≠ w → G.adj v w end friendship_def variables [fintype V] {G : simple_graph V} {d : ℕ} (hG : friendship G) include hG namespace friendship variables (R) /-- One characterization of a friendship graph is that there is exactly one walk of length 2 between distinct vertices. These walks are counted in off-diagonal entries of the square of the adjacency matrix, so for a friendship graph, those entries are all 1. -/ theorem adj_matrix_sq_of_ne {v w : V} (hvw : v ≠ w) : ((G.adj_matrix R) ^ 2) v w = 1 := begin rw [sq, ← nat.cast_one, ← hG hvw], simp [common_neighbors, neighbor_finset_eq_filter, finset.filter_filter, finset.filter_inter, and_comm], end /-- This calculation amounts to counting the number of length 3 walks between nonadjacent vertices. We use it to show that nonadjacent vertices have equal degrees. -/ lemma adj_matrix_pow_three_of_not_adj {v w : V} (non_adj : ¬ G.adj v w) : ((G.adj_matrix R) ^ 3) v w = degree G v := begin rw [pow_succ, mul_eq_mul, adj_matrix_mul_apply, degree, card_eq_sum_ones, nat.cast_sum], apply sum_congr rfl, intros x hx, rw [adj_matrix_sq_of_ne _ hG, nat.cast_one], rintro ⟨rfl⟩, rw mem_neighbor_finset at hx, exact non_adj hx, end variable {R} /-- As `v` and `w` not being adjacent implies `degree G v = ((G.adj_matrix R) ^ 3) v w` and `degree G w = ((G.adj_matrix R) ^ 3) v w`, the degrees are equal if `((G.adj_matrix R) ^ 3) v w = ((G.adj_matrix R) ^ 3) w v` This is true as the adjacency matrix is symmetric. -/ lemma degree_eq_of_not_adj {v w : V} (hvw : ¬ G.adj v w) : degree G v = degree G w := begin rw [← nat.cast_id (G.degree v), ← nat.cast_id (G.degree w), ← adj_matrix_pow_three_of_not_adj ℕ hG hvw, ← adj_matrix_pow_three_of_not_adj ℕ hG (λ h, hvw (G.symm h))], conv_lhs {rw ← transpose_adj_matrix}, simp only [pow_succ, sq, mul_eq_mul, ← transpose_mul, transpose_apply], simp only [← mul_eq_mul, mul_assoc], end /-- Let `A` be the adjacency matrix of a graph `G`. If `G` is a friendship graph, then all of the off-diagonal entries of `A^2` are 1. If `G` is `d`-regular, then all of the diagonal entries of `A^2` are `d`. Putting these together determines `A^2` exactly for a `d`-regular friendship graph. -/ theorem adj_matrix_sq_of_regular (hd : G.is_regular_of_degree d) : ((G.adj_matrix R) ^ 2) = λ v w, if v = w then d else 1 := begin ext v w, by_cases h : v = w, { rw [h, sq, mul_eq_mul, adj_matrix_mul_self_apply_self, hd], simp, }, { rw [adj_matrix_sq_of_ne R hG h, if_neg h], }, end lemma adj_matrix_sq_mod_p_of_regular {p : ℕ} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (G.adj_matrix (zmod p)) ^ 2 = λ _ _, 1 := by simp [adj_matrix_sq_of_regular hG hd, dmod] section nonempty variable [nonempty V] /-- If `G` is a friendship graph without a politician (a vertex adjacent to all others), then it is regular. We have shown that nonadjacent vertices of a friendship graph have the same degree, and if there isn't a politician, we can show this for adjacent vertices by finding a vertex neither is adjacent to, and then using transitivity. -/ theorem is_regular_of_not_exists_politician (hG' : ¬exists_politician G) : ∃ (d : ℕ), G.is_regular_of_degree d := begin have v := classical.arbitrary V, use G.degree v, intro x, by_cases hvx : G.adj v x, swap, { exact (degree_eq_of_not_adj hG hvx).symm, }, dunfold exists_politician at hG', push_neg at hG', rcases hG' v with ⟨w, hvw', hvw⟩, rcases hG' x with ⟨y, hxy', hxy⟩, by_cases hxw : G.adj x w, swap, { rw degree_eq_of_not_adj hG hvw, exact degree_eq_of_not_adj hG hxw }, rw degree_eq_of_not_adj hG hxy, by_cases hvy : G.adj v y, swap, { exact (degree_eq_of_not_adj hG hvy).symm }, rw degree_eq_of_not_adj hG hvw, apply degree_eq_of_not_adj hG, intro hcontra, rcases finset.card_eq_one.mp (hG hvw') with ⟨⟨a, ha⟩, h⟩, have key : ∀ {x}, x ∈ G.common_neighbors v w → x = a, { intros x hx, have h' := mem_univ (subtype.mk x hx), rw [h, mem_singleton] at h', injection h', }, apply hxy', rw [key ((mem_common_neighbors G).mpr ⟨hvx, G.symm hxw⟩), key ((mem_common_neighbors G).mpr ⟨hvy, G.symm hcontra⟩)], end /-- Let `A` be the adjacency matrix of a `d`-regular friendship graph, and let `v` be a vector all of whose components are `1`. Then `v` is an eigenvector of `A ^ 2`, and we can compute the eigenvalue to be `d * d`, or as `d + (fintype.card V - 1)`, so those quantities must be equal. This essentially means that the graph has `d ^ 2 - d + 1` vertices. -/ lemma card_of_regular (hd : G.is_regular_of_degree d) : d + (fintype.card V - 1) = d * d := begin have v := classical.arbitrary V, transitivity ((G.adj_matrix ℕ) ^ 2).mul_vec (λ _, 1) v, { rw [adj_matrix_sq_of_regular hG hd, mul_vec, dot_product, ← insert_erase (mem_univ v)], simp only [sum_insert, mul_one, if_true, nat.cast_id, eq_self_iff_true, mem_erase, not_true, ne.def, not_false_iff, add_right_inj, false_and], rw [finset.sum_const_nat, card_erase_of_mem (mem_univ v), mul_one], { refl }, intros x hx, simp [(ne_of_mem_erase hx).symm], }, { rw [sq, mul_eq_mul, ← mul_vec_mul_vec], simp [adj_matrix_mul_vec_const_apply_of_regular hd, neighbor_finset, card_neighbor_set_eq_degree, hd v], } end /-- The size of a `d`-regular friendship graph is `1 mod (d-1)`, and thus `1 mod p` for a factor `p ∣ d-1`. -/ lemma card_mod_p_of_regular {p : ℕ} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (fintype.card V : zmod p) = 1 := begin have hpos : 0 < fintype.card V := fintype.card_pos_iff.mpr infer_instance, rw [← nat.succ_pred_eq_of_pos hpos, nat.succ_eq_add_one, nat.pred_eq_sub_one], simp only [add_left_eq_self, nat.cast_add, nat.cast_one], have h := congr_arg (λ n, (↑n : zmod p)) (card_of_regular hG hd), revert h, simp [dmod], end end nonempty omit hG lemma adj_matrix_sq_mul_const_one_of_regular (hd : G.is_regular_of_degree d) : (G.adj_matrix R) * (λ _ _, 1) = λ _ _, d := by { ext x, simp [← hd x, degree] } lemma adj_matrix_mul_const_one_mod_p_of_regular {p : ℕ} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) : (G.adj_matrix (zmod p)) * (λ _ _, 1) = λ _ _, 1 := by rw [adj_matrix_sq_mul_const_one_of_regular hd, dmod] include hG /-- Modulo a factor of `d-1`, the square and all higher powers of the adjacency matrix of a `d`-regular friendship graph reduce to the matrix whose entries are all 1. -/ lemma adj_matrix_pow_mod_p_of_regular {p : ℕ} (dmod : (d : zmod p) = 1) (hd : G.is_regular_of_degree d) {k : ℕ} (hk : 2 ≤ k) : (G.adj_matrix (zmod p)) ^ k = λ _ _, 1 := begin iterate 2 {cases k with k, { exfalso, linarith, }, }, induction k with k hind, { exact adj_matrix_sq_mod_p_of_regular hG dmod hd, }, rw [pow_succ, hind (nat.le_add_left 2 k)], exact adj_matrix_mul_const_one_mod_p_of_regular dmod hd, end variable [nonempty V] /-- This is the main proof. Assuming that `3 ≤ d`, we take `p` to be a prime factor of `d-1`. Then the `p`th power of the adjacency matrix of a `d`-regular friendship graph must have trace 1 mod `p`, but we can also show that the trace must be the `p`th power of the trace of the original adjacency matrix, which is 0, a contradiction. -/ lemma false_of_three_le_degree (hd : G.is_regular_of_degree d) (h : 3 ≤ d) : false := begin -- get a prime factor of d - 1 let p : ℕ := (d - 1).min_fac, have p_dvd_d_pred := (zmod.nat_coe_zmod_eq_zero_iff_dvd _ _).mpr (d - 1).min_fac_dvd, have dpos : 0 < d := by linarith, have d_cast : ↑(d - 1) = (d : ℤ) - 1 := by norm_cast, haveI : fact p.prime := ⟨nat.min_fac_prime (by linarith)⟩, have hp2 : 2 ≤ p := (fact.out p.prime).two_le, have dmod : (d : zmod p) = 1, { rw [← nat.succ_pred_eq_of_pos dpos, nat.succ_eq_add_one, nat.pred_eq_sub_one], simp only [add_left_eq_self, nat.cast_add, nat.cast_one], exact p_dvd_d_pred, }, have Vmod := card_mod_p_of_regular hG dmod hd, -- now we reduce to a trace calculation have := zmod.trace_pow_card (G.adj_matrix (zmod p)), contrapose! this, clear this, -- the trace is 0 mod p when computed one way rw [trace_adj_matrix, zero_pow (fact.out p.prime).pos], -- but the trace is 1 mod p when computed the other way rw adj_matrix_pow_mod_p_of_regular hG dmod hd hp2, dunfold fintype.card at Vmod, simp only [matrix.trace, diag_apply, mul_one, nsmul_eq_mul, linear_map.coe_mk, sum_const], rw [Vmod, ← nat.cast_one, zmod.nat_coe_zmod_eq_zero_iff_dvd, nat.dvd_one, nat.min_fac_eq_one_iff], linarith, end /-- If `d ≤ 1`, a `d`-regular friendship graph has at most one vertex, which is trivially a politician. -/ lemma exists_politician_of_degree_le_one (hd : G.is_regular_of_degree d) (hd1 : d ≤ 1) : exists_politician G := begin have sq : d * d = d := by { interval_cases d; norm_num }, have h := card_of_regular hG hd, rw sq at h, have : fintype.card V ≤ 1, { cases fintype.card V with n, { exact zero_le _, }, { have : n = 0, { rw [nat.succ_sub_succ_eq_sub, nat.sub_zero] at h, linarith }, subst n, } }, use classical.arbitrary V, intros w h, exfalso, apply h, apply fintype.card_le_one_iff.mp this, end /-- If `d = 2`, a `d`-regular friendship graph has 3 vertices, so it must be complete graph, and all the vertices are politicians. -/ lemma neighbor_finset_eq_of_degree_eq_two (hd : G.is_regular_of_degree 2) (v : V) : G.neighbor_finset v = finset.univ.erase v := begin apply finset.eq_of_subset_of_card_le, { rw finset.subset_iff, intro x, rw [mem_neighbor_finset, finset.mem_erase], exact λ h, ⟨(G.ne_of_adj h).symm, finset.mem_univ _⟩ }, convert_to 2 ≤ _, { convert_to _ = fintype.card V - 1, { have hfr:= card_of_regular hG hd, linarith }, { exact finset.card_erase_of_mem (finset.mem_univ _), }, }, { dsimp [is_regular_of_degree, degree] at hd, rw hd, } end lemma exists_politician_of_degree_eq_two (hd : G.is_regular_of_degree 2) : exists_politician G := begin have v := classical.arbitrary V, use v, intros w hvw, rw [← mem_neighbor_finset, neighbor_finset_eq_of_degree_eq_two hG hd v, finset.mem_erase], exact ⟨hvw.symm, finset.mem_univ _⟩, end lemma exists_politician_of_degree_le_two (hd : G.is_regular_of_degree d) (h : d ≤ 2) : exists_politician G := begin interval_cases d, iterate 2 { apply exists_politician_of_degree_le_one hG hd, norm_num }, { exact exists_politician_of_degree_eq_two hG hd }, end end friendship /-- **Friendship theorem**: We wish to show that a friendship graph has a politician (a vertex adjacent to all others). We proceed by contradiction, and assume the graph has no politician. We have already proven that a friendship graph with no politician is `d`-regular for some `d`, and now we do casework on `d`. If the degree is at most 2, we observe by casework that it has a politician anyway. If the degree is at least 3, the graph cannot exist. -/ theorem friendship_theorem [nonempty V] : exists_politician G := begin by_contradiction npG, rcases hG.is_regular_of_not_exists_politician npG with ⟨d, dreg⟩, cases lt_or_le d 3 with dle2 dge3, { exact npG (hG.exists_politician_of_degree_le_two dreg (nat.lt_succ_iff.mp dle2)) }, { exact hG.false_of_three_le_degree dreg dge3 }, end
bf87323a792de1695cf35fe81a5d65c764f3a0f1
ee8cdbabf07f77e7be63a449b8483ce308d37218
/lean/src/valid/mathd-algebra-10.lean
8efb1b4258777a08b9593f9089ec8dcc1fbbb063
[ "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
405
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.real.basic example : abs ( (120:ℝ) / 100 * 30 - 130 / 100 * 20 ) = 10 := begin calc abs ( (120:ℝ) / 100 * 30 - 130 / 100 * 20 ) = abs 10 : by {ring} ... = 10 : by {refine if_pos _, linarith}, end
19b104002f184bd2522d86477ed5177875596c80
b12fedd6f2d718a9a3bf95f8eeb27d592548c935
/xenalib/m1f/square_root.lean
1c38d0d0fa92c4a79fa670b037b05e0abd7883fe
[]
no_license
EllenArlt/xena
014727beff556fdc8e844f1c9a06bf399fd752a0
e372c75f1d59929aef750ce70448153c4b675760
refs/heads/master
1,630,567,635,968
1,514,648,885,000
1,514,648,885,000
115,862,301
0
0
null
1,515,693,953,000
1,514,723,974,000
Lean
UTF-8
Lean
false
false
13,515
lean
/- Copyright (c) 2017 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Kevin Buzzard A square root function. 1) The non-computable function square_root : Π (x : ℝ), x ≥ 0 → ℝ returns the non-negative square root of x, defined as the sup of all the reals whose square is at most x. 2) The non-computable function sqrt_abs : ℝ → ℝ sends a real number x to the non-negative square root of abs x (a.k.a. |x|). -/ import analysis.real tactic.norm_num -- analysis.real -- for reals -- tactic.norm_num -- because I want to do proofs of things like 1/4 < 1 in ℝ quickly -- init.classical -- don't know why yet. -- Can I just dump the below things into Lean within no namespace? infix ` ** `: 80 := monoid.pow theorem pow_two_eq_mul_self {α : Type} [monoid α] {x : α} : x ** 2 = x * x := by simp [monoid.pow] theorem mul_self_eq_pow_two {α : Type} [monoid α] {x : α} : x * x = x ** 2 := eq.symm pow_two_eq_mul_self theorem imp_of_not_or {A B : Prop} : (A ∨ B) → (¬ A) → B := begin intros Hor HnBofA, cases Hor, contradiction, exact a end -- #check @abs_sub_square -- why is that even there? /- Possibly more useful: #check @nonneg_le_nonneg_of_squares_le nonneg_le_nonneg_of_squares_le : ∀ {α : Type u_1} [_inst_1 : linear_ordered_ring α] {a b : α}, b ≥ 0 → a * a ≤ b * b → a ≤ b -/ namespace square_root -- I have no idea whether this is the right thing to do open square_root theorem square_inj_on_nonneg {x y : ℝ} : (x ≥ 0) → (y ≥ 0) → (x**2 = y**2) → (x=y) := begin assume H_x_ge_zero : x ≥ 0, assume H_y_ge_zero : y ≥ 0, assume H : x ** 2 = y ** 2, rw [pow_two_eq_mul_self,pow_two_eq_mul_self,mul_self_eq_mul_self_iff] at H, cases H with Heq Hneg, exact Heq, have H2 : x = 0 ∧ y = 0, exact (add_eq_zero_iff_eq_zero_of_nonneg H_x_ge_zero H_y_ge_zero).1 (add_eq_zero_iff_eq_neg.2 Hneg), rw [H2.left,H2.right], end theorem square_cont_at_zero : ∀ (r : ℝ), r > 0 → ∃ (eps : ℝ), (eps > 0) ∧ eps ** 2 < r := begin intros r Hr_gt_0, cases lt_or_ge r 1 with Hrl1 Hrge1, have H : r**2<r, unfold monoid.pow, exact calc r*(r*1) = r*r : by simp ... < r*1 : mul_lt_mul_of_pos_left Hrl1 Hr_gt_0 ... = r : mul_one r, existsi r, exact ⟨Hr_gt_0,H⟩, cases le_iff_eq_or_lt.mp Hrge1 with r1 rg1, rw [←r1], exact ⟨((1/2):ℝ),by norm_num⟩, existsi (1:ℝ), split, exact zero_lt_one, suffices : 1<r, by simpa, exact rg1, end -- #check @nonneg_le_nonneg_of_squares_le -- ∀ {α : Type u_1} [_inst_1 : linear_ordered_ring α] {a b : α}, -- b ≥ 0 → a * a ≤ b * b → a ≤ b theorem exists_square_root (r:ℝ) (rnneg : r ≥ 0) : ∃ (q : ℝ), (q ≥ 0) ∧ q**2=r := begin cases le_iff_eq_or_lt.mp rnneg with r0 rpos, rw [←r0], exact ⟨(0:ℝ),by norm_num⟩, clear rnneg, let S := { x:ℝ | x**2 ≤ r}, -- S non-empty have H0 : (0:ℝ) ∈ S, suffices : 0 ≤ r, by simpa [pow_two_eq_mul_self], exact le_of_lt rpos, -- S has upper bound have H1 : max r 1 ∈ upper_bounds S, cases classical.em (r ≤ 1) with rle1 rgt1, intros t Ht, suffices H : t ≤ 1, exact le_trans H (le_max_right r 1), exact nonneg_le_nonneg_of_squares_le (zero_le_one) (by rw [mul_one,←pow_two_eq_mul_self];exact (le_trans Ht rle1)), have H : 1<r, exact lt_of_not_ge rgt1, clear rgt1, intros t Ht, suffices H : t ≤ r, exact le_trans H (le_max_left r 1), -- need to prove t^2<=r implies t<=r apply imp_of_not_or (lt_or_ge r t), assume H1 : r<t, apply not_le_of_gt H1, apply nonneg_le_nonneg_of_squares_le (le_of_lt rpos), exact le_of_lt (calc t*t=t**2 : mul_self_eq_pow_two ... ≤ r : Ht ... = r*1 : eq.symm (mul_one r) ... < r*r : mul_lt_mul_of_pos_left H rpos), -- get LUB have H : ∃ (x : ℝ), is_lub S x, exact exists_supremum_real H0 H1, cases H with q Hq, existsi q, have Hqge0 : 0 ≤ q, exact Hq.left 0 H0, split, exact Hqge0, -- I tidied the code up, up to here; the rest is my original effort. -- idea is to prove q^2=r by showing not < or > -- first not < have H2 : ¬ (q**2<r), intro Hq2r, have H2 : q ∈ upper_bounds S, exact Hq.left, clear Hq H0 H1, unfold upper_bounds at H2, have H3 : ∀ qe, q<qe → ¬(qe**2≤r), intro qe, intro qlqe, intro H4, have H5 : qe ≤ q, exact H2 qe H4, exact not_lt_of_ge H5 qlqe, have H4 : ∀ eps > 0,(q+eps)**2>r, intros eps Heps, exact lt_of_not_ge (H3 (q+eps) ((lt_add_iff_pos_right q).mpr Heps)), clear H3 H2 S, cases le_iff_eq_or_lt.mp Hqge0 with Hq0 Hqg0, cases (square_cont_at_zero r rpos) with eps Heps, specialize H4 eps, rw [←Hq0] at H4, simp at H4, have H3 : eps**2>r, exact H4 Heps.left, exact (lt_iff_not_ge r (eps**2)).mp H3 (le_of_lt Heps.right), clear Hqge0, -- want eps such that 2*q*eps+eps^2 <= r-q^2 -- so eps=min((r-q^2)/4q,thing-produced-by-square-cts-function) have H0 : (0:ℝ)<2, norm_num, have H : 0<(r-q**2), exact sub_pos_of_lt Hq2r, have H2 : 0 < (r-q**2)/2, exact div_pos_of_pos_of_pos H H0, have H3 : 0 < (r-q**2)/2/(2*q), exact div_pos_of_pos_of_pos H2 (mul_pos H0 Hqg0), cases (square_cont_at_zero ((r-q**2)/2) H2) with e0 He0, let e1 := min ((r-q**2)/2/(2*q)) e0, have He1 : e1>0, exact lt_min H3 He0.left, specialize H4 e1, -- should be a contradiction have H1 : (q+e1)**2 > r, exact H4 He1, have H5 : e1 ≤ ((r-q**2)/2/(2*q)), exact (min_le_left ((r-q**2)/2/(2*q)) e0), have H6 : e1*e1<(r - q ** 2) / 2, exact calc e1*e1 ≤ e0*e1 : mul_le_mul_of_nonneg_right (min_le_right ((r - q ** 2) / 2 / (2 * q)) e0) (le_of_lt He1) ... ≤ e0*e0 : mul_le_mul_of_nonneg_left (min_le_right ((r - q ** 2) / 2 / (2 * q)) e0) (le_of_lt He0.left ) ... = e0**2 : by {unfold monoid.pow,simp} ... < (r-q**2)/2 : He0.right, have Hn1 : (q+e1)**2 < r, exact calc (q+e1)**2 = (q+e1)*(q+e1) : by {unfold monoid.pow,simp} ... = q*q+2*q*e1+e1*e1 : by rw [mul_add,add_mul,add_mul,mul_comm e1 q,two_mul,add_mul,add_assoc,add_assoc,add_assoc] ... = q**2 + (2*q)*e1 + e1*e1 : by {unfold monoid.pow,simp} ... ≤ q**2 + (2*q)*((r - q ** 2) / 2 / (2 * q)) + e1*e1 : add_le_add_right (add_le_add_left ((mul_le_mul_left (mul_pos H0 Hqg0)).mpr H5) (q**2)) (e1*e1) ... < q**2 + (2*q)*((r - q ** 2) / 2 / (2 * q)) + (r-q**2)/2 : add_lt_add_left H6 _ ... = r : by rw [mul_comm,div_mul_eq_mul_div,mul_div_assoc,div_self (ne_of_gt (mul_pos H0 Hqg0)),mul_one,add_assoc,div_add_div_same,←two_mul,mul_comm,mul_div_assoc,div_self (ne_of_gt H0),mul_one,add_sub,add_comm,←add_sub,sub_self,add_zero], -- rw [mul_div_cancel'], -- nearly there exact not_lt_of_ge (le_of_lt H1) Hn1, -- now not > have H3 : ¬ (q**2>r), intro Hq2r, have H3 : q ∈ lower_bounds (upper_bounds S), exact Hq.right, clear Hq H0 H1 H2, have Hqg0 : 0 < q, cases le_iff_eq_or_lt.mp Hqge0 with Hq0 H, tactic.swap, exact H, unfold monoid.pow at Hq2r, rw [←Hq0] at Hq2r, simp at Hq2r, exfalso, exact not_lt_of_ge (le_of_lt rpos) Hq2r, clear Hqge0, have H : ∀ (eps:ℝ), (eps > 0 ∧ eps < q) → (q-eps)**2 < r, unfold lower_bounds at H3, unfold set_of at H3, unfold has_mem.mem set.mem has_mem.mem at H3, intros eps Heps, have H : ¬ ((q-eps) ∈ (upper_bounds S)), intro H, have H2 : q ≤ q-eps, exact H3 (q-eps) H, rw [le_sub_iff_add_le] at H2, have Hf : q<q, exact calc q < eps+q : lt_add_of_pos_left q Heps.left ... = q+eps : add_comm eps q ... ≤ q : H2, have Hf2 : ¬ (q=q), exact ne_of_lt Hf, exact Hf2 (by simp), unfold upper_bounds at H, unfold has_mem.mem set.mem has_mem.mem set_of at H, have H2 : ∃ (b:ℝ), ¬ (S b → b ≤ q-eps), exact classical.not_forall.mp H, cases H2 with b Hb, clear H, cases classical.em (S b) with Hsb Hsnb, tactic.swap, have Hnb : S b → b ≤ q - eps, intro Hsb, exfalso, exact Hsnb Hsb, exfalso, exact Hb Hnb, cases classical.em (b ≤ q - eps) with Hlt Hg, exfalso, exact Hb (λ _,Hlt), have Hh : q-eps < b, exact lt_of_not_ge Hg, clear Hg Hb, -- todo: (q-eps)>0, (q-eps)^2<b^2<=r, have H0 : 0<q-eps, rw [lt_sub_iff,zero_add],exact Heps.right, unfold monoid.pow, exact calc (q-eps)*((q-eps)*1) = (q-eps)*(q-eps) : congr_arg (λ t, (q-eps)*t) (mul_one (q-eps)) ... < (q-eps) * b : mul_lt_mul_of_pos_left Hh H0 ... < b * b : mul_lt_mul_of_pos_right Hh (lt_trans H0 Hh) ... = b**2 : by { unfold monoid.pow, simp} ... ≤ r : Hsb, -- We now know (q-eps)^2<r for all eps>0, and q^2>r. Need a contradiction. -- Idea: (q^2-2*q*eps+eps^2)<r so 2q.eps-eps^2>q^2-r>0, -- so we need to find eps such that 2q.eps-eps^2<(q^2-r) -- so set eps=min((q^2-r)/2q,q) have H0 : (0:ℝ)<2, norm_num, have H1 : 0<(q**2-r), exact sub_pos_of_lt Hq2r, have H2 : 0 < (q/2), exact div_pos_of_pos_of_pos Hqg0 H0, have J1 : 0 < (q**2-r)/(2*q), exact div_pos_of_pos_of_pos H1 (mul_pos H0 Hqg0), let e1 := min ((q**2-r)/(2*q)) (q/2), have He1 : e1>0, exact lt_min J1 H2, specialize H e1, -- should be a contradiction have J0 : e1<q, exact calc e1 ≤ (q/2) : min_le_right ((q**2-r)/(2*q)) (q/2) ... = q*(1/2) : by rw [←mul_div_assoc,mul_one] ... < q*1 : mul_lt_mul_of_pos_left (by norm_num) Hqg0 ... = q : by rw [mul_one], have H4 : (q-e1)**2 < r, exact H ⟨He1,J0⟩, have H5 : e1 ≤ ((q**2-r)/(2*q)), exact (min_le_left ((q**2-r)/(2*q)) (q/2)), have H6 : e1*e1>0, exact mul_pos He1 He1, have Hn1 : (q-e1)**2 > r, exact calc (q-e1)**2 = (q-e1)*(q-e1) : by {unfold monoid.pow,simp} ... = q*q-2*q*e1+e1*e1 : by rw [mul_sub,sub_mul,sub_mul,mul_comm e1 q,two_mul,add_mul];simp ... = q**2 - (2*q)*e1 + e1*e1 : by {unfold monoid.pow,simp} ... > q**2 - (2*q)*e1 : lt_add_of_pos_right (q**2 -(2*q)*e1) H6 ... ≥ q**2 - (2*q)*((q ** 2 - r) / (2 * q)) : sub_le_sub (le_of_eq (eq.refl (q**2))) (mul_le_mul_of_nonneg_left H5 (le_of_lt (mul_pos H0 Hqg0))) -- lt_add_iff_pos_right -- (add_le_add_left ((mul_le_mul_left (mul_pos H0 Hqg0)).mpr H5) (q^2)) (e1*e1) ... = r : by rw [←div_mul_eq_mul_div_comm,div_self (ne_of_gt (mul_pos H0 Hqg0)),one_mul];simp, -- ... = r : by rw [mul_comm,div_mul_eq_mul_div,mul_div_assoc,div_self (ne_of_gt (mul_pos H0 Hqg0)),mul_one,add_assoc,div_add_div_same,←two_mul,mul_comm,mul_div_assoc,div_self (ne_of_gt H0),mul_one,add_sub,add_comm,←add_sub,sub_self,add_zero], -- rw [mul_div_cancel'], -- nearly there exact not_lt_of_ge (le_of_lt (H ⟨He1,J0⟩)) Hn1, have H : q**2 ≤ r, exact le_of_not_lt H3, cases lt_or_eq_of_le H with Hlt Heq, exfalso, exact H2 Hlt, exact Heq end -- #check exists_square_root -- exists_square_root : ∀ (r : ℝ), r ≥ 0 → (∃ (q : ℝ), q ≥ 0 ∧ q ^ 2 = r) theorem exists_unique_square_root : ∀ (r:ℝ), (r ≥ 0) → ∃ (q:ℝ), (q ≥ 0 ∧ q**2 = r ∧ ∀ (s:ℝ), s ≥ 0 ∧ s**2 = r → s=q) := begin intro r, assume H_r_ge_zero : r ≥ 0, cases (exists_square_root r H_r_ge_zero) with q H_q_squared_is_r, suffices H_unique : ∀ (s:ℝ), s ≥ 0 ∧ s ** 2 = r → s = q, exact ⟨q,⟨H_q_squared_is_r.left,⟨H_q_squared_is_r.right,H_unique⟩⟩⟩, intro s, assume H_s_ge_zero_and_square_is_r, exact square_inj_on_nonneg H_s_ge_zero_and_square_is_r.left H_q_squared_is_r.left (eq.trans H_s_ge_zero_and_square_is_r.right (eq.symm H_q_squared_is_r.right)) end noncomputable def square_root (x:ℝ) (H_x_nonneg : x ≥ 0) : ℝ := classical.some (exists_unique_square_root x H_x_nonneg) -- #reduce (square_root 2 (by norm_num)) -- oops -- Next is what Mario says I should do (at least in terms of where the proof that x>=0 goes) noncomputable def sqrt_abs (x : ℝ) : ℝ := square_root (abs x) (abs_nonneg x) def square_root_proof (x:ℝ) (h : x ≥ 0) : (square_root x h) ** 2 = x := (classical.some_spec (exists_unique_square_root x h)).right.left def square_root_allinfo (x:ℝ) (h : x ≥ 0) := classical.some_spec (exists_unique_square_root x h) theorem sqrt_abs_ge_zero (x : ℝ) : sqrt_abs x ≥ 0 := (classical.some_spec (exists_unique_square_root (abs x) (abs_nonneg x))).left theorem sqrt_abs_unique (x : ℝ) (Hx_nonneg : 0 ≤ x) : ∀ (s : ℝ), s ≥ 0 ∧ s ** 2 = x → s = sqrt_abs x := begin have H : ∀ (s : ℝ), s ≥ 0 ∧ s ** 2 = abs x → s = square_root (abs x) (abs_nonneg x), exact (classical.some_spec (exists_unique_square_root (abs x) (abs_nonneg x))).right.right, intro s, intro Hs, rw [eq.symm (abs_of_nonneg Hx_nonneg)] at Hs, exact H s Hs, end theorem sqrt_abs_squared (x : ℝ) (Hx_nonneg : 0 ≤ x) : (sqrt_abs x) ** 2 = x := begin have H0 : sqrt_abs x ** 2 = abs x, exact square_root_proof (abs x) (abs_nonneg x), rw [H0], exact abs_of_nonneg Hx_nonneg, end theorem sqrt_abs_mul_self (x : ℝ) (Hx_nonneg : 0 ≤ x) : (sqrt_abs x) * (sqrt_abs x) = x := begin rw [mul_self_eq_pow_two], exact sqrt_abs_squared x Hx_nonneg, end meta def sqrt_tac : tactic unit := `[assumption <|> norm_num] noncomputable def sqrt (r : ℝ) (h : r ≥ 0 . sqrt_tac) : ℝ := classical.some (exists_unique_square_root r h) /- example of usage: noncomputable def s2 : ℝ := sqrt 2 example : s2^2=2 := sqrt_proof 2 -/ end square_root
8d1ad46e4f077b63ae3fff01caa66921efd4db87
c777c32c8e484e195053731103c5e52af26a25d1
/archive/imo/imo1960_q1.lean
fa3ee7680092f695d7cf99d1c28edcf337276bc3
[ "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
3,389
lean
/- Copyright (c) 2020 Kevin Lacker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Lacker -/ import data.nat.digits /-! # IMO 1960 Q1 Determine all three-digit numbers $N$ having the property that $N$ is divisible by 11, and $\dfrac{N}{11}$ is equal to the sum of the squares of the digits of $N$. Since Lean doesn't have a way to directly express problem statements of the form "Determine all X satisfying Y", we express two predicates where proving that one implies the other is equivalent to solving the problem. A human solver also has to discover the second predicate. The strategy here is roughly brute force, checking the possible multiples of 11. -/ open nat namespace imo1960_q1 def sum_of_squares (L : list ℕ) : ℕ := (L.map (λ x, x * x)).sum def problem_predicate (n : ℕ) : Prop := (nat.digits 10 n).length = 3 ∧ 11 ∣ n ∧ n / 11 = sum_of_squares (nat.digits 10 n) def solution_predicate (n : ℕ) : Prop := n = 550 ∨ n = 803 /- Proving that three digit numbers are the ones in [100, 1000). -/ lemma not_zero {n : ℕ} (h1 : problem_predicate n) : n ≠ 0 := have h2 : nat.digits 10 n ≠ list.nil, from list.ne_nil_of_length_eq_succ h1.left, digits_ne_nil_iff_ne_zero.mp h2 lemma ge_100 {n : ℕ} (h1 : problem_predicate n) : 100 ≤ n := have h2 : 10^3 ≤ 10 * n, begin rw ← h1.left, refine nat.base_pow_length_digits_le 10 n _ (not_zero h1), simp, end, by linarith lemma lt_1000 {n : ℕ} (h1 : problem_predicate n) : n < 1000 := have h2 : n < 10^3, begin rw ← h1.left, refine nat.lt_base_pow_length_digits _, simp, end, by linarith /- We do an exhaustive search to show that all results are covered by `solution_predicate`. -/ def search_up_to (c n : ℕ) : Prop := n = c * 11 ∧ ∀ m : ℕ, m < n → problem_predicate m → solution_predicate m lemma search_up_to_start : search_up_to 9 99 := ⟨rfl, λ n h p, by linarith [ge_100 p]⟩ lemma search_up_to_step {c n} (H : search_up_to c n) {c' n'} (ec : c + 1 = c') (en : n + 11 = n') {l} (el : nat.digits 10 n = l) (H' : c = sum_of_squares l → c = 50 ∨ c = 73) : search_up_to c' n' := begin subst ec, subst en, subst el, obtain ⟨rfl, H⟩ := H, refine ⟨by ring, λ m l p, _⟩, obtain ⟨h₁, ⟨m, rfl⟩, h₂⟩ := id p, by_cases h : 11 * m < c * 11, { exact H _ h p }, obtain rfl : m = c := by linarith, rw [nat.mul_div_cancel_left _ (by norm_num : 11 > 0), mul_comm] at h₂, refine (H' h₂).imp _ _; {rintro rfl, norm_num} end lemma search_up_to_end {c} (H : search_up_to c 1001) {n : ℕ} (ppn : problem_predicate n) : solution_predicate n := H.2 _ (by linarith [lt_1000 ppn]) ppn lemma right_direction {n : ℕ} : problem_predicate n → solution_predicate n := begin have := search_up_to_start, iterate 82 { replace := search_up_to_step this (by norm_num1; refl) (by norm_num1; refl) (by norm_num1; refl) dec_trivial }, exact search_up_to_end this end /- Now we just need to prove the equivalence, for the precise problem statement. -/ lemma left_direction (n : ℕ) (spn : solution_predicate n) : problem_predicate n := by rcases spn with (rfl | rfl); norm_num [problem_predicate, sum_of_squares] end imo1960_q1 open imo1960_q1 theorem imo1960_q1 (n : ℕ) : problem_predicate n ↔ solution_predicate n := ⟨right_direction, left_direction n⟩
a2749e70f9b4335275e74a7958e570dece96970d
969dbdfed67fda40a6f5a2b4f8c4a3c7dc01e0fb
/src/ring_theory/localization.lean
4af78312c7ecf3e7caa19ef206c75f9473d9a325
[ "Apache-2.0" ]
permissive
SAAluthwela/mathlib
62044349d72dd63983a8500214736aa7779634d3
83a4b8b990907291421de54a78988c024dc8a552
refs/heads/master
1,679,433,873,417
1,615,998,031,000
1,615,998,031,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
69,505
lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston -/ import data.equiv.ring import group_theory.monoid_localization import ring_theory.ideal.operations import ring_theory.algebraic import ring_theory.integral_closure import ring_theory.non_zero_divisors /-! # Localizations of commutative rings We characterize the localization of a commutative ring `R` at a submonoid `M` up to isomorphism; that is, a commutative ring `S` is the localization of `R` at `M` iff we can find a ring homomorphism `f : R →+* S` satisfying 3 properties: 1. For all `y ∈ M`, `f y` is a unit; 2. For all `z : S`, there exists `(x, y) : R × M` such that `z * f y = f x`; 3. For all `x, y : R`, `f x = f y` iff there exists `c ∈ M` such that `x * c = y * c`. Given such a localization map `f : R →+* S`, we can define the surjection `localization_map.mk'` sending `(x, y) : R × M` to `f x * (f y)⁻¹`, and `localization_map.lift`, the homomorphism from `S` induced by a homomorphism from `R` which maps elements of `M` to invertible elements of the codomain. Similarly, given commutative rings `P, Q`, a submonoid `T` of `P` and a localization map for `T` from `P` to `Q`, then a homomorphism `g : R →+* P` such that `g(M) ⊆ T` induces a homomorphism of localizations, `localization_map.map`, from `S` to `Q`. We treat the special case of localizing away from an element in the sections `away_map` and `away`. We show the localization as a quotient type, defined in `group_theory.monoid_localization` as `submonoid.localization`, is a `comm_ring` and that the natural ring hom `of : R →+* localization M` is a localization map. We show that a localization at the complement of a prime ideal is a local ring. We prove some lemmas about the `R`-algebra structure of `S`. When `R` is an integral domain, we define `fraction_map R K` as an abbreviation for `localization (non_zero_divisors R) K`, the natural map to `R`'s field of fractions. We show that a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field. We use this to show the field of fractions as a quotient type, `fraction_ring`, is a field. ## Implementation notes In maths it is natural to reason up to isomorphism, but in Lean we cannot naturally `rewrite` one structure with an isomorphic one; one way around this is to isolate a predicate characterizing a structure up to isomorphism, and reason about things that satisfy the predicate. A ring localization map is defined to be a localization map of the underlying `comm_monoid` (a `submonoid.localization_map`) which is also a ring hom. To prove most lemmas about a `localization_map` `f` in this file we invoke the corresponding proof for the underlying `comm_monoid` localization map `f.to_localization_map`, which can be found in `group_theory.monoid_localization` and the namespace `submonoid.localization_map`. To apply a localization map `f` as a function, we use `f.to_map`, as coercions don't work well for this structure. To reason about the localization as a quotient type, use `mk_eq_of_mk'` and associated lemmas. These show the quotient map `mk : R → M → localization M` equals the surjection `localization_map.mk'` induced by the map `of : localization_map M (localization M)` (where `of` establishes the localization as a quotient type satisfies the characteristic predicate). The lemma `mk_eq_of_mk'` hence gives you access to the results in the rest of the file, which are about the `localization_map.mk'` induced by any localization map. We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that instances on `S` induced by `f` can 'know' the map needed to induce the instance. The proof that "a `comm_ring` `K` which is the localization of an integral domain `R` at `R \ {0}` is a field" is a `def` rather than an `instance`, so if you want to reason about a field of fractions `K`, assume `[field K]` instead of just `[comm_ring K]`. ## Tags localization, ring localization, commutative ring localization, characteristic predicate, commutative ring, field of fractions -/ variables {R : Type*} [comm_ring R] (M : submonoid R) (S : Type*) [comm_ring S] {P : Type*} [comm_ring P] open function set_option old_structure_cmd true /-- The type of ring homomorphisms satisfying the characteristic predicate: if `f : R →+* S` satisfies this predicate, then `S` is isomorphic to the localization of `R` at `M`. We later define an instance coercing a localization map `f` to its codomain `S` so that instances on `S` induced by `f` can 'know' the map needed to induce the instance. -/ @[nolint has_inhabited_instance] structure localization_map extends ring_hom R S, submonoid.localization_map M S /-- The ring hom underlying a `localization_map`. -/ add_decl_doc localization_map.to_ring_hom /-- The `comm_monoid` `localization_map` underlying a `comm_ring` `localization_map`. See `group_theory.monoid_localization` for its definition. -/ add_decl_doc localization_map.to_localization_map variables {M S} namespace ring_hom /-- Makes a localization map from a `comm_ring` hom satisfying the characteristic predicate. -/ def to_localization_map (f : R →+* S) (H1 : ∀ y : M, is_unit (f y)) (H2 : ∀ z, ∃ x : R × M, z * f x.2 = f x.1) (H3 : ∀ x y, f x = f y ↔ ∃ c : M, x * c = y * c) : localization_map M S := { map_units' := H1, surj' := H2, eq_iff_exists' := H3, .. f } end ring_hom /-- Makes a `comm_ring` localization map from an additive `comm_monoid` localization map of `comm_ring`s. -/ def submonoid.localization_map.to_ring_localization (f : submonoid.localization_map M S) (h : ∀ x y, f.to_map (x + y) = f.to_map x + f.to_map y) : localization_map M S := { ..ring_hom.mk' f.to_monoid_hom h, ..f } namespace localization_map variables (f : localization_map M S) /-- We define a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that instances on `S` induced by `f` can 'know` the map needed to induce the instance. -/ @[nolint unused_arguments has_inhabited_instance] def codomain (f : localization_map M S) := S instance : comm_ring f.codomain := by assumption instance {K : Type*} [field K] (f : localization_map M K) : field f.codomain := by assumption /-- Short for `to_ring_hom`; used for applying a localization map as a function. -/ abbreviation to_map := f.to_ring_hom lemma map_units (y : M) : is_unit (f.to_map y) := f.6 y lemma surj (z) : ∃ x : R × M, z * f.to_map x.2 = f.to_map x.1 := f.7 z lemma eq_iff_exists {x y} : f.to_map x = f.to_map y ↔ ∃ c : M, x * c = y * c := f.8 x y @[ext] lemma ext {f g : localization_map M S} (h : ∀ x, f.to_map x = g.to_map x) : f = g := begin cases f, cases g, simp only at *, exact funext h end lemma ext_iff {f g : localization_map M S} : f = g ↔ ∀ x, f.to_map x = g.to_map x := ⟨λ h x, h ▸ rfl, ext⟩ lemma to_map_injective : injective (@localization_map.to_map _ _ M S _) := λ _ _ h, ext $ ring_hom.ext_iff.1 h /-- Given `a : S`, `S` a localization of `R`, `is_integer a` iff `a` is in the image of the localization map from `R` to `S`. -/ def is_integer (a : S) : Prop := a ∈ set.range f.to_map -- TODO: define a subalgebra of `is_integer`s lemma is_integer_zero : f.is_integer 0 := ⟨0, f.to_map.map_zero⟩ lemma is_integer_one : f.is_integer 1 := ⟨1, f.to_map.map_one⟩ variables {f} lemma is_integer_add {a b} (ha : f.is_integer a) (hb : f.is_integer b) : f.is_integer (a + b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' + b', rw [f.to_map.map_add, ha, hb] end lemma is_integer_mul {a b} (ha : f.is_integer a) (hb : f.is_integer b) : f.is_integer (a * b) := begin rcases ha with ⟨a', ha⟩, rcases hb with ⟨b', hb⟩, use a' * b', rw [f.to_map.map_mul, ha, hb] end lemma is_integer_smul {a : R} {b} (hb : f.is_integer b) : f.is_integer (f.to_map a * b) := begin rcases hb with ⟨b', hb⟩, use a * b', rw [←hb, f.to_map.map_mul] end variables (f) /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the right, matching the argument order in `localization_map.surj`. -/ lemma exists_integer_multiple' (a : S) : ∃ (b : M), is_integer f (a * f.to_map b) := let ⟨⟨num, denom⟩, h⟩ := f.surj a in ⟨denom, set.mem_range.mpr ⟨num, h.symm⟩⟩ /-- Each element `a : S` has an `M`-multiple which is an integer. This version multiplies `a` on the left, matching the argument order in the `has_scalar` instance. -/ lemma exists_integer_multiple (a : S) : ∃ (b : M), is_integer f (f.to_map b * a) := by { simp_rw mul_comm _ a, apply exists_integer_multiple' } /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x` (so this lemma is true by definition). -/ lemma sec_spec {f : localization_map M S} (z : S) : z * f.to_map (f.to_localization_map.sec z).2 = f.to_map (f.to_localization_map.sec z).1 := classical.some_spec $ f.surj z /-- Given `z : S`, `f.to_localization_map.sec z` is defined to be a pair `(x, y) : R × M` such that `z * f y = f x`, so this lemma is just an application of `S`'s commutativity. -/ lemma sec_spec' {f : localization_map M S} (z : S) : f.to_map (f.to_localization_map.sec z).1 = f.to_map (f.to_localization_map.sec z).2 * z := by rw [mul_comm, sec_spec] open_locale big_operators /-- We can clear the denominators of a finite set of fractions. -/ lemma exist_integer_multiples_of_finset (s : finset S) : ∃ (b : M), ∀ a ∈ s, is_integer f (f.to_map b * a) := begin haveI := classical.prop_decidable, use ∏ a in s, (f.to_localization_map.sec a).2, intros a ha, use (∏ x in s.erase a, (f.to_localization_map.sec x).2) * (f.to_localization_map.sec a).1, rw [ring_hom.map_mul, sec_spec', ←mul_assoc, ←f.to_map.map_mul], congr' 2, refine trans _ ((submonoid.subtype M).map_prod _ _).symm, rw [mul_comm, ←finset.prod_insert (s.not_mem_erase a), finset.insert_erase ha], refl, end lemma map_right_cancel {x y} {c : M} (h : f.to_map (c * x) = f.to_map (c * y)) : f.to_map x = f.to_map y := f.to_localization_map.map_right_cancel h lemma map_left_cancel {x y} {c : M} (h : f.to_map (x * c) = f.to_map (y * c)) : f.to_map x = f.to_map y := f.to_localization_map.map_left_cancel h lemma eq_zero_of_fst_eq_zero {z x} {y : M} (h : z * f.to_map y = f.to_map x) (hx : x = 0) : z = 0 := by rw [hx, f.to_map.map_zero] at h; exact (f.map_units y).mul_left_eq_zero.1 h /-- Given a localization map `f : R →+* S`, the surjection sending `(x, y) : R × M` to `f x * (f y)⁻¹`. -/ noncomputable def mk' (f : localization_map M S) (x : R) (y : M) : S := f.to_localization_map.mk' x y @[simp] lemma mk'_sec (z : S) : f.mk' (f.to_localization_map.sec z).1 (f.to_localization_map.sec z).2 = z := f.to_localization_map.mk'_sec _ lemma mk'_mul (x₁ x₂ : R) (y₁ y₂ : M) : f.mk' (x₁ * x₂) (y₁ * y₂) = f.mk' x₁ y₁ * f.mk' x₂ y₂ := f.to_localization_map.mk'_mul _ _ _ _ lemma mk'_one (x) : f.mk' x (1 : M) = f.to_map x := f.to_localization_map.mk'_one _ @[simp] lemma mk'_spec (x) (y : M) : f.mk' x y * f.to_map y = f.to_map x := f.to_localization_map.mk'_spec _ _ @[simp] lemma mk'_spec' (x) (y : M) : f.to_map y * f.mk' x y = f.to_map x := f.to_localization_map.mk'_spec' _ _ theorem eq_mk'_iff_mul_eq {x} {y : M} {z} : z = f.mk' x y ↔ z * f.to_map y = f.to_map x := f.to_localization_map.eq_mk'_iff_mul_eq theorem mk'_eq_iff_eq_mul {x} {y : M} {z} : f.mk' x y = z ↔ f.to_map x = z * f.to_map y := f.to_localization_map.mk'_eq_iff_eq_mul lemma mk'_surjective (z : S) : ∃ x (y : M), f.mk' x y = z := let ⟨r, hr⟩ := f.surj z in ⟨r.1, r.2, (f.eq_mk'_iff_mul_eq.2 hr).symm⟩ lemma mk'_eq_iff_eq {x₁ x₂} {y₁ y₂ : M} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ f.to_map (x₁ * y₂) = f.to_map (x₂ * y₁) := f.to_localization_map.mk'_eq_iff_eq lemma mk'_mem_iff {x} {y : M} {I : ideal S} : f.mk' x y ∈ I ↔ f.to_map x ∈ I := begin split; intro h, { rw [← mk'_spec f x y, mul_comm], exact I.mul_mem_left (f.to_map y) h }, { rw ← mk'_spec f x y at h, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.1 (map_units f y), have := I.mul_mem_left b h, rwa [mul_comm, mul_assoc, hb, mul_one] at this } end protected lemma eq {a₁ b₁} {a₂ b₂ : M} : f.mk' a₁ a₂ = f.mk' b₁ b₂ ↔ ∃ c : M, a₁ * b₂ * c = b₁ * a₂ * c := f.to_localization_map.eq lemma eq_iff_eq (g : localization_map M P) {x y} : f.to_map x = f.to_map y ↔ g.to_map x = g.to_map y := f.to_localization_map.eq_iff_eq g.to_localization_map lemma mk'_eq_iff_mk'_eq (g : localization_map M P) {x₁ x₂} {y₁ y₂ : M} : f.mk' x₁ y₁ = f.mk' x₂ y₂ ↔ g.mk' x₁ y₁ = g.mk' x₂ y₂ := f.to_localization_map.mk'_eq_iff_mk'_eq g.to_localization_map lemma mk'_eq_of_eq {a₁ b₁ : R} {a₂ b₂ : M} (H : b₁ * a₂ = a₁ * b₂) : f.mk' a₁ a₂ = f.mk' b₁ b₂ := f.to_localization_map.mk'_eq_of_eq H @[simp] lemma mk'_self {x : R} (hx : x ∈ M) : f.mk' x ⟨x, hx⟩ = 1 := f.to_localization_map.mk'_self _ hx @[simp] lemma mk'_self' {x : M} : f.mk' x x = 1 := f.to_localization_map.mk'_self' _ lemma mk'_self'' {x : M} : f.mk' x.1 x = 1 := f.mk'_self' lemma mul_mk'_eq_mk'_of_mul (x y : R) (z : M) : f.to_map x * f.mk' y z = f.mk' (x * y) z := f.to_localization_map.mul_mk'_eq_mk'_of_mul _ _ _ lemma mk'_eq_mul_mk'_one (x : R) (y : M) : f.mk' x y = f.to_map x * f.mk' 1 y := (f.to_localization_map.mul_mk'_one_eq_mk' _ _).symm @[simp] lemma mk'_mul_cancel_left (x : R) (y : M) : f.mk' (y * x) y = f.to_map x := f.to_localization_map.mk'_mul_cancel_left _ _ lemma mk'_mul_cancel_right (x : R) (y : M) : f.mk' (x * y) y = f.to_map x := f.to_localization_map.mk'_mul_cancel_right _ _ @[simp] lemma mk'_mul_mk'_eq_one (x y : M) : f.mk' x y * f.mk' y x = 1 := by rw [←f.mk'_mul, mul_comm]; exact f.mk'_self _ lemma mk'_mul_mk'_eq_one' (x : R) (y : M) (h : x ∈ M) : f.mk' x y * f.mk' y ⟨x, h⟩ = 1 := f.mk'_mul_mk'_eq_one ⟨x, h⟩ _ lemma is_unit_comp (j : S →+* P) (y : M) : is_unit (j.comp f.to_map y) := f.to_localization_map.is_unit_comp j.to_monoid_hom _ /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g(M) ⊆ units P`, `f x = f y → g x = g y` for all `x y : R`. -/ lemma eq_of_eq {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) {x y} (h : f.to_map x = f.to_map y) : g x = g y := @submonoid.localization_map.eq_of_eq _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg _ _ h lemma mk'_add (x₁ x₂ : R) (y₁ y₂ : M) : f.mk' (x₁ * y₂ + x₂ * y₁) (y₁ * y₂) = f.mk' x₁ y₁ + f.mk' x₂ y₂ := f.mk'_eq_iff_eq_mul.2 $ eq.symm begin rw [mul_comm (_ + _), mul_add, mul_mk'_eq_mk'_of_mul, ←eq_sub_iff_add_eq, mk'_eq_iff_eq_mul, mul_comm _ (f.to_map _), mul_sub, eq_sub_iff_add_eq, ←eq_sub_iff_add_eq', ←mul_assoc, ←f.to_map.map_mul, mul_mk'_eq_mk'_of_mul, mk'_eq_iff_eq_mul], simp only [f.to_map.map_add, submonoid.coe_mul, f.to_map.map_mul], ring_exp, end /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →+* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` sending `z : S` to `g x * (g y)⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) : S →+* P := ring_hom.mk' (@submonoid.localization_map.lift _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg) $ begin intros x y, rw [f.to_localization_map.lift_spec, mul_comm, add_mul, ←sub_eq_iff_eq_add, eq_comm, f.to_localization_map.lift_spec_mul, mul_comm _ (_ - _), sub_mul, eq_sub_iff_add_eq', ←eq_sub_iff_add_eq, mul_assoc, f.to_localization_map.lift_spec_mul], show g _ * (g _ * g _) = g _ * (g _ * g _ - g _ * g _), repeat {rw ←g.map_mul}, rw [←g.map_sub, ←g.map_mul], apply f.eq_of_eq hg, erw [f.to_map.map_mul, sec_spec', mul_sub, f.to_map.map_sub], simp only [f.to_map.map_mul, sec_spec'], ring_exp, end variables {g : R →+* P} (hg : ∀ y : M, is_unit (g y)) /-- Given a localization map `f : R →+* S` for a submonoid `M ⊆ R` and a map of `comm_ring`s `g : R →* P` such that `g y` is invertible for all `y : M`, the homomorphism induced from `S` to `P` maps `f x * (f y)⁻¹` to `g x * (g y)⁻¹` for all `x : R, y ∈ M`. -/ lemma lift_mk' (x y) : f.lift hg (f.mk' x y) = g x * ↑(is_unit.lift_right (g.to_monoid_hom.mrestrict M) hg y)⁻¹ := f.to_localization_map.lift_mk' _ _ _ lemma lift_mk'_spec (x v) (y : M) : f.lift hg (f.mk' x y) = v ↔ g x = g y * v := f.to_localization_map.lift_mk'_spec _ _ _ _ @[simp] lemma lift_eq (x : R) : f.lift hg (f.to_map x) = g x := f.to_localization_map.lift_eq _ _ lemma lift_eq_iff {x y : R × M} : f.lift hg (f.mk' x.1 x.2) = f.lift hg (f.mk' y.1 y.2) ↔ g (x.1 * y.2) = g (y.1 * x.2) := f.to_localization_map.lift_eq_iff _ @[simp] lemma lift_comp : (f.lift hg).comp f.to_map = g := ring_hom.ext $ monoid_hom.ext_iff.1 $ f.to_localization_map.lift_comp _ @[simp] lemma lift_of_comp (j : S →+* P) : f.lift (f.is_unit_comp j) = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ f.to_localization_map.lift_of_comp j.to_monoid_hom lemma epic_of_localization_map {j k : S →+* P} (h : ∀ a, j.comp f.to_map a = k.comp f.to_map a) : j = k := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.epic_of_localization_map _ _ _ _ _ _ _ f.to_localization_map j.to_monoid_hom k.to_monoid_hom h lemma lift_unique {j : S →+* P} (hj : ∀ x, j (f.to_map x) = g x) : f.lift hg = j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.lift_unique _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom hg j.to_monoid_hom hj @[simp] lemma lift_id (x) : f.lift f.map_units x = x := f.to_localization_map.lift_id _ /-- Given two localization maps `f : R →+* S, k : R →+* P` for a submonoid `M ⊆ R`, the hom from `P` to `S` induced by `f` is left inverse to the hom from `S` to `P` induced by `k`. -/ @[simp] lemma lift_left_inverse {k : localization_map M S} (z : S) : k.lift f.map_units (f.lift k.map_units z) = z := f.to_localization_map.lift_left_inverse _ lemma lift_surjective_iff : surjective (f.lift hg) ↔ ∀ v : P, ∃ x : R × M, v * g x.2 = g x.1 := f.to_localization_map.lift_surjective_iff hg lemma lift_injective_iff : injective (f.lift hg) ↔ ∀ x y, f.to_map x = f.to_map y ↔ g x = g y := f.to_localization_map.lift_injective_iff hg variables {T : submonoid P} (hy : ∀ y : M, g y ∈ T) {Q : Type*} [comm_ring Q] (k : localization_map T Q) /-- Given a `comm_ring` homomorphism `g : R →+* P` where for submonoids `M ⊆ R, T ⊆ P` we have `g(M) ⊆ T`, the induced ring homomorphism from the localization of `R` at `M` to the localization of `P` at `T`: if `f : R →+* S` and `k : P →+* Q` are localization maps for `M` and `T` respectively, we send `z : S` to `k (g x) * (k (g y))⁻¹`, where `(x, y) : R × M` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map : S →+* Q := @lift _ _ _ _ _ _ _ f (k.to_map.comp g) $ λ y, k.map_units ⟨g y, hy y⟩ variables {k} lemma map_eq (x) : f.map hy k (f.to_map x) = k.to_map (g x) := f.lift_eq (λ y, k.map_units ⟨g y, hy y⟩) x @[simp] lemma map_comp : (f.map hy k).comp f.to_map = k.to_map.comp g := f.lift_comp $ λ y, k.map_units ⟨g y, hy y⟩ lemma map_mk' (x) (y : M) : f.map hy k (f.mk' x y) = k.mk' (g x) ⟨g y, hy y⟩ := @submonoid.localization_map.map_mk' _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom _ hy _ _ k.to_localization_map _ _ @[simp] lemma map_id (z : S) : f.map (λ y, show ring_hom.id R y ∈ M, from y.2) f z = z := f.lift_id _ /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_comp_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ w : T, l w ∈ U) : (k.map hl j).comp (f.map hy k) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j := ring_hom.ext $ monoid_hom.ext_iff.1 $ @submonoid.localization_map.map_comp_map _ _ _ _ _ _ _ f.to_localization_map g.to_monoid_hom _ hy _ _ k.to_localization_map _ _ _ _ _ j.to_localization_map l.to_monoid_hom hl /-- If `comm_ring` homs `g : R →+* P, l : P →+* A` induce maps of localizations, the composition of the induced maps equals the map of localizations induced by `l ∘ g`. -/ lemma map_map {A : Type*} [comm_ring A] {U : submonoid A} {W} [comm_ring W] (j : localization_map U W) {l : P →+* A} (hl : ∀ w : T, l w ∈ U) (x) : k.map hl j (f.map hy k x) = f.map (λ x, show l.comp g x ∈ U, from hl ⟨g x, hy x⟩) j x := by rw ←f.map_comp_map hy j hl; refl /-- Given localization maps `f : R →+* S, k : P →+* Q` for submonoids `M, T` respectively, an isomorphism `j : R ≃+* P` such that `j(M) = T` induces an isomorphism of localizations `S ≃+* Q`. -/ noncomputable def ring_equiv_of_ring_equiv (k : localization_map T Q) (h : R ≃+* P) (H : M.map h.to_monoid_hom = T) : S ≃+* Q := (f.to_localization_map.mul_equiv_of_mul_equiv k.to_localization_map H).to_ring_equiv $ (@lift _ _ _ _ _ _ _ f (k.to_map.comp h.to_ring_hom) (λ y, k.map_units ⟨(h y), H ▸ set.mem_image_of_mem h y.2⟩)).map_add @[simp] lemma ring_equiv_of_ring_equiv_eq_map_apply {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : f.ring_equiv_of_ring_equiv k j H x = f.map (λ y : M, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k x := rfl lemma ring_equiv_of_ring_equiv_eq_map {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) : (f.ring_equiv_of_ring_equiv k j H).to_monoid_hom = f.map (λ y : M, show j.to_monoid_hom y ∈ T, from H ▸ set.mem_image_of_mem j y.2) k := rfl @[simp] lemma ring_equiv_of_ring_equiv_eq {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x) : f.ring_equiv_of_ring_equiv k j H (f.to_map x) = k.to_map (j x) := f.to_localization_map.mul_equiv_of_mul_equiv_eq H _ lemma ring_equiv_of_ring_equiv_mk' {j : R ≃+* P} (H : M.map j.to_monoid_hom = T) (x y) : f.ring_equiv_of_ring_equiv k j H (f.mk' x y) = k.mk' (j x) ⟨j y, H ▸ set.mem_image_of_mem j y.2⟩ := f.to_localization_map.mul_equiv_of_mul_equiv_mk' H _ _ section away_map variables (x : R) /-- Given `x : R`, the type of homomorphisms `f : R →* S` such that `S` is isomorphic to the localization of `R` at the submonoid generated by `x`. -/ @[reducible] def away_map (S' : Type*) [comm_ring S'] := localization_map (submonoid.powers x) S' variables (F : away_map x S) /-- Given `x : R` and a localization map `F : R →+* S` away from `x`, `inv_self` is `(F x)⁻¹`. -/ noncomputable def away_map.inv_self : S := F.mk' 1 ⟨x, submonoid.mem_powers _⟩ /-- Given `x : R`, a localization map `F : R →+* S` away from `x`, and a map of `comm_ring`s `g : R →+* P` such that `g x` is invertible, the homomorphism induced from `S` to `P` sending `z : S` to `g y * (g x)⁻ⁿ`, where `y : R, n : ℕ` are such that `z = F y * (F x)⁻ⁿ`. -/ noncomputable def away_map.lift (hg : is_unit (g x)) : S →+* P := localization_map.lift F $ λ y, show is_unit (g y.1), begin obtain ⟨n, hn⟩ := y.2, rw [←hn, g.map_pow], exact is_unit.map (monoid_hom.of $ ((^ n) : P → P)) hg, end @[simp] lemma away_map.lift_eq (hg : is_unit (g x)) (a : R) : F.lift x hg (F.to_map a) = g a := lift_eq _ _ _ @[simp] lemma away_map.lift_comp (hg : is_unit (g x)) : (F.lift x hg).comp F.to_map = g := lift_comp _ _ /-- Given `x y : R` and localization maps `F : R →+* S, G : R →+* P` away from `x` and `x * y` respectively, the homomorphism induced from `S` to `P`. -/ noncomputable def away_to_away_right (y : R) (G : away_map (x * y) P) : S →* P := F.lift x $ show is_unit (G.to_map x), from is_unit_of_mul_eq_one (G.to_map x) (G.mk' y ⟨x * y, submonoid.mem_powers _⟩) $ by rw [mul_mk'_eq_mk'_of_mul, mk'_self] end away_map end localization_map namespace localization variables {M} instance : has_add (localization M) := ⟨λ z w, con.lift_on₂ z w (λ x y : R × M, mk ((x.2 : R) * y.1 + y.2 * x.1) (x.2 * y.2)) $ λ r1 r2 r3 r4 h1 h2, (con.eq _).2 begin rw r_eq_r' at h1 h2 ⊢, cases h1 with t₅ ht₅, cases h2 with t₆ ht₆, use t₆ * t₅, calc ((r1.2 : R) * r2.1 + r2.2 * r1.1) * (r3.2 * r4.2) * (t₆ * t₅) = (r2.1 * r4.2 * t₆) * (r1.2 * r3.2 * t₅) + (r1.1 * r3.2 * t₅) * (r2.2 * r4.2 * t₆) : by ring ... = (r3.2 * r4.1 + r4.2 * r3.1) * (r1.2 * r2.2) * (t₆ * t₅) : by rw [ht₆, ht₅]; ring end⟩ instance : has_neg (localization M) := ⟨λ z, con.lift_on z (λ x : R × M, mk (-x.1) x.2) $ λ r1 r2 h, (con.eq _).2 begin rw r_eq_r' at h ⊢, cases h with t ht, use t, rw [neg_mul_eq_neg_mul_symm, neg_mul_eq_neg_mul_symm, ht], ring, end⟩ instance : has_zero (localization M) := ⟨mk 0 1⟩ private meta def tac := `[{ intros, refine quotient.sound' (r_of_eq _), simp only [prod.snd_mul, prod.fst_mul, submonoid.coe_mul], ring }] instance : comm_ring (localization M) := { zero := 0, one := 1, add := (+), mul := (*), add_assoc := λ m n k, quotient.induction_on₃' m n k (by tac), zero_add := λ y, quotient.induction_on' y (by tac), add_zero := λ y, quotient.induction_on' y (by tac), neg := has_neg.neg, sub := λ x y, x + -y, sub_eq_add_neg := λ x y, rfl, add_left_neg := λ y, quotient.induction_on' y (by tac), add_comm := λ y z, quotient.induction_on₂' z y (by tac), left_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), right_distrib := λ m n k, quotient.induction_on₃' m n k (by tac), ..localization.comm_monoid M } variables (M) /-- Natural hom sending `x : R`, `R` a `comm_ring`, to the equivalence class of `(x, 1)` in the localization of `R` at a submonoid. -/ def of : localization_map M (localization M) := (localization.monoid_of M).to_ring_localization $ λ x y, (con.eq _).2 $ r_of_eq $ by simp [add_comm] variables {M} lemma monoid_of_eq_of (x) : (monoid_of M).to_map x = (of M).to_map x := rfl lemma mk_one_eq_of (x) : mk x 1 = (of M).to_map x := rfl lemma mk_eq_mk'_apply (x y) : mk x y = (of M).mk' x y := mk_eq_monoid_of_mk'_apply _ _ @[simp] lemma mk_eq_mk' : mk = (of M).mk' := mk_eq_monoid_of_mk' variables (f : localization_map M S) /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ noncomputable def ring_equiv_of_quotient : localization M ≃+* S := (mul_equiv_of_quotient f.to_localization_map).to_ring_equiv $ ((of M).lift f.map_units).map_add variables {f} @[simp] lemma ring_equiv_of_quotient_apply (x) : ring_equiv_of_quotient f x = (of M).lift f.map_units x := rfl @[simp] lemma ring_equiv_of_quotient_mk' (x y) : ring_equiv_of_quotient f ((of M).mk' x y) = f.mk' x y := mul_equiv_of_quotient_mk' _ _ lemma ring_equiv_of_quotient_mk (x y) : ring_equiv_of_quotient f (mk x y) = f.mk' x y := mul_equiv_of_quotient_mk _ _ @[simp] lemma ring_equiv_of_quotient_of (x) : ring_equiv_of_quotient f ((of M).to_map x) = f.to_map x := mul_equiv_of_quotient_monoid_of _ @[simp] lemma ring_equiv_of_quotient_symm_mk' (x y) : (ring_equiv_of_quotient f).symm (f.mk' x y) = (of M).mk' x y := mul_equiv_of_quotient_symm_mk' _ _ lemma ring_equiv_of_quotient_symm_mk (x y) : (ring_equiv_of_quotient f).symm (f.mk' x y) = mk x y := mul_equiv_of_quotient_symm_mk _ _ @[simp] lemma ring_equiv_of_quotient_symm_of (x) : (ring_equiv_of_quotient f).symm (f.to_map x) = (of M).to_map x := mul_equiv_of_quotient_symm_monoid_of _ section away variables (x : R) /-- Given `x : R`, the natural hom sending `y : R`, `R` a `comm_ring`, to the equivalence class of `(y, 1)` in the localization of `R` at the submonoid generated by `x`. -/ @[reducible] def away.of : localization_map.away_map x (away x) := of (submonoid.powers x) @[simp] lemma away.mk_eq_mk' : mk = (away.of x).mk' := mk_eq_mk' /-- Given `x : R` and a localization map `f : R →+* S` away from `x`, we get an isomorphism between the localization of `R` at the submonoid generated by `x` as a quotient type and `S`. -/ noncomputable def away.ring_equiv_of_quotient (f : localization_map.away_map x S) : away x ≃+* S := ring_equiv_of_quotient f end away end localization variables {M} section at_prime variables (I : ideal R) [hp : I.is_prime] include hp namespace ideal /-- The complement of a prime ideal `I ⊆ R` is a submonoid of `R`. -/ def prime_compl : submonoid R := { carrier := (Iᶜ : set R), one_mem' := by convert I.ne_top_iff_one.1 hp.1; refl, mul_mem' := λ x y hnx hny hxy, or.cases_on (hp.mem_or_mem hxy) hnx hny } end ideal namespace localization_map variables (S) /-- A localization map from `R` to `S` where the submonoid is the complement of a prime ideal of `R`. -/ @[reducible] def at_prime := localization_map I.prime_compl S end localization_map namespace localization /-- The localization of `R` at the complement of a prime ideal, as a quotient type. -/ @[reducible] def at_prime := localization I.prime_compl end localization namespace localization_map variables {I} /-- When `f` is a localization map from `R` at the complement of a prime ideal `I`, we use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `local_ring` instance on `S` can 'know' the map needed to induce the instance. -/ instance at_prime.local_ring (f : at_prime S I) : local_ring f.codomain := local_of_nonunits_ideal (λ hze, begin rw [←f.to_map.map_one, ←f.to_map.map_zero] at hze, obtain ⟨t, ht⟩ := f.eq_iff_exists.1 hze, exact ((show (t : R) ∉ I, from t.2) (have htz : (t : R) = 0, by simpa using ht.symm, htz.symm ▸ I.zero_mem)) end) (begin intros x y hx hy hu, cases is_unit_iff_exists_inv.1 hu with z hxyz, have : ∀ {r s}, f.mk' r s ∈ nonunits S → r ∈ I, from λ r s, not_imp_comm.1 (λ nr, is_unit_iff_exists_inv.2 ⟨f.mk' s ⟨r, nr⟩, f.mk'_mul_mk'_eq_one' _ _ nr⟩), rcases f.mk'_surjective x with ⟨rx, sx, hrx⟩, rcases f.mk'_surjective y with ⟨ry, sy, hry⟩, rcases f.mk'_surjective z with ⟨rz, sz, hrz⟩, rw [←hrx, ←hry, ←hrz, ←f.mk'_add, ←f.mk'_mul, ←f.mk'_self I.prime_compl.one_mem] at hxyz, rw ←hrx at hx, rw ←hry at hy, cases f.eq.1 hxyz with t ht, simp only [mul_one, one_mul, submonoid.coe_mul, subtype.coe_mk] at ht, rw [←sub_eq_zero, ←sub_mul] at ht, have hr := (hp.mem_or_mem_of_mul_eq_zero ht).resolve_right t.2, rw sub_eq_add_neg at hr, have := I.neg_mem_iff.1 ((ideal.add_mem_iff_right _ _).1 hr), { exact not_or (mt hp.mem_or_mem (not_or sx.2 sy.2)) sz.2 (hp.mem_or_mem this)}, { exact I.mul_mem_right _ (I.add_mem (I.mul_mem_right _ (this hx)) (I.mul_mem_right _ (this hy)))} end) end localization_map namespace localization /-- The localization of `R` at the complement of a prime ideal is a local ring. -/ instance at_prime.local_ring : local_ring (localization I.prime_compl) := localization_map.at_prime.local_ring (of I.prime_compl) end localization end at_prime namespace localization_map variables (f : localization_map M S) section ideals /-- Explicit characterization of the ideal given by `ideal.map f.to_map I`. In practice, this ideal differs only in that the carrier set is defined explicitly. This definition is only meant to be used in proving `mem_map_to_map_iff`, and any proof that needs to refer to the explicit carrier set should use that theorem. -/ private def to_map_ideal (I : ideal R) : ideal S := { carrier := { z : S | ∃ x : I × M, z * (f.to_map x.2) = f.to_map x.1}, zero_mem' := ⟨⟨0, 1⟩, by simp⟩, add_mem' := begin rintros a b ⟨a', ha⟩ ⟨b', hb⟩, use ⟨a'.2 * b'.1 + b'.2 * a'.1, I.add_mem (I.mul_mem_left _ b'.1.2) (I.mul_mem_left _ a'.1.2)⟩, use a'.2 * b'.2, simp only [ring_hom.map_add, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], rw [add_mul, ← mul_assoc a, ha, mul_comm (f.to_map a'.2) (f.to_map b'.2), ← mul_assoc b, hb], ring end, smul_mem' := begin rintros c x ⟨x', hx⟩, obtain ⟨c', hc⟩ := localization_map.surj f c, use ⟨c'.1 * x'.1, I.mul_mem_left c'.1 x'.1.2⟩, use c'.2 * x'.2, simp only [←hx, ←hc, smul_eq_mul, submodule.coe_mk, submonoid.coe_mul, ring_hom.map_mul], ring end } theorem mem_map_to_map_iff {I : ideal R} {z} : z ∈ ideal.map f.to_map I ↔ ∃ x : I × M, z * (f.to_map x.2) = f.to_map x.1 := begin split, { show _ → z ∈ to_map_ideal f I, refine λ h, ideal.mem_Inf.1 h (λ z hz, _), obtain ⟨y, hy⟩ := hz, use ⟨⟨⟨y, hy.left⟩, 1⟩, by simp [hy.right]⟩ }, { rintros ⟨⟨a, s⟩, h⟩, rw [← ideal.unit_mul_mem_iff_mem _ (map_units f s), mul_comm], exact h.symm ▸ ideal.mem_map_of_mem a.2 } end theorem map_comap (J : ideal S) : ideal.map f.to_map (ideal.comap f.to_map J) = J := le_antisymm (ideal.map_le_iff_le_comap.2 (le_refl _)) $ λ x hJ, begin obtain ⟨r, s, hx⟩ := f.mk'_surjective x, rw ←hx at ⊢ hJ, exact ideal.mul_mem_right _ _ (ideal.mem_map_of_mem (show f.to_map r ∈ J, from f.mk'_spec r s ▸ J.mul_mem_right (f.to_map s) hJ)), end theorem comap_map_of_is_prime_disjoint (I : ideal R) (hI : I.is_prime) (hM : disjoint (M : set R) I) : ideal.comap f.to_map (ideal.map f.to_map I) = I := begin refine le_antisymm (λ a ha, _) ideal.le_comap_map, rw [ideal.mem_comap, mem_map_to_map_iff] at ha, obtain ⟨⟨b, s⟩, h⟩ := ha, have : f.to_map (a * ↑s - b) = 0 := by simpa [sub_eq_zero] using h, rw [← f.to_map.map_zero, eq_iff_exists] at this, obtain ⟨c, hc⟩ := this, have : a * s ∈ I, { rw zero_mul at hc, let this : (a * ↑s - ↑b) * ↑c ∈ I := hc.symm ▸ I.zero_mem, cases hI.mem_or_mem this with h1 h2, { simpa using I.add_mem h1 b.2 }, { exfalso, refine hM ⟨c.2, h2⟩ } }, cases hI.mem_or_mem this with h1 h2, { exact h1 }, { exfalso, refine hM ⟨s.2, h2⟩ } end /-- If `S` is the localization of `R` at a submonoid, the ordering of ideals of `S` is embedded in the ordering of ideals of `R`. -/ def order_embedding : ideal S ↪o ideal R := { to_fun := λ J, ideal.comap f.to_map J, inj' := function.left_inverse.injective f.map_comap, map_rel_iff' := λ J₁ J₂, ⟨λ hJ, f.map_comap J₁ ▸ f.map_comap J₂ ▸ ideal.map_mono hJ, ideal.comap_mono⟩ } /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its comap, see `le_rel_iso_of_prime` for the more general relation isomorphism -/ lemma is_prime_iff_is_prime_disjoint (J : ideal S) : J.is_prime ↔ (ideal.comap f.to_map J).is_prime ∧ disjoint (M : set R) ↑(ideal.comap f.to_map J) := begin split, { refine λ h, ⟨⟨_, _⟩, λ m hm, h.ne_top (ideal.eq_top_of_is_unit_mem _ hm.2 (map_units f ⟨m, hm.left⟩))⟩, { refine λ hJ, h.ne_top _, rw [eq_top_iff, ← f.order_embedding.le_iff_le], exact le_of_eq hJ.symm }, { intros x y hxy, rw [ideal.mem_comap, ring_hom.map_mul] at hxy, exact h.mem_or_mem hxy } }, { refine λ h, ⟨λ hJ, h.left.ne_top (eq_top_iff.2 _), _⟩, { rwa [eq_top_iff, ← f.order_embedding.le_iff_le] at hJ }, { intros x y hxy, obtain ⟨a, s, ha⟩ := mk'_surjective f x, obtain ⟨b, t, hb⟩ := mk'_surjective f y, have : f.mk' (a * b) (s * t) ∈ J := by rwa [mk'_mul, ha, hb], rw [mk'_mem_iff, ← ideal.mem_comap] at this, replace this := h.left.mem_or_mem this, rw [ideal.mem_comap, ideal.mem_comap] at this, rwa [← ha, ← hb, mk'_mem_iff, mk'_mem_iff] } } end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M`. This lemma gives the particular case for an ideal and its map, see `le_rel_iso_of_prime` for the more general relation isomorphism, and the reverse implication -/ lemma is_prime_of_is_prime_disjoint (I : ideal R) (hp : I.is_prime) (hd : disjoint (M : set R) ↑I) : (ideal.map f.to_map I).is_prime := begin rw [is_prime_iff_is_prime_disjoint f, comap_map_of_is_prime_disjoint f I hp hd], exact ⟨hp, hd⟩ end /-- If `R` is a ring, then prime ideals in the localization at `M` correspond to prime ideals in the original ring `R` that are disjoint from `M` -/ def order_iso_of_prime (f : localization_map M S) : {p : ideal S // p.is_prime} ≃o {p : ideal R // p.is_prime ∧ disjoint (M : set R) ↑p} := { to_fun := λ p, ⟨ideal.comap f.to_map p.1, (is_prime_iff_is_prime_disjoint f p.1).1 p.2⟩, inv_fun := λ p, ⟨ideal.map f.to_map p.1, is_prime_of_is_prime_disjoint f p.1 p.2.1 p.2.2⟩, left_inv := λ J, subtype.eq (map_comap f J), right_inv := λ I, subtype.eq (comap_map_of_is_prime_disjoint f I.1 I.2.1 I.2.2), map_rel_iff' := λ I I', ⟨λ h, (show I.val ≤ I'.val, from (map_comap f I.val) ▸ (map_comap f I'.val) ▸ (ideal.map_mono h)), λ h x hx, h hx⟩ } /-- `quotient_map` applied to maximal ideals of a localization is `surjective`. The quotient by a maximal ideal is a field, so inverses to elements already exist, and the localization necessarily maps the equivalence class of the inverse in the localization -/ lemma surjective_quotient_map_of_maximal_of_localization {f : localization_map M S} {I : ideal S} [I.is_prime] {J : ideal R} {H : J ≤ I.comap f.to_map} (hI : (I.comap f.to_map).is_maximal) : function.surjective (I.quotient_map f.to_map H) := begin intro s, obtain ⟨s, rfl⟩ := ideal.quotient.mk_surjective s, obtain ⟨r, ⟨m, hm⟩, rfl⟩ := f.mk'_surjective s, by_cases hM : (ideal.quotient.mk (I.comap f.to_map)) m = 0, { have : I = ⊤, { rw ideal.eq_top_iff_one, rw [ideal.quotient.eq_zero_iff_mem, ideal.mem_comap] at hM, convert I.mul_mem_right (f.mk' 1 ⟨m, hm⟩) hM, rw [← f.mk'_eq_mul_mk'_one, f.mk'_self] }, exact ⟨0, eq_comm.1 (by simp [ideal.quotient.eq_zero_iff_mem, this])⟩ }, { rw ideal.quotient.maximal_ideal_iff_is_field_quotient at hI, obtain ⟨n, hn⟩ := hI.3 hM, obtain ⟨rn, rfl⟩ := ideal.quotient.mk_surjective n, refine ⟨(ideal.quotient.mk J) (r * rn), _⟩, -- The rest of the proof is essentially just algebraic manipulations to prove the equality rw ← ring_hom.map_mul at hn, replace hn := congr_arg (ideal.quotient_map I f.to_map le_rfl) hn, simp only [ring_hom.map_one, ideal.quotient_map_mk, ring_hom.map_mul] at hn, rw [ideal.quotient_map_mk, ← sub_eq_zero_iff_eq, ← ring_hom.map_sub, ideal.quotient.eq_zero_iff_mem, ← ideal.quotient.eq_zero_iff_mem, ring_hom.map_sub, sub_eq_zero_iff_eq, localization_map.mk'_eq_mul_mk'_one], simp only [mul_eq_mul_left_iff, ring_hom.map_mul], exact or.inl (mul_left_cancel' (λ hn, hM (ideal.quotient.eq_zero_iff_mem.2 (ideal.mem_comap.2 (ideal.quotient.eq_zero_iff_mem.1 hn)))) (trans hn (by rw [← ring_hom.map_mul, ← f.mk'_eq_mul_mk'_one, f.mk'_self, ring_hom.map_one]))) } end end ideals /-! ### `algebra` section Defines the `R`-algebra instance on a copy of `S` carrying the data of the localization map `f` needed to induce the `R`-algebra structure. -/ /-- We use a copy of the localization map `f`'s codomain `S` carrying the data of `f` so that the `R`-algebra instance on `S` can 'know' the map needed to induce the `R`-algebra structure. -/ instance algebra : algebra R f.codomain := f.to_map.to_algebra end localization_map namespace localization instance : algebra R (localization M) := localization_map.algebra (of M) end localization namespace localization_map variables (f : localization_map M S) @[simp] lemma of_id (a : R) : (algebra.of_id R f.codomain) a = f.to_map a := rfl @[simp] lemma algebra_map_eq : algebra_map R f.codomain = f.to_map := rfl variables (f) /-- Localization map `f` from `R` to `S` as an `R`-linear map. -/ def lin_coe : R →ₗ[R] f.codomain := { to_fun := f.to_map, map_add' := f.to_map.map_add, map_smul' := f.to_map.map_mul } /-- Map from ideals of `R` to submodules of `S` induced by `f`. -/ -- This was previously a `has_coe` instance, but if `f.codomain = R` then this will loop. -- It could be a `has_coe_t` instance, but we keep it explicit here to avoid slowing down -- the rest of the library. def coe_submodule (I : ideal R) : submodule R f.codomain := submodule.map f.lin_coe I variables {f} lemma mem_coe_submodule (I : ideal R) {x : S} : x ∈ f.coe_submodule I ↔ ∃ y : R, y ∈ I ∧ f.to_map y = x := iff.rfl @[simp] lemma lin_coe_apply {x} : f.lin_coe x = f.to_map x := rfl variables {g : R →+* P} variables {T : submonoid P} (hy : ∀ y : M, g y ∈ T) {Q : Type*} [comm_ring Q] (k : localization_map T Q) lemma map_smul (x : f.codomain) (z : R) : f.map hy k (z • x : f.codomain) = @has_scalar.smul P k.codomain _ (g z) (f.map hy k x) := show f.map hy k (f.to_map z * x) = k.to_map (g z) * f.map hy k x, by rw [ring_hom.map_mul, map_eq] lemma is_noetherian_ring (h : is_noetherian_ring R) : is_noetherian_ring f.codomain := begin rw [is_noetherian_ring_iff, is_noetherian_iff_well_founded] at h ⊢, exact order_embedding.well_founded (f.order_embedding.dual) h end end localization_map namespace localization variables (f : localization_map M S) /-- Given a localization map `f : R →+* S` for a submonoid `M`, we get an `R`-preserving isomorphism between the localization of `R` at `M` as a quotient type and `S`. -/ noncomputable def alg_equiv_of_quotient : localization M ≃ₐ[R] f.codomain := { commutes' := ring_equiv_of_quotient_of, ..ring_equiv_of_quotient f } lemma alg_equiv_of_quotient_apply (x : localization M) : alg_equiv_of_quotient f x = ring_equiv_of_quotient f x := rfl lemma alg_equiv_of_quotient_symm_apply (x : f.codomain) : (alg_equiv_of_quotient f).symm x = (ring_equiv_of_quotient f).symm x := rfl end localization namespace localization_map section integer_normalization variables {f : localization_map M S} open finsupp polynomial open_locale classical /-- `coeff_integer_normalization p` gives the coefficients of the polynomial `integer_normalization p` -/ noncomputable def coeff_integer_normalization (p : polynomial f.codomain) (i : ℕ) : R := if hi : i ∈ p.support then classical.some (classical.some_spec (f.exist_integer_multiples_of_finset (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) else 0 lemma coeff_integer_normalization_mem_support (p : polynomial f.codomain) (i : ℕ) (h : coeff_integer_normalization p i ≠ 0) : i ∈ p.support := begin contrapose h, rw [ne.def, not_not, coeff_integer_normalization, dif_neg h] end /-- `integer_normalization g` normalizes `g` to have integer coefficients by clearing the denominators -/ noncomputable def integer_normalization (f : localization_map M S) : polynomial f.codomain → polynomial R := λ p, on_finset p.support (coeff_integer_normalization p) (coeff_integer_normalization_mem_support p) @[simp] lemma integer_normalization_coeff (p : polynomial f.codomain) (i : ℕ) : (f.integer_normalization p).coeff i = coeff_integer_normalization p i := rfl lemma integer_normalization_spec (p : polynomial f.codomain) : ∃ (b : M), ∀ i, f.to_map ((f.integer_normalization p).coeff i) = f.to_map b * p.coeff i := begin use classical.some (f.exist_integer_multiples_of_finset (p.support.image p.coeff)), intro i, rw [integer_normalization_coeff, coeff_integer_normalization], split_ifs with hi, { exact classical.some_spec (classical.some_spec (f.exist_integer_multiples_of_finset (p.support.image p.coeff)) (p.coeff i) (finset.mem_image.mpr ⟨i, hi, rfl⟩)) }, { convert (_root_.mul_zero (f.to_map _)).symm, { exact f.to_ring_hom.map_zero }, { exact finsupp.not_mem_support_iff.mp hi } } end lemma integer_normalization_map_to_map (p : polynomial f.codomain) : ∃ (b : M), (f.integer_normalization p).map f.to_map = f.to_map b • p := let ⟨b, hb⟩ := integer_normalization_spec p in ⟨b, polynomial.ext (λ i, by { rw coeff_map, exact hb i })⟩ variables {R' : Type*} [comm_ring R'] lemma integer_normalization_eval₂_eq_zero (g : f.codomain →+* R') (p : polynomial f.codomain) {x : R'} (hx : eval₂ g x p = 0) : eval₂ (g.comp f.to_map) x (f.integer_normalization p) = 0 := let ⟨b, hb⟩ := integer_normalization_map_to_map p in trans (eval₂_map f.to_map g x).symm (by rw [hb, eval₂_smul, hx, mul_zero]) lemma integer_normalization_aeval_eq_zero [algebra R R'] [algebra f.codomain R'] [is_scalar_tower R f.codomain R'] (p : polynomial f.codomain) {x : R'} (hx : aeval x p = 0) : aeval x (f.integer_normalization p) = 0 := by rw [aeval_def, is_scalar_tower.algebra_map_eq R f.codomain R', algebra_map_eq, integer_normalization_eval₂_eq_zero _ _ hx] end integer_normalization variables {R} {A K : Type*} [integral_domain A] lemma to_map_eq_zero_iff (f : localization_map M S) {x : R} (hM : M ≤ non_zero_divisors R) : f.to_map x = 0 ↔ x = 0 := begin rw ← f.to_map.map_zero, split; intro h, { cases f.eq_iff_exists.mp h with c hc, rw zero_mul at hc, exact hM c.2 x hc }, { rw h }, end lemma injective (f : localization_map M S) (hM : M ≤ non_zero_divisors R) : injective f.to_map := begin rw ring_hom.injective_iff f.to_map, intros a ha, rw [← f.to_map.map_zero, f.eq_iff_exists] at ha, cases ha with c hc, rw zero_mul at hc, exact hM c.2 a hc, end protected lemma to_map_ne_zero_of_mem_non_zero_divisors [nontrivial R] (f : localization_map M S) (hM : M ≤ non_zero_divisors R) (x : non_zero_divisors R) : f.to_map x ≠ 0 := map_ne_zero_of_mem_non_zero_divisors (f.injective hM) /-- A `comm_ring` `S` which is the localization of an integral domain `R` at a subset of non-zero elements is an integral domain. -/ def integral_domain_of_le_non_zero_divisors {M : submonoid A} (f : localization_map M S) (hM : M ≤ non_zero_divisors A) : integral_domain S := { eq_zero_or_eq_zero_of_mul_eq_zero := begin intros z w h, cases f.surj z with x hx, cases f.surj w with y hy, have : z * w * f.to_map y.2 * f.to_map x.2 = f.to_map x.1 * f.to_map y.1, by rw [mul_assoc z, hy, ←hx]; ac_refl, rw [h, zero_mul, zero_mul, ← f.to_map.map_mul] at this, cases eq_zero_or_eq_zero_of_mul_eq_zero ((to_map_eq_zero_iff f hM).mp this.symm) with H H, { exact or.inl (f.eq_zero_of_fst_eq_zero hx H) }, { exact or.inr (f.eq_zero_of_fst_eq_zero hy H) }, end, exists_pair_ne := ⟨f.to_map 0, f.to_map 1, λ h, zero_ne_one (f.injective hM h)⟩, ..(infer_instance : comm_ring S) } /-- The localization at of an integral domain to a set of non-zero elements is an integral domain -/ def integral_domain_localization {M : submonoid A} (hM : M ≤ non_zero_divisors A) : integral_domain (localization M) := (localization.of M).integral_domain_of_le_non_zero_divisors hM /-- The localization of an integral domain at the complement of a prime ideal is an integral domain. -/ instance integral_domain_of_local_at_prime {P : ideal A} (hp : P.is_prime) : integral_domain (localization.at_prime P) := integral_domain_localization (le_non_zero_divisors_of_domain (by simpa only [] using P.zero_mem)) end localization_map section at_prime namespace localization local attribute [instance] classical.prop_decidable /-- The image of `P` in the localization at `P.prime_compl` is a maximal ideal, and in particular it is the unique maximal ideal given by the local ring structure `at_prime.local_ring` -/ lemma at_prime.map_eq_maximal_ideal {P : ideal R} [hP : ideal.is_prime P] : ideal.map (localization.of P.prime_compl).to_map P = (local_ring.maximal_ideal (localization P.prime_compl)) := begin let f := localization.of P.prime_compl, ext x, split; simp only [local_ring.mem_maximal_ideal, mem_nonunits_iff]; intro hx, { exact λ h, (localization_map.is_prime_of_is_prime_disjoint f P hP disjoint_compl_left).ne_top (ideal.eq_top_of_is_unit_mem _ hx h) }, { obtain ⟨⟨a, b⟩, hab⟩ := localization_map.surj f x, contrapose! hx, rw is_unit_iff_exists_inv, rw localization_map.mem_map_to_map_iff at hx, obtain ⟨a', ha'⟩ := is_unit_iff_exists_inv.1 (localization_map.map_units f ⟨a, λ ha, hx ⟨⟨⟨a, ha⟩, b⟩, hab⟩⟩), exact ⟨f.to_map b * a', by rwa [← mul_assoc, hab]⟩ } end /-- The unique maximal ideal of the localization at `P.prime_compl` lies over the ideal `P`. -/ lemma at_prime.comap_maximal_ideal {P : ideal R} [ideal.is_prime P] : ideal.comap (localization.of P.prime_compl).to_map (local_ring.maximal_ideal (localization P.prime_compl)) = P := begin let Pₚ := local_ring.maximal_ideal (localization P.prime_compl), refine le_antisymm (λ x hx, _) (le_trans ideal.le_comap_map (ideal.comap_mono (le_of_eq at_prime.map_eq_maximal_ideal))), by_cases h0 : x = 0, { exact h0.symm ▸ P.zero_mem }, { have : Pₚ.is_prime := ideal.is_maximal.is_prime (local_ring.maximal_ideal.is_maximal _), rw localization_map.is_prime_iff_is_prime_disjoint (localization.of P.prime_compl) at this, contrapose! h0 with hx', simpa using this.2 ⟨hx', hx⟩ } end end localization end at_prime /-- If `R` is a field, then localizing at a submonoid not containing `0` adds no new elements. -/ lemma localization_map_bijective_of_field {R Rₘ : Type*} [integral_domain R] [comm_ring Rₘ] {M : submonoid R} (hM : (0 : R) ∉ M) (hR : is_field R) (f : localization_map M Rₘ) : function.bijective f.to_map := begin refine ⟨f.injective (le_non_zero_divisors_of_domain hM), λ x, _⟩, obtain ⟨r, ⟨m, hm⟩, rfl⟩ := f.mk'_surjective x, obtain ⟨n, hn⟩ := hR.mul_inv_cancel (λ hm0, hM (hm0 ▸ hm) : m ≠ 0), exact ⟨r * n, by erw [f.eq_mk'_iff_mul_eq, ← f.to_map.map_mul, mul_assoc, mul_comm n, hn, mul_one]⟩ end variables (R) {A : Type*} [integral_domain A] variables (K : Type*) /-- Localization map from an integral domain `R` to its field of fractions. -/ @[reducible] def fraction_map [comm_ring K] := localization_map (non_zero_divisors R) K namespace fraction_map open localization_map variables {R K} lemma to_map_eq_zero_iff [comm_ring K] (φ : fraction_map R K) {x : R} : φ.to_map x = 0 ↔ x = 0 := φ.to_map_eq_zero_iff (le_of_eq rfl) protected theorem injective [comm_ring K] (φ : fraction_map R K) : function.injective φ.to_map := φ.injective (le_of_eq rfl) protected lemma to_map_ne_zero_of_mem_non_zero_divisors [nontrivial R] [comm_ring K] (φ : fraction_map R K) (x : non_zero_divisors R) : φ.to_map x ≠ 0 := φ.to_map_ne_zero_of_mem_non_zero_divisors (le_of_eq rfl) x /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is an integral domain. -/ def to_integral_domain [comm_ring K] (φ : fraction_map A K) : integral_domain K := φ.integral_domain_of_le_non_zero_divisors (le_of_eq rfl) local attribute [instance] classical.dec_eq /-- The inverse of an element in the field of fractions of an integral domain. -/ protected noncomputable def inv [comm_ring K] (φ : fraction_map A K) (z : K) : K := if h : z = 0 then 0 else φ.mk' (φ.to_localization_map.sec z).2 ⟨(φ.to_localization_map.sec z).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, h $ φ.eq_zero_of_fst_eq_zero (sec_spec z) h0⟩ protected lemma mul_inv_cancel [comm_ring K] (φ : fraction_map A K) (x : K) (hx : x ≠ 0) : x * φ.inv x = 1 := show x * dite _ _ _ = 1, by rw [dif_neg hx, ←is_unit.mul_left_inj (φ.map_units ⟨(φ.to_localization_map.sec x).1, mem_non_zero_divisors_iff_ne_zero.2 $ λ h0, hx $ φ.eq_zero_of_fst_eq_zero (sec_spec x) h0⟩), one_mul, mul_assoc, mk'_spec, ←eq_mk'_iff_mul_eq]; exact (φ.mk'_sec x).symm /-- A `comm_ring` `K` which is the localization of an integral domain `R` at `R - {0}` is a field. -/ noncomputable def to_field [comm_ring K] (φ : fraction_map A K) : field K := { inv := φ.inv, mul_inv_cancel := φ.mul_inv_cancel, inv_zero := dif_pos rfl, ..φ.to_integral_domain } variables {B : Type*} [integral_domain B] [field K] {L : Type*} [field L] (f : fraction_map A K) {g : A →+* L} lemma mk'_eq_div {r s} : f.mk' r s = f.to_map r / f.to_map s := f.mk'_eq_iff_eq_mul.2 $ (div_mul_cancel _ (f.to_map_ne_zero_of_mem_non_zero_divisors _)).symm lemma is_unit_map_of_injective (hg : function.injective g) (y : non_zero_divisors A) : is_unit (g y) := is_unit.mk0 (g y) $ map_ne_zero_of_mem_non_zero_divisors hg /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, we get a field hom sending `z : K` to `g x * (g y)⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def lift (hg : injective g) : K →+* L := f.lift $ is_unit_map_of_injective hg /-- Given an integral domain `A`, a localization map to its fields of fractions `f : A →+* K`, and an injective ring hom `g : A →+* L` where `L` is a field, field hom induced from `K` to `L` maps `f x / f y` to `g x / g y` for all `x : A, y ∈ non_zero_divisors A`. -/ @[simp] lemma lift_mk' (hg : injective g) (x y) : f.lift hg (f.mk' x y) = g x / g y := begin erw f.lift_mk' (is_unit_map_of_injective hg), erw submonoid.localization_map.mul_inv_left (λ y : non_zero_divisors A, show is_unit (g.to_monoid_hom y), from is_unit_map_of_injective hg y), exact (mul_div_cancel' _ (map_ne_zero_of_mem_non_zero_divisors hg)).symm, end /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L` and an injective ring hom `j : A →+* B`, we get a field hom sending `z : K` to `g (j x) * (g (j y))⁻¹`, where `(x, y) : A × (non_zero_divisors A)` are such that `z = f x * (f y)⁻¹`. -/ noncomputable def map (g : fraction_map B L) {j : A →+* B} (hj : injective j) : K →+* L := f.map (λ y, mem_non_zero_divisors_iff_ne_zero.2 $ map_ne_zero_of_mem_non_zero_divisors hj) g /-- Given integral domains `A, B` and localization maps to their fields of fractions `f : A →+* K, g : B →+* L`, an isomorphism `j : A ≃+* B` induces an isomorphism of fields of fractions `K ≃+* L`. -/ noncomputable def field_equiv_of_ring_equiv (g : fraction_map B L) (h : A ≃+* B) : K ≃+* L := f.ring_equiv_of_ring_equiv g h begin ext b, show b ∈ h.to_equiv '' _ ↔ _, erw [h.to_equiv.image_eq_preimage, set.preimage, set.mem_set_of_eq, mem_non_zero_divisors_iff_ne_zero, mem_non_zero_divisors_iff_ne_zero], exact h.symm.map_ne_zero_iff end /-- The cast from `int` to `rat` as a `fraction_map`. -/ def int.fraction_map : fraction_map ℤ ℚ := { to_fun := coe, map_units' := begin rintro ⟨x, hx⟩, rw mem_non_zero_divisors_iff_ne_zero at hx, simpa only [is_unit_iff_ne_zero, int.cast_eq_zero, ne.def, subtype.coe_mk] using hx, end, surj' := begin rintro ⟨n, d, hd, h⟩, refine ⟨⟨n, ⟨d, _⟩⟩, rat.mul_denom_eq_num⟩, rwa [mem_non_zero_divisors_iff_ne_zero, int.coe_nat_ne_zero_iff_pos] end, eq_iff_exists' := begin intros x y, rw [int.cast_inj], refine ⟨by { rintro rfl, use 1 }, _⟩, rintro ⟨⟨c, hc⟩, h⟩, apply int.eq_of_mul_eq_mul_right _ h, rwa mem_non_zero_divisors_iff_ne_zero at hc, end, ..int.cast_ring_hom ℚ } lemma integer_normalization_eq_zero_iff {p : polynomial f.codomain} : f.integer_normalization p = 0 ↔ p = 0 := begin refine (polynomial.ext_iff.trans (polynomial.ext_iff.trans _).symm), obtain ⟨⟨b, nonzero⟩, hb⟩ := integer_normalization_spec p, split; intros h i, { apply f.to_map_eq_zero_iff.mp, rw [hb i, h i], exact _root_.mul_zero _ }, { have hi := h i, rw [polynomial.coeff_zero, ← f.to_map_eq_zero_iff, hb i] at hi, apply or.resolve_left (eq_zero_or_eq_zero_of_mul_eq_zero hi), intro h, apply mem_non_zero_divisors_iff_ne_zero.mp nonzero, exact f.to_map_eq_zero_iff.mp h } end /-- A field is algebraic over the ring `A` iff it is algebraic over the field of fractions of `A`. -/ lemma comap_is_algebraic_iff [algebra A L] [algebra f.codomain L] [is_scalar_tower A f.codomain L] : algebra.is_algebraic A L ↔ algebra.is_algebraic f.codomain L := begin split; intros h x; obtain ⟨p, hp, px⟩ := h x, { refine ⟨p.map f.to_map, λ h, hp (polynomial.ext (λ i, _)), _⟩, { have : f.to_map (p.coeff i) = 0 := trans (polynomial.coeff_map _ _).symm (by simp [h]), exact f.to_map_eq_zero_iff.mp this }, { rwa [is_scalar_tower.aeval_apply _ f.codomain, algebra_map_eq] at px } }, { exact ⟨f.integer_normalization p, mt f.integer_normalization_eq_zero_iff.mp hp, integer_normalization_aeval_eq_zero p px⟩ }, end section num_denom variables [unique_factorization_monoid A] (φ : fraction_map A K) lemma exists_reduced_fraction (x : φ.codomain) : ∃ (a : A) (b : non_zero_divisors A), (∀ {d}, d ∣ a → d ∣ b → is_unit d) ∧ φ.mk' a b = x := begin obtain ⟨⟨b, b_nonzero⟩, a, hab⟩ := φ.exists_integer_multiple x, obtain ⟨a', b', c', no_factor, rfl, rfl⟩ := unique_factorization_monoid.exists_reduced_factors' a b (mem_non_zero_divisors_iff_ne_zero.mp b_nonzero), obtain ⟨c'_nonzero, b'_nonzero⟩ := mul_mem_non_zero_divisors.mp b_nonzero, refine ⟨a', ⟨b', b'_nonzero⟩, @no_factor, _⟩, apply mul_left_cancel' (φ.to_map_ne_zero_of_mem_non_zero_divisors ⟨c' * b', b_nonzero⟩), simp only [subtype.coe_mk, φ.to_map.map_mul] at *, erw [←hab, mul_assoc, φ.mk'_spec' a' ⟨b', b'_nonzero⟩], end /-- `f.num x` is the numerator of `x : f.codomain` as a reduced fraction. -/ noncomputable def num (x : φ.codomain) : A := classical.some (φ.exists_reduced_fraction x) /-- `f.num x` is the denominator of `x : f.codomain` as a reduced fraction. -/ noncomputable def denom (x : φ.codomain) : non_zero_divisors A := classical.some (classical.some_spec (φ.exists_reduced_fraction x)) lemma num_denom_reduced (x : φ.codomain) : ∀ {d}, d ∣ φ.num x → d ∣ φ.denom x → is_unit d := (classical.some_spec (classical.some_spec (φ.exists_reduced_fraction x))).1 @[simp] lemma mk'_num_denom (x : φ.codomain) : φ.mk' (φ.num x) (φ.denom x) = x := (classical.some_spec (classical.some_spec (φ.exists_reduced_fraction x))).2 lemma num_mul_denom_eq_num_iff_eq {x y : φ.codomain} : x * φ.to_map (φ.denom y) = φ.to_map (φ.num y) ↔ x = y := ⟨ λ h, by simpa only [mk'_num_denom] using φ.eq_mk'_iff_mul_eq.mpr h, λ h, φ.eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom]) ⟩ lemma num_mul_denom_eq_num_iff_eq' {x y : φ.codomain} : y * φ.to_map (φ.denom x) = φ.to_map (φ.num x) ↔ x = y := ⟨ λ h, by simpa only [eq_comm, mk'_num_denom] using φ.eq_mk'_iff_mul_eq.mpr h, λ h, φ.eq_mk'_iff_mul_eq.mp (by rw [h, mk'_num_denom]) ⟩ lemma num_mul_denom_eq_num_mul_denom_iff_eq {x y : φ.codomain} : φ.num y * φ.denom x = φ.num x * φ.denom y ↔ x = y := ⟨ λ h, by simpa only [mk'_num_denom] using φ.mk'_eq_of_eq h, λ h, by rw h ⟩ lemma eq_zero_of_num_eq_zero {x : φ.codomain} (h : φ.num x = 0) : x = 0 := φ.num_mul_denom_eq_num_iff_eq'.mp (by rw [zero_mul, h, ring_hom.map_zero]) lemma is_integer_of_is_unit_denom {x : φ.codomain} (h : is_unit (φ.denom x : A)) : φ.is_integer x := begin cases h with d hd, have d_ne_zero : φ.to_map (φ.denom x) ≠ 0 := φ.to_map_ne_zero_of_mem_non_zero_divisors (φ.denom x), use ↑d⁻¹ * φ.num x, refine trans _ (φ.mk'_num_denom x), rw [φ.to_map.map_mul, φ.to_map.map_units_inv, hd], apply mul_left_cancel' d_ne_zero, rw [←mul_assoc, mul_inv_cancel d_ne_zero, one_mul, φ.mk'_spec'] end lemma is_unit_denom_of_num_eq_zero {x : φ.codomain} (h : φ.num x = 0) : is_unit (φ.denom x : A) := φ.num_denom_reduced x (h.symm ▸ dvd_zero _) (dvd_refl _) end num_denom end fraction_map section algebra section is_integral variables {R S} {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] [algebra R S] /-- Definition of the natural algebra induced by the localization of an algebra. Given an algebra `R → S`, a submonoid `R` of `M`, and a localization `Rₘ` for `M`, let `Sₘ` be the localization of `S` to the image of `M` under `algebra_map R S`. Then this is the natural algebra structure on `Rₘ → Sₘ`, such that the entire square commutes, where `localization_map.map_comp` gives the commutativity of the underlying maps -/ noncomputable def localization_algebra (M : submonoid R) (f : localization_map M Rₘ) (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) : algebra Rₘ Sₘ := (f.map (@algebra.mem_algebra_map_submonoid_of_mem R S _ _ _ _) g).to_algebra variables (f : localization_map M Rₘ) variables (g : localization_map (algebra.algebra_map_submonoid S M) Sₘ) lemma algebra_map_mk' (r : R) (m : M) : (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) (f.mk' r m) = g.mk' (algebra_map R S r) ⟨algebra_map R S m, algebra.mem_algebra_map_submonoid_of_mem m⟩ := localization_map.map_mk' f _ r m /-- Injectivity of a map descends to the map induced on localizations. -/ lemma map_injective_of_injective {R S : Type*} [comm_ring R] [comm_ring S] (ϕ : R →+* S) (hϕ : function.injective ϕ) (M : submonoid R) (f : localization_map M Rₘ) (g : localization_map (M.map ϕ : submonoid S) Sₘ) (hM : (M.map ϕ : submonoid S) ≤ non_zero_divisors S) : function.injective (f.map (M.mem_map_of_mem (ϕ : R →* S)) g) := begin rintros x y hxy, obtain ⟨a, b, rfl⟩ := localization_map.mk'_surjective f x, obtain ⟨c, d, rfl⟩ := localization_map.mk'_surjective f y, rw [localization_map.map_mk' f _ a b, localization_map.map_mk' f _ c d, localization_map.mk'_eq_iff_eq] at hxy, refine (localization_map.mk'_eq_iff_eq f).2 (congr_arg f.to_map (hϕ _)), convert g.injective hM hxy; simp, end /-- Injectivity of the underlying `algebra_map` descends to the algebra induced by localization. -/ lemma localization_algebra_injective (hRS : function.injective (algebra_map R S)) (hM : algebra.algebra_map_submonoid S M ≤ non_zero_divisors S) : function.injective (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) := map_injective_of_injective (algebra_map R S) hRS M f g hM open polynomial lemma ring_hom.is_integral_elem_localization_at_leading_coeff {R S : Type*} [comm_ring R] [comm_ring S] (f : R →+* S) (x : S) (p : polynomial R) (hf : p.eval₂ f x = 0) (M : submonoid R) (hM : p.leading_coeff ∈ M) {Rₘ Sₘ : Type*} [comm_ring Rₘ] [comm_ring Sₘ] (ϕ : localization_map M Rₘ) (ϕ' : localization_map (M.map ↑f : submonoid S) Sₘ) : (ϕ.map (M.mem_map_of_mem (f : R →* S)) ϕ').is_integral_elem (ϕ'.to_map x) := begin by_cases triv : (1 : Rₘ) = 0, { exact ⟨0, ⟨trans leading_coeff_zero triv.symm, eval₂_zero _ _⟩⟩ }, haveI : nontrivial Rₘ := nontrivial_of_ne 1 0 triv, obtain ⟨b, hb⟩ := is_unit_iff_exists_inv.mp (localization_map.map_units ϕ ⟨p.leading_coeff, hM⟩), refine ⟨(p.map ϕ.to_map) * C b, ⟨_, _⟩⟩, { refine monic_mul_C_of_leading_coeff_mul_eq_one _, rwa leading_coeff_map_of_leading_coeff_ne_zero ϕ.to_map, refine λ hfp, zero_ne_one (trans (zero_mul b).symm (hfp ▸ hb) : (0 : Rₘ) = 1) }, { refine eval₂_mul_eq_zero_of_left _ _ _ _, erw [eval₂_map, localization_map.map_comp, ← hom_eval₂ _ f ϕ'.to_map x], exact trans (congr_arg ϕ'.to_map hf) ϕ'.to_map.map_zero } end /-- Given a particular witness to an element being algebraic over an algebra `R → S`, We can localize to a submonoid containing the leading coefficient to make it integral. Explicitly, the map between the localizations will be an integral ring morphism -/ theorem is_integral_localization_at_leading_coeff {x : S} (p : polynomial R) (hp : aeval x p = 0) (hM : p.leading_coeff ∈ M) : (f.map (@algebra.mem_algebra_map_submonoid_of_mem R S _ _ _ _) g).is_integral_elem (g.to_map x) := (algebra_map R S).is_integral_elem_localization_at_leading_coeff x p hp M hM f g /-- If `R → S` is an integral extension, `M` is a submonoid of `R`, `Rₘ` is the localization of `R` at `M`, and `Sₘ` is the localization of `S` at the image of `M` under the extension map, then the induced map `Rₘ → Sₘ` is also an integral extension -/ theorem is_integral_localization (H : algebra.is_integral R S) : (f.map (@algebra.mem_algebra_map_submonoid_of_mem R S _ _ _ _) g).is_integral := begin intro x, by_cases triv : (1 : R) = 0, { have : (1 : Rₘ) = 0 := by convert congr_arg f.to_map triv; simp, exact ⟨0, ⟨trans leading_coeff_zero this.symm, eval₂_zero _ _⟩⟩ }, { haveI : nontrivial R := nontrivial_of_ne 1 0 triv, obtain ⟨⟨s, ⟨u, hu⟩⟩, hx⟩ := g.surj x, obtain ⟨v, hv⟩ := hu, obtain ⟨v', hv'⟩ := is_unit_iff_exists_inv'.1 (f.map_units ⟨v, hv.1⟩), refine @is_integral_of_is_integral_mul_unit Rₘ _ _ _ (localization_algebra M f g) x (g.to_map u) v' _ _, { replace hv' := congr_arg (@algebra_map Rₘ Sₘ _ _ (localization_algebra M f g)) hv', rw [ring_hom.map_mul, ring_hom.map_one, ← ring_hom.comp_apply _ f.to_map] at hv', erw localization_map.map_comp at hv', exact hv.2 ▸ hv' }, { obtain ⟨p, hp⟩ := H s, exact hx.symm ▸ is_integral_localization_at_leading_coeff f g p hp.2 (hp.1.symm ▸ M.one_mem) } } end lemma is_integral_localization' {R S : Type*} [comm_ring R] [comm_ring S] {f : R →+* S} (hf : f.is_integral) (M : submonoid R) : ((localization.of M).map (M.mem_map_of_mem (f : R →* S)) (localization.of (M.map ↑f))).is_integral := @is_integral_localization R _ M S _ _ _ _ _ f.to_algebra _ _ hf end is_integral namespace integral_closure variables {L : Type*} [field K] [field L] (f : fraction_map A K) open algebra /-- If the field `L` is an algebraic extension of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ def fraction_map_of_algebraic [algebra A L] (alg : is_algebraic A L) (inj : ∀ x, algebra_map A L x = 0 → x = 0) : fraction_map (integral_closure A L) L := (algebra_map (integral_closure A L) L).to_localization_map (λ ⟨⟨y, integral⟩, nonzero⟩, have y ≠ 0 := λ h, mem_non_zero_divisors_iff_ne_zero.mp nonzero (subtype.ext_iff_val.mpr h), show is_unit y, from ⟨⟨y, y⁻¹, mul_inv_cancel this, inv_mul_cancel this⟩, rfl⟩) (λ z, let ⟨x, y, hy, hxy⟩ := exists_integral_multiple (alg z) inj in ⟨⟨x, ⟨y, mem_non_zero_divisors_iff_ne_zero.mpr hy⟩⟩, hxy⟩) (λ x y, ⟨ λ (h : x.1 = y.1), ⟨1, by simpa using subtype.ext_iff_val.mpr h⟩, λ ⟨c, hc⟩, congr_arg (algebra_map _ L) (mul_right_cancel' (mem_non_zero_divisors_iff_ne_zero.mp c.2) hc) ⟩) variables {K} (L) /-- If the field `L` is a finite extension of the fraction field of the integral domain `A`, the integral closure of `A` in `L` has fraction field `L`. -/ def fraction_map_of_finite_extension [algebra A L] [algebra f.codomain L] [is_scalar_tower A f.codomain L] [finite_dimensional f.codomain L] : fraction_map (integral_closure A L) L := fraction_map_of_algebraic (f.comap_is_algebraic_iff.mpr is_algebraic_of_finite) (λ x hx, f.to_map_eq_zero_iff.mp ((algebra_map f.codomain L).map_eq_zero.mp $ (is_scalar_tower.algebra_map_apply _ _ _ _).symm.trans hx)) end integral_closure end algebra variables (A) /-- The fraction field of an integral domain as a quotient type. -/ @[reducible] def fraction_ring := localization (non_zero_divisors A) namespace fraction_ring /-- Natural hom sending `x : A`, `A` an integral domain, to the equivalence class of `(x, 1)` in the field of fractions of `A`. -/ def of : fraction_map A (localization (non_zero_divisors A)) := localization.of (non_zero_divisors A) variables {A} noncomputable instance : field (fraction_ring A) := (of A).to_field @[simp] lemma mk_eq_div {r s} : (localization.mk r s : fraction_ring A) = ((of A).to_map r / (of A).to_map s : fraction_ring A) := by erw [localization.mk_eq_mk', (of A).mk'_eq_div] /-- Given an integral domain `A` and a localization map to a field of fractions `f : A →+* K`, we get an `A`-isomorphism between the field of fractions of `A` as a quotient type and `K`. -/ noncomputable def alg_equiv_of_quotient {K : Type*} [field K] (f : fraction_map A K) : fraction_ring A ≃ₐ[A] f.codomain := localization.alg_equiv_of_quotient f instance : algebra A (fraction_ring A) := (of A).to_map.to_algebra end fraction_ring
6461e7502aa93210a653fdbfdb22b557d774cb1b
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/tests/lean/mvar3.lean
bc2914fc7a14f14c14d0b01d14ef24d4538af0f6
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
2,861
lean
import Lean.MetavarContext open Lean def mkLambdaTest (mctx : MetavarContext) (ngen : NameGenerator) (lctx : LocalContext) (xs : Array Expr) (e : Expr) : Except MetavarContext.MkBinding.Exception (MetavarContext × NameGenerator × Expr) := match MetavarContext.mkLambda xs e false true lctx { mctx := mctx, ngen := ngen } with | EStateM.Result.ok e s => Except.ok (s.mctx, s.ngen, e) | EStateM.Result.error e s => Except.error e def check (b : Bool) : IO Unit := unless b do throw $ IO.userError "error" def f := mkConst `f def g := mkConst `g def a := mkConst `a def b := mkConst `b def c := mkConst `c def b0 := mkBVar 0 def b1 := mkBVar 1 def b2 := mkBVar 2 def u := mkLevelParam `u def typeE := mkSort levelOne def natE := mkConst `Nat def boolE := mkConst `Bool def vecE := mkConst `Vec [levelZero] def α := mkFVar `α def x := mkFVar `x def y := mkFVar `y def z := mkFVar `z def w := mkFVar `w def m1 := mkMVar `m1 def m2 := mkMVar `m2 def m3 := mkMVar `m3 def bi := BinderInfo.default def arrow (d b : Expr) := mkForall `_ bi d b def lctx1 : LocalContext := {} def lctx2 := lctx1.mkLocalDecl `α `α typeE def lctx3 := lctx2.mkLocalDecl `x `x m1 def lctx4 := lctx3.mkLocalDecl `y `y (arrow natE m2) def mctx1 : MetavarContext := {} def mctx2 := mctx1.addExprMVarDecl `m1 `m1 lctx2 #[] typeE def mctx3 := mctx2.addExprMVarDecl `m2 `m2 lctx3 #[] natE def mctx4 := mctx3.addExprMVarDecl `m3 `m3 lctx3 #[] natE def mctx4' := mctx3.addExprMVarDecl `m3 `m3 lctx3 #[] natE MetavarKind.syntheticOpaque def R1 := match mkLambdaTest mctx4 {namePrefix := `n} lctx4 #[α, x, y] $ mkAppN f #[m3, x] with | Except.ok s => s | Except.error e => panic! (toString e) def e1 := R1.2.2 def mctx5 := R1.1 def sortNames (xs : List Name) : List Name := (xs.toArray.qsort Name.lt).toList def sortNamePairs {α} [Inhabited α] (xs : List (Name × α)) : List (Name × α) := (xs.toArray.qsort (fun a b => Name.lt a.1 b.1)).toList #eval toString $ sortNames $ mctx5.decls.toList.map Prod.fst #eval toString $ sortNamePairs $ mctx5.eAssignment.toList #eval e1 #eval check (!e1.hasFVar) def R2 := match mkLambdaTest mctx4' {namePrefix := `n} lctx4 #[α, x, y] $ mkAppN f #[m3, y] with | Except.ok s => s | Except.error e => panic! (toString e) def e2 := R2.2.2 def mctx6 := R2.1 #eval toString $ sortNames $ mctx6.decls.toList.map Prod.fst #eval toString $ sortNamePairs $ mctx6.eAssignment.toList -- ?n.2 was delayed assigned because ?m.3 is synthetic #eval toString $ sortNames $ mctx6.dAssignment.toList.map Prod.fst #eval e2 #print "assigning ?m1 and ?n.1" def R3 := let mctx := mctx6.assignExpr `m3 x; let mctx := mctx.assignExpr (Name.mkNum `n 1) (mkLambda `_ bi typeE natE); -- ?n.2 is instantiated because we have the delayed assignment `?n.2 α x := ?m1` (mctx.instantiateMVars e2) def e3 := R3.1 def mctx7 := R3.2 #eval e3
313e00a2525b5241b27a461d315bc8941a19ab44
02005f45e00c7ecf2c8ca5db60251bd1e9c860b5
/src/order/filter/basic.lean
338b495dc3c25a21740a4ed77a59b3261f97a369
[ "Apache-2.0" ]
permissive
anthony2698/mathlib
03cd69fe5c280b0916f6df2d07c614c8e1efe890
407615e05814e98b24b2ff322b14e8e3eb5e5d67
refs/heads/master
1,678,792,774,873
1,614,371,563,000
1,614,371,563,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
103,805
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jeremy Avigad -/ import order.zorn import order.copy import data.set.finite import tactic.monotonicity /-! # Theory of filters on sets ## Main definitions * `filter` : filters on a set; * `at_top`, `at_bot`, `cofinite`, `principal` : specific filters; * `map`, `comap`, `prod` : operations on filters; * `tendsto` : limit with respect to filters; * `eventually` : `f.eventually p` means `{x | p x} ∈ f`; * `frequently` : `f.frequently p` means `{x | ¬p x} ∉ f`; * `filter_upwards [h₁, ..., hₙ]` : takes a list of proofs `hᵢ : sᵢ ∈ f`, and replaces a goal `s ∈ f` with `∀ x, x ∈ s₁ → ... → x ∈ sₙ → x ∈ s`; * `ne_bot f` : an utility class stating that `f` is a non-trivial filter. Filters on a type `X` are sets of sets of `X` satisfying three conditions. They are mostly used to abstract two related kinds of ideas: * *limits*, including finite or infinite limits of sequences, finite or infinite limits of functions at a point or at infinity, etc... * *things happening eventually*, including things happening for large enough `n : ℕ`, or near enough a point `x`, or for close enough pairs of points, or things happening almost everywhere in the sense of measure theory. Dually, filters can also express the idea of *things happening often*: for arbitrarily large `n`, or at a point in any neighborhood of given a point etc... In this file, we define the type `filter X` of filters on `X`, and endow it with a complete lattice structure. This structure is lifted from the lattice structure on `set (set X)` using the Galois insertion which maps a filter to its elements in one direction, and an arbitrary set of sets to the smallest filter containing it in the other direction. We also prove `filter` is a monadic functor, with a push-forward operation `filter.map` and a pull-back operation `filter.comap` that form a Galois connections for the order on filters. Finally we describe a product operation `filter X → filter Y → filter (X × Y)`. The examples of filters appearing in the description of the two motivating ideas are: * `(at_top : filter ℕ)` : made of sets of `ℕ` containing `{n | n ≥ N}` for some `N` * `𝓝 x` : made of neighborhoods of `x` in a topological space (defined in topology.basic) * `𝓤 X` : made of entourages of a uniform space (those space are generalizations of metric spaces defined in topology.uniform_space.basic) * `μ.ae` : made of sets whose complement has zero measure with respect to `μ` (defined in `measure_theory.measure_space`) The general notion of limit of a map with respect to filters on the source and target types is `filter.tendsto`. It is defined in terms of the order and the push-forward operation. The predicate "happening eventually" is `filter.eventually`, and "happening often" is `filter.frequently`, whose definitions are immediate after `filter` is defined (but they come rather late in this file in order to immediately relate them to the lattice structure). For instance, anticipating on topology.basic, the statement: "if a sequence `u` converges to some `x` and `u n` belongs to a set `M` for `n` large enough then `x` is in the closure of `M`" is formalized as: `tendsto u at_top (𝓝 x) → (∀ᶠ n in at_top, u n ∈ M) → x ∈ closure M`, which is a special case of `mem_closure_of_tendsto` from topology.basic. ## Notations * `∀ᶠ x in f, p x` : `f.eventually p`; * `∃ᶠ x in f, p x` : `f.frequently p`; * `f =ᶠ[l] g` : `∀ᶠ x in l, f x = g x`; * `f ≤ᶠ[l] g` : `∀ᶠ x in l, f x ≤ g x`; * `f ×ᶠ g` : `filter.prod f g`, localized in `filter`; * `𝓟 s` : `principal s`, localized in `filter`. ## References * [N. Bourbaki, *General Topology*][bourbaki1966] Important note: Bourbaki requires that a filter on `X` cannot contain all sets of `X`, which we do *not* require. This gives `filter X` better formal properties, in particular a bottom element `⊥` for its lattice structure, at the cost of including the assumption `[ne_bot f]` in a number of lemmas and definitions. -/ open set universes u v w x y open_locale classical /-- A filter `F` on a type `α` is a collection of sets of `α` which contains the whole `α`, is upwards-closed, and is stable under intersection. We do not forbid this collection to be all sets of `α`. -/ structure filter (α : Type*) := (sets : set (set α)) (univ_sets : set.univ ∈ sets) (sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets) (inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets) /-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/ instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩ namespace filter variables {α : Type u} {f g : filter α} {s t : set α} @[simp] protected lemma mem_mk {t : set (set α)} {h₁ h₂ h₃} : s ∈ mk t h₁ h₂ h₃ ↔ s ∈ t := iff.rfl @[simp] protected lemma mem_sets : s ∈ f.sets ↔ s ∈ f := iff.rfl instance inhabited_mem : inhabited {s : set α // s ∈ f} := ⟨⟨univ, f.univ_sets⟩⟩ lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g | ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl lemma filter_eq_iff : f = g ↔ f.sets = g.sets := ⟨congr_arg _, filter_eq⟩ protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g := by simp only [filter_eq_iff, ext_iff, filter.mem_sets] @[ext] protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g := filter.ext_iff.2 @[simp] lemma univ_mem_sets : univ ∈ f := f.univ_sets lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f → x ⊆ y → y ∈ f := f.sets_of_superset lemma inter_mem_sets : ∀{s t}, s ∈ f → t ∈ f → s ∩ t ∈ f := f.inter_sets @[simp] lemma inter_mem_sets_iff {s t} : s ∩ t ∈ f ↔ s ∈ f ∧ t ∈ f := ⟨λ h, ⟨mem_sets_of_superset h (inter_subset_left s t), mem_sets_of_superset h (inter_subset_right s t)⟩, and_imp.2 inter_mem_sets⟩ lemma univ_mem_sets' (h : ∀ a, a ∈ s) : s ∈ f := mem_sets_of_superset univ_mem_sets (assume x _, h x) lemma mp_sets (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f := mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁ lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f := ⟨λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mp)), λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mpr))⟩ @[simp] lemma bInter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) : (⋂ i ∈ is, s i) ∈ f ↔ ∀ i ∈ is, s i ∈ f := finite.induction_on hf (by simp) (λ i s hi _ hs, by simp [hs]) @[simp] lemma bInter_finset_mem_sets {β : Type v} {s : β → set α} (is : finset β) : (⋂ i ∈ is, s i) ∈ f ↔ ∀ i ∈ is, s i ∈ f := bInter_mem_sets is.finite_to_set alias bInter_finset_mem_sets ← finset.Inter_mem_sets attribute [protected] finset.Inter_mem_sets @[simp] lemma sInter_mem_sets {s : set (set α)} (hfin : finite s) : ⋂₀ s ∈ f ↔ ∀ U ∈ s, U ∈ f := by rw [sInter_eq_bInter, bInter_mem_sets hfin] @[simp] lemma Inter_mem_sets {β : Type v} {s : β → set α} [fintype β] : (⋂ i, s i) ∈ f ↔ ∀ i, s i ∈ f := by simpa using bInter_mem_sets finite_univ lemma exists_sets_subset_iff : (∃t ∈ f, t ⊆ s) ↔ s ∈ f := ⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩ lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f) := assume s t hst h, mem_sets_of_superset h hst end filter namespace tactic.interactive open tactic interactive /-- `filter_upwards [h1, ⋯, hn]` replaces a goal of the form `s ∈ f` and terms `h1 : t1 ∈ f, ⋯, hn : tn ∈ f` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`. `filter_upwards [h1, ⋯, hn] e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`. -/ meta def filter_upwards (s : parse types.pexpr_list) (e' : parse $ optional types.texpr) : tactic unit := do s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e), eapplyc `filter.univ_mem_sets', `[dsimp only [set.mem_set_of_eq]], match e' with | some e := interactive.exact e | none := skip end add_tactic_doc { name := "filter_upwards", category := doc_category.tactic, decl_names := [`tactic.interactive.filter_upwards], tags := ["goal management", "lemma application"] } end tactic.interactive namespace filter variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} section principal /-- The principal filter of `s` is the collection of all supersets of `s`. -/ def principal (s : set α) : filter α := { sets := {t | s ⊆ t}, univ_sets := subset_univ s, sets_of_superset := assume x y hx hy, subset.trans hx hy, inter_sets := assume x y, subset_inter } localized "notation `𝓟` := filter.principal" in filter instance : inhabited (filter α) := ⟨𝓟 ∅⟩ @[simp] lemma mem_principal_sets {s t : set α} : s ∈ 𝓟 t ↔ t ⊆ s := iff.rfl lemma mem_principal_self (s : set α) : s ∈ 𝓟 s := subset.refl _ end principal open_locale filter section join /-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/ def join (f : filter (filter α)) : filter α := { sets := {s | {t : filter α | s ∈ t} ∈ f}, univ_sets := by simp only [mem_set_of_eq, univ_sets, ← filter.mem_sets, set_of_true], sets_of_superset := assume x y hx xy, mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy, inter_sets := assume x y hx hy, mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ } @[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} : s ∈ join f ↔ {t | s ∈ t} ∈ f := iff.rfl end join section lattice instance : partial_order (filter α) := { le := λf g, ∀ ⦃U : set α⦄, U ∈ g → U ∈ f, le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁, le_refl := assume a, subset.refl _, le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ } theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl /-- `generate_sets g s`: `s` is in the filter closure of `g`. -/ inductive generate_sets (g : set (set α)) : set α → Prop | basic {s : set α} : s ∈ g → generate_sets s | univ : generate_sets univ | superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t | inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t) /-- `generate g` is the smallest filter containing the sets `g`. -/ def generate (g : set (set α)) : filter α := { sets := generate_sets g, univ_sets := generate_sets.univ, sets_of_superset := assume x y, generate_sets.superset, inter_sets := assume s t, generate_sets.inter } lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets := iff.intro (assume h u hu, h $ generate_sets.basic $ hu) (assume h u hu, hu.rec_on h univ_mem_sets (assume x y _ hxy hx, mem_sets_of_superset hx hxy) (assume x y _ _ hx hy, inter_mem_sets hx hy)) lemma mem_generate_iff {s : set $ set α} {U : set α} : U ∈ generate s ↔ ∃ t ⊆ s, finite t ∧ ⋂₀ t ⊆ U := begin split ; intro h, { induction h with V V_in V W V_in hVW hV V W V_in W_in hV hW, { use {V}, simp [V_in] }, { use ∅, simp [subset.refl, univ] }, { rcases hV with ⟨t, hts, htfin, hinter⟩, exact ⟨t, hts, htfin, subset.trans hinter hVW⟩ }, { rcases hV with ⟨t, hts, htfin, htinter⟩, rcases hW with ⟨z, hzs, hzfin, hzinter⟩, refine ⟨t ∪ z, union_subset hts hzs, htfin.union hzfin, _⟩, rw sInter_union, exact inter_subset_inter htinter hzinter } }, { rcases h with ⟨t, ts, tfin, h⟩, apply generate_sets.superset _ h, revert ts, apply finite.induction_on tfin, { intro h, rw sInter_empty, exact generate_sets.univ }, { intros V r hV rfin hinter h, cases insert_subset.mp h with V_in r_sub, rw [insert_eq V r, sInter_union], apply generate_sets.inter _ (hinter r_sub), rw sInter_singleton, exact generate_sets.basic V_in } }, end /-- `mk_of_closure s hs` constructs a filter on `α` whose elements set is exactly `s : set (set α)`, provided one gives the assumption `hs : (generate s).sets = s`. -/ protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α := { sets := s, univ_sets := hs ▸ (univ_mem_sets : univ ∈ generate s), sets_of_superset := λ x y, hs ▸ (mem_sets_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s), inter_sets := λ x y, hs ▸ (inter_mem_sets : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) } lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} : filter.mk_of_closure s hs = generate s := filter.ext $ assume u, show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.rfl /-- Galois insertion from sets of sets into filters. -/ def gi_generate (α : Type*) : @galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets := { gc := assume s f, sets_iff_generate, le_l_u := assume f u h, generate_sets.basic h, choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _), choice_eq := assume s hs, mk_of_closure_sets } /-- The infimum of filters is the filter generated by intersections of elements of the two filters. -/ instance : has_inf (filter α) := ⟨λf g : filter α, { sets := {s | ∃ (a ∈ f) (b ∈ g), a ∩ b ⊆ s }, univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩, sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩, inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩, ⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd, calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl ... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩ @[simp] lemma mem_inf_sets {f g : filter α} {s : set α} : s ∈ f ⊓ g ↔ ∃t₁∈f, ∃t₂∈g, t₁ ∩ t₂ ⊆ s := iff.rfl lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g := ⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩ lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g := ⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩ lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g := inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht) instance : has_top (filter α) := ⟨{ sets := {s | ∀x, x ∈ s}, univ_sets := assume x, mem_univ x, sets_of_superset := assume x y hx hxy a, hxy (hx a), inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩ lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀x, x ∈ s) := iff.rfl @[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ := by rw [mem_top_sets_iff_forall, eq_univ_iff_forall] section complete_lattice /- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately, we want to have different definitional equalities for the lattice operations. So we define them upfront and change the lattice operations for the complete lattice instance. -/ private def original_complete_lattice : complete_lattice (filter α) := @order_dual.complete_lattice _ (gi_generate α).lift_complete_lattice local attribute [instance] original_complete_lattice instance : complete_lattice (filter α) := original_complete_lattice.copy /- le -/ filter.partial_order.le rfl /- top -/ (filter.has_top).1 (top_unique $ assume s hs, by simp [mem_top_sets.1 hs]) /- bot -/ _ rfl /- sup -/ _ rfl /- inf -/ (filter.has_inf).1 begin ext f g : 2, exact le_antisymm (le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right)) (assume s ⟨a, ha, b, hb, hs⟩, show s ∈ complete_lattice.inf f g, from mem_sets_of_superset (inter_mem_sets (@inf_le_left (filter α) _ _ _ _ ha) (@inf_le_right (filter α) _ _ _ _ hb)) hs) end /- Sup -/ (join ∘ 𝓟) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm) /- Inf -/ _ rfl end complete_lattice /-- A filter is `ne_bot` if it is not equal to `⊥`, or equivalently the empty set does not belong to the filter. Bourbaki include this assumption in the definition of a filter but we prefer to have a `complete_lattice` structure on filter, so we use a typeclass argument in lemmas instead. -/ class ne_bot (f : filter α) : Prop := (ne' : f ≠ ⊥) lemma ne_bot_iff {f : filter α} : ne_bot f ↔ f ≠ ⊥ := ⟨λ h, h.1, λ h, ⟨h⟩⟩ lemma ne_bot.ne {f : filter α} (hf : ne_bot f) : f ≠ ⊥ := ne_bot.ne' @[simp] lemma not_ne_bot {α : Type*} {f : filter α} : ¬ f.ne_bot ↔ f = ⊥ := not_iff_comm.1 ne_bot_iff.symm lemma ne_bot.mono {f g : filter α} (hf : ne_bot f) (hg : f ≤ g) : ne_bot g := ⟨ne_bot_of_le_ne_bot hf.1 hg⟩ lemma ne_bot_of_le {f g : filter α} [hf : ne_bot f] (hg : f ≤ g) : ne_bot g := hf.mono hg lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets := (gi_generate α).gc.u_inf lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) := (gi_generate α).gc.u_Inf lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) := (gi_generate α).gc.u_infi lemma generate_empty : filter.generate ∅ = (⊤ : filter α) := (gi_generate α).gc.l_bot lemma generate_univ : filter.generate univ = (⊥ : filter α) := mk_of_closure_sets.symm lemma generate_union {s t : set (set α)} : filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t := (gi_generate α).gc.l_sup lemma generate_Union {s : ι → set (set α)} : filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) := (gi_generate α).gc.l_supr @[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α) := trivial @[simp] lemma mem_sup_sets {f g : filter α} {s : set α} : s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g := iff.rfl lemma union_mem_sup {f g : filter α} {s t : set α} (hs : s ∈ f) (ht : t ∈ g) : s ∪ t ∈ f ⊔ g := ⟨mem_sets_of_superset hs (subset_union_left s t), mem_sets_of_superset ht (subset_union_right s t)⟩ @[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} : x ∈ Sup s ↔ (∀f∈s, x ∈ (f:filter α)) := iff.rfl @[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} : x ∈ supr f ↔ (∀i, x ∈ f i) := by simp only [← filter.mem_sets, supr_sets_eq, iff_self, mem_Inter] lemma infi_eq_generate (s : ι → filter α) : infi s = generate (⋃ i, (s i).sets) := show generate _ = generate _, from congr_arg _ supr_range lemma mem_infi_iff {ι} {s : ι → filter α} {U : set α} : (U ∈ ⨅ i, s i) ↔ ∃ I : set ι, finite I ∧ ∃ V : {i | i ∈ I} → set α, (∀ i, V i ∈ s i) ∧ (⋂ i, V i) ⊆ U := begin rw [infi_eq_generate, mem_generate_iff], split, { rintro ⟨t, tsub, tfin, tinter⟩, rcases eq_finite_Union_of_finite_subset_Union tfin tsub with ⟨I, Ifin, σ, σfin, σsub, rfl⟩, rw sInter_Union at tinter, let V := λ i, ⋂₀ σ i, have V_in : ∀ i, V i ∈ s i, { rintro ⟨i, i_in⟩, rw sInter_mem_sets (σfin _), apply σsub }, exact ⟨I, Ifin, V, V_in, tinter⟩ }, { rintro ⟨I, Ifin, V, V_in, h⟩, refine ⟨range V, _, _, h⟩, { rintro _ ⟨i, rfl⟩, rw mem_Union, use [i, V_in i] }, { haveI : fintype {i : ι | i ∈ I} := finite.fintype Ifin, exact finite_range _ } }, end @[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ 𝓟 s ↔ s ∈ f := show (∀{t}, s ⊆ t → t ∈ f) ↔ s ∈ f, from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩ lemma principal_mono {s t : set α} : 𝓟 s ≤ 𝓟 t ↔ s ⊆ t := by simp only [le_principal_iff, iff_self, mem_principal_sets] @[mono] lemma monotone_principal : monotone (𝓟 : set α → filter α) := λ _ _, principal_mono.2 @[simp] lemma principal_eq_iff_eq {s t : set α} : 𝓟 s = 𝓟 t ↔ s = t := by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl @[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (𝓟 s) = Sup s := rfl @[simp] lemma principal_univ : 𝓟 (univ : set α) = ⊤ := top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true] @[simp] lemma principal_empty : 𝓟 (∅ : set α) = ⊥ := bot_unique $ assume s _, empty_subset _ /-! ### Lattice equations -/ lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ := ⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s), assume : f = ⊥, this.symm ▸ mem_bot_sets⟩ lemma nonempty_of_mem_sets {f : filter α} [hf : ne_bot f] {s : set α} (hs : s ∈ f) : s.nonempty := s.eq_empty_or_nonempty.elim (λ h, absurd hs (h.symm ▸ mt empty_in_sets_eq_bot.mp hf.1)) id lemma ne_bot.nonempty_of_mem {f : filter α} (hf : ne_bot f) {s : set α} (hs : s ∈ f) : s.nonempty := @nonempty_of_mem_sets α f hf s hs @[simp] lemma empty_nmem_sets (f : filter α) [ne_bot f] : ¬(∅ ∈ f) := λ h, (nonempty_of_mem_sets h).ne_empty rfl lemma nonempty_of_ne_bot (f : filter α) [ne_bot f] : nonempty α := nonempty_of_exists $ nonempty_of_mem_sets (univ_mem_sets : univ ∈ f) lemma compl_not_mem_sets {f : filter α} {s : set α} [ne_bot f] (h : s ∈ f) : sᶜ ∉ f := λ hsc, (nonempty_of_mem_sets (inter_mem_sets h hsc)).ne_empty $ inter_compl_self s lemma filter_eq_bot_of_not_nonempty (f : filter α) (ne : ¬ nonempty α) : f = ⊥ := empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩) lemma forall_sets_nonempty_iff_ne_bot {f : filter α} : (∀ (s : set α), s ∈ f → s.nonempty) ↔ ne_bot f := ⟨λ h, ⟨λ hf, empty_not_nonempty (h ∅ $ hf.symm ▸ mem_bot_sets)⟩, @nonempty_of_mem_sets _ _⟩ lemma nontrivial_iff_nonempty : nontrivial (filter α) ↔ nonempty α := ⟨λ ⟨⟨f, g, hfg⟩⟩, by_contra $ λ h, hfg $ (filter_eq_bot_of_not_nonempty f h).trans (filter_eq_bot_of_not_nonempty g h).symm, λ ⟨x⟩, ⟨⟨⊤, ⊥, ne_bot.ne $ forall_sets_nonempty_iff_ne_bot.1 $ λ s hs, by rwa [mem_top_sets.1 hs, ← nonempty_iff_univ_nonempty]⟩⟩⟩ lemma mem_sets_of_eq_bot {f : filter α} {s : set α} (h : f ⊓ 𝓟 sᶜ = ⊥) : s ∈ f := have ∅ ∈ f ⊓ 𝓟 sᶜ, from h.symm ▸ mem_bot_sets, let ⟨s₁, hs₁, s₂, (hs₂ : sᶜ ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩ lemma eq_Inf_of_mem_sets_iff_exists_mem {S : set (filter α)} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = Inf S := le_antisymm (le_Inf $ λ f hf s hs, h.2 ⟨f, hf, hs⟩) (λ s hs, let ⟨f, hf, hs⟩ := h.1 hs in (Inf_le hf : Inf S ≤ f) hs) lemma eq_infi_of_mem_sets_iff_exists_mem {f : ι → filter α} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i, s ∈ f i) : l = infi f := eq_Inf_of_mem_sets_iff_exists_mem $ λ s, h.trans exists_range_iff.symm lemma eq_binfi_of_mem_sets_iff_exists_mem {f : ι → filter α} {p : ι → Prop} {l : filter α} (h : ∀ {s}, s ∈ l ↔ ∃ i (_ : p i), s ∈ f i) : l = ⨅ i (_ : p i), f i := begin rw [infi_subtype'], apply eq_infi_of_mem_sets_iff_exists_mem, intro s, exact h.trans ⟨λ ⟨i, pi, si⟩, ⟨⟨i, pi⟩, si⟩, λ ⟨⟨i, pi⟩, si⟩, ⟨i, pi, si⟩⟩ end lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) [ne : nonempty ι] : (infi f).sets = (⋃ i, (f i).sets) := let ⟨i⟩ := ne, u := { filter . sets := (⋃ i, (f i).sets), univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩, sets_of_superset := by simp only [mem_Union, exists_imp_distrib]; intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩, inter_sets := begin simp only [mem_Union, exists_imp_distrib], assume x y a hx b hy, rcases h a b with ⟨c, ha, hb⟩, exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩ end } in have u = infi f, from eq_infi_of_mem_sets_iff_exists_mem (λ s, by simp only [filter.mem_mk, mem_Union, filter.mem_sets]), congr_arg filter.sets this.symm lemma mem_infi {f : ι → filter α} (h : directed (≥) f) [nonempty ι] (s) : s ∈ infi f ↔ ∃ i, s ∈ f i := by simp only [← filter.mem_sets, infi_sets_eq h, mem_Union] lemma mem_binfi {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) {t : set α} : t ∈ (⨅ i∈s, f i) ↔ ∃ i ∈ s, t ∈ f i := by haveI : nonempty {x // x ∈ s} := ne.to_subtype; erw [infi_subtype', mem_infi h.directed_coe, subtype.exists]; refl lemma binfi_sets_eq {f : β → filter α} {s : set β} (h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) : (⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) := ext $ λ t, by simp [mem_binfi h ne] lemma infi_sets_eq_finite {ι : Type*} (f : ι → filter α) : (⨅i, f i).sets = (⋃t:finset ι, (⨅i∈t, f i).sets) := begin rw [infi_eq_infi_finset, infi_sets_eq], exact (directed_of_sup $ λs₁ s₂ hs, infi_le_infi $ λi, infi_le_infi_const $ λh, hs h), end lemma infi_sets_eq_finite' (f : ι → filter α) : (⨅i, f i).sets = (⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets) := by rw [← infi_sets_eq_finite, ← equiv.plift.surjective.infi_comp]; refl lemma mem_infi_finite {ι : Type*} {f : ι → filter α} (s) : s ∈ infi f ↔ ∃ t:finset ι, s ∈ ⨅i∈t, f i := (set.ext_iff.1 (infi_sets_eq_finite f) s).trans mem_Union lemma mem_infi_finite' {f : ι → filter α} (s) : s ∈ infi f ↔ ∃ t:finset (plift ι), s ∈ ⨅i∈t, f (plift.down i) := (set.ext_iff.1 (infi_sets_eq_finite' f) s).trans mem_Union @[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) := filter.ext $ λ x, by simp only [mem_sup_sets, mem_join_sets] @[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} : (⨆x, join (f x)) = join (⨆x, f x) := filter.ext $ assume x, by simp only [mem_supr_sets, mem_join_sets] instance : bounded_distrib_lattice (filter α) := { le_sup_inf := begin assume x y z s, simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp], intros hs t₁ ht₁ t₂ ht₂ hts, exact ⟨s ∪ t₁, x.sets_of_superset hs $ subset_union_left _ _, y.sets_of_superset ht₁ $ subset_union_right _ _, s ∪ t₂, x.sets_of_superset hs $ subset_union_left _ _, z.sets_of_superset ht₂ $ subset_union_right _ _, subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩ end, ..filter.complete_lattice } /- the complementary version with ⨆i, f ⊓ g i does not hold! -/ lemma infi_sup_left {f : filter α} {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g := begin refine le_antisymm _ (le_infi $ assume i, sup_le_sup_left (infi_le _ _) _), rintros t ⟨h₁, h₂⟩, rw [infi_sets_eq_finite'] at h₂, simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂, rcases h₂ with ⟨s, hs⟩, suffices : (⨅i, f ⊔ g i) ≤ f ⊔ s.inf (λi, g i.down), { exact this ⟨h₁, hs⟩ }, refine finset.induction_on s _ _, { exact le_sup_right_of_le le_top }, { rintros ⟨i⟩ s his ih, rw [finset.inf_insert, sup_inf_left], exact le_inf (infi_le _ _) ih } end lemma infi_sup_right {f : filter α} {g : ι → filter α} : (⨅ x, g x ⊔ f) = infi g ⊔ f := by simp [sup_comm, ← infi_sup_left] lemma binfi_sup_right (p : ι → Prop) (f : ι → filter α) (g : filter α) : (⨅ i (h : p i), (f i ⊔ g)) = (⨅ i (h : p i), f i) ⊔ g := by rw [infi_subtype', infi_sup_right, infi_subtype'] lemma binfi_sup_left (p : ι → Prop) (f : ι → filter α) (g : filter α) : (⨅ i (h : p i), (g ⊔ f i)) = g ⊔ (⨅ i (h : p i), f i) := by rw [infi_subtype', infi_sup_left, infi_subtype'] lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} : ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⋂a∈s, p a) ⊆ t) := show ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⨅a∈s, p a) ≤ t), begin simp only [(finset.inf_eq_infi _ _).symm], refine finset.induction_on s _ _, { simp only [finset.not_mem_empty, false_implies_iff, finset.inf_empty, top_le_iff, imp_true_iff, mem_top_sets, true_and, exists_const], intros; refl }, { intros a s has ih t, simp only [ih, finset.forall_mem_insert, finset.inf_insert, mem_inf_sets, exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt}, split, { intros t₁ ht₁ t₂ p hp ht₂ ht, existsi function.update p a t₁, have : ∀a'∈s, function.update p a t₁ a' = p a', from assume a' ha', have a' ≠ a, from assume h, has $ h ▸ ha', function.update_noteq this _ _, have eq : s.inf (λj, function.update p a t₁ j) = s.inf (λj, p j) := finset.inf_congr rfl this, simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt}, exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht }, assume p hpa hp ht, exact ⟨p a, hpa, (s.inf p), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ } end /-- If `f : ι → filter α` is directed, `ι` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`. See also `infi_ne_bot_of_directed` for a version assuming `nonempty α` instead of `nonempty ι`. -/ lemma infi_ne_bot_of_directed' {f : ι → filter α} [nonempty ι] (hd : directed (≥) f) (hb : ∀i, ne_bot (f i)) : ne_bot (infi f) := ⟨begin intro h, have he: ∅ ∈ (infi f), from h.symm ▸ (mem_bot_sets : ∅ ∈ (⊥ : filter α)), obtain ⟨i, hi⟩ : ∃i, ∅ ∈ f i, from (mem_infi hd ∅).1 he, exact (hb i).ne (empty_in_sets_eq_bot.1 hi) end⟩ /-- If `f : ι → filter α` is directed, `α` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`. See also `infi_ne_bot_of_directed'` for a version assuming `nonempty ι` instead of `nonempty α`. -/ lemma infi_ne_bot_of_directed {f : ι → filter α} [hn : nonempty α] (hd : directed (≥) f) (hb : ∀i, ne_bot (f i)) : ne_bot (infi f) := if hι : nonempty ι then @infi_ne_bot_of_directed' _ _ _ hι hd hb else ⟨λ h : infi f = ⊥, have univ ⊆ (∅ : set α), begin rw [←principal_mono, principal_univ, principal_empty, ←h], exact (le_infi $ assume i, false.elim $ hι ⟨i⟩) end, let ⟨x⟩ := hn in this (mem_univ x)⟩ lemma infi_ne_bot_iff_of_directed' {f : ι → filter α} [nonempty ι] (hd : directed (≥) f) : ne_bot (infi f) ↔ ∀i, ne_bot (f i) := ⟨assume H i, H.mono (infi_le _ i), infi_ne_bot_of_directed' hd⟩ lemma infi_ne_bot_iff_of_directed {f : ι → filter α} [nonempty α] (hd : directed (≥) f) : ne_bot (infi f) ↔ (∀i, ne_bot (f i)) := ⟨assume H i, H.mono (infi_le _ i), infi_ne_bot_of_directed hd⟩ lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ f i → s ∈ ⨅i, f i := show (⨅i, f i) ≤ f i, from infi_le _ _ @[elab_as_eliminator] lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop} (uni : p univ) (ins : ∀{i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂)) (upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s := begin rw [mem_infi_finite'] at hs, simp only [← finset.inf_eq_infi] at hs, rcases hs with ⟨is, his⟩, revert s, refine finset.induction_on is _ _, { assume s hs, rwa [mem_top_sets.1 hs] }, { rintros ⟨i⟩ js his ih s hs, rw [finset.inf_insert, mem_inf_sets] at hs, rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩, exact upw hs (ins hs₁ (ih hs₂)) } end /- principal equations -/ @[simp] lemma inf_principal {s t : set α} : 𝓟 s ⊓ 𝓟 t = 𝓟 (s ∩ t) := le_antisymm (by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩) (by simp [le_inf_iff, inter_subset_left, inter_subset_right]) @[simp] lemma sup_principal {s t : set α} : 𝓟 s ⊔ 𝓟 t = 𝓟 (s ∪ t) := filter.ext $ λ u, by simp only [union_subset_iff, mem_sup_sets, mem_principal_sets] @[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, 𝓟 (s x)) = 𝓟 (⋃i, s i) := filter.ext $ assume x, by simp only [mem_supr_sets, mem_principal_sets, Union_subset_iff] @[simp] lemma principal_eq_bot_iff {s : set α} : 𝓟 s = ⊥ ↔ s = ∅ := empty_in_sets_eq_bot.symm.trans $ mem_principal_sets.trans subset_empty_iff @[simp] lemma principal_ne_bot_iff {s : set α} : ne_bot (𝓟 s) ↔ s.nonempty := ne_bot_iff.trans $ (not_congr principal_eq_bot_iff).trans ne_empty_iff_nonempty lemma is_compl_principal (s : set α) : is_compl (𝓟 s) (𝓟 sᶜ) := ⟨by simp only [inf_principal, inter_compl_self, principal_empty, le_refl], by simp only [sup_principal, union_compl_self, principal_univ, le_refl]⟩ lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : sᶜ ∈ f) : f ⊓ 𝓟 s = ⊥ := empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩ theorem mem_inf_principal {f : filter α} {s t : set α} : s ∈ f ⊓ 𝓟 t ↔ {x | x ∈ t → x ∈ s} ∈ f := begin simp only [← le_principal_iff, (is_compl_principal s).le_left_iff, disjoint, inf_assoc, inf_principal, imp_iff_not_or], rw [← disjoint, ← (is_compl_principal (t ∩ sᶜ)).le_right_iff, compl_inter, compl_compl], refl end lemma diff_mem_inf_principal_compl {f : filter α} {s : set α} (hs : s ∈ f) (t : set α) : s \ t ∈ f ⊓ 𝓟 tᶜ := begin rw mem_inf_principal, filter_upwards [hs], intros a has hat, exact ⟨has, hat⟩ end lemma principal_le_iff {s : set α} {f : filter α} : 𝓟 s ≤ f ↔ ∀ V ∈ f, s ⊆ V := begin change (∀ V, V ∈ f → V ∈ _) ↔ _, simp_rw mem_principal_sets, end @[simp] lemma infi_principal_finset {ι : Type w} (s : finset ι) (f : ι → set α) : (⨅i∈s, 𝓟 (f i)) = 𝓟 (⋂i∈s, f i) := begin ext t, simp [mem_infi_sets_finset], split, { rintros ⟨p, hp, ht⟩, calc (⋂ (i : ι) (H : i ∈ s), f i) ≤ (⋂ (i : ι) (H : i ∈ s), p i) : infi_le_infi (λi, infi_le_infi (λhi, mem_principal_sets.1 (hp i hi))) ... ≤ t : ht }, { assume h, exact ⟨f, λi hi, subset.refl _, h⟩ } end @[simp] lemma infi_principal_fintype {ι : Type w} [fintype ι] (f : ι → set α) : (⨅i, 𝓟 (f i)) = 𝓟 (⋂i, f i) := by simpa using infi_principal_finset finset.univ f end lattice @[mono] lemma join_mono {f₁ f₂ : filter (filter α)} (h : f₁ ≤ f₂) : join f₁ ≤ join f₂ := λ s hs, h hs /-! ### Eventually -/ /-- `f.eventually p` or `∀ᶠ x in f, p x` mean that `{x | p x} ∈ f`. E.g., `∀ᶠ x in at_top, p x` means that `p` holds true for sufficiently large `x`. -/ protected def eventually (p : α → Prop) (f : filter α) : Prop := {x | p x} ∈ f notation `∀ᶠ` binders ` in ` f `, ` r:(scoped p, filter.eventually p f) := r lemma eventually_iff {f : filter α} {P : α → Prop} : (∀ᶠ x in f, P x) ↔ {x | P x} ∈ f := iff.rfl protected lemma ext' {f₁ f₂ : filter α} (h : ∀ p : α → Prop, (∀ᶠ x in f₁, p x) ↔ (∀ᶠ x in f₂, p x)) : f₁ = f₂ := filter.ext h lemma eventually.filter_mono {f₁ f₂ : filter α} (h : f₁ ≤ f₂) {p : α → Prop} (hp : ∀ᶠ x in f₂, p x) : ∀ᶠ x in f₁, p x := h hp lemma eventually_of_mem {f : filter α} {P : α → Prop} {U : set α} (hU : U ∈ f) (h : ∀ x ∈ U, P x) : ∀ᶠ x in f, P x := mem_sets_of_superset hU h protected lemma eventually.and {p q : α → Prop} {f : filter α} : f.eventually p → f.eventually q → ∀ᶠ x in f, p x ∧ q x := inter_mem_sets @[simp] lemma eventually_true (f : filter α) : ∀ᶠ x in f, true := univ_mem_sets lemma eventually_of_forall {p : α → Prop} {f : filter α} (hp : ∀ x, p x) : ∀ᶠ x in f, p x := univ_mem_sets' hp @[simp] lemma eventually_false_iff_eq_bot {f : filter α} : (∀ᶠ x in f, false) ↔ f = ⊥ := empty_in_sets_eq_bot @[simp] lemma eventually_const {f : filter α} [t : ne_bot f] {p : Prop} : (∀ᶠ x in f, p) ↔ p := classical.by_cases (λ h : p, by simp [h]) (λ h, by simpa [h] using t.ne) lemma eventually_iff_exists_mem {p : α → Prop} {f : filter α} : (∀ᶠ x in f, p x) ↔ ∃ v ∈ f, ∀ y ∈ v, p y := exists_sets_subset_iff.symm lemma eventually.exists_mem {p : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) : ∃ v ∈ f, ∀ y ∈ v, p y := eventually_iff_exists_mem.1 hp lemma eventually.mp {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ᶠ x in f, p x → q x) : ∀ᶠ x in f, q x := mp_sets hp hq lemma eventually.mono {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hq : ∀ x, p x → q x) : ∀ᶠ x in f, q x := hp.mp (eventually_of_forall hq) @[simp] lemma eventually_and {p q : α → Prop} {f : filter α} : (∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in f, q x) := inter_mem_sets_iff lemma eventually.congr {f : filter α} {p q : α → Prop} (h' : ∀ᶠ x in f, p x) (h : ∀ᶠ x in f, p x ↔ q x) : ∀ᶠ x in f, q x := h'.mp (h.mono $ λ x hx, hx.mp) lemma eventually_congr {f : filter α} {p q : α → Prop} (h : ∀ᶠ x in f, p x ↔ q x) : (∀ᶠ x in f, p x) ↔ (∀ᶠ x in f, q x) := ⟨λ hp, hp.congr h, λ hq, hq.congr $ by simpa only [iff.comm] using h⟩ @[simp] lemma eventually_all {ι} [fintype ι] {l} {p : ι → α → Prop} : (∀ᶠ x in l, ∀ i, p i x) ↔ ∀ i, ∀ᶠ x in l, p i x := by simpa only [filter.eventually, set_of_forall] using Inter_mem_sets @[simp] lemma eventually_all_finite {ι} {I : set ι} (hI : I.finite) {l} {p : ι → α → Prop} : (∀ᶠ x in l, ∀ i ∈ I, p i x) ↔ (∀ i ∈ I, ∀ᶠ x in l, p i x) := by simpa only [filter.eventually, set_of_forall] using bInter_mem_sets hI alias eventually_all_finite ← set.finite.eventually_all attribute [protected] set.finite.eventually_all @[simp] lemma eventually_all_finset {ι} (I : finset ι) {l} {p : ι → α → Prop} : (∀ᶠ x in l, ∀ i ∈ I, p i x) ↔ ∀ i ∈ I, ∀ᶠ x in l, p i x := I.finite_to_set.eventually_all alias eventually_all_finset ← finset.eventually_all attribute [protected] finset.eventually_all @[simp] lemma eventually_or_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p ∨ q x) ↔ (p ∨ ∀ᶠ x in f, q x) := classical.by_cases (λ h : p, by simp [h]) (λ h, by simp [h]) @[simp] lemma eventually_or_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x ∨ q) ↔ ((∀ᶠ x in f, p x) ∨ q) := by simp only [or_comm _ q, eventually_or_distrib_left] @[simp] lemma eventually_imp_distrib_left {f : filter α} {p : Prop} {q : α → Prop} : (∀ᶠ x in f, p → q x) ↔ (p → ∀ᶠ x in f, q x) := by simp only [imp_iff_not_or, eventually_or_distrib_left] @[simp] lemma eventually_bot {p : α → Prop} : ∀ᶠ x in ⊥, p x := ⟨⟩ @[simp] lemma eventually_top {p : α → Prop} : (∀ᶠ x in ⊤, p x) ↔ (∀ x, p x) := iff.rfl @[simp] lemma eventually_sup {p : α → Prop} {f g : filter α} : (∀ᶠ x in f ⊔ g, p x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in g, p x) := iff.rfl @[simp] lemma eventually_Sup {p : α → Prop} {fs : set (filter α)} : (∀ᶠ x in Sup fs, p x) ↔ (∀ f ∈ fs, ∀ᶠ x in f, p x) := iff.rfl @[simp] lemma eventually_supr {p : α → Prop} {fs : β → filter α} : (∀ᶠ x in (⨆ b, fs b), p x) ↔ (∀ b, ∀ᶠ x in fs b, p x) := mem_supr_sets @[simp] lemma eventually_principal {a : set α} {p : α → Prop} : (∀ᶠ x in 𝓟 a, p x) ↔ (∀ x ∈ a, p x) := iff.rfl theorem eventually_inf_principal {f : filter α} {p : α → Prop} {s : set α} : (∀ᶠ x in f ⊓ 𝓟 s, p x) ↔ ∀ᶠ x in f, x ∈ s → p x := mem_inf_principal /-! ### Frequently -/ /-- `f.frequently p` or `∃ᶠ x in f, p x` mean that `{x | ¬p x} ∉ f`. E.g., `∃ᶠ x in at_top, p x` means that there exist arbitrarily large `x` for which `p` holds true. -/ protected def frequently (p : α → Prop) (f : filter α) : Prop := ¬∀ᶠ x in f, ¬p x notation `∃ᶠ` binders ` in ` f `, ` r:(scoped p, filter.frequently p f) := r lemma eventually.frequently {f : filter α} [ne_bot f] {p : α → Prop} (h : ∀ᶠ x in f, p x) : ∃ᶠ x in f, p x := compl_not_mem_sets h lemma frequently_of_forall {f : filter α} [ne_bot f] {p : α → Prop} (h : ∀ x, p x) : ∃ᶠ x in f, p x := eventually.frequently (eventually_of_forall h) lemma frequently.mp {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ᶠ x in f, p x → q x) : ∃ᶠ x in f, q x := mt (λ hq, hq.mp $ hpq.mono $ λ x, mt) h lemma frequently.mono {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x) (hpq : ∀ x, p x → q x) : ∃ᶠ x in f, q x := h.mp (eventually_of_forall hpq) lemma frequently.and_eventually {p q : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) (hq : ∀ᶠ x in f, q x) : ∃ᶠ x in f, p x ∧ q x := begin refine mt (λ h, hq.mp $ h.mono _) hp, assume x hpq hq hp, exact hpq ⟨hp, hq⟩ end lemma frequently.exists {p : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) : ∃ x, p x := begin by_contradiction H, replace H : ∀ᶠ x in f, ¬ p x, from eventually_of_forall (not_exists.1 H), exact hp H end lemma eventually.exists {p : α → Prop} {f : filter α} [ne_bot f] (hp : ∀ᶠ x in f, p x) : ∃ x, p x := hp.frequently.exists lemma frequently_iff_forall_eventually_exists_and {p : α → Prop} {f : filter α} : (∃ᶠ x in f, p x) ↔ ∀ {q : α → Prop}, (∀ᶠ x in f, q x) → ∃ x, p x ∧ q x := ⟨assume hp q hq, (hp.and_eventually hq).exists, assume H hp, by simpa only [and_not_self, exists_false] using H hp⟩ lemma frequently_iff {f : filter α} {P : α → Prop} : (∃ᶠ x in f, P x) ↔ ∀ {U}, U ∈ f → ∃ x ∈ U, P x := begin rw frequently_iff_forall_eventually_exists_and, split ; intro h, { intros U U_in, simpa [exists_prop, and_comm] using h U_in }, { intros H H', simpa [and_comm] using h H' }, end @[simp] lemma not_eventually {p : α → Prop} {f : filter α} : (¬ ∀ᶠ x in f, p x) ↔ (∃ᶠ x in f, ¬ p x) := by simp [filter.frequently] @[simp] lemma not_frequently {p : α → Prop} {f : filter α} : (¬ ∃ᶠ x in f, p x) ↔ (∀ᶠ x in f, ¬ p x) := by simp only [filter.frequently, not_not] @[simp] lemma frequently_true_iff_ne_bot (f : filter α) : (∃ᶠ x in f, true) ↔ ne_bot f := by simp [filter.frequently, -not_eventually, eventually_false_iff_eq_bot, ne_bot_iff] @[simp] lemma frequently_false (f : filter α) : ¬ ∃ᶠ x in f, false := by simp @[simp] lemma frequently_const {f : filter α} [ne_bot f] {p : Prop} : (∃ᶠ x in f, p) ↔ p := classical.by_cases (λ h : p, by simpa [h]) (λ h, by simp [h]) @[simp] lemma frequently_or_distrib {f : filter α} {p q : α → Prop} : (∃ᶠ x in f, p x ∨ q x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in f, q x) := by simp only [filter.frequently, ← not_and_distrib, not_or_distrib, eventually_and] lemma frequently_or_distrib_left {f : filter α} [ne_bot f] {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p ∨ q x) ↔ (p ∨ ∃ᶠ x in f, q x) := by simp lemma frequently_or_distrib_right {f : filter α} [ne_bot f] {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x ∨ q) ↔ (∃ᶠ x in f, p x) ∨ q := by simp @[simp] lemma frequently_imp_distrib {f : filter α} {p q : α → Prop} : (∃ᶠ x in f, p x → q x) ↔ ((∀ᶠ x in f, p x) → ∃ᶠ x in f, q x) := by simp [imp_iff_not_or, not_eventually, frequently_or_distrib] lemma frequently_imp_distrib_left {f : filter α} [ne_bot f] {p : Prop} {q : α → Prop} : (∃ᶠ x in f, p → q x) ↔ (p → ∃ᶠ x in f, q x) := by simp lemma frequently_imp_distrib_right {f : filter α} [ne_bot f] {p : α → Prop} {q : Prop} : (∃ᶠ x in f, p x → q) ↔ ((∀ᶠ x in f, p x) → q) := by simp @[simp] lemma eventually_imp_distrib_right {f : filter α} {p : α → Prop} {q : Prop} : (∀ᶠ x in f, p x → q) ↔ ((∃ᶠ x in f, p x) → q) := by simp only [imp_iff_not_or, eventually_or_distrib_right, not_frequently] @[simp] lemma frequently_bot {p : α → Prop} : ¬ ∃ᶠ x in ⊥, p x := by simp @[simp] lemma frequently_top {p : α → Prop} : (∃ᶠ x in ⊤, p x) ↔ (∃ x, p x) := by simp [filter.frequently] @[simp] lemma frequently_principal {a : set α} {p : α → Prop} : (∃ᶠ x in 𝓟 a, p x) ↔ (∃ x ∈ a, p x) := by simp [filter.frequently, not_forall] lemma frequently_sup {p : α → Prop} {f g : filter α} : (∃ᶠ x in f ⊔ g, p x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in g, p x) := by simp only [filter.frequently, eventually_sup, not_and_distrib] @[simp] lemma frequently_Sup {p : α → Prop} {fs : set (filter α)} : (∃ᶠ x in Sup fs, p x) ↔ (∃ f ∈ fs, ∃ᶠ x in f, p x) := by simp [filter.frequently, -not_eventually, not_forall] @[simp] lemma frequently_supr {p : α → Prop} {fs : β → filter α} : (∃ᶠ x in (⨆ b, fs b), p x) ↔ (∃ b, ∃ᶠ x in fs b, p x) := by simp [filter.frequently, -not_eventually, not_forall] /-! ### Relation “eventually equal” -/ /-- Two functions `f` and `g` are *eventually equal* along a filter `l` if the set of `x` such that `f x = g x` belongs to `l`. -/ def eventually_eq (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x = g x notation f ` =ᶠ[`:50 l:50 `] `:0 g:50 := eventually_eq l f g lemma eventually_eq.eventually {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) : ∀ᶠ x in l, f x = g x := h lemma eventually_eq.rw {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) (p : α → β → Prop) (hf : ∀ᶠ x in l, p x (f x)) : ∀ᶠ x in l, p x (g x) := hf.congr $ h.mono $ λ x hx, hx ▸ iff.rfl lemma eventually_eq_set {s t : set α} {l : filter α} : s =ᶠ[l] t ↔ ∀ᶠ x in l, x ∈ s ↔ x ∈ t := eventually_congr $ eventually_of_forall $ λ x, ⟨eq.to_iff, iff.to_eq⟩ alias eventually_eq_set ↔ filter.eventually_eq.mem_iff filter.eventually.set_eq lemma eventually_eq.exists_mem {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) : ∃ s ∈ l, eq_on f g s := h.exists_mem lemma eventually_eq_of_mem {l : filter α} {f g : α → β} {s : set α} (hs : s ∈ l) (h : eq_on f g s) : f =ᶠ[l] g := eventually_of_mem hs h lemma eventually_eq_iff_exists_mem {l : filter α} {f g : α → β} : (f =ᶠ[l] g) ↔ ∃ s ∈ l, eq_on f g s := eventually_iff_exists_mem lemma eventually_eq.filter_mono {l l' : filter α} {f g : α → β} (h₁ : f =ᶠ[l] g) (h₂ : l' ≤ l) : f =ᶠ[l'] g := h₂ h₁ @[refl] lemma eventually_eq.refl (l : filter α) (f : α → β) : f =ᶠ[l] f := eventually_of_forall $ λ x, rfl lemma eventually_eq.rfl {l : filter α} {f : α → β} : f =ᶠ[l] f := eventually_eq.refl l f @[symm] lemma eventually_eq.symm {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) : g =ᶠ[l] f := H.mono $ λ _, eq.symm @[trans] lemma eventually_eq.trans {f g h : α → β} {l : filter α} (H₁ : f =ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f =ᶠ[l] h := H₂.rw (λ x y, f x = y) H₁ lemma eventually_eq.prod_mk {l} {f f' : α → β} (hf : f =ᶠ[l] f') {g g' : α → γ} (hg : g =ᶠ[l] g') : (λ x, (f x, g x)) =ᶠ[l] (λ x, (f' x, g' x)) := hf.mp $ hg.mono $ by { intros, simp only * } lemma eventually_eq.fun_comp {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) (h : β → γ) : (h ∘ f) =ᶠ[l] (h ∘ g) := H.mono $ λ x hx, congr_arg h hx lemma eventually_eq.comp₂ {δ} {f f' : α → β} {g g' : α → γ} {l} (Hf : f =ᶠ[l] f') (h : β → γ → δ) (Hg : g =ᶠ[l] g') : (λ x, h (f x) (g x)) =ᶠ[l] (λ x, h (f' x) (g' x)) := (Hf.prod_mk Hg).fun_comp (function.uncurry h) @[to_additive] lemma eventually_eq.mul [has_mul β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x * f' x) =ᶠ[l] (λ x, g x * g' x)) := h.comp₂ (*) h' @[to_additive] lemma eventually_eq.inv [has_inv β] {f g : α → β} {l : filter α} (h : f =ᶠ[l] g) : ((λ x, (f x)⁻¹) =ᶠ[l] (λ x, (g x)⁻¹)) := h.fun_comp has_inv.inv lemma eventually_eq.div [group_with_zero β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x / f' x) =ᶠ[l] (λ x, g x / g' x)) := by simpa only [div_eq_mul_inv] using h.mul h'.inv lemma eventually_eq.div' [group β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x / f' x) =ᶠ[l] (λ x, g x / g' x)) := by simpa only [div_eq_mul_inv] using h.mul h'.inv lemma eventually_eq.sub [add_group β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g) (h' : f' =ᶠ[l] g') : ((λ x, f x - f' x) =ᶠ[l] (λ x, g x - g' x)) := by simpa only [sub_eq_add_neg] using h.add h'.neg lemma eventually_eq.inter {s t s' t' : set α} {l : filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s ∩ s' : set α) =ᶠ[l] (t ∩ t' : set α) := h.comp₂ (∧) h' lemma eventually_eq.union {s t s' t' : set α} {l : filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s ∪ s' : set α) =ᶠ[l] (t ∪ t' : set α) := h.comp₂ (∨) h' lemma eventually_eq.compl {s t : set α} {l : filter α} (h : s =ᶠ[l] t) : (sᶜ : set α) =ᶠ[l] (tᶜ : set α) := h.fun_comp not lemma eventually_eq.diff {s t s' t' : set α} {l : filter α} (h : s =ᶠ[l] t) (h' : s' =ᶠ[l] t') : (s \ s' : set α) =ᶠ[l] (t \ t' : set α) := h.inter h'.compl lemma eventually_eq_empty {s : set α} {l : filter α} : s =ᶠ[l] (∅ : set α) ↔ ∀ᶠ x in l, x ∉ s := eventually_eq_set.trans $ by simp @[simp] lemma eventually_eq_principal {s : set α} {f g : α → β} : f =ᶠ[𝓟 s] g ↔ eq_on f g s := iff.rfl lemma eventually_eq_inf_principal_iff {F : filter α} {s : set α} {f g : α → β} : (f =ᶠ[F ⊓ 𝓟 s] g) ↔ ∀ᶠ x in F, x ∈ s → f x = g x := eventually_inf_principal lemma eventually_eq.sub_eq [add_group β] {f g : α → β} {l : filter α} (h : f =ᶠ[l] g) : f - g =ᶠ[l] 0 := by simpa using (eventually_eq.sub (eventually_eq.refl l f) h).symm lemma eventually_eq_iff_sub [add_group β] {f g : α → β} {l : filter α} : f =ᶠ[l] g ↔ f - g =ᶠ[l] 0 := ⟨λ h, h.sub_eq, λ h, by simpa using h.add (eventually_eq.refl l g)⟩ section has_le variables [has_le β] {l : filter α} /-- A function `f` is eventually less than or equal to a function `g` at a filter `l`. -/ def eventually_le (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x ≤ g x notation f ` ≤ᶠ[`:50 l:50 `] `:0 g:50 := eventually_le l f g lemma eventually_le.congr {f f' g g' : α → β} (H : f ≤ᶠ[l] g) (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : f' ≤ᶠ[l] g' := H.mp $ hg.mp $ hf.mono $ λ x hf hg H, by rwa [hf, hg] at H lemma eventually_le_congr {f f' g g' : α → β} (hf : f =ᶠ[l] f') (hg : g =ᶠ[l] g') : f ≤ᶠ[l] g ↔ f' ≤ᶠ[l] g' := ⟨λ H, H.congr hf hg, λ H, H.congr hf.symm hg.symm⟩ end has_le section preorder variables [preorder β] {l : filter α} {f g h : α → β} lemma eventually_eq.le (h : f =ᶠ[l] g) : f ≤ᶠ[l] g := h.mono $ λ x, le_of_eq @[refl] lemma eventually_le.refl (l : filter α) (f : α → β) : f ≤ᶠ[l] f := eventually_eq.rfl.le lemma eventually_le.rfl : f ≤ᶠ[l] f := eventually_le.refl l f @[trans] lemma eventually_le.trans (H₁ : f ≤ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h := H₂.mp $ H₁.mono $ λ x, le_trans @[trans] lemma eventually_eq.trans_le (H₁ : f =ᶠ[l] g) (H₂ : g ≤ᶠ[l] h) : f ≤ᶠ[l] h := H₁.le.trans H₂ @[trans] lemma eventually_le.trans_eq (H₁ : f ≤ᶠ[l] g) (H₂ : g =ᶠ[l] h) : f ≤ᶠ[l] h := H₁.trans H₂.le end preorder lemma eventually_le.antisymm [partial_order β] {l : filter α} {f g : α → β} (h₁ : f ≤ᶠ[l] g) (h₂ : g ≤ᶠ[l] f) : f =ᶠ[l] g := h₂.mp $ h₁.mono $ λ x, le_antisymm lemma eventually_le_antisymm_iff [partial_order β] {l : filter α} {f g : α → β} : f =ᶠ[l] g ↔ f ≤ᶠ[l] g ∧ g ≤ᶠ[l] f := by simp only [eventually_eq, eventually_le, le_antisymm_iff, eventually_and] lemma eventually_le.le_iff_eq [partial_order β] {l : filter α} {f g : α → β} (h : f ≤ᶠ[l] g) : g ≤ᶠ[l] f ↔ g =ᶠ[l] f := ⟨λ h', h'.antisymm h, eventually_eq.le⟩ @[mono] lemma eventually_le.inter {s t s' t' : set α} {l : filter α} (h : s ≤ᶠ[l] t) (h' : s' ≤ᶠ[l] t') : (s ∩ s' : set α) ≤ᶠ[l] (t ∩ t' : set α) := h'.mp $ h.mono $ λ x, and.imp @[mono] lemma eventually_le.union {s t s' t' : set α} {l : filter α} (h : s ≤ᶠ[l] t) (h' : s' ≤ᶠ[l] t') : (s ∪ s' : set α) ≤ᶠ[l] (t ∪ t' : set α) := h'.mp $ h.mono $ λ x, or.imp @[mono] lemma eventually_le.compl {s t : set α} {l : filter α} (h : s ≤ᶠ[l] t) : (tᶜ : set α) ≤ᶠ[l] (sᶜ : set α) := h.mono $ λ x, mt @[mono] lemma eventually_le.diff {s t s' t' : set α} {l : filter α} (h : s ≤ᶠ[l] t) (h' : t' ≤ᶠ[l] s') : (s \ s' : set α) ≤ᶠ[l] (t \ t' : set α) := h.inter h'.compl lemma join_le {f : filter (filter α)} {l : filter α} (h : ∀ᶠ m in f, m ≤ l) : join f ≤ l := λ s hs, h.mono $ λ m hm, hm hs /-! ### Push-forwards, pull-backs, and the monad structure -/ section map /-- The forward map of a filter -/ def map (m : α → β) (f : filter α) : filter β := { sets := preimage m ⁻¹' f.sets, univ_sets := univ_mem_sets, sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st, inter_sets := assume s t hs ht, inter_mem_sets hs ht } @[simp] lemma map_principal {s : set α} {f : α → β} : map f (𝓟 s) = 𝓟 (set.image f s) := filter_eq $ set.ext $ assume a, image_subset_iff.symm variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] lemma eventually_map {P : β → Prop} : (∀ᶠ b in map m f, P b) ↔ ∀ᶠ a in f, P (m a) := iff.rfl @[simp] lemma frequently_map {P : β → Prop} : (∃ᶠ b in map m f, P b) ↔ ∃ᶠ a in f, P (m a) := iff.rfl @[simp] lemma mem_map : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f := f.sets_of_superset hs $ subset_preimage_image m s lemma image_mem_map_iff (hf : function.injective m) : m '' s ∈ map m f ↔ s ∈ f := ⟨λ h, by rwa [← preimage_image_eq s hf], image_mem_map⟩ lemma range_mem_map : range m ∈ map m f := by rw ←image_univ; exact image_mem_map univ_mem_sets lemma mem_map_sets_iff : t ∈ map m f ↔ (∃s∈f, m '' s ⊆ t) := iff.intro (assume ht, ⟨m ⁻¹' t, ht, image_preimage_subset _ _⟩) (assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht) @[simp] lemma map_id : filter.map id f = f := filter_eq $ rfl @[simp] lemma map_id' : filter.map (λ x, x) f = f := map_id @[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) := funext $ assume _, filter_eq $ rfl @[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f := congr_fun (@@filter.map_compose m m') f /-- If functions `m₁` and `m₂` are eventually equal at a filter `f`, then they map this filter to the same filter. -/ lemma map_congr {m₁ m₂ : α → β} {f : filter α} (h : m₁ =ᶠ[f] m₂) : map m₁ f = map m₂ f := filter.ext' $ λ p, by { simp only [eventually_map], exact eventually_congr (h.mono $ λ x hx, hx ▸ iff.rfl) } end map section comap /-- The inverse map of a filter -/ def comap (m : α → β) (f : filter β) : filter α := { sets := { s | ∃t∈ f, m ⁻¹' t ⊆ s }, univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩, sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab, ⟨a', ha', subset.trans ma'a ab⟩, inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩, ⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ } @[simp] lemma eventually_comap {f : filter β} {φ : α → β} {P : α → Prop} : (∀ᶠ a in comap φ f, P a) ↔ ∀ᶠ b in f, ∀ a, φ a = b → P a := begin split ; intro h, { rcases h with ⟨t, t_in, ht⟩, apply mem_sets_of_superset t_in, rintros y y_in _ rfl, apply ht y_in }, { exact ⟨_, h, λ _ x_in, x_in _ rfl⟩ } end @[simp] lemma frequently_comap {f : filter β} {φ : α → β} {P : α → Prop} : (∃ᶠ a in comap φ f, P a) ↔ ∃ᶠ b in f, ∃ a, φ a = b ∧ P a := begin classical, erw [← not_iff_not, not_not, not_not, filter.eventually_comap], simp only [not_exists, not_and], end end comap /-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`. Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the applicative instance. -/ def bind (f : filter α) (m : α → filter β) : filter β := join (map m f) /-- The applicative sequentiation operation. This is not induced by the bind operation. -/ def seq (f : filter (α → β)) (g : filter α) : filter β := ⟨{ s | ∃u∈ f, ∃t∈ g, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) }, ⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩, assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩, assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩, ⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁, assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩ /-- `pure x` is the set of sets that contain `x`. It is equal to `𝓟 {x}` but with this definition we have `s ∈ pure a` defeq `a ∈ s`. -/ instance : has_pure filter := ⟨λ (α : Type u) x, { sets := {s | x ∈ s}, inter_sets := λ s t, and.intro, sets_of_superset := λ s t hs hst, hst hs, univ_sets := trivial }⟩ instance : has_bind filter := ⟨@filter.bind⟩ instance : has_seq filter := ⟨@filter.seq⟩ instance : functor filter := { map := @filter.map } lemma pure_sets (a : α) : (pure a : filter α).sets = {s | a ∈ s} := rfl @[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := iff.rfl @[simp] lemma eventually_pure {a : α} {p : α → Prop} : (∀ᶠ x in pure a, p x) ↔ p a := iff.rfl @[simp] lemma principal_singleton (a : α) : 𝓟 {a} = pure a := filter.ext $ λ s, by simp only [mem_pure_sets, mem_principal_sets, singleton_subset_iff] @[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) := rfl @[simp] lemma join_pure (f : filter α) : join (pure f) = f := filter.ext $ λ s, iff.rfl @[simp] lemma pure_bind (a : α) (m : α → filter β) : bind (pure a) m = m a := by simp only [has_bind.bind, bind, map_pure, join_pure] section -- this section needs to be before applicative, otherwise the wrong instance will be chosen /-- The monad structure on filters. -/ protected def monad : monad filter := { map := @filter.map } local attribute [instance] filter.monad protected lemma is_lawful_monad : is_lawful_monad filter := { id_map := assume α f, filter_eq rfl, pure_bind := assume α β, pure_bind, bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl, bind_pure_comp_eq_map := assume α β f x, filter.ext $ λ s, by simp only [has_bind.bind, bind, functor.map, mem_map, mem_join_sets, mem_set_of_eq, function.comp, mem_pure_sets] } end instance : applicative filter := { map := @filter.map, seq := @filter.seq } instance : alternative filter := { failure := λα, ⊥, orelse := λα x y, x ⊔ y } @[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl @[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl /- map and comap equations -/ section map variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β} @[simp] theorem mem_comap_sets : s ∈ comap m g ↔ ∃t∈ g, m ⁻¹' t ⊆ s := iff.rfl theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g := ⟨t, ht, subset.refl _⟩ lemma comap_id : comap id f = f := le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst) lemma comap_const_of_not_mem {x : α} {f : filter α} {V : set α} (hV : V ∈ f) (hx : x ∉ V) : comap (λ y : α, x) f = ⊥ := begin ext W, suffices : ∃ t ∈ f, (λ (y : α), x) ⁻¹' t ⊆ W, by simpa, use [V, hV], simp [preimage_const_of_not_mem hx], end lemma comap_const_of_mem {x : α} {f : filter α} (h : ∀ V ∈ f, x ∈ V) : comap (λ y : α, x) f = ⊤ := begin ext W, suffices : (∃ (t : set α), t ∈ f.sets ∧ (λ (y : α), x) ⁻¹' t ⊆ W) ↔ W = univ, by simpa, split, { rintros ⟨V, V_in, hW⟩, simpa [preimage_const_of_mem (h V V_in), univ_subset_iff] using hW }, { rintro rfl, use univ, simp [univ_mem_sets] }, end lemma comap_comap {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f := le_antisymm (assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩) (assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩, ⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩) @[simp] theorem comap_principal {t : set β} : comap m (𝓟 t) = 𝓟 (m ⁻¹' t) := filter_eq $ set.ext $ assume s, ⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b, assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩ @[simp] theorem comap_pure {b : β} : comap m (pure b) = 𝓟 (m ⁻¹' {b}) := by rw [← principal_singleton, comap_principal] lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g := ⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩ lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) := assume f g, map_le_iff_le_comap @[mono] lemma map_mono : monotone (map m) := (gc_map_comap m).monotone_l @[mono] lemma comap_mono : monotone (comap m) := (gc_map_comap m).monotone_u @[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot @[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup @[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) := (gc_map_comap m).l_supr @[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top @[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf @[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) := (gc_map_comap m).u_infi lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ := by rw [comap_top]; exact le_top lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _ lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _ @[simp] lemma comap_bot : comap m ⊥ = ⊥ := bot_unique $ λ s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩ lemma comap_supr {ι} {f : ι → filter β} {m : α → β} : comap m (supr f) = (⨆i, comap m (f i)) := le_antisymm (assume s hs, have ∀i, ∃t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs, let ⟨t, ht⟩ := classical.axiom_of_choice this in ⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _), begin rw [preimage_Union, Union_subset_iff], assume i, exact (ht i).2 end⟩) (supr_le $ assume i, comap_mono $ le_supr _ _) lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) := by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true] lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ := le_antisymm (assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩, ⟨t₁ ∪ t₂, ⟨mem_sets_of_superset ht₁ (subset_union_left _ _), mem_sets_of_superset ht₂ (subset_union_right _ _)⟩, union_subset hs₁ hs₂⟩) ((@comap_mono _ _ m).le_map_sup _ _) lemma map_comap (f : filter β) (m : α → β) : (f.comap m).map m = f ⊓ 𝓟 (range m) := begin refine le_antisymm (le_inf map_comap_le $ le_principal_iff.2 range_mem_map) _, rintro t' ⟨t, ht, sub⟩, refine mem_inf_principal.2 (mem_sets_of_superset ht _), rintro _ hxt ⟨x, rfl⟩, exact sub hxt end lemma map_comap_of_mem {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f := by rw [map_comap, inf_eq_left.2 (le_principal_iff.2 hf)] lemma comap_le_comap_iff {f g : filter β} {m : α → β} (hf : range m ∈ f) : comap m f ≤ comap m g ↔ f ≤ g := ⟨λ h, map_comap_of_mem hf ▸ (map_mono h).trans map_comap_le, λ h, comap_mono h⟩ theorem map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) : map f (comap f l) = l := map_comap_of_mem $ by simp only [hf.range_eq, univ_mem_sets] lemma subtype_coe_map_comap (s : set α) (f : filter α) : map (coe : s → α) (comap (coe : s → α) f) = f ⊓ 𝓟 s := by rw [map_comap, subtype.range_coe] lemma subtype_coe_map_comap_prod (s : set α) (f : filter (α × α)) : map (coe : s × s → α × α) (comap (coe : s × s → α × α) f) = f ⊓ 𝓟 (s.prod s) := have (coe : s × s → α × α) = (λ x, (x.1, x.2)), by ext ⟨x, y⟩; refl, by simp [this, map_comap, ← prod_range_range_eq] lemma image_mem_sets {f : filter α} {c : β → α} (h : range c ∈ f) {W : set β} (W_in : W ∈ comap c f) : c '' W ∈ f := begin rw ← map_comap_of_mem h, exact image_mem_map W_in end lemma image_coe_mem_sets {f : filter α} {U : set α} (h : U ∈ f) {W : set U} (W_in : W ∈ comap (coe : U → α) f) : coe '' W ∈ f := image_mem_sets (by simp [h]) W_in lemma comap_map {f : filter α} {m : α → β} (h : function.injective m) : comap m (map m f) = f := le_antisymm (assume s hs, mem_sets_of_superset (preimage_mem_comap $ image_mem_map hs) $ by simp only [preimage_image_eq s h]) le_comap_map lemma mem_comap_iff {f : filter β} {m : α → β} (inj : function.injective m) (large : set.range m ∈ f) {S : set α} : S ∈ comap m f ↔ m '' S ∈ f := by rw [← image_mem_map_iff inj, map_comap_of_mem large] lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f ≤ map m g) : f ≤ g := assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)] assume a has ⟨b, ⟨hbs, hb⟩, h⟩, have b = a, from hm _ hbs _ has h, this ▸ hb lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) : map m f ≤ map m g ↔ f ≤ g := iff.intro (le_of_map_le_map_inj' hsf hsg hm) (λ h, map_mono h) lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α} (hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) (h : map m f = map m g) : f = g := le_antisymm (le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h) (le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm) lemma map_inj {f g : filter α} {m : α → β} (hm : function.injective m) (h : map m f = map m g) : f = g := have comap m (map m f) = comap m (map m g), by rw h, by rwa [comap_map hm, comap_map hm] at this lemma comap_ne_bot_iff {f : filter β} {m : α → β} : ne_bot (comap m f) ↔ ∀ t ∈ f, ∃ a, m a ∈ t := begin rw ← forall_sets_nonempty_iff_ne_bot, exact ⟨λ h t t_in, h (m ⁻¹' t) ⟨t, t_in, subset.refl _⟩, λ h s ⟨u, u_in, hu⟩, let ⟨x, hx⟩ := h u u_in in ⟨x, hu hx⟩⟩, end lemma comap_ne_bot {f : filter β} {m : α → β} (hm : ∀t∈ f, ∃a, m a ∈ t) : ne_bot (comap m f) := comap_ne_bot_iff.mpr hm lemma comap_ne_bot_iff_frequently {f : filter β} {m : α → β} : ne_bot (comap m f) ↔ ∃ᶠ y in f, y ∈ range m := by simp [comap_ne_bot_iff, frequently_iff, ← exists_and_distrib_left, and.comm] lemma comap_ne_bot_iff_compl_range {f : filter β} {m : α → β} : ne_bot (comap m f) ↔ (range m)ᶜ ∉ f := comap_ne_bot_iff_frequently lemma ne_bot.comap_of_range_mem {f : filter β} {m : α → β} (hf : ne_bot f) (hm : range m ∈ f) : ne_bot (comap m f) := comap_ne_bot_iff_frequently.2 $ eventually.frequently hm lemma comap_inf_principal_ne_bot_of_image_mem {f : filter β} {m : α → β} (hf : ne_bot f) {s : set α} (hs : m '' s ∈ f) : ne_bot (comap m f ⊓ 𝓟 s) := begin refine ⟨compl_compl s ▸ mt mem_sets_of_eq_bot _⟩, rintros ⟨t, ht, hts⟩, rcases hf.nonempty_of_mem (inter_mem_sets hs ht) with ⟨_, ⟨x, hxs, rfl⟩, hxt⟩, exact absurd hxs (hts hxt) end lemma ne_bot.comap_of_surj {f : filter β} {m : α → β} (hf : ne_bot f) (hm : function.surjective m) : ne_bot (comap m f) := hf.comap_of_range_mem $ univ_mem_sets' hm lemma ne_bot.comap_of_image_mem {f : filter β} {m : α → β} (hf : ne_bot f) {s : set α} (hs : m '' s ∈ f) : ne_bot (comap m f) := hf.comap_of_range_mem $ mem_sets_of_superset hs (image_subset_range _ _) @[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ := ⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id, assume h, by simp only [h, eq_self_iff_true, map_bot]⟩ lemma map_ne_bot_iff (f : α → β) {F : filter α} : ne_bot (map f F) ↔ ne_bot F := by simp only [ne_bot_iff, ne, map_eq_bot_iff] lemma ne_bot.map (hf : ne_bot f) (m : α → β) : ne_bot (map m f) := (map_ne_bot_iff m).2 hf instance map_ne_bot [hf : ne_bot f] : ne_bot (f.map m) := hf.map m lemma sInter_comap_sets (f : α → β) (F : filter β) : ⋂₀ (comap f F).sets = ⋂ U ∈ F, f ⁻¹' U := begin ext x, suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔ ∀ (B : set β), B ∈ F → f x ∈ B, by simp only [mem_sInter, mem_Inter, filter.mem_sets, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter, mem_Inter, mem_preimage, exists_imp_distrib], split, { intros h U U_in, simpa only [set.subset.refl, forall_prop_of_true, mem_preimage] using h (f ⁻¹' U) U U_in }, { intros h V U U_in f_U_V, exact f_U_V (h U U_in) }, end end map -- this is a generic rule for monotone functions: lemma map_infi_le {f : ι → filter α} {m : α → β} : map m (infi f) ≤ (⨅ i, map m (f i)) := le_infi $ assume i, map_mono $ infi_le _ _ lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) [nonempty ι] : map m (infi f) = (⨅ i, map m (f i)) := le_antisymm map_infi_le (assume s (hs : preimage m s ∈ infi f), have ∃i, preimage m s ∈ f i, by simp only [mem_infi hf, mem_Union] at hs; assumption, let ⟨i, hi⟩ := this in have (⨅ i, map m (f i)) ≤ 𝓟 s, from infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption, by simp only [filter.le_principal_iff] at this; assumption) lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop} (h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) : map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) := begin haveI := nonempty_subtype.2 ne, simp only [infi_subtype'], exact map_infi_eq h.directed_coe end lemma map_inf_le {f g : filter α} {m : α → β} : map m (f ⊓ g) ≤ map m f ⊓ map m g := (@map_mono _ _ m).map_inf_le f g lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g) (h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g := begin refine le_antisymm map_inf_le (assume s hs, _), simp only [mem_inf_sets, exists_prop, mem_map, mem_preimage, mem_inf_sets] at hs ⊢, rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩, refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩, { filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ }, { rw [image_inter_on], { refine image_subset_iff.2 _, exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ }, { exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } } end lemma map_inf {f g : filter α} {m : α → β} (h : function.injective m) : map m (f ⊓ g) = map m f ⊓ map m g := map_inf' univ_mem_sets univ_mem_sets (assume x _ y _ hxy, h hxy) lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α} (h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f := le_antisymm (assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $ calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true] ... ⊆ preimage m b : preimage_mono h) (assume b (hb : preimage m b ∈ f), ⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩) lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f := map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀ s ∈ f, m '' s ∈ g) : g ≤ f.map m := assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _ protected lemma push_pull (f : α → β) (F : filter α) (G : filter β) : map f (F ⊓ comap f G) = map f F ⊓ G := begin apply le_antisymm, { calc map f (F ⊓ comap f G) ≤ map f F ⊓ (map f $ comap f G) : map_inf_le ... ≤ map f F ⊓ G : inf_le_inf_left (map f F) map_comap_le }, { rintros U ⟨V, V_in, W, ⟨Z, Z_in, hZ⟩, h⟩, rw ← image_subset_iff at h, use [f '' V, image_mem_map V_in, Z, Z_in], refine subset.trans _ h, have : f '' (V ∩ f ⁻¹' Z) ⊆ f '' (V ∩ W), from image_subset _ (inter_subset_inter_right _ ‹_›), rwa image_inter_preimage at this } end protected lemma push_pull' (f : α → β) (F : filter α) (G : filter β) : map f (comap f G ⊓ F) = G ⊓ map f F := by simp only [filter.push_pull, inf_comm] section applicative lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α) := mem_singleton a lemma pure_injective : function.injective (pure : α → filter α) := assume a b hab, (filter.ext_iff.1 hab {x | a = x}).1 rfl instance pure_ne_bot {α : Type u} {a : α} : ne_bot (pure a) := ⟨mt empty_in_sets_eq_bot.2 $ not_mem_empty a⟩ @[simp] lemma le_pure_iff {f : filter α} {a : α} : f ≤ pure a ↔ {a} ∈ f := ⟨λ h, h singleton_mem_pure_sets, λ h s hs, mem_sets_of_superset h $ singleton_subset_iff.2 hs⟩ lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) := iff.rfl lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} : s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, set.seq u t ⊆ s) := by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self] lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} : s ∈ (f.map m).seq g ↔ (∃t u, t ∈ g ∧ u ∈ f ∧ ∀x∈u, ∀y∈t, m x y ∈ s) := iff.intro (λ ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, λ a, hts _⟩) (λ ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, λ f ⟨a, has, eq⟩, eq ▸ hts _ has⟩) lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α} (hs : s ∈ f) (ht : t ∈ g) : s.seq t ∈ f.seq g := ⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩ lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β} (hh : ∀t ∈ f, ∀u ∈ g, set.seq t u ∈ h) : h ≤ seq f g := assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $ assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha @[mono] lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ := le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht) @[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← singleton_seq, apply seq_mem_seq_sets _ hs, exact singleton_mem_pure_sets }, { refine sets_of_superset (map g f) (image_mem_map ht) _, rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ } end @[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f := begin refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _), { rw ← seq_singleton, exact seq_mem_seq_sets hs singleton_mem_pure_sets }, { refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _, rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ } end @[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) : seq h (seq g x) = seq (seq (map (∘) h) g) x := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)), rw ← set.seq_seq, exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) }, { rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht), rw set.seq_seq, exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv } end lemma prod_map_seq_comm (f : filter α) (g : filter β) : (map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f := begin refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _), { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw ← set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu }, { rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩, refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)), rw set.prod_image_seq_comm, exact seq_mem_seq_sets (image_mem_map ht) hu } end instance : is_lawful_functor (filter : Type u → Type u) := { id_map := assume α f, map_id, comp_map := assume α β γ f g a, map_map.symm } instance : is_lawful_applicative (filter : Type u → Type u) := { pure_seq_eq_map := assume α β, pure_seq_eq_map, map_pure := assume α β, map_pure, seq_pure := assume α β, seq_pure, seq_assoc := assume α β γ, seq_assoc } instance : is_comm_applicative (filter : Type u → Type u) := ⟨assume α β f g, prod_map_seq_comm f g⟩ lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) : f <*> g = seq f g := rfl end applicative /- bind equations -/ section bind @[simp] lemma eventually_bind {f : filter α} {m : α → filter β} {p : β → Prop} : (∀ᶠ y in bind f m, p y) ↔ ∀ᶠ x in f, ∀ᶠ y in m x, p y := iff.rfl @[simp] lemma eventually_eq_bind {f : filter α} {m : α → filter β} {g₁ g₂ : β → γ} : (g₁ =ᶠ[bind f m] g₂) ↔ ∀ᶠ x in f, g₁ =ᶠ[m x] g₂ := iff.rfl @[simp] lemma eventually_le_bind [has_le γ] {f : filter α} {m : α → filter β} {g₁ g₂ : β → γ} : (g₁ ≤ᶠ[bind f m] g₂) ↔ ∀ᶠ x in f, g₁ ≤ᶠ[m x] g₂ := iff.rfl lemma mem_bind_sets' {s : set β} {f : filter α} {m : α → filter β} : s ∈ bind f m ↔ {a | s ∈ m a} ∈ f := iff.rfl @[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} : s ∈ bind f m ↔ ∃t ∈ f, ∀x ∈ t, s ∈ m x := calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : iff.rfl ... ↔ (∃t ∈ f, t ⊆ {a | s ∈ m a}) : exists_sets_subset_iff.symm ... ↔ (∃t ∈ f, ∀x ∈ t, s ∈ m x) : iff.rfl lemma bind_le {f : filter α} {g : α → filter β} {l : filter β} (h : ∀ᶠ x in f, g x ≤ l) : f.bind g ≤ l := join_le $ eventually_map.2 h @[mono] lemma bind_mono {f₁ f₂ : filter α} {g₁ g₂ : α → filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ᶠ[f₁] g₂) : bind f₁ g₁ ≤ bind f₂ g₂ := begin refine le_trans (λ s hs, _) (join_mono $ map_mono hf), simp only [mem_join_sets, mem_bind_sets', mem_map] at hs ⊢, filter_upwards [hg, hs], exact λ x hx hs, hx hs end lemma bind_inf_principal {f : filter α} {g : α → filter β} {s : set β} : f.bind (λ x, g x ⊓ 𝓟 s) = (f.bind g) ⊓ 𝓟 s := filter.ext $ λ s, by simp only [mem_bind_sets, mem_inf_principal] lemma sup_bind {f g : filter α} {h : α → filter β} : bind (f ⊔ g) h = bind f h ⊔ bind g h := by simp only [bind, sup_join, map_sup, eq_self_iff_true] lemma principal_bind {s : set α} {f : α → filter β} : (bind (𝓟 s) f) = (⨆x ∈ s, f x) := show join (map f (𝓟 s)) = (⨆x ∈ s, f x), by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true] end bind section list_traverse /- This is a separate section in order to open `list`, but mostly because of universe equality requirements in `traverse` -/ open list lemma sequence_mono : ∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs | [] [] forall₂.nil := le_refl _ | (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs) variables {α' β' γ' : Type u} {f : β' → filter α'} {s : γ' → set α'} lemma mem_traverse_sets : ∀(fs : list β') (us : list γ'), forall₂ (λb c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs | [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _ | (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs) lemma mem_traverse_sets_iff (fs : list β') (t : set (list α')) : t ∈ traverse f fs ↔ (∃us:list (set α'), forall₂ (λb (s : set α'), s ∈ f b) fs us ∧ sequence us ⊆ t) := begin split, { induction fs generalizing t, case nil { simp only [sequence, mem_pure_sets, imp_self, forall₂_nil_left_iff, exists_eq_left, set.pure_def, singleton_subset_iff, traverse_nil] }, case cons : b fs ih t { assume ht, rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩, rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩, rcases ih v hv with ⟨us, hus, hu⟩, exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } }, { rintros ⟨us, hus, hs⟩, exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs } end end list_traverse /-! ### Limits -/ /-- `tendsto` is the generic "limit of a function" predicate. `tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`, the `f`-preimage of `a` is an `l₁` neighborhood. -/ def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂ lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl lemma tendsto_iff_eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ ∀ ⦃p : β → Prop⦄, (∀ᶠ y in l₂, p y) → ∀ᶠ x in l₁, p (f x) := iff.rfl lemma tendsto.eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop} (hf : tendsto f l₁ l₂) (h : ∀ᶠ y in l₂, p y) : ∀ᶠ x in l₁, p (f x) := hf h lemma tendsto.frequently {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop} (hf : tendsto f l₁ l₂) (h : ∃ᶠ x in l₁, p (f x)) : ∃ᶠ y in l₂, p y := mt hf.eventually h @[simp] lemma tendsto_bot {f : α → β} {l : filter β} : tendsto f ⊥ l := by simp [tendsto] @[simp] lemma tendsto_top {f : α → β} {l : filter α} : tendsto f l ⊤ := le_top lemma le_map_of_right_inverse {mab : α → β} {mba : β → α} {f : filter α} {g : filter β} (h₁ : mab ∘ mba =ᶠ[g] id) (h₂ : tendsto mba g f) : g ≤ map mab f := by { rw [← @map_id _ g, ← map_congr h₁, ← map_map], exact map_mono h₂ } lemma tendsto_of_not_nonempty {f : α → β} {la : filter α} {lb : filter β} (h : ¬nonempty α) : tendsto f la lb := by simp only [filter_eq_bot_of_not_nonempty la h, tendsto_bot] lemma eventually_eq_of_left_inv_of_right_inv {f : α → β} {g₁ g₂ : β → α} {fa : filter α} {fb : filter β} (hleft : ∀ᶠ x in fa, g₁ (f x) = x) (hright : ∀ᶠ y in fb, f (g₂ y) = y) (htendsto : tendsto g₂ fb fa) : g₁ =ᶠ[fb] g₂ := (htendsto.eventually hleft).mp $ hright.mono $ λ y hr hl, (congr_arg g₁ hr.symm).trans hl lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} : tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f := map_le_iff_le_comap alias tendsto_iff_comap ↔ filter.tendsto.le_comap _ lemma tendsto_congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : f₁ =ᶠ[l₁] f₂) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := by rw [tendsto, tendsto, map_congr hl] lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (hl : f₁ =ᶠ[l₁] f₂) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ := (tendsto_congr' hl).1 h theorem tendsto_congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ := tendsto_congr' (univ_mem_sets' h) theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β} (h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ := (tendsto_congr h).1 lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y := by simp only [tendsto, map_id, forall_true_iff] {contextual := tt} lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ} (hg : tendsto g y z) (hf : tendsto f x y) : tendsto (g ∘ f) x z := calc map (g ∘ f) x = map g (map f x) : by rw [map_map] ... ≤ map g y : map_mono hf ... ≤ z : hg lemma tendsto.mono_left {f : α → β} {x y : filter α} {z : filter β} (hx : tendsto f x z) (h : y ≤ x) : tendsto f y z := le_trans (map_mono h) hx lemma tendsto.mono_right {f : α → β} {x : filter α} {y z : filter β} (hy : tendsto f x y) (hz : y ≤ z) : tendsto f x z := le_trans hy hz lemma tendsto.ne_bot {f : α → β} {x : filter α} {y : filter β} (h : tendsto f x y) [hx : ne_bot x] : ne_bot y := (hx.map _).mono h lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x) lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} (h : tendsto (f ∘ g) x y) : tendsto f (map g x) y := by rwa [tendsto, map_map] lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} : tendsto f (map g x) y ↔ tendsto (f ∘ g) x y := by rw [tendsto, map_map]; refl lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x := map_comap_le lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} : tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c := ⟨assume h, tendsto_comap.comp h, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩ lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α} (h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g := by rw [tendsto, ← map_compose]; simp only [(∘), map_comap_of_mem h, tendsto] lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f := begin refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ), rw [comap_comap, eq, comap_id], exact le_refl _ end lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α) (eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g := begin refine le_antisymm hφ (le_trans _ (map_mono hψ)), rw [map_map, eq, map_id], exact le_refl _ end lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} : tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ := by simp only [tendsto, le_inf_iff, iff_self] lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_left) h lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β} (h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y := le_trans (map_mono inf_le_right) h lemma tendsto.inf {f : α → β} {x₁ x₂ : filter α} {y₁ y₂ : filter β} (h₁ : tendsto f x₁ y₁) (h₂ : tendsto f x₂ y₂) : tendsto f (x₁ ⊓ x₂) (y₁ ⊓ y₂) := tendsto_inf.2 ⟨tendsto_inf_left h₁, tendsto_inf_right h₂⟩ @[simp] lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} : tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) := by simp only [tendsto, iff_self, le_infi_iff] lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) (hi : tendsto f (x i) y) : tendsto f (⨅i, x i) y := hi.mono_left $ infi_le _ _ lemma tendsto_sup {f : α → β} {x₁ x₂ : filter α} {y : filter β} : tendsto f (x₁ ⊔ x₂) y ↔ tendsto f x₁ y ∧ tendsto f x₂ y := by simp only [tendsto, map_sup, sup_le_iff] lemma tendsto.sup {f : α → β} {x₁ x₂ : filter α} {y : filter β} : tendsto f x₁ y → tendsto f x₂ y → tendsto f (x₁ ⊔ x₂) y := λ h₁ h₂, tendsto_sup.mpr ⟨ h₁, h₂ ⟩ @[simp] lemma tendsto_principal {f : α → β} {l : filter α} {s : set β} : tendsto f l (𝓟 s) ↔ ∀ᶠ a in l, f a ∈ s := by simp only [tendsto, le_principal_iff, mem_map, filter.eventually] @[simp] lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} : tendsto f (𝓟 s) (𝓟 t) ↔ ∀a∈s, f a ∈ t := by simp only [tendsto_principal, eventually_principal] @[simp] lemma tendsto_pure {f : α → β} {a : filter α} {b : β} : tendsto f a (pure b) ↔ ∀ᶠ x in a, f x = b := by simp only [tendsto, le_pure_iff, mem_map, mem_singleton_iff, filter.eventually] lemma tendsto_pure_pure (f : α → β) (a : α) : tendsto f (pure a) (pure (f a)) := tendsto_pure.2 rfl lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λx, b) a (pure b) := tendsto_pure.2 $ univ_mem_sets' $ λ _, rfl lemma pure_le_iff {a : α} {l : filter α} : pure a ≤ l ↔ ∀ s ∈ l, a ∈ s := iff.rfl lemma tendsto_pure_left {f : α → β} {a : α} {l : filter β} : tendsto f (pure a) l ↔ ∀ s ∈ l, f a ∈ s := iff.rfl @[simp] lemma map_inf_principal_preimage {f : α → β} {s : set β} {l : filter α} : map f (l ⊓ 𝓟 (f ⁻¹' s)) = map f l ⊓ 𝓟 s := filter.ext $ λ t, by simp only [mem_map, mem_inf_principal, mem_set_of_eq, mem_preimage] /-- If two filters are disjoint, then a function cannot tend to both of them along a non-trivial filter. -/ lemma tendsto.not_tendsto {f : α → β} {a : filter α} {b₁ b₂ : filter β} (hf : tendsto f a b₁) [ne_bot a] (hb : disjoint b₁ b₂) : ¬ tendsto f a b₂ := λ hf', (tendsto_inf.2 ⟨hf, hf'⟩).ne_bot.ne hb.eq_bot lemma tendsto.if {l₁ : filter α} {l₂ : filter β} {f g : α → β} {p : α → Prop} [∀ x, decidable (p x)] (h₀ : tendsto f (l₁ ⊓ 𝓟 {x | p x}) l₂) (h₁ : tendsto g (l₁ ⊓ 𝓟 { x | ¬ p x }) l₂) : tendsto (λ x, if p x then f x else g x) l₁ l₂ := begin simp only [tendsto_def, mem_inf_principal] at *, intros s hs, filter_upwards [h₀ s hs, h₁ s hs], simp only [mem_preimage], intros x hp₀ hp₁, split_ifs, exacts [hp₀ h, hp₁ h] end lemma tendsto.piecewise {l₁ : filter α} {l₂ : filter β} {f g : α → β} {s : set α} [∀ x, decidable (x ∈ s)] (h₀ : tendsto f (l₁ ⊓ 𝓟 s) l₂) (h₁ : tendsto g (l₁ ⊓ 𝓟 sᶜ) l₂) : tendsto (piecewise s f g) l₁ l₂ := h₀.if h₁ /-! ### Products of filters -/ section prod variables {s : set α} {t : set β} {f : filter α} {g : filter β} /- The product filter cannot be defined using the monad structure on filters. For example: F := do {x ← seq, y ← top, return (x, y)} hence: s ∈ F ↔ ∃n, [n..∞] × univ ⊆ s G := do {y ← top, x ← seq, return (x, y)} hence: s ∈ G ↔ ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s Now ⋃i, [i..∞] × {i} is in G but not in F. As product filter we want to have F as result. -/ /-- Product of filters. This is the filter generated by cartesian products of elements of the component filters. -/ protected def prod (f : filter α) (g : filter β) : filter (α × β) := f.comap prod.fst ⊓ g.comap prod.snd localized "infix ` ×ᶠ `:60 := filter.prod" in filter lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β} (hs : s ∈ f) (ht : t ∈ g) : set.prod s t ∈ f ×ᶠ g := inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht) lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} : s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, set.prod t₁ t₂ ⊆ s) := begin simp only [filter.prod], split, exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩, ⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩, exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩, ⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩ end lemma comap_prod (f : α → β × γ) (b : filter β) (c : filter γ) : comap f (b ×ᶠ c) = (comap (prod.fst ∘ f) b) ⊓ (comap (prod.snd ∘ f) c) := by erw [comap_inf, filter.comap_comap, filter.comap_comap] lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} : (∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x) (pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) := by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f := tendsto_inf_left tendsto_comap lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g := tendsto_inf_right tendsto_comap lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ} (h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (g ×ᶠ h) := tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩ lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 := tendsto_fst.eventually h lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) : ∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 := tendsto_snd.eventually h lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x) {lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) : ∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl lb).and (hb.prod_inr la) lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop} (h : ∀ᶠ x in la ×ᶠ lb, p x) : ∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) := begin rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩, exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb) end lemma prod_infi_left [nonempty ι] {f : ι → filter α} {g : filter β}: (⨅i, f i) ×ᶠ g = (⨅i, (f i) ×ᶠ g) := by rw [filter.prod, comap_infi, infi_inf]; simp only [filter.prod, eq_self_iff_true] lemma prod_infi_right [nonempty ι] {f : filter α} {g : ι → filter β} : f ×ᶠ (⨅i, g i) = (⨅i, f ×ᶠ (g i)) := by rw [filter.prod, comap_infi, inf_infi]; simp only [filter.prod, eq_self_iff_true] @[mono] lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ := inf_le_inf (comap_mono hf) (comap_mono hg) lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} : (comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := by simp only [filter.prod, comap_comap, eq_self_iff_true, comap_inf] lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) := by simp only [filter.prod, comap_comap, (∘), inf_comm, prod.fst_swap, eq_self_iff_true, prod.snd_swap, comap_inf] lemma prod_comm : f ×ᶠ g = map (λp:β×α, (p.2, p.1)) (g ×ᶠ f) := by rw [prod_comm', ← map_swap_eq_comap_swap]; refl lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x} {f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} : (map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) := le_antisymm (assume s hs, let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $ calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ : set.prod_image_image_eq ... ⊆ _ : by rwa [image_subset_iff]) ((tendsto.comp (le_refl _) tendsto_fst).prod_mk (tendsto.comp (le_refl _) tendsto_snd)) lemma prod_map_map_eq' {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} (f : α₁ → α₂) (g : β₁ → β₂) (F : filter α₁) (G : filter β₁) : (map f F) ×ᶠ (map g G) = map (prod.map f g) (F ×ᶠ G) := by { rw filter.prod_map_map_eq, refl } lemma tendsto.prod_map {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β} {c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) : tendsto (prod.map f g) (a ×ᶠ b) (c ×ᶠ d) := begin erw [tendsto, ← prod_map_map_eq], exact filter.prod_mono hf hg, end lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) : map m (f ×ᶠ g) = (f.map (λa b, m (a, b))).seq g := begin simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff], assume s, split, exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩, exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩ end lemma prod_eq {f : filter α} {g : filter β} : f ×ᶠ g = (f.map prod.mk).seq g := have h : _ := map_prod id f g, by rwa [map_id] at h lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} : (f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) := by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm] @[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod] @[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod] @[simp] lemma prod_principal_principal {s : set α} {t : set β} : (𝓟 s) ×ᶠ (𝓟 t) = 𝓟 (set.prod s t) := by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl @[simp] lemma pure_prod {a : α} {f : filter β} : pure a ×ᶠ f = map (prod.mk a) f := by rw [prod_eq, map_pure, pure_seq_eq_map] @[simp] lemma prod_pure {f : filter α} {b : β} : f ×ᶠ pure b = map (λ a, (a, b)) f := by rw [prod_eq, seq_pure, map_map] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) := by simp lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) := begin split, { assume h, rcases mem_prod_iff.1 (empty_in_sets_eq_bot.2 h) with ⟨s, hs, t, ht, hst⟩, rw [subset_empty_iff, set.prod_eq_empty_iff] at hst, cases hst with s_eq t_eq, { left, exact empty_in_sets_eq_bot.1 (s_eq ▸ hs) }, { right, exact empty_in_sets_eq_bot.1 (t_eq ▸ ht) } }, { rintros (rfl | rfl), exact bot_prod, exact prod_bot } end lemma prod_ne_bot {f : filter α} {g : filter β} : ne_bot (f ×ᶠ g) ↔ (ne_bot f ∧ ne_bot g) := by simp only [ne_bot_iff, ne, prod_eq_bot, not_or_distrib] lemma ne_bot.prod {f : filter α} {g : filter β} (hf : ne_bot f) (hg : ne_bot g) : ne_bot (f ×ᶠ g) := prod_ne_bot.2 ⟨hf, hg⟩ instance prod_ne_bot' {f : filter α} {g : filter β} [hf : ne_bot f] [hg : ne_bot g] : ne_bot (f ×ᶠ g) := hf.prod hg lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} : filter.tendsto f (x ×ᶠ y) z ↔ ∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W := by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self] end prod end filter open_locale filter lemma set.eq_on.eventually_eq {α β} {s : set α} {f g : α → β} (h : eq_on f g s) : f =ᶠ[𝓟 s] g := h lemma set.eq_on.eventually_eq_of_mem {α β} {s : set α} {l : filter α} {f g : α → β} (h : eq_on f g s) (hl : s ∈ l) : f =ᶠ[l] g := h.eventually_eq.filter_mono $ filter.le_principal_iff.2 hl lemma set.subset.eventually_le {α} {l : filter α} {s t : set α} (h : s ⊆ t) : s ≤ᶠ[l] t := filter.eventually_of_forall h
353cc53a66cd8c2198c566eea5b618b8359732b0
bb31430994044506fa42fd667e2d556327e18dfe
/src/algebra/hom/freiman.lean
f55b01bfd49b5e8c691871572143b0c7329e7758
[ "Apache-2.0" ]
permissive
sgouezel/mathlib
0cb4e5335a2ba189fa7af96d83a377f83270e503
00638177efd1b2534fc5269363ebf42a7871df9a
refs/heads/master
1,674,527,483,042
1,673,665,568,000
1,673,665,568,000
119,598,202
0
0
null
1,517,348,647,000
1,517,348,646,000
null
UTF-8
Lean
false
false
17,494
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.multiset.basic import data.fun_like.basic /-! # Freiman homomorphisms In this file, we define Freiman homomorphisms. A `n`-Freiman homomorphism on `A` is a function `f : α → β` such that `f (x₁) * ... * f (xₙ) = f (y₁) * ... * f (yₙ)` for all `x₁, ..., xₙ, y₁, ..., yₙ ∈ A` such that `x₁ * ... * xₙ = y₁ * ... * yₙ`. In particular, any `mul_hom` is a Freiman homomorphism. They are of interest in additive combinatorics. ## Main declaration * `freiman_hom`: Freiman homomorphism. * `add_freiman_hom`: Additive Freiman homomorphism. ## Notation * `A →*[n] β`: Multiplicative `n`-Freiman homomorphism on `A` * `A →+[n] β`: Additive `n`-Freiman homomorphism on `A` ## Implementation notes In the context of combinatorics, we are interested in Freiman homomorphisms over sets which are not necessarily closed under addition/multiplication. This means we must parametrize them with a set in an `add_monoid`/`monoid` instead of the `add_monoid`/`monoid` itself. ## References [Yufei Zhao, *18.225: Graph Theory and Additive Combinatorics*](https://yufeizhao.com/gtac/) ## TODO `monoid_hom.to_freiman_hom` could be relaxed to `mul_hom.to_freiman_hom` by proving `(s.map f).prod = (t.map f).prod` directly by induction instead of going through `f s.prod`. Define `n`-Freiman isomorphisms. Affine maps induce Freiman homs. Concretely, provide the `add_freiman_hom_class (α →ₐ[𝕜] β) A β n` instance. -/ open multiset variables {F α β γ δ G : Type*} /-- An additive `n`-Freiman homomorphism is a map which preserves sums of `n` elements. -/ structure add_freiman_hom (A : set α) (β : Type*) [add_comm_monoid α] [add_comm_monoid β] (n : ℕ) := (to_fun : α → β) (map_sum_eq_map_sum' {s t : multiset α} (hsA : ∀ ⦃x⦄, x ∈ s → x ∈ A) (htA : ∀ ⦃x⦄, x ∈ t → x ∈ A) (hs : s.card = n) (ht : t.card = n) (h : s.sum = t.sum) : (s.map to_fun).sum = (t.map to_fun).sum) /-- A `n`-Freiman homomorphism on a set `A` is a map which preserves products of `n` elements. -/ @[to_additive add_freiman_hom] structure freiman_hom (A : set α) (β : Type*) [comm_monoid α] [comm_monoid β] (n : ℕ) := (to_fun : α → β) (map_prod_eq_map_prod' {s t : multiset α} (hsA : ∀ ⦃x⦄, x ∈ s → x ∈ A) (htA : ∀ ⦃x⦄, x ∈ t → x ∈ A) (hs : s.card = n) (ht : t.card = n) (h : s.prod = t.prod) : (s.map to_fun).prod = (t.map to_fun).prod) notation (name := add_freiman_hom) A ` →+[`:25 n:25 `] `:0 β:0 := add_freiman_hom A β n notation (name := freiman_hom) A ` →*[`:25 n:25 `] `:0 β:0 := freiman_hom A β n /-- `add_freiman_hom_class F s β n` states that `F` is a type of `n`-ary sums-preserving morphisms. You should extend this class when you extend `add_freiman_hom`. -/ class add_freiman_hom_class (F : Type*) (A : out_param $ set α) (β : out_param $ Type*) [add_comm_monoid α] [add_comm_monoid β] (n : ℕ) [fun_like F α (λ _, β)] : Prop := (map_sum_eq_map_sum' (f : F) {s t : multiset α} (hsA : ∀ ⦃x⦄, x ∈ s → x ∈ A) (htA : ∀ ⦃x⦄, x ∈ t → x ∈ A) (hs : s.card = n) (ht : t.card = n) (h : s.sum = t.sum) : (s.map f).sum = (t.map f).sum) /-- `freiman_hom_class F A β n` states that `F` is a type of `n`-ary products-preserving morphisms. You should extend this class when you extend `freiman_hom`. -/ @[to_additive add_freiman_hom_class "`add_freiman_hom_class F A β n` states that `F` is a type of `n`-ary sums-preserving morphisms. You should extend this class when you extend `add_freiman_hom`."] class freiman_hom_class (F : Type*) (A : out_param $ set α) (β : out_param $ Type*) [comm_monoid α] [comm_monoid β] (n : ℕ) [fun_like F α (λ _, β)] : Prop := (map_prod_eq_map_prod' (f : F) {s t : multiset α} (hsA : ∀ ⦃x⦄, x ∈ s → x ∈ A) (htA : ∀ ⦃x⦄, x ∈ t → x ∈ A) (hs : s.card = n) (ht : t.card = n) (h : s.prod = t.prod) : (s.map f).prod = (t.map f).prod) variables [fun_like F α (λ _, β)] section comm_monoid variables [comm_monoid α] [comm_monoid β] [comm_monoid γ] [comm_monoid δ] [comm_group G] {A : set α} {B : set β} {C : set γ} {n : ℕ} {a b c d : α} @[to_additive] lemma map_prod_eq_map_prod [freiman_hom_class F A β n] (f : F) {s t : multiset α} (hsA : ∀ ⦃x⦄, x ∈ s → x ∈ A) (htA : ∀ ⦃x⦄, x ∈ t → x ∈ A) (hs : s.card = n) (ht : t.card = n) (h : s.prod = t.prod) : (s.map f).prod = (t.map f).prod := freiman_hom_class.map_prod_eq_map_prod' f hsA htA hs ht h @[to_additive] lemma map_mul_map_eq_map_mul_map [freiman_hom_class F A β 2] (f : F) (ha : a ∈ A) (hb : b ∈ A) (hc : c ∈ A) (hd : d ∈ A) (h : a * b = c * d) : f a * f b = f c * f d := begin simp_rw ←prod_pair at ⊢ h, refine map_prod_eq_map_prod f _ _ (card_pair _ _) (card_pair _ _) h; simp [ha, hb, hc, hd], end namespace freiman_hom @[to_additive] instance fun_like : fun_like (A →*[n] β) α (λ _, β) := { coe := to_fun, coe_injective' := λ f g h, by cases f; cases g; congr' } @[to_additive] instance freiman_hom_class : freiman_hom_class (A →*[n] β) A β n := { map_prod_eq_map_prod' := map_prod_eq_map_prod' } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ @[to_additive "Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly."] instance : has_coe_to_fun (A →*[n] β) (λ _, α → β) := ⟨to_fun⟩ initialize_simps_projections freiman_hom (to_fun → apply) @[simp, to_additive] lemma to_fun_eq_coe (f : A →*[n] β) : f.to_fun = f := rfl @[ext, to_additive] lemma ext ⦃f g : A →*[n] β⦄ (h : ∀ x, f x = g x) : f = g := fun_like.ext f g h @[simp, to_additive] lemma coe_mk (f : α → β) (h : ∀ s t : multiset α, (∀ ⦃x⦄, x ∈ s → x ∈ A) → (∀ ⦃x⦄, x ∈ t → x ∈ A) → s.card = n → t.card = n → s.prod = t.prod → (s.map f).prod = (t.map f).prod) : ⇑(mk f h) = f := rfl @[simp, to_additive] lemma mk_coe (f : A →*[n] β) (h) : mk f h = f := ext $ λ _, rfl /-- The identity map from a commutative monoid to itself. -/ @[to_additive "The identity map from an additive commutative monoid to itself.", simps] protected def id (A : set α) (n : ℕ) : A →*[n] α := { to_fun := λ x, x, map_prod_eq_map_prod' := λ s t _ _ _ _ h, by rw [map_id', map_id', h] } /-- Composition of Freiman homomorphisms as a Freiman homomorphism. -/ @[to_additive "Composition of additive Freiman homomorphisms as an additive Freiman homomorphism."] protected def comp (f : B →*[n] γ) (g : A →*[n] β) (hAB : A.maps_to g B) : A →*[n] γ := { to_fun := f ∘ g, map_prod_eq_map_prod' := λ s t hsA htA hs ht h, begin rw [←map_map, map_prod_eq_map_prod f _ _ ((s.card_map _).trans hs) ((t.card_map _).trans ht) (map_prod_eq_map_prod g hsA htA hs ht h), map_map], { simpa using (λ a h, hAB (hsA h)) }, { simpa using (λ a h, hAB (htA h)) } end } @[simp, to_additive] lemma coe_comp (f : B →*[n] γ) (g : A →*[n] β) {hfg} : ⇑(f.comp g hfg) = f ∘ g := rfl @[to_additive] lemma comp_apply (f : B →*[n] γ) (g : A →*[n] β) {hfg} (x : α) : f.comp g hfg x = f (g x) := rfl @[to_additive] lemma comp_assoc (f : A →*[n] β) (g : B →*[n] γ) (h : C →*[n] δ) {hf hhg hgf} {hh : A.maps_to (g.comp f hgf) C} : (h.comp g hhg).comp f hf = h.comp (g.comp f hgf) hh := rfl @[to_additive] lemma cancel_right {g₁ g₂ : B →*[n] γ} {f : A →*[n] β} (hf : function.surjective f) {hg₁ hg₂} : g₁.comp f hg₁ = g₂.comp f hg₂ ↔ g₁ = g₂ := ⟨λ h, ext $ hf.forall.2 $ fun_like.ext_iff.1 h, λ h, h ▸ rfl⟩ @[to_additive] lemma cancel_right_on {g₁ g₂ : B →*[n] γ} {f : A →*[n] β} (hf : A.surj_on f B) {hf'} : A.eq_on (g₁.comp f hf') (g₂.comp f hf') ↔ B.eq_on g₁ g₂ := hf.cancel_right hf' @[to_additive] lemma cancel_left_on {g : B →*[n] γ} {f₁ f₂ : A →*[n] β} (hg : B.inj_on g) {hf₁ hf₂} : A.eq_on (g.comp f₁ hf₁) (g.comp f₂ hf₂) ↔ A.eq_on f₁ f₂ := hg.cancel_left hf₁ hf₂ @[simp, to_additive] lemma comp_id (f : A →*[n] β) {hf} : f.comp (freiman_hom.id A n) hf = f := ext $ λ x, rfl @[simp, to_additive] lemma id_comp (f : A →*[n] β) {hf} : (freiman_hom.id B n).comp f hf = f := ext $ λ x, rfl /-- `freiman_hom.const A n b` is the Freiman homomorphism sending everything to `b`. -/ @[to_additive "`add_freiman_hom.const n b` is the Freiman homomorphism sending everything to `b`."] def const (A : set α) (n : ℕ) (b : β) : A →*[n] β := { to_fun := λ _, b, map_prod_eq_map_prod' := λ s t _ _ hs ht _, by rw [multiset.map_const, multiset.map_const, prod_repeat, prod_repeat, hs, ht] } @[simp, to_additive] lemma const_apply (n : ℕ) (b : β) (x : α) : const A n b x = b := rfl @[simp, to_additive] lemma const_comp (n : ℕ) (c : γ) (f : A →*[n] β) {hf} : (const B n c).comp f hf = const A n c := rfl /-- `1` is the Freiman homomorphism sending everything to `1`. -/ @[to_additive "`0` is the Freiman homomorphism sending everything to `0`."] instance : has_one (A →*[n] β) := ⟨const A n 1⟩ @[simp, to_additive] lemma one_apply (x : α) : (1 : A →*[n] β) x = 1 := rfl @[simp, to_additive] lemma one_comp (f : A →*[n] β) {hf} : (1 : B →*[n] γ).comp f hf = 1 := rfl @[to_additive] instance : inhabited (A →*[n] β) := ⟨1⟩ /-- `f * g` is the Freiman homomorphism sends `x` to `f x * g x`. -/ @[to_additive "`f + g` is the Freiman homomorphism sending `x` to `f x + g x`."] instance : has_mul (A →*[n] β) := ⟨λ f g, { to_fun := λ x, f x * g x, map_prod_eq_map_prod' := λ s t hsA htA hs ht h, by rw [prod_map_mul, prod_map_mul, map_prod_eq_map_prod f hsA htA hs ht h, map_prod_eq_map_prod g hsA htA hs ht h] }⟩ @[simp, to_additive] lemma mul_apply (f g : A →*[n] β) (x : α) : (f * g) x = f x * g x := rfl @[to_additive] lemma mul_comp (g₁ g₂ : B →*[n] γ) (f : A →*[n] β) {hg hg₁ hg₂} : (g₁ * g₂).comp f hg = g₁.comp f hg₁ * g₂.comp f hg₂ := rfl /-- If `f` is a Freiman homomorphism to a commutative group, then `f⁻¹` is the Freiman homomorphism sending `x` to `(f x)⁻¹`. -/ @[to_additive "If `f` is a Freiman homomorphism to an additive commutative group, then `-f` is the Freiman homomorphism sending `x` to `-f x`."] instance : has_inv (A →*[n] G) := ⟨λ f, { to_fun := λ x, (f x)⁻¹, map_prod_eq_map_prod' := λ s t hsA htA hs ht h, by rw [prod_map_inv, prod_map_inv, map_prod_eq_map_prod f hsA htA hs ht h] }⟩ @[simp, to_additive] lemma inv_apply (f : A →*[n] G) (x : α) : f⁻¹ x = (f x)⁻¹ := rfl @[simp, to_additive] lemma inv_comp (f : B →*[n] G) (g : A →*[n] β) {hf hf'} : f⁻¹.comp g hf = (f.comp g hf')⁻¹ := ext $ λ x, rfl /-- If `f` and `g` are Freiman homomorphisms to a commutative group, then `f / g` is the Freiman homomorphism sending `x` to `f x / g x`. -/ @[to_additive "If `f` and `g` are additive Freiman homomorphisms to an additive commutative group, then `f - g` is the additive Freiman homomorphism sending `x` to `f x - g x`"] instance : has_div (A →*[n] G) := ⟨λ f g, { to_fun := λ x, f x / g x, map_prod_eq_map_prod' := λ s t hsA htA hs ht h, by rw [prod_map_div, prod_map_div, map_prod_eq_map_prod f hsA htA hs ht h, map_prod_eq_map_prod g hsA htA hs ht h] }⟩ @[simp, to_additive] lemma div_apply (f g : A →*[n] G) (x : α) : (f / g) x = f x / g x := rfl @[simp, to_additive] lemma div_comp (f₁ f₂ : B →*[n] G) (g : A →*[n] β) {hf hf₁ hf₂} : (f₁ / f₂).comp g hf = f₁.comp g hf₁ / f₂.comp g hf₂ := ext $ λ x, rfl /-! ### Instances -/ /-- `A →*[n] β` is a `comm_monoid`. -/ @[to_additive "`α →+[n] β` is an `add_comm_monoid`."] instance : comm_monoid (A →*[n] β) := { mul := (*), mul_assoc := λ a b c, by { ext, apply mul_assoc }, one := 1, one_mul := λ a, by { ext, apply one_mul }, mul_one := λ a, by { ext, apply mul_one }, mul_comm := λ a b, by { ext, apply mul_comm }, npow := λ m f, { to_fun := λ x, f x ^ m, map_prod_eq_map_prod' := λ s t hsA htA hs ht h, by rw [prod_map_pow, prod_map_pow, map_prod_eq_map_prod f hsA htA hs ht h] }, npow_zero' := λ f, by { ext x, exact pow_zero _ }, npow_succ' := λ n f, by { ext x, exact pow_succ _ _ } } /-- If `β` is a commutative group, then `A →*[n] β` is a commutative group too. -/ @[to_additive "If `β` is an additive commutative group, then `A →*[n] β` is an additive commutative group too."] instance {β} [comm_group β] : comm_group (A →*[n] β) := { 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_prod_eq_map_prod' := λ s t hsA htA hs ht h, by rw [prod_map_zpow, prod_map_zpow, map_prod_eq_map_prod f hsA htA hs ht h] }, zpow_zero' := λ f, by { ext x, exact zpow_zero _ }, zpow_succ' := λ n f, by { ext x, simp_rw [zpow_of_nat, pow_succ, mul_apply, coe_mk] }, zpow_neg' := λ n f, by { ext x, simp_rw [zpow_neg_succ_of_nat, zpow_coe_nat], refl }, ..freiman_hom.comm_monoid } end freiman_hom /-! ### Hom hierarchy -/ --TODO: change to `monoid_hom_class F A β → freiman_hom_class F A β n` once `map_multiset_prod` is -- generalized /-- A monoid homomorphism is naturally a `freiman_hom` on its entire domain. We can't leave the domain `A : set α` of the `freiman_hom` a free variable, since it wouldn't be inferrable. -/ @[to_additive " An additive monoid homomorphism is naturally an `add_freiman_hom` on its entire domain. We can't leave the domain `A : set α` of the `freiman_hom` a free variable, since it wouldn't be inferrable."] instance monoid_hom.freiman_hom_class : freiman_hom_class (α →* β) set.univ β n := { map_prod_eq_map_prod' := λ f s t _ _ _ _ h, by rw [←f.map_multiset_prod, h, f.map_multiset_prod] } /-- A `monoid_hom` is naturally a `freiman_hom`. -/ @[to_additive add_monoid_hom.to_add_freiman_hom "An `add_monoid_hom` is naturally an `add_freiman_hom`"] def monoid_hom.to_freiman_hom (A : set α) (n : ℕ) (f : α →* β) : A →*[n] β := { to_fun := f, map_prod_eq_map_prod' := λ s t hsA htA, map_prod_eq_map_prod f (λ _ _, set.mem_univ _) (λ _ _, set.mem_univ _) } @[simp, to_additive] lemma monoid_hom.to_freiman_hom_coe (f : α →* β) : (f.to_freiman_hom A n : α → β) = f := rfl @[to_additive] lemma monoid_hom.to_freiman_hom_injective : function.injective (monoid_hom.to_freiman_hom A n : (α →* β) → A →*[n] β) := λ f g h, monoid_hom.ext $ show _, from fun_like.ext_iff.mp h end comm_monoid section cancel_comm_monoid variables [comm_monoid α] [cancel_comm_monoid β] {A : set α} {m n : ℕ} @[to_additive] lemma map_prod_eq_map_prod_of_le [freiman_hom_class F A β n] (f : F) {s t : multiset α} (hsA : ∀ x ∈ s, x ∈ A) (htA : ∀ x ∈ t, x ∈ A) (hs : s.card = m) (ht : t.card = m) (hst : s.prod = t.prod) (h : m ≤ n) : (s.map f).prod = (t.map f).prod := begin obtain rfl | hm := m.eq_zero_or_pos, { rw card_eq_zero at hs ht, rw [hs, ht] }, rw [←hs, card_pos_iff_exists_mem] at hm, obtain ⟨a, ha⟩ := hm, suffices : ((s + repeat a (n - m)).map f).prod = ((t + repeat a (n - m)).map f).prod, { simp_rw [multiset.map_add, prod_add] at this, exact mul_right_cancel this }, replace ha := hsA _ ha, refine map_prod_eq_map_prod f (λ x hx, _) (λ x hx, _) _ _ _, rotate 2, assumption, -- Can't infer `A` and `n` from the context, so do it manually. { rw mem_add at hx, refine hx.elim (hsA _) (λ h, _), rwa eq_of_mem_repeat h }, { rw mem_add at hx, refine hx.elim (htA _) (λ h, _), rwa eq_of_mem_repeat h }, { rw [card_add, hs, card_repeat, add_tsub_cancel_of_le h] }, { rw [card_add, ht, card_repeat, add_tsub_cancel_of_le h] }, { rw [prod_add, prod_add, hst] } end /-- `α →*[n] β` is naturally included in `A →*[m] β` for any `m ≤ n`. -/ @[to_additive add_freiman_hom.to_add_freiman_hom "`α →+[n] β` is naturally included in `α →+[m] β` for any `m ≤ n`"] def freiman_hom.to_freiman_hom (h : m ≤ n) (f : A →*[n] β) : A →*[m] β := { to_fun := f, map_prod_eq_map_prod' := λ s t hsA htA hs ht hst, map_prod_eq_map_prod_of_le f hsA htA hs ht hst h } /-- A `n`-Freiman homomorphism is also a `m`-Freiman homomorphism for any `m ≤ n`. -/ @[to_additive add_freiman_hom.add_freiman_hom_class_of_le "An additive `n`-Freiman homomorphism is also an additive `m`-Freiman homomorphism for any `m ≤ n`."] lemma freiman_hom.freiman_hom_class_of_le [freiman_hom_class F A β n] (h : m ≤ n) : freiman_hom_class F A β m := { map_prod_eq_map_prod' := λ f s t hsA htA hs ht hst, map_prod_eq_map_prod_of_le f hsA htA hs ht hst h } @[simp, to_additive add_freiman_hom.to_add_freiman_hom_coe] lemma freiman_hom.to_freiman_hom_coe (h : m ≤ n) (f : A →*[n] β) : (f.to_freiman_hom h : α → β) = f := rfl @[to_additive] lemma freiman_hom.to_freiman_hom_injective (h : m ≤ n) : function.injective (freiman_hom.to_freiman_hom h : (A →*[n] β) → A →*[m] β) := λ f g hfg, freiman_hom.ext $ by convert fun_like.ext_iff.1 hfg end cancel_comm_monoid
4e54929d93a3522f9ceca68e70a3b03b880acc6e
8c9f90127b78cbeb5bb17fd6b5db1db2ffa3cbc4
/exists_easy.lean
4cd00d6b0492bf2bc9a783d7d7da85f447404ba8
[]
no_license
picrin/lean
420f4d08bb3796b911d56d0938e4410e1da0e072
3d10c509c79704aa3a88ebfb24d08b30ce1137cc
refs/heads/master
1,611,166,610,726
1,536,671,438,000
1,536,671,438,000
60,029,899
0
0
null
null
null
null
UTF-8
Lean
false
false
331
lean
section variable U : Type variable P : U → Prop variable p : Prop variable Hforall : (∀ v : U, P v → p) variable Hexist : (∃ u : U, P u) theorem t1 : p := have H : (Π a, P a → p) → p, from exists.elim Hexist, H Hforall check exists.elim Hexist end
96834113d74e19b3f5a871b5a46b98614a39ad7e
947b78d97130d56365ae2ec264df196ce769371a
/tests/lean/extract.lean
2756d01a7556e711e90b66c2ffddaf73d9756f87
[ "Apache-2.0" ]
permissive
shyamalschandra/lean4
27044812be8698f0c79147615b1d5090b9f4b037
6e7a883b21eaf62831e8111b251dc9b18f40e604
refs/heads/master
1,671,417,126,371
1,601,859,995,000
1,601,860,020,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,557
lean
new_frontend #eval "abc" /- some "a" -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator; let it₂ := it₁.next; it₁.extract it₂ /- some "" -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator; it₁.extract it₁ /- none -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator; let it₂ := it₁.next; it₂.extract it₁ /- some "abc" -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator; let it₂ := it₁.next.next.next.prev.next; it₁.extract it₂ /- some "bcde" -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator.next; let it₂ := it₁.next.next.next.next; it₁.extract it₂ /- some "abcde" -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator; let it₂ := it₁.next.next.next.next.next; it₁.extract it₂ /- some "ab" -/ #eval let s₁ := "abcde"; let s₂ := "abcde"; let it₁ := s₁.mkIterator; let it₂ := s₂.mkIterator.next.next; it₁.extract it₂ /- none -/ #eval let s₁ := "abcde"; let s₂ := "abhde"; let it₁ := s₁.mkIterator; let it₂ := s₂.mkIterator.next.next; it₁.extract it₂ /- none -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator; let it₂ := it₁.next.setCurr 'a'; it₁.extract it₂ /- some "a" -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator; let it₂ := it₁.next.setCurr 'b'; it₁.extract it₂ /- some "a" -/ #eval let s₁ := "abcde"; let it₁ := s₁.mkIterator; let it₂ := (it₁.next.setCurr 'a').setCurr 'b'; it₁.extract it₂
484c9692fa170c6f50c174e73759fa24db5ecb84
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/explode_auto.lean
6ca1e10fb88cc8d3e26525ac1c499d3f3de023f7
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
4,156
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro, Minchao Wu -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.core import Mathlib.PostPort universes l namespace Mathlib /-! # `#explode` command Displays a proof term in a line by line format somewhat akin to a Fitch style proof or the Metamath proof style. -/ namespace tactic namespace explode inductive status where | reg : status | intro : status | lam : status | sintro : status /-- A type to distinguish introduction or elimination rules represented as strings from theorems referred to by their names. -/ /-- Turn a thm into a string. -/ end explode /-- `#explode decl_name` displays a proof term in a line-by-line format somewhat akin to a Fitch-style proof or the Metamath proof style. `#explode_widget decl_name` renders a widget that displays an `#explode` proof. `#explode iff_true_intro` produces ```lean iff_true_intro : ∀ {a : Prop}, a → (a ↔ true) 0│ │ a ├ Prop 1│ │ h ├ a 2│ │ hl │ ┌ a 3│ │ trivial │ │ true 4│2,3│ ∀I │ a → true 5│ │ hr │ ┌ true 6│5,1│ ∀I │ true → a 7│4,6│ iff.intro │ a ↔ true 8│1,7│ ∀I │ a → (a ↔ true) 9│0,8│ ∀I │ ∀ {a : Prop}, a → (a ↔ true) ``` In more detail: The output of `#explode` is a Fitch-style proof in a four-column diagram modeled after Metamath proof displays like [this](http://us.metamath.org/mpeuni/ru.html). The headers of the columns are "Step", "Hyp", "Ref", "Type" (or "Expression" in the case of Metamath): * Step: An increasing sequence of numbers to number each step in the proof, used in the Hyp field. * Hyp: The direct children of the current step. Most theorems are implications like `A -> B -> C`, and so on the step proving `C` the Hyp field will refer to the steps that prove `A` and `B`. * Ref: The name of the theorem being applied. This is well-defined in Metamath, but in Lean there are some special steps that may have long names because the structure of proof terms doesn't exactly match this mold. * If the theorem is `foo (x y : Z) : A x -> B y -> C x y`: * the Ref field will contain `foo`, * `x` and `y` will be suppressed, because term construction is not interesting, and * the Hyp field will reference steps proving `A x` and `B y`. This corresponds to a proof term like `@foo x y pA pB` where `pA` and `pB` are subproofs. * If the head of the proof term is a local constant or lambda, then in this case the Ref will say `∀E` for forall-elimination. This happens when you have for example `h : A -> B` and `ha : A` and prove `b` by `h ha`; we reinterpret this as if it said `∀E h ha` where `∀E` is (n-ary) modus ponens. * If the proof term is a lambda, we will also use `∀I` for forall-introduction, referencing the body of the lambda. The indentation level will increase, and a bracket will surround the proof of the body of the lambda, starting at a proof step labeled with the name of the lambda variable and its type, and ending with the `∀I` step. Metamath doesn't have steps like this, but the style is based on Fitch proofs in first-order logic. * Type: This contains the type of the proof term, the theorem being proven at the current step. This proof layout differs from `#print` in using lots of intermediate step displays so that you can follow along and don't have to see term construction steps because they are implicitly in the intermediate step displays. Also, it is common for a Lean theorem to begin with a sequence of lambdas introducing local constants of the theorem. In order to minimize the indentation level, the `∀I` steps at the end of the proof will be introduced in a group and the indentation will stay fixed. (The indentation brackets are only needed in order to delimit the scope of assumptions, and these assumptions have global scope anyway so detailed tracking is not necessary.) -/ end Mathlib
46f19215f2b0b94c97d40f6f151d48298718773a
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/ring_theory/derivation/basic.lean
95a1d1ed3e837d521a96bdd25b3740a18f9fe065
[ "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
13,866
lean
/- Copyright © 2020 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri, Andrew Yang -/ import ring_theory.adjoin.basic /-! # Derivations > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file defines derivation. A derivation `D` from the `R`-algebra `A` to the `A`-module `M` is an `R`-linear map that satisfy the Leibniz rule `D (a * b) = a * D b + D a * b`. ## Main results - `derivation`: The type of `R`-derivations from `A` to `M`. This has an `A`-module structure. - `derivation.llcomp`: We may compose linear maps and derivations to obtain a derivation, and the composition is bilinear. See `ring_theory.derivation.lie` for - `derivation.lie_algebra`: The `R`-derivations from `A` to `A` form an lie algebra over `R`. and `ring_theory.derivation.to_square_zero` for - `derivation_to_square_zero_equiv_lift`: The `R`-derivations from `A` into a square-zero ideal `I` of `B` corresponds to the lifts `A →ₐ[R] B` of the map `A →ₐ[R] B ⧸ I`. ## Future project - Generalize derivations into bimodules. -/ open algebra open_locale big_operators /-- `D : derivation R A M` is an `R`-linear map from `A` to `M` that satisfies the `leibniz` equality. We also require that `D 1 = 0`. See `derivation.mk'` for a constructor that deduces this assumption from the Leibniz rule when `M` is cancellative. TODO: update this when bimodules are defined. -/ @[protect_proj] structure derivation (R : Type*) (A : Type*) [comm_semiring R] [comm_semiring A] [algebra R A] (M : Type*) [add_comm_monoid M] [module A M] [module R M] extends A →ₗ[R] M := (map_one_eq_zero' : to_linear_map 1 = 0) (leibniz' (a b : A) : to_linear_map (a * b) = a • to_linear_map b + b • to_linear_map a) /-- The `linear_map` underlying a `derivation`. -/ add_decl_doc derivation.to_linear_map namespace derivation section variables {R : Type*} [comm_semiring R] variables {A : Type*} [comm_semiring A] [algebra R A] variables {M : Type*} [add_comm_monoid M] [module A M] [module R M] variables (D : derivation R A M) {D1 D2 : derivation R A M} (r : R) (a b : A) instance : add_monoid_hom_class (derivation R A M) A M := { coe := λ D, D.to_fun, coe_injective' := λ D1 D2 h, by { cases D1, cases D2, congr, exact fun_like.coe_injective h }, map_add := λ D, D.to_linear_map.map_add', map_zero := λ D, D.to_linear_map.map_zero } /-- Helper instance for when there's too many metavariables to apply `fun_like.has_coe_to_fun` directly. -/ instance : has_coe_to_fun (derivation R A M) (λ _, A → M) := ⟨λ D, D.to_linear_map.to_fun⟩ -- Not a simp lemma because it can be proved via `coe_fn_coe` + `to_linear_map_eq_coe` lemma to_fun_eq_coe : D.to_fun = ⇑D := rfl instance has_coe_to_linear_map : has_coe (derivation R A M) (A →ₗ[R] M) := ⟨λ D, D.to_linear_map⟩ @[simp] lemma to_linear_map_eq_coe : D.to_linear_map = D := rfl @[simp] lemma mk_coe (f : A →ₗ[R] M) (h₁ h₂) : ((⟨f, h₁, h₂⟩ : derivation R A M) : A → M) = f := rfl @[simp, norm_cast] lemma coe_fn_coe (f : derivation R A M) : ⇑(f : A →ₗ[R] M) = f := rfl lemma coe_injective : @function.injective (derivation R A M) (A → M) coe_fn := fun_like.coe_injective @[ext] theorem ext (H : ∀ a, D1 a = D2 a) : D1 = D2 := fun_like.ext _ _ H lemma congr_fun (h : D1 = D2) (a : A) : D1 a = D2 a := fun_like.congr_fun h a protected lemma map_add : D (a + b) = D a + D b := map_add D a b protected lemma map_zero : D 0 = 0 := map_zero D @[simp] lemma map_smul : D (r • a) = r • D a := D.to_linear_map.map_smul r a @[simp] lemma leibniz : D (a * b) = a • D b + b • D a := D.leibniz' _ _ lemma map_sum {ι : Type*} (s : finset ι) (f : ι → A) : D (∑ i in s, f i) = ∑ i in s, D (f i) := D.to_linear_map.map_sum @[simp, priority 900] lemma map_smul_of_tower {S : Type*} [has_smul S A] [has_smul S M] [linear_map.compatible_smul A M S R] (D : derivation R A M) (r : S) (a : A) : D (r • a) = r • D a := D.to_linear_map.map_smul_of_tower r a @[simp] lemma map_one_eq_zero : D 1 = 0 := D.map_one_eq_zero' @[simp] lemma map_algebra_map : D (algebra_map R A r) = 0 := by rw [←mul_one r, ring_hom.map_mul, ring_hom.map_one, ←smul_def, map_smul, map_one_eq_zero, smul_zero] @[simp] lemma map_coe_nat (n : ℕ) : D (n : A) = 0 := by rw [← nsmul_one, D.map_smul_of_tower n, map_one_eq_zero, smul_zero] @[simp] lemma leibniz_pow (n : ℕ) : D (a ^ n) = n • a ^ (n - 1) • D a := begin induction n with n ihn, { rw [pow_zero, map_one_eq_zero, zero_smul] }, { rcases (zero_le n).eq_or_lt with (rfl|hpos), { rw [pow_one, one_smul, pow_zero, one_smul] }, { have : a * a ^ (n - 1) = a ^ n, by rw [← pow_succ, nat.sub_add_cancel hpos], simp only [pow_succ, leibniz, ihn, smul_comm a n, smul_smul a, add_smul, this, nat.succ_eq_add_one, nat.add_succ_sub_one, add_zero, one_nsmul] } } end lemma eq_on_adjoin {s : set A} (h : set.eq_on D1 D2 s) : set.eq_on D1 D2 (adjoin R s) := λ x hx, algebra.adjoin_induction hx h (λ r, (D1.map_algebra_map r).trans (D2.map_algebra_map r).symm) (λ x y hx hy, by simp only [map_add, *]) (λ x y hx hy, by simp only [leibniz, *]) /-- If adjoin of a set is the whole algebra, then any two derivations equal on this set are equal on the whole algebra. -/ lemma ext_of_adjoin_eq_top (s : set A) (hs : adjoin R s = ⊤) (h : set.eq_on D1 D2 s) : D1 = D2 := ext $ λ a, eq_on_adjoin h $ hs.symm ▸ trivial /- Data typeclasses -/ instance : has_zero (derivation R A M) := ⟨{ to_linear_map := 0, map_one_eq_zero' := rfl, leibniz' := λ a b, by simp only [add_zero, linear_map.zero_apply, smul_zero] }⟩ @[simp] lemma coe_zero : ⇑(0 : derivation R A M) = 0 := rfl @[simp] lemma coe_zero_linear_map : ↑(0 : derivation R A M) = (0 : A →ₗ[R] M) := rfl lemma zero_apply (a : A) : (0 : derivation R A M) a = 0 := rfl instance : has_add (derivation R A M) := ⟨λ D1 D2, { to_linear_map := D1 + D2, map_one_eq_zero' := by simp, leibniz' := λ a b, by simp only [leibniz, linear_map.add_apply, coe_fn_coe, smul_add, add_add_add_comm] }⟩ @[simp] lemma coe_add (D1 D2 : derivation R A M) : ⇑(D1 + D2) = D1 + D2 := rfl @[simp] lemma coe_add_linear_map (D1 D2 : derivation R A M) : ↑(D1 + D2) = (D1 + D2 : A →ₗ[R] M) := rfl lemma add_apply : (D1 + D2) a = D1 a + D2 a := rfl instance : inhabited (derivation R A M) := ⟨0⟩ section scalar variables {S T : Type*} variables [monoid S] [distrib_mul_action S M] [smul_comm_class R S M] [smul_comm_class S A M] variables [monoid T] [distrib_mul_action T M] [smul_comm_class R T M] [smul_comm_class T A M] @[priority 100] instance : has_smul S (derivation R A M) := ⟨λ r D, { to_linear_map := r • D, map_one_eq_zero' := by rw [linear_map.smul_apply, coe_fn_coe, D.map_one_eq_zero, smul_zero], leibniz' := λ a b, by simp only [linear_map.smul_apply, coe_fn_coe, leibniz, smul_add, smul_comm r] }⟩ @[simp] lemma coe_smul (r : S) (D : derivation R A M) : ⇑(r • D) = r • D := rfl @[simp] lemma coe_smul_linear_map (r : S) (D : derivation R A M) : ↑(r • D) = (r • D : A →ₗ[R] M) := rfl lemma smul_apply (r : S) (D : derivation R A M) : (r • D) a = r • D a := rfl instance : add_comm_monoid (derivation R A M) := coe_injective.add_comm_monoid _ coe_zero coe_add (λ _ _, rfl) /-- `coe_fn` as an `add_monoid_hom`. -/ def coe_fn_add_monoid_hom : derivation R A M →+ (A → M) := { to_fun := coe_fn, map_zero' := coe_zero, map_add' := coe_add } @[priority 100] instance : distrib_mul_action S (derivation R A M) := function.injective.distrib_mul_action coe_fn_add_monoid_hom coe_injective coe_smul instance [distrib_mul_action Sᵐᵒᵖ M] [is_central_scalar S M] : is_central_scalar S (derivation R A M) := { op_smul_eq_smul := λ _ _, ext $ λ _, op_smul_eq_smul _ _} instance [has_smul S T] [is_scalar_tower S T M] : is_scalar_tower S T (derivation R A M) := ⟨λ x y z, ext $ λ a, smul_assoc _ _ _⟩ instance [smul_comm_class S T M] : smul_comm_class S T (derivation R A M) := ⟨λ x y z, ext $ λ a, smul_comm _ _ _⟩ end scalar @[priority 100] instance {S : Type*} [semiring S] [module S M] [smul_comm_class R S M] [smul_comm_class S A M] : module S (derivation R A M) := function.injective.module S coe_fn_add_monoid_hom coe_injective coe_smul section push_forward variables {N : Type*} [add_comm_monoid N] [module A N] [module R N] [is_scalar_tower R A M] [is_scalar_tower R A N] variables (f : M →ₗ[A] N) (e : M ≃ₗ[A] N) /-- We can push forward derivations using linear maps, i.e., the composition of a derivation with a linear map is a derivation. Furthermore, this operation is linear on the spaces of derivations. -/ def _root_.linear_map.comp_der : derivation R A M →ₗ[R] derivation R A N := { to_fun := λ D, { to_linear_map := (f : M →ₗ[R] N).comp (D : A →ₗ[R] M), map_one_eq_zero' := by simp only [linear_map.comp_apply, coe_fn_coe, map_one_eq_zero, map_zero], leibniz' := λ a b, by simp only [coe_fn_coe, linear_map.comp_apply, linear_map.map_add, leibniz, linear_map.coe_coe_is_scalar_tower, linear_map.map_smul] }, map_add' := λ D₁ D₂, by { ext, exact linear_map.map_add _ _ _, }, map_smul' := λ r D, by { ext, exact linear_map.map_smul _ _ _, }, } @[simp] lemma coe_to_linear_map_comp : (f.comp_der D : A →ₗ[R] N) = (f : M →ₗ[R] N).comp (D : A →ₗ[R] M) := rfl @[simp] lemma coe_comp : (f.comp_der D : A → N) = (f : M →ₗ[R] N).comp (D : A →ₗ[R] M) := rfl /-- The composition of a derivation with a linear map as a bilinear map -/ @[simps] def llcomp : (M →ₗ[A] N) →ₗ[A] derivation R A M →ₗ[R] derivation R A N := { to_fun := λ f, f.comp_der, map_add' := λ f₁ f₂, by { ext, refl }, map_smul' := λ r D, by { ext, refl } } /-- Pushing a derivation foward through a linear equivalence is an equivalence. -/ def _root_.linear_equiv.comp_der : derivation R A M ≃ₗ[R] derivation R A N := { inv_fun := e.symm.to_linear_map.comp_der, left_inv := λ D, by { ext a, exact e.symm_apply_apply (D a) }, right_inv := λ D, by { ext a, exact e.apply_symm_apply (D a) }, ..e.to_linear_map.comp_der } end push_forward section restrict_scalars variables {S : Type*} [comm_semiring S] variables [algebra S A] [module S M] [linear_map.compatible_smul A M R S] variables (R) /-- If `A` is both an `R`-algebra and an `S`-algebra; `M` is both an `R`-module and an `S`-module, then an `S`-derivation `A → M` is also an `R`-derivation if it is also `R`-linear. -/ protected def restrict_scalars (d : derivation S A M) : derivation R A M := { map_one_eq_zero' := d.map_one_eq_zero, leibniz' := d.leibniz, to_linear_map := d.to_linear_map.restrict_scalars R } end restrict_scalars end section cancel variables {R : Type*} [comm_semiring R] {A : Type*} [comm_semiring A] [algebra R A] {M : Type*} [add_cancel_comm_monoid M] [module R M] [module A M] /-- Define `derivation R A M` from a linear map when `M` is cancellative by verifying the Leibniz rule. -/ def mk' (D : A →ₗ[R] M) (h : ∀ a b, D (a * b) = a • D b + b • D a) : derivation R A M := { to_linear_map := D, map_one_eq_zero' := add_right_eq_self.1 $ by simpa only [one_smul, one_mul] using (h 1 1).symm, leibniz' := h } @[simp] lemma coe_mk' (D : A →ₗ[R] M) (h) : ⇑(mk' D h) = D := rfl @[simp] lemma coe_mk'_linear_map (D : A →ₗ[R] M) (h) : (mk' D h : A →ₗ[R] M) = D := rfl end cancel section variables {R : Type*} [comm_ring R] variables {A : Type*} [comm_ring A] [algebra R A] section variables {M : Type*} [add_comm_group M] [module A M] [module R M] variables (D : derivation R A M) {D1 D2 : derivation R A M} (r : R) (a b : A) protected lemma map_neg : D (-a) = -D a := map_neg D a protected lemma map_sub : D (a - b) = D a - D b := map_sub D a b @[simp] lemma map_coe_int (n : ℤ) : D (n : A) = 0 := by rw [← zsmul_one, D.map_smul_of_tower n, map_one_eq_zero, smul_zero] lemma leibniz_of_mul_eq_one {a b : A} (h : a * b = 1) : D a = -a^2 • D b := begin rw neg_smul, refine eq_neg_of_add_eq_zero_left _, calc D a + a ^ 2 • D b = a • b • D a + a • a • D b : by simp only [smul_smul, h, one_smul, sq] ... = a • D (a * b) : by rw [leibniz, smul_add, add_comm] ... = 0 : by rw [h, map_one_eq_zero, smul_zero] end lemma leibniz_inv_of [invertible a] : D (⅟a) = -⅟a^2 • D a := D.leibniz_of_mul_eq_one $ inv_of_mul_self a lemma leibniz_inv {K : Type*} [field K] [module K M] [algebra R K] (D : derivation R K M) (a : K) : D (a⁻¹) = -a⁻¹ ^ 2 • D a := begin rcases eq_or_ne a 0 with (rfl|ha), { simp }, { exact D.leibniz_of_mul_eq_one (inv_mul_cancel ha) } end instance : has_neg (derivation R A M) := ⟨λ D, mk' (-D) $ λ a b, by simp only [linear_map.neg_apply, smul_neg, neg_add_rev, leibniz, coe_fn_coe, add_comm]⟩ @[simp] lemma coe_neg (D : derivation R A M) : ⇑(-D) = -D := rfl @[simp] lemma coe_neg_linear_map (D : derivation R A M) : ↑(-D) = (-D : A →ₗ[R] M) := rfl lemma neg_apply : (-D) a = -D a := rfl instance : has_sub (derivation R A M) := ⟨λ D1 D2, mk' (D1 - D2 : A →ₗ[R] M) $ λ a b, by simp only [linear_map.sub_apply, leibniz, coe_fn_coe, smul_sub, add_sub_add_comm]⟩ @[simp] lemma coe_sub (D1 D2 : derivation R A M) : ⇑(D1 - D2) = D1 - D2 := rfl @[simp] lemma coe_sub_linear_map (D1 D2 : derivation R A M) : ↑(D1 - D2) = (D1 - D2 : A →ₗ[R] M) := rfl lemma sub_apply : (D1 - D2) a = D1 a - D2 a := rfl instance : add_comm_group (derivation R A M) := coe_injective.add_comm_group _ coe_zero coe_add coe_neg coe_sub (λ _ _, rfl) (λ _ _, rfl) end end end derivation
1ce0380ba983c64119344cec74ce85cb7379992b
1789ef53372ad44b5ce5db2341556f91c8c31395
/src/mathlib_tutorial.lean
ee64d813524b75fe980d476b48818f23b21bc303
[]
no_license
iceplant/Mermin_Peres
d0a0f4c9f27111b3fee4e0ab5119ef1eafc59dc2
d7ea59b5767157420b0fae9dfc09f22ad2826564
refs/heads/master
1,609,583,206,767
1,582,220,678,000
1,582,220,678,000
239,577,114
1
1
null
null
null
null
UTF-8
Lean
false
false
17,937
lean
/- This file is intended for Lean beginners. The goal is to demonstrate what it feels like to prove things using Lean and mathlib. Complicated definitions and theory building are not covered. -/ -- We want real numbers and their basic properties import data.real.basic -- We want to be able to define functions using the law of excluded middle noncomputable theory open_locale classical /- Our first goal is to define the set of upper bounds of a set of real numbers. This is already defined in mathlib (in a more general context), but we repeat it for the sake of exposition. Right-click "upper_bounds" below to get offered to jump to mathlib's version -/ #check upper_bounds /-- The set of upper bounds of a set of real numbers ℝ -/ def up_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, a ≤ x} /-- Predicate `is_max a A` means `a` is a maximum of `A` -/ def is_max (a : ℝ) (A : set ℝ) := a ∈ A ∧ a ∈ up_bounds A /- In the above definition, the symbol `∧` means "and". We also see the most visible difference between set theoretic foundations and type theoretic ones (used by almost all proof assistants). In set theory, everything is a set, and the only relation you get from foundations are `=` and `∈`. In type theory, there is a meta-theoretic relation of "typing": `a : ℝ` reads "`a` is a real number" or, more precisely, "the type of `a` is `ℝ`". Here "meta-theoretic" means this is not a statement you can prove or disprove inside the theory, it's a fact that is true or not. Here we impose this fact, in other circumstances, it would be checked by the Lean kernel. By contrast, `a ∈ A` is a statement inside the theory. Here it's part of the definition, in other circumstances it could be something proven inside Lean. -/ /- For illustrative purposes, we now define an infix version of the above predicate. It will allow us to write `a is_a_max_of A`, which is closer to a sentence. -/ infix `is_a_max_of`:55 := is_max /- Let's prove something now! A set of real number has at most one maximum. Here everything left of the final `:` is introducing the objects and assumption. The equality `x = y` right of the colon is the conclusion. -/ lemma unique_max (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin -- We first break our assumptions in their two constituent pieces. -- We are free to choose the name following `with` cases hx with x_in x_up, cases hy with y_in y_up, -- Assumption `x_up` means x isn't less than elements of A, let's apply this to y specialize x_up y, -- Assumption `x_up` now needs the information that `y` is indeed in `A`. specialize x_up y_in, -- Let's do this quicker with roles swapped specialize y_up x x_in, -- We explained to Lean the idea of this proof. -- Now we know `x ≤ y` and `y ≤ x`, and Lean shouldn't need more help. -- `linarith` proves equalities and inequalities that follow linearly from -- assumption we have. linarith, end /- The above proof is too long, even if you remove comments. We don't really need the unpacking steps at the beginning, we can access both parts of the assumption `hx : x is_a_max_of A` using shortcuts `h.1` and `h.2`. We can also improve readability without assistance from the tactic state display, clearly announcing intermediate goals using `have`. This way we get to the following version of the same proof. -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := begin have : x ≤ y, from hy.2 x hx.1, have : y ≤ x, from hx.2 y hy.1, linarith, end /- Notice how mathematics based on type theory treats the assumption `∀ a ∈ A, a ≤ y` as a function turning an element `a` of `A` into the statement `a ≤ y`. More precisely, this assumption is the abbreviation of `∀ a : ℝ, a ∈ A → a ≤ y`. The expression `hy.2 x` appearing in the above proof is then the statement `x ∈ A → x ≤ y`, which itself is a function turning a statement `x ∈ A` into `x ≤ y` so that the full expression `hy.2 x hx.1` is indeed a proof of `x ≤ y`. One could argue a three line long proof of this lemma is still two lines too long. This is debatable, but mathlib's style is to write very short proofs for trivial lemmas. Those proofs are not easy to read but they are meant to indicate that the proof is probably not worth reading. In order to reach this stage, we need to know what linarith did for us. It invoked the lemma `le_antisymm` which says: `x ≤ y → y ≤ x → x = y`. This arrow, which is used both for function and implication, is right associative. So the statement is `x ≤ y → (y ≤ x → x = y)` which reads: I will send a proof `p` of `x ≤ y` to a function sending a proof `p'` of `y ≤ x` to a proof of `x = y`. Hence `le_antisymm p p'` is a proof of `x = y`. Using this we can get our one-line proof: -/ example (A : set ℝ) (x y : ℝ) (hx : x is_a_max_of A) (hy : y is_a_max_of A) : x = y := le_antisymm (hy.2 x hx.1) (hx.2 y hy.1) /- Such a proof is called a proof term (or a "term mode" proof). Notice it has no `begin` and `end`. It is directly the kind of low level proof that the Lean kernel is consuming. Commands like `cases`, `specialize` or `linarith` are called tactics, they help users constructing proof terms that could be very tedious to write directly. The most efficient proof style combines tactics with proof terms like our previous `have : x ≤ y, from hy.2 x hx.1` where `hy.2 x hx.1` is a proof term embeded inside a tactic mode proof. In the remaining of this file, we'll be characterizing infima of sets of real numbers in term of sequences. -/ /-- The set of lower bounds of a set of real numbers ℝ -/ def low_bounds (A : set ℝ) := { x : ℝ | ∀ a ∈ A, x ≤ a} /- We now define `a` is an infimum of `A`. Again there is already a more general version in mathlib. -/ def is_inf (x : ℝ) (A : set ℝ) := x is_a_max_of (low_bounds A) infix `is_an_inf_of`:55 := is_inf /- We need to prove that any number which is greater than the infimum of A is greater than some element of A. -/ lemma inf_lt {A : set ℝ} {x : ℝ} (hx : x is_an_inf_of A) : ∀ y, x < y → ∃ a ∈ A, a < y := begin -- Let `y` be any real number. intro y, -- Let's prove the contrapositive contrapose, -- The symbol `¬` means negation. Let's ask Lean to rewrite the goal without negation, -- pushing negation through quantifiers and inequalities push_neg, -- Let's assume the premise, calling the assumption `h` intro h, -- `h` is exactly saying `y` is a lower bound of `A` so the second part of -- the infimum assumption `hx` applied to `y` and `h` is exactly what we want. exact hx.2 y h end /- In the above proof, the sequence `contrapose, push_neg` is so common it can be abbreviated to `contrapose!`. With these commands, we enter the gray zone between proof checking and proof finding. Practical computer proof checking crucially needs the computer to handle tedious proof steps. In the next proof, we'll start using `linarith` a bit more seriously, going one step further into automation. Our next real goal is to prove inequalities for limits of sequences. We extract the following lemma: if `y ≤ x + ε` for all positive `ε` then `y ≤ x`. -/ lemma le_of_le_add_eps {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin -- Let's prove the contrapositive, asking Lean to push negations right away. contrapose!, -- Assume `h : x < y`. intro h, -- We need to find `ε` such that `ε` is positive and `x + ε < y`. -- Let's use `(y-x)/2` use ((y-x)/2), -- we now have two properties to prove. Let's do both in turn, using `linarith` split, linarith, linarith, end /- Note how `linarith` was used for both sub-goals at the end of the above proof. We could have shortened that using the semi-colon combinator instead of comma, writing `split ; linarith`. Next we will study a compressed version of that proof: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩, end /- The angle brackets `⟨` and `⟩` introduce compound data or proofs. A proof of a `∃ z, P z` statemement is composed of a witness `z₀` and a proof `z` of `P z₀`. The compound is denoted by `⟨z₀, h⟩`. In the example above, the predicate is itself compound, it is a conjunction `P z ∧ Q z`. So the proof term should read `⟨z₀, ⟨h₁, h₂⟩⟩` where `h₁` (resp. `h₂`) is a proof of `P z₀` (resp. `Q z₀`). But these so-called "anonymous constructor" brackets are right-associative, so we can get rid of the nested brackets. The keyword `by` introduces tactic mode inside term mode, it is a shorter version of the `begin`/`end` pair, which is more convenient for single tactic blocks. In this example, `begin` enters tactic mode, `exact` leaves it, `by` re-enters it. Going all the way to a proof term would make the proof much longer, because we crucially use automation with `contrapose!` and `linarith`. We can still get a one-line proof using curly braces to gather several tactic invocation, and the `by` abbreviation instead of `begin`/`end`: -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := by { contrapose!, exact assume h, ⟨(y-x)/2, by linarith, by linarith⟩ } /- One could argue that the above proof is a bit too terse, and we are relying too much on linarith. Let's have more `linarith` calls for smaller steps. For the sake of (tiny) variation, we will also assume the premise and argue by contradiction instead of contraposing. -/ example {x y : ℝ} : (∀ ε > 0, y ≤ x + ε) → y ≤ x := begin intro h, -- Assume the conclusion is false, and call this assumption H. by_contradiction H, push_neg at H, -- Now let's compute. have key := calc -- Each line must end with a colon followed by a proof term -- We want to specialize our assumption `h` to `ε = (y-x)/2` but this is long to -- type, so let's put a hole `_` that Lean will fill in by comparing the -- statement we want to prove and our proof term with a hole. As usual, -- positivity of `(y-x)/2` is proved by `linarith` y ≤ x + (y-x)/2 : h _ (by linarith) ... = x/2 + y/2 : by linarith ... < y : by linarith, -- our key now says `y < y` (notice how the sequence `≤`, `=`, `<` was correctly -- merged into a `<`). Let `linarith` find the desired contradiction now. linarith, -- alternatively, we could have provided the proof term -- `exact lt_irrefl y key` end /- Now we are ready for some analysis. Let's setup notation for absolute value -/ local notation `|`x`|` := abs x /- And let's define convergence of sequences of real numbers (of course there is a much more general definition in mathlib). -/ /-- The sequence `u` tends to `l` -/ def limit (u : ℕ → ℝ) (l : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |u n - l| ≤ ε /- In the above definition, `u n` denotes the n-th term of the sequence. We can add parentheses to get `u(n)` but we try to avoid parentheses because they pile up very quickly -/ -- If y ≤ u n for all n and u n goes to x then y ≤ x lemma le_lim {x y : ℝ} {u : ℕ → ℝ} (hu : limit u x) (ineq : ∀ n, y ≤ u n) : y ≤ x := begin -- Let's apply our previous lemma apply le_of_le_add_eps, -- We need to prove y ≤ x + ε for all positive ε. -- Let ε be any positive real intros ε ε_pos, -- we now specialize our limit assumption to this `ε`, and immediately -- fix a `N` as promised by the definition. cases hu ε ε_pos with N HN, -- Now we only need to compute until reaching the conclusion calc y ≤ u N : ineq N ... = x + (u N - x) : by linarith -- We'll need `add_le_add` which says `a ≤ b` and `c ≤ d` implies `a + c ≤ b + d` -- We need a lemma saying `z ≤ |z|`. Because we don't know the name of this lemma, -- let's use `library_search`. Because searching thourgh the library is slow, -- Lean will write what it found in the Lean message window when cursor is on -- that line, so that we can replace it by the lemma. We see `le_max_left` which -- says `a ≤ max a b`. Actually there is a more specific lemma `le_abs_self` ... ≤ x + |u N - x| : add_le_add (by linarith) (by library_search) ... ≤ x + ε : add_le_add (by linarith) (HN N (by linarith)), end /- The next lemma has been extracted from the main proof in order to discuss numbers. In ordinary maths, we know that ℕ is *not* contained in `ℝ`, whatever the construction of real numbers that we use. For instance a natural number is not an equivalence class of Cauchy sequences. But it's very easy to pretend otherwise. Formal maths requires slightly more care. In the statement below, the "type ascription" `(n + 1 : ℝ)` forces Lean to convert the natural number `n+1` into a real number. The "inclusion" map will be displayed in tactic state as `↑`. There are various lemmas asserting this map is compatible with addition and monotone, but we don't want to bother writing their names. The `norm_cast` tactic is designed to wisely apply those lemmas for us. -/ lemma inv_succ_pos : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := begin -- Let `n` be any integer intro n, -- Since we don't know the name of the relevant lemma, asserting that the inverse of -- a positive number is positive, let's state that is suffices -- to prove that `n+1`, seen as a real number, is positive, and ask `library_search` suffices : (n + 1 : ℝ) > 0, { library_search }, -- Now we want to reduce to a statement about natural numbers, not real numbers -- coming from natural numbers. norm_cast, -- and then get the usual help from `linarith` linarith, end /- That was a pretty long proof for an obvious fact. And stating it as a lemma feels stupid, so let's find a way to write it on one line in case we want to include it in some other proof without stating a lemma. First the `library_search` call above displays the name of the relevant lemma: `one_div_pos_of_pos`. We can also replace the `linarith` call on the last line by `library_search` to learn the name of the lemma `nat.succ_pos` asserting that the successor of a natural number is positive. There is also a variant on `norm_cast` that combines it with `exact`. The term mode analogue of `intro` is `λ`. We get down to: -/ example : ∀ n : ℕ, 1/(n+1 : ℝ) > 0 := λ n, one_div_pos_of_pos (by exact_mod_cast nat.succ_pos n) /- The next proof uses mostly known things, so we will commment only new aspects. -/ lemma limit_inv_succ : ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, 1/(n + 1 : ℝ) ≤ ε := begin intros ε ε_pos, suffices : ∃ N : ℕ, 1/ε ≤ N, { -- Because we didn't provide a name for the above statement, Lean called it `this`. -- Let's fix an `N` that works. cases this with N HN, use N, intros n Hn, -- Now we want to rewrite the goal using lemmas -- `div_le_iff' : 0 < b → (a / b ≤ c ↔ a ≤ b * c)` -- `div_le_iff : 0 < b → (a / b ≤ c ↔ a ≤ c * b)` -- the second one will be rewritten from right to left, as indicated by `←`. -- Lean will create a side goal for the required positivity assumption that -- we don't provide for `div_le_iff'`. rw [div_le_iff', ← div_le_iff ε_pos], -- We want to replace assumption `Hn` by its real counter-part so that -- linarith can find what it needs. replace Hn : (N : ℝ) ≤ n, exact_mod_cast Hn, linarith, -- we are still left with the positivity assumption, but already discussed -- how to prove it in the precedining lemma exact_mod_cast nat.succ_pos n }, -- Now we need to prove that sufficient statement. -- We want to use that `ℝ` is archimedean. So we start typing -- `exact archimedean_` and hit Ctrl-space to see what completion Lean proposes -- the lemma `archimedean_iff_nat_le` sounds promising. We select the left to -- right implication using `.1`. This a generic lemma for fields equiped with -- a linear (ie total) order. We need to provide a proof that `ℝ` is indeed -- archimedean. This is done using the `apply_instance` tactic that will be -- covered elsewhere. exact archimedean_iff_nat_le.1 (by apply_instance) (1/ε), end /- We can now put all pieces together, with almost no new things to explain. -/ lemma inf_seq (A : set ℝ) (x : ℝ) : (x is_an_inf_of A) ↔ (x ∈ low_bounds A ∧ ∃ u : ℕ → ℝ, limit u x ∧ ∀ n, u n ∈ A ) := begin split, { intro h, split, { exact h.1 }, -- On the next line, we don't need to tell Lean to treat `n+1` as a real number because -- we add `x` to it, so Lean knows there is only one way to make sense of this expression. have key : ∀ n : ℕ, ∃ a ∈ A, a < x + 1/(n+1), { intro n, -- we can use the lemma we proved above apply inf_lt h, -- and another one we proved! have : 0 < 1/(n+1 : ℝ), from inv_succ_pos n, linarith }, -- Now we need to use axiom of (countable) choice choose u hu using key, use u, split, { intros ε ε_pos, -- again we use a lemma we proved, specializing it to our fixed `ε`, and fixing a `N` cases limit_inv_succ ε ε_pos with N H, use N, intros n hn, have : x ≤ u n, from h.1 _ (hu n).1, have := calc u n < x + 1/(n + 1) : (hu n).2 ... ≤ x + ε : add_le_add (le_refl x) (H n hn), rw abs_of_nonneg ; linarith }, { intro n, exact (hu n).1 } }, { intro h, -- Assumption `h` is made of nested compound statements. We can use the -- recursive version of `cases` to unpack it in one go. rcases h with ⟨x_min, u, lim, huA⟩, split, exact x_min, intros y y_mino, apply le_lim lim, intro n, exact y_mino (u n) (huA n) }, end
c9b61acfcf6e07bae0b321fcc12329a4560f0304
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/tactic/wlog.lean
830906f2de4b21e07cb78431a59a68457491a0d9
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
9,406
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 Without loss of generality tactic. -/ import data.list.perm open expr tactic lean lean.parser local postfix `?`:9001 := optional local postfix *:9001 := many namespace tactic private meta def update_pp_name : expr → name → expr | (local_const n _ bi d) pp := local_const n pp bi d | e n := e private meta def elim_or : ℕ → expr → tactic (list expr) | 0 h := fail "zero cases" | 1 h := return [h] | (n + 1) h := do [(_, [hl], []), (_, [hr], [])] ← induction h, -- there should be no dependent terms [gl, gr] ← get_goals, set_goals [gr], hsr ← elim_or n hr, gsr ← get_goals, set_goals (gl :: gsr), return (hl :: hsr) private meta def dest_or : expr → tactic (list expr) | e := do `(%%a ∨ %%b) ← whnf e | return [e], lb ← dest_or b, return (a :: lb) private meta def match_perms (pat : pattern) : expr → tactic (list $ list expr) | t := (do m ← match_pattern pat t, guard (m.2.all expr.is_local_constant), return [m.2]) <|> (do `(%%l ∨ %%r) ← whnf t, m ← match_pattern pat l, rs ← match_perms r, return (m.2 :: rs)) private meta def update_type : expr → expr → expr | (local_const n pp bi d) t := local_const n pp bi t | e t := e private meta def intron' : ℕ → tactic (list expr) | 0 := return [] | (i + 1) := do n ← intro1, ls ← intron' i, return (n :: ls) meta def wlog (vars' : list expr) (h_cases fst_case : expr) (perms : list (list expr)) : tactic unit := do guard h_cases.is_local_constant, -- reorder s.t. context is Γ ⬝ vars ⬝ cases ⊢ ∀deps, … nr ← revert_lst (vars' ++ [h_cases]), vars ← intron' vars'.length, h_cases ← intro h_cases.local_pp_name, cases ← infer_type h_cases, h_fst_case ← mk_local_def h_cases.local_pp_name (fst_case.instantiate_locals $ (vars'.zip vars).map $ λ⟨o, n⟩, (o.local_uniq_name, n)), ((), pr) ← solve_aux cases (repeat $ exact h_fst_case <|> left >> skip), t ← target, fixed_vars ← vars.mmap (λv, do t ← infer_type v, return (update_type v t) ), let t' := (instantiate_local h_cases.local_uniq_name pr t).pis (fixed_vars ++ [h_fst_case]), (h, [g]) ← local_proof `this t' (do clear h_cases, vars.mmap clear, intron nr), h₀ :: hs ← elim_or perms.length h_cases, solve1 (do exact (h.mk_app $ vars ++ [h₀])), focus ((hs.zip perms.tail).map $ λ⟨h_case, perm⟩, do let p_v := (vars'.zip vars).map (λ⟨p, v⟩, (p.local_uniq_name, v)), let p := perm.map (λp, p.instantiate_locals p_v), note `this none (h.mk_app $ p ++ [h_case]), clear h, return ()), gs ← get_goals, set_goals (g :: gs) namespace interactive open interactive interactive.types expr private meta def parse_permutations : option (list (list name)) → tactic (list (list expr)) | none := return [] | (some []) := return [] | (some perms@(p₀ :: ps)) := do (guard p₀.nodup <|> fail "No permutation `xs_i` in `using [xs_1, …, xs_n]` should contain the same variable twice."), (guard (perms.all $ λp, p.perm p₀) <|> fail "The permutations `xs_i` in `using [xs_1, …, xs_n]` must be permutations of the same variables."), perms.mmap (λp, p.mmap get_local) /-- Without loss of generality: reduces to one goal under variables permutations. Given a goal of the form `g xs`, a predicate `p` over a set of variables, as well as variable permutations `xs_i`. Then `wlog` produces goals of the form The case goal, i.e. the permutation `xs_i` covers all possible cases: `⊢ p xs_0 ∨ ⋯ ∨ p xs_n` The main goal, i.e. the goal reduced to `xs_0`: `(h : p xs_0) ⊢ g xs_0` The invariant goals, i.e. `g` is invariant under `xs_i`: `(h : p xs_i) (this : g xs_0) ⊢ gs xs_i` Either the permutation is provided, or a proof of the disjunction is provided to compute the permutation. The disjunction need to be in assoc normal form, e.g. `p₀ ∨ (p₁ ∨ p₂)`. In many cases the invariant goals can be solved by AC rewriting using `cc` etc. Example: On a state `(n m : ℕ) ⊢ p n m` the tactic `wlog h : n ≤ m using [n m, m n]` produces the following states: `(n m : ℕ) ⊢ n ≤ m ∨ m ≤ n` `(n m : ℕ) (h : n ≤ m) ⊢ p n m` `(n m : ℕ) (h : m ≤ n) (this : p n m) ⊢ p m n` `wlog` supports different calling conventions. The name `h` is used to give a name to the introduced case hypothesis. If the name is avoided, the default will be `case`. (1) `wlog : p xs0 using [xs0, …, xsn]` Results in the case goal `p xs0 ∨ ⋯ ∨ ps xsn`, the main goal `(case : p xs0) ⊢ g xs0` and the invariance goals `(case : p xsi) (this : g xs0) ⊢ g xsi`. (2) `wlog : p xs0 := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the variable permutations. (3) `wlog := r using xs0` The expression `r` is a proof of the shape `p xs0 ∨ ⋯ ∨ p xsi`, it is also used to compute the variable permutations. This is not as stable as (2), for example `p` cannot be a disjunction. (4) `wlog : R x y using x y` and `wlog : R x y` Produces the case `R x y ∨ R y x`. If `R` is ≤, then the disjunction discharged using linearity. If `using x y` is avoided then `x` and `y` are the last two variables appearing in the expression `R x y`. -/ meta def wlog (h : parse ident?) (pat : parse (tk ":" *> texpr)?) (cases : parse (tk ":=" *> texpr)?) (perms : parse (tk "using" *> (list_of (ident*) <|> (λx, [x]) <$> ident*))?) (discharger : tactic unit := (tactic.solve_by_elim <|> tactic.tautology tt <|> using_smt (smt_tactic.intros >> smt_tactic.solve_goals))) : tactic unit := do perms ← parse_permutations perms, (pat, cases_pr, cases_goal, vars, perms) ← (match cases with | some r := do vars::_ ← return perms | fail "At least one set of variables expected, i.e. `using x y` or `using [x y, y x]`.", cases_pr ← to_expr r, cases_pr ← (if cases_pr.is_local_constant then return $ match h with some n := update_pp_name cases_pr n | none := cases_pr end else do note (h.get_or_else `case) none cases_pr), cases ← infer_type cases_pr, (pat, perms') ← match pat with | some pat := do pat ← to_expr pat, let vars' := vars.filter $ λv, v.occurs pat, case_pat ← mk_pattern [] vars' pat [] vars', perms' ← match_perms case_pat cases, return (pat, perms') | none := do (p :: ps) ← dest_or cases, let vars' := vars.filter $ λv, v.occurs p, case_pat ← mk_pattern [] vars' p [] vars', perms' ← (p :: ps).mmap (λp, do m ← match_pattern case_pat p, return m.2), return (p, perms') end, let vars_name := vars.map local_uniq_name, guard (perms'.all $ λp, p.all $ λv, v.is_local_constant ∧ v.local_uniq_name ∈ vars_name) <|> fail "Cases contains variables not declared in `using x y z`", perms ← (if perms.length = 1 then do return (perms'.map $ λp, p ++ vars.filter (λv, p.all (λv', v'.local_uniq_name ≠ v.local_uniq_name))) else do guard (perms.length = perms'.length) <|> fail "The provided permutation list has a different length then the provided cases.", return perms), return (pat, cases_pr, @none expr, vars, perms) | none := do let name_h := h.get_or_else `case, some pat ← return pat | fail "Either specify cases or a pattern with permutations", pat ← to_expr pat, (do [x, y] ← match perms with | [] := return pat.list_local_consts | [l] := return l | _ := failed end, let cases := mk_or_lst [pat, pat.instantiate_locals [(x.local_uniq_name, y), (y.local_uniq_name, x)]], (do `(%%x' ≤ %%y') ← return pat, (cases_pr, []) ← local_proof name_h cases (exact ``(le_total %%x' %%y')), return (pat, cases_pr, none, [x, y], [[x, y], [y, x]])) <|> (do (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, [x, y], [[x, y], [y, x]]))) <|> (do guard (perms.length ≥ 2) <|> fail ("To generate cases at least two permutations are required, i.e. `using [x y, y x]`" ++ " or exactly 0 or 2 variables"), (vars :: perms') ← return perms, let names := vars.map local_uniq_name, let cases := mk_or_lst (pat :: perms'.map (λp, pat.instantiate_locals (names.zip p))), (cases_pr, [g]) ← local_proof name_h cases skip, return (pat, cases_pr, some g, vars, perms)) end), let name_fn := (if perms.length = 2 then λi, `invariant else λi, mk_simple_name ("invariant_" ++ to_string (i + 1))), with_enable_tags $ tactic.focus1 $ do t ← get_main_tag, tactic.wlog vars cases_pr pat perms, tactic.focus (set_main_tag (mk_num_name `_case 0 :: `main :: t) :: (list.range (perms.length - 1)).map (λi, do set_main_tag (mk_num_name `_case 0 :: name_fn i :: t), try discharger)), match cases_goal with | some g := do set_tag g (mk_num_name `_case 0 :: `cases :: t), gs ← get_goals, set_goals (g :: gs) | none := skip end add_tactic_doc { name := "wlog", category := doc_category.tactic, decl_names := [``wlog], tags := ["logic"] } end interactive end tactic
99dd6228506ff6851615d69d7aef00f1d924b329
26bff4ed296b8373c92b6b025f5d60cdf02104b9
/hott/types/sigma.hlean
6bae143891d6c0d598a81d8c997a1e0c2713afbd
[ "Apache-2.0" ]
permissive
guiquanz/lean
b8a878ea24f237b84b0e6f6be2f300e8bf028229
242f8ba0486860e53e257c443e965a82ee342db3
refs/heads/master
1,526,680,092,098
1,427,492,833,000
1,427,493,281,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
16,762
hlean
/- Copyright (c) 2014 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Module: types.sigma Author: Floris van Doorn Ported from Coq HoTT Theorems about sigma-types (dependent sums) -/ import types.prod open eq sigma sigma.ops equiv is_equiv namespace sigma local infixr ∘ := function.compose --remove variables {A A' : Type} {B : A → Type} {B' : A' → Type} {C : Πa, B a → Type} {D : Πa b, C a b → Type} {a a' a'' : A} {b b₁ b₂ : B a} {b' : B a'} {b'' : B a''} {u v w : Σa, B a} -- sigma.eta is already used for the eta rule for strict equality protected definition eta : Π (u : Σa, B a), ⟨u.1 , u.2⟩ = u | eta ⟨u₁, u₂⟩ := idp definition eta2 : Π (u : Σa b, C a b), ⟨u.1, u.2.1, u.2.2⟩ = u | eta2 ⟨u₁, u₂, u₃⟩ := idp definition eta3 : Π (u : Σa b c, D a b c), ⟨u.1, u.2.1, u.2.2.1, u.2.2.2⟩ = u | eta3 ⟨u₁, u₂, u₃, u₄⟩ := idp definition dpair_eq_dpair (p : a = a') (q : p ▹ b = b') : ⟨a, b⟩ = ⟨a', b'⟩ := by cases p; cases q; apply idp /- In Coq they often have to give u and v explicitly -/ definition sigma_eq (p : u.1 = v.1) (q : p ▹ u.2 = v.2) : u = v := by cases u; cases v; apply (dpair_eq_dpair p q) /- Projections of paths from a total space -/ definition eq_pr1 (p : u = v) : u.1 = v.1 := ap pr1 p postfix `..1`:(max+1) := eq_pr1 definition eq_pr2 (p : u = v) : p..1 ▹ u.2 = v.2 := by cases p; apply idp --Coq uses the following proof, which only computes if u,v are dpairs AND p is idp --(transport_compose B dpr1 p u.2)⁻¹ ⬝ apD dpr2 p postfix `..2`:(max+1) := eq_pr2 private definition dpair_sigma_eq (p : u.1 = v.1) (q : p ▹ u.2 = v.2) : ⟨(sigma_eq p q)..1, (sigma_eq p q)..2⟩ = ⟨p, q⟩ := by cases u; cases v; cases p; cases q; apply idp definition sigma_eq_pr1 (p : u.1 = v.1) (q : p ▹ u.2 = v.2) : (sigma_eq p q)..1 = p := (dpair_sigma_eq p q)..1 definition sigma_eq_pr2 (p : u.1 = v.1) (q : p ▹ u.2 = v.2) : sigma_eq_pr1 p q ▹ (sigma_eq p q)..2 = q := (dpair_sigma_eq p q)..2 definition sigma_eq_eta (p : u = v) : sigma_eq (p..1) (p..2) = p := by cases p; cases u; apply idp definition tr_pr1_sigma_eq {B' : A → Type} (p : u.1 = v.1) (q : p ▹ u.2 = v.2) : transport (λx, B' x.1) (sigma_eq p q) = transport B' p := by cases u; cases v; cases p; cases q; apply idp /- the uncurried version of sigma_eq. We will prove that this is an equivalence -/ definition sigma_eq_uncurried : Π (pq : Σ(p : pr1 u = pr1 v), p ▹ (pr2 u) = pr2 v), u = v | sigma_eq_uncurried ⟨pq₁, pq₂⟩ := sigma_eq pq₁ pq₂ definition dpair_sigma_eq_uncurried : Π (pq : Σ(p : u.1 = v.1), p ▹ u.2 = v.2), sigma.mk (sigma_eq_uncurried pq)..1 (sigma_eq_uncurried pq)..2 = pq | dpair_sigma_eq_uncurried ⟨pq₁, pq₂⟩ := dpair_sigma_eq pq₁ pq₂ definition sigma_eq_pr1_uncurried (pq : Σ(p : u.1 = v.1), p ▹ u.2 = v.2) : (sigma_eq_uncurried pq)..1 = pq.1 := (dpair_sigma_eq_uncurried pq)..1 definition sigma_eq_pr2_uncurried (pq : Σ(p : u.1 = v.1), p ▹ u.2 = v.2) : (sigma_eq_pr1_uncurried pq) ▹ (sigma_eq_uncurried pq)..2 = pq.2 := (dpair_sigma_eq_uncurried pq)..2 definition sigma_eq_eta_uncurried (p : u = v) : sigma_eq_uncurried (sigma.mk p..1 p..2) = p := sigma_eq_eta p definition tr_sigma_eq_pr1_uncurried {B' : A → Type} (pq : Σ(p : u.1 = v.1), p ▹ u.2 = v.2) : transport (λx, B' x.1) (@sigma_eq_uncurried A B u v pq) = transport B' pq.1 := destruct pq tr_pr1_sigma_eq definition is_equiv_sigma_eq [instance] (u v : Σa, B a) : is_equiv (@sigma_eq_uncurried A B u v) := adjointify sigma_eq_uncurried (λp, ⟨p..1, p..2⟩) sigma_eq_eta_uncurried dpair_sigma_eq_uncurried definition equiv_sigma_eq (u v : Σa, B a) : (Σ(p : u.1 = v.1), p ▹ u.2 = v.2) ≃ (u = v) := equiv.mk sigma_eq_uncurried !is_equiv_sigma_eq definition dpair_eq_dpair_con (p1 : a = a' ) (q1 : p1 ▹ b = b' ) (p2 : a' = a'') (q2 : p2 ▹ b' = b'') : dpair_eq_dpair (p1 ⬝ p2) (tr_con B p1 p2 b ⬝ ap (transport B p2) q1 ⬝ q2) = dpair_eq_dpair p1 q1 ⬝ dpair_eq_dpair p2 q2 := by cases p1; cases p2; cases q1; cases q2; apply idp definition sigma_eq_con (p1 : u.1 = v.1) (q1 : p1 ▹ u.2 = v.2) (p2 : v.1 = w.1) (q2 : p2 ▹ v.2 = w.2) : sigma_eq (p1 ⬝ p2) (tr_con B p1 p2 u.2 ⬝ ap (transport B p2) q1 ⬝ q2) = sigma_eq p1 q1 ⬝ sigma_eq p2 q2 := by cases u; cases v; cases w; apply dpair_eq_dpair_con local attribute dpair_eq_dpair [reducible] definition dpair_eq_dpair_con_idp (p : a = a') (q : p ▹ b = b') : dpair_eq_dpair p q = dpair_eq_dpair p idp ⬝ dpair_eq_dpair idp q := by cases p; cases q; apply idp /- eq_pr1 commutes with the groupoid structure. -/ definition eq_pr1_idp (u : Σa, B a) : (refl u) ..1 = refl (u.1) := idp definition eq_pr1_con (p : u = v) (q : v = w) : (p ⬝ q) ..1 = (p..1) ⬝ (q..1) := !ap_con definition eq_pr1_inv (p : u = v) : p⁻¹ ..1 = (p..1)⁻¹ := !ap_inv /- Applying dpair to one argument is the same as dpair_eq_dpair with reflexivity in the first place. -/ definition ap_dpair (q : b₁ = b₂) : ap (sigma.mk a) q = dpair_eq_dpair idp q := by cases q; apply idp /- Dependent transport is the same as transport along a sigma_eq. -/ definition transportD_eq_transport (p : a = a') (c : C a b) : p ▹D c = transport (λu, C (u.1) (u.2)) (dpair_eq_dpair p idp) c := by cases p; apply idp definition sigma_eq_eq_sigma_eq {p1 q1 : a = a'} {p2 : p1 ▹ b = b'} {q2 : q1 ▹ b = b'} (r : p1 = q1) (s : r ▹ p2 = q2) : sigma_eq p1 p2 = sigma_eq q1 q2 := by cases r; cases s; apply idp /- A path between paths in a total space is commonly shown component wise. -/ definition sigma_eq2 {p q : u = v} (r : p..1 = q..1) (s : r ▹ p..2 = q..2) : p = q := begin reverts (q, r, s), cases p, cases u with (u1, u2), intros (q, r, s), apply concat, rotate 1, apply sigma_eq_eta, apply (sigma_eq_eq_sigma_eq r s) end /- In Coq they often have to give u and v explicitly when using the following definition -/ definition sigma_eq2_uncurried {p q : u = v} (rs : Σ(r : p..1 = q..1), transport (λx, transport B x u.2 = v.2) r p..2 = q..2) : p = q := destruct rs sigma_eq2 /- Transport -/ /- The concrete description of transport in sigmas (and also pis) is rather trickier than in the other types. In particular, these cannot be described just in terms of transport in simpler types; they require also the dependent transport [transportD]. In particular, this indicates why `transport` alone cannot be fully defined by induction on the structure of types, although Id-elim/transportD can be (cf. Observational Type Theory). A more thorough set of lemmas, along the lines of the present ones but dealing with Id-elim rather than just transport, might be nice to have eventually? -/ definition transport_eq (p : a = a') (bc : Σ(b : B a), C a b) : p ▹ bc = ⟨p ▹ bc.1, p ▹D bc.2⟩ := by cases p; cases bc; apply idp /- The special case when the second variable doesn't depend on the first is simpler. -/ definition tr_eq_nondep {B : Type} {C : A → B → Type} (p : a = a') (bc : Σ(b : B), C a b) : p ▹ bc = ⟨bc.1, p ▹ bc.2⟩ := by cases p; cases bc; apply idp /- Or if the second variable contains a first component that doesn't depend on the first. -/ definition tr_eq2_nondep {C : A → Type} {D : Π a:A, B a → C a → Type} (p : a = a') (bcd : Σ(b : B a) (c : C a), D a b c) : p ▹ bcd = ⟨p ▹ bcd.1, p ▹ bcd.2.1, p ▹D2 bcd.2.2⟩ := begin cases p, cases bcd with (b, cd), cases cd, apply idp end /- Functorial action -/ variables (f : A → A') (g : Πa, B a → B' (f a)) definition sigma_functor (u : Σa, B a) : Σa', B' a' := ⟨f u.1, g u.1 u.2⟩ /- Equivalences -/ definition is_equiv_sigma_functor [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : is_equiv (sigma_functor f g) := adjointify (sigma_functor f g) (sigma_functor f⁻¹ (λ(a' : A') (b' : B' a'), ((g (f⁻¹ a'))⁻¹ (transport B' (retr f a')⁻¹ b')))) begin intro u', cases u' with (a', b'), apply (sigma_eq (retr f a')), -- rewrite retr, -- end -- "rewrite retr (g (f⁻¹ a'))" apply concat, apply (ap (λx, (transport B' (retr f a') x))), apply (retr (g (f⁻¹ a'))), show retr f a' ▹ ((retr f a')⁻¹ ▹ b') = b', from tr_inv_tr B' (retr f a') b' end begin intro u, cases u with (a, b), apply (sigma_eq (sect f a)), show transport B (sect f a) ((g (f⁻¹ (f a)))⁻¹ (transport B' (retr f (f a))⁻¹ (g a b))) = b, from calc transport B (sect f a) ((g (f⁻¹ (f a)))⁻¹ (transport B' (retr f (f a))⁻¹ (g a b))) = (g a)⁻¹ (transport (B' ∘ f) (sect f a) (transport B' (retr f (f a))⁻¹ (g a b))) : by rewrite (fn_tr_eq_tr_fn (sect f a) (λ a, (g a)⁻¹)) ... = (g a)⁻¹ (transport B' (ap f (sect f a)) (transport B' (retr f (f a))⁻¹ (g a b))) : ap (g a)⁻¹ !transport_compose ... = (g a)⁻¹ (transport B' (ap f (sect f a)) (transport B' (ap f (sect f a))⁻¹ (g a b))) : ap (λ x, (g a)⁻¹ (transport B' (ap f (sect f a)) (transport B' x⁻¹ (g a b)))) (adj f a) ... = (g a)⁻¹ (g a b) : {!tr_inv_tr} ... = b : by rewrite (sect (g a) b) end definition sigma_equiv_sigma_of_is_equiv [H1 : is_equiv f] [H2 : Π a, is_equiv (g a)] : (Σa, B a) ≃ (Σa', B' a') := equiv.mk (sigma_functor f g) !is_equiv_sigma_functor context attribute inv [irreducible] attribute function.compose [irreducible] --this is needed for the following class inference problem definition sigma_equiv_sigma (Hf : A ≃ A') (Hg : Π a, B a ≃ B' (to_fun Hf a)) : (Σa, B a) ≃ (Σa', B' a') := sigma_equiv_sigma_of_is_equiv (to_fun Hf) (λ a, to_fun (Hg a)) end definition sigma_equiv_sigma_id {B' : A → Type} (Hg : Π a, B a ≃ B' a) : (Σa, B a) ≃ Σa, B' a := sigma_equiv_sigma equiv.refl Hg definition ap_sigma_functor_eq_dpair (p : a = a') (q : p ▹ b = b') : ap (sigma.sigma_functor f g) (sigma_eq p q) = sigma_eq (ap f p) ((transport_compose _ f p (g a b))⁻¹ ⬝ (fn_tr_eq_tr_fn p g b)⁻¹ ⬝ ap (g a') q) := by cases p; cases q; apply idp definition ap_sigma_functor_eq (p : u.1 = v.1) (q : p ▹ u.2 = v.2) : ap (sigma.sigma_functor f g) (sigma_eq p q) = sigma_eq (ap f p) ((transport_compose B' f p (g u.1 u.2))⁻¹ ⬝ (fn_tr_eq_tr_fn p g u.2)⁻¹ ⬝ ap (g v.1) q) := by cases u; cases v; apply ap_sigma_functor_eq_dpair /- definition 3.11.9(i): Summing up a contractible family of types does nothing. -/ open is_trunc definition is_equiv_pr1 [instance] (B : A → Type) [H : Π a, is_contr (B a)] : is_equiv (@pr1 A B) := adjointify pr1 (λa, ⟨a, !center⟩) (λa, idp) (λu, sigma_eq idp !contr) definition sigma_equiv_of_is_contr_pr2 [H : Π a, is_contr (B a)] : (Σa, B a) ≃ A := equiv.mk pr1 _ /- definition 3.11.9(ii): Dually, summing up over a contractible type does nothing. -/ definition sigma_equiv_of_is_contr_pr1 (B : A → Type) [H : is_contr A] : (Σa, B a) ≃ B (center A) := equiv.mk _ (adjointify (λu, (contr u.1)⁻¹ ▹ u.2) (λb, ⟨!center, b⟩) (λb, ap (λx, x ▹ b) !hprop_eq_of_is_contr) (λu, sigma_eq !contr !tr_inv_tr)) /- Associativity -/ --this proof is harder than in Coq because we don't have eta definitionally for sigma definition sigma_assoc_equiv (C : (Σa, B a) → Type) : (Σa b, C ⟨a, b⟩) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨⟨av.1, av.2.1⟩, av.2.2⟩) (λuc, ⟨uc.1.1, uc.1.2, !eta⁻¹ ▹ uc.2⟩) begin intro uc; cases uc with (u, c); cases u; apply idp end begin intro av; cases av with (a, v); cases v; apply idp end) open prod definition assoc_equiv_prod (C : (A × A') → Type) : (Σa a', C (a,a')) ≃ (Σu, C u) := equiv.mk _ (adjointify (λav, ⟨(av.1, av.2.1), av.2.2⟩) (λuc, ⟨pr₁ (uc.1), pr₂ (uc.1), !prod.eta⁻¹ ▹ uc.2⟩) proof (λuc, destruct uc (λu, prod.destruct u (λa b c, idp))) qed proof (λav, destruct av (λa v, destruct v (λb c, idp))) qed) /- Symmetry -/ definition comm_equiv_uncurried (C : A × A' → Type) : (Σa a', C (a, a')) ≃ (Σa' a, C (a, a')) := calc (Σa a', C (a, a')) ≃ Σu, C u : assoc_equiv_prod ... ≃ Σv, C (flip v) : sigma_equiv_sigma !prod_comm_equiv (λu, prod.destruct u (λa a', equiv.refl)) ... ≃ (Σa' a, C (a, a')) : assoc_equiv_prod definition sigma_comm_equiv (C : A → A' → Type) : (Σa a', C a a') ≃ (Σa' a, C a a') := comm_equiv_uncurried (λu, C (prod.pr1 u) (prod.pr2 u)) definition equiv_prod (A B : Type) : (Σ(a : A), B) ≃ A × B := equiv.mk _ (adjointify (λs, (s.1, s.2)) (λp, ⟨pr₁ p, pr₂ p⟩) proof (λp, prod.destruct p (λa b, idp)) qed proof (λs, destruct s (λa b, idp)) qed) definition comm_equiv_nondep (A B : Type) : (Σ(a : A), B) ≃ Σ(b : B), A := calc (Σ(a : A), B) ≃ A × B : equiv_prod ... ≃ B × A : prod_comm_equiv ... ≃ Σ(b : B), A : equiv_prod /- ** Universal mapping properties -/ /- *** The positive universal property. -/ section definition is_equiv_sigma_rec [instance] (C : (Σa, B a) → Type) : is_equiv (@sigma.rec _ _ C) := adjointify _ (λ g a b, g ⟨a, b⟩) (λ g, proof eq_of_homotopy (λu, destruct u (λa b, idp)) qed) (λ f, refl f) definition equiv_sigma_rec (C : (Σa, B a) → Type) : (Π(a : A) (b: B a), C ⟨a, b⟩) ≃ (Πxy, C xy) := equiv.mk sigma.rec _ /- *** The negative universal property. -/ protected definition coind_uncurried (fg : Σ(f : Πa, B a), Πa, C a (f a)) (a : A) : Σ(b : B a), C a b := ⟨fg.1 a, fg.2 a⟩ protected definition coind (f : Π a, B a) (g : Π a, C a (f a)) (a : A) : Σ(b : B a), C a b := coind_uncurried ⟨f, g⟩ a --is the instance below dangerous? --in Coq this can be done without function extensionality definition is_equiv_coind [instance] (C : Πa, B a → Type) : is_equiv (@coind_uncurried _ _ C) := adjointify _ (λ h, ⟨λa, (h a).1, λa, (h a).2⟩) (λ h, proof eq_of_homotopy (λu, !eta) qed) (λfg, destruct fg (λ(f : Π (a : A), B a) (g : Π (x : A), C x (f x)), proof idp qed)) definition sigma_pi_equiv_pi_sigma : (Σ(f : Πa, B a), Πa, C a (f a)) ≃ (Πa, Σb, C a b) := equiv.mk coind_uncurried _ end /- ** Subtypes (sigma types whose second components are hprops) -/ /- To prove equality in a subtype, we only need equality of the first component. -/ definition subtype_eq [H : Πa, is_hprop (B a)] (u v : Σa, B a) : u.1 = v.1 → u = v := (sigma_eq_uncurried ∘ (@inv _ _ pr1 (@is_equiv_pr1 _ _ (λp, !is_trunc.is_trunc_eq)))) definition is_equiv_subtype_eq [H : Πa, is_hprop (B a)] (u v : Σa, B a) : is_equiv (subtype_eq u v) := !is_equiv_compose local attribute is_equiv_subtype_eq [instance] definition equiv_subtype [H : Πa, is_hprop (B a)] (u v : Σa, B a) : (u.1 = v.1) ≃ (u = v) := equiv.mk !subtype_eq _ /- truncatedness -/ definition is_trunc_sigma (B : A → Type) (n : trunc_index) [HA : is_trunc n A] [HB : Πa, is_trunc n (B a)] : is_trunc n (Σa, B a) := begin reverts (A, B, HA, HB), apply (trunc_index.rec_on n), intros (A, B, HA, HB), fapply is_trunc.is_trunc_equiv_closed, apply equiv.symm, apply sigma_equiv_of_is_contr_pr1, intros (n, IH, A, B, HA, HB), fapply is_trunc.is_trunc_succ_intro, intros (u, v), fapply is_trunc.is_trunc_equiv_closed, apply equiv_sigma_eq, apply IH, apply is_trunc.is_trunc_eq, intro p, show is_trunc n (p ▹ u .2 = v .2), from is_trunc.is_trunc_eq n (p ▹ u.2) (v.2), end end sigma attribute sigma.is_trunc_sigma [instance] open is_trunc sigma prod /- truncatedness -/ definition prod.is_trunc_prod [instance] (A B : Type) (n : trunc_index) [HA : is_trunc n A] [HB : is_trunc n B] : is_trunc n (A × B) := is_trunc.is_trunc_equiv_closed n !equiv_prod
8ac7413948a2a26db45e4ed8812054b7ff455793
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/convex/star.lean
3ef0ab4a502bb29bc4061b7ea11f2dbcc421a5dd
[ "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
16,741
lean
/- Copyright (c) 2021 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import analysis.convex.basic /-! # Star-convex sets This files defines star-convex sets (aka star domains, star-shaped set, radially convex set). A set is star-convex at `x` if every segment from `x` to a point in the set is contained in the set. This is the prototypical example of a contractible set in homotopy theory (by scaling every point towards `x`), but has wider uses. Note that this has nothing to do with star rings, `has_star` and co. ## Main declarations * `star_convex 𝕜 x s`: `s` is star-convex at `x` with scalars `𝕜`. ## Implementation notes Instead of saying that a set is star-convex, we say a set is star-convex *at a point*. This has the advantage of allowing us to talk about convexity as being "everywhere star-convexity" and of making the union of star-convex sets be star-convex. Incidentally, this choice means we don't need to assume a set is nonempty for it to be star-convex. Concretely, the empty set is star-convex at every point. ## TODO Balanced sets are star-convex. The closure of a star-convex set is star-convex. Star-convex sets are contractible. A nonempty open star-convex set in `ℝ^n` is diffeomorphic to the entire space. -/ open set open_locale convex pointwise variables {𝕜 E F β : Type*} section ordered_semiring variables [ordered_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] section has_smul variables (𝕜) [has_smul 𝕜 E] [has_smul 𝕜 F] (x : E) (s : set E) /-- Star-convexity of sets. `s` is star-convex at `x` if every segment from `x` to a point in `s` is contained in `s`. -/ def star_convex : Prop := ∀ ⦃y : E⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • x + b • y ∈ s variables {𝕜 x s} {t : set E} lemma convex_iff_forall_star_convex : convex 𝕜 s ↔ ∀ x ∈ s, star_convex 𝕜 x s := forall_congr $ λ x, forall_swap lemma convex.star_convex (h : convex 𝕜 s) (hx : x ∈ s) : star_convex 𝕜 x s := convex_iff_forall_star_convex.1 h _ hx lemma star_convex_iff_segment_subset : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → [x -[𝕜] y] ⊆ s := begin split, { rintro h y hy z ⟨a, b, ha, hb, hab, rfl⟩, exact h hy ha hb hab }, { rintro h y hy a b ha hb hab, exact h hy ⟨a, b, ha, hb, hab, rfl⟩ } end lemma star_convex.segment_subset (h : star_convex 𝕜 x s) {y : E} (hy : y ∈ s) : [x -[𝕜] y] ⊆ s := star_convex_iff_segment_subset.1 h hy lemma star_convex.open_segment_subset (h : star_convex 𝕜 x s) {y : E} (hy : y ∈ s) : open_segment 𝕜 x y ⊆ s := (open_segment_subset_segment 𝕜 x y).trans (h.segment_subset hy) /-- Alternative definition of star-convexity, in terms of pointwise set operations. -/ lemma star_convex_iff_pointwise_add_subset : star_convex 𝕜 x s ↔ ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → a + b = 1 → a • {x} + b • s ⊆ s := begin refine ⟨_, λ h y hy a b ha hb hab, h ha hb hab (add_mem_add (smul_mem_smul_set $ mem_singleton _) ⟨_, hy, rfl⟩)⟩, rintro hA a b ha hb hab w ⟨au, bv, ⟨u, (rfl : u = x), rfl⟩, ⟨v, hv, rfl⟩, rfl⟩, exact hA hv ha hb hab, end lemma star_convex_empty (x : E) : star_convex 𝕜 x ∅ := λ y hy, hy.elim lemma star_convex_univ (x : E) : star_convex 𝕜 x univ := λ _ _ _ _ _ _ _, trivial lemma star_convex.inter (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 x t) : star_convex 𝕜 x (s ∩ t) := λ y hy a b ha hb hab, ⟨hs hy.left ha hb hab, ht hy.right ha hb hab⟩ lemma star_convex_sInter {S : set (set E)} (h : ∀ s ∈ S, star_convex 𝕜 x s) : star_convex 𝕜 x (⋂₀ S) := λ y hy a b ha hb hab s hs, h s hs (hy s hs) ha hb hab lemma star_convex_Inter {ι : Sort*} {s : ι → set E} (h : ∀ i, star_convex 𝕜 x (s i)) : star_convex 𝕜 x (⋂ i, s i) := (sInter_range s) ▸ star_convex_sInter $ forall_range_iff.2 h lemma star_convex.union (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 x t) : star_convex 𝕜 x (s ∪ t) := begin rintro y (hy | hy) a b ha hb hab, { exact or.inl (hs hy ha hb hab) }, { exact or.inr (ht hy ha hb hab) } end lemma star_convex_Union {ι : Sort*} {s : ι → set E} (hs : ∀ i, star_convex 𝕜 x (s i)) : star_convex 𝕜 x (⋃ i, s i) := begin rintro y hy a b ha hb hab, rw mem_Union at ⊢ hy, obtain ⟨i, hy⟩ := hy, exact ⟨i, hs i hy ha hb hab⟩, end lemma star_convex_sUnion {S : set (set E)} (hS : ∀ s ∈ S, star_convex 𝕜 x s) : star_convex 𝕜 x (⋃₀ S) := begin rw sUnion_eq_Union, exact star_convex_Union (λ s, hS _ s.2), end lemma star_convex.prod {y : F} {s : set E} {t : set F} (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x, y) (s ×ˢ t) := λ y hy a b ha hb hab, ⟨hs hy.1 ha hb hab, ht hy.2 ha hb hab⟩ lemma star_convex_pi {ι : Type*} {E : ι → Type*} [Π i, add_comm_monoid (E i)] [Π i, has_smul 𝕜 (E i)] {x : Π i, E i} {s : set ι} {t : Π i, set (E i)} (ht : ∀ i, star_convex 𝕜 (x i) (t i)) : star_convex 𝕜 x (s.pi t) := λ y hy a b ha hb hab i hi, ht i (hy i hi) ha hb hab end has_smul section module variables [module 𝕜 E] [module 𝕜 F] {x y z : E} {s : set E} lemma star_convex.mem (hs : star_convex 𝕜 x s) (h : s.nonempty) : x ∈ s := begin obtain ⟨y, hy⟩ := h, convert hs hy zero_le_one le_rfl (add_zero 1), rw [one_smul, zero_smul, add_zero], end lemma convex.star_convex_iff (hs : convex 𝕜 s) (h : s.nonempty) : star_convex 𝕜 x s ↔ x ∈ s := ⟨λ hxs, hxs.mem h, hs.star_convex⟩ lemma star_convex_iff_forall_pos (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h y hy a b ha hb hab, h hy ha.le hb.le hab, _⟩, intros h y hy a b ha hb hab, obtain rfl | ha := ha.eq_or_lt, { rw zero_add at hab, rwa [hab, one_smul, zero_smul, zero_add] }, obtain rfl | hb := hb.eq_or_lt, { rw add_zero at hab, rwa [hab, one_smul, zero_smul, add_zero] }, exact h hy ha hb hab, end lemma star_convex_iff_forall_ne_pos (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → x ≠ y → ∀ ⦃a b : 𝕜⦄, 0 < a → 0 < b → a + b = 1 → a • x + b • y ∈ s := begin refine ⟨λ h y hy _ a b ha hb hab, h hy ha.le hb.le hab, _⟩, intros h y hy a b ha hb hab, obtain rfl | ha' := ha.eq_or_lt, { rw [zero_add] at hab, rwa [hab, zero_smul, one_smul, zero_add] }, obtain rfl | hb' := hb.eq_or_lt, { rw [add_zero] at hab, rwa [hab, zero_smul, one_smul, add_zero] }, obtain rfl | hxy := eq_or_ne x y, { rwa convex.combo_self hab }, exact h hy hxy ha' hb' hab, end lemma star_convex_iff_open_segment_subset (hx : x ∈ s) : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → open_segment 𝕜 x y ⊆ s := star_convex_iff_segment_subset.trans $ forall₂_congr $ λ y hy, (open_segment_subset_iff_segment_subset hx hy).symm lemma star_convex_singleton (x : E) : star_convex 𝕜 x {x} := begin rintro y (rfl : y = x) a b ha hb hab, exact convex.combo_self hab _, end lemma star_convex.linear_image (hs : star_convex 𝕜 x s) (f : E →ₗ[𝕜] F) : star_convex 𝕜 (f x) (s.image f) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, exact ⟨a • x + b • y', hs hy' ha hb hab, by rw [f.map_add, f.map_smul, f.map_smul]⟩, end lemma star_convex.is_linear_image (hs : star_convex 𝕜 x s) {f : E → F} (hf : is_linear_map 𝕜 f) : star_convex 𝕜 (f x) (f '' s) := hs.linear_image $ hf.mk' f lemma star_convex.linear_preimage {s : set F} (f : E →ₗ[𝕜] F) (hs : star_convex 𝕜 (f x) s) : star_convex 𝕜 x (s.preimage f) := begin intros y hy a b ha hb hab, rw [mem_preimage, f.map_add, f.map_smul, f.map_smul], exact hs hy ha hb hab, end lemma star_convex.is_linear_preimage {s : set F} {f : E → F} (hs : star_convex 𝕜 (f x) s) (hf : is_linear_map 𝕜 f) : star_convex 𝕜 x (preimage f s) := hs.linear_preimage $ hf.mk' f lemma star_convex.add {t : set E} (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x + y) (s + t) := by { rw ←add_image_prod, exact (hs.prod ht).is_linear_image is_linear_map.is_linear_map_add } lemma star_convex.add_left (hs : star_convex 𝕜 x s) (z : E) : star_convex 𝕜 (z + x) ((λ x, z + x) '' s) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, refine ⟨a • x + b • y', hs hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end lemma star_convex.add_right (hs : star_convex 𝕜 x s) (z : E) : star_convex 𝕜 (x + z) ((λ x, x + z) '' s) := begin intros y hy a b ha hb hab, obtain ⟨y', hy', rfl⟩ := hy, refine ⟨a • x + b • y', hs hy' ha hb hab, _⟩, rw [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul], end /-- The translation of a star-convex set is also star-convex. -/ lemma star_convex.preimage_add_right (hs : star_convex 𝕜 (z + x) s) : star_convex 𝕜 x ((λ x, z + x) ⁻¹' s) := begin intros y hy a b ha hb hab, have h := hs hy ha hb hab, rwa [smul_add, smul_add, add_add_add_comm, ←add_smul, hab, one_smul] at h, end /-- The translation of a star-convex set is also star-convex. -/ lemma star_convex.preimage_add_left (hs : star_convex 𝕜 (x + z) s) : star_convex 𝕜 x ((λ x, x + z) ⁻¹' s) := begin rw add_comm at hs, simpa only [add_comm] using hs.preimage_add_right, end end module end add_comm_monoid section add_comm_group variables [add_comm_group E] [module 𝕜 E] {x y : E} lemma star_convex.sub' {s : set (E × E)} (hs : star_convex 𝕜 (x, y) s) : star_convex 𝕜 (x - y) ((λ x : E × E, x.1 - x.2) '' s) := hs.is_linear_image is_linear_map.is_linear_map_sub end add_comm_group end ordered_semiring section ordered_comm_semiring variables [ordered_comm_semiring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [add_comm_monoid F] [module 𝕜 E] [module 𝕜 F] {x : E} {s : set E} lemma star_convex.smul (hs : star_convex 𝕜 x s) (c : 𝕜) : star_convex 𝕜 (c • x) (c • s) := hs.linear_image $ linear_map.lsmul _ _ c lemma star_convex.preimage_smul {c : 𝕜} (hs : star_convex 𝕜 (c • x) s) : star_convex 𝕜 x ((λ z, c • z) ⁻¹' s) := hs.linear_preimage (linear_map.lsmul _ _ c) lemma star_convex.affinity (hs : star_convex 𝕜 x s) (z : E) (c : 𝕜) : star_convex 𝕜 (z + c • x) ((λ x, z + c • x) '' s) := begin have h := (hs.smul c).add_left z, rwa [←image_smul, image_image] at h, end end add_comm_monoid end ordered_comm_semiring section ordered_ring variables [ordered_ring 𝕜] section add_comm_monoid variables [add_comm_monoid E] [smul_with_zero 𝕜 E]{s : set E} lemma star_convex_zero_iff : star_convex 𝕜 0 s ↔ ∀ ⦃x : E⦄, x ∈ s → ∀ ⦃a : 𝕜⦄, 0 ≤ a → a ≤ 1 → a • x ∈ s := begin refine forall_congr (λ x, forall_congr $ λ hx, ⟨λ h a ha₀ ha₁, _, λ h a b ha hb hab, _⟩), { simpa only [sub_add_cancel, eq_self_iff_true, forall_true_left, zero_add, smul_zero'] using h (sub_nonneg_of_le ha₁) ha₀ }, { rw [smul_zero', zero_add], exact h hb (by { rw ←hab, exact le_add_of_nonneg_left ha }) } end end add_comm_monoid section add_comm_group variables [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F] {x y : E} {s t : set E} lemma star_convex.add_smul_mem (hs : star_convex 𝕜 x s) (hy : x + y ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 1) : x + t • y ∈ s := begin have h : x + t • y = (1 - t) • x + t • (x + y), { rw [smul_add, ←add_assoc, ←add_smul, sub_add_cancel, one_smul] }, rw h, exact hs hy (sub_nonneg_of_le ht₁) ht₀ (sub_add_cancel _ _), end lemma star_convex.smul_mem (hs : star_convex 𝕜 0 s) (hx : x ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 1) : t • x ∈ s := by simpa using hs.add_smul_mem (by simpa using hx) ht₀ ht₁ lemma star_convex.add_smul_sub_mem (hs : star_convex 𝕜 x s) (hy : y ∈ s) {t : 𝕜} (ht₀ : 0 ≤ t) (ht₁ : t ≤ 1) : x + t • (y - x) ∈ s := begin apply hs.segment_subset hy, rw segment_eq_image', exact mem_image_of_mem _ ⟨ht₀, ht₁⟩, end /-- The preimage of a star-convex set under an affine map is star-convex. -/ lemma star_convex.affine_preimage (f : E →ᵃ[𝕜] F) {s : set F} (hs : star_convex 𝕜 (f x) s) : star_convex 𝕜 x (f ⁻¹' s) := begin intros y hy a b ha hb hab, rw [mem_preimage, convex.combo_affine_apply hab], exact hs hy ha hb hab, end /-- The image of a star-convex set under an affine map is star-convex. -/ lemma star_convex.affine_image (f : E →ᵃ[𝕜] F) {s : set E} (hs : star_convex 𝕜 x s) : star_convex 𝕜 (f x) (f '' s) := begin rintro y ⟨y', ⟨hy', hy'f⟩⟩ a b ha hb hab, refine ⟨a • x + b • y', ⟨hs hy' ha hb hab, _⟩⟩, rw [convex.combo_affine_apply hab, hy'f], end lemma star_convex.neg (hs : star_convex 𝕜 x s) : star_convex 𝕜 (-x) (-s) := by { rw ←image_neg, exact hs.is_linear_image is_linear_map.is_linear_map_neg } lemma star_convex.sub (hs : star_convex 𝕜 x s) (ht : star_convex 𝕜 y t) : star_convex 𝕜 (x - y) (s - t) := by { simp_rw sub_eq_add_neg, exact hs.add ht.neg } end add_comm_group end ordered_ring section linear_ordered_field variables [linear_ordered_field 𝕜] section add_comm_group variables [add_comm_group E] [module 𝕜 E] {x : E} {s : set E} /-- Alternative definition of star-convexity, using division. -/ lemma star_convex_iff_div : star_convex 𝕜 x s ↔ ∀ ⦃y⦄, y ∈ s → ∀ ⦃a b : 𝕜⦄, 0 ≤ a → 0 ≤ b → 0 < a + b → (a / (a + b)) • x + (b / (a + b)) • y ∈ s := ⟨λ h y hy a b ha hb hab, begin apply h hy, { have ha', from mul_le_mul_of_nonneg_left ha (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at ha' }, { have hb', from mul_le_mul_of_nonneg_left hb (inv_pos.2 hab).le, rwa [mul_zero, ←div_eq_inv_mul] at hb' }, { rw ←add_div, exact div_self hab.ne' } end, λ h y hy a b ha hb hab, begin have h', from h hy ha hb, rw [hab, div_one, div_one] at h', exact h' zero_lt_one end⟩ lemma star_convex.mem_smul (hs : star_convex 𝕜 0 s) (hx : x ∈ s) {t : 𝕜} (ht : 1 ≤ t) : x ∈ t • s := begin rw mem_smul_set_iff_inv_smul_mem₀ (zero_lt_one.trans_le ht).ne', exact hs.smul_mem hx (inv_nonneg.2 $ zero_le_one.trans ht) (inv_le_one ht), end end add_comm_group end linear_ordered_field /-! #### Star-convex sets in an ordered space Relates `star_convex` and `set.ord_connected`. -/ section ord_connected lemma set.ord_connected.star_convex [ordered_semiring 𝕜] [ordered_add_comm_monoid E] [module 𝕜 E] [ordered_smul 𝕜 E] {x : E} {s : set E} (hs : s.ord_connected) (hx : x ∈ s) (h : ∀ y ∈ s, x ≤ y ∨ y ≤ x) : star_convex 𝕜 x s := begin intros y hy a b ha hb hab, obtain hxy | hyx := h _ hy, { refine hs.out hx hy (mem_Icc.2 ⟨_, _⟩), calc x = a • x + b • x : (convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_left (smul_le_smul_of_nonneg hxy hb) _, calc a • x + b • y ≤ a • y + b • y : add_le_add_right (smul_le_smul_of_nonneg hxy ha) _ ... = y : convex.combo_self hab _ }, { refine hs.out hy hx (mem_Icc.2 ⟨_, _⟩), calc y = a • y + b • y : (convex.combo_self hab _).symm ... ≤ a • x + b • y : add_le_add_right (smul_le_smul_of_nonneg hyx ha) _, calc a • x + b • y ≤ a • x + b • x : add_le_add_left (smul_le_smul_of_nonneg hyx hb) _ ... = x : convex.combo_self hab _ } end lemma star_convex_iff_ord_connected [linear_ordered_field 𝕜] {x : 𝕜} {s : set 𝕜} (hx : x ∈ s) : star_convex 𝕜 x s ↔ s.ord_connected := by simp_rw [ord_connected_iff_interval_subset_left hx, star_convex_iff_segment_subset, segment_eq_interval] alias star_convex_iff_ord_connected ↔ star_convex.ord_connected _ end ord_connected /-! #### Star-convexity of submodules/subspaces -/ section submodule open submodule lemma submodule.star_convex [ordered_semiring 𝕜] [add_comm_monoid E] [module 𝕜 E] (K : submodule 𝕜 E) : star_convex 𝕜 (0 : E) K := K.convex.star_convex K.zero_mem lemma subspace.star_convex [linear_ordered_field 𝕜] [add_comm_group E] [module 𝕜 E] (K : subspace 𝕜 E) : star_convex 𝕜 (0 : E) K := K.convex.star_convex K.zero_mem end submodule
caa22eac7b7e87296a29e26b0aca028a07739f8b
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/nat/choose/default.lean
1b94964d592ee9dce82d4cc5c08862d35794f0f1
[ "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
54
lean
import data.nat.choose.dvd import data.nat.choose.sum
67a5d8a2246e389fb5093026c87a76fd448ef39b
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/category_theory/limits/shapes/images.lean
ef61f57f9c364c13efd8fc2aa0a8647a4d75c5c3
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
20,725
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Markus Himmel -/ import category_theory.limits.shapes.equalizers import category_theory.limits.shapes.strong_epi /-! # Categorical images We define the categorical image of `f` as a factorisation `f = e ≫ m` through a monomorphism `m`, so that `m` factors through the `m'` in any other such factorisation. ## Main definitions * A `mono_factorisation` is a factorisation `f = e ≫ m`, where `m` is a monomorphism * `is_image F` means that a given mono factorisation `F` has the universal property of the image. * `has_image f` means that we have chosen an image for the morphism `f : X ⟶ Y`. * In this case, `image f` is the image object, `image.ι f : image f ⟶ Y` is the monomorphism `m` of the factorisation and `factor_thru_image f : X ⟶ image f` is the morphism `e`. * `has_images C` means that every morphism in `C` has an image. * Let `f : X ⟶ Y` and `g : P ⟶ Q` be morphisms in `C`, which we will represent as objects of the arrow category `arrow C`. Then `sq : f ⟶ g` is a commutative square in `C`. If `f` and `g` have images, then `has_image_map sq` represents the fact that there is a morphism `i : image f ⟶ image g` making the diagram X ----→ image f ----→ Y | | | | | | ↓ ↓ ↓ P ----→ image g ----→ Q commute, where the top row is the image factorisation of `f`, the bottom row is the image factorisation of `g`, and the outer rectangle is the commutative square `sq`. * If a category `has_images`, then `has_image_maps` means that every commutative square admits an image map. * If a category `has_images`, then `has_strong_epi_images` means that the morphism to the image is always a strong epimorphism. ## Main statements * When `C` has equalizers, the morphism `e` appearing in an image factorisation is an epimorphism. * When `C` has strong epi images, then these images admit image maps. ## Future work * TODO: coimages, and abelian categories. * TODO: connect this with existing working in the group theory and ring theory libraries. -/ universes v u open category_theory open category_theory.limits.walking_parallel_pair namespace category_theory.limits variables {C : Type u} [category.{v} C] variables {X Y : C} (f : X ⟶ Y) /-- A factorisation of a morphism `f = e ≫ m`, with `m` monic. -/ structure mono_factorisation (f : X ⟶ Y) := (I : C) (m : I ⟶ Y) [m_mono : mono m] (e : X ⟶ I) (fac' : e ≫ m = f . obviously) restate_axiom mono_factorisation.fac' attribute [simp, reassoc] mono_factorisation.fac attribute [instance] mono_factorisation.m_mono attribute [instance] mono_factorisation.m_mono namespace mono_factorisation /-- The obvious factorisation of a monomorphism through itself. -/ def self [mono f] : mono_factorisation f := { I := X, m := f, e := 𝟙 X } -- I'm not sure we really need this, but the linter says that an inhabited instance ought to exist... instance [mono f] : inhabited (mono_factorisation f) := ⟨self f⟩ /-- The morphism `m` in a factorisation `f = e ≫ m` through a monomorphism is uniquely determined. -/ @[ext] lemma ext {F F' : mono_factorisation f} (hI : F.I = F'.I) (hm : F.m = (eq_to_hom hI) ≫ F'.m) : F = F' := begin cases F, cases F', cases hI, simp at hm, dsimp at F_fac' F'_fac', congr, { assumption }, { resetI, apply (cancel_mono F_m).1, rw [F_fac', hm, F'_fac'], } end end mono_factorisation variable {f} /-- Data exhibiting that a given factorisation through a mono is initial. -/ structure is_image (F : mono_factorisation f) := (lift : Π (F' : mono_factorisation f), F.I ⟶ F'.I) (lift_fac' : Π (F' : mono_factorisation f), lift F' ≫ F'.m = F.m . obviously) restate_axiom is_image.lift_fac' attribute [simp, reassoc] is_image.lift_fac @[simp, reassoc] lemma is_image.fac_lift {F : mono_factorisation f} (hF : is_image F) (F' : mono_factorisation f) : F.e ≫ hF.lift F' = F'.e := (cancel_mono F'.m).1 $ by simp variable (f) namespace is_image /-- The trivial factorisation of a monomorphism satisfies the universal property. -/ @[simps] def self [mono f] : is_image (mono_factorisation.self f) := { lift := λ F', F'.e } instance [mono f] : inhabited (is_image (mono_factorisation.self f)) := ⟨self f⟩ variable {f} /-- Two factorisations through monomorphisms satisfying the universal property must factor through isomorphic objects. -/ -- TODO this is another good candidate for a future `unique_up_to_canonical_iso`. @[simps] def iso_ext {F F' : mono_factorisation f} (hF : is_image F) (hF' : is_image F') : F.I ≅ F'.I := { hom := hF.lift F', inv := hF'.lift F, hom_inv_id' := (cancel_mono F.m).1 (by simp), inv_hom_id' := (cancel_mono F'.m).1 (by simp) } end is_image /-- Data exhibiting that a morphism `f` has an image. -/ class has_image (f : X ⟶ Y) := (F : mono_factorisation f) (is_image : is_image F) section variable [has_image f] /-- The chosen factorisation of `f` through a monomorphism. -/ def image.mono_factorisation : mono_factorisation f := has_image.F /-- The witness of the universal property for the chosen factorisation of `f` through a monomorphism. -/ def image.is_image : is_image (image.mono_factorisation f) := has_image.is_image /-- The categorical image of a morphism. -/ def image : C := (image.mono_factorisation f).I /-- The inclusion of the image of a morphism into the target. -/ def image.ι : image f ⟶ Y := (image.mono_factorisation f).m @[simp] lemma image.as_ι : (image.mono_factorisation f).m = image.ι f := rfl instance : mono (image.ι f) := (image.mono_factorisation f).m_mono /-- The map from the source to the image of a morphism. -/ def factor_thru_image : X ⟶ image f := (image.mono_factorisation f).e /-- Rewrite in terms of the `factor_thru_image` interface. -/ @[simp] lemma as_factor_thru_image : (image.mono_factorisation f).e = factor_thru_image f := rfl @[simp, reassoc] lemma image.fac : factor_thru_image f ≫ image.ι f = f := (image.mono_factorisation f).fac' variable {f} /-- Any other factorisation of the morphism `f` through a monomorphism receives a map from the image. -/ def image.lift (F' : mono_factorisation f) : image f ⟶ F'.I := (image.is_image f).lift F' @[simp, reassoc] lemma image.lift_fac (F' : mono_factorisation f) : image.lift F' ≫ F'.m = image.ι f := (image.is_image f).lift_fac' F' @[simp, reassoc] lemma image.fac_lift (F' : mono_factorisation f) : factor_thru_image f ≫ image.lift F' = F'.e := (image.is_image f).fac_lift F' -- TODO we could put a category structure on `mono_factorisation f`, -- with the morphisms being `g : I ⟶ I'` commuting with the `m`s -- (they then automatically commute with the `e`s) -- and show that an `image_of f` gives an initial object there -- (uniqueness of the lift comes for free). instance lift_mono (F' : mono_factorisation f) : mono (image.lift F') := begin split, intros Z a b w, have w' : a ≫ image.ι f = b ≫ image.ι f := calc a ≫ image.ι f = a ≫ (image.lift F' ≫ F'.m) : by simp ... = (a ≫ image.lift F') ≫ F'.m : by rw [category.assoc] ... = (b ≫ image.lift F') ≫ F'.m : by rw w ... = b ≫ (image.lift F' ≫ F'.m) : by rw [←category.assoc] ... = b ≫ image.ι f : by simp, exact (cancel_mono (image.ι f)).1 w', end lemma has_image.uniq (F' : mono_factorisation f) (l : image f ⟶ F'.I) (w : l ≫ F'.m = image.ι f) : l = image.lift F' := (cancel_mono F'.m).1 (by simp [w]) end section variables (C) /-- `has_images` represents a choice of image for every morphism -/ class has_images := (has_image : Π {X Y : C} (f : X ⟶ Y), has_image f) attribute [instance, priority 100] has_images.has_image end section variables (f) [has_image f] /-- The image of a monomorphism is isomorphic to the source. -/ def image_mono_iso_source [mono f] : image f ≅ X := is_image.iso_ext (image.is_image f) (is_image.self f) @[simp, reassoc] lemma image_mono_iso_source_inv_ι [mono f] : (image_mono_iso_source f).inv ≫ image.ι f = f := by simp [image_mono_iso_source] @[simp, reassoc] lemma image_mono_iso_source_hom_self [mono f] : (image_mono_iso_source f).hom ≫ f = image.ι f := begin conv { to_lhs, congr, skip, rw ←image_mono_iso_source_inv_ι f, }, rw [←category.assoc, iso.hom_inv_id, category.id_comp], end -- This is the proof that `factor_thru_image f` is an epimorphism -- from https://en.wikipedia.org/wiki/Image_(category_theory), which is in turn taken from: -- Mitchell, Barry (1965), Theory of categories, MR 0202787, p.12, Proposition 10.1 @[ext] lemma image.ext {W : C} {g h : image f ⟶ W} [has_limit (parallel_pair g h)] (w : factor_thru_image f ≫ g = factor_thru_image f ≫ h) : g = h := begin let q := equalizer.ι g h, let e' := equalizer.lift _ w, let F' : mono_factorisation f := { I := equalizer g h, m := q ≫ image.ι f, m_mono := by apply mono_comp, e := e' }, let v := image.lift F', have t₀ : v ≫ q ≫ image.ι f = image.ι f := image.lift_fac F', have t : v ≫ q = 𝟙 (image f) := (cancel_mono_id (image.ι f)).1 (by { convert t₀ using 1, rw category.assoc }), -- The proof from wikipedia next proves `q ≫ v = 𝟙 _`, -- and concludes that `equalizer g h ≅ image f`, -- but this isn't necessary. calc g = 𝟙 (image f) ≫ g : by rw [category.id_comp] ... = v ≫ q ≫ g : by rw [←t, category.assoc] ... = v ≫ q ≫ h : by rw [equalizer.condition g h] ... = 𝟙 (image f) ≫ h : by rw [←category.assoc, t] ... = h : by rw [category.id_comp] end instance [Π {Z : C} (g h : image f ⟶ Z), has_limit (parallel_pair g h)] : epi (factor_thru_image f) := ⟨λ Z g h w, image.ext f w⟩ lemma epi_image_of_epi {X Y : C} (f : X ⟶ Y) [has_image f] [E : epi f] : epi (image.ι f) := begin rw ←image.fac f at E, resetI, exact epi_of_epi (factor_thru_image f) (image.ι f), end lemma epi_of_epi_image {X Y : C} (f : X ⟶ Y) [has_image f] [epi (image.ι f)] [epi (factor_thru_image f)] : epi f := by { rw [←image.fac f], apply epi_comp, } end section variables {f} {f' : X ⟶ Y} [has_image f] [has_image f'] /-- An equation between morphisms gives a comparison map between the images (which momentarily we prove is an iso). -/ def image.eq_to_hom (h : f = f') : image f ⟶ image f' := image.lift { I := image f', m := image.ι f', e := factor_thru_image f', }. instance (h : f = f') : is_iso (image.eq_to_hom h) := { inv := image.eq_to_hom h.symm, hom_inv_id' := (cancel_mono (image.ι f)).1 (by simp [image.eq_to_hom]), inv_hom_id' := (cancel_mono (image.ι f')).1 (by simp [image.eq_to_hom]), } /-- An equation between morphisms gives an isomorphism between the images. -/ def image.eq_to_iso (h : f = f') : image f ≅ image f' := as_iso (image.eq_to_hom h) /-- As long as the category has equalizers, the image inclusion maps commute with `image.eq_to_iso`. -/ lemma image.eq_fac [has_equalizers C] (h : f = f') : image.ι f = (image.eq_to_iso h).hom ≫ image.ι f' := by { ext, simp [image.eq_to_iso, image.eq_to_hom], } end section variables {Z : C} (g : Y ⟶ Z) /-- The comparison map `image (f ≫ g) ⟶ image g`. -/ def image.pre_comp [has_image g] [has_image (f ≫ g)] : image (f ≫ g) ⟶ image g := image.lift { I := image g, m := image.ι g, e := f ≫ factor_thru_image g } /-- The two step comparison map `image (f ≫ (g ≫ h)) ⟶ image (g ≫ h) ⟶ image h` agrees with the one step comparison map `image (f ≫ (g ≫ h)) ≅ image ((f ≫ g) ≫ h) ⟶ image h`. -/ lemma image.pre_comp_comp {W : C} (h : Z ⟶ W) [has_image (g ≫ h)] [has_image (f ≫ g ≫ h)] [has_image h] [has_image ((f ≫ g) ≫ h)] : image.pre_comp f (g ≫ h) ≫ image.pre_comp g h = image.eq_to_hom (category.assoc f g h).symm ≫ (image.pre_comp (f ≫ g) h) := begin apply (cancel_mono (image.ι h)).1, simp [image.pre_comp, image.eq_to_hom], end -- Note that in general we don't have the other comparison map you might expect -- `image f ⟶ image (f ≫ g)`. end end category_theory.limits namespace category_theory.limits variables {C : Type u} [category.{v} C] section instance {X Y : C} (f : X ⟶ Y) [has_image f] : has_image (arrow.mk f).hom := show has_image f, by apply_instance end section has_image_map /-- An image map is a morphism `image f → image g` fitting into a commutative square and satisfying the obvious commutativity conditions. -/ class has_image_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) := (map : image f.hom ⟶ image g.hom) (map_ι' : map ≫ image.ι g.hom = image.ι f.hom ≫ sq.right . obviously) restate_axiom has_image_map.map_ι' attribute [simp, reassoc] has_image_map.map_ι @[simp, reassoc] lemma has_image_map.factor_map {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) [has_image_map sq] : factor_thru_image f.hom ≫ has_image_map.map sq = sq.left ≫ factor_thru_image g.hom := (cancel_mono (image.ι g.hom)).1 $ by simp [arrow.w] variables {f g : arrow C} [has_image f.hom] [has_image g.hom] (sq : f ⟶ g) section local attribute [ext] has_image_map instance : subsingleton (has_image_map sq) := subsingleton.intro $ λ a b, has_image_map.ext a b $ (cancel_mono (image.ι g.hom)).1 $ by simp only [has_image_map.map_ι] end variable [has_image_map sq] /-- The map on images induced by a commutative square. -/ abbreviation image.map : image f.hom ⟶ image g.hom := has_image_map.map sq lemma image.factor_map : factor_thru_image f.hom ≫ image.map sq = sq.left ≫ factor_thru_image g.hom := by simp lemma image.map_ι : image.map sq ≫ image.ι g.hom = image.ι f.hom ≫ sq.right := by simp lemma image.map_hom_mk'_ι {X Y P Q : C} {k : X ⟶ Y} [has_image k] {l : P ⟶ Q} [has_image l] {m : X ⟶ P} {n : Y ⟶ Q} (w : m ≫ l = k ≫ n) [has_image_map (arrow.hom_mk' w)] : image.map (arrow.hom_mk' w) ≫ image.ι l = image.ι k ≫ n := image.map_ι _ section variables {h : arrow C} [has_image h.hom] (sq' : g ⟶ h) variables [has_image_map sq'] /-- Image maps for composable commutative squares induce an image map in the composite square. -/ def has_image_map_comp : has_image_map (sq ≫ sq') := { map := image.map sq ≫ image.map sq' } @[simp] lemma image.map_comp [has_image_map (sq ≫ sq')] : image.map (sq ≫ sq') = image.map sq ≫ image.map sq' := show (has_image_map.map (sq ≫ sq')) = (has_image_map_comp sq sq').map, by congr end section variables (f) /-- The identity `image f ⟶ image f` fits into the commutative square represented by the identity morphism `𝟙 f` in the arrow category. -/ def has_image_map_id : has_image_map (𝟙 f) := { map := 𝟙 (image f.hom) } @[simp] lemma image.map_id [has_image_map (𝟙 f)] : image.map (𝟙 f) = 𝟙 (image f.hom) := show (image.map (𝟙 f)) = (has_image_map_id f).map, by congr end end has_image_map section variables (C) [has_images C] /-- If a category `has_image_maps`, then all commutative squares induce morphisms on images. -/ class has_image_maps := (has_image_map : Π {f g : arrow C} (st : f ⟶ g), has_image_map st) attribute [instance, priority 100] has_image_maps.has_image_map end section has_image_maps variables [has_images C] [has_image_maps C] /-- The functor from the arrow category of `C` to `C` itself that maps a morphism to its image and a commutative square to the induced morphism on images. -/ @[simps] def im : arrow C ⥤ C := { obj := λ f, image f.hom, map := λ _ _ st, image.map st } end has_image_maps section strong_epi_mono_factorisation /-- A strong epi-mono factorisation is a decomposition `f = e ≫ m` with `e` a strong epimorphism and `m` a monomorphism. -/ structure strong_epi_mono_factorisation {X Y : C} (f : X ⟶ Y) extends mono_factorisation f := [e_strong_epi : strong_epi e] attribute [instance] strong_epi_mono_factorisation.e_strong_epi /-- Satisfying the inhabited linter -/ instance strong_epi_mono_factorisation_inhabited {X Y : C} (f : X ⟶ Y) [strong_epi f] : inhabited (strong_epi_mono_factorisation f) := ⟨⟨⟨Y, 𝟙 Y, f, by simp⟩⟩⟩ /-- A mono factorisation coming from a strong epi-mono factorisation always has the universal property of the image. -/ def strong_epi_mono_factorisation.to_mono_is_image {X Y : C} {f : X ⟶ Y} (F : strong_epi_mono_factorisation f) : is_image F.to_mono_factorisation := { lift := λ G, arrow.lift $ arrow.hom_mk' $ show G.e ≫ G.m = F.e ≫ F.m, by rw [F.to_mono_factorisation.fac, G.fac] } variable (C) /-- A category has strong epi-mono factorisations if every morphism admits a strong epi-mono factorisation. -/ class has_strong_epi_mono_factorisations := (has_fac : Π {X Y : C} (f : X ⟶ Y), strong_epi_mono_factorisation f) @[priority 100] instance has_images_of_has_strong_epi_mono_factorisations [has_strong_epi_mono_factorisations C] : has_images C := { has_image := λ X Y f, let F' := has_strong_epi_mono_factorisations.has_fac f in { F := F'.to_mono_factorisation, is_image := F'.to_mono_is_image } } end strong_epi_mono_factorisation section has_strong_epi_images variables (C) [has_images C] /-- A category has strong epi images if it has all images and `factor_thru_image f` is a strong epimorphism for all `f`. -/ class has_strong_epi_images := (strong_factor_thru_image : Π {X Y : C} (f : X ⟶ Y), strong_epi (factor_thru_image f)) attribute [instance] has_strong_epi_images.strong_factor_thru_image end has_strong_epi_images section has_strong_epi_images /-- If we constructed our images from strong epi-mono factorisations, then these images are strong epi images. -/ @[priority 100] instance has_strong_epi_images_of_has_strong_epi_mono_factorisations [has_strong_epi_mono_factorisations C] : has_strong_epi_images C := { strong_factor_thru_image := λ X Y f, (has_strong_epi_mono_factorisations.has_fac f).e_strong_epi } end has_strong_epi_images section has_strong_epi_images variables [has_images C] /-- A category with strong epi images has image maps. The construction is taken from Borceux, Handbook of Categorical Algebra 1, Proposition 4.4.5. -/ @[priority 100] instance has_image_maps_of_has_strong_epi_images [has_strong_epi_images C] : has_image_maps C := { has_image_map := λ f g st, let I := image (image.ι f.hom ≫ st.right) in let I' := image (st.left ≫ factor_thru_image g.hom) in let upper : strong_epi_mono_factorisation (f.hom ≫ st.right) := { I := I, e := factor_thru_image f.hom ≫ factor_thru_image (image.ι f.hom ≫ st.right), m := image.ι (image.ι f.hom ≫ st.right), e_strong_epi := strong_epi_comp _ _, m_mono := by apply_instance } in let lower : strong_epi_mono_factorisation (f.hom ≫ st.right) := { I := I', e := factor_thru_image (st.left ≫ factor_thru_image g.hom), m := image.ι (st.left ≫ factor_thru_image g.hom) ≫ image.ι g.hom, fac' := by simp [arrow.w], e_strong_epi := by apply_instance, m_mono := mono_comp _ _ } in let s : I ⟶ I' := is_image.lift upper.to_mono_is_image lower.to_mono_factorisation in { map := factor_thru_image (image.ι f.hom ≫ st.right) ≫ s ≫ image.ι (st.left ≫ factor_thru_image g.hom), map_ι' := by rw [category.assoc, category.assoc, is_image.lift_fac upper.to_mono_is_image lower.to_mono_factorisation, image.fac] } } end has_strong_epi_images variables [has_strong_epi_mono_factorisations.{v} C] variables {X Y : C} {f : X ⟶ Y} /-- If `C` has strong epi mono factorisations, then the image is unique up to isomorphism, in that if `f` factors as a strong epi followed by a mono, this factorisation is essentially the image factorisation. -/ def image.iso_strong_epi_mono {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : I' ≅ image f := is_image.iso_ext {strong_epi_mono_factorisation . I := I', m := m, e := e}.to_mono_is_image (image.is_image f) @[simp] lemma image.iso_strong_epi_mono_hom_comp_ι {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : (image.iso_strong_epi_mono e m comm).hom ≫ image.ι f = m := is_image.lift_fac _ _ @[simp] lemma image.iso_strong_epi_mono_inv_comp_mono {I' : C} (e : X ⟶ I') (m : I' ⟶ Y) (comm : e ≫ m = f) [strong_epi e] [mono m] : (image.iso_strong_epi_mono e m comm).inv ≫ m = image.ι f := image.lift_fac _ end category_theory.limits
08d009cb0c9458f90165e016ba9ce595abb268e1
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/order/rel_classes.lean
cb15319e6a830205bd61ff67090ee19ba0d35d0c
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
17,532
lean
/- Copyright (c) 2020 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Mario Carneiro, Yury G. Kudryashov -/ import data.set.basic /-! # Unbundled relation classes In this file we prove some properties of `is_*` classes defined in `init.algebra.classes`. The main difference between these classes and the usual order classes (`preorder` etc) is that usual classes extend `has_le` and/or `has_lt` while these classes take a relation as an explicit argument. -/ universes u v variables {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} open function theorem is_refl.swap (r) [is_refl α r] : is_refl α (swap r) := ⟨refl_of r⟩ theorem is_irrefl.swap (r) [is_irrefl α r] : is_irrefl α (swap r) := ⟨irrefl_of r⟩ theorem is_trans.swap (r) [is_trans α r] : is_trans α (swap r) := ⟨λ a b c h₁ h₂, trans_of r h₂ h₁⟩ theorem is_antisymm.swap (r) [is_antisymm α r] : is_antisymm α (swap r) := ⟨λ a b h₁ h₂, antisymm h₂ h₁⟩ theorem is_asymm.swap (r) [is_asymm α r] : is_asymm α (swap r) := ⟨λ a b h₁ h₂, asymm_of r h₂ h₁⟩ theorem is_total.swap (r) [is_total α r] : is_total α (swap r) := ⟨λ a b, (total_of r a b).swap⟩ theorem is_trichotomous.swap (r) [is_trichotomous α r] : is_trichotomous α (swap r) := ⟨λ a b, by simpa [swap, or.comm, or.left_comm] using trichotomous_of r a b⟩ theorem is_preorder.swap (r) [is_preorder α r] : is_preorder α (swap r) := {..@is_refl.swap α r _, ..@is_trans.swap α r _} theorem is_strict_order.swap (r) [is_strict_order α r] : is_strict_order α (swap r) := {..@is_irrefl.swap α r _, ..@is_trans.swap α r _} theorem is_partial_order.swap (r) [is_partial_order α r] : is_partial_order α (swap r) := {..@is_preorder.swap α r _, ..@is_antisymm.swap α r _} theorem is_total_preorder.swap (r) [is_total_preorder α r] : is_total_preorder α (swap r) := {..@is_preorder.swap α r _, ..@is_total.swap α r _} theorem is_linear_order.swap (r) [is_linear_order α r] : is_linear_order α (swap r) := {..@is_partial_order.swap α r _, ..@is_total.swap α r _} protected theorem is_asymm.is_antisymm (r) [is_asymm α r] : is_antisymm α r := ⟨λ x y h₁ h₂, (asymm h₁ h₂).elim⟩ protected theorem is_asymm.is_irrefl [is_asymm α r] : is_irrefl α r := ⟨λ a h, asymm h h⟩ /- Convert algebraic structure style to explicit relation style typeclasses -/ instance [preorder α] : is_refl α (≤) := ⟨le_refl⟩ instance [preorder α] : is_refl α (≥) := is_refl.swap _ instance [preorder α] : is_trans α (≤) := ⟨@le_trans _ _⟩ instance [preorder α] : is_trans α (≥) := is_trans.swap _ instance [preorder α] : is_preorder α (≤) := {} instance [preorder α] : is_preorder α (≥) := {} instance [preorder α] : is_irrefl α (<) := ⟨lt_irrefl⟩ instance [preorder α] : is_irrefl α (>) := is_irrefl.swap _ instance [preorder α] : is_trans α (<) := ⟨@lt_trans _ _⟩ instance [preorder α] : is_trans α (>) := is_trans.swap _ instance [preorder α] : is_asymm α (<) := ⟨@lt_asymm _ _⟩ instance [preorder α] : is_asymm α (>) := is_asymm.swap _ instance [preorder α] : is_antisymm α (<) := is_asymm.is_antisymm _ instance [preorder α] : is_antisymm α (>) := is_asymm.is_antisymm _ instance [preorder α] : is_strict_order α (<) := {} instance [preorder α] : is_strict_order α (>) := {} instance preorder.is_total_preorder [preorder α] [is_total α (≤)] : is_total_preorder α (≤) := {} instance [partial_order α] : is_antisymm α (≤) := ⟨@le_antisymm _ _⟩ instance [partial_order α] : is_antisymm α (≥) := is_antisymm.swap _ instance [partial_order α] : is_partial_order α (≤) := {} instance [partial_order α] : is_partial_order α (≥) := {} instance [linear_order α] : is_total α (≤) := ⟨le_total⟩ instance [linear_order α] : is_total α (≥) := is_total.swap _ instance linear_order.is_total_preorder [linear_order α] : is_total_preorder α (≤) := by apply_instance instance [linear_order α] : is_total_preorder α (≥) := {} instance [linear_order α] : is_linear_order α (≤) := {} instance [linear_order α] : is_linear_order α (≥) := {} instance [linear_order α] : is_trichotomous α (<) := ⟨lt_trichotomy⟩ instance [linear_order α] : is_trichotomous α (>) := is_trichotomous.swap _ lemma trans_trichotomous_left [is_trans α r] [is_trichotomous α r] {a b c : α} : ¬r b a → r b c → r a c := begin intros h₁ h₂, rcases trichotomous_of r a b with h₃|h₃|h₃, exact trans h₃ h₂, rw h₃, exact h₂, exfalso, exact h₁ h₃ end lemma trans_trichotomous_right [is_trans α r] [is_trichotomous α r] {a b c : α} : r a b → ¬r c b → r a c := begin intros h₁ h₂, rcases trichotomous_of r b c with h₃|h₃|h₃, exact trans h₁ h₃, rw ←h₃, exact h₁, exfalso, exact h₂ h₃ end /-- Construct a partial order from a `is_strict_order` relation -/ def partial_order_of_SO (r) [is_strict_order α r] : partial_order α := { le := λ x y, x = y ∨ r x y, lt := r, le_refl := λ x, or.inl rfl, le_trans := λ x y z h₁ h₂, match y, z, h₁, h₂ with | _, _, or.inl rfl, h₂ := h₂ | _, _, h₁, or.inl rfl := h₁ | _, _, or.inr h₁, or.inr h₂ := or.inr (trans h₁ h₂) end, le_antisymm := λ x y h₁ h₂, match y, h₁, h₂ with | _, or.inl rfl, h₂ := rfl | _, h₁, or.inl rfl := rfl | _, or.inr h₁, or.inr h₂ := (asymm h₁ h₂).elim end, lt_iff_le_not_le := λ x y, ⟨λ h, ⟨or.inr h, not_or (λ e, by rw e at h; exact irrefl _ h) (asymm h)⟩, λ ⟨h₁, h₂⟩, h₁.resolve_left (λ e, h₂ $ e ▸ or.inl rfl)⟩ } section prio set_option default_priority 100 -- see Note [default priority] /-- This is basically the same as `is_strict_total_order`, but that definition is in Type (probably by mistake) and also has redundant assumptions. -/ @[algebra] class is_strict_total_order' (α : Type u) (lt : α → α → Prop) extends is_trichotomous α lt, is_strict_order α lt : Prop. end prio /-- Construct a linear order from a `is_strict_total_order'` relation -/ def linear_order_of_STO' (r) [is_strict_total_order' α r] : linear_order α := { le_total := λ x y, match y, trichotomous_of r x y with | y, or.inl h := or.inl (or.inr h) | _, or.inr (or.inl rfl) := or.inl (or.inl rfl) | _, or.inr (or.inr h) := or.inr (or.inr h) end, ..partial_order_of_SO r } /-- Construct a decidable linear order from a `is_strict_total_order'` relation -/ def decidable_linear_order_of_STO' (r) [is_strict_total_order' α r] [decidable_rel r] : decidable_linear_order α := by letI LO := linear_order_of_STO' r; exact { decidable_le := λ x y, decidable_of_iff (¬ r y x) (@not_lt _ _ y x), ..LO } theorem is_strict_total_order'.swap (r) [is_strict_total_order' α r] : is_strict_total_order' α (swap r) := {..is_trichotomous.swap r, ..is_strict_order.swap r} instance [linear_order α] : is_strict_total_order' α (<) := {} /-- A connected order is one satisfying the condition `a < c → a < b ∨ b < c`. This is recognizable as an intuitionistic substitute for `a ≤ b ∨ b ≤ a` on the constructive reals, and is also known as negative transitivity, since the contrapositive asserts transitivity of the relation `¬ a < b`. -/ @[algebra] class is_order_connected (α : Type u) (lt : α → α → Prop) : Prop := (conn : ∀ a b c, lt a c → lt a b ∨ lt b c) theorem is_order_connected.neg_trans {r : α → α → Prop} [is_order_connected α r] {a b c} (h₁ : ¬ r a b) (h₂ : ¬ r b c) : ¬ r a c := mt (is_order_connected.conn a b c) $ by simp [h₁, h₂] theorem is_strict_weak_order_of_is_order_connected [is_asymm α r] [is_order_connected α r] : is_strict_weak_order α r := { trans := λ a b c h₁ h₂, (is_order_connected.conn _ c _ h₁).resolve_right (asymm h₂), incomp_trans := λ a b c ⟨h₁, h₂⟩ ⟨h₃, h₄⟩, ⟨is_order_connected.neg_trans h₁ h₃, is_order_connected.neg_trans h₄ h₂⟩, ..@is_asymm.is_irrefl α r _ } @[priority 100] -- see Note [lower instance priority] instance is_order_connected_of_is_strict_total_order' [is_strict_total_order' α r] : is_order_connected α r := ⟨λ a b c h, (trichotomous _ _).imp_right (λ o, o.elim (λ e, e ▸ h) (λ h', trans h' h))⟩ @[priority 100] -- see Note [lower instance priority] instance is_strict_total_order_of_is_strict_total_order' [is_strict_total_order' α r] : is_strict_total_order α r := {..is_strict_weak_order_of_is_order_connected} instance [linear_order α] : is_strict_total_order α (<) := by apply_instance instance [linear_order α] : is_order_connected α (<) := by apply_instance instance [linear_order α] : is_incomp_trans α (<) := by apply_instance instance [linear_order α] : is_strict_weak_order α (<) := by apply_instance /-- An extensional relation is one in which an element is determined by its set of predecessors. It is named for the `x ∈ y` relation in set theory, whose extensionality is one of the first axioms of ZFC. -/ @[algebra] class is_extensional (α : Type u) (r : α → α → Prop) : Prop := (ext : ∀ a b, (∀ x, r x a ↔ r x b) → a = b) @[priority 100] -- see Note [lower instance priority] instance is_extensional_of_is_strict_total_order' [is_strict_total_order' α r] : is_extensional α r := ⟨λ a b H, ((@trichotomous _ r _ a b) .resolve_left $ mt (H _).2 (irrefl a)) .resolve_right $ mt (H _).1 (irrefl b)⟩ section prio set_option default_priority 100 -- see Note [default priority] /-- A well order is a well-founded linear order. -/ @[algebra] class is_well_order (α : Type u) (r : α → α → Prop) extends is_strict_total_order' α r : Prop := (wf : well_founded r) end prio @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_strict_total_order {α} (r : α → α → Prop) [is_well_order α r] : is_strict_total_order α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_extensional {α} (r : α → α → Prop) [is_well_order α r] : is_extensional α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_trichotomous {α} (r : α → α → Prop) [is_well_order α r] : is_trichotomous α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_trans {α} (r : α → α → Prop) [is_well_order α r] : is_trans α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_irrefl {α} (r : α → α → Prop) [is_well_order α r] : is_irrefl α r := by apply_instance @[priority 100] -- see Note [lower instance priority] instance is_well_order.is_asymm {α} (r : α → α → Prop) [is_well_order α r] : is_asymm α r := by apply_instance /-- Construct a decidable linear order from a well-founded linear order. -/ noncomputable def is_well_order.decidable_linear_order (r : α → α → Prop) [is_well_order α r] : decidable_linear_order α := by { haveI := linear_order_of_STO' r, exact classical.DLO α } instance empty_relation.is_well_order [subsingleton α] : is_well_order α empty_relation := { trichotomous := λ a b, or.inr $ or.inl $ subsingleton.elim _ _, irrefl := λ a, id, trans := λ a b c, false.elim, wf := ⟨λ a, ⟨_, λ y, false.elim⟩⟩ } instance nat.lt.is_well_order : is_well_order ℕ (<) := ⟨nat.lt_wf⟩ instance sum.lex.is_well_order [is_well_order α r] [is_well_order β s] : is_well_order (α ⊕ β) (sum.lex r s) := { trichotomous := λ a b, by cases a; cases b; simp; apply trichotomous, irrefl := λ a, by cases a; simp; apply irrefl, trans := λ a b c, by cases a; cases b; simp; cases c; simp; apply trans, wf := sum.lex_wf is_well_order.wf is_well_order.wf } instance prod.lex.is_well_order [is_well_order α r] [is_well_order β s] : is_well_order (α × β) (prod.lex r s) := { trichotomous := λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩, match @trichotomous _ r _ a₁ b₁ with | or.inl h₁ := or.inl $ prod.lex.left _ _ h₁ | or.inr (or.inr h₁) := or.inr $ or.inr $ prod.lex.left _ _ h₁ | or.inr (or.inl e) := e ▸ match @trichotomous _ s _ a₂ b₂ with | or.inl h := or.inl $ prod.lex.right _ h | or.inr (or.inr h) := or.inr $ or.inr $ prod.lex.right _ h | or.inr (or.inl e) := e ▸ or.inr $ or.inl rfl end end, irrefl := λ ⟨a₁, a₂⟩ h, by cases h with _ _ _ _ h _ _ _ h; [exact irrefl _ h, exact irrefl _ h], trans := λ a b c h₁ h₂, begin cases h₁ with a₁ a₂ b₁ b₂ ab a₁ b₁ b₂ ab; cases h₂ with _ _ c₁ c₂ bc _ _ c₂ bc, { exact prod.lex.left _ _ (trans ab bc) }, { exact prod.lex.left _ _ ab }, { exact prod.lex.left _ _ bc }, { exact prod.lex.right _ (trans ab bc) } end, wf := prod.lex_wf is_well_order.wf is_well_order.wf } /-- An unbounded or cofinal set -/ def unbounded (r : α → α → Prop) (s : set α) : Prop := ∀ a, ∃ b ∈ s, ¬ r b a /-- A bounded or final set -/ def bounded (r : α → α → Prop) (s : set α) : Prop := ∃a, ∀ b ∈ s, r b a @[simp] lemma not_bounded_iff {r : α → α → Prop} (s : set α) : ¬bounded r s ↔ unbounded r s := begin classical, simp only [bounded, unbounded, not_forall, not_exists, exists_prop, not_and, not_not] end @[simp] lemma not_unbounded_iff {r : α → α → Prop} (s : set α) : ¬unbounded r s ↔ bounded r s := by { classical, rw [not_iff_comm, not_bounded_iff] } namespace well_founded /-- If `r` is a well-founded relation, then any nonempty set has a minimal element with respect to `r`. -/ theorem has_min {α} {r : α → α → Prop} (H : well_founded r) (s : set α) : s.nonempty → ∃ a ∈ s, ∀ x ∈ s, ¬ r x a | ⟨a, ha⟩ := (acc.rec_on (H.apply a) $ λ x _ IH, not_imp_not.1 $ λ hne hx, hne $ ⟨x, hx, λ y hy hyx, hne $ IH y hyx hy⟩) ha /-- A minimal element of a nonempty set in a well-founded order -/ noncomputable def min {α} {r : α → α → Prop} (H : well_founded r) (p : set α) (h : p.nonempty) : α := classical.some (H.has_min p h) theorem min_mem {α} {r : α → α → Prop} (H : well_founded r) (p : set α) (h : p.nonempty) : H.min p h ∈ p := let ⟨h, _⟩ := classical.some_spec (H.has_min p h) in h theorem not_lt_min {α} {r : α → α → Prop} (H : well_founded r) (p : set α) (h : p.nonempty) {x} (xp : x ∈ p) : ¬ r x (H.min p h) := let ⟨_, h'⟩ := classical.some_spec (H.has_min p h) in h' _ xp theorem well_founded_iff_has_min {α} {r : α → α → Prop} : (well_founded r) ↔ ∀ (p : set α), p.nonempty → ∃ m ∈ p, ∀ x ∈ p, ¬ r x m := begin classical, split, { exact has_min, }, { set counterexamples := { x : α | ¬ acc r x}, intro exists_max, fconstructor, intro x, by_contra hx, obtain ⟨m, m_mem, hm⟩ := exists_max counterexamples ⟨x, hx⟩, refine m_mem (acc.intro _ ( λ y y_gt_m, _)), by_contra hy, exact hm y hy y_gt_m, }, end lemma eq_iff_not_lt_of_le {α} [partial_order α] {x y : α} : x ≤ y → y = x ↔ ¬ x < y := begin split, { intros xle nge, cases le_not_le_of_lt nge, rw xle left at nge, exact lt_irrefl x nge }, { intros ngt xle, contrapose! ngt, exact lt_of_le_of_ne xle (ne.symm ngt) } end theorem well_founded_iff_has_max' [partial_order α] : (well_founded ((>) : α → α → Prop) ↔ ∀ (p : set α), p.nonempty → ∃ m ∈ p, ∀ x ∈ p, m ≤ x → x = m) := by simp only [eq_iff_not_lt_of_le, well_founded_iff_has_min] theorem well_founded_iff_has_min' [partial_order α] : (well_founded (has_lt.lt : α → α → Prop)) ↔ ∀ (p : set α), p.nonempty → ∃ m ∈ p, ∀ x ∈ p, x ≤ m → x = m := @well_founded_iff_has_max' (order_dual α) _ open set /-- The supremum of a bounded, well-founded order -/ protected noncomputable def sup {α} {r : α → α → Prop} (wf : well_founded r) (s : set α) (h : bounded r s) : α := wf.min { x | ∀a ∈ s, r a x } h protected lemma lt_sup {α} {r : α → α → Prop} (wf : well_founded r) {s : set α} (h : bounded r s) {x} (hx : x ∈ s) : r x (wf.sup s h) := min_mem wf { x | ∀a ∈ s, r a x } h x hx section open_locale classical /-- A successor of an element `x` in a well-founded order is a minimal element `y` such that `x < y` if one exists. Otherwise it is `x` itself. -/ protected noncomputable def succ {α} {r : α → α → Prop} (wf : well_founded r) (x : α) : α := if h : ∃y, r x y then wf.min { y | r x y } h else x protected lemma lt_succ {α} {r : α → α → Prop} (wf : well_founded r) {x : α} (h : ∃y, r x y) : r x (wf.succ x) := by { rw [well_founded.succ, dif_pos h], apply min_mem } end protected lemma lt_succ_iff {α} {r : α → α → Prop} [wo : is_well_order α r] {x : α} (h : ∃y, r x y) (y : α) : r y (wo.wf.succ x) ↔ r y x ∨ y = x := begin split, { intro h', have : ¬r x y, { intro hy, rw [well_founded.succ, dif_pos] at h', exact wo.wf.not_lt_min _ h hy h' }, rcases trichotomous_of r x y with hy | hy | hy, exfalso, exact this hy, right, exact hy.symm, left, exact hy }, rintro (hy | rfl), exact trans hy (wo.wf.lt_succ h), exact wo.wf.lt_succ h end end well_founded
6adbc4893b2e6f80660b060f9c09a6048c159db4
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/tests/lean/run/nondet.lean
b566163cd2d697f510a89a62539788da0a45b0d9
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
1,290
lean
import Std.Control.Nondet open Std namespace ex1 -- The state is not backtrackable abbrev M := NondetT $ StateT Nat $ IO def checkVal (x : Nat) : M Nat := do IO.println s!"x: {x}" guard (x % 2 == 0) pure x def f : M Nat := do let x ← NondetT.choose [1, 2, 3, 4, 5, 6, 7, 8] checkVal x def h : M Nat := do let a ← f modify (· + 1) guard (a % 3 == 0) pure (a+1) def g : IO Unit := do let (some (x, _), s) ← h.run.run 0 | throw $ IO.userError "failed" IO.println s!"x: {x}, s: {s}" #eval g end ex1 namespace ex2 -- The state is backtrackable abbrev M := StateT Nat $ NondetT $ IO def checkVal (x : Nat) : M Nat := do IO.println s!"x: {x}" guard (x % 2 == 0) pure x def f : M Nat := do let x ← NondetT.choose [1, 2, 3, 4, 5, 6, 7, 8] checkVal x def h : M Nat := do let a ← f modify (· + 1) guard (a % 3 == 0) pure (a+1) def g : IO Unit := do let some ((x, s), _) ← (h.run 0).run | throw $ IO.userError "failed" IO.println s!"x: {x}, s: {s}" #eval g end ex2 namespace ex3 abbrev M := NondetT IO def a : M Unit := IO.println "a" def b : M Unit := IO.println "b" def c : M Unit := IO.println "c" def t1 : M Unit := ((a <|> a) *> b) *> c def t2 : M Unit := (a <|> a) *> (b *> c) #eval t1.run' #eval t2.run' end ex3
2ccb44bec25be7a97f0a816161e9876b414c630b
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/qpf/multivariate/constructions/quot.lean
0869d40073f28f88a960d59500d15d9a5b44735b
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
2,400
lean
/- Copyright (c) 2018 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Simon Hudon -/ import data.qpf.multivariate.basic /-! # The quotient of QPF is itself a QPF > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. The quotients are here defined using a surjective function and its right inverse. They are very similar to the `abs` and `repr` functions found in the definition of `mvqpf` -/ universes u open_locale mvfunctor namespace mvqpf variables {n : ℕ} variables {F : typevec.{u} n → Type u} section repr variables [mvfunctor F] [q : mvqpf F] variables {G : typevec.{u} n → Type u} [mvfunctor G] variable {FG_abs : Π {α}, F α → G α} variable {FG_repr : Π {α}, G α → F α} /-- If `F` is a QPF then `G` is a QPF as well. Can be used to construct `mvqpf` instances by transporting them across surjective functions -/ def quotient_qpf (FG_abs_repr : Π {α} (x : G α), FG_abs (FG_repr x) = x) (FG_abs_map : ∀ {α β} (f : α ⟹ β) (x : F α), FG_abs (f <$$> x) = f <$$> FG_abs x) : mvqpf G := { P := q.P, abs := λ α p, FG_abs (abs p), repr := λ α x, repr (FG_repr x), abs_repr := λ α x, by rw [abs_repr,FG_abs_repr], abs_map := λ α β f p, by rw [abs_map,FG_abs_map] } end repr section rel variables (R : ∀ ⦃α⦄, F α → F α → Prop) /-- Functorial quotient type -/ def quot1 (α : typevec n) := quot (@R α) instance quot1.inhabited {α : typevec n} [inhabited $ F α] : inhabited (quot1 R α) := ⟨ quot.mk _ default ⟩ variables [mvfunctor F] [q : mvqpf F] variables (Hfunc : ∀ ⦃α β⦄ (a b : F α) (f : α ⟹ β), R a b → R (f <$$> a) (f <$$> b)) /-- `map` of the `quot1` functor -/ def quot1.map ⦃α β⦄ (f : α ⟹ β) : quot1.{u} R α → quot1.{u} R β := quot.lift (λ x : F α, quot.mk _ (f <$$> x : F β)) $ λ a b h, quot.sound $ Hfunc a b _ h /-- `mvfunctor` instance for `quot1` with well-behaved `R` -/ def quot1.mvfunctor : mvfunctor (quot1 R) := { map := quot1.map R Hfunc } /-- `quot1` is a qpf -/ noncomputable def rel_quot : @mvqpf _ (quot1 R) (mvqpf.quot1.mvfunctor R Hfunc) := @quotient_qpf n F _ q _ (mvqpf.quot1.mvfunctor R Hfunc) (λ α x, quot.mk _ x) (λ α, quot.out) (λ α x, quot.out_eq _) (λ α β f x, rfl) end rel end mvqpf
d5d33bda601078c5aab908570347ecde83e42374
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/tactic/scc.lean
cb5cd34cacf82b6059df1e5529d196cb94d84dd8
[ "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
13,230
lean
/- Copyright (c) 2018 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon -/ import tactic.tauto /-! # Strongly Connected Components This file defines tactics to construct proofs of equivalences between a set of mutually equivalent propositions. The tactics use implications transitively to find sets of equivalent propositions. ## Implementation notes The tactics use a strongly connected components algorithm on a graph where propositions are vertices and edges are proofs that the source implies the target. The strongly connected components are therefore sets of propositions that are pairwise equivalent to each other. The resulting strongly connected components are encoded in a disjoint set data structure to facilitate the construction of equivalence proofs between two arbitrary members of an equivalence class. ## Possible generalizations Instead of reasoning about implications and equivalence, we could generalize the machinery to reason about arbitrary partial orders. ## References * Tarjan, R. E. (1972), "Depth-first search and linear graph algorithms", SIAM Journal on Computing, 1 (2): 146–160, doi:10.1137/0201010 * Dijkstra, Edsger (1976), A Discipline of Programming, NJ: Prentice Hall, Ch. 25. * <https://en.wikipedia.org/wiki/Disjoint-set_data_structure> ## Tags graphs, tactic, strongly connected components, disjoint sets -/ namespace tactic /-- `closure` implements a disjoint set data structure using path compression optimization. For the sake of the scc algorithm, it also stores the preorder numbering of the equivalence graph of the local assumptions. The `expr_map` encodes a directed forest by storing for every non-root node, a reference to its parent and a proof of equivalence between that node's expression and its parent's expression. Given that data structure, checking that two nodes belong to the same tree is easy and fast by repeatedly following the parent references until a root is reached. If both nodes have the same root, they belong to the same tree, i.e. their expressions are equivalent. The proof of equivalence can be formed by composing the proofs along the edges of the paths to the root. More concretely, if we ignore preorder numbering, the set `{ {e₀,e₁,e₂,e₃}, {e₄,e₅} }` is represented as: ``` e₀ → ⊥ -- no parent, i.e. e₀ is a root e₁ → e₀, p₁ -- with p₁ : e₁ ↔ e₀ e₂ → e₁, p₂ -- with p₂ : e₂ ↔ e₁ e₃ → e₀, p₃ -- with p₃ : e₃ ↔ e₀ e₄ → ⊥ -- no parent, i.e. e₄ is a root e₅ → e₄, p₅ -- with p₅ : e₅ ↔ e₄ ``` We can check that `e₂` and `e₃` are equivalent by seeking the root of the tree of each. The parent of `e₂` is `e₁`, the parent of `e₁` is `e₀` and `e₀` does not have a parent, and thus, this is the root of its tree. The parent of `e₃` is `e₀` and it's also the root, the same as for `e₂` and they are therefore equivalent. We can build a proof of that equivalence by using transitivity on `p₂`, `p₁` and `p₃.symm` in that order. Similarly, we can discover that `e₂` and `e₅` aren't equivalent. A description of the path compression optimization can be found at: <https://en.wikipedia.org/wiki/Disjoint-set_data_structure#Path_compression> -/ meta def closure := ref (expr_map (ℕ ⊕ (expr × expr))) namespace closure /-- `with_new_closure f` creates an empty `closure` `c`, executes `f` on `c`, and then deletes `c`, returning the output of `f`. -/ meta def with_new_closure {α} : (closure → tactic α) → tactic α := using_new_ref (expr_map.mk _) /-- `to_tactic_format cl` pretty-prints the `closure` `cl` as a list. Assuming `cl` was built by `dfs_at`, each element corresponds to a node `pᵢ : expr` and is one of the folllowing: - if `pᵢ` is a root: `"pᵢ ⇐ i"`, where `i` is the preorder number of `pᵢ`, - otherwise: `"(pᵢ, pⱼ) : P"`, where `P` is `pᵢ ↔ pⱼ`. Useful for debugging. -/ meta def to_tactic_format (cl : closure) : tactic format := do m ← read_ref cl, let l := m.to_list, fmt ← l.mmap $ λ ⟨x,y⟩, match y with | sum.inl y := pformat!"{x} ⇐ {y}" | sum.inr ⟨y,p⟩ := pformat!"({x}, {y}) : {infer_type p}" end, pure $ to_fmt fmt meta instance : has_to_tactic_format closure := ⟨ to_tactic_format ⟩ /-- `(n,r,p) ← root cl e` returns `r` the root of the tree that `e` is a part of (which might be itself) along with `p` a proof of `e ↔ r` and `n`, the preorder numbering of the root. -/ meta def root (cl : closure) : expr → tactic (ℕ × expr × expr) | e := do m ← read_ref cl, match m.find e with | none := do p ← mk_app ``iff.refl [e], pure (0,e,p) | (some (sum.inl n)) := do p ← mk_app ``iff.refl [e], pure (n,e,p) | (some (sum.inr (e₀,p₀))) := do (n,e₁,p₁) ← root e₀, p ← mk_app ``iff.trans [p₀,p₁], modify_ref cl $ λ m, m.insert e (sum.inr (e₁,p)), pure (n,e₁,p) end /-- (Implementation of `merge`.) -/ meta def merge_intl (cl : closure) (p e₀ p₀ e₁ p₁ : expr) : tactic unit := do p₂ ← mk_app ``iff.symm [p₀], p ← mk_app ``iff.trans [p₂,p], p ← mk_app ``iff.trans [p,p₁], modify_ref cl $ λ m, m.insert e₀ $ sum.inr (e₁,p) /-- `merge cl p`, with `p` a proof of `e₀ ↔ e₁` for some `e₀` and `e₁`, merges the trees of `e₀` and `e₁` and keeps the root with the smallest preorder number as the root. This ensures that, in the depth-first traversal of the graph, when encountering an edge going into a vertex whose equivalence class includes a vertex that originated the current search, that vertex will be the root of the corresponding tree. -/ meta def merge (cl : closure) (p : expr) : tactic unit := do `(%%e₀ ↔ %%e₁) ← infer_type p >>= instantiate_mvars, (n₂,e₂,p₂) ← root cl e₀, (n₃,e₃,p₃) ← root cl e₁, if e₂ ≠ e₃ then do if n₂ < n₃ then do p ← mk_app ``iff.symm [p], cl.merge_intl p e₃ p₃ e₂ p₂ else cl.merge_intl p e₂ p₂ e₃ p₃ else pure () /-- Sequentially assign numbers to the nodes of the graph as they are being visited. -/ meta def assign_preorder (cl : closure) (e : expr) : tactic unit := modify_ref cl $ λ m, m.insert e (sum.inl m.size) /-- `prove_eqv cl e₀ e₁` constructs a proof of equivalence of `e₀` and `e₁` if they are equivalent. -/ meta def prove_eqv (cl : closure) (e₀ e₁ : expr) : tactic expr := do (_,r,p₀) ← root cl e₀, (_,r',p₁) ← root cl e₁, guard (r = r') <|> fail!"{e₀} and {e₁} are not equivalent", p₁ ← mk_app ``iff.symm [p₁], mk_app ``iff.trans [p₀,p₁] /-- `prove_impl cl e₀ e₁` constructs a proof of `e₀ -> e₁` if they are equivalent. -/ meta def prove_impl (cl : closure) (e₀ e₁ : expr) : tactic expr := cl.prove_eqv e₀ e₁ >>= iff_mp /-- `is_eqv cl e₀ e₁` checks whether `e₀` and `e₁` are equivalent without building a proof. -/ meta def is_eqv (cl : closure) (e₀ e₁ : expr) : tactic bool := do (_,r,p₀) ← root cl e₀, (_,r',p₁) ← root cl e₁, return $ r = r' end closure /-- mutable graphs between local propositions that imply each other with the proof of implication -/ @[reducible] meta def impl_graph := ref (expr_map (list $ expr × expr)) /-- `with_impl_graph f` creates an empty `impl_graph` `g`, executes `f` on `g`, and then deletes `g`, returning the output of `f`. -/ meta def with_impl_graph {α} : (impl_graph → tactic α) → tactic α := using_new_ref (expr_map.mk (list $ expr × expr)) namespace impl_graph /-- `add_edge g p`, with `p` a proof of `v₀ → v₁` or `v₀ ↔ v₁`, adds an edge to the implication graph `g`. -/ meta def add_edge (g : impl_graph) : expr → tactic unit | p := do t ← infer_type p, match t with | `(%%v₀ → %%v₁) := do is_prop v₀ >>= guardb, is_prop v₁ >>= guardb, m ← read_ref g, let xs := (m.find v₀).get_or_else [], let xs' := (m.find v₁).get_or_else [], modify_ref g $ λ m, (m.insert v₀ ((v₁,p) :: xs)).insert v₁ xs' | `(%%v₀ ↔ %%v₁) := do p₀ ← mk_mapp ``iff.mp [none,none,p], p₁ ← mk_mapp ``iff.mpr [none,none,p], add_edge p₀, add_edge p₁ | _ := failed end section scc open list parameter g : expr_map (list $ expr × expr) parameter visit : ref $ expr_map bool parameter cl : closure /-- `merge_path path e`, where `path` and `e` forms a cycle with proofs of implication between consecutive vertices. The proofs are compiled into proofs of equivalences and added to the closure structure. `e` and the first vertex of `path` do not have to be the same but they have to be in the same equivalence class. -/ meta def merge_path (path : list (expr × expr)) (e : expr) : tactic unit := do p₁ ← cl.prove_impl e path.head.fst, p₂ ← mk_mapp ``id [e], let path := (e,p₁) :: path, (_,ls) ← path.mmap_accuml (λ p p', prod.mk <$> mk_mapp ``implies.trans [none,p'.1,none,p,p'.2] <*> pure p) p₂, (_,rs) ← path.mmap_accumr (λ p p', prod.mk <$> mk_mapp ``implies.trans [none,none,none,p.2,p'] <*> pure p') p₂, ps ← mzip_with (λ p₀ p₁, mk_app ``iff.intro [p₀,p₁]) ls.tail rs.init, ps.mmap' cl.merge /-- (implementation of `collapse`) -/ meta def collapse' : list (expr × expr) → list (expr × expr) → expr → tactic unit | acc [] v := merge_path acc v | acc ((x,pr) :: xs) v := do b ← cl.is_eqv x v, let acc' := (x,pr)::acc, if b then merge_path acc' v else collapse' acc' xs v /-- `collapse path v`, where `v` is a vertex that originated the current search (or a vertex in the same equivalence class as the one that originated the current search). It or its equivalent should be found in `path`. Since the vertices following `v` in the path form a cycle with `v`, they can all be added to an equivalence class. -/ meta def collapse : list (expr × expr) → expr → tactic unit := collapse' [] /-- Strongly connected component algorithm inspired by Tarjan's and Dijkstra's scc algorithm. Whereas they return strongly connected components by enumerating them, this algorithm returns a disjoint set data structure using path compression. This is a compact representation that allows us, after the fact, to construct a proof of equivalence between any two members of an equivalence class. * Tarjan, R. E. (1972), "Depth-first search and linear graph algorithms", SIAM Journal on Computing, 1 (2): 146–160, doi:10.1137/0201010 * Dijkstra, Edsger (1976), A Discipline of Programming, NJ: Prentice Hall, Ch. 25. -/ meta def dfs_at : list (expr × expr) → expr → tactic unit | vs v := do m ← read_ref visit, (_,v',_) ← cl.root v, match m.find v' with | (some tt) := pure () | (some ff) := collapse vs v | none := do cl.assign_preorder v, modify_ref visit $ λ m, m.insert v ff, ns ← g.find v, ns.mmap' $ λ ⟨w,e⟩, dfs_at ((v,e) :: vs) w, modify_ref visit $ λ m, m.insert v tt, pure () end end scc /-- Use the local assumptions to create a set of equivalence classes. -/ meta def mk_scc (cl : closure) : tactic (expr_map (list (expr × expr))) := with_impl_graph $ λ g, using_new_ref (expr_map.mk bool) $ λ visit, do ls ← local_context, ls.mmap' $ λ l, try (g.add_edge l), m ← read_ref g, m.to_list.mmap $ λ ⟨v,_⟩, impl_graph.dfs_at m visit cl [] v, pure m end impl_graph meta def prove_eqv_target (cl : closure) : tactic unit := do `(%%p ↔ %%q) ← target >>= whnf, cl.prove_eqv p q >>= exact /-- `scc` uses the available equivalences and implications to prove a goal of the form `p ↔ q`. ```lean example (p q r : Prop) (hpq : p → q) (hqr : q ↔ r) (hrp : r → p) : p ↔ r := by scc ``` -/ meta def interactive.scc : tactic unit := closure.with_new_closure $ λ cl, do impl_graph.mk_scc cl, `(%%p ↔ %%q) ← target, cl.prove_eqv p q >>= exact /-- Collect all the available equivalences and implications and add assumptions for every equivalence that can be proven using the strongly connected components technique. Mostly useful for testing. -/ meta def interactive.scc' : tactic unit := closure.with_new_closure $ λ cl, do m ← impl_graph.mk_scc cl, let ls := m.to_list.map prod.fst, let ls' := prod.mk <$> ls <*> ls, ls'.mmap' $ λ x, do { h ← get_unused_name `h, try $ closure.prove_eqv cl x.1 x.2 >>= note h none } /-- `scc` uses the available equivalences and implications to prove a goal of the form `p ↔ q`. ```lean example (p q r : Prop) (hpq : p → q) (hqr : q ↔ r) (hrp : r → p) : p ↔ r := by scc ``` The variant `scc'` populates the local context with all equivalences that `scc` is able to prove. This is mostly useful for testing purposes. -/ add_tactic_doc { name := "scc", category := doc_category.tactic, decl_names := [``interactive.scc, ``interactive.scc'], tags := ["logic"] } end tactic
d42207f626bf9b5cc1f264b81abdcf709d0b6b6d
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/int/basic.lean
4fb610560e59a2e296419695e8250b2853236b1c
[ "Apache-2.0" ]
permissive
fpvandoorn/mathlib
b21ab4068db079cbb8590b58fda9cc4bc1f35df4
b3433a51ea8bc07c4159c1073838fc0ee9b8f227
refs/heads/master
1,624,791,089,608
1,556,715,231,000
1,556,715,231,000
165,722,980
5
0
Apache-2.0
1,552,657,455,000
1,547,494,646,000
Lean
UTF-8
Lean
false
false
49,425
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad The integers, with addition, multiplication, and subtraction. -/ import data.nat.basic data.list.basic algebra.char_zero algebra.order_functions open nat namespace int instance : inhabited ℤ := ⟨int.zero⟩ @[simp] lemma default_eq_zero : default ℤ = 0 := rfl meta instance : has_to_format ℤ := ⟨λ z, int.rec_on z (λ k, ↑k) (λ k, "-("++↑k++"+1)")⟩ meta instance : has_reflect ℤ := by tactic.mk_has_reflect_instance attribute [simp] int.coe_nat_add int.coe_nat_mul int.coe_nat_zero int.coe_nat_one int.coe_nat_succ @[simp] theorem add_def {a b : ℤ} : int.add a b = a + b := rfl @[simp] theorem mul_def {a b : ℤ} : int.mul a b = a * b := rfl @[simp] theorem coe_nat_mul_neg_succ (m n : ℕ) : (m : ℤ) * -[1+ n] = -(m * succ n) := rfl @[simp] theorem neg_succ_mul_coe_nat (m n : ℕ) : -[1+ m] * n = -(succ m * n) := rfl @[simp] theorem neg_succ_mul_neg_succ (m n : ℕ) : -[1+ m] * -[1+ n] = succ m * succ n := rfl @[simp] theorem coe_nat_le {m n : ℕ} : (↑m : ℤ) ≤ ↑n ↔ m ≤ n := coe_nat_le_coe_nat_iff m n @[simp] theorem coe_nat_lt {m n : ℕ} : (↑m : ℤ) < ↑n ↔ m < n := coe_nat_lt_coe_nat_iff m n @[simp] theorem coe_nat_inj' {m n : ℕ} : (↑m : ℤ) = ↑n ↔ m = n := int.coe_nat_eq_coe_nat_iff m n @[simp] theorem coe_nat_pos {n : ℕ} : (0 : ℤ) < n ↔ 0 < n := by rw [← int.coe_nat_zero, coe_nat_lt] @[simp] theorem coe_nat_eq_zero {n : ℕ} : (n : ℤ) = 0 ↔ n = 0 := by rw [← int.coe_nat_zero, coe_nat_inj'] @[simp] theorem coe_nat_ne_zero {n : ℕ} : (n : ℤ) ≠ 0 ↔ n ≠ 0 := not_congr coe_nat_eq_zero lemma coe_nat_nonneg (n : ℕ) : 0 ≤ (n : ℤ) := coe_nat_le.2 (nat.zero_le _) lemma coe_nat_ne_zero_iff_pos {n : ℕ} : (n : ℤ) ≠ 0 ↔ 0 < n := ⟨λ h, nat.pos_of_ne_zero (coe_nat_ne_zero.1 h), λ h, (ne_of_lt (coe_nat_lt.2 h)).symm⟩ lemma coe_nat_succ_pos (n : ℕ) : 0 < (n.succ : ℤ) := int.coe_nat_pos.2 (succ_pos n) @[simp] theorem coe_nat_abs (n : ℕ) : abs (n : ℤ) = n := abs_of_nonneg (coe_nat_nonneg n) /- succ and pred -/ /-- Immediate successor of an integer: `succ n = n + 1` -/ def succ (a : ℤ) := a + 1 /-- Immediate predecessor of an integer: `pred n = n - 1` -/ def pred (a : ℤ) := a - 1 theorem nat_succ_eq_int_succ (n : ℕ) : (nat.succ n : ℤ) = int.succ n := rfl theorem pred_succ (a : ℤ) : pred (succ a) = a := add_sub_cancel _ _ theorem succ_pred (a : ℤ) : succ (pred a) = a := sub_add_cancel _ _ theorem neg_succ (a : ℤ) : -succ a = pred (-a) := neg_add _ _ theorem succ_neg_succ (a : ℤ) : succ (-succ a) = -a := by rw [neg_succ, succ_pred] theorem neg_pred (a : ℤ) : -pred a = succ (-a) := by rw [eq_neg_of_eq_neg (neg_succ (-a)).symm, neg_neg] theorem pred_neg_pred (a : ℤ) : pred (-pred a) = -a := by rw [neg_pred, pred_succ] theorem pred_nat_succ (n : ℕ) : pred (nat.succ n) = n := pred_succ n theorem neg_nat_succ (n : ℕ) : -(nat.succ n : ℤ) = pred (-n) := neg_succ n theorem succ_neg_nat_succ (n : ℕ) : succ (-nat.succ n) = -n := succ_neg_succ n theorem lt_succ_self (a : ℤ) : a < succ a := lt_add_of_pos_right _ zero_lt_one theorem pred_self_lt (a : ℤ) : pred a < a := sub_lt_self _ zero_lt_one theorem add_one_le_iff {a b : ℤ} : a + 1 ≤ b ↔ a < b := iff.rfl theorem lt_add_one_iff {a b : ℤ} : a < b + 1 ↔ a ≤ b := @add_le_add_iff_right _ _ a b 1 theorem sub_one_lt_iff {a b : ℤ} : a - 1 < b ↔ a ≤ b := sub_lt_iff_lt_add.trans lt_add_one_iff theorem le_sub_one_iff {a b : ℤ} : a ≤ b - 1 ↔ a < b := le_sub_iff_add_le @[elab_as_eliminator] protected lemma induction_on {p : ℤ → Prop} (i : ℤ) (hz : p 0) (hp : ∀i : ℕ, p i → p (i + 1)) (hn : ∀i : ℕ, p (-i) → p (-i - 1)) : p i := begin induction i, { induction i, { exact hz }, { exact hp _ i_ih } }, { have : ∀n:ℕ, p (- n), { intro n, induction n, { simp [hz] }, { have := hn _ n_ih, simpa } }, exact this (i + 1) } end protected def induction_on' {C : ℤ → Sort*} (z : ℤ) (b : ℤ) : C b → (∀ k ≥ b, C k → C (k + 1)) → (∀ k ≤ b, C k → C (k - 1)) → C z := λ H0 Hs Hp, begin rw ←sub_add_cancel z b, induction (z - b), { induction a with n ih, { rwa [of_nat_zero, zero_add] }, rw [of_nat_succ, add_assoc, add_comm 1 b, ←add_assoc], exact Hs _ (le_add_of_nonneg_left (of_nat_nonneg _)) ih }, { induction a with n ih, { rw [neg_succ_of_nat_eq, ←of_nat_eq_coe, of_nat_zero, zero_add, neg_add_eq_sub], exact Hp _ (le_refl _) H0 }, { rw [neg_succ_of_nat_coe', nat.succ_eq_add_one, ←neg_succ_of_nat_coe, sub_add_eq_add_sub], exact Hp _ (le_of_lt (add_lt_of_neg_of_le (neg_succ_lt_zero _) (le_refl _))) ih } } end /- nat abs -/ attribute [simp] nat_abs nat_abs_of_nat nat_abs_zero nat_abs_one theorem nat_abs_add_le (a b : ℤ) : nat_abs (a + b) ≤ nat_abs a + nat_abs b := begin have : ∀ (a b : ℕ), nat_abs (sub_nat_nat a (nat.succ b)) ≤ nat.succ (a + b), { refine (λ a b : ℕ, sub_nat_nat_elim a b.succ (λ m n i, n = b.succ → nat_abs i ≤ (m + b).succ) _ _ rfl); intros i n e, { subst e, rw [add_comm _ i, add_assoc], exact nat.le_add_right i (b.succ + b).succ }, { apply succ_le_succ, rw [← succ_inj e, ← add_assoc, add_comm], apply nat.le_add_right } }, cases a; cases b with b b; simp [nat_abs, nat.succ_add]; try {refl}; [skip, rw add_comm a b]; apply this end theorem nat_abs_neg_of_nat (n : ℕ) : nat_abs (neg_of_nat n) = n := by cases n; refl theorem nat_abs_mul (a b : ℤ) : nat_abs (a * b) = (nat_abs a) * (nat_abs b) := by cases a; cases b; simp only [(*), int.mul, nat_abs_neg_of_nat, eq_self_iff_true, int.nat_abs] @[simp] lemma nat_abs_mul_self' (a : ℤ) : (nat_abs a * nat_abs a : ℤ) = a * a := by rw [← int.coe_nat_mul, nat_abs_mul_self] theorem neg_succ_of_nat_eq' (m : ℕ) : -[1+ m] = -m - 1 := by simp [neg_succ_of_nat_eq] lemma nat_abs_ne_zero_of_ne_zero {z : ℤ} (hz : z ≠ 0) : z.nat_abs ≠ 0 := λ h, hz $ int.eq_zero_of_nat_abs_eq_zero h /- / -/ @[simp] theorem of_nat_div (m n : ℕ) : of_nat (m / n) = (of_nat m) / (of_nat n) := rfl @[simp] theorem coe_nat_div (m n : ℕ) : ((m / n : ℕ) : ℤ) = m / n := rfl theorem neg_succ_of_nat_div (m : ℕ) {b : ℤ} (H : b > 0) : -[1+m] / b = -(m / b + 1) := match b, eq_succ_of_zero_lt H with ._, ⟨n, rfl⟩ := rfl end @[simp] protected theorem div_neg : ∀ (a b : ℤ), a / -b = -(a / b) | (m : ℕ) 0 := show of_nat (m / 0) = -(m / 0 : ℕ), by rw nat.div_zero; refl | (m : ℕ) (n+1:ℕ) := rfl | 0 -[1+ n] := rfl | (m+1:ℕ) -[1+ n] := (neg_neg _).symm | -[1+ m] 0 := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl theorem div_of_neg_of_pos {a b : ℤ} (Ha : a < 0) (Hb : b > 0) : a / b = -((-a - 1) / b + 1) := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := by change (- -[1+ m] : ℤ) with (m+1 : ℤ); rw add_sub_cancel; refl end protected theorem div_nonneg {a b : ℤ} (Ha : a ≥ 0) (Hb : b ≥ 0) : a / b ≥ 0 := match a, b, eq_coe_of_zero_le Ha, eq_coe_of_zero_le Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := coe_zero_le _ end protected theorem div_nonpos {a b : ℤ} (Ha : a ≥ 0) (Hb : b ≤ 0) : a / b ≤ 0 := nonpos_of_neg_nonneg $ by rw [← int.div_neg]; exact int.div_nonneg Ha (neg_nonneg_of_nonpos Hb) theorem div_neg' {a b : ℤ} (Ha : a < 0) (Hb : b > 0) : a / b < 0 := match a, b, eq_neg_succ_of_lt_zero Ha, eq_succ_of_zero_lt Hb with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩ := neg_succ_lt_zero _ end @[simp] protected theorem zero_div : ∀ (b : ℤ), 0 / b = 0 | 0 := rfl | (n+1:ℕ) := rfl | -[1+ n] := rfl @[simp] protected theorem div_zero : ∀ (a : ℤ), a / 0 = 0 | 0 := rfl | (n+1:ℕ) := rfl | -[1+ n] := rfl @[simp] protected theorem div_one : ∀ (a : ℤ), a / 1 = a | 0 := rfl | (n+1:ℕ) := congr_arg of_nat (nat.div_one _) | -[1+ n] := congr_arg neg_succ_of_nat (nat.div_one _) theorem div_eq_zero_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a / b = 0 := match a, b, eq_coe_of_zero_le H1, eq_succ_of_zero_lt (lt_of_le_of_lt H1 H2), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.div_eq_of_lt $ lt_of_coe_nat_lt_coe_nat H2 end theorem div_eq_zero_of_lt_abs {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < abs b) : a / b = 0 := match b, abs b, abs_eq_nat_abs b, H2 with | (n : ℕ), ._, rfl, H2 := div_eq_zero_of_lt H1 H2 | -[1+ n], ._, rfl, H2 := neg_inj $ by rw [← int.div_neg]; exact div_eq_zero_of_lt H1 H2 end protected theorem add_mul_div_right (a b : ℤ) {c : ℤ} (H : c ≠ 0) : (a + b * c) / c = a / c + b := have ∀ {k n : ℕ} {a : ℤ}, (a + n * k.succ) / k.succ = a / k.succ + n, from λ k n a, match a with | (m : ℕ) := congr_arg of_nat $ nat.add_mul_div_right _ _ k.succ_pos | -[1+ m] := show ((n * k.succ:ℕ) - m.succ : ℤ) / k.succ = n - (m / k.succ + 1 : ℕ), begin cases lt_or_ge m (n*k.succ) with h h, { rw [← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.div_lt_iff_lt_mul _ _ k.succ_pos).2 h)], apply congr_arg of_nat, rw [mul_comm, nat.mul_sub_div], rwa mul_comm }, { change (↑(n * nat.succ k) - (m + 1) : ℤ) / ↑(nat.succ k) = ↑n - ((m / nat.succ k : ℕ) + 1), rw [← sub_sub, ← sub_sub, ← neg_sub (m:ℤ), ← neg_sub _ (n:ℤ), ← int.coe_nat_sub h, ← int.coe_nat_sub ((nat.le_div_iff_mul_le _ _ k.succ_pos).2 h), ← neg_succ_of_nat_coe', ← neg_succ_of_nat_coe'], { apply congr_arg neg_succ_of_nat, rw [mul_comm, nat.sub_mul_div], rwa mul_comm } } end end, have ∀ {a b c : ℤ}, c > 0 → (a + b * c) / c = a / c + b, from λ a b c H, match c, eq_succ_of_zero_lt H, b with | ._, ⟨k, rfl⟩, (n : ℕ) := this | ._, ⟨k, rfl⟩, -[1+ n] := show (a - n.succ * k.succ) / k.succ = (a / k.succ) - n.succ, from eq_sub_of_add_eq $ by rw [← this, sub_add_cancel] end, match lt_trichotomy c 0 with | or.inl hlt := neg_inj $ by rw [← int.div_neg, neg_add, ← int.div_neg, ← neg_mul_neg]; apply this (neg_pos_of_neg hlt) | or.inr (or.inl heq) := absurd heq H | or.inr (or.inr hgt) := this hgt end protected theorem add_mul_div_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b ≠ 0) : (a + b * c) / b = a / b + c := by rw [mul_comm, int.add_mul_div_right _ _ H] @[simp] protected theorem mul_div_cancel (a : ℤ) {b : ℤ} (H : b ≠ 0) : a * b / b = a := by have := int.add_mul_div_right 0 a H; rwa [zero_add, int.zero_div, zero_add] at this @[simp] protected theorem mul_div_cancel_left {a : ℤ} (b : ℤ) (H : a ≠ 0) : a * b / a = b := by rw [mul_comm, int.mul_div_cancel _ H] @[simp] protected theorem div_self {a : ℤ} (H : a ≠ 0) : a / a = 1 := by have := int.mul_div_cancel 1 H; rwa one_mul at this /- mod -/ theorem of_nat_mod (m n : nat) : (m % n : ℤ) = of_nat (m % n) := rfl @[simp] theorem coe_nat_mod (m n : ℕ) : (↑(m % n) : ℤ) = ↑m % ↑n := rfl theorem neg_succ_of_nat_mod (m : ℕ) {b : ℤ} (bpos : b > 0) : -[1+m] % b = b - 1 - m % b := by rw [sub_sub, add_comm]; exact match b, eq_succ_of_zero_lt bpos with ._, ⟨n, rfl⟩ := rfl end @[simp] theorem mod_neg : ∀ (a b : ℤ), a % -b = a % b | (m : ℕ) n := @congr_arg ℕ ℤ _ _ (λ i, ↑(m % i)) (nat_abs_neg _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λ i, sub_nat_nat i (nat.succ (m % i))) (nat_abs_neg _) @[simp] theorem mod_abs (a b : ℤ) : a % (abs b) = a % b := abs_by_cases (λ i, a % i = a % b) rfl (mod_neg _ _) @[simp] theorem zero_mod (b : ℤ) : 0 % b = 0 := congr_arg of_nat $ nat.zero_mod _ @[simp] theorem mod_zero : ∀ (a : ℤ), a % 0 = a | (m : ℕ) := congr_arg of_nat $ nat.mod_zero _ | -[1+ m] := congr_arg neg_succ_of_nat $ nat.mod_zero _ @[simp] theorem mod_one : ∀ (a : ℤ), a % 1 = 0 | (m : ℕ) := congr_arg of_nat $ nat.mod_one _ | -[1+ m] := show (1 - (m % 1).succ : ℤ) = 0, by rw nat.mod_one; refl theorem mod_eq_of_lt {a b : ℤ} (H1 : 0 ≤ a) (H2 : a < b) : a % b = a := match a, b, eq_coe_of_zero_le H1, eq_coe_of_zero_le (le_trans H1 (le_of_lt H2)), H2 with | ._, ._, ⟨m, rfl⟩, ⟨n, rfl⟩, H2 := congr_arg of_nat $ nat.mod_eq_of_lt (lt_of_coe_nat_lt_coe_nat H2) end theorem mod_nonneg : ∀ (a : ℤ) {b : ℤ}, b ≠ 0 → a % b ≥ 0 | (m : ℕ) n H := coe_zero_le _ | -[1+ m] n H := sub_nonneg_of_le $ coe_nat_le_coe_nat_of_le $ nat.mod_lt _ (nat_abs_pos_of_ne_zero H) theorem mod_lt_of_pos (a : ℤ) {b : ℤ} (H : b > 0) : a % b < b := match a, b, eq_succ_of_zero_lt H with | (m : ℕ), ._, ⟨n, rfl⟩ := coe_nat_lt_coe_nat_of_lt (nat.mod_lt _ (nat.succ_pos _)) | -[1+ m], ._, ⟨n, rfl⟩ := sub_lt_self _ (coe_nat_lt_coe_nat_of_lt $ nat.succ_pos _) end theorem mod_lt (a : ℤ) {b : ℤ} (H : b ≠ 0) : a % b < abs b := by rw [← mod_abs]; exact mod_lt_of_pos _ (abs_pos_of_ne_zero H) theorem mod_add_div_aux (m n : ℕ) : (n - (m % n + 1) - (n * (m / n) + n) : ℤ) = -[1+ m] := begin rw [← sub_sub, neg_succ_of_nat_coe, sub_sub (n:ℤ)], apply eq_neg_of_eq_neg, rw [neg_sub, sub_sub_self, add_right_comm], exact @congr_arg ℕ ℤ _ _ (λi, (i + 1 : ℤ)) (nat.mod_add_div _ _).symm end theorem mod_add_div : ∀ (a b : ℤ), a % b + b * (a / b) = a | (m : ℕ) 0 := congr_arg of_nat (nat.mod_add_div _ _) | (m : ℕ) (n+1:ℕ) := congr_arg of_nat (nat.mod_add_div _ _) | 0 -[1+ n] := rfl | (m+1:ℕ) -[1+ n] := show (_ + -(n+1) * -((m + 1) / (n + 1) : ℕ) : ℤ) = _, by rw [neg_mul_neg]; exact congr_arg of_nat (nat.mod_add_div _ _) | -[1+ m] 0 := by rw [mod_zero, int.div_zero]; refl | -[1+ m] (n+1:ℕ) := mod_add_div_aux m n.succ | -[1+ m] -[1+ n] := mod_add_div_aux m n.succ theorem mod_def (a b : ℤ) : a % b = a - b * (a / b) := eq_sub_of_add_eq (mod_add_div _ _) @[simp] theorem add_mul_mod_self {a b c : ℤ} : (a + b * c) % c = a % c := if cz : c = 0 then by rw [cz, mul_zero, add_zero] else by rw [mod_def, mod_def, int.add_mul_div_right _ _ cz, mul_add, mul_comm, add_sub_add_right_eq_sub] @[simp] theorem add_mul_mod_self_left (a b c : ℤ) : (a + b * c) % b = a % b := by rw [mul_comm, add_mul_mod_self] @[simp] theorem add_mod_self {a b : ℤ} : (a + b) % b = a % b := by have := add_mul_mod_self_left a b 1; rwa mul_one at this @[simp] theorem add_mod_self_left {a b : ℤ} : (a + b) % a = b % a := by rw [add_comm, add_mod_self] @[simp] theorem mod_add_mod (m n k : ℤ) : (m % n + k) % n = (m + k) % n := by have := (add_mul_mod_self_left (m % n + k) n (m / n)).symm; rwa [add_right_comm, mod_add_div] at this @[simp] theorem add_mod_mod (m n k : ℤ) : (m + n % k) % k = (m + n) % k := by rw [add_comm, mod_add_mod, add_comm] theorem add_mod_eq_add_mod_right {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (m + i) % n = (k + i) % n := by rw [← mod_add_mod, ← mod_add_mod k, H] theorem add_mod_eq_add_mod_left {m n k : ℤ} (i : ℤ) (H : m % n = k % n) : (i + m) % n = (i + k) % n := by rw [add_comm, add_mod_eq_add_mod_right _ H, add_comm] theorem mod_add_cancel_right {m n k : ℤ} (i) : (m + i) % n = (k + i) % n ↔ m % n = k % n := ⟨λ H, by have := add_mod_eq_add_mod_right (-i) H; rwa [add_neg_cancel_right, add_neg_cancel_right] at this, add_mod_eq_add_mod_right _⟩ theorem mod_add_cancel_left {m n k i : ℤ} : (i + m) % n = (i + k) % n ↔ m % n = k % n := by rw [add_comm, add_comm i, mod_add_cancel_right] theorem mod_sub_cancel_right {m n k : ℤ} (i) : (m - i) % n = (k - i) % n ↔ m % n = k % n := mod_add_cancel_right _ theorem mod_eq_mod_iff_mod_sub_eq_zero {m n k : ℤ} : m % n = k % n ↔ (m - k) % n = 0 := (mod_sub_cancel_right k).symm.trans $ by simp @[simp] theorem mul_mod_left (a b : ℤ) : (a * b) % b = 0 := by rw [← zero_add (a * b), add_mul_mod_self, zero_mod] @[simp] theorem mul_mod_right (a b : ℤ) : (a * b) % a = 0 := by rw [mul_comm, mul_mod_left] @[simp] theorem mod_self {a : ℤ} : a % a = 0 := by have := mul_mod_left 1 a; rwa one_mul at this @[simp] lemma mod_mod (a b : ℤ) : a % b % b = a % b := by conv {to_rhs, rw [← mod_add_div a b, add_mul_mod_self_left]} /- properties of / and % -/ @[simp] theorem mul_div_mul_of_pos {a : ℤ} (b c : ℤ) (H : a > 0) : a * b / (a * c) = b / c := suffices ∀ (m k : ℕ) (b : ℤ), (m.succ * b / (m.succ * k) : ℤ) = b / k, from match a, eq_succ_of_zero_lt H, c, eq_coe_or_neg c with | ._, ⟨m, rfl⟩, ._, ⟨k, or.inl rfl⟩ := this _ _ _ | ._, ⟨m, rfl⟩, ._, ⟨k, or.inr rfl⟩ := by rw [← neg_mul_eq_mul_neg, int.div_neg, int.div_neg]; apply congr_arg has_neg.neg; apply this end, λ m k b, match b, k with | (n : ℕ), k := congr_arg of_nat (nat.mul_div_mul _ _ m.succ_pos) | -[1+ n], 0 := by rw [int.coe_nat_zero, mul_zero, int.div_zero, int.div_zero] | -[1+ n], k+1 := congr_arg neg_succ_of_nat $ show (m.succ * n + m) / (m.succ * k.succ) = n / k.succ, begin apply nat.div_eq_of_lt_le, { refine le_trans _ (nat.le_add_right _ _), rw [← nat.mul_div_mul _ _ m.succ_pos], apply nat.div_mul_le_self }, { change m.succ * n.succ ≤ _, rw [mul_left_comm], apply nat.mul_le_mul_left, apply (nat.div_lt_iff_lt_mul _ _ k.succ_pos).1, apply nat.lt_succ_self } end end @[simp] theorem mul_div_mul_of_pos_left (a : ℤ) {b : ℤ} (c : ℤ) (H : b > 0) : a * b / (c * b) = a / c := by rw [mul_comm, mul_comm c, mul_div_mul_of_pos _ _ H] @[simp] theorem mul_mod_mul_of_pos {a : ℤ} (b c : ℤ) (H : a > 0) : a * b % (a * c) = a * (b % c) := by rw [mod_def, mod_def, mul_div_mul_of_pos _ _ H, mul_sub_left_distrib, mul_assoc] theorem lt_div_add_one_mul_self (a : ℤ) {b : ℤ} (H : b > 0) : a < (a / b + 1) * b := by rw [add_mul, one_mul, mul_comm]; apply lt_add_of_sub_left_lt; rw [← mod_def]; apply mod_lt_of_pos _ H theorem abs_div_le_abs : ∀ (a b : ℤ), abs (a / b) ≤ abs a := suffices ∀ (a : ℤ) (n : ℕ), abs (a / n) ≤ abs a, from λ a b, match b, eq_coe_or_neg b with | ._, ⟨n, or.inl rfl⟩ := this _ _ | ._, ⟨n, or.inr rfl⟩ := by rw [int.div_neg, abs_neg]; apply this end, λ a n, by rw [abs_eq_nat_abs, abs_eq_nat_abs]; exact coe_nat_le_coe_nat_of_le (match a, n with | (m : ℕ), n := nat.div_le_self _ _ | -[1+ m], 0 := nat.zero_le _ | -[1+ m], n+1 := nat.succ_le_succ (nat.div_le_self _ _) end) theorem div_le_self {a : ℤ} (b : ℤ) (Ha : a ≥ 0) : a / b ≤ a := by have := le_trans (le_abs_self _) (abs_div_le_abs a b); rwa [abs_of_nonneg Ha] at this theorem mul_div_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : b * (a / b) = a := by have := mod_add_div a b; rwa [H, zero_add] at this theorem div_mul_cancel_of_mod_eq_zero {a b : ℤ} (H : a % b = 0) : a / b * b = a := by rw [mul_comm, mul_div_cancel_of_mod_eq_zero H] lemma mod_two_eq_zero_or_one (n : ℤ) : n % 2 = 0 ∨ n % 2 = 1 := have h : n % 2 < 2 := abs_of_nonneg (show (2 : ℤ) ≥ 0, from dec_trivial) ▸ int.mod_lt _ dec_trivial, have h₁ : n % 2 ≥ 0 := int.mod_nonneg _ dec_trivial, match (n % 2), h, h₁ with | (0 : ℕ) := λ _ _, or.inl rfl | (1 : ℕ) := λ _ _, or.inr rfl | (k + 2 : ℕ) := λ h _, absurd h dec_trivial | -[1+ a] := λ _ h₁, absurd h₁ dec_trivial end /- dvd -/ theorem coe_nat_dvd {m n : ℕ} : (↑m : ℤ) ∣ ↑n ↔ m ∣ n := ⟨λ ⟨a, ae⟩, m.eq_zero_or_pos.elim (λm0, by simp [m0] at ae; simp [ae, m0]) (λm0l, by { cases eq_coe_of_zero_le (@nonneg_of_mul_nonneg_left ℤ _ m a (by simp [ae.symm]) (by simpa using m0l)) with k e, subst a, exact ⟨k, int.coe_nat_inj ae⟩ }), λ ⟨k, e⟩, dvd.intro k $ by rw [e, int.coe_nat_mul]⟩ theorem coe_nat_dvd_left {n : ℕ} {z : ℤ} : (↑n : ℤ) ∣ z ↔ n ∣ z.nat_abs := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem coe_nat_dvd_right {n : ℕ} {z : ℤ} : z ∣ (↑n : ℤ) ↔ z.nat_abs ∣ n := by rcases nat_abs_eq z with eq | eq; rw eq; simp [coe_nat_dvd] theorem dvd_antisymm {a b : ℤ} (H1 : a ≥ 0) (H2 : b ≥ 0) : a ∣ b → b ∣ a → a = b := begin rw [← abs_of_nonneg H1, ← abs_of_nonneg H2, abs_eq_nat_abs, abs_eq_nat_abs], rw [coe_nat_dvd, coe_nat_dvd, coe_nat_inj'], apply nat.dvd_antisymm end theorem dvd_of_mod_eq_zero {a b : ℤ} (H : b % a = 0) : a ∣ b := ⟨b / a, (mul_div_cancel_of_mod_eq_zero H).symm⟩ theorem mod_eq_zero_of_dvd : ∀ {a b : ℤ}, a ∣ b → b % a = 0 | a ._ ⟨c, rfl⟩ := mul_mod_right _ _ theorem dvd_iff_mod_eq_zero (a b : ℤ) : a ∣ b ↔ b % a = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ theorem nat_abs_dvd {a b : ℤ} : (a.nat_abs : ℤ) ∣ b ↔ a ∣ b := (nat_abs_eq a).elim (λ e, by rw ← e) (λ e, by rw [← neg_dvd_iff_dvd, ← e]) theorem dvd_nat_abs {a b : ℤ} : a ∣ b.nat_abs ↔ a ∣ b := (nat_abs_eq b).elim (λ e, by rw ← e) (λ e, by rw [← dvd_neg_iff_dvd, ← e]) instance decidable_dvd : @decidable_rel ℤ (∣) := assume a n, decidable_of_decidable_of_iff (by apply_instance) (dvd_iff_mod_eq_zero _ _).symm protected theorem div_mul_cancel {a b : ℤ} (H : b ∣ a) : a / b * b = a := div_mul_cancel_of_mod_eq_zero (mod_eq_zero_of_dvd H) protected theorem mul_div_cancel' {a b : ℤ} (H : a ∣ b) : a * (b / a) = b := by rw [mul_comm, int.div_mul_cancel H] protected theorem mul_div_assoc (a : ℤ) : ∀ {b c : ℤ}, c ∣ b → (a * b) / c = a * (b / c) | ._ c ⟨d, rfl⟩ := if cz : c = 0 then by simp [cz] else by rw [mul_left_comm, int.mul_div_cancel_left _ cz, int.mul_div_cancel_left _ cz] theorem div_dvd_div : ∀ {a b c : ℤ} (H1 : a ∣ b) (H2 : b ∣ c), b / a ∣ c / a | a ._ ._ ⟨b, rfl⟩ ⟨c, rfl⟩ := if az : a = 0 then by simp [az] else by rw [int.mul_div_cancel_left _ az, mul_assoc, int.mul_div_cancel_left _ az]; apply dvd_mul_right protected theorem eq_mul_of_div_eq_right {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = b * c := by rw [← H2, int.mul_div_cancel' H1] protected theorem div_eq_of_eq_mul_right {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = b * c) : a / b = c := by rw [H2, int.mul_div_cancel_left _ H1] protected theorem div_eq_iff_eq_mul_right {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = b * c := ⟨int.eq_mul_of_div_eq_right H', int.div_eq_of_eq_mul_right H⟩ protected theorem div_eq_iff_eq_mul_left {a b c : ℤ} (H : b ≠ 0) (H' : b ∣ a) : a / b = c ↔ a = c * b := by rw mul_comm; exact int.div_eq_iff_eq_mul_right H H' protected theorem eq_mul_of_div_eq_left {a b c : ℤ} (H1 : b ∣ a) (H2 : a / b = c) : a = c * b := by rw [mul_comm, int.eq_mul_of_div_eq_right H1 H2] protected theorem div_eq_of_eq_mul_left {a b c : ℤ} (H1 : b ≠ 0) (H2 : a = c * b) : a / b = c := int.div_eq_of_eq_mul_right H1 (by rw [mul_comm, H2]) theorem neg_div_of_dvd : ∀ {a b : ℤ} (H : b ∣ a), -a / b = -(a / b) | ._ b ⟨c, rfl⟩ := if bz : b = 0 then by simp [bz] else by rw [neg_mul_eq_mul_neg, int.mul_div_cancel_left _ bz, int.mul_div_cancel_left _ bz] lemma add_div_of_dvd {a b c : ℤ} : c ∣ a → c ∣ b → (a + b) / c = a / c + b / c := begin intros h1 h2, by_cases h3 : c = 0, { rw [h3, zero_dvd_iff] at *, rw [h1, h2, h3], refl }, { apply eq_of_mul_eq_mul_right h3, rw add_mul, repeat {rw [int.div_mul_cancel]}; try {apply dvd_add}; assumption } end theorem div_sign : ∀ a b, a / sign b = a * sign b | a (n+1:ℕ) := by unfold sign; simp | a 0 := by simp [sign] | a -[1+ n] := by simp [sign] @[simp] theorem sign_mul : ∀ a b, sign (a * b) = sign a * sign b | a 0 := by simp | 0 b := by simp | (m+1:ℕ) (n+1:ℕ) := rfl | (m+1:ℕ) -[1+ n] := rfl | -[1+ m] (n+1:ℕ) := rfl | -[1+ m] -[1+ n] := rfl protected theorem sign_eq_div_abs (a : ℤ) : sign a = a / (abs a) := if az : a = 0 then by simp [az] else (int.div_eq_of_eq_mul_left (mt eq_zero_of_abs_eq_zero az) (sign_mul_abs _).symm).symm theorem mul_sign : ∀ (i : ℤ), i * sign i = nat_abs i | (n+1:ℕ) := mul_one _ | 0 := mul_zero _ | -[1+ n] := mul_neg_one _ theorem le_of_dvd {a b : ℤ} (bpos : b > 0) (H : a ∣ b) : a ≤ b := match a, b, eq_succ_of_zero_lt bpos, H with | (m : ℕ), ._, ⟨n, rfl⟩, H := coe_nat_le_coe_nat_of_le $ nat.le_of_dvd n.succ_pos $ coe_nat_dvd.1 H | -[1+ m], ._, ⟨n, rfl⟩, _ := le_trans (le_of_lt $ neg_succ_lt_zero _) (coe_zero_le _) end theorem eq_one_of_dvd_one {a : ℤ} (H : a ≥ 0) (H' : a ∣ 1) : a = 1 := match a, eq_coe_of_zero_le H, H' with | ._, ⟨n, rfl⟩, H' := congr_arg coe $ nat.eq_one_of_dvd_one $ coe_nat_dvd.1 H' end theorem eq_one_of_mul_eq_one_right {a b : ℤ} (H : a ≥ 0) (H' : a * b = 1) : a = 1 := eq_one_of_dvd_one H ⟨b, H'.symm⟩ theorem eq_one_of_mul_eq_one_left {a b : ℤ} (H : b ≥ 0) (H' : a * b = 1) : b = 1 := eq_one_of_mul_eq_one_right H (by rw [mul_comm, H']) lemma of_nat_dvd_of_dvd_nat_abs {a : ℕ} : ∀ {z : ℤ} (haz : a ∣ z.nat_abs), ↑a ∣ z | (int.of_nat _) haz := int.coe_nat_dvd.2 haz | -[1+k] haz := begin change ↑a ∣ -(k+1 : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, exact haz end lemma dvd_nat_abs_of_of_nat_dvd {a : ℕ} : ∀ {z : ℤ} (haz : ↑a ∣ z), a ∣ z.nat_abs | (int.of_nat _) haz := int.coe_nat_dvd.1 (int.dvd_nat_abs.2 haz) | -[1+k] haz := have haz' : (↑a:ℤ) ∣ (↑(k+1):ℤ), from dvd_of_dvd_neg haz, int.coe_nat_dvd.1 haz' lemma pow_dvd_of_le_of_pow_dvd {p m n : ℕ} {k : ℤ} (hmn : m ≤ n) (hdiv : ↑(p ^ n) ∣ k) : ↑(p ^ m) ∣ k := begin induction k, { apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1 hdiv }, { change -[1+k] with -(↑(k+1) : ℤ), apply dvd_neg_of_dvd, apply int.coe_nat_dvd.2, apply pow_dvd_of_le_of_pow_dvd hmn, apply int.coe_nat_dvd.1, apply dvd_of_dvd_neg, exact hdiv } end lemma dvd_of_pow_dvd {p k : ℕ} {m : ℤ} (hk : 1 ≤ k) (hpk : ↑(p^k) ∣ m) : ↑p ∣ m := by rw ←nat.pow_one p; exact pow_dvd_of_le_of_pow_dvd hk hpk /- / and ordering -/ protected theorem div_mul_le (a : ℤ) {b : ℤ} (H : b ≠ 0) : a / b * b ≤ a := le_of_sub_nonneg $ by rw [mul_comm, ← mod_def]; apply mod_nonneg _ H protected theorem div_le_of_le_mul {a b c : ℤ} (H : c > 0) (H' : a ≤ b * c) : a / c ≤ b := le_of_mul_le_mul_right (le_trans (int.div_mul_le _ (ne_of_gt H)) H') H protected theorem mul_lt_of_lt_div {a b c : ℤ} (H : c > 0) (H3 : a < b / c) : a * c < b := lt_of_not_ge $ mt (int.div_le_of_le_mul H) (not_le_of_gt H3) protected theorem mul_le_of_le_div {a b c : ℤ} (H1 : c > 0) (H2 : a ≤ b / c) : a * c ≤ b := le_trans (mul_le_mul_of_nonneg_right H2 (le_of_lt H1)) (int.div_mul_le _ (ne_of_gt H1)) protected theorem le_div_of_mul_le {a b c : ℤ} (H1 : c > 0) (H2 : a * c ≤ b) : a ≤ b / c := le_of_lt_add_one $ lt_of_mul_lt_mul_right (lt_of_le_of_lt H2 (lt_div_add_one_mul_self _ H1)) (le_of_lt H1) protected theorem le_div_iff_mul_le {a b c : ℤ} (H : c > 0) : a ≤ b / c ↔ a * c ≤ b := ⟨int.mul_le_of_le_div H, int.le_div_of_mul_le H⟩ protected theorem div_le_div {a b c : ℤ} (H : c > 0) (H' : a ≤ b) : a / c ≤ b / c := int.le_div_of_mul_le H (le_trans (int.div_mul_le _ (ne_of_gt H)) H') protected theorem div_lt_of_lt_mul {a b c : ℤ} (H : c > 0) (H' : a < b * c) : a / c < b := lt_of_not_ge $ mt (int.mul_le_of_le_div H) (not_le_of_gt H') protected theorem lt_mul_of_div_lt {a b c : ℤ} (H1 : c > 0) (H2 : a / c < b) : a < b * c := lt_of_not_ge $ mt (int.le_div_of_mul_le H1) (not_le_of_gt H2) protected theorem div_lt_iff_lt_mul {a b c : ℤ} (H : c > 0) : a / c < b ↔ a < b * c := ⟨int.lt_mul_of_div_lt H, int.div_lt_of_lt_mul H⟩ protected theorem le_mul_of_div_le {a b c : ℤ} (H1 : b ≥ 0) (H2 : b ∣ a) (H3 : a / b ≤ c) : a ≤ c * b := by rw [← int.div_mul_cancel H2]; exact mul_le_mul_of_nonneg_right H3 H1 protected theorem lt_div_of_mul_lt {a b c : ℤ} (H1 : b ≥ 0) (H2 : b ∣ c) (H3 : a * b < c) : a < c / b := lt_of_not_ge $ mt (int.le_mul_of_div_le H1 H2) (not_le_of_gt H3) protected theorem lt_div_iff_mul_lt {a b : ℤ} (c : ℤ) (H : c > 0) (H' : c ∣ b) : a < b / c ↔ a * c < b := ⟨int.mul_lt_of_lt_div H, int.lt_div_of_mul_lt (le_of_lt H) H'⟩ theorem div_pos_of_pos_of_dvd {a b : ℤ} (H1 : a > 0) (H2 : b ≥ 0) (H3 : b ∣ a) : a / b > 0 := int.lt_div_of_mul_lt H2 H3 (by rwa zero_mul) theorem div_eq_div_of_mul_eq_mul {a b c d : ℤ} (H1 : b ∣ a) (H2 : d ∣ c) (H3 : b ≠ 0) (H4 : d ≠ 0) (H5 : a * d = b * c) : a / b = c / d := int.div_eq_of_eq_mul_right H3 $ by rw [← int.mul_div_assoc _ H2]; exact (int.div_eq_of_eq_mul_left H4 H5.symm).symm theorem eq_mul_div_of_mul_eq_mul_of_dvd_left {a b c d : ℤ} (hb : b ≠ 0) (hd : d ≠ 0) (hbc : b ∣ c) (h : b * a = c * d) : a = c / b * d := begin cases hbc with k hk, subst hk, rw int.mul_div_cancel_left, rw mul_assoc at h, apply _root_.eq_of_mul_eq_mul_left _ h, repeat {assumption} end theorem of_nat_add_neg_succ_of_nat_of_lt {m n : ℕ} (h : m < n.succ) : of_nat m + -[1+n] = -[1+ n - m] := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = (n - m).succ, apply succ_sub, apply le_of_lt_succ h, simp [*, sub_nat_nat] end theorem of_nat_add_neg_succ_of_nat_of_ge {m n : ℕ} (h : m ≥ n.succ) : of_nat m + -[1+n] = of_nat (m - n.succ) := begin change sub_nat_nat _ _ = _, have h' : n.succ - m = 0, apply sub_eq_zero_of_le h, simp [*, sub_nat_nat] end @[simp] theorem neg_add_neg (m n : ℕ) : -[1+m] + -[1+n] = -[1+nat.succ(m+n)] := rfl /- to_nat -/ theorem to_nat_eq_max : ∀ (a : ℤ), (to_nat a : ℤ) = max a 0 | (n : ℕ) := (max_eq_left (coe_zero_le n)).symm | -[1+ n] := (max_eq_right (le_of_lt (neg_succ_lt_zero n))).symm @[simp] theorem to_nat_of_nonneg {a : ℤ} (h : 0 ≤ a) : (to_nat a : ℤ) = a := by rw [to_nat_eq_max, max_eq_left h] @[simp] theorem to_nat_coe_nat (n : ℕ) : to_nat ↑n = n := rfl theorem le_to_nat (a : ℤ) : a ≤ to_nat a := by rw [to_nat_eq_max]; apply le_max_left @[simp] theorem to_nat_le (a : ℤ) (n : ℕ) : to_nat a ≤ n ↔ a ≤ n := by rw [(coe_nat_le_coe_nat_iff _ _).symm, to_nat_eq_max, max_le_iff]; exact and_iff_left (coe_zero_le _) theorem to_nat_le_to_nat {a b : ℤ} (h : a ≤ b) : to_nat a ≤ to_nat b := by rw to_nat_le; exact le_trans h (le_to_nat b) def to_nat' : ℤ → option ℕ | (n : ℕ) := some n | -[1+ n] := none theorem mem_to_nat' : ∀ (a : ℤ) (n : ℕ), n ∈ to_nat' a ↔ a = n | (m : ℕ) n := option.some_inj.trans coe_nat_inj'.symm | -[1+ m] n := by split; intro h; cases h /- units -/ @[simp] theorem units_nat_abs (u : units ℤ) : nat_abs u = 1 := units.ext_iff.1 $ nat.units_eq_one ⟨nat_abs u, nat_abs ↑u⁻¹, by rw [← nat_abs_mul, units.mul_inv]; refl, by rw [← nat_abs_mul, units.inv_mul]; refl⟩ theorem units_eq_one_or (u : units ℤ) : u = 1 ∨ u = -1 := by simpa [units.ext_iff, units_nat_abs] using nat_abs_eq u lemma units_inv_eq_self (u : units ℤ) : u⁻¹ = u := (units_eq_one_or u).elim (λ h, h.symm ▸ rfl) (λ h, h.symm ▸ rfl) /- bitwise ops -/ @[simp] lemma bodd_zero : bodd 0 = ff := rfl @[simp] lemma bodd_one : bodd 1 = tt := rfl @[simp] lemma bodd_two : bodd 2 = ff := rfl @[simp] lemma bodd_sub_nat_nat (m n : ℕ) : bodd (sub_nat_nat m n) = bxor m.bodd n.bodd := by apply sub_nat_nat_elim m n (λ m n i, bodd i = bxor m.bodd n.bodd); intros i m; simp [bodd]; cases i.bodd; cases m.bodd; refl @[simp] lemma bodd_neg_of_nat (n : ℕ) : bodd (neg_of_nat n) = n.bodd := by cases n; simp; refl @[simp] lemma bodd_neg (n : ℤ) : bodd (-n) = bodd n := by cases n; unfold has_neg.neg; simp [int.coe_nat_eq, int.neg, bodd] @[simp] lemma bodd_add (m n : ℤ) : bodd (m + n) = bxor (bodd m) (bodd n) := by cases m with m m; cases n with n n; unfold has_add.add; simp [int.add, bodd]; cases m.bodd; cases n.bodd; refl @[simp] lemma bodd_mul (m n : ℤ) : bodd (m * n) = bodd m && bodd n := by cases m with m m; cases n with n n; unfold has_mul.mul; simp [int.mul, bodd]; cases m.bodd; cases n.bodd; refl theorem bodd_add_div2 : ∀ n, cond (bodd n) 1 0 + 2 * div2 n = n | (n : ℕ) := by rw [show (cond (bodd n) 1 0 : ℤ) = (cond (bodd n) 1 0 : ℕ), by cases bodd n; refl]; exact congr_arg of_nat n.bodd_add_div2 | -[1+ n] := begin refine eq.trans _ (congr_arg neg_succ_of_nat n.bodd_add_div2), dsimp [bodd], cases nat.bodd n; dsimp [cond, bnot, div2, int.mul], { change -[1+ 2 * nat.div2 n] = _, rw zero_add }, { rw [zero_add, add_comm], refl } end theorem div2_val : ∀ n, div2 n = n / 2 | (n : ℕ) := congr_arg of_nat n.div2_val | -[1+ n] := congr_arg neg_succ_of_nat n.div2_val lemma bit0_val (n : ℤ) : bit0 n = 2 * n := (two_mul _).symm lemma bit1_val (n : ℤ) : bit1 n = 2 * n + 1 := congr_arg (+(1:ℤ)) (bit0_val _) lemma bit_val (b n) : bit b n = 2 * n + cond b 1 0 := by { cases b, apply (bit0_val n).trans (add_zero _).symm, apply bit1_val } lemma bit_decomp (n : ℤ) : bit (bodd n) (div2 n) = n := (bit_val _ _).trans $ (add_comm _ _).trans $ bodd_add_div2 _ def {u} bit_cases_on {C : ℤ → Sort u} (n) (h : ∀ b n, C (bit b n)) : C n := by rw [← bit_decomp n]; apply h @[simp] lemma bit_zero : bit ff 0 = 0 := rfl @[simp] lemma bit_coe_nat (b) (n : ℕ) : bit b n = nat.bit b n := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bit_neg_succ (b) (n : ℕ) : bit b -[1+ n] = -[1+ nat.bit (bnot b) n] := by rw [bit_val, nat.bit_val]; cases b; refl @[simp] lemma bodd_bit (b n) : bodd (bit b n) = b := by rw bit_val; simp; cases b; cases bodd n; refl @[simp] lemma div2_bit (b n) : div2 (bit b n) = n := begin rw [bit_val, div2_val, add_comm, int.add_mul_div_left, (_ : (_/2:ℤ) = 0), zero_add], cases b, all_goals {exact dec_trivial} end @[simp] lemma test_bit_zero (b) : ∀ n, test_bit (bit b n) 0 = b | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_zero | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_zero]; clear test_bit_zero; cases b; refl @[simp] lemma test_bit_succ (m b) : ∀ n, test_bit (bit b n) (nat.succ m) = test_bit n m | (n : ℕ) := by rw [bit_coe_nat]; apply nat.test_bit_succ | -[1+ n] := by rw [bit_neg_succ]; dsimp [test_bit]; rw [nat.test_bit_succ] private meta def bitwise_tac : tactic unit := `[ funext m, funext n, cases m with m m; cases n with n n; try {refl}, all_goals { apply congr_arg of_nat <|> apply congr_arg neg_succ_of_nat, try {dsimp [nat.land, nat.ldiff, nat.lor]}, try {rw [ show nat.bitwise (λ a b, a && bnot b) n m = nat.bitwise (λ a b, b && bnot a) m n, from congr_fun (congr_fun (@nat.bitwise_swap (λ a b, b && bnot a) rfl) n) m]}, apply congr_arg (λ f, nat.bitwise f m n), funext a, funext b, cases a; cases b; refl }, all_goals {unfold nat.land nat.ldiff nat.lor} ] theorem bitwise_or : bitwise bor = lor := by bitwise_tac theorem bitwise_and : bitwise band = land := by bitwise_tac theorem bitwise_diff : bitwise (λ a b, a && bnot b) = ldiff := by bitwise_tac theorem bitwise_xor : bitwise bxor = lxor := by bitwise_tac @[simp] lemma bitwise_bit (f : bool → bool → bool) (a m b n) : bitwise f (bit a m) (bit b n) = bit (f a b) (bitwise f m n) := begin cases m with m m; cases n with n n; repeat { rw [← int.coe_nat_eq] <|> rw bit_coe_nat <|> rw bit_neg_succ }; unfold bitwise nat_bitwise bnot; [ induction h : f ff ff, induction h : f ff tt, induction h : f tt ff, induction h : f tt tt ], all_goals { unfold cond, rw nat.bitwise_bit, repeat { rw bit_coe_nat <|> rw bit_neg_succ <|> rw bnot_bnot } }, all_goals { unfold bnot {fail_if_unchanged := ff}; rw h; refl } end @[simp] lemma lor_bit (a m b n) : lor (bit a m) (bit b n) = bit (a || b) (lor m n) := by rw [← bitwise_or, bitwise_bit] @[simp] lemma land_bit (a m b n) : land (bit a m) (bit b n) = bit (a && b) (land m n) := by rw [← bitwise_and, bitwise_bit] @[simp] lemma ldiff_bit (a m b n) : ldiff (bit a m) (bit b n) = bit (a && bnot b) (ldiff m n) := by rw [← bitwise_diff, bitwise_bit] @[simp] lemma lxor_bit (a m b n) : lxor (bit a m) (bit b n) = bit (bxor a b) (lxor m n) := by rw [← bitwise_xor, bitwise_bit] @[simp] lemma lnot_bit (b) : ∀ n, lnot (bit b n) = bit (bnot b) (lnot n) | (n : ℕ) := by simp [lnot] | -[1+ n] := by simp [lnot] @[simp] lemma test_bit_bitwise (f : bool → bool → bool) (m n k) : test_bit (bitwise f m n) k = f (test_bit m k) (test_bit n k) := begin induction k with k IH generalizing m n; apply bit_cases_on m; intros a m'; apply bit_cases_on n; intros b n'; rw bitwise_bit, { simp [test_bit_zero] }, { simp [test_bit_succ, IH] } end @[simp] lemma test_bit_lor (m n k) : test_bit (lor m n) k = test_bit m k || test_bit n k := by rw [← bitwise_or, test_bit_bitwise] @[simp] lemma test_bit_land (m n k) : test_bit (land m n) k = test_bit m k && test_bit n k := by rw [← bitwise_and, test_bit_bitwise] @[simp] lemma test_bit_ldiff (m n k) : test_bit (ldiff m n) k = test_bit m k && bnot (test_bit n k) := by rw [← bitwise_diff, test_bit_bitwise] @[simp] lemma test_bit_lxor (m n k) : test_bit (lxor m n) k = bxor (test_bit m k) (test_bit n k) := by rw [← bitwise_xor, test_bit_bitwise] @[simp] lemma test_bit_lnot : ∀ n k, test_bit (lnot n) k = bnot (test_bit n k) | (n : ℕ) k := by simp [lnot, test_bit] | -[1+ n] k := by simp [lnot, test_bit] lemma shiftl_add : ∀ (m : ℤ) (n : ℕ) (k : ℤ), shiftl m (n + k) = shiftl (shiftl m n) k | (m : ℕ) n (k:ℕ) := congr_arg of_nat (nat.shiftl_add _ _ _) | -[1+ m] n (k:ℕ) := congr_arg neg_succ_of_nat (nat.shiftl'_add _ _ _ _) | (m : ℕ) n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl ↑m i = nat.shiftr (nat.shiftl m n) k) (λ i n, congr_arg coe $ by rw [← nat.shiftl_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg coe $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl_sub, nat.sub_self]; refl) | -[1+ m] n -[1+k] := sub_nat_nat_elim n k.succ (λ n k i, shiftl -[1+ m] i = -[1+ nat.shiftr (nat.shiftl' tt m n) k]) (λ i n, congr_arg neg_succ_of_nat $ by rw [← nat.shiftl'_sub, nat.add_sub_cancel_left]; apply nat.le_add_right) (λ i n, congr_arg neg_succ_of_nat $ by rw [add_assoc, nat.shiftr_add, ← nat.shiftl'_sub, nat.sub_self]; refl) lemma shiftl_sub (m : ℤ) (n : ℕ) (k : ℤ) : shiftl m (n - k) = shiftr (shiftl m n) k := shiftl_add _ _ _ @[simp] lemma shiftl_neg (m n : ℤ) : shiftl m (-n) = shiftr m n := rfl @[simp] lemma shiftr_neg (m n : ℤ) : shiftr m (-n) = shiftl m n := by rw [← shiftl_neg, neg_neg] @[simp] lemma shiftl_coe_nat (m n : ℕ) : shiftl m n = nat.shiftl m n := rfl @[simp] lemma shiftr_coe_nat (m n : ℕ) : shiftr m n = nat.shiftr m n := by cases n; refl @[simp] lemma shiftl_neg_succ (m n : ℕ) : shiftl -[1+ m] n = -[1+ nat.shiftl' tt m n] := rfl @[simp] lemma shiftr_neg_succ (m n : ℕ) : shiftr -[1+ m] n = -[1+ nat.shiftr m n] := by cases n; refl lemma shiftr_add : ∀ (m : ℤ) (n k : ℕ), shiftr m (n + k) = shiftr (shiftr m n) k | (m : ℕ) n k := by rw [shiftr_coe_nat, shiftr_coe_nat, ← int.coe_nat_add, shiftr_coe_nat, nat.shiftr_add] | -[1+ m] n k := by rw [shiftr_neg_succ, shiftr_neg_succ, ← int.coe_nat_add, shiftr_neg_succ, nat.shiftr_add] lemma shiftl_eq_mul_pow : ∀ (m : ℤ) (n : ℕ), shiftl m n = m * ↑(2 ^ n) | (m : ℕ) n := congr_arg coe (nat.shiftl_eq_mul_pow _ _) | -[1+ m] n := @congr_arg ℕ ℤ _ _ (λi, -i) (nat.shiftl'_tt_eq_mul_pow _ _) lemma shiftr_eq_div_pow : ∀ (m : ℤ) (n : ℕ), shiftr m n = m / ↑(2 ^ n) | (m : ℕ) n := by rw shiftr_coe_nat; exact congr_arg coe (nat.shiftr_eq_div_pow _ _) | -[1+ m] n := begin rw [shiftr_neg_succ, neg_succ_of_nat_div, nat.shiftr_eq_div_pow], refl, exact coe_nat_lt_coe_nat_of_lt (nat.pos_pow_of_pos _ dec_trivial) end lemma one_shiftl (n : ℕ) : shiftl 1 n = (2 ^ n : ℕ) := congr_arg coe (nat.one_shiftl _) @[simp] lemma zero_shiftl : ∀ n : ℤ, shiftl 0 n = 0 | (n : ℕ) := congr_arg coe (nat.zero_shiftl _) | -[1+ n] := congr_arg coe (nat.zero_shiftr _) @[simp] lemma zero_shiftr (n) : shiftr 0 n = 0 := zero_shiftl _ /- Least upper bound property for integers -/ theorem exists_least_of_bdd {P : ℤ → Prop} [HP : decidable_pred P] (Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → b ≤ z) (Hinh : ∃ z : ℤ, P z) : ∃ lb : ℤ, P lb ∧ (∀ z : ℤ, P z → lb ≤ z) := let ⟨b, Hb⟩ := Hbdd in have EX : ∃ n : ℕ, P (b + n), from let ⟨elt, Helt⟩ := Hinh in match elt, le.dest (Hb _ Helt), Helt with | ._, ⟨n, rfl⟩, Hn := ⟨n, Hn⟩ end, ⟨b + (nat.find EX : ℤ), nat.find_spec EX, λ z h, match z, le.dest (Hb _ h), h with | ._, ⟨n, rfl⟩, h := add_le_add_left (int.coe_nat_le.2 $ nat.find_min' _ h) _ end⟩ theorem exists_greatest_of_bdd {P : ℤ → Prop} [HP : decidable_pred P] (Hbdd : ∃ b : ℤ, ∀ z : ℤ, P z → z ≤ b) (Hinh : ∃ z : ℤ, P z) : ∃ ub : ℤ, P ub ∧ (∀ z : ℤ, P z → z ≤ ub) := have Hbdd' : ∃ (b : ℤ), ∀ (z : ℤ), P (-z) → b ≤ z, from let ⟨b, Hb⟩ := Hbdd in ⟨-b, λ z h, neg_le.1 (Hb _ h)⟩, have Hinh' : ∃ z : ℤ, P (-z), from let ⟨elt, Helt⟩ := Hinh in ⟨-elt, by rw [neg_neg]; exact Helt⟩, let ⟨lb, Plb, al⟩ := exists_least_of_bdd Hbdd' Hinh' in ⟨-lb, Plb, λ z h, le_neg.1 $ al _ $ by rwa neg_neg⟩ /- cast (injection into groups with one) -/ @[simp] theorem nat_cast_eq_coe_nat : ∀ n, @coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n = @coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ int.has_coe)) n | 0 := rfl | (n+1) := congr_arg (+(1:ℤ)) (nat_cast_eq_coe_nat n) section cast variables {α : Type*} section variables [has_neg α] [has_zero α] [has_one α] [has_add α] /-- Canonical homomorphism from the integers to any ring(-like) structure `α` -/ protected def cast : ℤ → α | (n : ℕ) := n | -[1+ n] := -(n+1) @[priority 0] instance cast_coe : has_coe ℤ α := ⟨int.cast⟩ @[simp] theorem cast_zero : ((0 : ℤ) : α) = 0 := rfl @[simp] theorem cast_of_nat (n : ℕ) : (of_nat n : α) = n := rfl @[simp] theorem cast_coe_nat (n : ℕ) : ((n : ℤ) : α) = n := rfl @[simp] theorem cast_coe_nat' (n : ℕ) : (@coe ℕ ℤ (@coe_to_lift _ _ (@coe_base _ _ nat.cast_coe)) n : α) = n := by simp @[simp] theorem cast_neg_succ_of_nat (n : ℕ) : (-[1+ n] : α) = -(n + 1) := rfl end @[simp] theorem cast_one [add_monoid α] [has_one α] [has_neg α] : ((1 : ℤ) : α) = 1 := nat.cast_one @[simp] theorem cast_sub_nat_nat [add_group α] [has_one α] (m n) : ((int.sub_nat_nat m n : ℤ) : α) = m - n := begin unfold sub_nat_nat, cases e : n - m, { simp [sub_nat_nat, e, nat.le_of_sub_eq_zero e] }, { rw [sub_nat_nat, cast_neg_succ_of_nat, ← nat.cast_succ, ← e, nat.cast_sub $ _root_.le_of_lt $ nat.lt_of_sub_eq_succ e, neg_sub] }, end @[simp] theorem cast_neg_of_nat [add_group α] [has_one α] : ∀ n, ((neg_of_nat n : ℤ) : α) = -n | 0 := neg_zero.symm | (n+1) := rfl @[simp] theorem cast_add [add_group α] [has_one α] : ∀ m n, ((m + n : ℤ) : α) = m + n | (m : ℕ) (n : ℕ) := nat.cast_add _ _ | (m : ℕ) -[1+ n] := cast_sub_nat_nat _ _ | -[1+ m] (n : ℕ) := (cast_sub_nat_nat _ _).trans $ sub_eq_of_eq_add $ show (n:α) = -(m+1) + n + (m+1), by rw [add_assoc, ← cast_succ, ← nat.cast_add, add_comm, nat.cast_add, cast_succ, neg_add_cancel_left] | -[1+ m] -[1+ n] := show -((m + n + 1 + 1 : ℕ) : α) = -(m + 1) + -(n + 1), by rw [← neg_add_rev, ← nat.cast_add_one, ← nat.cast_add_one, ← nat.cast_add]; apply congr_arg (λ x:ℕ, -(x:α)); simp @[simp] theorem cast_neg [add_group α] [has_one α] : ∀ n, ((-n : ℤ) : α) = -n | (n : ℕ) := cast_neg_of_nat _ | -[1+ n] := (neg_neg _).symm theorem cast_sub [add_group α] [has_one α] (m n) : ((m - n : ℤ) : α) = m - n := by simp @[simp] theorem cast_eq_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) = 0 ↔ n = 0 := ⟨λ h, begin cases n, { exact congr_arg coe (nat.cast_eq_zero.1 h) }, { rw [cast_neg_succ_of_nat, neg_eq_zero, ← cast_succ, nat.cast_eq_zero] at h, contradiction } end, λ h, by rw [h, cast_zero]⟩ @[simp] theorem cast_inj [add_group α] [has_one α] [char_zero α] {m n : ℤ} : (m : α) = n ↔ m = n := by rw [← sub_eq_zero, ← cast_sub, cast_eq_zero, sub_eq_zero] theorem cast_injective [add_group α] [has_one α] [char_zero α] : function.injective (coe : ℤ → α) | m n := cast_inj.1 @[simp] theorem cast_ne_zero [add_group α] [has_one α] [char_zero α] {n : ℤ} : (n : α) ≠ 0 ↔ n ≠ 0 := not_congr cast_eq_zero @[simp] theorem cast_mul [ring α] : ∀ m n, ((m * n : ℤ) : α) = m * n | (m : ℕ) (n : ℕ) := nat.cast_mul _ _ | (m : ℕ) -[1+ n] := (cast_neg_of_nat _).trans $ show (-(m * (n + 1) : ℕ) : α) = m * -(n + 1), by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_mul_neg] | -[1+ m] (n : ℕ) := (cast_neg_of_nat _).trans $ show (-((m + 1) * n : ℕ) : α) = -(m + 1) * n, by rw [nat.cast_mul, nat.cast_add_one, neg_mul_eq_neg_mul] | -[1+ m] -[1+ n] := show (((m + 1) * (n + 1) : ℕ) : α) = -(m + 1) * -(n + 1), by rw [nat.cast_mul, nat.cast_add_one, nat.cast_add_one, neg_mul_neg] instance cast.is_ring_hom [ring α] : is_ring_hom (int.cast : ℤ → α) := ⟨cast_one, cast_mul, cast_add⟩ theorem mul_cast_comm [ring α] (a : α) (n : ℤ) : a * n = n * a := by cases n; simp [nat.mul_cast_comm, left_distrib, right_distrib, *] @[simp] theorem cast_bit0 [ring α] (n : ℤ) : ((bit0 n : ℤ) : α) = bit0 n := cast_add _ _ @[simp] theorem cast_bit1 [ring α] (n : ℤ) : ((bit1 n : ℤ) : α) = bit1 n := by rw [bit1, cast_add, cast_one, cast_bit0]; refl lemma cast_two [ring α] : ((2 : ℤ) : α) = 2 := by simp theorem cast_nonneg [linear_ordered_ring α] : ∀ {n : ℤ}, (0 : α) ≤ n ↔ 0 ≤ n | (n : ℕ) := by simp | -[1+ n] := by simpa [not_le_of_gt (neg_succ_lt_zero n)] using show -(n:α) < 1, from lt_of_le_of_lt (by simp) zero_lt_one @[simp] theorem cast_le [linear_ordered_ring α] {m n : ℤ} : (m : α) ≤ n ↔ m ≤ n := by rw [← sub_nonneg, ← cast_sub, cast_nonneg, sub_nonneg] @[simp] theorem cast_lt [linear_ordered_ring α] {m n : ℤ} : (m : α) < n ↔ m < n := by simpa [-cast_le] using not_congr (@cast_le α _ n m) @[simp] theorem cast_nonpos [linear_ordered_ring α] {n : ℤ} : (n : α) ≤ 0 ↔ n ≤ 0 := by rw [← cast_zero, cast_le] @[simp] theorem cast_pos [linear_ordered_ring α] {n : ℤ} : (0 : α) < n ↔ 0 < n := by rw [← cast_zero, cast_lt] @[simp] theorem cast_lt_zero [linear_ordered_ring α] {n : ℤ} : (n : α) < 0 ↔ n < 0 := by rw [← cast_zero, cast_lt] theorem eq_cast [add_group α] [has_one α] (f : ℤ → α) (H1 : f 1 = 1) (Hadd : ∀ x y, f (x + y) = f x + f y) (n : ℤ) : f n = n := begin have H : ∀ (n : ℕ), f n = n := nat.eq_cast' (λ n, f n) H1 (λ x y, Hadd x y), cases n, {apply H}, apply eq_neg_of_add_eq_zero, rw [← nat.cast_zero, ← H 0, int.coe_nat_zero, ← show -[1+ n] + (↑n + 1) = 0, from neg_add_self (↑n+1), Hadd, show f (n+1) = n+1, from H (n+1)] end @[simp] theorem cast_id (n : ℤ) : ↑n = n := (eq_cast id rfl (λ _ _, rfl) n).symm @[simp] theorem cast_min [decidable_linear_ordered_comm_ring α] {a b : ℤ} : (↑(min a b) : α) = min a b := by by_cases a ≤ b; simp [h, min] @[simp] theorem cast_max [decidable_linear_ordered_comm_ring α] {a b : ℤ} : (↑(max a b) : α) = max a b := by by_cases a ≤ b; simp [h, max] @[simp] theorem cast_abs [decidable_linear_ordered_comm_ring α] {q : ℤ} : ((abs q : ℤ) : α) = abs q := by simp [abs] end cast section decidable def range (m n : ℤ) : list ℤ := (list.range (to_nat (n-m))).map $ λ r, m+r theorem mem_range_iff {m n r : ℤ} : r ∈ range m n ↔ m ≤ r ∧ r < n := ⟨λ H, let ⟨s, h1, h2⟩ := list.mem_map.1 H in h2 ▸ ⟨le_add_of_nonneg_right trivial, add_lt_of_lt_sub_left $ match n-m, h1 with | (k:ℕ), h1 := by rwa [list.mem_range, to_nat_coe_nat, ← coe_nat_lt] at h1 end⟩, λ ⟨h1, h2⟩, list.mem_map.2 ⟨to_nat (r-m), list.mem_range.2 $ by rw [← coe_nat_lt, to_nat_of_nonneg (sub_nonneg_of_le h1), to_nat_of_nonneg (sub_nonneg_of_le (le_of_lt (lt_of_le_of_lt h1 h2)))]; exact sub_lt_sub_right h2 _, show m + _ = _, by rw [to_nat_of_nonneg (sub_nonneg_of_le h1), add_sub_cancel'_right]⟩⟩ instance decidable_le_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r < n → P r) := decidable_of_iff (∀ r ∈ range m n, P r) $ by simp only [mem_range_iff, and_imp] instance decidable_le_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m ≤ r → r ≤ n → P r) := decidable_of_iff (∀ r ∈ range m (n+1), P r) $ by simp only [mem_range_iff, and_imp, lt_add_one_iff] instance decidable_lt_lt (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r < n → P r) := int.decidable_le_lt P _ _ instance decidable_lt_le (P : int → Prop) [decidable_pred P] (m n : ℤ) : decidable (∀ r, m < r → r ≤ n → P r) := int.decidable_le_le P _ _ end decidable end int
37e63924c6ea842a9c445b45370d7babbc57683f
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/t11.lean
d7a077f6199dc6c6d0613aae6efcec1ed5559533
[ "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
393
lean
prelude constant A : Type.{1} definition bool : Type.{1} := Type.{0} constant Exists (P : A → bool) : bool notation `exists` binders `, ` b:(scoped b, Exists b) := b notation `∃` binders `, ` b:(scoped b, Exists b) := b constant p : A → bool constant q : A → A → bool #check exists x : A, p x #check ∃ x y : A, q x y notation `{` binder `|` b:scoped `}` := b #check {x : A | x}
da7cd65a2db5db992a5f2eb90a2ea9af1bf39f33
8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3
/src/measure_theory/measure/measure_space.lean
ea4d5fd3d447282ba843ded3938b117e26c12150
[ "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
117,954
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import measure_theory.measure.measure_space_def import measure_theory.measurable_space /-! # Measure spaces The definition of a measure and a measure space are in `measure_theory.measure_space_def`, with only a few basic properties. This file provides many more properties of these objects. This separation allows the measurability tactic to import only the file `measure_space_def`, and to be available in `measure_space` (through `measurable_space`). Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the extended nonnegative reals that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint sets is equal to the measure of the individual sets. Every measure can be canonically extended to an outer measure, so that it assigns values to all subsets, not just the measurable subsets. On the other hand, a measure that is countably additive on measurable sets can be restricted to measurable sets to obtain a measure. In this file a measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ℝ≥0∞`. We introduce the following typeclasses for measures: * `probability_measure μ`: `μ univ = 1`; * `finite_measure μ`: `μ univ < ∞`; * `sigma_finite μ`: there exists a countable collection of measurable sets that cover `univ` where `μ` is finite; * `locally_finite_measure μ` : `∀ x, ∃ s ∈ 𝓝 x, μ s < ∞`; * `has_no_atoms μ` : `∀ x, μ {x} = 0`; possibly should be redefined as `∀ s, 0 < μ s → ∃ t ⊆ s, 0 < μ t ∧ μ t < μ s`. Given a measure, the null sets are the sets where `μ s = 0`, where `μ` denotes the corresponding outer measure (so `s` might not be measurable). We can then define the completion of `μ` as the measure on the least `σ`-algebra that also contains all null sets, by defining the measure to be `0` on the null sets. ## Main statements * `completion` is the completion of a measure to all null measurable sets. * `measure.of_measurable` and `outer_measure.to_measure` are two important ways to define a measure. ## Implementation notes Given `μ : measure α`, `μ s` is the value of the *outer measure* applied to `s`. This conveniently allows us to apply the measure to sets without proving that they are measurable. We get countable subadditivity for all sets, but only countable additivity for measurable sets. You often don't want to define a measure via its constructor. Two ways that are sometimes more convenient: * `measure.of_measurable` is a way to define a measure by only giving its value on measurable sets and proving the properties (1) and (2) mentioned above. * `outer_measure.to_measure` is a way of obtaining a measure from an outer measure by showing that all measurable sets in the measurable space are Carathéodory measurable. To prove that two measures are equal, there are multiple options: * `ext`: two measures are equal if they are equal on all measurable sets. * `ext_of_generate_from_of_Union`: two measures are equal if they are equal on a π-system generating the measurable sets, if the π-system contains a spanning increasing sequence of sets where the measures take finite value (in particular the measures are σ-finite). This is a special case of the more general `ext_of_generate_from_of_cover` * `ext_of_generate_finite`: two finite measures are equal if they are equal on a π-system generating the measurable sets. This is a special case of `ext_of_generate_from_of_Union` using `C ∪ {univ}`, but is easier to work with. A `measure_space` is a class that is a measurable space with a canonical measure. The measure is denoted `volume`. ## References * <https://en.wikipedia.org/wiki/Measure_(mathematics)> * <https://en.wikipedia.org/wiki/Complete_measure> * <https://en.wikipedia.org/wiki/Almost_everywhere> ## Tags measure, almost everywhere, measure space, completion, null set, null measurable set -/ noncomputable theory open classical set filter (hiding map) function measurable_space open_locale classical topological_space big_operators filter ennreal nnreal variables {α β γ δ ι : Type*} namespace measure_theory section variables {m : measurable_space α} {μ μ₁ μ₂ : measure α} {s s₁ s₂ t : set α} instance ae_is_measurably_generated : is_measurably_generated μ.ae := ⟨λ s hs, let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs in ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩ lemma measure_Union [encodable β] {f : β → set α} (hn : pairwise (disjoint on f)) (h : ∀ i, measurable_set (f i)) : μ (⋃ i, f i) = ∑' i, μ (f i) := begin rw [measure_eq_extend (measurable_set.Union h), extend_Union measurable_set.empty _ measurable_set.Union _ hn h], { simp [measure_eq_extend, h] }, { exact μ.empty }, { exact μ.m_Union } end lemma measure_union (hd : disjoint s₁ s₂) (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ := begin rw [union_eq_Union, measure_Union, tsum_fintype, fintype.sum_bool, cond, cond], exacts [pairwise_disjoint_on_bool.2 hd, λ b, bool.cases_on b h₂ h₁] end lemma measure_add_measure_compl (h : measurable_set s) : μ s + μ sᶜ = μ univ := by { rw [← union_compl_self s, measure_union _ h h.compl], exact disjoint_compl_right } lemma measure_bUnion {s : set β} {f : β → set α} (hs : countable s) (hd : pairwise_on s (disjoint on f)) (h : ∀ b ∈ s, measurable_set (f b)) : μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) := begin haveI := hs.to_encodable, rw bUnion_eq_Union, exact measure_Union (hd.on_injective subtype.coe_injective $ λ x, x.2) (λ x, h x x.2) end lemma measure_sUnion {S : set (set α)} (hs : countable S) (hd : pairwise_on S disjoint) (h : ∀ s ∈ S, measurable_set s) : μ (⋃₀ S) = ∑' s : S, μ s := by rw [sUnion_eq_bUnion, measure_bUnion hs hd h] lemma measure_bUnion_finset {s : finset ι} {f : ι → set α} (hd : pairwise_on ↑s (disjoint on f)) (hm : ∀ b ∈ s, measurable_set (f b)) : μ (⋃ b ∈ s, f b) = ∑ p in s, μ (f p) := begin rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype], exact measure_bUnion s.countable_to_set hd hm end /-- If `s` is a countable set, then the measure of its preimage can be found as the sum of measures of the fibers `f ⁻¹' {y}`. -/ lemma tsum_measure_preimage_singleton {s : set β} (hs : countable s) {f : α → β} (hf : ∀ y ∈ s, measurable_set (f ⁻¹' {y})) : ∑' b : s, μ (f ⁻¹' {↑b}) = μ (f ⁻¹' s) := by rw [← set.bUnion_preimage_singleton, measure_bUnion hs (pairwise_on_disjoint_fiber _ _) hf] /-- If `s` is a `finset`, then the measure of its preimage can be found as the sum of measures of the fibers `f ⁻¹' {y}`. -/ lemma sum_measure_preimage_singleton (s : finset β) {f : α → β} (hf : ∀ y ∈ s, measurable_set (f ⁻¹' {y})) : ∑ b in s, μ (f ⁻¹' {b}) = μ (f ⁻¹' ↑s) := by simp only [← measure_bUnion_finset (pairwise_on_disjoint_fiber _ _) hf, finset.set_bUnion_preimage_singleton] lemma measure_diff_null' (h : μ (s₁ ∩ s₂) = 0) : μ (s₁ \ s₂) = μ s₁ := measure_congr $ diff_ae_eq_self.2 h lemma measure_diff_null (h : μ s₂ = 0) : μ (s₁ \ s₂) = μ s₁ := measure_diff_null' $ measure_mono_null (inter_subset_right _ _) h lemma measure_diff (h : s₂ ⊆ s₁) (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) (h_fin : μ s₂ < ∞) : μ (s₁ \ s₂) = μ s₁ - μ s₂ := begin refine (ennreal.add_sub_self' h_fin).symm.trans _, rw [← measure_union disjoint_diff h₂ (h₁.diff h₂), union_diff_cancel h] end lemma meas_eq_meas_of_null_diff {s t : set α} (hst : s ⊆ t) (h_nulldiff : μ (t.diff s) = 0) : μ s = μ t := by { rw [←diff_diff_cancel_left hst, ←@measure_diff_null _ _ _ t _ h_nulldiff], refl, } lemma meas_eq_meas_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃ \ s₁) = 0) : (μ s₁ = μ s₂) ∧ (μ s₂ = μ s₃) := begin have le12 : μ s₁ ≤ μ s₂ := measure_mono h12, have le23 : μ s₂ ≤ μ s₃ := measure_mono h23, have key : μ s₃ ≤ μ s₁ := calc μ s₃ = μ ((s₃ \ s₁) ∪ s₁) : by rw (diff_union_of_subset (h12.trans h23)) ... ≤ μ (s₃ \ s₁) + μ s₁ : measure_union_le _ _ ... = μ s₁ : by simp only [h_nulldiff, zero_add], exact ⟨le12.antisymm (le23.trans key), le23.antisymm (key.trans le12)⟩, end lemma meas_eq_meas_smaller_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃.diff s₁) = 0) : μ s₁ = μ s₂ := (meas_eq_meas_of_between_null_diff h12 h23 h_nulldiff).1 lemma meas_eq_meas_larger_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃.diff s₁) = 0) : μ s₂ = μ s₃ := (meas_eq_meas_of_between_null_diff h12 h23 h_nulldiff).2 lemma measure_compl (h₁ : measurable_set s) (h_fin : μ s < ∞) : μ (sᶜ) = μ univ - μ s := by { rw compl_eq_univ_diff, exact measure_diff (subset_univ s) measurable_set.univ h₁ h_fin } lemma sum_measure_le_measure_univ {s : finset ι} {t : ι → set α} (h : ∀ i ∈ s, measurable_set (t i)) (H : pairwise_on ↑s (disjoint on t)) : ∑ i in s, μ (t i) ≤ μ (univ : set α) := by { rw ← measure_bUnion_finset H h, exact measure_mono (subset_univ _) } lemma tsum_measure_le_measure_univ {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (H : pairwise (disjoint on s)) : ∑' i, μ (s i) ≤ μ (univ : set α) := begin rw [ennreal.tsum_eq_supr_sum], exact supr_le (λ s, sum_measure_le_measure_univ (λ i hi, hs i) (λ i hi j hj hij, H i j hij)) end /-- Pigeonhole principle for measure spaces: if `∑' i, μ (s i) > μ univ`, then one of the intersections `s i ∩ s j` is not empty. -/ lemma exists_nonempty_inter_of_measure_univ_lt_tsum_measure {m : measurable_space α} (μ : measure α) {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (H : μ (univ : set α) < ∑' i, μ (s i)) : ∃ i j (h : i ≠ j), (s i ∩ s j).nonempty := begin contrapose! H, apply tsum_measure_le_measure_univ hs, exact λ i j hij x hx, H i j hij ⟨x, hx⟩ end /-- Pigeonhole principle for measure spaces: if `s` is a `finset` and `∑ i in s, μ (t i) > μ univ`, then one of the intersections `t i ∩ t j` is not empty. -/ lemma exists_nonempty_inter_of_measure_univ_lt_sum_measure {m : measurable_space α} (μ : measure α) {s : finset ι} {t : ι → set α} (h : ∀ i ∈ s, measurable_set (t i)) (H : μ (univ : set α) < ∑ i in s, μ (t i)) : ∃ (i ∈ s) (j ∈ s) (h : i ≠ j), (t i ∩ t j).nonempty := begin contrapose! H, apply sum_measure_le_measure_univ h, exact λ i hi j hj hij x hx, H i hi j hj hij ⟨x, hx⟩ end /-- Continuity from below: the measure of the union of a directed sequence of measurable sets is the supremum of the measures. -/ lemma measure_Union_eq_supr [encodable ι] {s : ι → set α} (h : ∀ i, measurable_set (s i)) (hd : directed (⊆) s) : μ (⋃ i, s i) = ⨆ i, μ (s i) := begin by_cases hι : nonempty ι, swap, { simp only [supr_of_empty hι, Union], exact measure_empty }, resetI, refine le_antisymm _ (supr_le $ λ i, measure_mono $ subset_Union _ _), have : ∀ n, measurable_set (disjointed (λ n, ⋃ b ∈ encodable.decode₂ ι n, s b) n) := measurable_set.disjointed (measurable_set.bUnion_decode₂ h), rw [← encodable.Union_decode₂, ← Union_disjointed, measure_Union (disjoint_disjointed _) this, ennreal.tsum_eq_supr_nat], simp only [← measure_bUnion_finset ((disjoint_disjointed _).pairwise_on _) (λ n _, this n)], refine supr_le (λ n, _), refine le_trans (_ : _ ≤ μ (⋃ (k ∈ finset.range n) (i ∈ encodable.decode₂ ι k), s i)) _, exact measure_mono (bUnion_subset_bUnion_right (λ k hk, disjointed_subset _ _)), simp only [← finset.set_bUnion_option_to_finset, ← finset.set_bUnion_bUnion], generalize : (finset.range n).bUnion (λ k, (encodable.decode₂ ι k).to_finset) = t, rcases hd.finset_le t with ⟨i, hi⟩, exact le_supr_of_le i (measure_mono $ bUnion_subset hi) end lemma measure_bUnion_eq_supr {s : ι → set α} {t : set ι} (ht : countable t) (h : ∀ i ∈ t, measurable_set (s i)) (hd : directed_on ((⊆) on s) t) : μ (⋃ i ∈ t, s i) = ⨆ i ∈ t, μ (s i) := begin haveI := ht.to_encodable, rw [bUnion_eq_Union, measure_Union_eq_supr (set_coe.forall'.1 h) hd.directed_coe, supr_subtype'], refl end /-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable sets is the infimum of the measures. -/ lemma measure_Inter_eq_infi [encodable ι] {s : ι → set α} (h : ∀ i, measurable_set (s i)) (hd : directed (⊇) s) (hfin : ∃ i, μ (s i) < ∞) : μ (⋂ i, s i) = (⨅ i, μ (s i)) := begin rcases hfin with ⟨k, hk⟩, rw [← ennreal.sub_sub_cancel (by exact hk) (infi_le _ k), ennreal.sub_infi, ← ennreal.sub_sub_cancel (by exact hk) (measure_mono (Inter_subset _ k)), ← measure_diff (Inter_subset _ k) (h k) (measurable_set.Inter h) (lt_of_le_of_lt (measure_mono (Inter_subset _ k)) hk), diff_Inter, measure_Union_eq_supr], { congr' 1, refine le_antisymm (supr_le_supr2 $ λ i, _) (supr_le_supr $ λ i, _), { rcases hd i k with ⟨j, hji, hjk⟩, use j, rw [← measure_diff hjk (h _) (h _) ((measure_mono hjk).trans_lt hk)], exact measure_mono (diff_subset_diff_right hji) }, { rw [ennreal.sub_le_iff_le_add, ← measure_union disjoint_diff.symm ((h k).diff (h i)) (h i), set.union_comm], exact measure_mono (diff_subset_iff.1 $ subset.refl _) } }, { exact λ i, (h k).diff (h i) }, { exact hd.mono_comp _ (λ _ _, diff_subset_diff_right) } end lemma measure_eq_inter_diff (hs : measurable_set s) (ht : measurable_set t) : μ s = μ (s ∩ t) + μ (s \ t) := have hd : disjoint (s ∩ t) (s \ t) := assume a ⟨⟨_, hs⟩, _, hns⟩, hns hs , by rw [← measure_union hd (hs.inter ht) (hs.diff ht), inter_union_diff s t] lemma measure_union_add_inter (hs : measurable_set s) (ht : measurable_set t) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by { rw [measure_eq_inter_diff (hs.union ht) ht, set.union_inter_cancel_right, union_diff_right, measure_eq_inter_diff hs ht], ac_refl } /-- Continuity from below: the measure of the union of an increasing sequence of measurable sets is the limit of the measures. -/ lemma tendsto_measure_Union {s : ℕ → set α} (hs : ∀ n, measurable_set (s n)) (hm : monotone s) : tendsto (μ ∘ s) at_top (𝓝 (μ (⋃ n, s n))) := begin rw measure_Union_eq_supr hs (directed_of_sup hm), exact tendsto_at_top_supr (assume n m hnm, measure_mono $ hm hnm) end /-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable sets is the limit of the measures. -/ lemma tendsto_measure_Inter {s : ℕ → set α} (hs : ∀ n, measurable_set (s n)) (hm : ∀ ⦃n m⦄, n ≤ m → s m ⊆ s n) (hf : ∃ i, μ (s i) < ∞) : tendsto (μ ∘ s) at_top (𝓝 (μ (⋂ n, s n))) := begin rw measure_Inter_eq_infi hs (directed_of_sup hm) hf, exact tendsto_at_top_infi (assume n m hnm, measure_mono $ hm hnm), end /-- One direction of the **Borel-Cantelli lemma**: if (sᵢ) is a sequence of measurable sets such that ∑ μ sᵢ exists, then the limit superior of the sᵢ is a null set. -/ lemma measure_limsup_eq_zero {s : ℕ → set α} (hs : ∀ i, measurable_set (s i)) (hs' : ∑' i, μ (s i) ≠ ∞) : μ (limsup at_top s) = 0 := begin simp only [limsup_eq_infi_supr_of_nat', set.infi_eq_Inter, set.supr_eq_Union], -- We will show that both `μ (⨅ n, ⨆ i, s (i + n))` and `0` are the limit of `μ (⊔ i, s (i + n))` -- as `n` tends to infinity. For the former, we use continuity from above. refine tendsto_nhds_unique (tendsto_measure_Inter (λ i, measurable_set.Union (λ b, hs (b + i))) _ ⟨0, lt_of_le_of_lt (measure_Union_le s) (ennreal.lt_top_iff_ne_top.2 hs')⟩) _, { intros n m hnm x, simp only [set.mem_Union], exact λ ⟨i, hi⟩, ⟨i + (m - n), by simpa only [add_assoc, nat.sub_add_cancel hnm] using hi⟩ }, { -- For the latter, notice that, `μ (⨆ i, s (i + n)) ≤ ∑' s (i + n)`. Since the right hand side -- converges to `0` by hypothesis, so does the former and the proof is complete. exact (tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (ennreal.tendsto_sum_nat_add (μ ∘ s) hs') (eventually_of_forall (by simp only [forall_const, zero_le])) (eventually_of_forall (λ i, measure_Union_le _))) } end lemma measure_if {x : β} {t : set β} {s : set α} : μ (if x ∈ t then s else ∅) = indicator t (λ _, μ s) x := by { split_ifs; simp [h] } end section outer_measure variables [ms : measurable_space α] {s t : set α} include ms /-- Obtain a measure by giving an outer measure where all sets in the σ-algebra are Carathéodory measurable. -/ def outer_measure.to_measure (m : outer_measure α) (h : ms ≤ m.caratheodory) : measure α := measure.of_measurable (λ s _, m s) m.empty (λ f hf hd, m.Union_eq_of_caratheodory (λ i, h _ (hf i)) hd) lemma le_to_outer_measure_caratheodory (μ : measure α) : ms ≤ μ.to_outer_measure.caratheodory := begin assume s hs, rw to_outer_measure_eq_induced_outer_measure, refine outer_measure.of_function_caratheodory (λ t, le_infi $ λ ht, _), rw [← measure_eq_extend (ht.inter hs), ← measure_eq_extend (ht.diff hs), ← measure_union _ (ht.inter hs) (ht.diff hs), inter_union_diff], exact le_refl _, exact λ x ⟨⟨_, h₁⟩, _, h₂⟩, h₂ h₁ end @[simp] lemma to_measure_to_outer_measure (m : outer_measure α) (h : ms ≤ m.caratheodory) : (m.to_measure h).to_outer_measure = m.trim := rfl @[simp] lemma to_measure_apply (m : outer_measure α) (h : ms ≤ m.caratheodory) {s : set α} (hs : measurable_set s) : m.to_measure h s = m s := m.trim_eq hs lemma le_to_measure_apply (m : outer_measure α) (h : ms ≤ m.caratheodory) (s : set α) : m s ≤ m.to_measure h s := m.le_trim s @[simp] lemma to_outer_measure_to_measure {μ : measure α} : μ.to_outer_measure.to_measure (le_to_outer_measure_caratheodory _) = μ := measure.ext $ λ s, μ.to_outer_measure.trim_eq end outer_measure variables {m0 : measurable_space α} [measurable_space β] [measurable_space γ] variables {μ μ₁ μ₂ μ₃ ν ν' ν₁ ν₂ : measure α} {s s' t : set α} namespace measure protected lemma caratheodory {m : measurable_space α} (μ : measure α) (hs : measurable_set s) : μ (t ∩ s) + μ (t \ s) = μ t := (le_to_outer_measure_caratheodory μ s hs t).symm /-! ### The `ℝ≥0∞`-module of measures -/ instance [measurable_space α] : has_zero (measure α) := ⟨{ to_outer_measure := 0, m_Union := λ f hf hd, tsum_zero.symm, trimmed := outer_measure.trim_zero }⟩ @[simp] theorem zero_to_outer_measure {m : measurable_space α} : (0 : measure α).to_outer_measure = 0 := rfl @[simp, norm_cast] theorem coe_zero {m : measurable_space α} : ⇑(0 : measure α) = 0 := rfl lemma eq_zero_of_not_nonempty {m : measurable_space α} (h : ¬nonempty α) (μ : measure α) : μ = 0 := ext $ λ s hs, by simp only [eq_empty_of_not_nonempty h s, measure_empty] instance [measurable_space α] : inhabited (measure α) := ⟨0⟩ instance [measurable_space α] : has_add (measure α) := ⟨λ μ₁ μ₂, { to_outer_measure := μ₁.to_outer_measure + μ₂.to_outer_measure, m_Union := λ s hs hd, show μ₁ (⋃ i, s i) + μ₂ (⋃ i, s i) = ∑' i, (μ₁ (s i) + μ₂ (s i)), by rw [ennreal.tsum_add, measure_Union hd hs, measure_Union hd hs], trimmed := by rw [outer_measure.trim_add, μ₁.trimmed, μ₂.trimmed] }⟩ @[simp] theorem add_to_outer_measure {m : measurable_space α} (μ₁ μ₂ : measure α) : (μ₁ + μ₂).to_outer_measure = μ₁.to_outer_measure + μ₂.to_outer_measure := rfl @[simp, norm_cast] theorem coe_add {m : measurable_space α} (μ₁ μ₂ : measure α) : ⇑(μ₁ + μ₂) = μ₁ + μ₂ := rfl theorem add_apply {m : measurable_space α} (μ₁ μ₂ : measure α) (s : set α) : (μ₁ + μ₂) s = μ₁ s + μ₂ s := rfl instance add_comm_monoid [measurable_space α] : add_comm_monoid (measure α) := to_outer_measure_injective.add_comm_monoid to_outer_measure zero_to_outer_measure add_to_outer_measure instance [measurable_space α] : has_scalar ℝ≥0∞ (measure α) := ⟨λ c μ, { to_outer_measure := c • μ.to_outer_measure, m_Union := λ s hs hd, by simp [measure_Union, *, ennreal.tsum_mul_left], trimmed := by rw [outer_measure.trim_smul, μ.trimmed] }⟩ @[simp] theorem smul_to_outer_measure {m : measurable_space α} (c : ℝ≥0∞) (μ : measure α) : (c • μ).to_outer_measure = c • μ.to_outer_measure := rfl @[simp, norm_cast] theorem coe_smul {m : measurable_space α} (c : ℝ≥0∞) (μ : measure α) : ⇑(c • μ) = c • μ := rfl theorem smul_apply {m : measurable_space α} (c : ℝ≥0∞) (μ : measure α) (s : set α) : (c • μ) s = c * μ s := rfl instance [measurable_space α] : module ℝ≥0∞ (measure α) := injective.module ℝ≥0∞ ⟨to_outer_measure, zero_to_outer_measure, add_to_outer_measure⟩ to_outer_measure_injective smul_to_outer_measure @[simp, norm_cast] theorem coe_nnreal_smul {m : measurable_space α} (c : ℝ≥0) (μ : measure α) : ⇑(c • μ) = c • μ := rfl /-! ### The complete lattice of measures -/ /-- Measures are partially ordered. The definition of less equal here is equivalent to the definition without the measurable set condition, and this is shown by `measure.le_iff'`. It is defined this way since, to prove `μ ≤ ν`, we may simply `intros s hs` instead of rewriting followed by `intros s hs`. -/ instance [measurable_space α] : partial_order (measure α) := { le := λ m₁ m₂, ∀ s, measurable_set s → m₁ s ≤ m₂ s, le_refl := assume m s hs, le_refl _, le_trans := assume m₁ m₂ m₃ h₁ h₂ s hs, le_trans (h₁ s hs) (h₂ s hs), le_antisymm := assume m₁ m₂ h₁ h₂, ext $ assume s hs, le_antisymm (h₁ s hs) (h₂ s hs) } theorem le_iff : μ₁ ≤ μ₂ ↔ ∀ s, measurable_set s → μ₁ s ≤ μ₂ s := iff.rfl theorem to_outer_measure_le : μ₁.to_outer_measure ≤ μ₂.to_outer_measure ↔ μ₁ ≤ μ₂ := by rw [← μ₂.trimmed, outer_measure.le_trim_iff]; refl theorem le_iff' : μ₁ ≤ μ₂ ↔ ∀ s, μ₁ s ≤ μ₂ s := to_outer_measure_le.symm theorem lt_iff : μ < ν ↔ μ ≤ ν ∧ ∃ s, measurable_set s ∧ μ s < ν s := lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff, not_forall, not_le, exists_prop] theorem lt_iff' : μ < ν ↔ μ ≤ ν ∧ ∃ s, μ s < ν s := lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff', not_forall, not_le] instance covariant_add_le [measurable_space α] : covariant_class (measure α) (measure α) (+) (≤) := ⟨λ ν μ₁ μ₂ hμ s hs, add_le_add_left (hμ s hs) _⟩ protected lemma le_add_left (h : μ ≤ ν) : μ ≤ ν' + ν := λ s hs, le_add_left (h s hs) protected lemma le_add_right (h : μ ≤ ν) : μ ≤ ν + ν' := λ s hs, le_add_right (h s hs) section Inf variables {m : set (measure α)} lemma Inf_caratheodory (s : set α) (hs : measurable_set s) : (Inf (to_outer_measure '' m)).caratheodory.measurable_set' s := begin rw [outer_measure.Inf_eq_bounded_by_Inf_gen], refine outer_measure.bounded_by_caratheodory (λ t, _), simp only [outer_measure.Inf_gen, le_infi_iff, ball_image_iff, coe_to_outer_measure, measure_eq_infi t], intros μ hμ u htu hu, have hm : ∀ {s t}, s ⊆ t → outer_measure.Inf_gen (to_outer_measure '' m) s ≤ μ t, { intros s t hst, rw [outer_measure.Inf_gen_def], refine infi_le_of_le (μ.to_outer_measure) (infi_le_of_le (mem_image_of_mem _ hμ) _), rw [to_outer_measure_apply], refine measure_mono hst }, rw [measure_eq_inter_diff hu hs], refine add_le_add (hm $ inter_subset_inter_left _ htu) (hm $ diff_subset_diff_left htu) end instance [measurable_space α] : has_Inf (measure α) := ⟨λ m, (Inf (to_outer_measure '' m)).to_measure $ Inf_caratheodory⟩ lemma Inf_apply (hs : measurable_set s) : Inf m s = Inf (to_outer_measure '' m) s := to_measure_apply _ _ hs private lemma measure_Inf_le (h : μ ∈ m) : Inf m ≤ μ := have Inf (to_outer_measure '' m) ≤ μ.to_outer_measure := Inf_le (mem_image_of_mem _ h), assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s private lemma measure_le_Inf (h : ∀ μ' ∈ m, μ ≤ μ') : μ ≤ Inf m := have μ.to_outer_measure ≤ Inf (to_outer_measure '' m) := le_Inf $ ball_image_of_ball $ assume μ hμ, to_outer_measure_le.2 $ h _ hμ, assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s instance [measurable_space α] : complete_semilattice_Inf (measure α) := { Inf_le := λ s a, measure_Inf_le, le_Inf := λ s a, measure_le_Inf, ..(by apply_instance : partial_order (measure α)), ..(by apply_instance : has_Inf (measure α)), } instance [measurable_space α] : complete_lattice (measure α) := { bot := 0, bot_le := assume a s hs, by exact bot_le, /- Adding an explicit `top` makes `leanchecker` fail, see lean#364, disable for now top := (⊤ : outer_measure α).to_measure (by rw [outer_measure.top_caratheodory]; exact le_top), le_top := assume a s hs, by cases s.eq_empty_or_nonempty with h h; simp [h, to_measure_apply ⊤ _ hs, outer_measure.top_apply], -/ .. complete_lattice_of_complete_semilattice_Inf (measure α) } end Inf protected lemma zero_le {m0 : measurable_space α} (μ : measure α) : 0 ≤ μ := bot_le lemma nonpos_iff_eq_zero' : μ ≤ 0 ↔ μ = 0 := μ.zero_le.le_iff_eq @[simp] lemma measure_univ_eq_zero : μ univ = 0 ↔ μ = 0 := ⟨λ h, bot_unique $ λ s hs, trans_rel_left (≤) (measure_mono (subset_univ s)) h, λ h, h.symm ▸ rfl⟩ /-! ### Pushforward and pullback -/ /-- Lift a linear map between `outer_measure` spaces such that for each measure `μ` every measurable set is caratheodory-measurable w.r.t. `f μ` to a linear map between `measure` spaces. -/ def lift_linear {m0 : measurable_space α} (f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β) (hf : ∀ μ : measure α, ‹_› ≤ (f μ.to_outer_measure).caratheodory) : measure α →ₗ[ℝ≥0∞] measure β := { to_fun := λ μ, (f μ.to_outer_measure).to_measure (hf μ), map_add' := λ μ₁ μ₂, ext $ λ s hs, by simp [hs], map_smul' := λ c μ, ext $ λ s hs, by simp [hs] } @[simp] lemma lift_linear_apply {f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β} (hf) {s : set β} (hs : measurable_set s) : lift_linear f hf μ s = f μ.to_outer_measure s := to_measure_apply _ _ hs lemma le_lift_linear_apply {f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β} (hf) (s : set β) : f μ.to_outer_measure s ≤ lift_linear f hf μ s := le_to_measure_apply _ _ s /-- The pushforward of a measure. It is defined to be `0` if `f` is not a measurable function. -/ def map [measurable_space α] (f : α → β) : measure α →ₗ[ℝ≥0∞] measure β := if hf : measurable f then lift_linear (outer_measure.map f) $ λ μ s hs t, le_to_outer_measure_caratheodory μ _ (hf hs) (f ⁻¹' t) else 0 /-- We can evaluate the pushforward on measurable sets. For non-measurable sets, see `measure_theory.measure.le_map_apply` and `measurable_equiv.map_apply`. -/ @[simp] theorem map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : map f μ s = μ (f ⁻¹' s) := by simp [map, dif_pos hf, hs] lemma map_to_outer_measure {f : α → β} (hf : measurable f) : (map f μ).to_outer_measure = (outer_measure.map f μ.to_outer_measure).trim := begin rw [← trimmed, outer_measure.trim_eq_trim_iff], intros s hs, rw [coe_to_outer_measure, map_apply hf hs, outer_measure.map_apply, coe_to_outer_measure] end theorem map_of_not_measurable {f : α → β} (hf : ¬measurable f) : map f μ = 0 := by rw [map, dif_neg hf, linear_map.zero_apply] @[simp] lemma map_id : map id μ = μ := ext $ λ s, map_apply measurable_id lemma map_map {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : map g (map f μ) = map (g ∘ f) μ := ext $ λ s hs, by simp [hf, hg, hs, hg hs, hg.comp hf, ← preimage_comp] @[mono] lemma map_mono (f : α → β) (h : μ ≤ ν) : map f μ ≤ map f ν := if hf : measurable f then λ s hs, by simp only [map_apply hf hs, h _ (hf hs)] else by simp only [map_of_not_measurable hf, le_rfl] /-- Even if `s` is not measurable, we can bound `map f μ s` from below. See also `measurable_equiv.map_apply`. -/ theorem le_map_apply {f : α → β} (hf : measurable f) (s : set β) : μ (f ⁻¹' s) ≤ map f μ s := begin rw [measure_eq_infi' (map f μ)], refine le_infi _, rintro ⟨t, hst, ht⟩, convert measure_mono (preimage_mono hst), exact map_apply hf ht end /-- Even if `s` is not measurable, `map f μ s = 0` implies that `μ (f ⁻¹' s) = 0`. -/ lemma preimage_null_of_map_null {f : α → β} (hf : measurable f) {s : set β} (hs : map f μ s = 0) : μ (f ⁻¹' s) = 0 := nonpos_iff_eq_zero.mp $ (le_map_apply hf s).trans_eq hs lemma tendsto_ae_map {f : α → β} (hf : measurable f) : tendsto f μ.ae (map f μ).ae := λ s hs, preimage_null_of_map_null hf hs /-- Pullback of a `measure`. If `f` sends each `measurable` set to a `measurable` set, then for each measurable set `s` we have `comap f μ s = μ (f '' s)`. -/ def comap [measurable_space α] (f : α → β) : measure β →ₗ[ℝ≥0∞] measure α := if hf : injective f ∧ ∀ s, measurable_set s → measurable_set (f '' s) then lift_linear (outer_measure.comap f) $ λ μ s hs t, begin simp only [coe_to_outer_measure, outer_measure.comap_apply, ← image_inter hf.1, image_diff hf.1], apply le_to_outer_measure_caratheodory, exact hf.2 s hs end else 0 lemma comap_apply {β} [measurable_space α] {mβ : measurable_space β} (f : α → β) (hfi : injective f) (hf : ∀ s, measurable_set s → measurable_set (f '' s)) (μ : measure β) (hs : measurable_set s) : comap f μ s = μ (f '' s) := begin rw [comap, dif_pos, lift_linear_apply _ hs, outer_measure.comap_apply, coe_to_outer_measure], exact ⟨hfi, hf⟩ end /-! ### Restricting a measure -/ /-- Restrict a measure `μ` to a set `s` as an `ℝ≥0∞`-linear map. -/ def restrictₗ {m0 : measurable_space α} (s : set α) : measure α →ₗ[ℝ≥0∞] measure α := lift_linear (outer_measure.restrict s) $ λ μ s' hs' t, begin suffices : μ (s ∩ t) = μ (s ∩ t ∩ s') + μ (s ∩ t \ s'), { simpa [← set.inter_assoc, set.inter_comm _ s, ← inter_diff_assoc] }, exact le_to_outer_measure_caratheodory _ _ hs' _, end /-- Restrict a measure `μ` to a set `s`. -/ def restrict {m0 : measurable_space α} (μ : measure α) (s : set α) : measure α := restrictₗ s μ @[simp] lemma restrictₗ_apply {m0 : measurable_space α} (s : set α) (μ : measure α) : restrictₗ s μ = μ.restrict s := rfl /-- If `t` is a measurable set, then the measure of `t` with respect to the restriction of the measure to `s` equals the outer measure of `t ∩ s`. An alternate version requiring that `s` be measurable instead of `t` exists as `measure.restrict_apply'`. -/ @[simp] lemma restrict_apply (ht : measurable_set t) : μ.restrict s t = μ (t ∩ s) := by simp [← restrictₗ_apply, restrictₗ, ht] lemma restrict_eq_self (h_meas_t : measurable_set t) (h : t ⊆ s) : μ.restrict s t = μ t := by rw [restrict_apply h_meas_t, inter_eq_left_iff_subset.mpr h] lemma restrict_apply_self {m0 : measurable_space α} (μ : measure α) (h_meas_s : measurable_set s) : (μ.restrict s) s = μ s := (restrict_eq_self h_meas_s (set.subset.refl _)) lemma restrict_apply_univ (s : set α) : μ.restrict s univ = μ s := by rw [restrict_apply measurable_set.univ, set.univ_inter] lemma le_restrict_apply (s t : set α) : μ (t ∩ s) ≤ μ.restrict s t := by { rw [restrict, restrictₗ], convert le_lift_linear_apply _ t, simp } @[simp] lemma restrict_add {m0 : measurable_space α} (μ ν : measure α) (s : set α) : (μ + ν).restrict s = μ.restrict s + ν.restrict s := (restrictₗ s).map_add μ ν @[simp] lemma restrict_zero {m0 : measurable_space α} (s : set α) : (0 : measure α).restrict s = 0 := (restrictₗ s).map_zero @[simp] lemma restrict_smul {m0 : measurable_space α} (c : ℝ≥0∞) (μ : measure α) (s : set α) : (c • μ).restrict s = c • μ.restrict s := (restrictₗ s).map_smul c μ @[simp] lemma restrict_restrict (hs : measurable_set s) : (μ.restrict t).restrict s = μ.restrict (s ∩ t) := ext $ λ u hu, by simp [*, set.inter_assoc] lemma restrict_comm (hs : measurable_set s) (ht : measurable_set t) : (μ.restrict t).restrict s = (μ.restrict s).restrict t := by rw [restrict_restrict hs, restrict_restrict ht, inter_comm] lemma restrict_apply_eq_zero (ht : measurable_set t) : μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 := by rw [restrict_apply ht] lemma measure_inter_eq_zero_of_restrict (h : μ.restrict s t = 0) : μ (t ∩ s) = 0 := nonpos_iff_eq_zero.1 (h ▸ le_restrict_apply _ _) lemma restrict_apply_eq_zero' (hs : measurable_set s) : μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 := begin refine ⟨measure_inter_eq_zero_of_restrict, λ h, _⟩, rcases exists_measurable_superset_of_null h with ⟨t', htt', ht', ht'0⟩, apply measure_mono_null ((inter_subset _ _ _).1 htt'), rw [restrict_apply (hs.compl.union ht'), union_inter_distrib_right, compl_inter_self, set.empty_union], exact measure_mono_null (inter_subset_left _ _) ht'0 end @[simp] lemma restrict_eq_zero : μ.restrict s = 0 ↔ μ s = 0 := by rw [← measure_univ_eq_zero, restrict_apply_univ] lemma restrict_zero_set {s : set α} (h : μ s = 0) : μ.restrict s = 0 := by simp only [measure.restrict_eq_zero, h] @[simp] lemma restrict_empty : μ.restrict ∅ = 0 := ext $ λ s hs, by simp [hs] @[simp] lemma restrict_univ : μ.restrict univ = μ := ext $ λ s hs, by simp [hs] lemma restrict_eq_self_of_measurable_subset (ht : measurable_set t) (t_subset : t ⊆ s) : μ.restrict s t = μ t := by rw [measure.restrict_apply ht, set.inter_eq_self_of_subset_left t_subset] lemma restrict_union_apply (h : disjoint (t ∩ s) (t ∩ s')) (hs : measurable_set s) (hs' : measurable_set s') (ht : measurable_set t) : μ.restrict (s ∪ s') t = μ.restrict s t + μ.restrict s' t := begin simp only [restrict_apply, ht, set.inter_union_distrib_left], exact measure_union h (ht.inter hs) (ht.inter hs'), end lemma restrict_union (h : disjoint s t) (hs : measurable_set s) (ht : measurable_set t) : μ.restrict (s ∪ t) = μ.restrict s + μ.restrict t := ext $ λ t' ht', restrict_union_apply (h.mono inf_le_right inf_le_right) hs ht ht' lemma restrict_union_add_inter (hs : measurable_set s) (ht : measurable_set t) : μ.restrict (s ∪ t) + μ.restrict (s ∩ t) = μ.restrict s + μ.restrict t := begin ext1 u hu, simp only [add_apply, restrict_apply hu, inter_union_distrib_left], convert measure_union_add_inter (hu.inter hs) (hu.inter ht) using 3, rw [set.inter_left_comm (u ∩ s), set.inter_assoc, ← set.inter_assoc u u, set.inter_self] end @[simp] lemma restrict_add_restrict_compl (hs : measurable_set s) : μ.restrict s + μ.restrict sᶜ = μ := by rw [← restrict_union (@disjoint_compl_right (set α) _ _) hs hs.compl, union_compl_self, restrict_univ] @[simp] lemma restrict_compl_add_restrict (hs : measurable_set s) : μ.restrict sᶜ + μ.restrict s = μ := by rw [add_comm, restrict_add_restrict_compl hs] lemma restrict_union_le (s s' : set α) : μ.restrict (s ∪ s') ≤ μ.restrict s + μ.restrict s' := begin intros t ht, suffices : μ (t ∩ s ∪ t ∩ s') ≤ μ (t ∩ s) + μ (t ∩ s'), by simpa [ht, inter_union_distrib_left], apply measure_union_le end lemma restrict_Union_apply [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s)) (hm : ∀ i, measurable_set (s i)) {t : set α} (ht : measurable_set t) : μ.restrict (⋃ i, s i) t = ∑' i, μ.restrict (s i) t := begin simp only [restrict_apply, ht, inter_Union], exact measure_Union (λ i j hij, (hd i j hij).mono inf_le_right inf_le_right) (λ i, ht.inter (hm i)) end lemma restrict_Union_apply_eq_supr [encodable ι] {s : ι → set α} (hm : ∀ i, measurable_set (s i)) (hd : directed (⊆) s) {t : set α} (ht : measurable_set t) : μ.restrict (⋃ i, s i) t = ⨆ i, μ.restrict (s i) t := begin simp only [restrict_apply ht, inter_Union], rw [measure_Union_eq_supr], exacts [λ i, ht.inter (hm i), hd.mono_comp _ (λ s₁ s₂, inter_subset_inter_right _)] end lemma restrict_map {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : (map f μ).restrict s = map f (μ.restrict $ f ⁻¹' s) := ext $ λ t ht, by simp [*, hf ht] lemma map_comap_subtype_coe {m0 : measurable_space α} (hs : measurable_set s) : (map (coe : s → α)).comp (comap coe) = restrictₗ s := linear_map.ext $ λ μ, ext $ λ t ht, by rw [restrictₗ_apply, restrict_apply ht, linear_map.comp_apply, map_apply measurable_subtype_coe ht, comap_apply (coe : s → α) subtype.val_injective (λ _, hs.subtype_image) _ (measurable_subtype_coe ht), subtype.image_preimage_coe] /-- Restriction of a measure to a subset is monotone both in set and in measure. -/ lemma restrict_mono' {m0 : measurable_space α} ⦃s s' : set α⦄ ⦃μ ν : measure α⦄ (hs : s ≤ᵐ[μ] s') (hμν : μ ≤ ν) : μ.restrict s ≤ ν.restrict s' := assume t ht, calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht ... ≤ μ (t ∩ s') : measure_mono_ae $ hs.mono $ λ x hx ⟨hxt, hxs⟩, ⟨hxt, hx hxs⟩ ... ≤ ν (t ∩ s') : le_iff'.1 hμν (t ∩ s') ... = ν.restrict s' t : (restrict_apply ht).symm /-- Restriction of a measure to a subset is monotone both in set and in measure. -/ @[mono] lemma restrict_mono {m0 : measurable_space α} ⦃s s' : set α⦄ (hs : s ⊆ s') ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) : μ.restrict s ≤ ν.restrict s' := restrict_mono' (ae_of_all _ hs) hμν lemma restrict_le_self : μ.restrict s ≤ μ := assume t ht, calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht ... ≤ μ t : measure_mono $ inter_subset_left t s lemma restrict_congr_meas (hs : measurable_set s) : μ.restrict s = ν.restrict s ↔ ∀ t ⊆ s, measurable_set t → μ t = ν t := ⟨λ H t hts ht, by rw [← inter_eq_self_of_subset_left hts, ← restrict_apply ht, H, restrict_apply ht], λ H, ext $ λ t ht, by rw [restrict_apply ht, restrict_apply ht, H _ (inter_subset_right _ _) (ht.inter hs)]⟩ lemma restrict_congr_mono (hs : s ⊆ t) (hm : measurable_set s) (h : μ.restrict t = ν.restrict t) : μ.restrict s = ν.restrict s := by rw [← inter_eq_self_of_subset_left hs, ← restrict_restrict hm, h, restrict_restrict hm] /-- If two measures agree on all measurable subsets of `s` and `t`, then they agree on all measurable subsets of `s ∪ t`. -/ lemma restrict_union_congr (hsm : measurable_set s) (htm : measurable_set t) : μ.restrict (s ∪ t) = ν.restrict (s ∪ t) ↔ μ.restrict s = ν.restrict s ∧ μ.restrict t = ν.restrict t := begin refine ⟨λ h, ⟨restrict_congr_mono (subset_union_left _ _) hsm h, restrict_congr_mono (subset_union_right _ _) htm h⟩, _⟩, simp only [restrict_congr_meas, hsm, htm, hsm.union htm], rintros ⟨hs, ht⟩ u hu hum, rw [measure_eq_inter_diff hum hsm, measure_eq_inter_diff hum hsm, hs _ (inter_subset_right _ _) (hum.inter hsm), ht _ (diff_subset_iff.2 hu) (hum.diff hsm)] end lemma restrict_finset_bUnion_congr {s : finset ι} {t : ι → set α} (htm : ∀ i ∈ s, measurable_set (t i)) : μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔ ∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) := begin induction s using finset.induction_on with i s hi hs, { simp }, simp only [finset.mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at htm ⊢, simp only [finset.set_bUnion_insert, ← hs htm.2], exact restrict_union_congr htm.1 (s.measurable_set_bUnion htm.2) end lemma restrict_Union_congr [encodable ι] {s : ι → set α} (hm : ∀ i, measurable_set (s i)) : μ.restrict (⋃ i, s i) = ν.restrict (⋃ i, s i) ↔ ∀ i, μ.restrict (s i) = ν.restrict (s i) := begin refine ⟨λ h i, restrict_congr_mono (subset_Union _ _) (hm i) h, λ h, _⟩, ext1 t ht, have M : ∀ t : finset ι, measurable_set (⋃ i ∈ t, s i) := λ t, t.measurable_set_bUnion (λ i _, hm i), have D : directed (⊆) (λ t : finset ι, ⋃ i ∈ t, s i) := directed_of_sup (λ t₁ t₂ ht, bUnion_subset_bUnion_left ht), rw [Union_eq_Union_finset], simp only [restrict_Union_apply_eq_supr M D ht, (restrict_finset_bUnion_congr (λ i hi, hm i)).2 (λ i hi, h i)], end lemma restrict_bUnion_congr {s : set ι} {t : ι → set α} (hc : countable s) (htm : ∀ i ∈ s, measurable_set (t i)) : μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔ ∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) := begin simp only [bUnion_eq_Union, set_coe.forall'] at htm ⊢, haveI := hc.to_encodable, exact restrict_Union_congr htm end lemma restrict_sUnion_congr {S : set (set α)} (hc : countable S) (hm : ∀ s ∈ S, measurable_set s) : μ.restrict (⋃₀ S) = ν.restrict (⋃₀ S) ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s := by rw [sUnion_eq_bUnion, restrict_bUnion_congr hc hm] /-- This lemma shows that `restrict` and `to_outer_measure` commute. Note that the LHS has a restrict on measures and the RHS has a restrict on outer measures. -/ lemma restrict_to_outer_measure_eq_to_outer_measure_restrict (h : measurable_set s) : (μ.restrict s).to_outer_measure = outer_measure.restrict s μ.to_outer_measure := by simp_rw [restrict, restrictₗ, lift_linear, linear_map.coe_mk, to_measure_to_outer_measure, outer_measure.restrict_trim h, μ.trimmed] /-- This lemma shows that `Inf` and `restrict` commute for measures. -/ lemma restrict_Inf_eq_Inf_restrict {m0 : measurable_space α} {m : set (measure α)} (hm : m.nonempty) (ht : measurable_set t) : (Inf m).restrict t = Inf ((λ μ : measure α, μ.restrict t) '' m) := begin ext1 s hs, simp_rw [Inf_apply hs, restrict_apply hs, Inf_apply (measurable_set.inter hs ht), set.image_image, restrict_to_outer_measure_eq_to_outer_measure_restrict ht, ← set.image_image _ to_outer_measure, ← outer_measure.restrict_Inf_eq_Inf_restrict _ (hm.image _), outer_measure.restrict_apply] end /-- If `s` is a measurable set, then the outer measure of `t` with respect to the restriction of the measure to `s` equals the outer measure of `t ∩ s`. This is an alternate version of `measure.restrict_apply`, requiring that `s` is measurable instead of `t`. -/ lemma restrict_apply' (hs : measurable_set s) : μ.restrict s t = μ (t ∩ s) := by rw [← coe_to_outer_measure, measure.restrict_to_outer_measure_eq_to_outer_measure_restrict hs, outer_measure.restrict_apply s t _, coe_to_outer_measure] lemma restrict_eq_self_of_subset_of_measurable (hs : measurable_set s) (t_subset : t ⊆ s) : μ.restrict s t = μ t := by rw [restrict_apply' hs, set.inter_eq_self_of_subset_left t_subset] /-! ### Extensionality results -/ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `Union`). -/ lemma ext_iff_of_Union_eq_univ [encodable ι] {s : ι → set α} (hm : ∀ i, measurable_set (s i)) (hs : (⋃ i, s i) = univ) : μ = ν ↔ ∀ i, μ.restrict (s i) = ν.restrict (s i) := by rw [← restrict_Union_congr hm, hs, restrict_univ, restrict_univ] alias ext_iff_of_Union_eq_univ ↔ _ measure_theory.measure.ext_of_Union_eq_univ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `bUnion`). -/ lemma ext_iff_of_bUnion_eq_univ {S : set ι} {s : ι → set α} (hc : countable S) (hm : ∀ i ∈ S, measurable_set (s i)) (hs : (⋃ i ∈ S, s i) = univ) : μ = ν ↔ ∀ i ∈ S, μ.restrict (s i) = ν.restrict (s i) := by rw [← restrict_bUnion_congr hc hm, hs, restrict_univ, restrict_univ] alias ext_iff_of_bUnion_eq_univ ↔ _ measure_theory.measure.ext_of_bUnion_eq_univ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `sUnion`). -/ lemma ext_iff_of_sUnion_eq_univ {S : set (set α)} (hc : countable S) (hm : ∀ s ∈ S, measurable_set s) (hs : (⋃₀ S) = univ) : μ = ν ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s := ext_iff_of_bUnion_eq_univ hc hm $ by rwa ← sUnion_eq_bUnion alias ext_iff_of_sUnion_eq_univ ↔ _ measure_theory.measure.ext_of_sUnion_eq_univ lemma ext_of_generate_from_of_cover {S T : set (set α)} (h_gen : ‹_› = generate_from S) (hc : countable T) (h_inter : is_pi_system S) (hm : ∀ t ∈ T, measurable_set t) (hU : ⋃₀ T = univ) (htop : ∀ t ∈ T, μ t < ∞) (ST_eq : ∀ (t ∈ T) (s ∈ S), μ (s ∩ t) = ν (s ∩ t)) (T_eq : ∀ t ∈ T, μ t = ν t) : μ = ν := begin refine ext_of_sUnion_eq_univ hc hm hU (λ t ht, _), ext1 u hu, simp only [restrict_apply hu], refine induction_on_inter h_gen h_inter _ (ST_eq t ht) _ _ hu, { simp only [set.empty_inter, measure_empty] }, { intros v hv hvt, have := T_eq t ht, rw [set.inter_comm] at hvt ⊢, rwa [measure_eq_inter_diff (hm _ ht) hv, measure_eq_inter_diff (hm _ ht) hv, ← hvt, ennreal.add_right_inj] at this, exact (measure_mono $ set.inter_subset_left _ _).trans_lt (htop t ht) }, { intros f hfd hfm h_eq, have : pairwise (disjoint on λ n, f n ∩ t) := λ m n hmn, (hfd m n hmn).mono (inter_subset_left _ _) (inter_subset_left _ _), simp only [Union_inter, measure_Union this (λ n, (hfm n).inter (hm t ht)), h_eq] } end /-- Two measures are equal if they are equal on the π-system generating the σ-algebra, and they are both finite on a increasing spanning sequence of sets in the π-system. This lemma is formulated using `sUnion`. -/ lemma ext_of_generate_from_of_cover_subset {S T : set (set α)} (h_gen : ‹_› = generate_from S) (h_inter : is_pi_system S) (h_sub : T ⊆ S) (hc : countable T) (hU : ⋃₀ T = univ) (htop : ∀ s ∈ T, μ s < ∞) (h_eq : ∀ s ∈ S, μ s = ν s) : μ = ν := begin refine ext_of_generate_from_of_cover h_gen hc h_inter _ hU htop _ (λ t ht, h_eq t (h_sub ht)), { intros t ht, rw [h_gen], exact generate_measurable.basic _ (h_sub ht) }, { intros t ht s hs, cases (s ∩ t).eq_empty_or_nonempty with H H, { simp only [H, measure_empty] }, { exact h_eq _ (h_inter _ _ hs (h_sub ht) H) } } end /-- Two measures are equal if they are equal on the π-system generating the σ-algebra, and they are both finite on a increasing spanning sequence of sets in the π-system. This lemma is formulated using `Union`. `finite_spanning_sets_in.ext` is a reformulation of this lemma. -/ lemma ext_of_generate_from_of_Union (C : set (set α)) (B : ℕ → set α) (hA : ‹_› = generate_from C) (hC : is_pi_system C) (h1B : (⋃ i, B i) = univ) (h2B : ∀ i, B i ∈ C) (hμB : ∀ i, μ (B i) < ∞) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν := begin refine ext_of_generate_from_of_cover_subset hA hC _ (countable_range B) h1B _ h_eq, { rintro _ ⟨i, rfl⟩, apply h2B }, { rintro _ ⟨i, rfl⟩, apply hμB } end section dirac variable [measurable_space α] /-- The dirac measure. -/ def dirac (a : α) : measure α := (outer_measure.dirac a).to_measure (by simp) lemma le_dirac_apply {a} : s.indicator 1 a ≤ dirac a s := outer_measure.dirac_apply a s ▸ le_to_measure_apply _ _ _ @[simp] lemma dirac_apply' (a : α) (hs : measurable_set s) : dirac a s = s.indicator 1 a := to_measure_apply _ _ hs @[simp] lemma dirac_apply_of_mem {a : α} (h : a ∈ s) : dirac a s = 1 := begin have : ∀ t : set α, a ∈ t → t.indicator (1 : α → ℝ≥0∞) a = 1, from λ t ht, indicator_of_mem ht 1, refine le_antisymm (this univ trivial ▸ _) (this s h ▸ le_dirac_apply), rw [← dirac_apply' a measurable_set.univ], exact measure_mono (subset_univ s) end @[simp] lemma dirac_apply [measurable_singleton_class α] (a : α) (s : set α) : dirac a s = s.indicator 1 a := begin by_cases h : a ∈ s, by rw [dirac_apply_of_mem h, indicator_of_mem h, pi.one_apply], rw [indicator_of_not_mem h, ← nonpos_iff_eq_zero], calc dirac a s ≤ dirac a {a}ᶜ : measure_mono (subset_compl_comm.1 $ singleton_subset_iff.2 h) ... = 0 : by simp [dirac_apply' _ (measurable_set_singleton _).compl] end lemma map_dirac {f : α → β} (hf : measurable f) (a : α) : map f (dirac a) = dirac (f a) := ext $ assume s hs, by simp [hs, map_apply hf hs, hf hs, indicator_apply] end dirac section sum include m0 /-- Sum of an indexed family of measures. -/ def sum (f : ι → measure α) : measure α := (outer_measure.sum (λ i, (f i).to_outer_measure)).to_measure $ le_trans (by exact le_infi (λ i, le_to_outer_measure_caratheodory _)) (outer_measure.le_sum_caratheodory _) lemma le_sum_apply (f : ι → measure α) (s : set α) : (∑' i, f i s) ≤ sum f s := le_to_measure_apply _ _ _ @[simp] lemma sum_apply (f : ι → measure α) {s : set α} (hs : measurable_set s) : sum f s = ∑' i, f i s := to_measure_apply _ _ hs lemma le_sum (μ : ι → measure α) (i : ι) : μ i ≤ sum μ := λ s hs, by simp only [sum_apply μ hs, ennreal.le_tsum i] @[simp] lemma sum_bool (f : bool → measure α) : sum f = f tt + f ff := ext $ λ s hs, by simp [hs, tsum_fintype] @[simp] lemma sum_cond (μ ν : measure α) : sum (λ b, cond b μ ν) = μ + ν := sum_bool _ @[simp] lemma restrict_sum (μ : ι → measure α) {s : set α} (hs : measurable_set s) : (sum μ).restrict s = sum (λ i, (μ i).restrict s) := ext $ λ t ht, by simp only [sum_apply, restrict_apply, ht, ht.inter hs] omit m0 end sum lemma restrict_Union [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s)) (hm : ∀ i, measurable_set (s i)) : μ.restrict (⋃ i, s i) = sum (λ i, μ.restrict (s i)) := ext $ λ t ht, by simp only [sum_apply _ ht, restrict_Union_apply hd hm ht] lemma restrict_Union_le [encodable ι] {s : ι → set α} : μ.restrict (⋃ i, s i) ≤ sum (λ i, μ.restrict (s i)) := begin intros t ht, suffices : μ (⋃ i, t ∩ s i) ≤ ∑' i, μ (t ∩ s i), by simpa [ht, inter_Union], apply measure_Union_le end section count variable [measurable_space α] /-- Counting measure on any measurable space. -/ def count : measure α := sum dirac lemma le_count_apply : (∑' i : s, 1 : ℝ≥0∞) ≤ count s := calc (∑' i : s, 1 : ℝ≥0∞) = ∑' i, indicator s 1 i : tsum_subtype s 1 ... ≤ ∑' i, dirac i s : ennreal.tsum_le_tsum $ λ x, le_dirac_apply ... ≤ count s : le_sum_apply _ _ lemma count_apply (hs : measurable_set s) : count s = ∑' i : s, 1 := by simp only [count, sum_apply, hs, dirac_apply', ← tsum_subtype s 1, pi.one_apply] @[simp] lemma count_apply_finset [measurable_singleton_class α] (s : finset α) : count (↑s : set α) = s.card := calc count (↑s : set α) = ∑' i : (↑s : set α), 1 : count_apply s.measurable_set ... = ∑ i in s, 1 : s.tsum_subtype 1 ... = s.card : by simp lemma count_apply_finite [measurable_singleton_class α] (s : set α) (hs : finite s) : count s = hs.to_finset.card := by rw [← count_apply_finset, finite.coe_to_finset] /-- `count` measure evaluates to infinity at infinite sets. -/ lemma count_apply_infinite (hs : s.infinite) : count s = ∞ := begin refine top_unique (le_of_tendsto' ennreal.tendsto_nat_nhds_top $ λ n, _), rcases hs.exists_subset_card_eq n with ⟨t, ht, rfl⟩, calc (t.card : ℝ≥0∞) = ∑ i in t, 1 : by simp ... = ∑' i : (t : set α), 1 : (t.tsum_subtype 1).symm ... ≤ count (t : set α) : le_count_apply ... ≤ count s : measure_mono ht end @[simp] lemma count_apply_eq_top [measurable_singleton_class α] : count s = ∞ ↔ s.infinite := begin by_cases hs : s.finite, { simp [set.infinite, hs, count_apply_finite] }, { change s.infinite at hs, simp [hs, count_apply_infinite] } end @[simp] lemma count_apply_lt_top [measurable_singleton_class α] : count s < ∞ ↔ s.finite := calc count s < ∞ ↔ count s ≠ ∞ : lt_top_iff_ne_top ... ↔ ¬s.infinite : not_congr count_apply_eq_top ... ↔ s.finite : not_not end count /-! ### Absolute continuity -/ /-- We say that `μ` is absolutely continuous with respect to `ν`, or that `μ` is dominated by `ν`, if `ν(A) = 0` implies that `μ(A) = 0`. -/ def absolutely_continuous {m0 : measurable_space α} (μ ν : measure α) : Prop := ∀ ⦃s : set α⦄, ν s = 0 → μ s = 0 infix ` ≪ `:50 := absolutely_continuous lemma absolutely_continuous_of_le (h : μ ≤ ν) : μ ≪ ν := λ s hs, nonpos_iff_eq_zero.1 $ hs ▸ le_iff'.1 h s alias absolutely_continuous_of_le ← has_le.le.absolutely_continuous lemma absolutely_continuous_of_eq (h : μ = ν) : μ ≪ ν := h.le.absolutely_continuous alias absolutely_continuous_of_eq ← eq.absolutely_continuous namespace absolutely_continuous lemma mk (h : ∀ ⦃s : set α⦄, measurable_set s → ν s = 0 → μ s = 0) : μ ≪ ν := begin intros s hs, rcases exists_measurable_superset_of_null hs with ⟨t, h1t, h2t, h3t⟩, exact measure_mono_null h1t (h h2t h3t), end @[refl] protected lemma refl {m0 : measurable_space α} (μ : measure α) : μ ≪ μ := rfl.absolutely_continuous protected lemma rfl : μ ≪ μ := λ s hs, hs @[trans] protected lemma trans (h1 : μ₁ ≪ μ₂) (h2 : μ₂ ≪ μ₃) : μ₁ ≪ μ₃ := λ s hs, h1 $ h2 hs @[mono] protected lemma map (h : μ ≪ ν) (f : α → β) : map f μ ≪ map f ν := if hf : measurable f then absolutely_continuous.mk $ λ s hs, by simpa [hf, hs] using @h _ else by simp only [map_of_not_measurable hf] end absolutely_continuous lemma ae_le_iff_absolutely_continuous : μ.ae ≤ ν.ae ↔ μ ≪ ν := ⟨λ h s, by { rw [measure_zero_iff_ae_nmem, measure_zero_iff_ae_nmem], exact λ hs, h hs }, λ h s hs, h hs⟩ alias ae_le_iff_absolutely_continuous ↔ has_le.le.absolutely_continuous_of_ae measure_theory.measure.absolutely_continuous.ae_le alias absolutely_continuous.ae_le ← ae_mono' lemma absolutely_continuous.ae_eq (h : μ ≪ ν) {f g : α → δ} (h' : f =ᵐ[ν] g) : f =ᵐ[μ] g := h.ae_le h' /-! ### Quasi measure preserving maps (a.k.a. non-singular maps) -/ /-- A map `f : α → β` is said to be *quasi measure preserving* (a.k.a. non-singular) w.r.t. measures `μa` and `μb` if it is measurable and `μb s = 0` implies `μa (f ⁻¹' s) = 0`. -/ @[protect_proj] structure quasi_measure_preserving {m0 : measurable_space α} (f : α → β) (μa : measure α . volume_tac) (μb : measure β . volume_tac) : Prop := (measurable : measurable f) (absolutely_continuous : map f μa ≪ μb) namespace quasi_measure_preserving protected lemma id {m0 : measurable_space α} (μ : measure α) : quasi_measure_preserving id μ μ := ⟨measurable_id, map_id.absolutely_continuous⟩ variables {μa μa' : measure α} {μb μb' : measure β} {μc : measure γ} {f : α → β} lemma mono_left (h : quasi_measure_preserving f μa μb) (ha : μa' ≪ μa) : quasi_measure_preserving f μa' μb := ⟨h.1, (ha.map f).trans h.2⟩ lemma mono_right (h : quasi_measure_preserving f μa μb) (ha : μb ≪ μb') : quasi_measure_preserving f μa μb' := ⟨h.1, h.2.trans ha⟩ @[mono] lemma mono (ha : μa' ≪ μa) (hb : μb ≪ μb') (h : quasi_measure_preserving f μa μb) : quasi_measure_preserving f μa' μb' := (h.mono_left ha).mono_right hb protected lemma comp {g : β → γ} {f : α → β} (hg : quasi_measure_preserving g μb μc) (hf : quasi_measure_preserving f μa μb) : quasi_measure_preserving (g ∘ f) μa μc := ⟨hg.measurable.comp hf.measurable, by { rw ← map_map hg.1 hf.1, exact (hf.2.map g).trans hg.2 }⟩ protected lemma iterate {f : α → α} (hf : quasi_measure_preserving f μa μa) : ∀ n, quasi_measure_preserving (f^[n]) μa μa | 0 := quasi_measure_preserving.id μa | (n + 1) := (iterate n).comp hf lemma ae_map_le (h : quasi_measure_preserving f μa μb) : (map f μa).ae ≤ μb.ae := h.2.ae_le lemma tendsto_ae (h : quasi_measure_preserving f μa μb) : tendsto f μa.ae μb.ae := (tendsto_ae_map h.1).mono_right h.ae_map_le lemma ae (h : quasi_measure_preserving f μa μb) {p : β → Prop} (hg : ∀ᵐ x ∂μb, p x) : ∀ᵐ x ∂μa, p (f x) := h.tendsto_ae hg lemma ae_eq (h : quasi_measure_preserving f μa μb) {g₁ g₂ : β → δ} (hg : g₁ =ᵐ[μb] g₂) : g₁ ∘ f =ᵐ[μa] g₂ ∘ f := h.ae hg end quasi_measure_preserving /-! ### The `cofinite` filter -/ /-- The filter of sets `s` such that `sᶜ` has finite measure. -/ def cofinite {m0 : measurable_space α} (μ : measure α) : filter α := { sets := {s | μ sᶜ < ∞}, univ_sets := by simp, inter_sets := λ s t hs ht, by { simp only [compl_inter, mem_set_of_eq], calc μ (sᶜ ∪ tᶜ) ≤ μ sᶜ + μ tᶜ : measure_union_le _ _ ... < ∞ : ennreal.add_lt_top.2 ⟨hs, ht⟩ }, sets_of_superset := λ s t hs hst, lt_of_le_of_lt (measure_mono $ compl_subset_compl.2 hst) hs } lemma mem_cofinite : s ∈ μ.cofinite ↔ μ sᶜ < ∞ := iff.rfl lemma compl_mem_cofinite : sᶜ ∈ μ.cofinite ↔ μ s < ∞ := by rw [mem_cofinite, compl_compl] lemma eventually_cofinite {p : α → Prop} : (∀ᶠ x in μ.cofinite, p x) ↔ μ {x | ¬p x} < ∞ := iff.rfl /-! ### Mutually singular measures -/ /-- Two measures `μ`, `ν` are said to be mutually singular if there exists a measurable set `s` such that `μ s = 0` and `ν sᶜ = 0`. -/ def mutually_singular {m0 : measurable_space α} (μ ν : measure α) : Prop := ∃ (s : set α), measurable_set s ∧ μ s = 0 ∧ ν sᶜ = 0 localized "infix ` ⊥ₘ `:60 := measure_theory.measure.mutually_singular" in measure_theory namespace mutually_singular lemma zero : μ ⊥ₘ 0 := ⟨∅, measurable_set.empty, measure_empty, rfl⟩ lemma symm (h : ν ⊥ₘ μ) : μ ⊥ₘ ν := let ⟨i, hi, his, hit⟩ := h in ⟨iᶜ, measurable_set.compl hi, hit, (compl_compl i).symm ▸ his⟩ lemma add (h₁ : ν₁ ⊥ₘ μ) (h₂ : ν₂ ⊥ₘ μ) : ν₁ + ν₂ ⊥ₘ μ := begin obtain ⟨s, hs, hs0, hs0'⟩ := h₁, obtain ⟨t, ht, ht0, ht0'⟩ := h₂, refine ⟨s ∩ t, hs.inter ht, _, _⟩, { simp only [pi.add_apply, add_eq_zero_iff, coe_add], exact ⟨measure_mono_null (inter_subset_left s t) hs0, measure_mono_null (inter_subset_right s t) ht0⟩ }, { rw [compl_inter, ← nonpos_iff_eq_zero], refine le_trans (measure_union_le _ _) _, rw [hs0', ht0', zero_add], exact le_refl _ } end lemma smul (r : ℝ≥0) (h : ν ⊥ₘ μ) : r • ν ⊥ₘ μ := let ⟨s, hs, hs0, hs0'⟩ := h in ⟨s, hs, by simp only [coe_nnreal_smul, pi.smul_apply, hs0, smul_zero], hs0'⟩ end mutually_singular end measure open measure @[simp] lemma ae_eq_bot : μ.ae = ⊥ ↔ μ = 0 := by rw [← empty_mem_iff_bot, mem_ae_iff, compl_empty, measure_univ_eq_zero] @[simp] lemma ae_ne_bot : μ.ae.ne_bot ↔ μ ≠ 0 := ne_bot_iff.trans (not_congr ae_eq_bot) @[simp] lemma ae_zero {m0 : measurable_space α} : (0 : measure α).ae = ⊥ := ae_eq_bot.2 rfl @[mono] lemma ae_mono (h : μ ≤ ν) : μ.ae ≤ ν.ae := h.absolutely_continuous.ae_le lemma mem_ae_map_iff {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : s ∈ (map f μ).ae ↔ (f ⁻¹' s) ∈ μ.ae := by simp only [mem_ae_iff, map_apply hf hs.compl, preimage_compl] lemma mem_ae_of_mem_ae_map {f : α → β} (hf : measurable f) {s : set β} (hs : s ∈ (map f μ).ae) : f ⁻¹' s ∈ μ.ae := begin apply le_antisymm _ bot_le, calc μ (f ⁻¹' sᶜ) ≤ (map f μ) sᶜ : le_map_apply hf sᶜ ... = 0 : hs end lemma ae_map_iff {f : α → β} (hf : measurable f) {p : β → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ y ∂ (map f μ), p y) ↔ ∀ᵐ x ∂ μ, p (f x) := mem_ae_map_iff hf hp lemma ae_of_ae_map {f : α → β} (hf : measurable f) {p : β → Prop} (h : ∀ᵐ y ∂ (map f μ), p y) : ∀ᵐ x ∂ μ, p (f x) := mem_ae_of_mem_ae_map hf h lemma ae_map_mem_range {m0 : measurable_space α} (f : α → β) (hf : measurable_set (range f)) (μ : measure α) : ∀ᵐ x ∂(map f μ), x ∈ range f := begin by_cases h : measurable f, { change range f ∈ (map f μ).ae, rw mem_ae_map_iff h hf, apply eventually_of_forall, exact mem_range_self }, { simp [map_of_not_measurable h] } end lemma ae_restrict_iff {p : α → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff, ← compl_set_of, restrict_apply hp.compl], congr' with x, simp [and_comm] end lemma ae_imp_of_ae_restrict {s : set α} {p : α → Prop} (h : ∀ᵐ x ∂(μ.restrict s), p x) : ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff] at h ⊢, simpa [set_of_and, inter_comm] using measure_inter_eq_zero_of_restrict h end lemma ae_restrict_iff' {s : set α} {p : α → Prop} (hs : measurable_set s) : (∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff, ← compl_set_of, restrict_apply_eq_zero' hs], congr' with x, simp [and_comm] end lemma ae_restrict_mem {s : set α} (hs : measurable_set s) : ∀ᵐ x ∂(μ.restrict s), x ∈ s := (ae_restrict_iff' hs).2 (filter.eventually_of_forall (λ x, id)) lemma ae_restrict_of_ae {s : set α} {p : α → Prop} (h : ∀ᵐ x ∂μ, p x) : (∀ᵐ x ∂(μ.restrict s), p x) := eventually.filter_mono (ae_mono measure.restrict_le_self) h lemma ae_restrict_of_ae_restrict_of_subset {s t : set α} {p : α → Prop} (hst : s ⊆ t) (h : ∀ᵐ x ∂(μ.restrict t), p x) : (∀ᵐ x ∂(μ.restrict s), p x) := h.filter_mono (ae_mono $ measure.restrict_mono hst (le_refl μ)) lemma ae_smul_measure {p : α → Prop} (h : ∀ᵐ x ∂μ, p x) (c : ℝ≥0∞) : ∀ᵐ x ∂(c • μ), p x := ae_iff.2 $ by rw [smul_apply, ae_iff.1 h, mul_zero] lemma ae_smul_measure_iff {p : α → Prop} {c : ℝ≥0∞} (hc : c ≠ 0) : (∀ᵐ x ∂(c • μ), p x) ↔ ∀ᵐ x ∂μ, p x := by simp [ae_iff, hc] lemma ae_add_measure_iff {p : α → Prop} {ν} : (∀ᵐ x ∂μ + ν, p x) ↔ (∀ᵐ x ∂μ, p x) ∧ ∀ᵐ x ∂ν, p x := add_eq_zero_iff lemma ae_eq_comp' {ν : measure β} {f : α → β} {g g' : β → δ} (hf : measurable f) (h : g =ᵐ[ν] g') (h2 : map f μ ≪ ν) : g ∘ f =ᵐ[μ] g' ∘ f := (quasi_measure_preserving.mk hf h2).ae_eq h lemma ae_eq_comp {f : α → β} {g g' : β → δ} (hf : measurable f) (h : g =ᵐ[measure.map f μ] g') : g ∘ f =ᵐ[μ] g' ∘ f := ae_eq_comp' hf h absolutely_continuous.rfl lemma le_ae_restrict : μ.ae ⊓ 𝓟 s ≤ (μ.restrict s).ae := λ s hs, eventually_inf_principal.2 (ae_imp_of_ae_restrict hs) @[simp] lemma ae_restrict_eq (hs : measurable_set s) : (μ.restrict s).ae = μ.ae ⊓ 𝓟 s := begin ext t, simp only [mem_inf_principal, mem_ae_iff, restrict_apply_eq_zero' hs, compl_set_of, not_imp, and_comm (_ ∈ s)], refl end @[simp] lemma ae_restrict_eq_bot {s} : (μ.restrict s).ae = ⊥ ↔ μ s = 0 := ae_eq_bot.trans restrict_eq_zero @[simp] lemma ae_restrict_ne_bot {s} : (μ.restrict s).ae.ne_bot ↔ 0 < μ s := ne_bot_iff.trans $ (not_congr ae_restrict_eq_bot).trans pos_iff_ne_zero.symm lemma self_mem_ae_restrict {s} (hs : measurable_set s) : s ∈ (μ.restrict s).ae := by simp only [ae_restrict_eq hs, exists_prop, mem_principal, mem_inf_iff]; exact ⟨_, univ_mem, s, subset.rfl, (univ_inter s).symm⟩ /-- A version of the Borel-Cantelli lemma: if `sᵢ` is a sequence of measurable sets such that `∑ μ sᵢ` exists, then for almost all `x`, `x` does not belong to almost all `sᵢ`. -/ lemma ae_eventually_not_mem {s : ℕ → set α} (hs : ∀ i, measurable_set (s i)) (hs' : ∑' i, μ (s i) ≠ ∞) : ∀ᵐ x ∂ μ, ∀ᶠ n in at_top, x ∉ s n := begin refine measure_mono_null _ (measure_limsup_eq_zero hs hs'), rw ←set.le_eq_subset, refine le_Inf (λ t ht x hx, _), simp only [le_eq_subset, not_exists, eventually_map, exists_prop, ge_iff_le, mem_set_of_eq, eventually_at_top, mem_compl_eq, not_forall, not_not_mem] at hx ht, rcases ht with ⟨i, hi⟩, rcases hx i with ⟨j, ⟨hj, hj'⟩⟩, exact hi j hj hj' end section dirac variable [measurable_space α] lemma mem_ae_dirac_iff {a : α} (hs : measurable_set s) : s ∈ (dirac a).ae ↔ a ∈ s := by by_cases a ∈ s; simp [mem_ae_iff, dirac_apply', hs.compl, indicator_apply, *] lemma ae_dirac_iff {a : α} {p : α → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ x ∂(dirac a), p x) ↔ p a := mem_ae_dirac_iff hp @[simp] lemma ae_dirac_eq [measurable_singleton_class α] (a : α) : (dirac a).ae = pure a := by { ext s, simp [mem_ae_iff, imp_false] } lemma ae_eq_dirac' [measurable_singleton_class β] {a : α} {f : α → β} (hf : measurable f) : f =ᵐ[dirac a] const α (f a) := (ae_dirac_iff $ show measurable_set (f ⁻¹' {f a}), from hf $ measurable_set_singleton _).2 rfl lemma ae_eq_dirac [measurable_singleton_class α] {a : α} (f : α → δ) : f =ᵐ[dirac a] const α (f a) := by simp [filter.eventually_eq] end dirac lemma restrict_mono_ae (h : s ≤ᵐ[μ] t) : μ.restrict s ≤ μ.restrict t := begin intros u hu, simp only [restrict_apply hu], exact measure_mono_ae (h.mono $ λ x hx, and.imp id hx) end lemma restrict_congr_set (H : s =ᵐ[μ] t) : μ.restrict s = μ.restrict t := le_antisymm (restrict_mono_ae H.le) (restrict_mono_ae H.symm.le) section finite_measure include m0 /-- A measure `μ` is called finite if `μ univ < ∞`. -/ class finite_measure (μ : measure α) : Prop := (measure_univ_lt_top : μ univ < ∞) instance restrict.finite_measure (μ : measure α) [hs : fact (μ s < ∞)] : finite_measure (μ.restrict s) := ⟨by simp [hs.elim]⟩ lemma measure_lt_top (μ : measure α) [finite_measure μ] (s : set α) : μ s < ∞ := (measure_mono (subset_univ s)).trans_lt finite_measure.measure_univ_lt_top lemma measure_ne_top (μ : measure α) [finite_measure μ] (s : set α) : μ s ≠ ∞ := ne_of_lt (measure_lt_top μ s) /-- The measure of the whole space with respect to a finite measure, considered as `ℝ≥0`. -/ def measure_univ_nnreal (μ : measure α) : ℝ≥0 := (μ univ).to_nnreal @[simp] lemma coe_measure_univ_nnreal (μ : measure α) [finite_measure μ] : ↑(measure_univ_nnreal μ) = μ univ := ennreal.coe_to_nnreal (measure_ne_top μ univ) instance finite_measure_zero : finite_measure (0 : measure α) := ⟨by simp⟩ @[simp] lemma measure_univ_nnreal_zero : measure_univ_nnreal (0 : measure α) = 0 := rfl omit m0 instance finite_measure_add [finite_measure μ] [finite_measure ν] : finite_measure (μ + ν) := { measure_univ_lt_top := begin rw [measure.coe_add, pi.add_apply, ennreal.add_lt_top], exact ⟨measure_lt_top _ _, measure_lt_top _ _⟩, end } instance finite_measure_smul_nnreal [finite_measure μ] {r : ℝ≥0} : finite_measure (r • μ) := { measure_univ_lt_top := ennreal.mul_lt_top ennreal.coe_lt_top (measure_lt_top _ _) } lemma finite_measure_of_le (μ : measure α) [finite_measure μ] (h : ν ≤ μ) : finite_measure ν := { measure_univ_lt_top := lt_of_le_of_lt (h set.univ measurable_set.univ) (measure_lt_top _ _) } @[simp] lemma measure_univ_nnreal_eq_zero [finite_measure μ] : measure_univ_nnreal μ = 0 ↔ μ = 0 := begin rw [← measure_theory.measure.measure_univ_eq_zero, ← coe_measure_univ_nnreal], norm_cast end lemma measure_univ_nnreal_pos [finite_measure μ] (hμ : μ ≠ 0) : 0 < measure_univ_nnreal μ := begin contrapose! hμ, simpa [measure_univ_nnreal_eq_zero, le_zero_iff] using hμ end /-- `le_of_add_le_add_left` is normally applicable to `ordered_cancel_add_comm_monoid`, but it holds for measures with the additional assumption that μ is finite. -/ lemma measure.le_of_add_le_add_left [finite_measure μ] (A2 : μ + ν₁ ≤ μ + ν₂) : ν₁ ≤ ν₂ := λ S B1, ennreal.le_of_add_le_add_left (measure_theory.measure_lt_top μ S) (A2 S B1) lemma summable_measure_to_real [hμ : finite_measure μ] {f : ℕ → set α} (hf₁ : ∀ (i : ℕ), measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) : summable (λ x, (μ (f x)).to_real) := begin apply ennreal.summable_to_real, rw ← measure_theory.measure_Union hf₂ hf₁, exact ne_of_lt (measure_lt_top _ _) end end finite_measure section probability_measure include m0 /-- A measure `μ` is called a probability measure if `μ univ = 1`. -/ class probability_measure (μ : measure α) : Prop := (measure_univ : μ univ = 1) export probability_measure (measure_univ) @[priority 100] instance probability_measure.to_finite_measure (μ : measure α) [probability_measure μ] : finite_measure μ := ⟨by simp only [measure_univ, ennreal.one_lt_top]⟩ lemma probability_measure.ne_zero (μ : measure α) [probability_measure μ] : μ ≠ 0 := mt measure_univ_eq_zero.2 $ by simp [measure_univ] omit m0 instance measure.dirac.probability_measure [measurable_space α] {x : α} : probability_measure (dirac x) := ⟨dirac_apply_of_mem $ mem_univ x⟩ lemma prob_add_prob_compl [probability_measure μ] (h : measurable_set s) : μ s + μ sᶜ = 1 := (measure_add_measure_compl h).trans measure_univ lemma prob_le_one [probability_measure μ] : μ s ≤ 1 := (measure_mono $ set.subset_univ _).trans_eq measure_univ end probability_measure section no_atoms /-- Measure `μ` *has no atoms* if the measure of each singleton is zero. NB: Wikipedia assumes that for any measurable set `s` with positive `μ`-measure, there exists a measurable `t ⊆ s` such that `0 < μ t < μ s`. While this implies `μ {x} = 0`, the converse is not true. -/ class has_no_atoms {m0 : measurable_space α} (μ : measure α) : Prop := (measure_singleton : ∀ x, μ {x} = 0) export has_no_atoms (measure_singleton) attribute [simp] measure_singleton variables [has_no_atoms μ] lemma measure_subsingleton (hs : s.subsingleton) : μ s = 0 := hs.induction_on measure_empty measure_singleton alias measure_subsingleton ← set.subsingleton.measure_eq @[simp] lemma measure.restrict_singleton' {a : α} : μ.restrict {a} = 0 := by simp only [measure_singleton, measure.restrict_eq_zero] instance (s : set α) : has_no_atoms (μ.restrict s) := begin refine ⟨λ x, _⟩, obtain ⟨t, hxt, ht1, ht2⟩ := exists_measurable_superset_of_null (measure_singleton x : μ {x} = 0), apply measure_mono_null hxt, rw measure.restrict_apply ht1, apply measure_mono_null (inter_subset_left t s) ht2 end lemma _root_.set.countable.measure_zero (h : countable s) : μ s = 0 := begin rw [← bUnion_of_singleton s, ← nonpos_iff_eq_zero], refine le_trans (measure_bUnion_le h _) _, simp end lemma _root_.set.finite.measure_zero (h : s.finite) : μ s = 0 := h.countable.measure_zero lemma _root_.finset.measure_zero (s : finset α) : μ ↑s = 0 := s.finite_to_set.measure_zero lemma insert_ae_eq_self (a : α) (s : set α) : (insert a s : set α) =ᵐ[μ] s := union_ae_eq_right.2 $ measure_mono_null (diff_subset _ _) (measure_singleton _) variables [partial_order α] {a b : α} lemma Iio_ae_eq_Iic : Iio a =ᵐ[μ] Iic a := by simp only [← Iic_diff_right, diff_ae_eq_self, measure_mono_null (set.inter_subset_right _ _) (measure_singleton a)] lemma Ioi_ae_eq_Ici : Ioi a =ᵐ[μ] Ici a := @Iio_ae_eq_Iic (order_dual α) ‹_› ‹_› _ _ _ lemma Ioo_ae_eq_Ioc : Ioo a b =ᵐ[μ] Ioc a b := (ae_eq_refl _).inter Iio_ae_eq_Iic lemma Ioc_ae_eq_Icc : Ioc a b =ᵐ[μ] Icc a b := Ioi_ae_eq_Ici.inter (ae_eq_refl _) lemma Ioo_ae_eq_Ico : Ioo a b =ᵐ[μ] Ico a b := Ioi_ae_eq_Ici.inter (ae_eq_refl _) lemma Ioo_ae_eq_Icc : Ioo a b =ᵐ[μ] Icc a b := Ioi_ae_eq_Ici.inter Iio_ae_eq_Iic lemma Ico_ae_eq_Icc : Ico a b =ᵐ[μ] Icc a b := (ae_eq_refl _).inter Iio_ae_eq_Iic lemma Ico_ae_eq_Ioc : Ico a b =ᵐ[μ] Ioc a b := Ioo_ae_eq_Ico.symm.trans Ioo_ae_eq_Ioc end no_atoms lemma ite_ae_eq_of_measure_zero {γ} (f : α → γ) (g : α → γ) (s : set α) (hs_zero : μ s = 0) : (λ x, ite (x ∈ s) (f x) (g x)) =ᵐ[μ] g := begin have h_ss : sᶜ ⊆ {a : α | ite (a ∈ s) (f a) (g a) = g a}, from λ x hx, by simp [(set.mem_compl_iff _ _).mp hx], refine measure_mono_null _ hs_zero, nth_rewrite 0 ←compl_compl s, rwa set.compl_subset_compl, end lemma ite_ae_eq_of_measure_compl_zero {γ} (f : α → γ) (g : α → γ) (s : set α) (hs_zero : μ sᶜ = 0) : (λ x, ite (x ∈ s) (f x) (g x)) =ᵐ[μ] f := by { filter_upwards [hs_zero], intros, split_ifs, refl } namespace measure /-- A measure is called finite at filter `f` if it is finite at some set `s ∈ f`. Equivalently, it is eventually finite at `s` in `f.lift' powerset`. -/ def finite_at_filter {m0 : measurable_space α} (μ : measure α) (f : filter α) : Prop := ∃ s ∈ f, μ s < ∞ lemma finite_at_filter_of_finite {m0 : measurable_space α} (μ : measure α) [finite_measure μ] (f : filter α) : μ.finite_at_filter f := ⟨univ, univ_mem, measure_lt_top μ univ⟩ lemma finite_at_filter.exists_mem_basis {f : filter α} (hμ : finite_at_filter μ f) {p : ι → Prop} {s : ι → set α} (hf : f.has_basis p s) : ∃ i (hi : p i), μ (s i) < ∞ := (hf.exists_iff (λ s t hst ht, (measure_mono hst).trans_lt ht)).1 hμ lemma finite_at_bot {m0 : measurable_space α} (μ : measure α) : μ.finite_at_filter ⊥ := ⟨∅, mem_bot, by simp only [measure_empty, with_top.zero_lt_top]⟩ /-- `μ` has finite spanning sets in `C` if there is a countable sequence of sets in `C` that have finite measures. This structure is a type, which is useful if we want to record extra properties about the sets, such as that they are monotone. `sigma_finite` is defined in terms of this: `μ` is σ-finite if there exists a sequence of finite spanning sets in the collection of all measurable sets. -/ @[protect_proj, nolint has_inhabited_instance] structure finite_spanning_sets_in {m0 : measurable_space α} (μ : measure α) (C : set (set α)) := (set : ℕ → set α) (set_mem : ∀ i, set i ∈ C) (finite : ∀ i, μ (set i) < ∞) (spanning : (⋃ i, set i) = univ) end measure open measure /-- A measure `μ` is called σ-finite if there is a countable collection of sets `{ A i | i ∈ ℕ }` such that `μ (A i) < ∞` and `⋃ i, A i = s`. -/ class sigma_finite {m0 : measurable_space α} (μ : measure α) : Prop := (out' : nonempty (μ.finite_spanning_sets_in {s | measurable_set s})) theorem sigma_finite_iff : sigma_finite μ ↔ nonempty (μ.finite_spanning_sets_in {s | measurable_set s}) := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem sigma_finite.out (h : sigma_finite μ) : nonempty (μ.finite_spanning_sets_in {s | measurable_set s}) := h.1 include m0 /-- If `μ` is σ-finite it has finite spanning sets in the collection of all measurable sets. -/ def measure.to_finite_spanning_sets_in (μ : measure α) [h : sigma_finite μ] : μ.finite_spanning_sets_in {s | measurable_set s} := classical.choice h.out /-- A noncomputable way to get a monotone collection of sets that span `univ` and have finite measure using `classical.some`. This definition satisfies monotonicity in addition to all other properties in `sigma_finite`. -/ def spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) : set α := accumulate μ.to_finite_spanning_sets_in.set i lemma monotone_spanning_sets (μ : measure α) [sigma_finite μ] : monotone (spanning_sets μ) := monotone_accumulate lemma measurable_spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) : measurable_set (spanning_sets μ i) := measurable_set.Union $ λ j, measurable_set.Union_Prop $ λ hij, μ.to_finite_spanning_sets_in.set_mem j lemma measure_spanning_sets_lt_top (μ : measure α) [sigma_finite μ] (i : ℕ) : μ (spanning_sets μ i) < ∞ := measure_bUnion_lt_top (finite_le_nat i) $ λ j _, μ.to_finite_spanning_sets_in.finite j lemma Union_spanning_sets (μ : measure α) [sigma_finite μ] : (⋃ i : ℕ, spanning_sets μ i) = univ := by simp_rw [spanning_sets, Union_accumulate, μ.to_finite_spanning_sets_in.spanning] lemma is_countably_spanning_spanning_sets (μ : measure α) [sigma_finite μ] : is_countably_spanning (range (spanning_sets μ)) := ⟨spanning_sets μ, mem_range_self, Union_spanning_sets μ⟩ omit m0 namespace measure lemma supr_restrict_spanning_sets [sigma_finite μ] (hs : measurable_set s) : (⨆ i, μ.restrict (spanning_sets μ i) s) = μ s := begin convert (restrict_Union_apply_eq_supr (measurable_spanning_sets μ) _ hs).symm, { simp [Union_spanning_sets] }, { exact directed_of_sup (monotone_spanning_sets μ) } end namespace finite_spanning_sets_in variables {C D : set (set α)} /-- If `μ` has finite spanning sets in `C` and `C ⊆ D` then `μ` has finite spanning sets in `D`. -/ protected def mono (h : μ.finite_spanning_sets_in C) (hC : C ⊆ D) : μ.finite_spanning_sets_in D := ⟨h.set, λ i, hC (h.set_mem i), h.finite, h.spanning⟩ /-- If `μ` has finite spanning sets in the collection of measurable sets `C`, then `μ` is σ-finite. -/ protected lemma sigma_finite (h : μ.finite_spanning_sets_in C) (hC : ∀ s ∈ C, measurable_set s) : sigma_finite μ := ⟨⟨h.mono hC⟩⟩ /-- An extensionality for measures. It is `ext_of_generate_from_of_Union` formulated in terms of `finite_spanning_sets_in`. -/ protected lemma ext {ν : measure α} {C : set (set α)} (hA : ‹_› = generate_from C) (hC : is_pi_system C) (h : μ.finite_spanning_sets_in C) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν := ext_of_generate_from_of_Union C _ hA hC h.spanning h.set_mem h.finite h_eq protected lemma is_countably_spanning (h : μ.finite_spanning_sets_in C) : is_countably_spanning C := ⟨_, h.set_mem, h.spanning⟩ end finite_spanning_sets_in lemma sigma_finite_of_not_nonempty {m0 : measurable_space α} (μ : measure α) (hα : ¬ nonempty α) : sigma_finite μ := ⟨⟨⟨λ _, ∅, λ n, measurable_set.empty, λ n, by simp, by simp [eq_empty_of_not_nonempty hα univ]⟩⟩⟩ lemma sigma_finite_of_countable {S : set (set α)} (hc : countable S) (hμ : ∀ s ∈ S, μ s < ∞) (hU : ⋃₀ S = univ) : sigma_finite μ := begin obtain ⟨s, hμ, hs⟩ : ∃ s : ℕ → set α, (∀ n, μ (s n) < ∞) ∧ (⋃ n, s n) = univ, from (exists_seq_cover_iff_countable ⟨∅, by simp⟩).2 ⟨S, hc, hμ, hU⟩, refine ⟨⟨⟨λ n, to_measurable μ (s n), λ n, measurable_set_to_measurable _ _, by simpa, _⟩⟩⟩, exact eq_univ_of_subset (Union_subset_Union $ λ n, subset_to_measurable μ (s n)) hs end lemma sigma_finite_of_le (μ : measure α) [hs : sigma_finite μ] (h : ν ≤ μ) : sigma_finite ν := begin cases hs.out with C, exact ⟨nonempty.intro ⟨C.set, C.set_mem, λ i, (lt_of_le_of_lt (le_iff'.1 h _) (C.finite i)), C.spanning⟩⟩, end end measure include m0 /-- Every finite measure is σ-finite. -/ @[priority 100] instance finite_measure.to_sigma_finite (μ : measure α) [finite_measure μ] : sigma_finite μ := ⟨⟨⟨λ _, univ, λ _, measurable_set.univ, λ _, measure_lt_top μ _, Union_const _⟩⟩⟩ instance restrict.sigma_finite (μ : measure α) [sigma_finite μ] (s : set α) : sigma_finite (μ.restrict s) := begin refine ⟨⟨⟨spanning_sets μ, measurable_spanning_sets μ, λ i, _, Union_spanning_sets μ⟩⟩⟩, rw [restrict_apply (measurable_spanning_sets μ i)], exact (measure_mono $ inter_subset_left _ _).trans_lt (measure_spanning_sets_lt_top μ i) end instance sum.sigma_finite {ι} [fintype ι] (μ : ι → measure α) [∀ i, sigma_finite (μ i)] : sigma_finite (sum μ) := begin haveI : encodable ι := fintype.encodable ι, have : ∀ n, measurable_set (⋂ (i : ι), spanning_sets (μ i) n) := λ n, measurable_set.Inter (λ i, measurable_spanning_sets (μ i) n), refine ⟨⟨⟨λ n, ⋂ i, spanning_sets (μ i) n, this, λ n, _, _⟩⟩⟩, { rw [sum_apply _ (this n), tsum_fintype, ennreal.sum_lt_top_iff], rintro i -, exact (measure_mono $ Inter_subset _ i).trans_lt (measure_spanning_sets_lt_top (μ i) n) }, { rw [Union_Inter_of_monotone], simp_rw [Union_spanning_sets, Inter_univ], exact λ i, monotone_spanning_sets (μ i), } end instance add.sigma_finite (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] : sigma_finite (μ + ν) := by { rw [← sum_cond], refine @sum.sigma_finite _ _ _ _ _ (bool.rec _ _); simpa } lemma sigma_finite.of_map (μ : measure α) {f : α → β} (hf : measurable f) (h : sigma_finite (map f μ)) : sigma_finite μ := ⟨⟨⟨λ n, f ⁻¹' (spanning_sets (map f μ) n), λ n, hf $ measurable_spanning_sets _ _, λ n, by simp only [← map_apply hf, measurable_spanning_sets, measure_spanning_sets_lt_top], by rw [← preimage_Union, Union_spanning_sets, preimage_univ]⟩⟩⟩ /-- A measure is called locally finite if it is finite in some neighborhood of each point. -/ class locally_finite_measure [topological_space α] (μ : measure α) : Prop := (finite_at_nhds : ∀ x, μ.finite_at_filter (𝓝 x)) @[priority 100] -- see Note [lower instance priority] instance finite_measure.to_locally_finite_measure [topological_space α] (μ : measure α) [finite_measure μ] : locally_finite_measure μ := ⟨λ x, finite_at_filter_of_finite _ _⟩ lemma measure.finite_at_nhds [topological_space α] (μ : measure α) [locally_finite_measure μ] (x : α) : μ.finite_at_filter (𝓝 x) := locally_finite_measure.finite_at_nhds x lemma measure.smul_finite (μ : measure α) [finite_measure μ] {c : ℝ≥0∞} (hc : c < ∞) : finite_measure (c • μ) := begin refine ⟨_⟩, rw measure.smul_apply, exact ennreal.mul_lt_top hc (measure_lt_top μ set.univ), end lemma measure.exists_is_open_measure_lt_top [topological_space α] (μ : measure α) [locally_finite_measure μ] (x : α) : ∃ s : set α, x ∈ s ∧ is_open s ∧ μ s < ∞ := by simpa only [exists_prop, and.assoc] using (μ.finite_at_nhds x).exists_mem_basis (nhds_basis_opens x) omit m0 @[priority 100] -- see Note [lower instance priority] instance sigma_finite_of_locally_finite [topological_space α] [topological_space.second_countable_topology α] [locally_finite_measure μ] : sigma_finite μ := begin choose s hsx hsμ using μ.finite_at_nhds, rcases topological_space.countable_cover_nhds hsx with ⟨t, htc, htU⟩, refine measure.sigma_finite_of_countable (htc.image s) (ball_image_iff.2 $ λ x hx, hsμ x) _, rwa sUnion_image end /-- If two finite measures give the same mass to the whole space and coincide on a π-system made of measurable sets, then they coincide on all sets in the σ-algebra generated by the π-system. -/ lemma ext_on_measurable_space_of_generate_finite {α} (m₀ : measurable_space α) {μ ν : measure α} [finite_measure μ] (C : set (set α)) (hμν : ∀ s ∈ C, μ s = ν s) {m : measurable_space α} (h : m ≤ m₀) (hA : m = measurable_space.generate_from C) (hC : is_pi_system C) (h_univ : μ set.univ = ν set.univ) {s : set α} (hs : m.measurable_set' s) : μ s = ν s := begin haveI : finite_measure ν := begin constructor, rw ← h_univ, apply finite_measure.measure_univ_lt_top, end, refine induction_on_inter hA hC (by simp) hμν _ _ hs, { intros t h1t h2t, have h1t_ : @measurable_set α m₀ t, from h _ h1t, rw [@measure_compl α m₀ μ t h1t_ (@measure_lt_top α m₀ μ _ t), @measure_compl α m₀ ν t h1t_ (@measure_lt_top α m₀ ν _ t), h_univ, h2t], }, { intros f h1f h2f h3f, have h2f_ : ∀ (i : ℕ), @measurable_set α m₀ (f i), from (λ i, h _ (h2f i)), have h_Union : @measurable_set α m₀ (⋃ (i : ℕ), f i),from @measurable_set.Union α ℕ m₀ _ f h2f_, simp [measure_Union, h_Union, h1f, h3f, h2f_], }, end /-- Two finite measures are equal if they are equal on the π-system generating the σ-algebra (and `univ`). -/ lemma ext_of_generate_finite (C : set (set α)) (hA : m0 = generate_from C) (hC : is_pi_system C) [finite_measure μ] (hμν : ∀ s ∈ C, μ s = ν s) (h_univ : μ univ = ν univ) : μ = ν := measure.ext (λ s hs, ext_on_measurable_space_of_generate_finite m0 C hμν le_rfl hA hC h_univ hs) namespace measure namespace finite_at_filter variables {f g : filter α} lemma filter_mono (h : f ≤ g) : μ.finite_at_filter g → μ.finite_at_filter f := λ ⟨s, hs, hμ⟩, ⟨s, h hs, hμ⟩ lemma inf_of_left (h : μ.finite_at_filter f) : μ.finite_at_filter (f ⊓ g) := h.filter_mono inf_le_left lemma inf_of_right (h : μ.finite_at_filter g) : μ.finite_at_filter (f ⊓ g) := h.filter_mono inf_le_right @[simp] lemma inf_ae_iff : μ.finite_at_filter (f ⊓ μ.ae) ↔ μ.finite_at_filter f := begin refine ⟨_, λ h, h.filter_mono inf_le_left⟩, rintros ⟨s, ⟨t, ht, u, hu, rfl⟩, hμ⟩, suffices : μ t ≤ μ (t ∩ u), from ⟨t, ht, this.trans_lt hμ⟩, exact measure_mono_ae (mem_of_superset hu (λ x hu ht, ⟨ht, hu⟩)) end alias inf_ae_iff ↔ measure_theory.measure.finite_at_filter.of_inf_ae _ lemma filter_mono_ae (h : f ⊓ μ.ae ≤ g) (hg : μ.finite_at_filter g) : μ.finite_at_filter f := inf_ae_iff.1 (hg.filter_mono h) protected lemma measure_mono (h : μ ≤ ν) : ν.finite_at_filter f → μ.finite_at_filter f := λ ⟨s, hs, hν⟩, ⟨s, hs, (measure.le_iff'.1 h s).trans_lt hν⟩ @[mono] protected lemma mono (hf : f ≤ g) (hμ : μ ≤ ν) : ν.finite_at_filter g → μ.finite_at_filter f := λ h, (h.filter_mono hf).measure_mono hμ protected lemma eventually (h : μ.finite_at_filter f) : ∀ᶠ s in f.lift' powerset, μ s < ∞ := (eventually_lift'_powerset' $ λ s t hst ht, (measure_mono hst).trans_lt ht).2 h lemma filter_sup : μ.finite_at_filter f → μ.finite_at_filter g → μ.finite_at_filter (f ⊔ g) := λ ⟨s, hsf, hsμ⟩ ⟨t, htg, htμ⟩, ⟨s ∪ t, union_mem_sup hsf htg, (measure_union_le s t).trans_lt (ennreal.add_lt_top.2 ⟨hsμ, htμ⟩)⟩ end finite_at_filter lemma finite_at_nhds_within [topological_space α] {m0 : measurable_space α} (μ : measure α) [locally_finite_measure μ] (x : α) (s : set α) : μ.finite_at_filter (𝓝[s] x) := (finite_at_nhds μ x).inf_of_left @[simp] lemma finite_at_principal : μ.finite_at_filter (𝓟 s) ↔ μ s < ∞ := ⟨λ ⟨t, ht, hμ⟩, (measure_mono ht).trans_lt hμ, λ h, ⟨s, mem_principal_self s, h⟩⟩ /-! ### Subtraction of measures -/ /-- The measure `μ - ν` is defined to be the least measure `τ` such that `μ ≤ τ + ν`. It is the equivalent of `(μ - ν) ⊔ 0` if `μ` and `ν` were signed measures. Compare with `ennreal.has_sub`. Specifically, note that if you have `α = {1,2}`, and `μ {1} = 2`, `μ {2} = 0`, and `ν {2} = 2`, `ν {1} = 0`, then `(μ - ν) {1, 2} = 2`. However, if `μ ≤ ν`, and `ν univ ≠ ∞`, then `(μ - ν) + ν = μ`. -/ noncomputable instance has_sub {α : Type*} [measurable_space α] : has_sub (measure α) := ⟨λ μ ν, Inf {τ | μ ≤ τ + ν} ⟩ section measure_sub lemma sub_def : μ - ν = Inf {d | μ ≤ d + ν} := rfl lemma sub_eq_zero_of_le (h : μ ≤ ν) : μ - ν = 0 := begin rw [← nonpos_iff_eq_zero', measure.sub_def], apply @Inf_le (measure α) _ _, simp [h], end /-- This application lemma only works in special circumstances. Given knowledge of when `μ ≤ ν` and `ν ≤ μ`, a more general application lemma can be written. -/ lemma sub_apply [finite_measure ν] (h₁ : measurable_set s) (h₂ : ν ≤ μ) : (μ - ν) s = μ s - ν s := begin -- We begin by defining `measure_sub`, which will be equal to `(μ - ν)`. let measure_sub : measure α := @measure_theory.measure.of_measurable α _ (λ (t : set α) (h_t_measurable_set : measurable_set t), (μ t - ν t)) begin simp end begin intros g h_meas h_disj, simp only, rw ennreal.tsum_sub, repeat { rw ← measure_theory.measure_Union h_disj h_meas }, apply measure_theory.measure_lt_top, intro i, apply h₂, apply h_meas end, -- Now, we demonstrate `μ - ν = measure_sub`, and apply it. begin have h_measure_sub_add : (ν + measure_sub = μ), { ext t h_t_measurable_set, simp only [pi.add_apply, coe_add], rw [measure_theory.measure.of_measurable_apply _ h_t_measurable_set, add_comm, ennreal.sub_add_cancel_of_le (h₂ t h_t_measurable_set)] }, have h_measure_sub_eq : (μ - ν) = measure_sub, { rw measure_theory.measure.sub_def, apply le_antisymm, { apply @Inf_le (measure α) measure.complete_semilattice_Inf, simp [le_refl, add_comm, h_measure_sub_add] }, apply @le_Inf (measure α) measure.complete_semilattice_Inf, intros d h_d, rw [← h_measure_sub_add, mem_set_of_eq, add_comm d] at h_d, apply measure.le_of_add_le_add_left h_d }, rw h_measure_sub_eq, apply measure.of_measurable_apply _ h₁, end end lemma sub_add_cancel_of_le [finite_measure ν] (h₁ : ν ≤ μ) : μ - ν + ν = μ := begin ext s h_s_meas, rw [add_apply, sub_apply h_s_meas h₁, ennreal.sub_add_cancel_of_le (h₁ s h_s_meas)], end lemma sub_le : μ - ν ≤ μ := Inf_le (measure.le_add_right (le_refl _)) end measure_sub lemma restrict_sub_eq_restrict_sub_restrict (h_meas_s : measurable_set s) : (μ - ν).restrict s = (μ.restrict s) - (ν.restrict s) := begin repeat {rw sub_def}, have h_nonempty : {d | μ ≤ d + ν}.nonempty, { apply @set.nonempty_of_mem _ _ μ, rw mem_set_of_eq, intros t h_meas, exact le_self_add }, rw restrict_Inf_eq_Inf_restrict h_nonempty h_meas_s, apply le_antisymm, { apply @Inf_le_Inf_of_forall_exists_le (measure α) _, intros ν' h_ν'_in, rw mem_set_of_eq at h_ν'_in, apply exists.intro (ν'.restrict s), split, { rw mem_image, apply exists.intro (ν' + (⊤ : measure_theory.measure α).restrict sᶜ), rw mem_set_of_eq, split, { rw [add_assoc, add_comm _ ν, ← add_assoc, measure_theory.measure.le_iff], intros t h_meas_t, have h_inter_inter_eq_inter : ∀ t' : set α , t ∩ t' ∩ t' = t ∩ t', { intro t', rw set.inter_eq_self_of_subset_left, apply set.inter_subset_right t t' }, have h_meas_t_inter_s : measurable_set (t ∩ s) := h_meas_t.inter h_meas_s, repeat {rw measure_eq_inter_diff h_meas_t h_meas_s, rw set.diff_eq}, refine add_le_add _ _, { rw add_apply, apply le_add_right _, rw add_apply, rw ← @restrict_eq_self _ _ μ s _ h_meas_t_inter_s (set.inter_subset_right _ _), rw ← @restrict_eq_self _ _ ν s _ h_meas_t_inter_s (set.inter_subset_right _ _), apply h_ν'_in _ h_meas_t_inter_s }, cases (@set.eq_empty_or_nonempty _ (t ∩ sᶜ)) with h_inter_empty h_inter_nonempty, { simp [h_inter_empty] }, { rw add_apply, have h_meas_inter_compl := h_meas_t.inter (measurable_set.compl h_meas_s), rw [restrict_apply h_meas_inter_compl, h_inter_inter_eq_inter sᶜ], have h_mu_le_add_top : μ ≤ ν' + ν + ⊤, { rw add_comm, have h_le_top : μ ≤ ⊤ := le_top, apply (λ t₂ h_meas, le_add_right (h_le_top t₂ h_meas)) }, apply h_mu_le_add_top _ h_meas_inter_compl } }, { ext1 t h_meas_t, simp [restrict_apply h_meas_t, restrict_apply (h_meas_t.inter h_meas_s), set.inter_assoc] } }, { apply restrict_le_self } }, { apply @Inf_le_Inf_of_forall_exists_le (measure α) _, intros s h_s_in, cases h_s_in with t h_t, cases h_t with h_t_in h_t_eq, subst s, apply exists.intro (t.restrict s), split, { rw [set.mem_set_of_eq, ← restrict_add], apply restrict_mono (set.subset.refl _) h_t_in }, { apply le_refl _ } }, end lemma sub_apply_eq_zero_of_restrict_le_restrict (h_le : μ.restrict s ≤ ν.restrict s) (h_meas_s : measurable_set s) : (μ - ν) s = 0 := begin rw [← restrict_apply_self _ h_meas_s, restrict_sub_eq_restrict_sub_restrict, sub_eq_zero_of_le], repeat {simp [*]}, end instance finite_measure_sub [finite_measure μ] : finite_measure (μ - ν) := { measure_univ_lt_top := lt_of_le_of_lt (measure.sub_le set.univ measurable_set.univ) (measure_lt_top _ _) } end measure end measure_theory open measure_theory measure_theory.measure namespace measurable_equiv /-! Interactions of measurable equivalences and measures -/ open equiv measure_theory.measure variables [measurable_space α] [measurable_space β] {μ : measure α} {ν : measure β} /-- If we map a measure along a measurable equivalence, we can compute the measure on all sets (not just the measurable ones). -/ protected theorem map_apply (f : α ≃ᵐ β) (s : set β) : map f μ s = μ (f ⁻¹' s) := begin refine le_antisymm _ (le_map_apply f.measurable s), rw [measure_eq_infi' μ], refine le_infi _, rintro ⟨t, hst, ht⟩, rw [subtype.coe_mk], have := f.symm.to_equiv.image_eq_preimage, simp only [←coe_eq, symm_symm, symm_to_equiv] at this, rw [← this, image_subset_iff] at hst, convert measure_mono hst, rw [map_apply, preimage_preimage], { refine congr_arg μ (eq.symm _), convert preimage_id, exact funext f.left_inv }, exacts [f.measurable, f.measurable_inv_fun ht] end @[simp] lemma map_symm_map (e : α ≃ᵐ β) : map e.symm (map e μ) = μ := by simp [map_map e.symm.measurable e.measurable] @[simp] lemma map_map_symm (e : α ≃ᵐ β) : map e (map e.symm ν) = ν := by simp [map_map e.measurable e.symm.measurable] lemma map_measurable_equiv_injective (e : α ≃ᵐ β) : injective (map e) := by { intros μ₁ μ₂ hμ, apply_fun map e.symm at hμ, simpa [map_symm_map e] using hμ } lemma map_apply_eq_iff_map_symm_apply_eq (e : α ≃ᵐ β) : map e μ = ν ↔ map e.symm ν = μ := by rw [← (map_measurable_equiv_injective e).eq_iff, map_map_symm, eq_comm] end measurable_equiv section is_complete /-- A measure is complete if every null set is also measurable. A null set is a subset of a measurable set with measure `0`. Since every measure is defined as a special case of an outer measure, we can more simply state that a set `s` is null if `μ s = 0`. -/ class measure_theory.measure.is_complete {_ : measurable_space α} (μ : measure α) : Prop := (out' : ∀ s, μ s = 0 → measurable_set s) theorem measure_theory.measure.is_complete_iff {_ : measurable_space α} {μ : measure α} : μ.is_complete ↔ ∀ s, μ s = 0 → measurable_set s := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem measure_theory.measure.is_complete.out {_ : measurable_space α} {μ : measure α} (h : μ.is_complete) : ∀ s, μ s = 0 → measurable_set s := h.1 variables [measurable_space α] {μ : measure α} {s t z : set α} /-- A set is null measurable if it is the union of a null set and a measurable set. -/ def null_measurable_set (μ : measure α) (s : set α) : Prop := ∃ t z, s = t ∪ z ∧ measurable_set t ∧ μ z = 0 theorem null_measurable_set_iff : null_measurable_set μ s ↔ ∃ t, t ⊆ s ∧ measurable_set t ∧ μ (s \ t) = 0 := begin split, { rintro ⟨t, z, rfl, ht, hz⟩, refine ⟨t, set.subset_union_left _ _, ht, measure_mono_null _ hz⟩, simp [union_diff_left, diff_subset] }, { rintro ⟨t, st, ht, hz⟩, exact ⟨t, _, (union_diff_cancel st).symm, ht, hz⟩ } end theorem null_measurable_set_measure_eq (st : t ⊆ s) (hz : μ (s \ t) = 0) : μ s = μ t := begin refine le_antisymm _ (measure_mono st), have := measure_union_le t (s \ t), rw [union_diff_cancel st, hz] at this, simpa end theorem measurable_set.null_measurable_set (μ : measure α) (hs : measurable_set s) : null_measurable_set μ s := ⟨s, ∅, by simp, hs, μ.empty⟩ theorem null_measurable_set_of_complete (μ : measure α) [c : μ.is_complete] : null_measurable_set μ s ↔ measurable_set s := ⟨by rintro ⟨t, z, rfl, ht, hz⟩; exact measurable_set.union ht (c.out _ hz), λ h, h.null_measurable_set _⟩ theorem null_measurable_set.union_null (hs : null_measurable_set μ s) (hz : μ z = 0) : null_measurable_set μ (s ∪ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, exact ⟨t, z' ∪ z, set.union_assoc _ _ _, ht, nonpos_iff_eq_zero.1 (le_trans (measure_union_le _ _) $ by simp [hz, hz'])⟩ end theorem null_null_measurable_set (hz : μ z = 0) : null_measurable_set μ z := by simpa using (measurable_set.empty.null_measurable_set _).union_null hz theorem null_measurable_set.Union_nat {s : ℕ → set α} (hs : ∀ i, null_measurable_set μ (s i)) : null_measurable_set μ (Union s) := begin choose t ht using assume i, null_measurable_set_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, refine null_measurable_set_iff.2 ⟨Union t, Union_subset_Union st, measurable_set.Union ht, measure_mono_null _ (measure_Union_null hz)⟩, rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (λ i, by rw ← diff_subset_iff) end theorem measurable_set.diff_null (hs : measurable_set s) (hz : μ z = 0) : null_measurable_set μ (s \ z) := begin rw measure_eq_infi at hz, choose f hf using show ∀ q : {q : ℚ // q > 0}, ∃ t : set α, z ⊆ t ∧ measurable_set t ∧ μ t < (real.to_nnreal q.1 : ℝ≥0∞), { rintro ⟨ε, ε0⟩, have : 0 < (real.to_nnreal ε : ℝ≥0∞), { simpa using ε0 }, rw ← hz at this, simpa [infi_lt_iff] }, refine null_measurable_set_iff.2 ⟨s \ Inter f, diff_subset_diff_right (subset_Inter (λ i, (hf i).1)), hs.diff (measurable_set.Inter (λ i, (hf i).2.1)), measure_mono_null _ (nonpos_iff_eq_zero.1 $ le_of_not_lt $ λ h, _)⟩, { exact Inter f }, { rw [diff_subset_iff, diff_union_self], exact subset.trans (diff_subset _ _) (subset_union_left _ _) }, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨ε, ε0', ε0, h⟩, simp at ε0, apply not_le_of_lt (lt_trans (hf ⟨ε, ε0⟩).2.2 h), exact measure_mono (Inter_subset _ _) end theorem null_measurable_set.diff_null (hs : null_measurable_set μ s) (hz : μ z = 0) : null_measurable_set μ (s \ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, rw [set.union_diff_distrib], exact (ht.diff_null hz).union_null (measure_mono_null (diff_subset _ _) hz') end theorem null_measurable_set.compl (hs : null_measurable_set μ s) : null_measurable_set μ sᶜ := begin rcases hs with ⟨t, z, rfl, ht, hz⟩, rw compl_union, exact ht.compl.diff_null hz end theorem null_measurable_set_iff_ae {s : set α} : null_measurable_set μ s ↔ ∃ t, measurable_set t ∧ s =ᵐ[μ] t := begin simp only [ae_eq_set], split, { assume h, rcases null_measurable_set_iff.1 h with ⟨t, ts, tmeas, ht⟩, refine ⟨t, tmeas, ht, _⟩, rw [diff_eq_empty.2 ts, measure_empty] }, { rintros ⟨t, tmeas, h₁, h₂⟩, have : null_measurable_set μ (t ∪ (s \ t)) := null_measurable_set.union_null (tmeas.null_measurable_set _) h₁, have A : null_measurable_set μ ((t ∪ (s \ t)) \ (t \ s)) := null_measurable_set.diff_null this h₂, have : (t ∪ (s \ t)) \ (t \ s) = s, { apply subset.antisymm, { assume x hx, simp only [mem_union_eq, not_and, mem_diff, not_not_mem] at hx, cases hx.1, { exact hx.2 h }, { exact h.1 } }, { assume x hx, simp [hx, classical.em (x ∈ t)] } }, rwa this at A } end theorem null_measurable_set_iff_sandwich {s : set α} : null_measurable_set μ s ↔ ∃ (t u : set α), measurable_set t ∧ measurable_set u ∧ t ⊆ s ∧ s ⊆ u ∧ μ (u \ t) = 0 := begin split, { assume h, rcases null_measurable_set_iff.1 h with ⟨t, ts, tmeas, ht⟩, rcases null_measurable_set_iff.1 h.compl with ⟨u', u's, u'meas, hu'⟩, have A : s ⊆ u'ᶜ := subset_compl_comm.mp u's, refine ⟨t, u'ᶜ, tmeas, u'meas.compl, ts, A, _⟩, have : sᶜ \ u' = u'ᶜ \ s, by simp [compl_eq_univ_diff, diff_diff, union_comm], rw this at hu', apply le_antisymm _ bot_le, calc μ (u'ᶜ \ t) ≤ μ ((u'ᶜ \ s) ∪ (s \ t)) : begin apply measure_mono, assume x hx, simp at hx, simp [hx, or_comm, classical.em], end ... ≤ μ (u'ᶜ \ s) + μ (s \ t) : measure_union_le _ _ ... = 0 : by rw [ht, hu', zero_add] }, { rintros ⟨t, u, tmeas, umeas, ts, su, hμ⟩, refine null_measurable_set_iff.2 ⟨t, ts, tmeas, _⟩, apply le_antisymm _ bot_le, calc μ (s \ t) ≤ μ (u \ t) : measure_mono (diff_subset_diff_left su) ... = 0 : hμ } end lemma restrict_apply_of_null_measurable_set {s t : set α} (ht : null_measurable_set (μ.restrict s) t) : μ.restrict s t = μ (t ∩ s) := begin rcases null_measurable_set_iff_sandwich.1 ht with ⟨u, v, umeas, vmeas, ut, tv, huv⟩, apply le_antisymm _ (le_restrict_apply _ _), calc μ.restrict s t ≤ μ.restrict s v : measure_mono tv ... = μ (v ∩ s) : restrict_apply vmeas ... ≤ μ ((u ∩ s) ∪ ((v \ u) ∩ s)) : measure_mono $ by { assume x hx, simp at hx, simp [hx, classical.em] } ... ≤ μ (u ∩ s) + μ ((v \ u) ∩ s) : measure_union_le _ _ ... = μ (u ∩ s) + μ.restrict s (v \ u) : by rw measure.restrict_apply (vmeas.diff umeas) ... = μ (u ∩ s) : by rw [huv, add_zero] ... ≤ μ (t ∩ s) : measure_mono $ inter_subset_inter_left s ut end /-- The measurable space of all null measurable sets. -/ def null_measurable (μ : measure α) : measurable_space α := { measurable_set' := null_measurable_set μ, measurable_set_empty := measurable_set.empty.null_measurable_set _, measurable_set_compl := λ s hs, hs.compl, measurable_set_Union := λ f, null_measurable_set.Union_nat } /-- Given a measure we can complete it to a (complete) measure on all null measurable sets. -/ def completion (μ : measure α) : @measure_theory.measure α (null_measurable μ) := { to_outer_measure := μ.to_outer_measure, m_Union := λ s hs hd, show μ (Union s) = ∑' i, μ (s i), begin choose t ht using assume i, null_measurable_set_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, rw null_measurable_set_measure_eq (Union_subset_Union st), { rw measure_Union _ ht, { congr, funext i, exact (null_measurable_set_measure_eq (st i) (hz i)).symm }, { rintro i j ij x ⟨h₁, h₂⟩, exact hd i j ij ⟨st i h₁, st j h₂⟩ } }, { refine measure_mono_null _ (measure_Union_null hz), rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (λ i, by rw ← diff_subset_iff) } end, trimmed := begin letI := null_measurable μ, refine le_antisymm (λ s, _) (outer_measure.le_trim _), rw outer_measure.trim_eq_infi, dsimp, clear _inst, resetI, rw measure_eq_infi s, exact infi_le_infi (λ t, infi_le_infi $ λ st, infi_le_infi2 $ λ ht, ⟨ht.null_measurable_set _, le_refl _⟩) end } instance completion.is_complete (μ : measure α) : (completion μ).is_complete := ⟨λ z hz, null_null_measurable_set hz⟩ lemma measurable.ae_eq {α β} [measurable_space α] [measurable_space β] {μ : measure α} [hμ : μ.is_complete] {f g : α → β} (hf : measurable f) (hfg : f =ᵐ[μ] g) : measurable g := begin intros s hs, let t := {x | f x = g x}, have ht_compl : μ tᶜ = 0, by rwa [filter.eventually_eq, ae_iff] at hfg, rw (set.inter_union_compl (g ⁻¹' s) t).symm, refine measurable_set.union _ _, { have h_g_to_f : (g ⁻¹' s) ∩ t = (f ⁻¹' s) ∩ t, { ext, simp only [set.mem_inter_iff, set.mem_preimage, and.congr_left_iff, set.mem_set_of_eq], exact λ hx, by rw hx, }, rw h_g_to_f, exact measurable_set.inter (hf hs) (measurable_set.compl_iff.mp (hμ.out tᶜ ht_compl)), }, { exact hμ.out (g ⁻¹' s ∩ tᶜ) (measure_mono_null (set.inter_subset_right _ _) ht_compl), }, end end is_complete namespace measure_theory lemma outer_measure.to_measure_zero [measurable_space α] : (0 : outer_measure α).to_measure ((le_top).trans outer_measure.zero_caratheodory.symm.le) = 0 := by rw [← measure.measure_univ_eq_zero, to_measure_apply _ _ measurable_set.univ, outer_measure.coe_zero, pi.zero_apply] section trim /-- Restriction of a measure to a sub-sigma algebra. It is common to see a measure `μ` on a measurable space structure `m0` as being also a measure on any `m ≤ m0`. Since measures in mathlib have to be trimmed to the measurable space, `μ` itself cannot be a measure on `m`, hence the definition of `μ.trim hm`. This notion is related to `outer_measure.trim`, see the lemma `to_outer_measure_trim_eq_trim_to_outer_measure`. -/ def measure.trim {m m0 : measurable_space α} (μ : @measure α m0) (hm : m ≤ m0) : @measure α m := @outer_measure.to_measure α m μ.to_outer_measure (hm.trans (le_to_outer_measure_caratheodory μ)) @[simp] lemma trim_eq_self [measurable_space α] {μ : measure α} : μ.trim le_rfl = μ := by simp [measure.trim] variables {m m0 : measurable_space α} {μ : measure α} {s : set α} lemma to_outer_measure_trim_eq_trim_to_outer_measure (μ : measure α) (hm : m ≤ m0) : @measure.to_outer_measure _ m (μ.trim hm) = @outer_measure.trim _ m μ.to_outer_measure := by rw [measure.trim, to_measure_to_outer_measure] @[simp] lemma zero_trim (hm : m ≤ m0) : (0 : measure α).trim hm = (0 : @measure α m) := by simp [measure.trim, outer_measure.to_measure_zero] lemma trim_measurable_set_eq (hm : m ≤ m0) (hs : @measurable_set α m s) : μ.trim hm s = μ s := by simp [measure.trim, hs] lemma le_trim (hm : m ≤ m0) : μ s ≤ μ.trim hm s := by { simp_rw [measure.trim], exact (@le_to_measure_apply _ m _ _ _), } lemma measure_eq_zero_of_trim_eq_zero (hm : m ≤ m0) (h : μ.trim hm s = 0) : μ s = 0 := le_antisymm ((le_trim hm).trans (le_of_eq h)) (zero_le _) lemma measure_trim_to_measurable_eq_zero {hm : m ≤ m0} (hs : μ.trim hm s = 0) : μ (@to_measurable α m (μ.trim hm) s) = 0 := measure_eq_zero_of_trim_eq_zero hm (by rwa measure_to_measurable) lemma ae_eq_of_ae_eq_trim {E} {hm : m ≤ m0} {f₁ f₂ : α → E} (h12 : f₁ =ᶠ[@measure.ae α m (μ.trim hm)] f₂) : f₁ =ᵐ[μ] f₂ := measure_eq_zero_of_trim_eq_zero hm h12 lemma restrict_trim (hm : m ≤ m0) (μ : measure α) (hs : @measurable_set α m s) : @measure.restrict α m (μ.trim hm) s = (μ.restrict s).trim hm := begin ext1 t ht, rw [@measure.restrict_apply α m _ _ _ ht, trim_measurable_set_eq hm ht, measure.restrict_apply (hm t ht), trim_measurable_set_eq hm (@measurable_set.inter α m t s ht hs)], end instance finite_measure_trim (hm : m ≤ m0) [finite_measure μ] : finite_measure (μ.trim hm) := { measure_univ_lt_top := by { rw trim_measurable_set_eq hm (@measurable_set.univ _ m), exact measure_lt_top _ _, } } end trim end measure_theory /-! # Almost everywhere measurable functions A function is almost everywhere measurable if it coincides almost everywhere with a measurable function. This property, called `ae_measurable f μ`, is defined in the file `measure_space_def`. We discuss several of its properties that are analogous to properties of measurable functions. -/ section open measure_theory variables [measurable_space α] [measurable_space β] {f g : α → β} {μ ν : measure α} @[nontriviality, measurability] lemma subsingleton.ae_measurable [subsingleton α] : ae_measurable f μ := subsingleton.measurable.ae_measurable @[simp, measurability] lemma ae_measurable_zero_measure : ae_measurable f (0 : measure α) := begin nontriviality α, inhabit α, exact ⟨λ x, f (default α), measurable_const, rfl⟩ end lemma ae_measurable_iff_measurable [μ.is_complete] : ae_measurable f μ ↔ measurable f := begin split; intro h, { rcases h with ⟨g, hg_meas, hfg⟩, exact hg_meas.ae_eq hfg.symm, }, { exact h.ae_measurable, }, end namespace ae_measurable lemma mono_measure (h : ae_measurable f μ) (h' : ν ≤ μ) : ae_measurable f ν := ⟨h.mk f, h.measurable_mk, eventually.filter_mono (ae_mono h') h.ae_eq_mk⟩ lemma mono_set {s t} (h : s ⊆ t) (ht : ae_measurable f (μ.restrict t)) : ae_measurable f (μ.restrict s) := ht.mono_measure (restrict_mono h le_rfl) protected lemma mono' (h : ae_measurable f μ) (h' : ν ≪ μ) : ae_measurable f ν := ⟨h.mk f, h.measurable_mk, h' h.ae_eq_mk⟩ lemma ae_mem_imp_eq_mk {s} (h : ae_measurable f (μ.restrict s)) : ∀ᵐ x ∂μ, x ∈ s → f x = h.mk f x := ae_imp_of_ae_restrict h.ae_eq_mk lemma ae_inf_principal_eq_mk {s} (h : ae_measurable f (μ.restrict s)) : f =ᶠ[μ.ae ⊓ 𝓟 s] h.mk f := le_ae_restrict h.ae_eq_mk @[measurability] lemma add_measure {f : α → β} (hμ : ae_measurable f μ) (hν : ae_measurable f ν) : ae_measurable f (μ + ν) := begin let s := {x | f x ≠ hμ.mk f x}, have : μ s = 0 := hμ.ae_eq_mk, obtain ⟨t, st, t_meas, μt⟩ : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = 0 := exists_measurable_superset_of_null this, let g : α → β := t.piecewise (hν.mk f) (hμ.mk f), refine ⟨g, measurable.piecewise t_meas hν.measurable_mk hμ.measurable_mk, _⟩, change μ {x | f x ≠ g x} + ν {x | f x ≠ g x} = 0, suffices : μ {x | f x ≠ g x} = 0 ∧ ν {x | f x ≠ g x} = 0, by simp [this.1, this.2], have ht : {x | f x ≠ g x} ⊆ t, { assume x hx, by_contra h, simp only [g, h, mem_set_of_eq, ne.def, not_false_iff, piecewise_eq_of_not_mem] at hx, exact h (st hx) }, split, { have : μ {x | f x ≠ g x} ≤ μ t := measure_mono ht, rw μt at this, exact le_antisymm this bot_le }, { have : {x | f x ≠ g x} ⊆ {x | f x ≠ hν.mk f x}, { assume x hx, simpa [ht hx, g] using hx }, apply le_antisymm _ bot_le, calc ν {x | f x ≠ g x} ≤ ν {x | f x ≠ hν.mk f x} : measure_mono this ... = 0 : hν.ae_eq_mk } end @[measurability] lemma smul_measure (h : ae_measurable f μ) (c : ℝ≥0∞) : ae_measurable f (c • μ) := ⟨h.mk f, h.measurable_mk, ae_smul_measure h.ae_eq_mk c⟩ lemma comp_measurable [measurable_space δ] {f : α → δ} {g : δ → β} (hg : ae_measurable g (map f μ)) (hf : measurable f) : ae_measurable (g ∘ f) μ := ⟨hg.mk g ∘ f, hg.measurable_mk.comp hf, ae_eq_comp hf hg.ae_eq_mk⟩ lemma comp_measurable' {δ} [measurable_space δ] {ν : measure δ} {f : α → δ} {g : δ → β} (hg : ae_measurable g ν) (hf : measurable f) (h : map f μ ≪ ν) : ae_measurable (g ∘ f) μ := (hg.mono' h).comp_measurable hf @[measurability] lemma prod_mk {γ : Type*} [measurable_space γ] {f : α → β} {g : α → γ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, (f x, g x)) μ := ⟨λ a, (hf.mk f a, hg.mk g a), hf.measurable_mk.prod_mk hg.measurable_mk, eventually_eq.prod_mk hf.ae_eq_mk hg.ae_eq_mk⟩ protected lemma null_measurable_set (h : ae_measurable f μ) {s : set β} (hs : measurable_set s) : null_measurable_set μ (f ⁻¹' s) := begin apply null_measurable_set_iff_ae.2, refine ⟨(h.mk f) ⁻¹' s, h.measurable_mk hs, _⟩, filter_upwards [h.ae_eq_mk], assume x hx, change (f x ∈ s) = ((h.mk f) x ∈ s), rwa hx end end ae_measurable @[simp] lemma ae_measurable_add_measure_iff : ae_measurable f (μ + ν) ↔ ae_measurable f μ ∧ ae_measurable f ν := ⟨λ h, ⟨h.mono_measure (measure.le_add_right (le_refl _)), h.mono_measure (measure.le_add_left (le_refl _))⟩, λ h, h.1.add_measure h.2⟩ @[simp, to_additive] lemma ae_measurable_one [has_one β] : ae_measurable (λ a : α, (1 : β)) μ := measurable_one.ae_measurable @[simp] lemma ae_measurable_smul_measure_iff {c : ℝ≥0∞} (hc : c ≠ 0) : ae_measurable f (c • μ) ↔ ae_measurable f μ := ⟨λ h, ⟨h.mk f, h.measurable_mk, (ae_smul_measure_iff hc).1 h.ae_eq_mk⟩, λ h, ⟨h.mk f, h.measurable_mk, (ae_smul_measure_iff hc).2 h.ae_eq_mk⟩⟩ lemma ae_measurable_of_ae_measurable_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → β} (hf : ae_measurable f (μ.trim hm)) : ae_measurable f μ := ⟨hf.mk f, measurable.mono hf.measurable_mk hm le_rfl, ae_eq_of_ae_eq_trim hf.ae_eq_mk⟩ lemma ae_measurable_restrict_of_measurable_subtype {s : set α} (hs : measurable_set s) (hf : measurable (λ x : s, f x)) : ae_measurable f (μ.restrict s) := begin by_cases h : nonempty β, { refine ⟨s.piecewise f (λ x, classical.choice h), _, (ae_restrict_iff' hs).mpr $ ae_of_all _ (λ x hx, (piecewise_eq_of_mem s _ _ hx).symm)⟩, intros t ht, rw piecewise_preimage, refine measurable_set.union _ ((measurable_const ht).diff hs), rw [← subtype.image_preimage_coe, ← preimage_comp], exact hs.subtype_image (hf ht) }, { exact (measurable_of_not_nonempty (mt (nonempty.map f) h) f).ae_measurable } end end namespace is_compact variables [topological_space α] [measurable_space α] {μ : measure α} {s : set α} lemma finite_measure_of_nhds_within (hs : is_compact s) : (∀ a ∈ s, μ.finite_at_filter (𝓝[s] a)) → μ s < ∞ := by simpa only [← measure.compl_mem_cofinite, measure.finite_at_filter] using hs.compl_mem_sets_of_nhds_within lemma finite_measure [locally_finite_measure μ] (hs : is_compact s) : μ s < ∞ := hs.finite_measure_of_nhds_within $ λ a ha, μ.finite_at_nhds_within _ _ lemma measure_zero_of_nhds_within (hs : is_compact s) : (∀ a ∈ s, ∃ t ∈ 𝓝[s] a, μ t = 0) → μ s = 0 := by simpa only [← compl_mem_ae_iff] using hs.compl_mem_sets_of_nhds_within end is_compact lemma metric.bounded.finite_measure [metric_space α] [proper_space α] [measurable_space α] {μ : measure α} [locally_finite_measure μ] {s : set α} (hs : metric.bounded s) : μ s < ∞ := (measure_mono subset_closure).trans_lt (metric.compact_iff_closed_bounded.2 ⟨is_closed_closure, metric.bounded_closure_of_bounded hs⟩).finite_measure section piecewise variables [measurable_space α] {μ : measure α} {s t : set α} {f g : α → β} lemma piecewise_ae_eq_restrict (hs : measurable_set s) : piecewise s f g =ᵐ[μ.restrict s] f := begin rw [ae_restrict_eq hs], exact (piecewise_eq_on s f g).eventually_eq.filter_mono inf_le_right end lemma piecewise_ae_eq_restrict_compl (hs : measurable_set s) : piecewise s f g =ᵐ[μ.restrict sᶜ] g := begin rw [ae_restrict_eq hs.compl], exact (piecewise_eq_on_compl s f g).eventually_eq.filter_mono inf_le_right end lemma piecewise_ae_eq_of_ae_eq_set (hst : s =ᵐ[μ] t) : s.piecewise f g =ᵐ[μ] t.piecewise f g := begin filter_upwards [hst], intros x hx, replace hx : x ∈ s ↔ x ∈ t := iff_of_eq hx, by_cases h : x ∈ s; have h' := h; rw hx at h'; simp [h, h'] end end piecewise section indicator_function variables [measurable_space α] {μ : measure α} {s t : set α} {f : α → β} lemma ae_measurable.restrict [measurable_space β] (hfm : ae_measurable f μ) {s} : ae_measurable f (μ.restrict s) := ⟨ae_measurable.mk f hfm, hfm.measurable_mk, ae_restrict_of_ae hfm.ae_eq_mk⟩ variables [has_zero β] lemma indicator_ae_eq_restrict (hs : measurable_set s) : indicator s f =ᵐ[μ.restrict s] f := piecewise_ae_eq_restrict hs lemma indicator_ae_eq_restrict_compl (hs : measurable_set s) : indicator s f =ᵐ[μ.restrict sᶜ] 0 := piecewise_ae_eq_restrict_compl hs lemma indicator_ae_eq_of_ae_eq_set (hst : s =ᵐ[μ] t) : s.indicator f =ᵐ[μ] t.indicator f := piecewise_ae_eq_of_ae_eq_set hst variables [measurable_space β] lemma ae_measurable_indicator_iff {s} (hs : measurable_set s) : ae_measurable (indicator s f) μ ↔ ae_measurable f (μ.restrict s) := begin split, { assume h, exact (h.mono_measure measure.restrict_le_self).congr (indicator_ae_eq_restrict hs) }, { assume h, refine ⟨indicator s (h.mk f), h.measurable_mk.indicator hs, _⟩, have A : s.indicator f =ᵐ[μ.restrict s] s.indicator (ae_measurable.mk f h) := (indicator_ae_eq_restrict hs).trans (h.ae_eq_mk.trans $ (indicator_ae_eq_restrict hs).symm), have B : s.indicator f =ᵐ[μ.restrict sᶜ] s.indicator (ae_measurable.mk f h) := (indicator_ae_eq_restrict_compl hs).trans (indicator_ae_eq_restrict_compl hs).symm, have : s.indicator f =ᵐ[μ.restrict s + μ.restrict sᶜ] s.indicator (ae_measurable.mk f h) := ae_add_measure_iff.2 ⟨A, B⟩, simpa only [hs, measure.restrict_add_restrict_compl] using this }, end lemma ae_measurable.indicator (hfm : ae_measurable f μ) {s} (hs : measurable_set s) : ae_measurable (s.indicator f) μ := (ae_measurable_indicator_iff hs).mpr hfm.restrict end indicator_function
cadca7923c790ef263df4e10be9bdecf41a27c9e
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/analysis/box_integral/basic.lean
971bd2284073cbcaa632ffce2a5fa46ecf1ee7d6
[ "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
43,292
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.box_integral.partition.filter import analysis.box_integral.partition.measure import topology.uniform_space.compact /-! # Integrals of Riemann, Henstock-Kurzweil, and McShane In this file we define the integral of a function over a box in `ℝⁿ. The same definition works for Riemann, Henstock-Kurzweil, and McShane integrals. As usual, we represent `ℝⁿ` as the type of functions `ι → ℝ` for some finite type `ι`. A rectangular box `(l, u]` in `ℝⁿ` is defined to be the set `{x : ι → ℝ | ∀ i, l i < x i ∧ x i ≤ u i}`, see `box_integral.box`. Let `vol` be a box-additive function on boxes in `ℝⁿ` with codomain `E →L[ℝ] F`. Given a function `f : ℝⁿ → E`, a box `I` and a tagged partition `π` of this box, the *integral sum* of `f` over `π` with respect to the volume `vol` is the sum of `vol J (f (π.tag J))` over all boxes of `π`. Here `π.tag J` is the point (tag) in `ℝⁿ` associated with the box `J`. The integral is defined as the limit of integral sums along a filter. Different filters correspond to different integration theories. In order to avoid code duplication, all our definitions and theorems take an argument `l : box_integral.integration_params`. This is a type that holds three boolean values, and encodes eight filters including those corresponding to Riemann, Henstock-Kurzweil, and McShane integrals. Following the design of infinite sums (see `has_sum` and `tsum`), we define a predicate `box_integral.has_integral` and a function `box_integral.integral` that returns a vector satisfying the predicate or zero if the function is not integrable. Then we prove some basic properties of box integrals (linearity, a formula for the integral of a constant). We also prove a version of the Henstock-Sacks inequality (see `box_integral.integrable.dist_integral_sum_le_of_mem_base_set` and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq`), prove integrability of continuous functions, and provide a criterion for integrability w.r.t. a non-Riemann filter (e.g., Henstock-Kurzweil and McShane). ## Notation - `ℝⁿ`: local notation for `ι → ℝ` ## Tags integral -/ open_locale big_operators classical topological_space nnreal filter uniformity box_integral open set finset function filter metric box_integral.integration_params noncomputable theory namespace box_integral universes u v w variables {ι : Type u} {E : Type v} {F : Type w} [normed_add_comm_group E] [normed_space ℝ E] [normed_add_comm_group F] [normed_space ℝ F] {I J : box ι} {π : tagged_prepartition I} open tagged_prepartition local notation `ℝⁿ` := ι → ℝ /-! ### Integral sum and its basic properties -/ /-- The integral sum of `f : ℝⁿ → E` over a tagged prepartition `π` w.r.t. box-additive volume `vol` with codomain `E →L[ℝ] F` is the sum of `vol J (f (π.tag J))` over all boxes of `π`. -/ def integral_sum (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : F := ∑ J in π.boxes, vol J (f (π.tag J)) lemma integral_sum_bUnion_tagged (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : prepartition I) (πi : Π J, tagged_prepartition J) : integral_sum f vol (π.bUnion_tagged πi) = ∑ J in π.boxes, integral_sum f vol (πi J) := begin refine (π.sum_bUnion_boxes _ _).trans (sum_congr rfl $ λ J hJ, sum_congr rfl $ λ J' hJ', _), rw π.tag_bUnion_tagged hJ hJ' end lemma integral_sum_bUnion_partition (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) (πi : Π J, prepartition J) (hπi : ∀ J ∈ π, (πi J).is_partition) : integral_sum f vol (π.bUnion_prepartition πi) = integral_sum f vol π := begin refine (π.to_prepartition.sum_bUnion_boxes _ _).trans (sum_congr rfl $ λ J hJ, _), calc ∑ J' in (πi J).boxes, vol J' (f (π.tag $ π.to_prepartition.bUnion_index πi J')) = ∑ J' in (πi J).boxes, vol J' (f (π.tag J)) : sum_congr rfl (λ J' hJ', by rw [prepartition.bUnion_index_of_mem _ hJ hJ']) ... = vol J (f (π.tag J)) : (vol.map ⟨λ g : E →L[ℝ] F, g (f (π.tag J)), rfl, λ _ _, rfl⟩).sum_partition_boxes le_top (hπi J hJ) end lemma integral_sum_inf_partition (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) {π' : prepartition I} (h : π'.is_partition) : integral_sum f vol (π.inf_prepartition π') = integral_sum f vol π := integral_sum_bUnion_partition f vol π _ $ λ J hJ, h.restrict (prepartition.le_of_mem _ hJ) lemma integral_sum_fiberwise {α} (g : box ι → α) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : ∑ y in π.boxes.image g, integral_sum f vol (π.filter (λ x, g x = y)) = integral_sum f vol π := π.to_prepartition.sum_fiberwise g (λ J, vol J (f $ π.tag J)) lemma integral_sum_sub_partitions (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) {π₁ π₂ : tagged_prepartition I} (h₁ : π₁.is_partition) (h₂ : π₂.is_partition) : integral_sum f vol π₁ - integral_sum f vol π₂ = ∑ J in (π₁.to_prepartition ⊓ π₂.to_prepartition).boxes, (vol J (f $ (π₁.inf_prepartition π₂.to_prepartition).tag J) - vol J (f $ (π₂.inf_prepartition π₁.to_prepartition).tag J)) := begin rw [← integral_sum_inf_partition f vol π₁ h₂, ← integral_sum_inf_partition f vol π₂ h₁, integral_sum, integral_sum, finset.sum_sub_distrib], simp only [inf_prepartition_to_prepartition, _root_.inf_comm] end @[simp] lemma integral_sum_disj_union (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) {π₁ π₂ : tagged_prepartition I} (h : disjoint π₁.Union π₂.Union) : integral_sum f vol (π₁.disj_union π₂ h) = integral_sum f vol π₁ + integral_sum f vol π₂ := begin refine (prepartition.sum_disj_union_boxes h _).trans (congr_arg2 (+) (sum_congr rfl $ λ J hJ, _) (sum_congr rfl $ λ J hJ, _)), { rw disj_union_tag_of_mem_left _ hJ }, { rw disj_union_tag_of_mem_right _ hJ } end @[simp] lemma integral_sum_add (f g : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (f + g) vol π = integral_sum f vol π + integral_sum g vol π := by simp only [integral_sum, pi.add_apply, (vol _).map_add, finset.sum_add_distrib] @[simp] lemma integral_sum_neg (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (-f) vol π = -integral_sum f vol π := by simp only [integral_sum, pi.neg_apply, (vol _).map_neg, finset.sum_neg_distrib] @[simp] lemma integral_sum_smul (c : ℝ) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (π : tagged_prepartition I) : integral_sum (c • f) vol π = c • integral_sum f vol π := by simp only [integral_sum, finset.smul_sum, pi.smul_apply, continuous_linear_map.map_smul] variables [fintype ι] /-! ### Basic integrability theory -/ /-- The predicate `has_integral I l f vol y` says that `y` is the integral of `f` over `I` along `l` w.r.t. volume `vol`. This means that integral sums of `f` tend to `𝓝 y` along `box_integral.integration_params.to_filter_Union I ⊤`. -/ def has_integral (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) (y : F) : Prop := tendsto (integral_sum f vol) (l.to_filter_Union I ⊤) (𝓝 y) /-- A function is integrable if there exists a vector that satisfies the `has_integral` predicate. -/ def integrable (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) := ∃ y, has_integral I l f vol y /-- The integral of a function `f` over a box `I` along a filter `l` w.r.t. a volume `vol`. Returns zero on non-integrable functions. -/ def integral (I : box ι) (l : integration_params) (f : ℝⁿ → E) (vol : ι →ᵇᵃ (E →L[ℝ] F)) := if h : integrable I l f vol then h.some else 0 variables {l : integration_params} {f g : ℝⁿ → E} {vol : ι →ᵇᵃ (E →L[ℝ] F)} {y y' : F} /-- Reinterpret `box_integral.has_integral` as `filter.tendsto`, e.g., dot-notation theorems that are shadowed in the `box_integral.has_integral` namespace. -/ lemma has_integral.tendsto (h : has_integral I l f vol y) : tendsto (integral_sum f vol) (l.to_filter_Union I ⊤) (𝓝 y) := h /-- The `ε`-`δ` definition of `box_integral.has_integral`. -/ lemma has_integral_iff : has_integral I l f vol y ↔ ∀ ε > (0 : ℝ), ∃ r : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c π, l.mem_base_set I c (r c) π → is_partition π → dist (integral_sum f vol π) y ≤ ε := ((l.has_basis_to_filter_Union_top I).tendsto_iff nhds_basis_closed_ball).trans $ by simp [@forall_swap ℝ≥0 (tagged_prepartition I)] /-- Quite often it is more natural to prove an estimate of the form `a * ε`, not `ε` in the RHS of `box_integral.has_integral_iff`, so we provide this auxiliary lemma. -/ lemma has_integral_of_mul (a : ℝ) (h : ∀ ε : ℝ, 0 < ε → ∃ r: ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c π, l.mem_base_set I c (r c) π → is_partition π → dist (integral_sum f vol π) y ≤ a * ε) : has_integral I l f vol y := begin refine has_integral_iff.2 (λ ε hε, _), rcases exists_pos_mul_lt hε a with ⟨ε', hε', ha⟩, rcases h ε' hε' with ⟨r, hr, H⟩, exact ⟨r, hr, λ c π hπ hπp, (H c π hπ hπp).trans ha.le⟩ end lemma integrable_iff_cauchy [complete_space F] : integrable I l f vol ↔ cauchy ((l.to_filter_Union I ⊤).map (integral_sum f vol)) := cauchy_map_iff_exists_tendsto.symm /-- In a complete space, a function is integrable if and only if its integral sums form a Cauchy net. Here we restate this fact in terms of `∀ ε > 0, ∃ r, ...`. -/ lemma integrable_iff_cauchy_basis [complete_space F] : integrable I l f vol ↔ ∀ ε > (0 : ℝ), ∃ r : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ), (∀ c, l.r_cond (r c)) ∧ ∀ c₁ c₂ π₁ π₂, l.mem_base_set I c₁ (r c₁) π₁ → π₁.is_partition → l.mem_base_set I c₂ (r c₂) π₂ → π₂.is_partition → dist (integral_sum f vol π₁) (integral_sum f vol π₂) ≤ ε := begin rw [integrable_iff_cauchy, cauchy_map_iff', (l.has_basis_to_filter_Union_top _).prod_self.tendsto_iff uniformity_basis_dist_le], refine forall₂_congr (λ ε ε0, exists_congr $ λ r, _), simp only [exists_prop, prod.forall, set.mem_Union, exists_imp_distrib, prod_mk_mem_set_prod_eq, and_imp, mem_inter_iff, mem_set_of_eq], exact and_congr iff.rfl ⟨λ H c₁ c₂ π₁ π₂ h₁ hU₁ h₂ hU₂, H π₁ π₂ c₁ h₁ hU₁ c₂ h₂ hU₂, λ H π₁ π₂ c₁ h₁ hU₁ c₂ h₂ hU₂, H c₁ c₂ π₁ π₂ h₁ hU₁ h₂ hU₂⟩ end lemma has_integral.mono {l₁ l₂ : integration_params} (h : has_integral I l₁ f vol y) (hl : l₂ ≤ l₁) : has_integral I l₂ f vol y := h.mono_left $ integration_params.to_filter_Union_mono _ hl _ protected lemma integrable.has_integral (h : integrable I l f vol) : has_integral I l f vol (integral I l f vol) := by { rw [integral, dif_pos h], exact classical.some_spec h } lemma integrable.mono {l'} (h : integrable I l f vol) (hle : l' ≤ l) : integrable I l' f vol := ⟨_, h.has_integral.mono hle⟩ lemma has_integral.unique (h : has_integral I l f vol y) (h' : has_integral I l f vol y') : y = y' := tendsto_nhds_unique h h' lemma has_integral.integrable (h : has_integral I l f vol y) : integrable I l f vol := ⟨_, h⟩ lemma has_integral.integral_eq (h : has_integral I l f vol y) : integral I l f vol = y := h.integrable.has_integral.unique h lemma has_integral.add (h : has_integral I l f vol y) (h' : has_integral I l g vol y') : has_integral I l (f + g) vol (y + y') := by simpa only [has_integral, ← integral_sum_add] using h.add h' lemma integrable.add (hf : integrable I l f vol) (hg : integrable I l g vol) : integrable I l (f + g) vol := (hf.has_integral.add hg.has_integral).integrable lemma integral_add (hf : integrable I l f vol) (hg : integrable I l g vol) : integral I l (f + g) vol = integral I l f vol + integral I l g vol := (hf.has_integral.add hg.has_integral).integral_eq lemma has_integral.neg (hf : has_integral I l f vol y) : has_integral I l (-f) vol (-y) := by simpa only [has_integral, ← integral_sum_neg] using hf.neg lemma integrable.neg (hf : integrable I l f vol) : integrable I l (-f) vol := hf.has_integral.neg.integrable lemma integrable.of_neg (hf : integrable I l (-f) vol) : integrable I l f vol := neg_neg f ▸ hf.neg @[simp] lemma integrable_neg : integrable I l (-f) vol ↔ integrable I l f vol := ⟨λ h, h.of_neg, λ h, h.neg⟩ @[simp] lemma integral_neg : integral I l (-f) vol = -integral I l f vol := if h : integrable I l f vol then h.has_integral.neg.integral_eq else by rw [integral, integral, dif_neg h, dif_neg (mt integrable.of_neg h), neg_zero] lemma has_integral.sub (h : has_integral I l f vol y) (h' : has_integral I l g vol y') : has_integral I l (f - g) vol (y - y') := by simpa only [sub_eq_add_neg] using h.add h'.neg lemma integrable.sub (hf : integrable I l f vol) (hg : integrable I l g vol) : integrable I l (f - g) vol := (hf.has_integral.sub hg.has_integral).integrable lemma integral_sub (hf : integrable I l f vol) (hg : integrable I l g vol) : integral I l (f - g) vol = integral I l f vol - integral I l g vol := (hf.has_integral.sub hg.has_integral).integral_eq lemma has_integral_const (c : E) : has_integral I l (λ _, c) vol (vol I c) := tendsto_const_nhds.congr' $ (l.eventually_is_partition I).mono $ λ π hπ, ((vol.map ⟨λ g : E →L[ℝ] F, g c, rfl, λ _ _, rfl⟩).sum_partition_boxes le_top hπ).symm @[simp] lemma integral_const (c : E) : integral I l (λ _, c) vol = vol I c := (has_integral_const c).integral_eq lemma integrable_const (c : E) : integrable I l (λ _, c) vol := ⟨_, has_integral_const c⟩ lemma has_integral_zero : has_integral I l (λ _, (0:E)) vol 0 := by simpa only [← (vol I).map_zero] using has_integral_const (0 : E) lemma integrable_zero : integrable I l (λ _, (0:E)) vol := ⟨0, has_integral_zero⟩ lemma integral_zero : integral I l (λ _, (0:E)) vol = 0 := has_integral_zero.integral_eq lemma has_integral_sum {α : Type*} {s : finset α} {f : α → ℝⁿ → E} {g : α → F} (h : ∀ i ∈ s, has_integral I l (f i) vol (g i)) : has_integral I l (λ x, ∑ i in s, f i x) vol (∑ i in s, g i) := begin induction s using finset.induction_on with a s ha ihs, { simp [has_integral_zero] }, simp only [finset.sum_insert ha], rw finset.forall_mem_insert at h, exact h.1.add (ihs h.2) end lemma has_integral.smul (hf : has_integral I l f vol y) (c : ℝ) : has_integral I l (c • f) vol (c • y) := by simpa only [has_integral, ← integral_sum_smul] using (tendsto_const_nhds : tendsto _ _ (𝓝 c)).smul hf lemma integrable.smul (hf : integrable I l f vol) (c : ℝ) : integrable I l (c • f) vol := (hf.has_integral.smul c).integrable lemma integrable.of_smul {c : ℝ} (hf : integrable I l (c • f) vol) (hc : c ≠ 0) : integrable I l f vol := by { convert hf.smul c⁻¹, ext x, simp only [pi.smul_apply, inv_smul_smul₀ hc] } @[simp] lemma integral_smul (c : ℝ) : integral I l (λ x, c • f x) vol = c • integral I l f vol := begin rcases eq_or_ne c 0 with rfl | hc, { simp only [zero_smul, integral_zero] }, by_cases hf : integrable I l f vol, { exact (hf.has_integral.smul c).integral_eq }, { have : ¬integrable I l (λ x, c • f x) vol, from mt (λ h, h.of_smul hc) hf, rw [integral, integral, dif_neg hf, dif_neg this, smul_zero] } end open measure_theory /-- The integral of a nonnegative function w.r.t. a volume generated by a locally-finite measure is nonnegative. -/ lemma integral_nonneg {g : ℝⁿ → ℝ} (hg : ∀ x ∈ I.Icc, 0 ≤ g x) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : 0 ≤ integral I l g μ.to_box_additive.to_smul := begin by_cases hgi : integrable I l g μ.to_box_additive.to_smul, { refine ge_of_tendsto' hgi.has_integral (λ π, sum_nonneg $ λ J hJ, _), exact mul_nonneg ennreal.to_real_nonneg (hg _ $ π.tag_mem_Icc _) }, { rw [integral, dif_neg hgi] } end /-- If `‖f x‖ ≤ g x` on `[l, u]` and `g` is integrable, then the norm of the integral of `f` is less than or equal to the integral of `g`. -/ lemma norm_integral_le_of_norm_le {g : ℝⁿ → ℝ} (hle : ∀ x ∈ I.Icc, ‖f x‖ ≤ g x) (μ : measure ℝⁿ) [is_locally_finite_measure μ] (hg : integrable I l g μ.to_box_additive.to_smul) : ‖(integral I l f μ.to_box_additive.to_smul : E)‖ ≤ integral I l g μ.to_box_additive.to_smul := begin by_cases hfi : integrable.{u v v} I l f μ.to_box_additive.to_smul, { refine le_of_tendsto_of_tendsto' hfi.has_integral.norm hg.has_integral (λ π, _), refine norm_sum_le_of_le _ (λ J hJ, _), simp only [box_additive_map.to_smul_apply, norm_smul, smul_eq_mul, real.norm_eq_abs, μ.to_box_additive_apply, abs_of_nonneg ennreal.to_real_nonneg], exact mul_le_mul_of_nonneg_left (hle _ $ π.tag_mem_Icc _) ennreal.to_real_nonneg }, { rw [integral, dif_neg hfi, norm_zero], exact integral_nonneg (λ x hx, (norm_nonneg _).trans (hle x hx)) μ } end lemma norm_integral_le_of_le_const {c : ℝ} (hc : ∀ x ∈ I.Icc, ‖f x‖ ≤ c) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : ‖(integral I l f μ.to_box_additive.to_smul : E)‖ ≤ (μ I).to_real * c := by simpa only [integral_const] using norm_integral_le_of_norm_le hc μ (integrable_const c) /-! # Henstock-Sacks inequality and integrability on subboxes Henstock-Sacks inequality for Henstock-Kurzweil integral says the following. Let `f` be a function integrable on a box `I`; let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged partition of `I` subordinate to `r`, the integral sum over this partition is `ε`-close to the integral. Then for any tagged prepartition (i.e. a finite collections of pairwise disjoint subboxes of `I` with tagged points) `π`, the integral sum over `π` differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement in the library is a bit more complicated to make it work for any `box_integral.integration_params`. We formalize several versions of this inequality in `box_integral.integrable.dist_integral_sum_le_of_mem_base_set`, `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq`, and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set`. Instead of using predicate assumptions on `r`, we define `box_integral.integrable.convergence_r (h : integrable I l f vol) (ε : ℝ) (c : ℝ≥0) : ℝⁿ → (0, ∞)` to be a function `r` such that - if `l.bRiemann`, then `r` is a constant; - if `ε > 0`, then for any tagged partition `π` of `I` subordinate to `r` (more precisely, satisfying the predicate `l.mem_base_set I c r`), the integral sum of `f` over `π` differs from the integral of `f` over `I` by at most `ε`. The proof is mostly based on [Russel A. Gordon, *The integrals of Lebesgue, Denjoy, Perron, and Henstock*][Gordon55]. -/ namespace integrable /-- If `ε > 0`, then `box_integral.integrable.convergence_r` is a function `r : ℝ≥0 → ℝⁿ → (0, ∞)` such that for every `c : ℝ≥0`, for every tagged partition `π` subordinate to `r` (and satisfying additional distortion estimates if `box_integral.integration_params.bDistortion l = tt`), the corresponding integral sum is `ε`-close to the integral. If `box.integral.integration_params.bRiemann = tt`, then `r c x` does not depend on `x`. If `ε ≤ 0`, then we use `r c x = 1`. -/ def convergence_r (h : integrable I l f vol) (ε : ℝ) : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ) := if hε : 0 < ε then (has_integral_iff.1 h.has_integral ε hε).some else λ _ _, ⟨1, set.mem_Ioi.2 zero_lt_one⟩ variables {c c₁ c₂ : ℝ≥0} {ε ε₁ ε₂ : ℝ} {π₁ π₂ : tagged_prepartition I} lemma convergence_r_cond (h : integrable I l f vol) (ε : ℝ) (c : ℝ≥0) : l.r_cond (h.convergence_r ε c) := begin rw convergence_r, split_ifs with h₀, exacts [(has_integral_iff.1 h.has_integral ε h₀).some_spec.1 _, λ _ x, rfl] end lemma dist_integral_sum_integral_le_of_mem_base_set (h : integrable I l f vol) (h₀ : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) (hπp : π.is_partition) : dist (integral_sum f vol π) (integral I l f vol) ≤ ε := begin rw [convergence_r, dif_pos h₀] at hπ, exact (has_integral_iff.1 h.has_integral ε h₀).some_spec.2 c _ hπ hπp end /-- **Henstock-Sacks inequality**. Let `r₁ r₂ : ℝⁿ → (0, ∞)` be function such that for any tagged *partition* of `I` subordinate to `rₖ`, `k=1,2`, the integral sum of `f` over this partition differs from the integral of `f` by at most `εₖ`. Then for any two tagged *prepartition* `π₁ π₂` subordinate to `r₁` and `r₂` respectively and covering the same part of `I`, the integral sums of `f` over these prepartitions differ from each other by at most `ε₁ + ε₂`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion. See also `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq` and `box_integral.integrable.dist_integral_sum_sum_integral_le_of_mem_base_set`. -/ lemma dist_integral_sum_le_of_mem_base_set (h : integrable I l f vol) (hpos₁ : 0 < ε₁) (hpos₂ : 0 < ε₂) (h₁ : l.mem_base_set I c₁ (h.convergence_r ε₁ c₁) π₁) (h₂ : l.mem_base_set I c₂ (h.convergence_r ε₂ c₂) π₂) (HU : π₁.Union = π₂.Union) : dist (integral_sum f vol π₁) (integral_sum f vol π₂) ≤ ε₁ + ε₂ := begin rcases h₁.exists_common_compl h₂ HU with ⟨π, hπU, hπc₁, hπc₂⟩, set r : ℝⁿ → Ioi (0 : ℝ) := λ x, min (h.convergence_r ε₁ c₁ x) (h.convergence_r ε₂ c₂ x), have hr : l.r_cond r := (h.convergence_r_cond _ c₁).min (h.convergence_r_cond _ c₂), set πr := π.to_subordinate r, have H₁ : dist (integral_sum f vol (π₁.union_compl_to_subordinate π hπU r)) (integral I l f vol) ≤ ε₁, from h.dist_integral_sum_integral_le_of_mem_base_set hpos₁ (h₁.union_compl_to_subordinate (λ _ _, min_le_left _ _) hπU hπc₁) (is_partition_union_compl_to_subordinate _ _ _ _), rw HU at hπU, have H₂ : dist (integral_sum f vol (π₂.union_compl_to_subordinate π hπU r)) (integral I l f vol) ≤ ε₂, from h.dist_integral_sum_integral_le_of_mem_base_set hpos₂ (h₂.union_compl_to_subordinate (λ _ _, min_le_right _ _) hπU hπc₂) (is_partition_union_compl_to_subordinate _ _ _ _), simpa [union_compl_to_subordinate] using (dist_triangle_right _ _ _).trans (add_le_add H₁ H₂) end /-- If `f` is integrable on `I` along `l`, then for two sufficiently fine tagged prepartitions (in the sense of the filter `box_integral.integration_params.to_filter l I`) such that they cover the same part of `I`, the integral sums of `f` over `π₁` and `π₂` are very close to each other. -/ lemma tendsto_integral_sum_to_filter_prod_self_inf_Union_eq_uniformity (h : integrable I l f vol) : tendsto (λ π : tagged_prepartition I × tagged_prepartition I, (integral_sum f vol π.1, integral_sum f vol π.2)) ((l.to_filter I ×ᶠ l.to_filter I) ⊓ 𝓟 {π | π.1.Union = π.2.Union}) (𝓤 F) := begin refine (((l.has_basis_to_filter I).prod_self.inf_principal _).tendsto_iff uniformity_basis_dist_le).2 (λ ε ε0, _), replace ε0 := half_pos ε0, use [h.convergence_r (ε / 2), h.convergence_r_cond (ε / 2)], rintro ⟨π₁, π₂⟩ ⟨⟨h₁, h₂⟩, hU⟩, rw ← add_halves ε, exact h.dist_integral_sum_le_of_mem_base_set ε0 ε0 h₁.some_spec h₂.some_spec hU end /-- If `f` is integrable on a box `I` along `l`, then for any fixed subset `s` of `I` that can be represented as a finite union of boxes, the integral sums of `f` over tagged prepartitions that cover exactly `s` form a Cauchy “sequence” along `l`. -/ lemma cauchy_map_integral_sum_to_filter_Union (h : integrable I l f vol) (π₀ : prepartition I) : cauchy ((l.to_filter_Union I π₀).map (integral_sum f vol)) := begin refine ⟨infer_instance, _⟩, rw [prod_map_map_eq, ← to_filter_inf_Union_eq, ← prod_inf_prod, prod_principal_principal], exact h.tendsto_integral_sum_to_filter_prod_self_inf_Union_eq_uniformity.mono_left (inf_le_inf_left _ $ principal_mono.2 $ λ π h, h.1.trans h.2.symm) end variable [complete_space F] lemma to_subbox_aux (h : integrable I l f vol) (hJ : J ≤ I) : ∃ y : F, has_integral J l f vol y ∧ tendsto (integral_sum f vol) (l.to_filter_Union I (prepartition.single I J hJ)) (𝓝 y) := begin refine (cauchy_map_iff_exists_tendsto.1 (h.cauchy_map_integral_sum_to_filter_Union (prepartition.single I J hJ))).imp (λ y hy, ⟨_, hy⟩), convert hy.comp (l.tendsto_embed_box_to_filter_Union_top hJ) -- faster than `exact` here end /-- If `f` is integrable on a box `I`, then it is integrable on any subbox of `I`. -/ lemma to_subbox (h : integrable I l f vol) (hJ : J ≤ I) : integrable J l f vol := (h.to_subbox_aux hJ).imp $ λ y, and.left /-- If `f` is integrable on a box `I`, then integral sums of `f` over tagged prepartitions that cover exactly a subbox `J ≤ I` tend to the integral of `f` over `J` along `l`. -/ lemma tendsto_integral_sum_to_filter_Union_single (h : integrable I l f vol) (hJ : J ≤ I) : tendsto (integral_sum f vol) (l.to_filter_Union I (prepartition.single I J hJ)) (𝓝 $ integral J l f vol) := let ⟨y, h₁, h₂⟩ := h.to_subbox_aux hJ in h₁.integral_eq.symm ▸ h₂ /-- **Henstock-Sacks inequality**. Let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged *partition* of `I` subordinate to `r`, the integral sum of `f` over this partition differs from the integral of `f` by at most `ε`. Then for any tagged *prepartition* `π` subordinate to `r`, the integral sum of `f` over this prepartition differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion; - takes an extra argument `π₀ : prepartition I` and an assumption `π.Union = π₀.Union` instead of using `π.to_prepartition`. -/ lemma dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq (h : integrable I l f vol) (h0 : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) {π₀ : prepartition I} (hU : π.Union = π₀.Union) : dist (integral_sum f vol π) (∑ J in π₀.boxes, integral J l f vol) ≤ ε := begin /- Let us prove that the distance is less than or equal to `ε + δ` for all positive `δ`. -/ refine le_of_forall_pos_le_add (λ δ δ0, _), /- First we choose some constants. -/ set δ' : ℝ := δ / (π₀.boxes.card + 1), have H0 : 0 < (π₀.boxes.card + 1 : ℝ) := nat.cast_add_one_pos _, have δ'0 : 0 < δ' := div_pos δ0 H0, set C := max π₀.distortion π₀.compl.distortion, /- Next we choose a tagged partition of each `J ∈ π₀` such that the integral sum of `f` over this partition is `δ'`-close to the integral of `f` over `J`. -/ have : ∀ J ∈ π₀, ∃ πi : tagged_prepartition J, πi.is_partition ∧ dist (integral_sum f vol πi) (integral J l f vol) ≤ δ' ∧ l.mem_base_set J C (h.convergence_r δ' C) πi, { intros J hJ, have Hle : J ≤ I := π₀.le_of_mem hJ, have HJi : integrable J l f vol := h.to_subbox Hle, set r := λ x, min (h.convergence_r δ' C x) (HJi.convergence_r δ' C x), have hr : l.r_cond r, from (h.convergence_r_cond _ C).min (HJi.convergence_r_cond _ C), have hJd : J.distortion ≤ C, from le_trans (finset.le_sup hJ) (le_max_left _ _), rcases l.exists_mem_base_set_is_partition J hJd r with ⟨πJ, hC, hp⟩, have hC₁ : l.mem_base_set J C (HJi.convergence_r δ' C) πJ, { refine hC.mono J le_rfl le_rfl (λ x hx, _), exact min_le_right _ _ }, have hC₂ : l.mem_base_set J C (h.convergence_r δ' C) πJ, { refine hC.mono J le_rfl le_rfl (λ x hx, _), exact min_le_left _ _ }, exact ⟨πJ, hp, HJi.dist_integral_sum_integral_le_of_mem_base_set δ'0 hC₁ hp, hC₂⟩ }, /- Now we combine these tagged partitions into a tagged prepartition of `I` that covers the same part of `I` as `π₀` and apply `box_integral.dist_integral_sum_le_of_mem_base_set` to `π` and this prepartition. -/ choose! πi hπip hπiδ' hπiC, have : l.mem_base_set I C (h.convergence_r δ' C) (π₀.bUnion_tagged πi), from bUnion_tagged_mem_base_set hπiC hπip (λ _, le_max_right _ _), have hU' : π.Union = (π₀.bUnion_tagged πi).Union, from hU.trans (prepartition.Union_bUnion_partition _ hπip).symm, have := h.dist_integral_sum_le_of_mem_base_set h0 δ'0 hπ this hU', rw integral_sum_bUnion_tagged at this, calc dist (integral_sum f vol π) (∑ J in π₀.boxes, integral J l f vol) ≤ dist (integral_sum f vol π) (∑ J in π₀.boxes, integral_sum f vol (πi J)) + dist (∑ J in π₀.boxes, integral_sum f vol (πi J)) (∑ J in π₀.boxes, integral J l f vol) : dist_triangle _ _ _ ... ≤ (ε + δ') + ∑ J in π₀.boxes, δ' : add_le_add this (dist_sum_sum_le_of_le _ hπiδ') ... = ε + δ : by { field_simp [H0.ne'], ring } end /-- **Henstock-Sacks inequality**. Let `r : ℝⁿ → (0, ∞)` be a function such that for any tagged *partition* of `I` subordinate to `r`, the integral sum of `f` over this partition differs from the integral of `f` by at most `ε`. Then for any tagged *prepartition* `π` subordinate to `r`, the integral sum of `f` over this prepartition differs from the integral of `f` over the part of `I` covered by `π` by at most `ε`. The actual statement - uses `box_integral.integrable.convergence_r` instead of a predicate assumption on `r`; - uses `box_integral.integration_params.mem_base_set` instead of “subordinate to `r`” to account for additional requirements like being a Henstock partition or having a bounded distortion; -/ lemma dist_integral_sum_sum_integral_le_of_mem_base_set (h : integrable I l f vol) (h0 : 0 < ε) (hπ : l.mem_base_set I c (h.convergence_r ε c) π) : dist (integral_sum f vol π) (∑ J in π.boxes, integral J l f vol) ≤ ε := h.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq h0 hπ rfl /-- Integral sum of `f` over a tagged prepartition `π` such that `π.Union = π₀.Union` tends to the sum of integrals of `f` over the boxes of `π₀`. -/ lemma tendsto_integral_sum_sum_integral (h : integrable I l f vol) (π₀ : prepartition I) : tendsto (integral_sum f vol) (l.to_filter_Union I π₀) (𝓝 $ ∑ J in π₀.boxes, integral J l f vol) := begin refine ((l.has_basis_to_filter_Union I π₀).tendsto_iff nhds_basis_closed_ball).2 (λ ε ε0, _), refine ⟨h.convergence_r ε, h.convergence_r_cond ε, _⟩, simp only [mem_inter_iff, set.mem_Union, mem_set_of_eq], rintro π ⟨c, hc, hU⟩, exact h.dist_integral_sum_sum_integral_le_of_mem_base_set_of_Union_eq ε0 hc hU end /-- If `f` is integrable on `I`, then `λ J, integral J l f vol` is box-additive on subboxes of `I`: if `π₁`, `π₂` are two prepartitions of `I` covering the same part of `I`, then the sum of integrals of `f` over the boxes of `π₁` is equal to the sum of integrals of `f` over the boxes of `π₂`. See also `box_integral.integrable.to_box_additive` for a bundled version. -/ lemma sum_integral_congr (h : integrable I l f vol) {π₁ π₂ : prepartition I} (hU : π₁.Union = π₂.Union) : ∑ J in π₁.boxes, integral J l f vol = ∑ J in π₂.boxes, integral J l f vol := begin refine tendsto_nhds_unique (h.tendsto_integral_sum_sum_integral π₁) _, rw l.to_filter_Union_congr _ hU, exact h.tendsto_integral_sum_sum_integral π₂ end /-- If `f` is integrable on `I`, then `λ J, integral J l f vol` is box-additive on subboxes of `I`: if `π₁`, `π₂` are two prepartitions of `I` covering the same part of `I`, then the sum of integrals of `f` over the boxes of `π₁` is equal to the sum of integrals of `f` over the boxes of `π₂`. See also `box_integral.integrable.sum_integral_congr` for an unbundled version. -/ @[simps] def to_box_additive (h : integrable I l f vol) : ι →ᵇᵃ[I] F := { to_fun := λ J, integral J l f vol, sum_partition_boxes' := λ J hJ π hπ, begin replace hπ := hπ.Union_eq, rw ← prepartition.Union_top at hπ, rw [(h.to_subbox (with_top.coe_le_coe.1 hJ)).sum_integral_congr hπ, prepartition.top_boxes, sum_singleton] end } end integrable open measure_theory /-! ### Integrability conditions -/ variable (l) /-- A continuous function is box-integrable with respect to any locally finite measure. This is true for any volume with bounded variation. -/ lemma integrable_of_continuous_on [complete_space E] {I : box ι} {f : ℝⁿ → E} (hc : continuous_on f I.Icc) (μ : measure ℝⁿ) [is_locally_finite_measure μ] : integrable.{u v v} I l f μ.to_box_additive.to_smul := begin have huc := I.is_compact_Icc.uniform_continuous_on_of_continuous hc, rw metric.uniform_continuous_on_iff_le at huc, refine integrable_iff_cauchy_basis.2 (λ ε ε0, _), rcases exists_pos_mul_lt ε0 (μ.to_box_additive I) with ⟨ε', ε0', hε⟩, rcases huc ε' ε0' with ⟨δ, δ0 : 0 < δ, Hδ⟩, refine ⟨λ _ _, ⟨δ / 2, half_pos δ0⟩, λ _ _ _, rfl, λ c₁ c₂ π₁ π₂ h₁ h₁p h₂ h₂p, _⟩, simp only [dist_eq_norm, integral_sum_sub_partitions _ _ h₁p h₂p, box_additive_map.to_smul_apply, ← smul_sub], have : ∀ J ∈ π₁.to_prepartition ⊓ π₂.to_prepartition, ‖μ.to_box_additive J • (f ((π₁.inf_prepartition π₂.to_prepartition).tag J) - f ((π₂.inf_prepartition π₁.to_prepartition).tag J))‖ ≤ μ.to_box_additive J * ε', { intros J hJ, have : 0 ≤ μ.to_box_additive J, from ennreal.to_real_nonneg, rw [norm_smul, real.norm_eq_abs, abs_of_nonneg this, ← dist_eq_norm], refine mul_le_mul_of_nonneg_left _ this, refine Hδ _ (tagged_prepartition.tag_mem_Icc _ _) _ (tagged_prepartition.tag_mem_Icc _ _) _, rw [← add_halves δ], refine (dist_triangle_left _ _ J.upper).trans (add_le_add (h₁.1 _ _ _) (h₂.1 _ _ _)), { exact prepartition.bUnion_index_mem _ hJ }, { exact box.le_iff_Icc.1 (prepartition.le_bUnion_index _ hJ) J.upper_mem_Icc }, { rw _root_.inf_comm at hJ, exact prepartition.bUnion_index_mem _ hJ }, { rw _root_.inf_comm at hJ, exact box.le_iff_Icc.1 (prepartition.le_bUnion_index _ hJ) J.upper_mem_Icc } }, refine (norm_sum_le_of_le _ this).trans _, rw [← finset.sum_mul, μ.to_box_additive.sum_partition_boxes le_top (h₁p.inf h₂p)], exact hε.le end variable {l} /-- This is an auxiliary lemma used to prove two statements at once. Use one of the next two lemmas instead. -/ lemma has_integral_of_bRiemann_eq_ff_of_forall_is_o (hl : l.bRiemann = ff) (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (s : set ℝⁿ) (hs : s.countable) (hlH : s.nonempty → l.bHenstock = tt) (H₁ : ∀ (c : ℝ≥0) (x ∈ I.Icc ∩ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε) (H₂ : ∀ (c : ℝ≥0) (x ∈ I.Icc \ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → (l.bHenstock → x ∈ J.Icc) → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I l f vol (g I) := begin /- We choose `r x` differently for `x ∈ s` and `x ∉ s`. For `x ∈ s`, we choose `εs` such that `∑' x : s, εs x < ε / 2 / 2 ^ #ι`, then choose `r x` so that `dist (vol J (f x)) (g J) ≤ εs x` for `J` in the `r x`-neighborhood of `x`. This guarantees that the sum of these distances over boxes `J` such that `π.tag J ∈ s` is less than `ε / 2`. We need an additional multiplier `2 ^ #ι` because different boxes can have the same tag. For `x ∉ s`, we choose `r x` so that `dist (vol (J (f x))) (g J) ≤ (ε / 2 / B I) * B J` for a box `J` in the `δ`-neighborhood of `x`. -/ refine ((l.has_basis_to_filter_Union_top _).tendsto_iff metric.nhds_basis_closed_ball).2 _, intros ε ε0, simp only [subtype.exists'] at H₁ H₂, choose! δ₁ Hδ₁ using H₁, choose! δ₂ Hδ₂ using H₂, have ε0' := half_pos ε0, have H0 : 0 < (2 ^ fintype.card ι : ℝ), from pow_pos zero_lt_two _, rcases hs.exists_pos_forall_sum_le (div_pos ε0' H0) with ⟨εs, hεs0, hεs⟩, simp only [le_div_iff' H0, mul_sum] at hεs, rcases exists_pos_mul_lt ε0' (B I) with ⟨ε', ε'0, hεI⟩, set δ : ℝ≥0 → ℝⁿ → Ioi (0 : ℝ) := λ c x, if x ∈ s then δ₁ c x (εs x) else (δ₂ c) x ε', refine ⟨δ, λ c, l.r_cond_of_bRiemann_eq_ff hl, _⟩, simp only [set.mem_Union, mem_inter_iff, mem_set_of_eq], rintro π ⟨c, hπδ, hπp⟩, /- Now we split the sum into two parts based on whether `π.tag J` belongs to `s` or not. -/ rw [← g.sum_partition_boxes le_rfl hπp, mem_closed_ball, integral_sum, ← sum_filter_add_sum_filter_not π.boxes (λ J, π.tag J ∈ s), ← sum_filter_add_sum_filter_not π.boxes (λ J, π.tag J ∈ s), ← add_halves ε], refine dist_add_add_le_of_le _ _, { unfreezingI { rcases s.eq_empty_or_nonempty with rfl|hsne }, { simp [ε0'.le] }, /- For the boxes such that `π.tag J ∈ s`, we use the fact that at most `2 ^ #ι` boxes have the same tag. -/ specialize hlH hsne, have : ∀ J ∈ π.boxes.filter (λ J, π.tag J ∈ s), dist (vol J (f $ π.tag J)) (g J) ≤ εs (π.tag J), { intros J hJ, rw finset.mem_filter at hJ, cases hJ with hJ hJs, refine Hδ₁ c _ ⟨π.tag_mem_Icc _, hJs⟩ _ (hεs0 _) _ (π.le_of_mem' _ hJ) _ (hπδ.2 hlH J hJ) (λ hD, (finset.le_sup hJ).trans (hπδ.3 hD)), convert hπδ.1 J hJ, exact (dif_pos hJs).symm }, refine (dist_sum_sum_le_of_le _ this).trans _, rw sum_comp, refine (sum_le_sum _).trans (hεs _ _), { rintro b -, rw [← nat.cast_two, ← nat.cast_pow, ← nsmul_eq_mul], refine nsmul_le_nsmul (hεs0 _).le _, refine (finset.card_le_of_subset _).trans ((hπδ.is_Henstock hlH).card_filter_tag_eq_le b), exact filter_subset_filter _ (filter_subset _ _) }, { rw [finset.coe_image, set.image_subset_iff], exact λ J hJ, (finset.mem_filter.1 hJ).2 } }, /- Now we deal with boxes such that `π.tag J ∉ s`. In this case the estimate is straightforward. -/ have H₂ : ∀ J ∈ π.boxes.filter (λ J, π.tag J ∉ s), dist (vol J (f $ π.tag J)) (g J) ≤ ε' * B J, { intros J hJ, rw finset.mem_filter at hJ, cases hJ with hJ hJs, refine Hδ₂ c _ ⟨π.tag_mem_Icc _, hJs⟩ _ ε'0 _ (π.le_of_mem' _ hJ) _ (λ hH, hπδ.2 hH J hJ) (λ hD, (finset.le_sup hJ).trans (hπδ.3 hD)), convert hπδ.1 J hJ, exact (dif_neg hJs).symm }, refine (dist_sum_sum_le_of_le _ H₂).trans ((sum_le_sum_of_subset_of_nonneg (filter_subset _ _) _).trans _), { exact λ _ _ _, mul_nonneg ε'0.le (hB0 _) }, { rw [← mul_sum, B.sum_partition_boxes le_rfl hπp, mul_comm], exact hεI.le } end /-- A function `f` has Henstock (or `⊥`) integral over `I` is equal to the value of a box-additive function `g` on `I` provided that `vol J (f x)` is sufficiently close to `g J` for sufficiently small boxes `J ∋ x`. This lemma is useful to prove, e.g., to prove the Divergence theorem for integral along `⊥`. Let `l` be either `box_integral.integration_params.Henstock` or `⊥`. Let `g` a box-additive function on subboxes of `I`. Suppose that there exists a nonnegative box-additive function `B` and a countable set `s` with the following property. For every `c : ℝ≥0`, a point `x ∈ I.Icc`, and a positive `ε` there exists `δ > 0` such that for any box `J ≤ I` such that - `x ∈ J.Icc ⊆ metric.closed_ball x δ`; - if `l.bDistortion` (i.e., `l = ⊥`), then the distortion of `J` is less than or equal to `c`, the distance between the term `vol J (f x)` of an integral sum corresponding to `J` and `g J` is less than or equal to `ε` if `x ∈ s` and is less than or equal to `ε * B J` otherwise. Then `f` is integrable on `I along `l` with integral `g I`. -/ lemma has_integral_of_le_Henstock_of_forall_is_o (hl : l ≤ Henstock) (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (s : set ℝⁿ) (hs : s.countable) (H₁ : ∀ (c : ℝ≥0) (x ∈ I.Icc ∩ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε) (H₂ : ∀ (c : ℝ≥0) (x ∈ I.Icc \ s) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → x ∈ J.Icc → (l.bDistortion → J.distortion ≤ c) → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I l f vol (g I) := have A : l.bHenstock, from hl.2.1.resolve_left dec_trivial, has_integral_of_bRiemann_eq_ff_of_forall_is_o (hl.1.resolve_right dec_trivial) B hB0 _ s hs (λ _, A) H₁ $ by simpa only [A, true_implies_iff] using H₂ /-- Suppose that there exists a nonnegative box-additive function `B` with the following property. For every `c : ℝ≥0`, a point `x ∈ I.Icc`, and a positive `ε` there exists `δ > 0` such that for any box `J ≤ I` such that - `J.Icc ⊆ metric.closed_ball x δ`; - if `l.bDistortion` (i.e., `l = ⊥`), then the distortion of `J` is less than or equal to `c`, the distance between the term `vol J (f x)` of an integral sum corresponding to `J` and `g J` is less than or equal to `ε * B J`. Then `f` is McShane integrable on `I` with integral `g I`. -/ lemma has_integral_McShane_of_forall_is_o (B : ι →ᵇᵃ[I] ℝ) (hB0 : ∀ J, 0 ≤ B J) (g : ι →ᵇᵃ[I] F) (H : ∀ (c : ℝ≥0) (x ∈ I.Icc) (ε > (0 : ℝ)), ∃ δ > 0, ∀ J ≤ I, J.Icc ⊆ metric.closed_ball x δ → dist (vol J (f x)) (g J) ≤ ε * B J) : has_integral I McShane f vol (g I) := has_integral_of_bRiemann_eq_ff_of_forall_is_o rfl B hB0 g ∅ countable_empty (λ ⟨x, hx⟩, hx.elim) (λ c x hx, hx.2.elim) $ by simpa only [McShane, coe_sort_ff, false_implies_iff, true_implies_iff, diff_empty] using H end box_integral
fd9dc9238973f524305cc55ce484a8aa9e1f0562
947b78d97130d56365ae2ec264df196ce769371a
/stage0/src/Lean/Compiler/IR/UnboxResult.lean
ec7de8284ae7a8f9cc936a51b07ca871ab9ed229
[ "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
864
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.Data.Format import Lean.Compiler.IR.Basic namespace Lean namespace IR namespace UnboxResult def mkUnboxAttr : IO TagAttribute := registerTagAttribute `unbox "compiler tries to unbox result values if their types are tagged with `[unbox]`" $ fun declName => do cinfo ← getConstInfo declName; match cinfo with | ConstantInfo.inductInfo v => if v.isRec then throwError "recursive inductive datatypes are not supported" else pure () | _ => throwError "constant must be an inductive type" @[init mkUnboxAttr] constant unboxAttr : TagAttribute := arbitrary _ def hasUnboxAttr (env : Environment) (n : Name) : Bool := unboxAttr.hasTag env n end UnboxResult end IR end Lean
6528afd0017fe19f67cd9fe8072e3f8fe970b20b
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/matchMissingCasesAsStuckError.lean
9b1a4bccfd0f0279ae46ce39f35d4328a09ccce7
[ "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
97
lean
example (a : α) (f : α → Option α) : Bool := by match h:f a with | some _ => exact true
aaad0c2edec0dfc048be2ec2b4a10a57d4882582
fecda8e6b848337561d6467a1e30cf23176d6ad0
/src/category_theory/abelian/non_preadditive.lean
e4a22f5a99f0bc1d97628b058bba549ecaa567c0
[ "Apache-2.0" ]
permissive
spolu/mathlib
bacf18c3d2a561d00ecdc9413187729dd1f705ed
480c92cdfe1cf3c2d083abded87e82162e8814f4
refs/heads/master
1,671,684,094,325
1,600,736,045,000
1,600,736,045,000
297,564,749
1
0
null
1,600,758,368,000
1,600,758,367,000
null
UTF-8
Lean
false
false
32,779
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import category_theory.limits.shapes.finite_products import category_theory.limits.shapes.kernels import category_theory.limits.shapes.pullbacks import category_theory.limits.shapes.regular_mono import category_theory.preadditive /-! # Every non_preadditive_abelian category is preadditive In mathlib, we define an abelian category as a preadditive category with a zero object, kernels and cokernels, products and coproducts and in which every monomorphism and epimorphis is normal. While virtually every interesting abelian category has a natural preadditive structure (which is why it is included in the definition), preadditivity is not actually needed: Every category that has all of the other properties appearing in the definition of an abelian category admits a preadditive structure. This is the construction we carry out in this file. The proof proceeds in roughly five steps: 1. Prove some results (for example that all equalizers exist) that would be trivial if we already had the preadditive structure but are a bit of work without it. 2. Develop images and coimages to show that every monomorphism is the kernel of its cokernel. The results of the first two steps are also useful for the "normal" development of abelian categories, and will be used there. 3. For every object `A`, define a "subtraction" morphism `σ : A ⨯ A ⟶ A` and use it to define subtraction on morphisms as `f - g := prod.lift f g ≫ σ`. 4. Prove a small number of identities about this subtraction from the definition of `σ`. 5. From these identities, prove a large number of other identities that imply that defining `f + g := f - (0 - g)` indeed gives an abelian group structure on morphisms such that composition is bilinear. The construction is non-trivial and it is quite remarkable that this abelian group structure can be constructed purely from the existence of a few limits and colimits. What's even more impressive is that all additive structures on a category are in some sense isomorphic, so for abelian categories with a natural preadditive structure, this construction manages to "almost" reconstruct this natural structure. However, we have not formalized this isomorphism. ## References * [F. Borceux, *Handbook of Categorical Algebra 2*][borceux-vol2] -/ noncomputable theory open category_theory open category_theory.limits namespace category_theory section universes v u variables (C : Type u) [category.{v} C] /-- We call a category `non_preadditive_abelian` if it has a zero object, kernels, cokernels, binary products and coproducts, and every monomorphism and every epimorphism is normal. -/ class non_preadditive_abelian := [has_zero_object : has_zero_object C] [has_zero_morphisms : has_zero_morphisms C] [has_kernels : has_kernels C] [has_cokernels : has_cokernels C] [has_finite_products : has_finite_products C] [has_finite_coproducts : has_finite_coproducts C] (normal_mono : Π {X Y : C} (f : X ⟶ Y) [mono f], normal_mono f) (normal_epi : Π {X Y : C} (f : X ⟶ Y) [epi f], normal_epi f) set_option default_priority 100 attribute [instance] non_preadditive_abelian.has_zero_object attribute [instance] non_preadditive_abelian.has_zero_morphisms attribute [instance] non_preadditive_abelian.has_kernels attribute [instance] non_preadditive_abelian.has_cokernels attribute [instance] non_preadditive_abelian.has_finite_products attribute [instance] non_preadditive_abelian.has_finite_coproducts end end category_theory open category_theory namespace category_theory.non_preadditive_abelian universes v u variables {C : Type u} [category.{v} C] section variables [non_preadditive_abelian C] section strong local attribute [instance] non_preadditive_abelian.normal_epi /-- In a `non_preadditive_abelian` category, every epimorphism is strong. -/ def strong_epi_of_epi {P Q : C} (f : P ⟶ Q) [epi f] : strong_epi f := by apply_instance end strong section mono_epi_iso variables {X Y : C} (f : X ⟶ Y) local attribute [instance] strong_epi_of_epi /-- In a `non_preadditive_abelian` category, a monomorphism which is also an epimorphism is an isomorphism. -/ def is_iso_of_mono_of_epi [mono f] [epi f] : is_iso f := is_iso_of_mono_of_strong_epi _ end mono_epi_iso /-- The pullback of two monomorphisms exists. -/ @[irreducible] lemma pullback_of_mono {X Y Z : C} (a : X ⟶ Z) (b : Y ⟶ Z) [mono a] [mono b] : has_limit (cospan a b) := let ⟨P, f, haf, i⟩ := non_preadditive_abelian.normal_mono a in let ⟨Q, g, hbg, i'⟩ := non_preadditive_abelian.normal_mono b in let ⟨a', ha'⟩ := kernel_fork.is_limit.lift' i (kernel.ι (prod.lift f g)) $ calc kernel.ι (prod.lift f g) ≫ f = kernel.ι (prod.lift f g) ≫ prod.lift f g ≫ limits.prod.fst : by rw prod.lift_fst ... = (0 : kernel (prod.lift f g) ⟶ P ⨯ Q) ≫ limits.prod.fst : by rw kernel.condition_assoc ... = 0 : zero_comp in let ⟨b', hb'⟩ := kernel_fork.is_limit.lift' i' (kernel.ι (prod.lift f g)) $ calc kernel.ι (prod.lift f g) ≫ g = kernel.ι (prod.lift f g) ≫ (prod.lift f g) ≫ limits.prod.snd : by rw prod.lift_snd ... = (0 : kernel (prod.lift f g) ⟶ P ⨯ Q) ≫ limits.prod.snd : by rw kernel.condition_assoc ... = 0 : zero_comp in has_limit.mk { cone := pullback_cone.mk a' b' $ by { simp at ha' hb', rw [ha', hb'] }, is_limit := pullback_cone.is_limit.mk _ _ _ (λ s, kernel.lift (prod.lift f g) (pullback_cone.snd s ≫ b) $ prod.hom_ext (calc ((pullback_cone.snd s ≫ b) ≫ prod.lift f g) ≫ limits.prod.fst = pullback_cone.snd s ≫ b ≫ f : by simp only [prod.lift_fst, category.assoc] ... = pullback_cone.fst s ≫ a ≫ f : by rw pullback_cone.condition_assoc ... = pullback_cone.fst s ≫ 0 : by rw haf ... = 0 ≫ limits.prod.fst : by rw [comp_zero, zero_comp]) (calc ((pullback_cone.snd s ≫ b) ≫ prod.lift f g) ≫ limits.prod.snd = pullback_cone.snd s ≫ b ≫ g : by simp only [prod.lift_snd, category.assoc] ... = pullback_cone.snd s ≫ 0 : by rw hbg ... = 0 ≫ limits.prod.snd : by rw [comp_zero, zero_comp])) (λ s, (cancel_mono a).1 $ by { rw kernel_fork.ι_of_ι at ha', simp [ha', pullback_cone.condition s] }) (λ s, (cancel_mono b).1 $ by { rw kernel_fork.ι_of_ι at hb', simp [hb'] }) (λ s m h₁ h₂, (cancel_mono (kernel.ι (prod.lift f g))).1 $ calc m ≫ kernel.ι (prod.lift f g) = m ≫ a' ≫ a : by { congr, exact ha'.symm } ... = pullback_cone.fst s ≫ a : by rw [←category.assoc, h₁] ... = pullback_cone.snd s ≫ b : pullback_cone.condition s ... = kernel.lift (prod.lift f g) (pullback_cone.snd s ≫ b) _ ≫ kernel.ι (prod.lift f g) : by rw kernel.lift_ι) } /-- The pushout of two epimorphisms exists. -/ @[irreducible] lemma pushout_of_epi {X Y Z : C} (a : X ⟶ Y) (b : X ⟶ Z) [epi a] [epi b] : has_colimit (span a b) := let ⟨P, f, hfa, i⟩ := non_preadditive_abelian.normal_epi a in let ⟨Q, g, hgb, i'⟩ := non_preadditive_abelian.normal_epi b in let ⟨a', ha'⟩ := cokernel_cofork.is_colimit.desc' i (cokernel.π (coprod.desc f g)) $ calc f ≫ cokernel.π (coprod.desc f g) = coprod.inl ≫ coprod.desc f g ≫ cokernel.π (coprod.desc f g) : by rw coprod.inl_desc_assoc ... = coprod.inl ≫ (0 : P ⨿ Q ⟶ cokernel (coprod.desc f g)) : by rw cokernel.condition ... = 0 : has_zero_morphisms.comp_zero _ _ in let ⟨b', hb'⟩ := cokernel_cofork.is_colimit.desc' i' (cokernel.π (coprod.desc f g)) $ calc g ≫ cokernel.π (coprod.desc f g) = coprod.inr ≫ coprod.desc f g ≫ cokernel.π (coprod.desc f g) : by rw coprod.inr_desc_assoc ... = coprod.inr ≫ (0 : P ⨿ Q ⟶ cokernel (coprod.desc f g)) : by rw cokernel.condition ... = 0 : has_zero_morphisms.comp_zero _ _ in has_colimit.mk { cocone := pushout_cocone.mk a' b' $ by { simp only [cofork.π_of_π] at ha' hb', rw [ha', hb'] }, is_colimit := pushout_cocone.is_colimit.mk _ _ _ (λ s, cokernel.desc (coprod.desc f g) (b ≫ pushout_cocone.inr s) $ coprod.hom_ext (calc coprod.inl ≫ coprod.desc f g ≫ b ≫ pushout_cocone.inr s = f ≫ b ≫ pushout_cocone.inr s : by rw coprod.inl_desc_assoc ... = f ≫ a ≫ pushout_cocone.inl s : by rw pushout_cocone.condition ... = 0 ≫ pushout_cocone.inl s : by rw reassoc_of hfa ... = coprod.inl ≫ 0 : by rw [comp_zero, zero_comp]) (calc coprod.inr ≫ coprod.desc f g ≫ b ≫ pushout_cocone.inr s = g ≫ b ≫ pushout_cocone.inr s : by rw coprod.inr_desc_assoc ... = 0 ≫ pushout_cocone.inr s : by rw reassoc_of hgb ... = coprod.inr ≫ 0 : by rw [comp_zero, zero_comp])) (λ s, (cancel_epi a).1 $ by { rw cokernel_cofork.π_of_π at ha', simp [reassoc_of ha', pushout_cocone.condition s] }) (λ s, (cancel_epi b).1 $ by { rw cokernel_cofork.π_of_π at hb', simp [reassoc_of hb'] }) (λ s m h₁ h₂, (cancel_epi (cokernel.π (coprod.desc f g))).1 $ calc cokernel.π (coprod.desc f g) ≫ m = (a ≫ a') ≫ m : by { congr, exact ha'.symm } ... = a ≫ pushout_cocone.inl s : by rw [category.assoc, h₁] ... = b ≫ pushout_cocone.inr s : pushout_cocone.condition s ... = cokernel.π (coprod.desc f g) ≫ cokernel.desc (coprod.desc f g) (b ≫ pushout_cocone.inr s) _ : by rw cokernel.π_desc) } section local attribute [instance] pullback_of_mono /-- The pullback of `(𝟙 X, f)` and `(𝟙 X, g)` -/ private abbreviation P {X Y : C} (f g : X ⟶ Y) [mono (prod.lift (𝟙 X) f)] [mono (prod.lift (𝟙 X) g)] : C := pullback (prod.lift (𝟙 X) f) (prod.lift (𝟙 X) g) /-- The equalizer of `f` and `g` exists. -/ @[irreducible] lemma has_limit_parallel_pair {X Y : C} (f g : X ⟶ Y) : has_limit (parallel_pair f g) := have h1f : mono (prod.lift (𝟙 X) f), from mono_of_mono_fac $ prod.lift_fst (𝟙 X) f, have h1g : mono (prod.lift (𝟙 X) g), from mono_of_mono_fac $ prod.lift_fst (𝟙 X) g, have huv : (pullback.fst : P f g ⟶ X) = pullback.snd, from calc (pullback.fst : P f g ⟶ X) = pullback.fst ≫ 𝟙 _ : eq.symm $ category.comp_id _ ... = pullback.fst ≫ prod.lift (𝟙 X) f ≫ limits.prod.fst : by rw prod.lift_fst ... = pullback.snd ≫ prod.lift (𝟙 X) g ≫ limits.prod.fst : by rw pullback.condition_assoc ... = pullback.snd : by rw [prod.lift_fst, category.comp_id], have hvu : (pullback.fst : P f g ⟶ X) ≫ f = pullback.snd ≫ g, from calc (pullback.fst : P f g ⟶ X) ≫ f = pullback.fst ≫ prod.lift (𝟙 X) f ≫ limits.prod.snd : by rw prod.lift_snd ... = pullback.snd ≫ prod.lift (𝟙 X) g ≫ limits.prod.snd : by rw pullback.condition_assoc ... = pullback.snd ≫ g : by rw prod.lift_snd, have huu : (pullback.fst : P f g ⟶ X) ≫ f = pullback.fst ≫ g, by rw [hvu, ←huv], has_limit.mk { cone := fork.of_ι pullback.fst huu, is_limit := fork.is_limit.mk _ (λ s, pullback.lift (fork.ι s) (fork.ι s) $ prod.hom_ext (by simp only [prod.lift_fst, category.assoc]) (by simp only [fork.app_zero_right, fork.app_zero_left, prod.lift_snd, category.assoc])) (λ s, by simp only [fork.ι_of_ι, pullback.lift_fst]) (λ s m h, pullback.hom_ext (by simpa only [pullback.lift_fst] using h walking_parallel_pair.zero) (by simpa only [huv.symm, pullback.lift_fst] using h walking_parallel_pair.zero)) } end section local attribute [instance] pushout_of_epi /-- The pushout of `(𝟙 Y, f)` and `(𝟙 Y, g)`. -/ private abbreviation Q {X Y : C} (f g : X ⟶ Y) [epi (coprod.desc (𝟙 Y) f)] [epi (coprod.desc (𝟙 Y) g)] : C := pushout (coprod.desc (𝟙 Y) f) (coprod.desc (𝟙 Y) g) /-- The coequalizer of `f` and `g` exists. -/ @[irreducible] lemma has_colimit_parallel_pair {X Y : C} (f g : X ⟶ Y) : has_colimit (parallel_pair f g) := have h1f : epi (coprod.desc (𝟙 Y) f), from epi_of_epi_fac $ coprod.inl_desc _ _, have h1g : epi (coprod.desc (𝟙 Y) g), from epi_of_epi_fac $ coprod.inl_desc _ _, have huv : (pushout.inl : Y ⟶ Q f g) = pushout.inr, from calc (pushout.inl : Y ⟶ Q f g) = 𝟙 _ ≫ pushout.inl : eq.symm $ category.id_comp _ ... = (coprod.inl ≫ coprod.desc (𝟙 Y) f) ≫ pushout.inl : by rw coprod.inl_desc ... = (coprod.inl ≫ coprod.desc (𝟙 Y) g) ≫ pushout.inr : by simp only [category.assoc, pushout.condition] ... = pushout.inr : by rw [coprod.inl_desc, category.id_comp], have hvu : f ≫ (pushout.inl : Y ⟶ Q f g) = g ≫ pushout.inr, from calc f ≫ (pushout.inl : Y ⟶ Q f g) = (coprod.inr ≫ coprod.desc (𝟙 Y) f) ≫ pushout.inl : by rw coprod.inr_desc ... = (coprod.inr ≫ coprod.desc (𝟙 Y) g) ≫ pushout.inr : by simp only [category.assoc, pushout.condition] ... = g ≫ pushout.inr : by rw coprod.inr_desc, have huu : f ≫ (pushout.inl : Y ⟶ Q f g) = g ≫ pushout.inl, by rw [hvu, huv], has_colimit.mk { cocone := cofork.of_π pushout.inl huu, is_colimit := cofork.is_colimit.mk _ (λ s, pushout.desc (cofork.π s) (cofork.π s) $ coprod.hom_ext (by simp only [coprod.inl_desc_assoc]) (by simp only [cofork.right_app_one, coprod.inr_desc_assoc, cofork.left_app_one])) (λ s, by simp only [pushout.inl_desc, cofork.π_of_π]) (λ s m h, pushout.hom_ext (by simpa only [pushout.inl_desc] using h walking_parallel_pair.one) (by simpa only [huv.symm, pushout.inl_desc] using h walking_parallel_pair.one)) } end section local attribute [instance] has_limit_parallel_pair /-- A `non_preadditive_abelian` category has all equalizers. -/ @[priority 100] instance has_equalizers : has_equalizers C := has_equalizers_of_has_limit_parallel_pair _ end section local attribute [instance] has_colimit_parallel_pair /-- A `non_preadditive_abelian` category has all coequalizers. -/ @[priority 100] instance has_coequalizers : has_coequalizers C := has_coequalizers_of_has_colimit_parallel_pair _ end section /-- If a zero morphism is a kernel of `f`, then `f` is a monomorphism. -/ lemma mono_of_zero_kernel {X Y : C} (f : X ⟶ Y) (Z : C) (l : is_limit (kernel_fork.of_ι (0 : Z ⟶ X) (show 0 ≫ f = 0, by simp))) : mono f := ⟨λ P u v huv, begin obtain ⟨W, w, hw, hl⟩ := non_preadditive_abelian.normal_epi (coequalizer.π u v), obtain ⟨m, hm⟩ := coequalizer.desc' f huv, have hwf : w ≫ f = 0, { rw [←hm, reassoc_of hw, zero_comp] }, obtain ⟨n, hn⟩ := kernel_fork.is_limit.lift' l _ hwf, rw [fork.ι_of_ι, has_zero_morphisms.comp_zero] at hn, haveI : is_iso (coequalizer.π u v) := by apply is_iso_colimit_cocone_parallel_pair_of_eq hn.symm hl, apply (cancel_mono (coequalizer.π u v)).1, exact coequalizer.condition _ _ end⟩ /-- If a zero morphism is a cokernel of `f`, then `f` is an epimorphism. -/ lemma epi_of_zero_cokernel {X Y : C} (f : X ⟶ Y) (Z : C) (l : is_colimit (cokernel_cofork.of_π (0 : Y ⟶ Z) (show f ≫ 0 = 0, by simp))) : epi f := ⟨λ P u v huv, begin obtain ⟨W, w, hw, hl⟩ := non_preadditive_abelian.normal_mono (equalizer.ι u v), obtain ⟨m, hm⟩ := equalizer.lift' f huv, have hwf : f ≫ w = 0, { rw [←hm, category.assoc, hw, comp_zero] }, obtain ⟨n, hn⟩ := cokernel_cofork.is_colimit.desc' l _ hwf, rw [cofork.π_of_π, zero_comp] at hn, haveI : is_iso (equalizer.ι u v) := by apply is_iso_limit_cone_parallel_pair_of_eq hn.symm hl, apply (cancel_epi (equalizer.ι u v)).1, exact equalizer.condition _ _ end⟩ local attribute [instance] has_zero_object.has_zero /-- If `g ≫ f = 0` implies `g = 0` for all `g`, then `0 : 0 ⟶ X` is a kernel of `f`. -/ def zero_kernel_of_cancel_zero {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Z ⟶ X) (hgf : g ≫ f = 0), g = 0) : is_limit (kernel_fork.of_ι (0 : 0 ⟶ X) (show 0 ≫ f = 0, by simp)) := fork.is_limit.mk _ (λ s, 0) (λ s, by rw [hf _ _ (kernel_fork.condition s), zero_comp]) (λ s m h, by ext) /-- If `f ≫ g = 0` implies `g = 0` for all `g`, then `0 : Y ⟶ 0` is a cokernel of `f`. -/ def zero_cokernel_of_zero_cancel {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Y ⟶ Z) (hgf : f ≫ g = 0), g = 0) : is_colimit (cokernel_cofork.of_π (0 : Y ⟶ 0) (show f ≫ 0 = 0, by simp)) := cofork.is_colimit.mk _ (λ s, 0) (λ s, by rw [hf _ _ (cokernel_cofork.condition s), comp_zero]) (λ s m h, by ext) /-- If `g ≫ f = 0` implies `g = 0` for all `g`, then `f` is a monomorphism. -/ lemma mono_of_cancel_zero {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Z ⟶ X) (hgf : g ≫ f = 0), g = 0) : mono f := mono_of_zero_kernel f 0 $ zero_kernel_of_cancel_zero f hf /-- If `f ≫ g = 0` implies `g = 0` for all `g`, then `g` is a monomorphism. -/ lemma epi_of_zero_cancel {X Y : C} (f : X ⟶ Y) (hf : ∀ (Z : C) (g : Y ⟶ Z) (hgf : f ≫ g = 0), g = 0) : epi f := epi_of_zero_cokernel f 0 $ zero_cokernel_of_zero_cancel f hf end section factor variables {P Q : C} (f : P ⟶ Q) /-- The kernel of the cokernel of `f` is called the image of `f`. -/ protected abbreviation image : C := kernel (cokernel.π f) /-- The inclusion of the image into the codomain. -/ protected abbreviation image.ι : non_preadditive_abelian.image f ⟶ Q := kernel.ι (cokernel.π f) /-- There is a canonical epimorphism `p : P ⟶ image f` for every `f`. -/ protected abbreviation factor_thru_image : P ⟶ non_preadditive_abelian.image f := kernel.lift (cokernel.π f) f $ cokernel.condition f /-- `f` factors through its image via the canonical morphism `p`. -/ @[simp, reassoc] protected lemma image.fac : non_preadditive_abelian.factor_thru_image f ≫ image.ι f = f := kernel.lift_ι _ _ _ /-- The map `p : P ⟶ image f` is an epimorphism -/ instance : epi (non_preadditive_abelian.factor_thru_image f) := let I := non_preadditive_abelian.image f, p := non_preadditive_abelian.factor_thru_image f, i := kernel.ι (cokernel.π f) in -- It will suffice to consider some g : I ⟶ R such that p ≫ g = 0 and show that g = 0. epi_of_zero_cancel _ $ λ R (g : I ⟶ R) (hpg : p ≫ g = 0), begin -- Since C is abelian, u := ker g ≫ i is the kernel of some morphism h. let u := kernel.ι g ≫ i, haveI : mono u := mono_comp _ _, haveI hu := non_preadditive_abelian.normal_mono u, let h := hu.g, -- By hypothesis, p factors through the kernel of g via some t. obtain ⟨t, ht⟩ := kernel.lift' g p hpg, have fh : f ≫ h = 0, calc f ≫ h = (p ≫ i) ≫ h : (image.fac f).symm ▸ rfl ... = ((t ≫ kernel.ι g) ≫ i) ≫ h : ht ▸ rfl ... = t ≫ u ≫ h : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = t ≫ 0 : hu.w ▸ rfl ... = 0 : has_zero_morphisms.comp_zero _ _, -- h factors through the cokernel of f via some l. obtain ⟨l, hl⟩ := cokernel.desc' f h fh, have hih : i ≫ h = 0, calc i ≫ h = i ≫ cokernel.π f ≫ l : hl ▸ rfl ... = 0 ≫ l : by rw [←category.assoc, kernel.condition] ... = 0 : zero_comp, -- i factors through u = ker h via some s. obtain ⟨s, hs⟩ := normal_mono.lift' u i hih, have hs' : (s ≫ kernel.ι g) ≫ i = 𝟙 I ≫ i, by rw [category.assoc, hs, category.id_comp], haveI : epi (kernel.ι g) := epi_of_epi_fac ((cancel_mono _).1 hs'), -- ker g is an epimorphism, but ker g ≫ g = 0 = ker g ≫ 0, so g = 0 as required. exact zero_of_epi_comp _ (kernel.condition g) end instance mono_factor_thru_image [mono f] : mono (non_preadditive_abelian.factor_thru_image f) := mono_of_mono_fac $ image.fac f instance is_iso_factor_thru_image [mono f] : is_iso (non_preadditive_abelian.factor_thru_image f) := is_iso_of_mono_of_epi _ /-- The cokernel of the kernel of `f` is called the coimage of `f`. -/ protected abbreviation coimage : C := cokernel (kernel.ι f) /-- The projection onto the coimage. -/ protected abbreviation coimage.π : P ⟶ non_preadditive_abelian.coimage f := cokernel.π (kernel.ι f) /-- There is a canonical monomorphism `i : coimage f ⟶ Q`. -/ protected abbreviation factor_thru_coimage : non_preadditive_abelian.coimage f ⟶ Q := cokernel.desc (kernel.ι f) f $ kernel.condition f /-- `f` factors through its coimage via the canonical morphism `p`. -/ protected lemma coimage.fac : coimage.π f ≫ non_preadditive_abelian.factor_thru_coimage f = f := cokernel.π_desc _ _ _ /-- The canonical morphism `i : coimage f ⟶ Q` is a monomorphism -/ instance : mono (non_preadditive_abelian.factor_thru_coimage f) := let I := non_preadditive_abelian.coimage f, i := non_preadditive_abelian.factor_thru_coimage f, p := cokernel.π (kernel.ι f) in mono_of_cancel_zero _ $ λ R (g : R ⟶ I) (hgi : g ≫ i = 0), begin -- Since C is abelian, u := p ≫ coker g is the cokernel of some morphism h. let u := p ≫ cokernel.π g, haveI : epi u := epi_comp _ _, haveI hu := non_preadditive_abelian.normal_epi u, let h := hu.g, -- By hypothesis, i factors through the cokernel of g via some t. obtain ⟨t, ht⟩ := cokernel.desc' g i hgi, have hf : h ≫ f = 0, calc h ≫ f = h ≫ (p ≫ i) : (coimage.fac f).symm ▸ rfl ... = h ≫ (p ≫ (cokernel.π g ≫ t)) : ht ▸ rfl ... = h ≫ u ≫ t : by simp only [category.assoc]; conv_lhs { congr, skip, rw ←category.assoc } ... = 0 ≫ t : by rw [←category.assoc, hu.w] ... = 0 : zero_comp, -- h factors through the kernel of f via some l. obtain ⟨l, hl⟩ := kernel.lift' f h hf, have hhp : h ≫ p = 0, calc h ≫ p = (l ≫ kernel.ι f) ≫ p : hl ▸ rfl ... = l ≫ 0 : by rw [category.assoc, cokernel.condition] ... = 0 : comp_zero, -- p factors through u = coker h via some s. obtain ⟨s, hs⟩ := normal_epi.desc' u p hhp, have hs' : p ≫ cokernel.π g ≫ s = p ≫ 𝟙 I, by rw [←category.assoc, hs, category.comp_id], haveI : mono (cokernel.π g) := mono_of_mono_fac ((cancel_epi _).1 hs'), -- coker g is a monomorphism, but g ≫ coker g = 0 = 0 ≫ coker g, so g = 0 as required. exact zero_of_comp_mono _ (cokernel.condition g) end instance epi_factor_thru_coimage [epi f] : epi (non_preadditive_abelian.factor_thru_coimage f) := epi_of_epi_fac $ coimage.fac f instance is_iso_factor_thru_coimage [epi f] : is_iso (non_preadditive_abelian.factor_thru_coimage f) := is_iso_of_mono_of_epi _ end factor section cokernel_of_kernel variables {X Y : C} {f : X ⟶ Y} /-- In a `non_preadditive_abelian` category, an epi is the cokernel of its kernel. More precisely: If `f` is an epimorphism and `s` is some limit kernel cone on `f`, then `f` is a cokernel of `fork.ι s`. -/ def epi_is_cokernel_of_kernel [epi f] (s : fork f 0) (h : is_limit s) : is_colimit (cokernel_cofork.of_π f (kernel_fork.condition s)) := is_cokernel.cokernel_iso _ _ (cokernel.of_iso_comp _ _ (limits.is_limit.cone_point_unique_up_to_iso (limit.is_limit _) h) (cone_morphism.w (limits.is_limit.unique_up_to_iso (limit.is_limit _) h).hom _)) (as_iso $ non_preadditive_abelian.factor_thru_coimage f) (coimage.fac f) /-- In a `non_preadditive_abelian` category, a mono is the kernel of its cokernel. More precisely: If `f` is a monomorphism and `s` is some colimit cokernel cocone on `f`, then `f` is a kernel of `cofork.π s`. -/ def mono_is_kernel_of_cokernel [mono f] (s : cofork f 0) (h : is_colimit s) : is_limit (kernel_fork.of_ι f (cokernel_cofork.condition s)) := is_kernel.iso_kernel _ _ (kernel.of_comp_iso _ _ (limits.is_colimit.cocone_point_unique_up_to_iso h (colimit.is_colimit _)) (cocone_morphism.w (limits.is_colimit.unique_up_to_iso h $ colimit.is_colimit _).hom _)) (as_iso $ non_preadditive_abelian.factor_thru_image f) (image.fac f) end cokernel_of_kernel section /-- The diagonal morphism `(𝟙 A, 𝟙 A) : A → A ⨯ A`. -/ abbreviation Δ (A : C) : A ⟶ A ⨯ A := prod.lift (𝟙 A) (𝟙 A) /-- The composite `A ⟶ A ⨯ A ⟶ cokernel (Δ A)`, where the first map is `(𝟙 A, 0)` and the second map is the canonical projection into the cokernel. -/ abbreviation r (A : C) : A ⟶ cokernel (Δ A) := prod.lift (𝟙 A) 0 ≫ cokernel.π (Δ A) instance mono_Δ {A : C} : mono (Δ A) := mono_of_mono_fac $ prod.lift_fst _ _ instance mono_r {A : C} : mono (r A) := begin let hl : is_limit (kernel_fork.of_ι (Δ A) (cokernel.condition (Δ A))), { exact mono_is_kernel_of_cokernel _ (colimit.is_colimit _) }, apply mono_of_cancel_zero, intros Z x hx, have hxx : (x ≫ prod.lift (𝟙 A) (0 : A ⟶ A)) ≫ cokernel.π (Δ A) = 0, { rw [category.assoc, hx] }, obtain ⟨y, hy⟩ := kernel_fork.is_limit.lift' hl _ hxx, rw kernel_fork.ι_of_ι at hy, have hyy : y = 0, { erw [←category.comp_id y, ←limits.prod.lift_snd (𝟙 A) (𝟙 A), ←category.assoc, hy, category.assoc, prod.lift_snd, has_zero_morphisms.comp_zero] }, haveI : mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1, rw [←hy, hyy, zero_comp, zero_comp] end instance epi_r {A : C} : epi (r A) := begin have hlp : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ limits.prod.snd = 0 := prod.lift_snd _ _, let hp1 : is_limit (kernel_fork.of_ι (prod.lift (𝟙 A) (0 : A ⟶ A)) hlp), { refine fork.is_limit.mk _ (λ s, fork.ι s ≫ limits.prod.fst) _ _, { intro s, ext; simp, erw category.comp_id }, { intros s m h, haveI : mono (prod.lift (𝟙 A) (0 : A ⟶ A)) := mono_of_mono_fac (prod.lift_fst _ _), apply (cancel_mono (prod.lift (𝟙 A) (0 : A ⟶ A))).1, convert h walking_parallel_pair.zero, ext; simp } }, let hp2 : is_colimit (cokernel_cofork.of_π (limits.prod.snd : A ⨯ A ⟶ A) hlp), { exact epi_is_cokernel_of_kernel _ hp1 }, apply epi_of_zero_cancel, intros Z z hz, have h : prod.lift (𝟙 A) (0 : A ⟶ A) ≫ cokernel.π (Δ A) ≫ z = 0, { rw [←category.assoc, hz] }, obtain ⟨t, ht⟩ := cokernel_cofork.is_colimit.desc' hp2 _ h, rw cokernel_cofork.π_of_π at ht, have htt : t = 0, { rw [←category.id_comp t], change 𝟙 A ≫ t = 0, rw [←limits.prod.lift_snd (𝟙 A) (𝟙 A), category.assoc, ht, ←category.assoc, cokernel.condition, zero_comp] }, apply (cancel_epi (cokernel.π (Δ A))).1, rw [←ht, htt, comp_zero, comp_zero] end instance is_iso_r {A : C} : is_iso (r A) := is_iso_of_mono_of_epi _ /-- The composite `A ⨯ A ⟶ cokernel (Δ A) ⟶ A` given by the natural projection into the cokernel followed by the inverse of `r`. In the category of modules, using the normal kernels and cokernels, this map is equal to the map `(a, b) ↦ a - b`, hence the name `σ` for "subtraction". -/ abbreviation σ {A : C} : A ⨯ A ⟶ A := cokernel.π (Δ A) ≫ is_iso.inv (r A) end @[simp, reassoc] lemma Δ_σ {X : C} : Δ X ≫ σ = 0 := by rw [cokernel.condition_assoc, zero_comp] @[simp, reassoc] lemma lift_σ {X : C} : prod.lift (𝟙 X) 0 ≫ σ = 𝟙 X := by rw [←category.assoc, is_iso.hom_inv_id] @[simp, reassoc] lemma Δ_map {X Y : C} (f : X ⟶ Y) : Δ X ≫ limits.prod.map f f = f ≫ Δ Y := by ext; simp; erw category.id_comp @[reassoc] lemma lift_map {X Y : C} (f : X ⟶ Y) : prod.lift (𝟙 X) 0 ≫ limits.prod.map f f = f ≫ prod.lift (𝟙 Y) 0 := by ext; simp; erw category.id_comp /-- σ is a cokernel of Δ X. -/ def is_colimit_σ {X : C} : is_colimit (cokernel_cofork.of_π σ Δ_σ) := cokernel.cokernel_iso _ σ (as_iso (r X)).symm (by simp) /-- This is the key identity satisfied by `σ`. -/ lemma σ_comp {X Y : C} (f : X ⟶ Y) : σ ≫ f = limits.prod.map f f ≫ σ := begin obtain ⟨g, hg⟩ := cokernel_cofork.is_colimit.desc' is_colimit_σ (limits.prod.map f f ≫ σ) (by simp), suffices hfg : f = g, { rw [←hg, cofork.π_of_π, hfg] }, calc f = f ≫ prod.lift (𝟙 Y) 0 ≫ σ : by rw [lift_σ, category.comp_id] ... = prod.lift (𝟙 X) 0 ≫ limits.prod.map f f ≫ σ : by rw lift_map_assoc ... = prod.lift (𝟙 X) 0 ≫ σ ≫ g : by rw [←hg, cokernel_cofork.π_of_π] ... = g : by rw [←category.assoc, lift_σ, category.id_comp] end section /- We write `f - g` for `prod.lift f g ≫ σ`. -/ /-- Subtraction of morphisms in a `non_preadditive_abelian` category. -/ def has_sub {X Y : C} : has_sub (X ⟶ Y) := ⟨λ f g, prod.lift f g ≫ σ⟩ local attribute [instance] has_sub /- We write `-f` for `0 - f`. -/ /-- Negation of morphisms in a `non_preadditive_abelian` category. -/ def has_neg {X Y : C} : has_neg (X ⟶ Y) := ⟨λ f, 0 - f⟩ local attribute [instance] has_neg /- We write `f + g` for `f - (-g)`. -/ /-- Addition of morphisms in a `non_preadditive_abelian` category. -/ def has_add {X Y : C} : has_add (X ⟶ Y) := ⟨λ f g, f - (-g)⟩ local attribute [instance] has_add lemma sub_def {X Y : C} (a b : X ⟶ Y) : a - b = prod.lift a b ≫ σ := rfl lemma add_def {X Y : C} (a b : X ⟶ Y) : a + b = a - (-b) := rfl lemma neg_def {X Y : C} (a : X ⟶ Y) : -a = 0 - a := rfl lemma sub_zero {X Y : C} (a : X ⟶ Y) : a - 0 = a := begin rw sub_def, conv_lhs { congr, congr, rw ←category.comp_id a, skip, rw (show 0 = a ≫ (0 : Y ⟶ Y), by simp)}, rw [prod.lift_comp_comp, category.assoc, lift_σ, category.comp_id] end lemma sub_self {X Y : C} (a : X ⟶ Y) : a - a = 0 := by rw [sub_def, ←category.comp_id a, prod.lift_comp_comp, category.assoc, Δ_σ, has_zero_morphisms.comp_zero] lemma lift_sub_lift {X Y : C} (a b c d : X ⟶ Y) : prod.lift a b - prod.lift c d = prod.lift (a - c) (b - d) := begin simp only [sub_def], ext, { rw [category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_fst, prod.lift_fst, prod.lift_fst] }, { rw [category.assoc, σ_comp, prod.lift_map_assoc, prod.lift_snd, prod.lift_snd, prod.lift_snd] } end lemma sub_sub_sub {X Y : C} (a b c d : X ⟶ Y) : (a - c) - (b - d) = (a - b) - (c - d) := begin rw sub_def, erw ←lift_sub_lift, rw [sub_def, category.assoc, σ_comp, ←category.assoc, prod.lift_map, sub_def, sub_def, sub_def] end lemma neg_sub {X Y : C} (a b : X ⟶ Y) : (-a) - b = (-b) - a := by conv_lhs { rw [neg_def, ←sub_zero b, sub_sub_sub, sub_zero, ←neg_def] } lemma neg_neg {X Y : C} (a : X ⟶ Y) : -(-a) = a := begin rw [neg_def, neg_def], conv_lhs { congr, rw ←sub_self a }, rw [sub_sub_sub, sub_zero, sub_self, sub_zero] end lemma add_comm {X Y : C} (a b : X ⟶ Y) : a + b = b + a := begin rw [add_def], conv_lhs { rw ←neg_neg a }, rw [neg_def, neg_def, neg_def, sub_sub_sub], conv_lhs {congr, skip, rw [←neg_def, neg_sub] }, rw [sub_sub_sub, add_def, ←neg_def, neg_neg b, neg_def] end lemma add_neg {X Y : C} (a b : X ⟶ Y) : a + (-b) = a - b := by rw [add_def, neg_neg] lemma add_neg_self {X Y : C} (a : X ⟶ Y) : a + (-a) = 0 := by rw [add_neg, sub_self] lemma neg_add_self {X Y : C} (a : X ⟶ Y) : (-a) + a = 0 := by rw [add_comm, add_neg_self] lemma neg_sub' {X Y : C} (a b : X ⟶ Y) : -(a - b) = (-a) + b := begin rw [neg_def, neg_def], conv_lhs { rw ←sub_self (0 : X ⟶ Y) }, rw [sub_sub_sub, add_def, neg_def] end lemma neg_add {X Y : C} (a b : X ⟶ Y) : -(a + b) = (-a) - b := by rw [add_def, neg_sub', add_neg] lemma sub_add {X Y : C} (a b c : X ⟶ Y) : (a - b) + c = a - (b - c) := by rw [add_def, neg_def, sub_sub_sub, sub_zero] lemma add_assoc {X Y : C} (a b c : X ⟶ Y) : (a + b) + c = a + (b + c) := begin conv_lhs { congr, rw add_def }, rw [sub_add, ←add_neg, neg_sub', neg_neg] end lemma add_zero {X Y : C} (a : X ⟶ Y) : a + 0 = a := by rw [add_def, neg_def, sub_self, sub_zero] lemma comp_sub {X Y Z : C} (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g - h) = f ≫ g - f ≫ h := by rw [sub_def, ←category.assoc, ←prod.lift_comp_comp, sub_def] lemma sub_comp {X Y Z : C} (f g : X ⟶ Y) (h : Y ⟶ Z) : (f - g) ≫ h = f ≫ h - g ≫ h := by rw [sub_def, category.assoc, σ_comp, ←category.assoc, prod.lift_map, sub_def] lemma comp_add (X Y Z : C) (f : X ⟶ Y) (g h : Y ⟶ Z) : f ≫ (g + h) = f ≫ g + f ≫ h := by rw [add_def, comp_sub, neg_def, comp_sub, comp_zero, add_def, neg_def] lemma add_comp (X Y Z : C) (f g : X ⟶ Y) (h : Y ⟶ Z) : (f + g) ≫ h = f ≫ h + g ≫ h := by rw [add_def, sub_comp, neg_def, sub_comp, zero_comp, add_def, neg_def] /-- Every `non_preadditive_abelian` category is preadditive. -/ def preadditive : preadditive C := { hom_group := λ X Y, { add := (+), add_assoc := add_assoc, zero := 0, zero_add := neg_neg, add_zero := add_zero, neg := λ f, -f, add_left_neg := neg_add_self, add_comm := add_comm }, add_comp' := add_comp, comp_add' := comp_add } end end end category_theory.non_preadditive_abelian
ba6c6216c08097f2af6cb09addefd305ef9a6fd8
8c433fccc85bae2c68bb34c8969dd5f6fe94945b
/src/mywork/hw4.lean
b792285c350ace4263f8bbc2b6f926c1e724f35e
[]
no_license
arh4uwe/cs2120f21
f2a844538124e9739b94bdb99a86221866c854ac
2cfb17c7b84a07a672b792fc85824f3de7c27e7a
refs/heads/main
1,691,945,681,056
1,632,839,762,000
1,632,839,762,000
400,231,555
0
0
null
null
null
null
UTF-8
Lean
false
false
6,125
lean
-- 1 example : 0 ≠ 1 := begin -- ¬ (0 = 1) -- (0 = 1) → false assume h, cases h, end -- 2 example : 0 ≠ 0 → 2 = 3 := begin assume h, have f : false := h (eq.refl 0), exact false.elim (f), end -- 3 example : ∀ (P : Prop), P → ¬¬P := begin assume P, assume (p : P), -- ¬¬P -- ¬P → false -- (P → false) → false assume h, have f := h p, exact f, end -- We might need classical (vs constructive) reasoning #check classical.em open classical #check em /- axiom em : ∀ (p : Prop), p ∨ ¬p This is the famous and historically controversial "law" (now axiom) of the excluded middle. It's is a key to proving many intuitive theorems in logic and mathematics. But it also leads to giving up on having evidence *why* something is either true or not true, in that you no longer need a proof of either P or of ¬P to have a proof of P ∨ ¬P. -/ -- 4 theorem neg_elim : ∀ (P : Prop), ¬¬P → P := begin assume P, assume h, have pornp := classical.em P, cases pornp with p pn, assumption, contradiction, end -- 5 theorem demorgan_1 : ∀ (P Q : Prop), ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q := begin assume P Q, apply iff.intro, assume h, have pornp := classical.em P, cases pornp with p np, have qornq := classical.em Q, cases qornq with q nq, have pandq : P ∧ Q := and.intro p q, apply false.elim, exact h pandq, apply or.intro_right, exact nq, apply or.intro_left, exact np, assume h₁ h₂, have p := and.elim_left h₂, have q := and.elim_right h₂, cases h₁ with np nq, exact np p, exact nq q, end -- 6 theorem demorgan_2 : ∀ (P Q : Prop), ¬ (P ∨ Q) → ¬P ∧ ¬Q := begin assume P Q h, apply and.intro, assume p, have porq : P ∨ Q := or.intro_left _ p, exact h porq, assume q, have porq : P ∨ Q := or.intro_right _ q, exact h porq, end -- 7 theorem disappearing_opposite : ∀ (P Q : Prop), P ∨ ¬P ∧ Q ↔ P ∨ Q := begin assume P Q, apply iff.intro, -- forward assume h, cases h, -- P apply or.intro_left, exact h, -- ¬P ∧ Q have q := and.elim_right h, apply or.intro_right, exact q, -- backward assume porq, cases porq with p q, -- P apply or.intro_left, exact p, -- Q have pornp := classical.em P, cases pornp with p np, -- P apply or.intro_left, exact p, -- ¬P apply or.intro_right, apply and.intro, exact np, exact q, end -- 8 theorem distrib_and_or : ∀ (P Q R: Prop), (P ∨ Q) ∧ (P ∨ R) ↔ P ∨ (Q ∧ R) := begin assume P Q R, apply iff.intro, -- forward assume h, have porq := and.elim_left h, have porr := and.elim_right h, cases porq with p q, -- P apply or.intro_left, exact p, -- Q cases porr with p r, -- P apply or.intro_left, exact p, -- R apply or.intro_right, apply and.intro, apply q, apply r, -- backward assume h, cases h with p qandr, -- P apply and.intro, apply or.intro_left, exact p, apply or.intro_left, exact p, -- Q ∧ R have q := and.elim_left qandr, have r := and.elim_right qandr, apply and.intro, apply or.intro_right, exact q, apply or.intro_right, exact r, end -- remember or is right associative -- you need this to know what the lefts and rights are -- 9 theorem distrib_and_or_foil : ∀ (P Q R S : Prop), (P ∨ Q) ∧ (R ∨ S) ↔ (P ∧ R) ∨ (P ∧ S) ∨ (Q ∧ R) ∨ (Q ∧ S) := begin assume P Q R S, apply iff.intro, -- forward assume h, have porq := and.elim_left h, have rors := and.elim_right h, cases porq with p q, -- P cases rors with r s, -- R apply or.intro_left, apply and.intro p r, -- S apply or.intro_right, apply or.intro_left, apply and.intro p s, -- Q cases rors with r s, -- R apply or.intro_right, apply or.intro_right, apply or.intro_left, exact and.intro q r, -- S repeat {apply or.intro_right}, exact and.intro q s, -- backward assume h, cases h with pandr h, -- P ∧ R apply and.intro, apply or.intro_left, exact and.elim_left pandr, apply or.intro_left, exact and.elim_right pandr, cases h with pands h, -- P ∧ S apply and.intro, apply or.intro_left, exact and.elim_left pands, apply or.intro_right, exact and.elim_right pands, cases h with qandr qands, -- Q ∧ R apply and.intro, apply or.intro_right, exact and.elim_left qandr, apply or.intro_left, exact and.elim_right qandr, -- Q ∧ S apply and.intro, apply or.intro_right, exact and.elim_left qands, apply or.intro_right, exact and.elim_right qands, end /- 10 Formally state and prove the proposition that not every natural number is equal to zero. -/ lemma not_all_nats_are_zero : ¬ (∀ n : ℕ, n = 0) := begin assume h, have f : 1 = 0 := h 1, contradiction, end -- 11. equivalence of P→Q and (¬P∨Q) example : ∀ (P Q : Prop), (P → Q) ↔ (¬P ∨ Q) := begin assume P Q, apply iff.intro, -- forward assume h, have pornp := classical.em P, cases pornp with p np, -- P have q : Q := h p, apply or.intro_right, exact q, -- ¬P apply or.intro_left, exact np, -- backward assume h, assume p, cases h with np q, -- ¬P contradiction, -- Q exact q, end -- 12 example : ∀ (P Q : Prop), (P → Q) → (¬ Q → ¬ P) := begin assume P Q h nq p, have q := h p, contradiction, end -- 13 example : ∀ (P Q : Prop), ( ¬P → ¬Q) → (Q → P) := begin assume P Q h q, have pornp : P ∨ ¬P := classical.em P, cases pornp with p np, -- P exact p, -- ¬P have nq : ¬Q := h np, contradiction, end
bc1c4d79faa39d4c30b6023990be3fbda114f18f
6b2a480f27775cba4f3ae191b1c1387a29de586e
/group_rep_2/permutation_representation/regular_representation.lean
a1f12ac881789f9b10acf4dae218fe8a27cdd94f
[]
no_license
Or7ando/group_representation
a681de2e19d1930a1e1be573d6735a2f0b8356cb
9b576984f17764ebf26c8caa2a542d248f1b50d2
refs/heads/master
1,662,413,107,324
1,590,302,389,000
1,590,302,389,000
258,130,829
0
1
null
null
null
null
UTF-8
Lean
false
false
2,310
lean
import permutation_representation.action_representation import Tools.tools import basic_definitions.matrix_representation open is_basis linear_map character universes u v w w' open general classical_basis open finite_action open fintype variables (G : Type u) [group G] (R : Type v) [comm_ring R] (X : Type w)[fintype X][decidable_eq X] [mul_action G X] @[simp]theorem valvl (g : G) : mat (Perm G R X) g = λ x y, if g • y = x then 1 else 0 := begin apply matrix.ext,intros i j, unfold mat, unfold to_matrix,unfold to_matrixₗ, dsimp, --- faire mieux erw ← finite_action.Perm_ext, erw action_on_basis_apply, end lemma Grall (g : G) : (λ i : X, mat (Perm G R X) g i i) = (λ i : X, if g • i = i then 1 else 0 ) := begin funext,rw valvl, end @[simp]theorem χ_value_eq_fixed_point (g : G ): χ (Perm G R X) g = card ({x : X | g • x = x} : set X):= begin unfold χ,simp,exact rfl, end namespace Regular def Cayley_action (G : Type u) [group G] : mul_action G G := { smul := λ g r, g * r, one_smul := begin intros, dsimp, rw one_mul, end, mul_smul := begin intros, dsimp, rw mul_assoc,end } def Regular_representation (G : Type u) [group G][fintype G][decidable_eq G](R :Type v)[comm_ring R] : group_representation G R (G → R) := @Perm G R G _ _(Cayley_action G) variables [fintype G][decidable_eq G] lemma terere (X : Type u)[fintype X][decidable_eq X] : (fintype.card ({x : X | true })) = (fintype.card X) := begin exact fintype.card_congr ( equiv.set.univ X), end /-- `χ (Regular_representation G R ) g = if g = 1 then card G else 0 ` -/ theorem character_of_regular_representation (g : G): χ (Regular_representation G R ) g = if g = 1 then card G else (0 ) := begin erw χ_value_eq_fixed_point, split_ifs, rw h,simp,apply congr_arg nat.cast, apply fintype.card_congr, exact equiv.set.univ G, change _ = nat.cast 0, apply congr_arg nat.cast, simp, refine le_bot_iff.mp _, intro, intro hyp, simp at hyp, have r : g * a = a, exact hyp, have rr : g = 1, exact mul_left_eq_self.mp (congr_arg (has_mul.mul g) (congr_arg (has_mul.mul g) hyp)), trivial, end end Regular
56c92da824c877e6733820626c7048649a4e4cad
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/norm.lean
a4c3f9778722d4f1c461c536bd6a79be0bbcbbb2
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
13,150
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 field_theory.primitive_element import linear_algebra.determinant import linear_algebra.finite_dimensional import linear_algebra.matrix.charpoly.minpoly import linear_algebra.matrix.to_linear_equiv import field_theory.is_alg_closed.algebraic_closure import field_theory.galois /-! # Norm for (finite) ring extensions Suppose we have an `R`-algebra `S` with a finite basis. For each `s : S`, the determinant of the linear map given by multiplying by `s` gives information about the roots of the minimal polynomial of `s` over `R`. ## Implementation notes Typically, the norm is defined specifically for finite field extensions. The current definition is as general as possible and the assumption that we have fields or that the extension is finite is added to the lemmas as needed. We only define the norm for left multiplication (`algebra.left_mul_matrix`, i.e. `linear_map.mul_left`). For now, the definitions assume `S` is commutative, so the choice doesn't matter anyway. See also `algebra.trace`, which is defined similarly as the trace of `algebra.left_mul_matrix`. ## References * https://en.wikipedia.org/wiki/Field_norm -/ universes u v w variables {R S T : Type*} [comm_ring R] [comm_ring S] variables [algebra R S] variables {K L F : Type*} [field K] [field L] [field F] variables [algebra K L] [algebra K F] variables {ι : Type w} open finite_dimensional open linear_map open matrix polynomial open_locale big_operators open_locale matrix namespace algebra variables (R) /-- The norm of an element `s` of an `R`-algebra is the determinant of `(*) s`. -/ noncomputable def norm : S →* R := linear_map.det.comp (lmul R S).to_ring_hom.to_monoid_hom lemma norm_apply (x : S) : norm R x = linear_map.det (lmul R S x) := rfl lemma norm_eq_one_of_not_exists_basis (h : ¬ ∃ (s : finset S), nonempty (basis s R S)) (x : S) : norm R x = 1 := by { rw [norm_apply, linear_map.det], split_ifs with h, refl } variables {R} -- Can't be a `simp` lemma because it depends on a choice of basis lemma norm_eq_matrix_det [fintype ι] [decidable_eq ι] (b : basis ι R S) (s : S) : norm R s = matrix.det (algebra.left_mul_matrix b s) := by { rwa [norm_apply, ← linear_map.det_to_matrix b, ← to_matrix_lmul_eq], refl } /-- If `x` is in the base field `K`, then the norm is `x ^ [L : K]`. -/ lemma norm_algebra_map_of_basis [fintype ι] (b : basis ι R S) (x : R) : norm R (algebra_map R S x) = x ^ fintype.card ι := begin haveI := classical.dec_eq ι, rw [norm_apply, ← det_to_matrix b, lmul_algebra_map], convert @det_diagonal _ _ _ _ _ (λ (i : ι), x), { ext i j, rw [to_matrix_lsmul, matrix.diagonal] }, { rw [finset.prod_const, finset.card_univ] } end /-- If `x` is in the base field `K`, then the norm is `x ^ [L : K]`. (If `L` is not finite-dimensional over `K`, then `norm = 1 = x ^ 0 = x ^ (finrank L K)`.) -/ @[simp] protected lemma norm_algebra_map {K L : Type*} [field K] [comm_ring L] [algebra K L] (x : K) : norm K (algebra_map K L x) = x ^ finrank K L := begin by_cases H : ∃ (s : finset L), nonempty (basis s K L), { rw [norm_algebra_map_of_basis H.some_spec.some, finrank_eq_card_basis H.some_spec.some] }, { rw [norm_eq_one_of_not_exists_basis K H, finrank_eq_zero_of_not_exists_basis, pow_zero], rintros ⟨s, ⟨b⟩⟩, exact H ⟨s, ⟨b⟩⟩ }, end section eq_prod_roots /-- Given `pb : power_basis K S`, then the norm of `pb.gen` is `(-1) ^ pb.dim * coeff (minpoly K pb.gen) 0`. -/ lemma power_basis.norm_gen_eq_coeff_zero_minpoly [algebra K S] (pb : power_basis K S) : norm K pb.gen = (-1) ^ pb.dim * coeff (minpoly K pb.gen) 0 := begin rw [norm_eq_matrix_det pb.basis, det_eq_sign_charpoly_coeff, charpoly_left_mul_matrix, fintype.card_fin] end /-- Given `pb : power_basis K S`, then the norm of `pb.gen` is `((minpoly K pb.gen).map (algebra_map K F)).roots.prod`. -/ lemma power_basis.norm_gen_eq_prod_roots [algebra K S] (pb : power_basis K S) (hf : (minpoly K pb.gen).splits (algebra_map K F)) : algebra_map K F (norm K pb.gen) = ((minpoly K pb.gen).map (algebra_map K F)).roots.prod := begin rw [power_basis.norm_gen_eq_coeff_zero_minpoly, ← pb.nat_degree_minpoly, ring_hom.map_mul, ← coeff_map, prod_roots_eq_coeff_zero_of_monic_of_split ((minpoly.monic (power_basis.is_integral_gen _)).map _) ((splits_id_iff_splits _).2 hf), nat_degree_map, map_pow, ← mul_assoc, ← mul_pow], simp end end eq_prod_roots section eq_zero_iff variables [finite ι] lemma norm_eq_zero_iff_of_basis [is_domain R] [is_domain S] (b : basis ι R S) {x : S} : algebra.norm R x = 0 ↔ x = 0 := begin casesI nonempty_fintype ι, have hι : nonempty ι := b.index_nonempty, letI := classical.dec_eq ι, rw algebra.norm_eq_matrix_det b, split, { rw ← matrix.exists_mul_vec_eq_zero_iff, rintros ⟨v, v_ne, hv⟩, rw [← b.equiv_fun.apply_symm_apply v, b.equiv_fun_symm_apply, b.equiv_fun_apply, algebra.left_mul_matrix_mul_vec_repr] at hv, refine (mul_eq_zero.mp (b.ext_elem $ λ i, _)).resolve_right (show ∑ i, v i • b i ≠ 0, from _), { simpa only [linear_equiv.map_zero, pi.zero_apply] using congr_fun hv i }, { contrapose! v_ne with sum_eq, apply b.equiv_fun.symm.injective, rw [b.equiv_fun_symm_apply, sum_eq, linear_equiv.map_zero] } }, { rintro rfl, rw [alg_hom.map_zero, matrix.det_zero hι] }, end lemma norm_ne_zero_iff_of_basis [is_domain R] [is_domain S] (b : basis ι R S) {x : S} : algebra.norm R x ≠ 0 ↔ x ≠ 0 := not_iff_not.mpr (algebra.norm_eq_zero_iff_of_basis b) /-- See also `algebra.norm_eq_zero_iff'` if you already have rewritten with `algebra.norm_apply`. -/ @[simp] lemma norm_eq_zero_iff {K L : Type*} [field K] [comm_ring L] [algebra K L] [is_domain L] [finite_dimensional K L] {x : L} : algebra.norm K x = 0 ↔ x = 0 := algebra.norm_eq_zero_iff_of_basis (basis.of_vector_space K L) /-- This is `algebra.norm_eq_zero_iff` composed with `algebra.norm_apply`. -/ @[simp] lemma norm_eq_zero_iff' {K L : Type*} [field K] [comm_ring L] [algebra K L] [is_domain L] [finite_dimensional K L] {x : L} : linear_map.det (linear_map.mul K L x) = 0 ↔ x = 0 := algebra.norm_eq_zero_iff_of_basis (basis.of_vector_space K L) end eq_zero_iff open intermediate_field variable (K) lemma norm_eq_norm_adjoin [finite_dimensional K L] [is_separable K L] (x : L) : norm K x = norm K (adjoin_simple.gen K x) ^ finrank K⟮x⟯ L := begin letI := is_separable_tower_top_of_is_separable K K⟮x⟯ L, let pbL := field.power_basis_of_finite_of_separable K⟮x⟯ L, let pbx := intermediate_field.adjoin.power_basis (is_separable.is_integral K x), rw [← adjoin_simple.algebra_map_gen K x, norm_eq_matrix_det (pbx.basis.smul pbL.basis) _, smul_left_mul_matrix_algebra_map, det_block_diagonal, norm_eq_matrix_det pbx.basis], simp only [finset.card_fin, finset.prod_const], congr, rw [← power_basis.finrank, adjoin_simple.algebra_map_gen K x] end variable {K} section intermediate_field lemma _root_.intermediate_field.adjoin_simple.norm_gen_eq_one {x : L} (hx : ¬_root_.is_integral K x) : norm K (adjoin_simple.gen K x) = 1 := begin rw [norm_eq_one_of_not_exists_basis], contrapose! hx, obtain ⟨s, ⟨b⟩⟩ := hx, refine is_integral_of_mem_of_fg (K⟮x⟯).to_subalgebra _ x _, { exact (submodule.fg_iff_finite_dimensional _).mpr (of_fintype_basis b) }, { exact intermediate_field.subset_adjoin K _ (set.mem_singleton x) } end lemma _root_.intermediate_field.adjoin_simple.norm_gen_eq_prod_roots (x : L) (hf : (minpoly K x).splits (algebra_map K F)) : (algebra_map K F) (norm K (adjoin_simple.gen K x)) = ((minpoly K x).map (algebra_map K F)).roots.prod := begin have injKxL := (algebra_map K⟮x⟯ L).injective, by_cases hx : _root_.is_integral K x, swap, { simp [minpoly.eq_zero hx, intermediate_field.adjoin_simple.norm_gen_eq_one hx] }, have hx' : _root_.is_integral K (adjoin_simple.gen K x), { rwa [← is_integral_algebra_map_iff injKxL, adjoin_simple.algebra_map_gen], apply_instance }, rw [← adjoin.power_basis_gen hx, power_basis.norm_gen_eq_prod_roots]; rw [adjoin.power_basis_gen hx, minpoly.eq_of_algebra_map_eq injKxL hx']; try { simp only [adjoin_simple.algebra_map_gen _ _] }, exact hf end end intermediate_field section eq_prod_embeddings open intermediate_field intermediate_field.adjoin_simple polynomial lemma norm_eq_prod_embeddings_gen {K L : Type*} [field K] [comm_ring L] [algebra K L] (E : Type*) [field E] [algebra K E] (pb : power_basis K L) (hE : (minpoly K pb.gen).splits (algebra_map K E)) (hfx : (minpoly K pb.gen).separable) : algebra_map K E (norm K pb.gen) = (@@finset.univ (power_basis.alg_hom.fintype pb)).prod (λ σ, σ pb.gen) := begin letI := classical.dec_eq E, rw [power_basis.norm_gen_eq_prod_roots pb hE, fintype.prod_equiv pb.lift_equiv', finset.prod_mem_multiset, finset.prod_eq_multiset_prod, multiset.to_finset_val, multiset.dedup_eq_self.mpr, multiset.map_id], { exact nodup_roots ((separable_map _).mpr hfx) }, { intro x, refl }, { intro σ, rw [power_basis.lift_equiv'_apply_coe, id.def] } end lemma norm_eq_prod_roots [is_separable K L] [finite_dimensional K L] {x : L} (hF : (minpoly K x).splits (algebra_map K F)) : algebra_map K F (norm K x) = ((minpoly K x).map (algebra_map K F)).roots.prod ^ finrank K⟮x⟯ L := by rw [norm_eq_norm_adjoin K x, map_pow, intermediate_field.adjoin_simple.norm_gen_eq_prod_roots _ hF] variables (F) (E : Type*) [field E] [algebra K E] lemma prod_embeddings_eq_finrank_pow [algebra L F] [is_scalar_tower K L F] [is_alg_closed E] [is_separable K F] [finite_dimensional K F] (pb : power_basis K L) : ∏ σ : F →ₐ[K] E, σ (algebra_map L F pb.gen) = ((@@finset.univ (power_basis.alg_hom.fintype pb)).prod (λ σ : L →ₐ[K] E, σ pb.gen)) ^ finrank L F := begin haveI : finite_dimensional L F := finite_dimensional.right K L F, haveI : is_separable L F := is_separable_tower_top_of_is_separable K L F, letI : fintype (L →ₐ[K] E) := power_basis.alg_hom.fintype pb, letI : ∀ (f : L →ₐ[K] E), fintype (@@alg_hom L F E _ _ _ _ f.to_ring_hom.to_algebra) := _, rw [fintype.prod_equiv alg_hom_equiv_sigma (λ (σ : F →ₐ[K] E), _) (λ σ, σ.1 pb.gen), ← finset.univ_sigma_univ, finset.prod_sigma, ← finset.prod_pow], refine finset.prod_congr rfl (λ σ _, _), { letI : algebra L E := σ.to_ring_hom.to_algebra, simp only [finset.prod_const, finset.card_univ], congr, rw alg_hom.card L F E }, { intros σ, simp only [alg_hom_equiv_sigma, equiv.coe_fn_mk, alg_hom.restrict_domain, alg_hom.comp_apply, is_scalar_tower.coe_to_alg_hom'] } end variable (K) /-- For `L/K` a finite separable extension of fields and `E` an algebraically closed extension of `K`, the norm (down to `K`) of an element `x` of `L` is equal to the product of the images of `x` over all the `K`-embeddings `σ` of `L` into `E`. -/ lemma norm_eq_prod_embeddings [finite_dimensional K L] [is_separable K L] [is_alg_closed E] {x : L} : algebra_map K E (norm K x) = ∏ σ : L →ₐ[K] E, σ x := begin have hx := is_separable.is_integral K x, rw [norm_eq_norm_adjoin K x, ring_hom.map_pow, ← adjoin.power_basis_gen hx, norm_eq_prod_embeddings_gen E (adjoin.power_basis hx) (is_alg_closed.splits_codomain _)], { exact (prod_embeddings_eq_finrank_pow L E (adjoin.power_basis hx)).symm }, { haveI := is_separable_tower_bot_of_is_separable K K⟮x⟯ L, exact is_separable.separable K _ } end lemma norm_eq_prod_automorphisms [finite_dimensional K L] [is_galois K L] {x : L}: algebra_map K L (norm K x) = ∏ (σ : L ≃ₐ[K] L), σ x := begin apply no_zero_smul_divisors.algebra_map_injective L (algebraic_closure L), rw map_prod (algebra_map L (algebraic_closure L)), rw ← fintype.prod_equiv (normal.alg_hom_equiv_aut K (algebraic_closure L) L), { rw ← norm_eq_prod_embeddings, simp only [algebra_map_eq_smul_one, smul_one_smul] }, { intro σ, simp only [normal.alg_hom_equiv_aut, alg_hom.restrict_normal', equiv.coe_fn_mk, alg_equiv.coe_of_bijective, alg_hom.restrict_normal_commutes, id.map_eq_id, ring_hom.id_apply] }, end lemma is_integral_norm [algebra S L] [algebra S K] [is_scalar_tower S K L] [is_separable K L] [finite_dimensional K L] {x : L} (hx : _root_.is_integral S x) : _root_.is_integral S (norm K x) := begin have hx' : _root_.is_integral K x := is_integral_of_is_scalar_tower _ hx, rw [← is_integral_algebra_map_iff (algebra_map K (algebraic_closure L)).injective, norm_eq_prod_roots], { refine (is_integral.multiset_prod (λ y hy, _)).pow _, rw mem_roots_map (minpoly.ne_zero hx') at hy, use [minpoly S x, minpoly.monic hx], rw ← aeval_def at ⊢ hy, exact minpoly.aeval_of_is_scalar_tower S x y hy }, { apply is_alg_closed.splits_codomain }, { apply_instance } end end eq_prod_embeddings end algebra
bd3134f3e4b35df39e58b75b8ea29906d637cd10
7541ac8517945d0f903ff5397e13e2ccd7c10573
/src/category_theory/universal/comma_categories.lean
05ac651aac04a03e7340bc1ce4dbf5ed9ec1ec05
[]
no_license
ramonfmir/lean-category-theory
29b6bad9f62c2cdf7517a3135e5a12b340b4ed90
be516bcbc2dc21b99df2bcb8dde0d1e8de79c9ad
refs/heads/master
1,586,110,684,637
1,541,927,184,000
1,541,927,184,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,379
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 category_theory.functor_category -- import category_theory.walking -- import category_theory.discrete_category -- -- FIXME why do we need this here? -- @[obviously] meta def obviously_5 := tactic.tidy { tactics := extended_tidy_tactics } -- open category_theory -- open category_theory.walking -- namespace category_theory.comma -- universes j u₁ v₁ u₂ v₂ u₃ v₃ -- section -- variables (J : Type u₁) [𝒥 : category.{u₁ v₁} J] (C : Type u₂) [𝒞 : category.{u₂ v₂} C] -- include 𝒥 𝒞 -- -- The diagonal functor sends X to the constant functor that sends everything to X. -- def DiagonalFunctor : C ⥤ (J ⥤ C) := -- { obj := λ X, { obj := λ _, X, -- map' := λ _ _ _, 𝟙 X }, -- map' := λ X Y f, { app := λ _, f } } -- @[simp] lemma DiagonalFunctor_map_app {X Y : C} (f : X ⟶ Y) (j : J) : ((DiagonalFunctor J C).map f) j = f := rfl -- end -- def ObjectAsFunctor {C : Type u₃} [category.{u₃ v₃} C] (X : C) : functor.{u₃ v₃ u₃ v₃} punit C := -- { obj := λ _, X, -- map' := λ _ _ _, 𝟙 X } -- @[simp] lemma ObjectAsFunctor_map {C : Type u₃} [category.{u₃ v₃} C] (X : C) (P Q : punit) (h : @category.hom.{u₃ v₃} punit _ P Q) : @category_theory.functor.map _ _ _ _ (ObjectAsFunctor.{u₃ v₃} X) P Q h = 𝟙 X := rfl -- section -- variables {A : Type u₁} [𝒜 : category.{u₁ v₁} A] {B : Type u₂} [ℬ : category.{u₂ v₂} B] {C : Type u₃} [𝒞 : category.{u₃ v₃} C] -- include 𝒜 ℬ 𝒞 -- def comma (S : A ⥤ C) (T : B ⥤ C) : Type (max u₁ u₂ v₃) := Σ p : A × B, (S p.1) ⟶ (T p.2) -- structure comma_morphism {S : A ⥤ C} {T : B ⥤ C} (p q : comma S T) : Type (max v₁ v₂):= -- (left : p.1.1 ⟶ q.1.1) -- (right : p.1.2 ⟶ q.1.2) -- (condition' : (S.map left) ≫ q.2 = p.2 ≫ (T.map right) . obviously) -- restate_axiom comma_morphism.condition' -- attribute [search] comma_morphism.condition -- @[extensionality] lemma comma_morphism_equal -- {S : A ⥤ C} {T : B ⥤ C} {p q : comma S T} (f g : comma_morphism p q) -- (wl : f.left = g.left) (wr : f.right = g.right) : f = g := -- begin -- induction f, -- induction g, -- tidy, -- end -- instance CommaCategory (S : A ⥤ C) (T : B ⥤ C) : category.{(max u₁ u₂ v₃) (max v₁ v₂)} (comma S T) := -- { hom := λ p q, comma_morphism p q, -- id := λ p, ⟨ 𝟙 p.1.1, 𝟙 p.1.2, by obviously ⟩, -- comp := λ p q r f g, ⟨ f.left ≫ g.left, f.right ≫ g.right, by obviously ⟩ } -- -- cf Leinster Remark 2.3.2 -- def CommaCategory_left_projection (S : A ⥤ C) (T : B ⥤ C) : (comma S T) ⥤ A := -- { obj := λ X, X.1.1, -- map' := λ _ _ f, f.left } -- def CommaCategory_right_projection (S : A ⥤ C) (T : B ⥤ C) : (comma S T) ⥤ B := -- { obj := λ X, X.1.2, -- map' := λ _ _ f, f.right } -- def CommaCategory_projection_transformation (S : A ⥤ C) (T : B ⥤ C) : ((CommaCategory_left_projection S T) ⋙ S) ⟹ ((CommaCategory_right_projection S T) ⋙ T) := -- { app := λ X, X.2 } -- -- TODO show these agree with the explicitly defined `over` and `under` categories. -- -- Notice that if C is large, these are large, and if C is small, these are small. -- def SliceCategory (X : C) : category.{(max u₃ v₃) v₃} (comma (functor.id C) (ObjectAsFunctor X)) := by apply_instance -- def CosliceCategory (X : C) : category.{(max u₃ v₃) v₃} (comma (ObjectAsFunctor X) (functor.id C)) := by apply_instance -- end -- -- In Cones, we have -- -- A = C -- -- B = . -- -- C = FunctorCategory J C -- variable {J : Type v₁} -- variable [small_category J] -- variable {C : Type u₁} -- variable [𝒞 : category.{u₁ v₁} C] -- include 𝒞 -- def Cone (F : J ⥤ C) := -- (comma (DiagonalFunctor.{v₁ v₁ u₁ v₁} J C) (ObjectAsFunctor F)) -- def Cocone (F : J ⥤ C) := -- (comma (ObjectAsFunctor F) (DiagonalFunctor.{v₁ v₁ u₁ v₁} J C)). -- @[search] lemma Cone.pointwise_condition {F : J ⥤ C} (X Y : Cone F) (f : comma_morphism X Y) (j : J) : f.left ≫ (Y.snd) j = (X.snd) j := -- begin -- have p := f.condition, -- have p' := congr_arg nat_trans.app p, -- have p'' := congr_fun p' j, -- simp at p'', -- erw category.comp_id at p'', -- exact p'' -- end -- @[simp] lemma Cone_comma_unit (F : J ⥤ C) (X : Cone F) : X.1.2 = punit.star := by obviously -- @[simp] lemma Cocone_comma_unit (F : J ⥤ C) (X : Cocone F) : X.1.1 = punit.star := by obviously -- instance Cones (F : J ⥤ C) : category (Cone F) := begin unfold Cone, apply_instance end -- instance Cocones (F : J ⥤ C) : category (Cocone F) := begin unfold Cocone, apply_instance end -- -- def Limit (F: J ⥤ C) := terminal_object (Cone F) -- -- def Colimit (F: J ⥤ C) := initial_object (Cocone F) -- -- def BinaryProduct (α β : C) := Limit (Pair_functor.{u₁ v₁} α β) -- -- def BinaryCoproduct (α β : C) := Colimit (Pair_functor α β) -- -- def Equalizer {α β : C} (f g : α ⟶ β) := Limit (ParallelPair_functor f g) -- -- def Coequalizer {α β : C} (f g : α ⟶ β) := Colimit (ParallelPair_functor f g) -- end category_theory.comma
453be36555a9641ec68fa269970e7ef86ab4e072
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/data/multiset/nodup.lean
3228ef95e41730e6fcfa731dee0121eabec9f829
[ "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
9,027
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Mario Carneiro -/ import data.multiset.powerset import data.multiset.range /-! # The `nodup` predicate for multisets without duplicate elements. -/ namespace multiset open list variables {α β γ : Type*} /- nodup -/ /-- `nodup s` means that `s` has no duplicates, i.e. the multiplicity of any element is at most 1. -/ def nodup (s : multiset α) : Prop := quot.lift_on s nodup (λ s t p, propext p.nodup_iff) @[simp] theorem coe_nodup {l : list α} : @nodup α l ↔ l.nodup := iff.rfl @[simp] theorem nodup_zero : @nodup α 0 := pairwise.nil @[simp] theorem nodup_cons {a : α} {s : multiset α} : nodup (a::s) ↔ a ∉ s ∧ nodup s := quot.induction_on s $ λ l, nodup_cons theorem nodup_cons_of_nodup {a : α} {s : multiset α} (m : a ∉ s) (n : nodup s) : nodup (a::s) := nodup_cons.2 ⟨m, n⟩ theorem nodup_singleton : ∀ a : α, nodup (a::0) := nodup_singleton theorem nodup_of_nodup_cons {a : α} {s : multiset α} (h : nodup (a::s)) : nodup s := (nodup_cons.1 h).2 theorem not_mem_of_nodup_cons {a : α} {s : multiset α} (h : nodup (a::s)) : a ∉ s := (nodup_cons.1 h).1 theorem nodup_of_le {s t : multiset α} (h : s ≤ t) : nodup t → nodup s := le_induction_on h $ λ l₁ l₂, nodup_of_sublist theorem not_nodup_pair : ∀ a : α, ¬ nodup (a::a::0) := not_nodup_pair theorem nodup_iff_le {s : multiset α} : nodup s ↔ ∀ a : α, ¬ a::a::0 ≤ s := quot.induction_on s $ λ l, nodup_iff_sublist.trans $ forall_congr $ λ a, not_congr (@repeat_le_coe _ a 2 _).symm theorem nodup_iff_count_le_one [decidable_eq α] {s : multiset α} : nodup s ↔ ∀ a, count a s ≤ 1 := quot.induction_on s $ λ l, nodup_iff_count_le_one @[simp] theorem count_eq_one_of_mem [decidable_eq α] {a : α} {s : multiset α} (d : nodup s) (h : a ∈ s) : count a s = 1 := le_antisymm (nodup_iff_count_le_one.1 d a) (count_pos.2 h) lemma pairwise_of_nodup {r : α → α → Prop} {s : multiset α} : (∀a∈s, ∀b∈s, a ≠ b → r a b) → nodup s → pairwise r s := quotient.induction_on s $ assume l h hl, ⟨l, rfl, hl.imp_of_mem $ assume a b ha hb, h a ha b hb⟩ lemma forall_of_pairwise {r : α → α → Prop} (H : symmetric r) {s : multiset α} (hs : pairwise r s) : (∀a∈s, ∀b∈s, a ≠ b → r a b) := let ⟨l, hl₁, hl₂⟩ := hs in hl₁.symm ▸ list.forall_of_pairwise H hl₂ theorem nodup_add {s t : multiset α} : nodup (s + t) ↔ nodup s ∧ nodup t ∧ disjoint s t := quotient.induction_on₂ s t $ λ l₁ l₂, nodup_append theorem disjoint_of_nodup_add {s t : multiset α} (d : nodup (s + t)) : disjoint s t := (nodup_add.1 d).2.2 theorem nodup_add_of_nodup {s t : multiset α} (d₁ : nodup s) (d₂ : nodup t) : nodup (s + t) ↔ disjoint s t := by simp [nodup_add, d₁, d₂] theorem nodup_of_nodup_map (f : α → β) {s : multiset α} : nodup (map f s) → nodup s := quot.induction_on s $ λ l, nodup_of_nodup_map f theorem nodup_map_on {f : α → β} {s : multiset α} : (∀x∈s, ∀y∈s, f x = f y → x = y) → nodup s → nodup (map f s) := quot.induction_on s $ λ l, nodup_map_on theorem nodup_map {f : α → β} {s : multiset α} (hf : function.injective f) : nodup s → nodup (map f s) := nodup_map_on (λ x _ y _ h, hf h) theorem nodup_filter (p : α → Prop) [decidable_pred p] {s} : nodup s → nodup (filter p s) := quot.induction_on s $ λ l, nodup_filter p @[simp] theorem nodup_attach {s : multiset α} : nodup (attach s) ↔ nodup s := quot.induction_on s $ λ l, nodup_attach theorem nodup_pmap {p : α → Prop} {f : Π a, p a → β} {s : multiset α} {H} (hf : ∀ a ha b hb, f a ha = f b hb → a = b) : nodup s → nodup (pmap f s H) := quot.induction_on s (λ l H, nodup_pmap hf) H instance nodup_decidable [decidable_eq α] (s : multiset α) : decidable (nodup s) := quotient.rec_on_subsingleton s $ λ l, l.nodup_decidable theorem nodup_erase_eq_filter [decidable_eq α] (a : α) {s} : nodup s → s.erase a = filter (≠ a) s := quot.induction_on s $ λ l d, congr_arg coe $ nodup_erase_eq_filter a d theorem nodup_erase_of_nodup [decidable_eq α] (a : α) {l} : nodup l → nodup (l.erase a) := nodup_of_le (erase_le _ _) theorem mem_erase_iff_of_nodup [decidable_eq α] {a b : α} {l} (d : nodup l) : a ∈ l.erase b ↔ a ≠ b ∧ a ∈ l := by rw nodup_erase_eq_filter b d; simp [and_comm] theorem mem_erase_of_nodup [decidable_eq α] {a : α} {l} (h : nodup l) : a ∉ l.erase a := by rw mem_erase_iff_of_nodup h; simp theorem nodup_product {s : multiset α} {t : multiset β} : nodup s → nodup t → nodup (product s t) := quotient.induction_on₂ s t $ λ l₁ l₂ d₁ d₂, by simp [nodup_product d₁ d₂] theorem nodup_sigma {σ : α → Type*} {s : multiset α} {t : Π a, multiset (σ a)} : nodup s → (∀ a, nodup (t a)) → nodup (s.sigma t) := quot.induction_on s $ assume l₁, begin choose f hf using assume a, quotient.exists_rep (t a), rw show t = λ a, f a, from (eq.symm $ funext $ λ a, hf a), simpa using nodup_sigma end theorem nodup_filter_map (f : α → option β) {s : multiset α} (H : ∀ (a a' : α) (b : β), b ∈ f a → b ∈ f a' → a = a') : nodup s → nodup (filter_map f s) := quot.induction_on s $ λ l, nodup_filter_map H theorem nodup_range (n : ℕ) : nodup (range n) := nodup_range _ theorem nodup_inter_left [decidable_eq α] {s : multiset α} (t) : nodup s → nodup (s ∩ t) := nodup_of_le $ inter_le_left _ _ theorem nodup_inter_right [decidable_eq α] (s) {t : multiset α} : nodup t → nodup (s ∩ t) := nodup_of_le $ inter_le_right _ _ @[simp] theorem nodup_union [decidable_eq α] {s t : multiset α} : nodup (s ∪ t) ↔ nodup s ∧ nodup t := ⟨λ h, ⟨nodup_of_le (le_union_left _ _) h, nodup_of_le (le_union_right _ _) h⟩, λ ⟨h₁, h₂⟩, nodup_iff_count_le_one.2 $ λ a, by rw [count_union]; exact max_le (nodup_iff_count_le_one.1 h₁ a) (nodup_iff_count_le_one.1 h₂ a)⟩ @[simp] theorem nodup_powerset {s : multiset α} : nodup (powerset s) ↔ nodup s := ⟨λ h, nodup_of_nodup_map _ (nodup_of_le (map_single_le_powerset _) h), quotient.induction_on s $ λ l h, by simp; refine list.nodup_map_on _ (nodup_sublists'.2 h); exact λ x sx y sy e, (h.sublist_ext (mem_sublists'.1 sx) (mem_sublists'.1 sy)).1 (quotient.exact e)⟩ theorem nodup_powerset_len {n : ℕ} {s : multiset α} (h : nodup s) : nodup (powerset_len n s) := nodup_of_le (powerset_len_le_powerset _ _) (nodup_powerset.2 h) @[simp] lemma nodup_bind {s : multiset α} {t : α → multiset β} : nodup (bind s t) ↔ ((∀a∈s, nodup (t a)) ∧ (s.pairwise (λa b, disjoint (t a) (t b)))) := have h₁ : ∀a, ∃l:list β, t a = l, from assume a, quot.induction_on (t a) $ assume l, ⟨l, rfl⟩, let ⟨t', h'⟩ := classical.axiom_of_choice h₁ in have t = λa, t' a, from funext h', have hd : symmetric (λa b, list.disjoint (t' a) (t' b)), from assume a b h, h.symm, quot.induction_on s $ by simp [this, list.nodup_bind, pairwise_coe_iff_pairwise hd] theorem nodup_ext {s t : multiset α} : nodup s → nodup t → (s = t ↔ ∀ a, a ∈ s ↔ a ∈ t) := quotient.induction_on₂ s t $ λ l₁ l₂ d₁ d₂, quotient.eq.trans $ perm_ext d₁ d₂ theorem le_iff_subset {s t : multiset α} : nodup s → (s ≤ t ↔ s ⊆ t) := quotient.induction_on₂ s t $ λ l₁ l₂ d, ⟨subset_of_le, subperm_of_subset_nodup d⟩ theorem range_le {m n : ℕ} : range m ≤ range n ↔ m ≤ n := (le_iff_subset (nodup_range _)).trans range_subset theorem mem_sub_of_nodup [decidable_eq α] {a : α} {s t : multiset α} (d : nodup s) : a ∈ s - t ↔ a ∈ s ∧ a ∉ t := ⟨λ h, ⟨mem_of_le (sub_le_self _ _) h, λ h', by refine count_eq_zero.1 _ h; rw [count_sub a s t, nat.sub_eq_zero_iff_le]; exact le_trans (nodup_iff_count_le_one.1 d _) (count_pos.2 h')⟩, λ ⟨h₁, h₂⟩, or.resolve_right (mem_add.1 $ mem_of_le (le_sub_add _ _) h₁) h₂⟩ lemma map_eq_map_of_bij_of_nodup (f : α → γ) (g : β → γ) {s : multiset α} {t : multiset β} (hs : s.nodup) (ht : t.nodup) (i : Πa∈s, β) (hi : ∀a ha, i a ha ∈ t) (h : ∀a ha, f a = g (i a ha)) (i_inj : ∀a₁ a₂ ha₁ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀b∈t, ∃a ha, b = i a ha) : s.map f = t.map g := have t = s.attach.map (λ x, i x.1 x.2), from (nodup_ext ht (nodup_map (show function.injective (λ x : {x // x ∈ s}, i x.1 x.2), from λ x y hxy, subtype.eq (i_inj x.1 y.1 x.2 y.2 hxy)) (nodup_attach.2 hs))).2 (λ x, by simp only [mem_map, true_and, subtype.exists, eq_comm, mem_attach]; exact ⟨i_surj _, λ ⟨y, hy⟩, hy.snd.symm ▸ hi _ _⟩), calc s.map f = s.pmap (λ x _, f x) (λ _, id) : by rw [pmap_eq_map] ... = s.attach.map (λ x, f x.1) : by rw [pmap_eq_map_attach] ... = t.map g : by rw [this, multiset.map_map]; exact map_congr (λ x _, h _ _) end multiset
d152879251ee3ce89fbe9f5338250cc4e6686a43
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/run/LiftMethodIssue.lean
250cf06296d7fed361672517ed68b23ef4969e98
[ "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
743
lean
def tst : IO (Option Nat) := do let x? : Option Nat ← pure none; pure x? def tst2 (x : Nat) : IO (Option Nat) := do let x? : Option Nat := x; if x?.isNone then /- We need the `some` because we propagate the expected type at `pure` applications. The expected type is `IO (Option Nat)`, and we elaborate `x+1` with expected type `Option Nat`, which forces us the elaborator (to try) to synthesize `[Add (Option Nat)]`. If we disable expected type propagation for `pure` we can elaborate it without `some`. The `x+1` will be elaborated without an expected type. We will infer the type `?m Nat` for `pure (x+1)`, and coercions are used to convert it into `IO (Option Nat)`. -/ return some (x+1) else return x?
abdf8bcf6f3e63c8936bd5c3d33c7bbf35494f30
0003047346476c031128723dfd16fe273c6bc605
/src/linear_algebra/basic.lean
1bd9ac051860a5474bcf26057255f0d646239853
[ "Apache-2.0" ]
permissive
ChandanKSingh/mathlib
d2bf4724ccc670bf24915c12c475748281d3fb73
d60d1616958787ccb9842dc943534f90ea0bab64
refs/heads/master
1,588,238,823,679
1,552,867,469,000
1,552,867,469,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
57,362
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, Kevin Buzzard Basics of linear algebra. This sets up the "categorical/lattice structure" of modules, submodules, and linear maps. -/ import algebra.pi_instances data.finsupp order.order_iso open function lattice reserve infix `≃ₗ` : 50 universes u v w x y z variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type y} {ε : Type z} {ι : Type x} namespace finset lemma smul_sum [ring γ] [add_comm_group β] [module γ β] {s : finset α} {a : γ} {f : α → β} : a • (s.sum f) = s.sum (λc, a • f c) := (finset.sum_hom ((•) a)).symm end finset namespace finsupp lemma smul_sum [has_zero β] [ring γ] [add_comm_group δ] [module γ δ] {v : α →₀ β} {c : γ} {h : α → β → δ} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum end finsupp namespace linear_map section variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] [add_comm_group ε] variables [module α β] [module α γ] [module α δ] [module α ε] variables (f g : β →ₗ[α] γ) include α @[simp] theorem comp_id : f.comp id = f := linear_map.ext $ λ x, rfl @[simp] theorem id_comp : id.comp f = f := linear_map.ext $ λ x, rfl theorem comp_assoc (g : γ →ₗ[α] δ) (h : δ →ₗ[α] ε) : (h.comp g).comp f = h.comp (g.comp f) := rfl def cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h : ∀c, f c ∈ p) : γ →ₗ[α] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule α β) (f : γ →ₗ[α] β) {h} (x : γ) : (cod_restrict p f h x : β) = f x := rfl @[simp] lemma comp_cod_restrict (p : submodule α γ) (h : ∀b, f b ∈ p) (g : δ →ₗ[α] β) : (cod_restrict p f h).comp g = cod_restrict p (f.comp g) (assume b, h _) := ext $ assume b, rfl @[simp] lemma subtype_comp_cod_restrict (p : submodule α γ) (h : ∀b, f b ∈ p) : p.subtype.comp (cod_restrict p f h) = f := ext $ assume b, rfl def inverse (g : γ → β) (h₁ : left_inverse g f) (h₂ : right_inverse g f) : γ →ₗ[α] β := by dsimp [left_inverse, function.right_inverse] at h₁ h₂; exact ⟨g, λ x y, by rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [h₂], λ a b, by rw [← h₁ (g (a • b)), ← h₁ (a • g b)]; simp [h₂]⟩ instance : has_zero (β →ₗ[α] γ) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ @[simp] lemma zero_apply (x : β) : (0 : β →ₗ[α] γ) x = 0 := rfl instance : has_neg (β →ₗ[α] γ) := ⟨λ f, ⟨λ b, - f b, by simp, by simp⟩⟩ @[simp] lemma neg_apply (x : β) : (- f) x = - f x := rfl instance : has_add (β →ₗ[α] γ) := ⟨λ f g, ⟨λ b, f b + g b, by simp, by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : β) : (f + g) x = f x + g x := rfl instance : add_comm_group (β →ₗ[α] γ) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp instance linear_map.is_add_group_hom : is_add_group_hom f := by refine_struct {..}; simp instance linear_map_apply_is_add_group_hom (a : β) : is_add_group_hom (λ f : β →ₗ[α] γ, f a) := by refine_struct {..}; simp lemma sum_apply [decidable_eq δ] (t : finset δ) (f : δ → β →ₗ[α] γ) (b : β) : t.sum f b = t.sum (λd, f d b) := (@finset.sum_hom _ _ _ t f _ _ (λ g : β →ₗ[α] γ, g b) _).symm @[simp] lemma sub_apply (x : β) : (f - g) x = f x - g x := rfl def smul_right (f : γ →ₗ[α] α) (x : β) : γ →ₗ[α] β := ⟨λb, f b • x, by simp [add_smul], by simp [smul_smul]⟩. @[simp] theorem smul_right_apply (f : γ →ₗ[α] α) (x : β) (c : γ) : (smul_right f x : γ → β) c = f c • x := rfl instance : has_one (β →ₗ[α] β) := ⟨linear_map.id⟩ instance : has_mul (β →ₗ[α] β) := ⟨linear_map.comp⟩ @[simp] lemma one_app (x : β) : (1 : β →ₗ[α] β) x = x := rfl @[simp] lemma mul_app (A B : β →ₗ[α] β) (x : β) : (A * B) x = A (B x) := rfl @[simp] theorem comp_zero : f.comp (0 : δ →ₗ[α] β) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero] @[simp] theorem zero_comp : (0 : γ →ₗ[α] δ).comp f = 0 := rfl section variables (α β) include β instance endomorphism_ring : ring (β →ₗ[α] β) := by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..}; { intros, apply linear_map.ext, simp } /-- The group of invertible linear maps from `β` to itself -/ def general_linear_group := units (β →ₗ[α] β) end section variables (α β γ) def fst : β × γ →ₗ[α] β := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩ def snd : β × γ →ₗ[α] γ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩ end @[simp] theorem fst_apply (x : β × γ) : fst α β γ x = x.1 := rfl @[simp] theorem snd_apply (x : β × γ) : snd α β γ x = x.2 := rfl def pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : β →ₗ[α] γ × δ := ⟨λ x, (f x, g x), λ x y, by simp, λ x y, by simp⟩ @[simp] theorem pair_apply (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (x : β) : pair f g x = (f x, g x) := rfl @[simp] theorem fst_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : (fst α γ δ).comp (pair f g) = f := by ext; refl @[simp] theorem snd_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : (snd α γ δ).comp (pair f g) = g := by ext; refl @[simp] theorem pair_fst_snd : pair (fst α β γ) (snd α β γ) = linear_map.id := by ext; refl section variables (α β γ) def inl : β →ₗ[α] β × γ := by refine ⟨prod.inl, _, _⟩; intros; simp [prod.inl] def inr : γ →ₗ[α] β × γ := by refine ⟨prod.inr, _, _⟩; intros; simp [prod.inr] end @[simp] theorem inl_apply (x : β) : inl α β γ x = (x, 0) := rfl @[simp] theorem inr_apply (x : γ) : inr α β γ x = (0, x) := rfl def copair (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : β × γ →ₗ[α] δ := ⟨λ x, f x.1 + g x.2, λ x y, by simp, λ x y, by simp [smul_add]⟩ @[simp] theorem copair_apply (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (x : β) (y : γ) : copair f g (x, y) = f x + g y := rfl @[simp] theorem copair_inl (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : (copair f g).comp (inl α β γ) = f := by ext; simp @[simp] theorem copair_inr (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : (copair f g).comp (inr α β γ) = g := by ext; simp @[simp] theorem copair_inl_inr : copair (inl α β γ) (inr α β γ) = linear_map.id := by ext ⟨x, y⟩; simp theorem fst_eq_copair : fst α β γ = copair linear_map.id 0 := by ext ⟨x, y⟩; simp theorem snd_eq_copair : snd α β γ = copair 0 linear_map.id := by ext ⟨x, y⟩; simp theorem inl_eq_pair : inl α β γ = pair linear_map.id 0 := rfl theorem inr_eq_pair : inr α β γ = pair 0 linear_map.id := rfl end section comm_ring variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f g : β →ₗ[α] γ) include α instance : has_scalar α (β →ₗ[α] γ) := ⟨λ a f, ⟨λ b, a • f b, by simp [smul_add], by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (a : α) (x : β) : (a • f) x = a • f x := rfl instance : module α (β →ₗ[α] γ) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] def congr_right (f : γ →ₗ[α] δ) : (β →ₗ[α] γ) →ₗ[α] (β →ₗ[α] δ) := ⟨linear_map.comp f, λ _ _, linear_map.ext $ λ _, f.2 _ _, λ _ _, linear_map.ext $ λ _, f.3 _ _⟩ theorem smul_comp (g : γ →ₗ[α] δ) (a : α) : (a • g).comp f = a • (g.comp f) := rfl theorem comp_smul (g : γ →ₗ[α] δ) (a : α) : g.comp (a • f) = a • (g.comp f) := ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl end comm_ring end linear_map namespace submodule variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (p p' : submodule α β) (q q' : submodule α γ) variables {r : α} {x y : β} open set lattice instance : partial_order (submodule α β) := partial_order.lift (coe : submodule α β → set β) $ λ a b, ext' lemma le_def {p p' : submodule α β} : p ≤ p' ↔ (p : set β) ⊆ p' := iff.rfl lemma le_def' {p p' : submodule α β} : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl def of_le {p p' : submodule α β} (h : p ≤ p') : p →ₗ[α] p' := linear_map.cod_restrict _ p.subtype $ λ ⟨x, hx⟩, h hx @[simp] theorem of_le_apply {p p' : submodule α β} (h : p ≤ p') (x : p) : (of_le h x : β) = x := rfl lemma subtype_comp_of_le (p q : submodule α β) (h : p ≤ q) : (submodule.subtype q).comp (of_le h) = submodule.subtype p := by ext ⟨b, hb⟩; simp instance : has_bot (submodule α β) := ⟨by split; try {exact {0}}; simp {contextual := tt}⟩ @[simp] lemma bot_coe : ((⊥ : submodule α β) : set β) = {0} := rfl section variables (α) @[simp] lemma mem_bot : x ∈ (⊥ : submodule α β) ↔ x = 0 := mem_singleton_iff end instance : order_bot (submodule α β) := { bot := ⊥, bot_le := λ p x, by simp {contextual := tt}, ..submodule.partial_order } instance : has_top (submodule α β) := ⟨by split; try {exact set.univ}; simp⟩ @[simp] lemma top_coe : ((⊤ : submodule α β) : set β) = univ := rfl @[simp] lemma mem_top : x ∈ (⊤ : submodule α β) := trivial instance : order_top (submodule α β) := { top := ⊤, le_top := λ p x _, trivial, ..submodule.partial_order } instance : has_Inf (submodule α β) := ⟨λ S, { carrier := ⋂ s ∈ S, ↑s, zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule α β)} {p} : p ∈ S → Inf S ≤ p := bInter_subset_of_mem private lemma le_Inf' {S : set (submodule α β)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S := subset_bInter instance : has_inf (submodule α β) := ⟨λ p p', { carrier := p ∩ p', zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule α β) := { sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha, le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb, sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c, subset_inter, inf_le_left := λ a b, inter_subset_left _ _, inf_le_right := λ a b, inter_subset_right _ _, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs, Sup_le := λ s p hs, Inf_le' hs, Inf := Inf, le_Inf := λ s a, le_Inf', Inf_le := λ s a, Inf_le', ..submodule.lattice.order_top, ..submodule.lattice.order_bot } instance : add_comm_monoid (submodule α β) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm } @[simp] lemma add_eq_sup (M N : submodule α β) : M + N = M ⊔ N := rfl @[simp] lemma zero_eq_bot : (0 : submodule α β) = ⊥ := rfl lemma eq_top_iff' {p : submodule α β} : p = ⊤ ↔ ∀ x, x ∈ p := eq_top_iff.trans ⟨λ h x, @h x trivial, λ h x _, h x⟩ @[simp] theorem inf_coe : (p ⊓ p' : set β) = p ∩ p' := rfl @[simp] theorem mem_inf {p p' : submodule α β} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[simp] theorem Inf_coe (P : set (submodule α β)) : (↑(Inf P) : set β) = ⋂ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ι} (p : ι → submodule α β) : (↑⨅ i, p i : set β) = ⋂ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩ @[simp] theorem mem_infi {ι} (p : ι → submodule α β) : x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i := by rw [← mem_coe, infi_coe, mem_Inter]; refl theorem disjoint_def {p p' : submodule α β} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:β) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set β)) ↔ _, by simp /-- The pushforward -/ def map (f : β →ₗ[α] γ) (p : submodule α β) : submodule α γ := { carrier := f '' p, zero := ⟨0, p.zero_mem, f.map_zero⟩, add := by rintro _ _ ⟨b₁, hb₁, rfl⟩ ⟨b₂, hb₂, rfl⟩; exact ⟨_, p.add_mem hb₁ hb₂, f.map_add _ _⟩, smul := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩ } lemma map_coe (f : β →ₗ[α] γ) (p : submodule α β) : (map f p : set γ) = f '' p := rfl @[simp] lemma mem_map {f : β →ₗ[α] γ} {p : submodule α β} {x : γ} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : β →ₗ[α] γ} {p : submodule α β} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma map_id : map linear_map.id p = p := submodule.ext $ λ a, by simp lemma map_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α β) : map (g.comp f) p = map g (map f p) := submodule.ext' $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : β →ₗ[α] γ} {p p' : submodule α β} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : β →ₗ[α] γ) p = ⊥ := have ∃ (x : β), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] /-- The pullback -/ def comap (f : β →ₗ[α] γ) (p : submodule α γ) : submodule α β := { carrier := f ⁻¹' p, zero := by simp, add := λ x y h₁ h₂, by simp [p.add_mem h₁ h₂], smul := λ a x h, by simp [p.smul_mem _ h] } @[simp] lemma comap_coe (f : β →ₗ[α] γ) (p : submodule α γ) : (comap f p : set β) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : β →ₗ[α] γ} {p : submodule α γ} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := submodule.ext' rfl lemma comap_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α δ) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : β →ₗ[α] γ} {q q' : submodule α γ} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono lemma map_le_iff_le_comap {f : β →ₗ[α] γ} {p : submodule α β} {q : submodule α γ} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : β →ₗ[α] γ) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : β →ₗ[α] γ) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : β →ₗ[α] γ) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ι : Sort*} (f : β →ₗ[α] γ) (p : ι → submodule α β) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr @[simp] lemma comap_top (f : β →ₗ[α] γ) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : β →ₗ[α] γ) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi {ι : Sort*} (f : β →ₗ[α] γ) (p : ι → submodule α γ) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : β →ₗ[α] γ) q = ⊤ := ext $ by simp lemma map_comap_le (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map (f : β →ₗ[α] γ) (p : submodule α β) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap {f : β →ₗ[α] γ} {p : submodule α β} {p' : submodule α γ} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ lemma eq_zero_of_bot_submodule : ∀(b : (⊥ : submodule α β)), b = 0 | ⟨b', hb⟩ := subtype.eq $ show b' = 0, from (mem_bot α).1 hb section variables (α) def span (s : set β) : submodule α β := Inf {p | s ⊆ p} end variables {s t : set β} lemma mem_span : x ∈ span α s ↔ ∀ p : submodule α β, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span α s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span α s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span α s ≤ span α t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span α s) : span α s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span α (p : set β) = p := span_eq_of_le _ (subset.refl _) subset_span @[elab_as_eliminator] lemma span_induction {p : β → Prop} (h : x ∈ span α s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:α) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h section variables (α β) protected def gi : galois_insertion (@span α β _ _ _) coe := { choice := λ s _, span α s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span α (∅ : set β) = ⊥ := (submodule.gi α β).gc.l_bot @[simp] lemma span_univ : span α (univ : set β) = ⊤ := eq_top_iff.2 $ le_def.2 $ subset_span lemma span_union (s t : set β) : span α (s ∪ t) = span α s ⊔ span α t := (submodule.gi α β).gc.l_sup lemma span_Union {ι} (s : ι → set β) : span α (⋃ i, s i) = ⨆ i, span α (s i) := (submodule.gi α β).gc.l_supr @[simp] theorem Union_coe_of_directed {ι} (hι : nonempty ι) (S : ι → submodule α β) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) : ((supr S : submodule α β) : set β) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), rw [show supr S = ⨆ i, span α (S i), by simp, ← span_Union], unfreezeI, refine λ x hx, span_induction hx (λ _, id) _ _ _, { cases hι with i, exact mem_Union.2 ⟨i, by simp⟩ }, { simp, intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { simp [-mem_coe]; exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end lemma mem_supr_of_mem {ι : Sort*} {b : β} (p : ι → submodule α β) (i : ι) (h : b ∈ p i) : b ∈ (⨆i, p i) := have p i ≤ (⨆i, p i) := le_supr p i, @this b h @[simp] theorem mem_supr_of_directed {ι} (hι : nonempty ι) (S : ι → submodule α β) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by rw [← mem_coe, Union_coe_of_directed hι S H, mem_Union]; refl theorem mem_Sup_of_directed {s : set (submodule α β)} {z} (hzs : z ∈ Sup s) (x ∈ s) (hdir : ∀ i ∈ s, ∀ j ∈ s, ∃ k ∈ s, i ≤ k ∧ j ≤ k) : ∃ y ∈ s, z ∈ y := begin haveI := classical.dec, rw Sup_eq_supr at hzs, have : ∃ (i : submodule α β), z ∈ ⨆ (H : i ∈ s), i, { refine (mem_supr_of_directed ⟨⊥⟩ _ (λ i j, _)).1 hzs, by_cases his : i ∈ s; by_cases hjs : j ∈ s, { rcases hdir i his j hjs with ⟨k, hks, hik, hjk⟩, exact ⟨k, le_supr_of_le hks (supr_le $ λ _, hik), le_supr_of_le hks (supr_le $ λ _, hjk)⟩ }, { exact ⟨i, le_refl _, supr_le $ hjs.elim⟩ }, { exact ⟨j, supr_le $ his.elim, le_refl _⟩ }, { exact ⟨⊥, supr_le $ his.elim, supr_le $ hjs.elim⟩ } }, cases this with N hzn, by_cases hns : N ∈ s, { have : (⨆ (H : N ∈ s), N) ≤ N := supr_le (λ _, le_refl _), exact ⟨N, hns, this hzn⟩ }, { have : (⨆ (H : N ∈ s), N) ≤ ⊥ := supr_le hns.elim, cases (mem_bot α).1 (this hzn), exact ⟨x, H, x.zero_mem⟩ } end section variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ end lemma mem_span_singleton {y : β} : x ∈ span α ({y} : set β) ↔ ∃ a:α, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma span_singleton_eq_range (y : β) : (span α ({y} : set β) : set β) = range ((• y) : α → β) := set.ext $ λ x, mem_span_singleton lemma mem_span_insert {y} : x ∈ span α (insert y s) ↔ ∃ (a:α) (z ∈ span α s), x = a • y + z := begin rw [← union_singleton, span_union, mem_sup], simp [mem_span_singleton], split, { rintro ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩, exact ⟨a, z, hz, rfl⟩ }, { rintro ⟨a, z, hz, rfl⟩, exact ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩ } end lemma mem_span_insert' {y} : x ∈ span α (insert y s) ↔ ∃(a:α), x + a • y ∈ span α s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp⟩ } end lemma span_insert_eq_span (h : x ∈ span α s) : span α (insert x s) = span α s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span α (span α s : set β) = span α s := span_eq _ lemma span_eq_bot : span α (s : set β) = ⊥ ↔ ∀ x ∈ s, (x:β) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot α).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot α).2 $ H x h)⟩ lemma span_singleton_eq_bot : span α ({x} : set β) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_image (f : β →ₗ[α] γ) : span α (f '' s) = map f (span α s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span def prod : submodule α (β × γ) := { carrier := set.prod p q, zero := ⟨zero_mem _, zero_mem _⟩, add := by rintro ⟨x₁, y₁⟩ ⟨x₂, y₂⟩ ⟨hx₁, hy₁⟩ ⟨hx₂, hy₂⟩; exact ⟨add_mem _ hx₁ hx₂, add_mem _ hy₁ hy₂⟩, smul := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩ } @[simp] lemma prod_coe : (prod p q : set (β × γ)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule α β} {q : submodule α γ} {x : β × γ} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set β) (t : set γ) : span α (set.prod s t) ≤ prod (span α s) (span α t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule α (β × γ)) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule α (β × γ)) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule α β} {q q' : submodule α γ} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q ⊓ prod p' q' = prod (p ⊓ p') (q ⊓ q') := ext' set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q ⊔ prod p' q' = prod (p ⊔ p') (q ⊔ q') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [le_def'], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end -- TODO(Mario): Factor through add_subgroup def quotient_rel : setoid β := ⟨λ x y, x - y ∈ p, λ x, by simp, λ x y h, by simpa using neg_mem _ h, λ x y z h₁ h₂, by simpa using add_mem _ h₁ h₂⟩ def quotient : Type* := quotient (quotient_rel p) namespace quotient def mk {p : submodule α β} : β → quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule α β} (x : β) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule α β} (x : β) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule α β} (x : β) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : β} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ @[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl @[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p := by simpa using (quotient.eq p : mk x = 0 ↔ _) instance : has_add (quotient p) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, mk (a + b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, (quotient.eq p).2 $ by simpa using add_mem p h₁ h₂⟩ @[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl instance : has_neg (quotient p) := ⟨λ a, quotient.lift_on' a (λ a, mk (-a)) $ λ a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩ @[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm] instance : has_scalar α (quotient p) := ⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_add] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl instance : module α (quotient p) := module.of_core $ by refine {smul := (•), ..}; repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul, -mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm] instance {α β} {R:discrete_field α} [add_comm_group β] [vector_space α β] (p : submodule α β) : vector_space α (quotient p) := {} end quotient end submodule namespace submodule variables [discrete_field α] variables [add_comm_group β] [vector_space α β] variables [add_comm_group γ] [vector_space α γ] lemma comap_smul (f : β →ₗ[α] γ) (p : submodule α γ) (a : α) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : β →ₗ[α] γ) (p : submodule α β) (a : α) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end set_option class.instance_max_depth 40 lemma comap_smul' (f : β →ₗ[α] γ) (p : submodule α γ) (a : α) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : β →ₗ[α] γ) (p : submodule α β) (a : α) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by by_cases a = 0; simp [h, map_smul] end submodule namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α open submodule @[simp] lemma finsupp_sum {α β γ δ} [ring α] [add_comm_group β] [module α β] [add_comm_group γ] [module α γ] [has_zero δ] (f : β →ₗ[α] γ) {t : ι →₀ δ} {g : ι → δ → β} : f (t.sum g) = t.sum (λi d, f (g i d)) := f.map_sum theorem map_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.coe_ext] theorem comap_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ def range (f : β →ₗ[α] γ) : submodule α γ := map f ⊤ theorem range_coe (f : β →ₗ[α] γ) : (range f : set γ) = set.range f := set.image_univ @[simp] theorem mem_range {f : β →ₗ[α] γ} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x := (set.ext_iff _ _).1 (range_coe f). @[simp] theorem range_id : range (linear_map.id : β →ₗ[α] β) = ⊤ := map_id _ theorem range_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) ≤ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : β →ₗ[α] γ} : range f = ⊤ ↔ surjective f := by rw [← submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : β →ₗ[α] γ} {p : submodule α γ} : range f ≤ p ↔ comap f p = ⊤ := by rw [range, map_le_iff_le_comap, eq_top_iff] def ker (f : β →ₗ[α] γ) : submodule α β := comap f ⊥ @[simp] theorem mem_ker {f : β →ₗ[α] γ} {y} : y ∈ ker f ↔ f y = 0 := mem_bot α @[simp] theorem ker_id : ker (linear_map.id : β →ₗ[α] β) = ⊥ := rfl theorem ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker f ≤ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem sub_mem_ker_iff {f : β →ₗ[α] γ} {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem disjoint_ker' {f : β →ₗ[α] γ} {p : submodule α β} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩ theorem inj_of_disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} {s : set β} (h : s ⊆ p) (hd : disjoint p (ker f)) : ∀ x y ∈ s, f x = f y → x = y := λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) theorem ker_eq_bot {f : β →ₗ[α] γ} : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ lemma le_ker_iff_map {f : β →ₗ[α] γ} {p : submodule α β} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma map_comap_eq (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma map_comap_eq_self {f : β →ₗ[α] γ} {q : submodule α γ} (h : q ≤ range f) : map f (comap f q) = q := by rw [map_comap_eq, inf_of_le_right h] lemma comap_map_eq (f : β →ₗ[α] γ) (p : submodule α β) : comap f (map f p) = p ⊔ ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : β →ₗ[α] γ} {p : submodule α β} (h : ker f ≤ p) : comap f (map f p) = p := by rw [comap_map_eq, sup_of_le_left h] @[simp] theorem ker_zero : ker (0 : β →ₗ[α] γ) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero : range (0 : β →ₗ[α] γ) = ⊥ := submodule.map_zero _ theorem ker_eq_top {f : β →ₗ[α] γ} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ lemma range_le_bot_iff (f : β →ₗ[α] γ) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem map_le_map_iff {f : β →ₗ[α] γ} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := ⟨λ H x hx, let ⟨y, hy, e⟩ := H ⟨x, hx, rfl⟩ in ker_eq_bot.1 hf e ▸ hy, map_mono⟩ theorem map_injective {f : β →ₗ[α] γ} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff hf).1 (le_of_eq h)) ((map_le_map_iff hf).1 (ge_of_eq h)) theorem comap_le_comap_iff {f : β →ₗ[α] γ} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : β →ₗ[α] γ} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) theorem map_copair_prod (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (p : submodule α β) (q : submodule α γ) : map (copair f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw le_def', rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_pair_prod (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (p : submodule α γ) (q : submodule α δ) : comap (pair f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule α β) (q : submodule α γ) : p.prod q = p.comap (linear_map.fst α β γ) ⊓ q.comap (linear_map.snd α β γ) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule α β) (q : submodule α γ) : p.prod q = p.map (linear_map.inl α β γ) ⊔ q.map (linear_map.inr α β γ) := by rw [← map_copair_prod, copair_inl_inr, map_id] lemma span_inl_union_inr {s : set β} {t : set γ} : span α (prod.inl '' s ∪ prod.inr '' t) = (span α s).prod (span α t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl lemma ker_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : ker (pair f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_pair_prod]; refl end linear_map namespace linear_map variables [discrete_field α] variables [add_comm_group β] [vector_space α β] variables [add_comm_group γ] [vector_space α γ] lemma ker_smul (f : β →ₗ[α] γ) (a : α) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : β →ₗ[α] γ) (a : α) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : β →ₗ[α] γ) (a : α) (h : a ≠ 0) : range (a • f) = range f := submodule.map_smul f _ a h lemma range_smul' (f : β →ₗ[α] γ) (a : α) : range (a • f) = ⨆(h : a ≠ 0), range f := submodule.map_smul' f _ a end linear_map namespace submodule variables {R:ring α} [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] variables (p p' : submodule α β) (q : submodule α γ) include R open linear_map @[simp] theorem map_top (f : β →ₗ[α] γ) : map f ⊤ = range f := rfl @[simp] theorem comap_bot (f : β →ₗ[α] γ) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot.2 $ λ x y, subtype.eq' @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule α p) : map p.subtype p' ≤ p := by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range) @[simp] theorem ker_of_le (p p' : submodule α β) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] lemma range_of_le (p q : submodule α β) (h : p ≤ q) : (of_le h).range = comap q.subtype p := by rw [← map_top, of_le, linear_map.map_cod_restrict, map_top, range_subtype] lemma disjoint_iff_comap_eq_bot (p q : submodule α β) : disjoint p q ↔ comap p.subtype q = ⊥ := by rw [eq_bot_iff, ← map_le_map_iff p.ker_subtype, map_bot, map_comap_subtype]; refl /-- If N ⊆ M then submodules of N are the same as submodules of M contained in N -/ def map_subtype.order_iso : ((≤) : submodule α p → submodule α p → Prop) ≃o ((≤) : {p' : submodule α β // p' ≤ p} → {p' : submodule α β // p' ≤ p} → Prop) := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [map_comap_subtype p, inf_of_le_right hq], ord := λ p₁ p₂, (map_le_map_iff $ ker_subtype _).symm } def map_subtype.le_order_embedding : ((≤) : submodule α p → submodule α p → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) := (order_iso.to_order_embedding $ map_subtype.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule α p) : map_subtype.le_order_embedding p p' = map p.subtype p' := rfl def map_subtype.lt_order_embedding : ((<) : submodule α p → submodule α p → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) := (map_subtype.le_order_embedding p).lt_embedding_of_le_embedding @[simp] theorem map_inl : p.map (inl α β γ) = prod p ⊥ := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem map_inr : q.map (inr α β γ) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst α β γ) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd α β γ) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl α β γ) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr α β γ) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst α β γ) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd α β γ) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl α β γ).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr α β γ).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst α β γ).range = ⊤ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd α β γ).range = ⊤ := by rw [range, ← prod_top, prod_map_snd] def mkq : β →ₗ[α] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : β) : p.mkq x = quotient.mk x := rfl def liftq (f : β →ₗ[α] γ) (h : p ≤ f.ker) : p.quotient →ₗ[α] γ := ⟨λ x, _root_.quotient.lift_on' x f $ λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y, by rintro a ⟨x⟩; exact f.map_smul a x⟩ @[simp] theorem liftq_apply (f : β →ₗ[α] γ) {h} (x : β) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : β →ₗ[α] γ) (h) : (p.liftq f h).comp p.mkq = f := by ext; refl @[simp] theorem range_mkq : p.mkq.range = ⊤ := eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩ @[simp] theorem ker_mkq : p.mkq.ker = p := by ext; simp lemma le_comap_mkq (p' : submodule α p.quotient) : p ≤ comap p.mkq p' := by simpa using (comap_mono bot_le : p.mkq.ker ≤ comap p.mkq p') @[simp] theorem mkq_map_self : map p.mkq p = ⊥ := by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _ @[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p ⊔ p' := by simp [comap_map_eq, sup_comm] def mapq (f : β →ₗ[α] γ) (h : p ≤ comap f q) : p.quotient →ₗ[α] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : β →ₗ[α] γ) {h} (x : β) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : β →ₗ[α] γ) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : β →ₗ[α] γ) (h) : q.comap (p.liftq f h) = (q.comap f).map (mkq p) := le_antisymm (by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩) (by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _) theorem map_liftq (f : β →ₗ[α] γ) (h) (q : submodule α (quotient p)) : q.map (p.liftq f h) = (q.comap p.mkq).map f := le_antisymm (by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩) (by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩) theorem ker_liftq (f : β →ₗ[α] γ) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : β →ₗ[α] γ) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : β →ₗ[α] γ) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- Correspondence Theorem -/ def comap_mkq.order_iso : ((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≃o ((≤) : {p' : submodule α β // p ≤ p'} → {p' : submodule α β // p ≤ p'} → Prop) := { to_fun := λ p', ⟨comap p.mkq p', le_comap_mkq p _⟩, inv_fun := λ q, map p.mkq q, left_inv := λ p', map_comap_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [comap_map_mkq p, sup_of_le_right hq], ord := λ p₁ p₂, (comap_le_comap_iff $ range_mkq _).symm } def comap_mkq.le_order_embedding : ((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) := (order_iso.to_order_embedding $ comap_mkq.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule α p.quotient) : comap_mkq.le_order_embedding p p' = comap p.mkq p' := rfl def comap_mkq.lt_order_embedding : ((<) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) := (comap_mkq.le_order_embedding p).lt_embedding_of_le_embedding end submodule section set_option old_structure_cmd true structure linear_equiv (α : Type u) (β : Type v) (γ : Type w) [ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] extends β →ₗ[α] γ, β ≃ γ end infix ` ≃ₗ ` := linear_equiv _ notation β ` ≃ₗ[`:50 α `] ` γ := linear_equiv α β γ namespace linear_equiv section ring variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α section variable (β) def refl : β ≃ₗ[α] β := { .. linear_map.id, .. equiv.refl β } end def symm (e : β ≃ₗ[α] γ) : γ ≃ₗ[α] β := { .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } def trans (e₁ : β ≃ₗ[α] γ) (e₂ : γ ≃ₗ[α] δ) : β ≃ₗ[α] δ := { .. e₂.to_linear_map.comp e₁.to_linear_map, .. e₁.to_equiv.trans e₂.to_equiv } instance : has_coe (β ≃ₗ[α] γ) (β →ₗ[α] γ) := ⟨to_linear_map⟩ @[simp] theorem apply_symm_apply (e : β ≃ₗ[α] γ) (c : γ) : e (e.symm c) = c := e.6 c @[simp] theorem symm_apply_apply (e : β ≃ₗ[α] γ) (b : β) : e.symm (e b) = b := e.5 b @[simp] theorem coe_apply (e : β ≃ₗ[α] γ) (b : β) : (e : β →ₗ[α] γ) b = e b := rfl noncomputable def of_bijective (f : β →ₗ[α] γ) (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : β ≃ₗ[α] γ := { ..f, ..@equiv.of_bijective _ _ f ⟨linear_map.ker_eq_bot.1 hf₁, linear_map.range_eq_top.1 hf₂⟩ } @[simp] theorem of_bijective_apply (f : β →ₗ[α] γ) {hf₁ hf₂} (x : β) : of_bijective f hf₁ hf₂ x = f x := rfl def of_linear (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : β ≃ₗ[α] γ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } @[simp] theorem of_linear_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂} (x : β) : of_linear f g h₁ h₂ x = f x := rfl @[simp] theorem of_linear_symm_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂} (x : γ) : (of_linear f g h₁ h₂).symm x = g x := rfl @[simp] protected theorem ker (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).ker = ⊥ := linear_map.ker_eq_bot.2 f.to_equiv.bijective.1 @[simp] protected theorem range (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).range = ⊤ := linear_map.range_eq_top.2 f.to_equiv.bijective.2 def of_top (p : submodule α β) (h : p = ⊤) : p ≃ₗ[α] β := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply (p : submodule α β) {h} (x : p) : of_top p h x = x := rfl @[simp] theorem of_top_symm_apply (p : submodule α β) {h} (x : β) : ↑((of_top p h).symm x) = x := rfl lemma eq_bot_of_equiv (p : submodule α β) (e : p ≃ₗ[α] (⊥ : submodule α γ)) : p = ⊥ := begin refine bot_unique (submodule.le_def'.2 $ assume b hb, (submodule.mem_bot α).2 _), have := e.symm_apply_apply ⟨b, hb⟩, rw [← e.coe_apply, submodule.eq_zero_of_bot_submodule ((e : p →ₗ[α] (⊥ : submodule α γ)) ⟨b, hb⟩), ← e.symm.coe_apply, linear_map.map_zero] at this, exact congr_arg (coe : p → β) this.symm end end ring section comm_ring variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α open linear_map def smul_of_unit (a : units α) : β ≃ₗ[α] β := of_linear ((a:α) • 1 : β →ₗ β) (((a⁻¹ : units α) : α) • 1 : β →ₗ β) (by rw [smul_comp, comp_smul, smul_smul, units.mul_inv, one_smul]; refl) (by rw [smul_comp, comp_smul, smul_smul, units.inv_mul, one_smul]; refl) def congr_right (f : γ ≃ₗ[α] δ) : (β →ₗ[α] γ) ≃ₗ (β →ₗ δ) := of_linear f.to_linear_map.congr_right f.symm.to_linear_map.congr_right (linear_map.ext $ λ _, linear_map.ext $ λ _, f.6 _) (linear_map.ext $ λ _, linear_map.ext $ λ _, f.5 _) end comm_ring section field variables [field α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variable (β) open linear_map def smul_of_ne_zero (a : α) (ha : a ≠ 0) : β ≃ₗ[α] β := smul_of_unit $ units.mk0 a ha end field end linear_equiv namespace equiv variables [ring α] [add_comm_group β] [module α β] [add_comm_group γ] [module α γ] def to_linear_equiv (e : β ≃ γ) (h : is_linear_map α (e : β → γ)) : β ≃ₗ[α] γ := { add := h.add, smul := h.smul, .. e} end equiv namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f : β →ₗ[α] γ) /-- First Isomorphism Law -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[α] f.range := have hr : ∀ x : f.range, ∃ y, f y = ↑x := λ x, x.2.imp $ λ _, and.right, let F : f.ker.quotient →ₗ[α] f.range := f.ker.liftq (cod_restrict f.range f $ λ x, ⟨x, trivial, rfl⟩) (λ x hx, by simp; apply subtype.coe_ext.2; simpa using hx) in { inv_fun := λx, submodule.quotient.mk (classical.some (hr x)), left_inv := by rintro ⟨x⟩; exact (submodule.quotient.eq _).2 (sub_mem_ker_iff.2 $ classical.some_spec $ hr $ F $ submodule.quotient.mk x), right_inv := λ x : range f, subtype.eq $ classical.some_spec (hr x), .. F } open submodule def sup_quotient_to_quotient_inf (p p' : submodule α β) : (comap p.subtype (p ⊓ p')).quotient →ₗ[α] (comap (p ⊔ p').subtype p').quotient := (comap p.subtype (p ⊓ p')).liftq ((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype], exact comap_mono (inf_le_inf le_sup_left (le_refl _)) end set_option class.instance_max_depth 41 /-- Second Isomorphism Law -/ noncomputable def sup_quotient_equiv_quotient_inf (p p' : submodule α β) : (comap p.subtype (p ⊓ p')).quotient ≃ₗ[α] (comap (p ⊔ p').subtype p').quotient := { .. sup_quotient_to_quotient_inf p p', .. show (comap p.subtype (p ⊓ p')).quotient ≃ (comap (p ⊔ p').subtype p').quotient, from @equiv.of_bijective _ _ (sup_quotient_to_quotient_inf p p') begin constructor, { rw [← ker_eq_bot, sup_quotient_to_quotient_inf, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], rintros ⟨x, hx1⟩ hx2, exact ⟨hx1, hx2⟩ }, rw [← range_eq_top, sup_quotient_to_quotient_inf, range_liftq, eq_top_iff'], rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩, use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end } section pi universe i variables {φ : ι → Type i} variables [∀i, add_comm_group (φ i)] [∀i, module α (φ i)] /-- `pi` construction for linear functions. From a family of linear functions it produces a linear function into a family of modules. -/ def pi (f : Πi, γ →ₗ[α] φ i) : γ →ₗ[α] (Πi, φ i) := ⟨λc i, f i c, assume c d, funext $ assume i, (f i).add _ _, assume c d, funext $ assume i, (f i).smul _ _⟩ @[simp] lemma pi_apply (f : Πi, γ →ₗ[α] φ i) (c : γ) (i : ι) : pi f c i = f i c := rfl lemma ker_pi (f : Πi, γ →ₗ[α] φ i) : ker (pi f) = (⨅i:ι, ker (f i)) := by ext c; simp [funext_iff]; refl lemma pi_eq_zero (f : Πi, γ →ₗ[α] φ i) : pi f = 0 ↔ (∀i, f i = 0) := by simp only [linear_map.ext_iff, pi_apply, funext_iff]; exact ⟨λh a b, h b a, λh a b, h b a⟩ lemma pi_zero : pi (λi, 0 : Πi, γ →ₗ[α] φ i) = 0 := by ext; refl lemma pi_comp (f : Πi, γ →ₗ[α] φ i) (g : δ →ₗ[α] γ) : (pi f).comp g = pi (λi, (f i).comp g) := rfl /-- Linear projection -/ def proj (i : ι) : (Πi, φ i) →ₗ[α] φ i := ⟨ λa, a i, assume f g, rfl, assume c f, rfl ⟩ @[simp] lemma proj_apply (i : ι) (b : Πi, φ i) : (proj i : (Πi, φ i) →ₗ[α] φ i) b = b i := rfl lemma proj_pi (f : Πi, γ →ₗ[α] φ i) (i : ι) : (proj i).comp (pi f) = f i := ext $ assume c, rfl lemma infi_ker_proj : (⨅i, ker (proj i) : submodule α (Πi, φ i)) = ⊥ := bot_unique $ submodule.le_def'.2 $ assume a h, begin simp only [mem_infi, mem_ker, proj_apply] at h, exact (mem_bot _).2 (funext $ assume i, h i) end section variables (α φ) def infi_ker_proj_equiv {I J : set ι} [decidable_pred (λi, i ∈ I)] (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) : (⨅i ∈ J, ker (proj i) : submodule α (Πi, φ i)) ≃ₗ[α] (Πi:I, φ i) := begin refine linear_equiv.of_linear (pi $ λi, (proj (i:ι)).comp (submodule.subtype _)) (cod_restrict _ (pi $ λi, if h : i ∈ I then proj (⟨i, h⟩ : I) else 0) _) _ _, { assume b, simp only [mem_infi, mem_ker, funext_iff, proj_apply, pi_apply], assume j hjJ, have : j ∉ I := assume hjI, hd ⟨hjI, hjJ⟩, rw [dif_neg this, zero_apply] }, { simp only [pi_comp, comp_assoc, subtype_comp_cod_restrict, proj_pi, dif_pos, subtype.val_prop'], ext b ⟨j, hj⟩, refl }, { ext ⟨b, hb⟩, apply subtype.coe_ext.2, ext j, have hb : ∀i ∈ J, b i = 0, { simpa only [mem_infi, mem_ker, proj_apply] using (mem_infi _).1 hb }, simp only [comp_apply, pi_apply, id_apply, proj_apply, subtype_apply, cod_restrict_apply], split_ifs, { rw [dif_pos h], refl }, { rw [dif_neg h], exact (hb _ $ (hu trivial).resolve_left h).symm } } end end section variable [decidable_eq ι] /-- `diag i j` is the identity map if `i = j` otherwise it is the constant 0 map. -/ def diag (i j : ι) : φ i →ₗ[α] φ j := @function.update ι (λj, φ i →ₗ[α] φ j) _ 0 i id j lemma update_apply (f : Πi, γ →ₗ[α] φ i) (c : γ) (i j : ι) (b : γ →ₗ[α] φ i) : (update f i b j) c = update (λi, f i c) i (b c) j := begin by_cases j = i, { rw [h, update_same, update_same] }, { rw [update_noteq h, update_noteq h] } end end section variable [decidable_eq ι] variables (α φ) /-- Standard basis -/ def std_basis (i : ι) : φ i →ₗ[α] (Πi, φ i) := pi (diag i) lemma std_basis_apply (i : ι) (b : φ i) : std_basis α φ i b = update 0 i b := by ext j; rw [std_basis, pi_apply, diag, update_apply]; refl @[simp] lemma std_basis_same (i : ι) (b : φ i) : std_basis α φ i b i = b := by rw [std_basis_apply, update_same] lemma std_basis_ne (i j : ι) (h : j ≠ i) (b : φ i) : std_basis α φ i b j = 0 := by rw [std_basis_apply, update_noteq h]; refl lemma ker_std_basis (i : ι) : ker (std_basis α φ i) = ⊥ := ker_eq_bot.2 $ assume f g hfg, have std_basis α φ i f i = std_basis α φ i g i := hfg ▸ rfl, by simpa only [std_basis_same] lemma proj_comp_std_basis (i j : ι) : (proj i).comp (std_basis α φ j) = diag j i := by rw [std_basis, proj_pi] lemma proj_std_basis_same (i : ι) : (proj i).comp (std_basis α φ i) = id := by ext b; simp lemma proj_std_basis_ne (i j : ι) (h : i ≠ j) : (proj i).comp (std_basis α φ j) = 0 := by ext b; simp [std_basis_ne α φ _ _ h] lemma supr_range_std_basis_le_infi_ker_proj (I J : set ι) (h : disjoint I J) : (⨆i∈I, range (std_basis α φ i)) ≤ (⨅i∈J, ker (proj i)) := begin refine (supr_le $ assume i, supr_le $ assume hi, range_le_iff_comap.2 _), simp only [(ker_comp _ _).symm, eq_top_iff, le_def', mem_ker, comap_infi, mem_infi], assume b hb j hj, have : i ≠ j := assume eq, h ⟨hi, eq.symm ▸ hj⟩, rw [proj_std_basis_ne α φ j i this.symm, zero_apply] end lemma infi_ker_proj_le_supr_range_std_basis {I : finset ι} {J : set ι} (hu : set.univ ⊆ ↑I ∪ J) : (⨅ i∈J, ker (proj i)) ≤ (⨆i∈I, range (std_basis α φ i)) := submodule.le_def'.2 begin assume b hb, simp only [mem_infi, mem_ker, proj_apply] at hb, rw ← show I.sum (λi, std_basis α φ i (b i)) = b, { ext i, rw [pi.finset_sum_apply, ← std_basis_same α φ i (b i)], refine finset.sum_eq_single i (assume j hjI ne, std_basis_ne _ _ _ _ ne.symm _) _, assume hiI, rw [std_basis_same], exact hb _ ((hu trivial).resolve_left hiI) }, exact sum_mem _ (assume i hiI, mem_supr_of_mem _ i $ mem_supr_of_mem _ hiI $ linear_map.mem_range.2 ⟨_, rfl⟩) end lemma supr_range_std_basis_eq_infi_ker_proj {I J : set ι} (hd : disjoint I J) (hu : set.univ ⊆ I ∪ J) (hI : set.finite I) : (⨆i∈I, range (std_basis α φ i)) = (⨅i∈J, ker (proj i)) := begin refine le_antisymm (supr_range_std_basis_le_infi_ker_proj _ _ _ _ hd) _, have : set.univ ⊆ ↑hI.to_finset ∪ J, { rwa [finset.coe_to_finset] }, refine le_trans (infi_ker_proj_le_supr_range_std_basis α φ this) (supr_le_supr $ assume i, _), rw [← finset.mem_coe, finset.coe_to_finset], exact le_refl _ end lemma supr_range_std_basis [fintype ι] : (⨆i:ι, range (std_basis α φ i)) = ⊤ := have (set.univ : set ι) ⊆ ↑(finset.univ : finset ι) ∪ ∅ := by rw [finset.coe_univ, set.union_empty], begin apply top_unique, convert (infi_ker_proj_le_supr_range_std_basis α φ this), exact infi_emptyset.symm, exact (funext $ λi, (@supr_pos _ _ _ (λh, range (std_basis α φ i)) $ finset.mem_univ i).symm) end lemma disjoint_std_basis_std_basis (I J : set ι) (h : disjoint I J) : disjoint (⨆i∈I, range (std_basis α φ i)) (⨆i∈J, range (std_basis α φ i)) := begin refine disjoint_mono (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl I) (supr_range_std_basis_le_infi_ker_proj _ _ _ _ $ set.disjoint_compl J) _, simp only [disjoint, submodule.le_def', mem_infi, mem_inf, mem_ker, mem_bot, proj_apply, funext_iff], rintros b ⟨hI, hJ⟩ i, classical, by_cases hiI : i ∈ I, { by_cases hiJ : i ∈ J, { exact (h ⟨hiI, hiJ⟩).elim }, { exact hJ i hiJ } }, { exact hI i hiI } end end end pi end linear_map
b5a52dead03668f38907e21a1cb38a006bf49a31
e2c1ee9c02c59b832eb48536755242ce5f9b2c3e
/src/t3.lean
35c81134cbb4150df476025ff22fefa9f965e549
[]
no_license
yairgueta/Lean
cb5bb16d81674e8a07f85c9dbeb56dc2f543d4eb
af8a4fa24f76edfdd0dd33f013db194e611e6a86
refs/heads/master
1,676,275,529,938
1,610,289,661,000
1,610,289,661,000
328,405,309
0
0
null
null
null
null
UTF-8
Lean
false
false
3,964
lean
/- Yair Gueta : 208624908 : t3 Exercise 3 -/ import data.nat.basic import data.real.basic variables (α : Type*) (p q : α → Prop) ---- Q1 ---- example : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := iff.intro (assume h :∀ x, p x ∧ q x, have h₁ : ∀ x, p x, from assume y, show p y, from (h y).left, have ∀ x, q x, from assume k, show q k, from (h k).right, ⟨h₁, this⟩) (assume h : (∀ x, p x) ∧ (∀ x, q x), assume z, ⟨h.left z, h.right z⟩) example : (∀ x, p x → q x) → (∀ x, p x) → (∀ x, q x) := assume h₁ : ∀ x, p x → q x, assume h₂ : ∀ x, p x, assume y, have hpq : p y → q y, from h₁ y, hpq (h₂ y) example : (∀ x, p x) ∨ (∀ x, q x) → ∀ x, p x ∨ q x := assume h : (∀ x, p x) ∨ (∀ x, q x), assume x, h.elim (assume : (∀ x, p x), or.inl (this x)) (assume : (∀ x, q x), or.inr (this x)) ---- Q2 ---- open classical variable r : Prop example : α → ((∀ x : α, r) ↔ r) := assume y: α, iff.intro (assume : (∀ x : α, r), this y) (assume hr : r, assume y : α, hr) example : (∀ x, p x ∨ r) ↔ (∀ x, p x) ∨ r := iff.intro (assume h : ∀ x, p x ∨ r, by_cases (assume hr : r, show (∀ x, p x) ∨ r, from or.inr hr) (assume hnr : ¬r, suffices (∀ x, p x), from or.inl this, assume y, (h y).elim (assume hpy : p y, hpy)(assume hr:r, absurd hr hnr) )) (assume h : (∀ x, p x) ∨ r, h.elim (assume ah : ∀ x, p x, assume y, or.inl (ah y)) (assume hr : r, assume y, or.inr hr)) example : (∀ x, r → p x) ↔ (r → ∀ x, p x) := iff.intro (assume h : ∀ x, r → p x, assume hr : r, assume y, (h y) hr) (assume h : r → ∀ x, p x, assume y, assume hr : r, h hr y) ---- Q3 ---- variables (men : Type*) (barber : men) variable (shaves : men → men → Prop) example (h : ∀ x : men, shaves barber x ↔ ¬ shaves x x) : false := have hb : shaves barber barber ↔ ¬ shaves barber barber, from h barber, by_cases (assume hsb : shaves barber barber, hb.elim_left hsb hsb) (assume hnsb : ¬ shaves barber barber, hnsb (hb.elim_right hnsb)) ---- Q4 ---- namespace Q4 def even (n : ℕ) : Prop := 2 ∣ n def prime (n : ℕ) : Prop := ∀ (m : ℕ), (m > 1) → (m < n) → ¬ ∃(l : ℕ), m*l = n def infinitely_many_primes : Prop := ∀ (n : ℕ), ∃ (p : ℕ), p > n → prime p def Fermat_prime (n : ℕ) : Prop := ∃ (m : ℕ), prime n ↔ (n = 2^(2^m)+1) def infinitely_many_Fermat_primes : Prop := ∀ (n : ℕ), ∃ (p : ℕ), p > n → Fermat_prime p def goldbach_conjecture : Prop := ∀ (n : ℕ), (even n) → (n > 2) → (∃ (m l : ℕ ), prime m → prime l → m+l=n) def Goldbach's_weak_conjecture : Prop := ∀ (n : ℕ), (even n) → (n > 5) → (∃ (m₁ m₂ m₃ : ℕ ), prime m₁ → prime m₂ → prime m₃ → m₁+m₂+m₃=n) def Fermat's_last_theorem : Prop := ∀ (n : ℕ), n > 2 → ¬ (∃ (x y z : ℕ), x^n+y^n=z^n) end Q4 ---- Q6 ---- variables log exp : real → real variable log_exp_eq : ∀ x, log (exp x) = x variable exp_log_eq : ∀ {x}, x > 0 → exp (log x) = x variable exp_pos : ∀ x, exp x > 0 variable exp_add : ∀ x y, exp (x + y) = exp x * exp y -- this ensures the assumptions are available in tactic proofs include log_exp_eq exp_log_eq exp_pos exp_add example (x y z : real) : exp (x + y + z) = exp x * exp y * exp z := by rw [exp_add, exp_add] example (y : real) (h : y > 0) : exp (log y) = y := exp_log_eq h theorem log_mul {x y : real} (hx : x > 0) (hy : y > 0) : log (x * y) = log x + log y := by rw [←log_exp_eq(log x + log y),exp_add,exp_log_eq hx,exp_log_eq hy]
0526d063289b3e87d89b1c323e17b283af4b1ae2
456e966f070b2628cdcf863a8eaf383504abb2c9
/Lean/src/Numerical/Array/Layout.lean
6b21d06e6c18d17190db724502c52e49b0bf037f
[]
no_license
wellposed/formal-numerical
1f964338577336ed731ed8d5b45c68a7e14e03ea
e54ff9cc72387595e8a18d62f5cf6ef9965eaeae
refs/heads/master
1,610,245,015,998
1,450,065,168,000
1,450,065,168,000
47,095,150
0
0
null
null
null
null
UTF-8
Lean
false
false
186
lean
import data.bool check bool inductive Var (a b : Type) : Type := | Free : a -> Var a b | Bound : b -> Var a b check Var inductive AST (a : Type) : Type := | V : a -> AST a
3ded54ef0ea202eecf77a27407876a901ab14f61
ec62863c729b7eedee77b86d974f2c529fa79d25
/14/b.lean
6a543e577706086f3388ee97736224cc87b3354a
[]
no_license
rwbarton/advent-of-lean-4
2ac9b17ba708f66051e3d8cd694b0249bc433b65
417c7e2718253ba7148c0279fcb251b6fc291477
refs/heads/main
1,675,917,092,057
1,609,864,581,000
1,609,864,581,000
317,700,289
24
0
null
null
null
null
UTF-8
Lean
false
false
1,522
lean
import Std.Data.HashMap inductive Command | setMask (mask : String) : Command | setMem (addr : Nat) (val : Nat) : Command open Command instance : Inhabited Command := ⟨setMask ""⟩ structure St : Type where mask : String mem : Std.HashMap Nat Nat def initSt : St := { mask := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", mem := Std.HashMap.empty } def clearBit (i : Nat) (val : Nat) : Nat := (val / (2^(i+1))) * 2^(i+1) + (val % (2^i)) def setBit (i : Nat) (val : Nat) : Nat := clearBit i val + 2^i instance : Monad List := { pure := λ a => [a], map := List.map, bind := λ x f => (x.map f).join } def maskedAddrs (mask : String) (addr : Nat) : List Nat := do let mut res := addr for i in [0:36] do match mask.get i with | 'X' => if (← [false, true]) then res := setBit (35-i) res else res := clearBit (35-i) res | '1' => res := setBit (35-i) res | '0' => pure () | _ => panic! "bad mask" [res] def exec (st : St) : Command → St | setMask mask => { st with mask := mask } | setMem addr val => { st with mem := (maskedAddrs st.mask addr).foldl (λ m a => m.insert a val) st.mem } def parseCmd (str : String) : Command := match str.splitOn " " with | ["mask", "=", rhs] => setMask rhs | [lhs, "=", rhs] => setMem ((lhs.split (λ c => c == '[' || c == ']')).get! 1).toNat! rhs.toNat! | _ => panic! "invalid command" def main : IO Unit := do let lines ← IO.FS.lines "a.in" let st := (lines.map parseCmd).foldl exec initSt let sum := st.mem.fold (λ s k v => s + v) 0 IO.print s!"{sum}\n"
b9db85d7ee06a10c72592f36f956ff975fcce3d3
4727251e0cd73359b15b664c3170e5d754078599
/test/norm_swap.lean
48bde1f14e3067294d7cbd5de8f78ebb197ae9b6
[ "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
4,327
lean
import logic.equiv.basic import tactic.norm_swap open equiv tactic /-- To properly test that norm_swap works without the help of the simplifier, we turn off the simp lemmas that simplify swaps of the form `swap x y x = y` and `swap x y y = x`. -/ local attribute [-simp] swap_apply_left local attribute [-simp] swap_apply_right /-- We can check all possibilities of swapping of `0, 1, bit0 _, bit1 _` using the 64 combinations of `[0, 1, 2, 3]`. This example should execute and complete without error, if `norm_swap.eval` is behaving properly. -/ example : true := by do let l : list ℕ := [0, 1, 2, 3], let l' : list ((ℕ × ℕ) × ℕ) := (do a ← l, b ← l, c ← l, pure ((a, b), c)), (lhs : list expr) ← mmap (λ (tup : (ℕ × ℕ) × ℕ), to_expr ``(equiv.swap %%tup.fst.fst %%tup.fst.snd %%tup.snd)) l', (rhs : list expr) ← mmap (λ (tup : (ℕ × ℕ) × ℕ), if tup.snd = tup.fst.fst then to_expr ``(%%tup.fst.snd) else if tup.snd = tup.fst.snd then to_expr ``(%%tup.fst.fst) else to_expr ``(%%tup.snd)) l', let eqs : list expr := list.zip_with (λ L R, `(@eq.{1} ℕ %%L %%R)) lhs rhs, g ← get_goals, gls ← mmap mk_meta_var eqs, set_goals $ g ++ gls, triv, -- to discharge the starting `true` goal repeat $ tactic.norm_num norm_swap.eval [] (interactive.loc.ns [none]), done example : true := by do let l : list ℤ := [0, 2, -1, 3, -4], let l' := (do a ← l, b ← l, c ← l, pure ((a, b), c)), ic ← mk_instance_cache `(ℤ), -- we have to convert the provided `int`s into numeral form for the goals -- to look like what they would usually: `⇑(swap -1 3) -4 = -4`, instead of -- `⇑(swap (int.neg_succ_of_nat 0) (int.of_nat 3)) (int.neg_succ_of_nat 3) = int.neg_succ_of_nat 3` el ← mmap (λ (tup : (ℤ × ℤ) × ℤ), do (_, a) ← ic.of_int tup.fst.fst, (_, b) ← ic.of_int tup.fst.snd, (_, c) ← ic.of_int tup.snd, pure ((a, b), c)) l', (lhs : list expr) ← mmap (λ (tup : (expr × expr) × expr), to_expr ``((equiv.swap %%tup.fst.fst %%tup.fst.snd) %%tup.snd)) el, let rhs : list expr := el.map (λ (tup : (expr × expr) × expr), if tup.snd = tup.fst.fst then tup.fst.snd else if tup.snd = tup.fst.snd then tup.fst.fst else tup.snd), let eqs : list expr := list.zip_with (λ L R, `(@eq.{1} ℤ %%L %%R)) lhs rhs, g ← get_goals, gls ← mmap mk_meta_var eqs, set_goals $ g ++ gls, triv, -- to discharge the starting `true` goal repeat $ tactic.norm_num norm_swap.eval [] (interactive.loc.ns [none]), done example : true := by do let l : list ℚ := [0, (2 : ℚ) / -2, -1, 3 / 5, 4], let l' := (do a ← l, b ← l, c ← l, pure ((a, b), c)), ic ← mk_instance_cache `(ℚ), -- we have to convert the provided `rat`s into numeral form for the goals -- to look like what they would usually: `⇑(swap -1 3) -4 = -4`, like with ℤ above el ← mmap (λ (tup : (ℚ × ℚ) × ℚ), do (_, a) ← ic.of_rat tup.fst.fst, (_, b) ← ic.of_rat tup.fst.snd, (_, c) ← ic.of_rat tup.snd, pure ((a, b), c)) l', (lhs : list expr) ← mmap (λ (tup : (expr × expr) × expr), to_expr ``((equiv.swap %%tup.fst.fst %%tup.fst.snd) %%tup.snd)) el, let rhs : list expr := el.map (λ (tup : (expr × expr) × expr), if tup.snd = tup.fst.fst then tup.fst.snd else if tup.snd = tup.fst.snd then tup.fst.fst else tup.snd), let eqs : list expr := list.zip_with (λ L R, `(@eq.{1} ℚ %%L %%R)) lhs rhs, g ← get_goals, gls ← mmap mk_meta_var eqs, set_goals $ g ++ gls, triv, -- to discharge the starting `true` goal repeat $ tactic.norm_num norm_swap.eval [] (interactive.loc.ns [none]), done example : swap (3 : fin 7) 5 0 = 0 := by norm_num example : swap (fin.succ 2 : fin 7) 5 0 = 0 := by norm_num example : swap (3 : fin 7) 5 3 = 5 := by norm_num example : swap (3 : fin 1) 5 0 = 0 := by norm_num example : swap (3 : fin 7) 5 10 = 12 := by norm_fin example : swap (2 : fin 7) 4 9 = 11 := by norm_fin example : swap (3 : fin 7) 5 0 = 0 := by norm_num example : swap (3 : fin 7) 12 9 = 2 := by norm_num example : swap (0 : fin (1 + 2)) (1 : fin (nat.succ (1 + 1))) ((fin.succ 1) : fin 3) = 2 := by norm_num -- norm_swap doesn't generate trace output on non-swap expressions example : (1 : ℤ) = (1 : ℕ) := by norm_num
0f691ac123fa7de10bf487f71be43d27d200b62c
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/data/set/finite.lean
614c5b362dc72879f00c53154f24e926d102006a
[ "Apache-2.0" ]
permissive
agjftucker/mathlib
d634cd0d5256b6325e3c55bb7fb2403548371707
87fe50de17b00af533f72a102d0adefe4a2285e8
refs/heads/master
1,625,378,131,941
1,599,166,526,000
1,599,166,526,000
160,748,509
0
0
Apache-2.0
1,544,141,789,000
1,544,141,789,000
null
UTF-8
Lean
false
false
28,070
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.fintype.basic import algebra.big_operators.basic /-! # Finite sets This file defines predicates `finite : set α → Prop` and `infinite : set α → Prop` and proves some basic facts about finite sets. -/ open set function open_locale big_operators universes u v w x variables {α : Type u} {β : Type v} {ι : Sort w} {γ : Type x} namespace set /-- A set is finite if the subtype is a fintype, i.e. there is a list that enumerates its members. -/ def finite (s : set α) : Prop := nonempty (fintype s) /-- A set is infinite if it is not finite. -/ def infinite (s : set α) : Prop := ¬ finite s /-- The subtype corresponding to a finite set is a finite type. Note that because `finite` isn't a typeclass, this will not fire if it is made into an instance -/ noncomputable def finite.fintype {s : set α} (h : finite s) : fintype s := classical.choice h /-- Get a finset from a finite set -/ noncomputable def finite.to_finset {s : set α} (h : finite s) : finset α := @set.to_finset _ _ h.fintype @[simp] theorem finite.mem_to_finset {s : set α} {h : finite s} {a : α} : a ∈ h.to_finset ↔ a ∈ s := @mem_to_finset _ _ h.fintype _ @[simp] lemma finite.coe_to_finset {α} {s : set α} (h : finite s) : ↑h.to_finset = s := @set.coe_to_finset _ s h.fintype theorem finite.exists_finset {s : set α} : finite s → ∃ s' : finset α, ∀ a : α, a ∈ s' ↔ a ∈ s | ⟨h⟩ := by exactI ⟨to_finset s, λ _, mem_to_finset⟩ theorem finite.exists_finset_coe {s : set α} (hs : finite s) : ∃ s' : finset α, ↑s' = s := ⟨hs.to_finset, hs.coe_to_finset⟩ /-- Finite sets can be lifted to finsets. -/ instance : can_lift (set α) (finset α) := { coe := coe, cond := finite, prf := λ s hs, hs.exists_finset_coe } theorem finite_mem_finset (s : finset α) : finite {a | a ∈ s} := ⟨fintype.of_finset s (λ _, iff.rfl)⟩ theorem finite.of_fintype [fintype α] (s : set α) : finite s := by classical; exact ⟨set_fintype s⟩ theorem exists_finite_iff_finset {p : set α → Prop} : (∃ s, finite s ∧ p s) ↔ ∃ s : finset α, p ↑s := ⟨λ ⟨s, hs, hps⟩, ⟨hs.to_finset, hs.coe_to_finset.symm ▸ hps⟩, λ ⟨s, hs⟩, ⟨↑s, finite_mem_finset s, hs⟩⟩ /-- Membership of a subset of a finite type is decidable. Using this as an instance leads to potential loops with `subtype.fintype` under certain decidability assumptions, so it should only be declared a local instance. -/ def decidable_mem_of_fintype [decidable_eq α] (s : set α) [fintype s] (a) : decidable (a ∈ s) := decidable_of_iff _ mem_to_finset instance fintype_empty : fintype (∅ : set α) := fintype.of_finset ∅ $ by simp theorem empty_card : fintype.card (∅ : set α) = 0 := rfl @[simp] theorem empty_card' {h : fintype.{u} (∅ : set α)} : @fintype.card (∅ : set α) h = 0 := eq.trans (by congr) empty_card @[simp] theorem finite_empty : @finite α ∅ := ⟨set.fintype_empty⟩ instance finite.inhabited : inhabited {s : set α // finite s} := ⟨⟨∅, finite_empty⟩⟩ /-- A `fintype` structure on `insert a s`. -/ def fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : fintype (insert a s : set α) := fintype.of_finset ⟨a :: s.to_finset.1, multiset.nodup_cons_of_nodup (by simp [h]) s.to_finset.2⟩ $ by simp theorem card_fintype_insert' {a : α} (s : set α) [fintype s] (h : a ∉ s) : @fintype.card _ (fintype_insert' s h) = fintype.card s + 1 := by rw [fintype_insert', fintype.card_of_finset]; simp [finset.card, to_finset]; refl @[simp] theorem card_insert {a : α} (s : set α) [fintype s] (h : a ∉ s) {d : fintype.{u} (insert a s : set α)} : @fintype.card _ d = fintype.card s + 1 := by rw ← card_fintype_insert' s h; congr lemma card_image_of_inj_on {s : set α} [fintype s] {f : α → β} [fintype (f '' s)] (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : fintype.card (f '' s) = fintype.card s := by haveI := classical.prop_decidable; exact calc fintype.card (f '' s) = (s.to_finset.image f).card : fintype.card_of_finset' _ (by simp) ... = s.to_finset.card : finset.card_image_of_inj_on (λ x hx y hy hxy, H x (mem_to_finset.1 hx) y (mem_to_finset.1 hy) hxy) ... = fintype.card s : (fintype.card_of_finset' _ (λ a, mem_to_finset)).symm lemma card_image_of_injective (s : set α) [fintype s] {f : α → β} [fintype (f '' s)] (H : function.injective f) : fintype.card (f '' s) = fintype.card s := card_image_of_inj_on $ λ _ _ _ _ h, H h section local attribute [instance] decidable_mem_of_fintype instance fintype_insert [decidable_eq α] (a : α) (s : set α) [fintype s] : fintype (insert a s : set α) := if h : a ∈ s then by rwa [insert_eq, union_eq_self_of_subset_left (singleton_subset_iff.2 h)] else fintype_insert' _ h end @[simp] theorem finite.insert (a : α) {s : set α} : finite s → finite (insert a s) | ⟨h⟩ := ⟨@set.fintype_insert _ (classical.dec_eq α) _ _ h⟩ lemma to_finset_insert [decidable_eq α] {a : α} {s : set α} (hs : finite s) : (hs.insert a).to_finset = insert a hs.to_finset := finset.ext $ by simp @[elab_as_eliminator] theorem finite.induction_on {C : set α → Prop} {s : set α} (h : finite s) (H0 : C ∅) (H1 : ∀ {a s}, a ∉ s → finite s → C s → C (insert a s)) : C s := let ⟨t⟩ := h in by exactI match s.to_finset, @mem_to_finset _ s _ with | ⟨l, nd⟩, al := begin change ∀ a, a ∈ l ↔ a ∈ s at al, clear _let_match _match t h, revert s nd al, refine multiset.induction_on l _ (λ a l IH, _); intros s nd al, { rw show s = ∅, from eq_empty_iff_forall_not_mem.2 (by simpa using al), exact H0 }, { rw ← show insert a {x | x ∈ l} = s, from set.ext (by simpa using al), cases multiset.nodup_cons.1 nd with m nd', refine H1 _ ⟨finset.subtype.fintype ⟨l, nd'⟩⟩ (IH nd' (λ _, iff.rfl)), exact m } end end @[elab_as_eliminator] theorem finite.dinduction_on {C : ∀s:set α, finite s → Prop} {s : set α} (h : finite s) (H0 : C ∅ finite_empty) (H1 : ∀ {a s}, a ∉ s → ∀h:finite s, C s h → C (insert a s) (h.insert a)) : C s h := have ∀h:finite s, C s h, from finite.induction_on h (assume h, H0) (assume a s has hs ih h, H1 has hs (ih _)), this h instance fintype_singleton (a : α) : fintype ({a} : set α) := unique.fintype @[simp] theorem card_singleton (a : α) : fintype.card ({a} : set α) = 1 := fintype.card_of_subsingleton _ @[simp] theorem finite_singleton (a : α) : finite ({a} : set α) := ⟨set.fintype_singleton _⟩ instance fintype_pure : ∀ a : α, fintype (pure a : set α) := set.fintype_singleton theorem finite_pure (a : α) : finite (pure a : set α) := ⟨set.fintype_pure a⟩ instance fintype_univ [fintype α] : fintype (@univ α) := fintype.of_equiv α $ (equiv.set.univ α).symm theorem finite_univ [fintype α] : finite (@univ α) := ⟨set.fintype_univ⟩ theorem infinite_univ_iff : (@univ α).infinite ↔ _root_.infinite α := ⟨λ h₁, ⟨λ h₂, h₁ $ @finite_univ α h₂⟩, λ ⟨h₁⟩ ⟨h₂⟩, h₁ $ @fintype.of_equiv _ _ h₂ $ equiv.set.univ _⟩ theorem infinite_univ [h : _root_.infinite α] : infinite (@univ α) := infinite_univ_iff.2 h theorem infinite_coe_iff {s : set α} : _root_.infinite s ↔ infinite s := ⟨λ ⟨h₁⟩ h₂, h₁ h₂.some, λ h₁, ⟨λ h₂, h₁ ⟨h₂⟩⟩⟩ theorem infinite.to_subtype {s : set α} (h : infinite s) : _root_.infinite s := infinite_coe_iff.2 h /-- Embedding of `ℕ` into an infinite set. -/ noncomputable def infinite.nat_embedding (s : set α) (h : infinite s) : ℕ ↪ s := by { haveI := h.to_subtype, exact infinite.nat_embedding s } lemma infinite.exists_subset_card_eq {s : set α} (hs : infinite s) (n : ℕ) : ∃ t : finset α, ↑t ⊆ s ∧ t.card = n := ⟨((finset.range n).map (hs.nat_embedding _)).map (embedding.subtype _), by simp⟩ instance fintype_union [decidable_eq α] (s t : set α) [fintype s] [fintype t] : fintype (s ∪ t : set α) := fintype.of_finset (s.to_finset ∪ t.to_finset) $ by simp theorem finite.union {s t : set α} : finite s → finite t → finite (s ∪ t) | ⟨hs⟩ ⟨ht⟩ := ⟨@set.fintype_union _ (classical.dec_eq α) _ _ hs ht⟩ instance fintype_sep (s : set α) (p : α → Prop) [fintype s] [decidable_pred p] : fintype ({a ∈ s | p a} : set α) := fintype.of_finset (s.to_finset.filter p) $ by simp instance fintype_inter (s t : set α) [fintype s] [decidable_pred t] : fintype (s ∩ t : set α) := set.fintype_sep s t /-- A `fintype` structure on a set defines a `fintype` structure on its subset. -/ def fintype_subset (s : set α) {t : set α} [fintype s] [decidable_pred t] (h : t ⊆ s) : fintype t := by rw ← inter_eq_self_of_subset_right h; apply_instance theorem finite.subset {s : set α} : finite s → ∀ {t : set α}, t ⊆ s → finite t | ⟨hs⟩ t h := ⟨@set.fintype_subset _ _ _ hs (classical.dec_pred t) h⟩ instance fintype_image [decidable_eq β] (s : set α) (f : α → β) [fintype s] : fintype (f '' s) := fintype.of_finset (s.to_finset.image f) $ by simp instance fintype_range [decidable_eq β] (f : α → β) [fintype α] : fintype (range f) := fintype.of_finset (finset.univ.image f) $ by simp [range] theorem finite_range (f : α → β) [fintype α] : finite (range f) := by haveI := classical.dec_eq β; exact ⟨by apply_instance⟩ theorem finite.image {s : set α} (f : α → β) : finite s → finite (f '' s) | ⟨h⟩ := ⟨@set.fintype_image _ _ (classical.dec_eq β) _ _ h⟩ lemma finite.dependent_image {s : set α} (hs : finite s) {F : Π i ∈ s, β} {t : set β} (H : ∀ y ∈ t, ∃ x (hx : x ∈ s), y = F x hx) : set.finite t := begin let G : s → β := λ x, F x.1 x.2, have A : t ⊆ set.range G, { assume y hy, rcases H y hy with ⟨x, hx, xy⟩, refine ⟨⟨x, hx⟩, xy.symm⟩ }, letI : fintype s := finite.fintype hs, exact (finite_range G).subset A end instance fintype_map {α β} [decidable_eq β] : ∀ (s : set α) (f : α → β) [fintype s], fintype (f <$> s) := set.fintype_image theorem finite.map {α β} {s : set α} : ∀ (f : α → β), finite s → finite (f <$> s) := finite.image /-- If a function `f` has a partial inverse and sends a set `s` to a set with `[fintype]` instance, then `s` has a `fintype` structure as well. -/ def fintype_of_fintype_image (s : set α) {f : α → β} {g} (I : is_partial_inv f g) [fintype (f '' s)] : fintype s := fintype.of_finset ⟨_, @multiset.nodup_filter_map β α g _ (@injective_of_partial_inv_right _ _ f g I) (f '' s).to_finset.2⟩ $ λ a, begin suffices : (∃ b x, f x = b ∧ g b = some a ∧ x ∈ s) ↔ a ∈ s, by simpa [exists_and_distrib_left.symm, and.comm, and.left_comm, and.assoc], rw exists_swap, suffices : (∃ x, x ∈ s ∧ g (f x) = some a) ↔ a ∈ s, {simpa [and.comm, and.left_comm, and.assoc]}, simp [I _, (injective_of_partial_inv I).eq_iff] end theorem finite_of_finite_image {s : set α} {f : α → β} (hi : set.inj_on f s) : finite (f '' s) → finite s | ⟨h⟩ := ⟨@fintype.of_injective _ _ h (λa:s, ⟨f a.1, mem_image_of_mem f a.2⟩) $ assume a b eq, subtype.eq $ hi a.2 b.2 $ subtype.ext_iff_val.1 eq⟩ theorem finite_image_iff {s : set α} {f : α → β} (hi : inj_on f s) : finite (f '' s) ↔ finite s := ⟨finite_of_finite_image hi, finite.image _⟩ theorem finite.preimage {s : set β} {f : α → β} (I : set.inj_on f (f⁻¹' s)) (h : finite s) : finite (f ⁻¹' s) := finite_of_finite_image I (h.subset (image_preimage_subset f s)) instance fintype_Union [decidable_eq α] {ι : Type*} [fintype ι] (f : ι → set α) [∀ i, fintype (f i)] : fintype (⋃ i, f i) := fintype.of_finset (finset.univ.bind (λ i, (f i).to_finset)) $ by simp theorem finite_Union {ι : Type*} [fintype ι] {f : ι → set α} (H : ∀i, finite (f i)) : finite (⋃ i, f i) := ⟨@set.fintype_Union _ (classical.dec_eq α) _ _ _ (λ i, finite.fintype (H i))⟩ /-- A union of sets with `fintype` structure over a set with `fintype` structure has a `fintype` structure. -/ def fintype_bUnion [decidable_eq α] {ι : Type*} {s : set ι} [fintype s] (f : ι → set α) (H : ∀ i ∈ s, fintype (f i)) : fintype (⋃ i ∈ s, f i) := by rw bUnion_eq_Union; exact @set.fintype_Union _ _ _ _ _ (by rintro ⟨i, hi⟩; exact H i hi) instance fintype_bUnion' [decidable_eq α] {ι : Type*} {s : set ι} [fintype s] (f : ι → set α) [H : ∀ i, fintype (f i)] : fintype (⋃ i ∈ s, f i) := fintype_bUnion _ (λ i _, H i) theorem finite.sUnion {s : set (set α)} (h : finite s) (H : ∀t∈s, finite t) : finite (⋃₀ s) := by rw sUnion_eq_Union; haveI := finite.fintype h; apply finite_Union; simpa using H theorem finite.bUnion {α} {ι : Type*} {s : set ι} {f : Π i ∈ s, set α} : finite s → (∀ i ∈ s, finite (f i ‹_›)) → finite (⋃ i∈s, f i ‹_›) | ⟨hs⟩ h := by rw [bUnion_eq_Union]; exactI finite_Union (λ i, h _ _) instance fintype_lt_nat (n : ℕ) : fintype {i | i < n} := fintype.of_finset (finset.range n) $ by simp instance fintype_le_nat (n : ℕ) : fintype {i | i ≤ n} := by simpa [nat.lt_succ_iff] using set.fintype_lt_nat (n+1) lemma finite_le_nat (n : ℕ) : finite {i | i ≤ n} := ⟨set.fintype_le_nat _⟩ lemma finite_lt_nat (n : ℕ) : finite {i | i < n} := ⟨set.fintype_lt_nat _⟩ instance fintype_prod (s : set α) (t : set β) [fintype s] [fintype t] : fintype (set.prod s t) := fintype.of_finset (s.to_finset.product t.to_finset) $ by simp lemma finite.prod {s : set α} {t : set β} : finite s → finite t → finite (set.prod s t) | ⟨hs⟩ ⟨ht⟩ := by exactI ⟨set.fintype_prod s t⟩ /-- `image2 f s t` is finitype if `s` and `t` are. -/ instance fintype_image2 [decidable_eq γ] (f : α → β → γ) (s : set α) (t : set β) [hs : fintype s] [ht : fintype t] : fintype (image2 f s t : set γ) := by { rw ← image_prod, apply set.fintype_image } lemma finite.image2 (f : α → β → γ) {s : set α} {t : set β} (hs : finite s) (ht : finite t) : finite (image2 f s t) := by { rw ← image_prod, exact (hs.prod ht).image _ } /-- If `s : set α` is a set with `fintype` instance and `f : α → set β` is a function such that each `f a`, `a ∈ s`, has a `fintype` structure, then `s >>= f` has a `fintype` structure. -/ def fintype_bind {α β} [decidable_eq β] (s : set α) [fintype s] (f : α → set β) (H : ∀ a ∈ s, fintype (f a)) : fintype (s >>= f) := set.fintype_bUnion _ H instance fintype_bind' {α β} [decidable_eq β] (s : set α) [fintype s] (f : α → set β) [H : ∀ a, fintype (f a)] : fintype (s >>= f) := fintype_bind _ _ (λ i _, H i) theorem finite_bind {α β} {s : set α} {f : α → set β} : finite s → (∀ a ∈ s, finite (f a)) → finite (s >>= f) | ⟨hs⟩ H := ⟨@fintype_bind _ _ (classical.dec_eq β) _ hs _ (λ a ha, (H a ha).fintype)⟩ instance fintype_seq {α β : Type u} [decidable_eq β] (f : set (α → β)) (s : set α) [fintype f] [fintype s] : fintype (f <*> s) := by rw seq_eq_bind_map; apply set.fintype_bind' theorem finite.seq {α β : Type u} {f : set (α → β)} {s : set α} : finite f → finite s → finite (f <*> s) | ⟨hf⟩ ⟨hs⟩ := by { haveI := classical.dec_eq β, exactI ⟨set.fintype_seq _ _⟩ } /-- There are finitely many subsets of a given finite set -/ lemma finite.finite_subsets {α : Type u} {a : set α} (h : finite a) : finite {b | b ⊆ a} := begin -- we just need to translate the result, already known for finsets, -- to the language of finite sets let s : set (set α) := coe '' (↑(finset.powerset (finite.to_finset h)) : set (finset α)), have : finite s := (finite_mem_finset _).image _, apply this.subset, refine λ b hb, ⟨(h.subset hb).to_finset, _, finite.coe_to_finset _⟩, simpa [finset.subset_iff] end lemma exists_min_image [linear_order β] (s : set α) (f : α → β) (h1 : finite s) : s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f a ≤ f b | ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset] using (finite.to_finset h1).exists_min_image f ⟨x, finite.mem_to_finset.2 hx⟩ lemma exists_max_image [linear_order β] (s : set α) (f : α → β) (h1 : finite s) : s.nonempty → ∃ a ∈ s, ∀ b ∈ s, f b ≤ f a | ⟨x, hx⟩ := by simpa only [exists_prop, finite.mem_to_finset] using (finite.to_finset h1).exists_max_image f ⟨x, finite.mem_to_finset.2 hx⟩ end set namespace finset variables [decidable_eq β] variables {s : finset α} lemma finite_to_set (s : finset α) : set.finite (↑s : set α) := set.finite_mem_finset s @[simp] lemma coe_bind {f : α → finset β} : ↑(s.bind f) = (⋃x ∈ (↑s : set α), ↑(f x) : set β) := by simp [set.ext_iff] @[simp] lemma finite_to_set_to_finset {α : Type*} (s : finset α) : (finite_to_set s).to_finset = s := by { ext, rw [set.finite.mem_to_finset, mem_coe] } end finset namespace set lemma finite_subset_Union {s : set α} (hs : finite s) {ι} {t : ι → set α} (h : s ⊆ ⋃ i, t i) : ∃ I : set ι, finite I ∧ s ⊆ ⋃ i ∈ I, t i := begin casesI hs, choose f hf using show ∀ x : s, ∃ i, x.1 ∈ t i, {simpa [subset_def] using h}, refine ⟨range f, finite_range f, _⟩, rintro x hx, simp, exact ⟨x, ⟨hx, hf _⟩⟩, end lemma eq_finite_Union_of_finite_subset_Union {ι} {s : ι → set α} {t : set α} (tfin : finite t) (h : t ⊆ ⋃ i, s i) : ∃ I : set ι, (finite I) ∧ ∃ σ : {i | i ∈ I} → set α, (∀ i, finite (σ i)) ∧ (∀ i, σ i ⊆ s i) ∧ t = ⋃ i, σ i := let ⟨I, Ifin, hI⟩ := finite_subset_Union tfin h in ⟨I, Ifin, λ x, s x ∩ t, λ i, tfin.subset (inter_subset_right _ _), λ i, inter_subset_left _ _, begin ext x, rw mem_Union, split, { intro x_in, rcases mem_Union.mp (hI x_in) with ⟨i, _, ⟨hi, rfl⟩, H⟩, use [i, hi, H, x_in] }, { rintros ⟨i, hi, H⟩, exact H } end⟩ instance nat.fintype_Iio (n : ℕ) : fintype (Iio n) := fintype.of_finset (finset.range n) $ by simp /-- If `P` is some relation between terms of `γ` and sets in `γ`, such that every finite set `t : set γ` has some `c : γ` related to it, then there is a recursively defined sequence `u` in `γ` so `u n` is related to the image of `{0, 1, ..., n-1}` under `u`. (We use this later to show sequentially compact sets are totally bounded.) -/ lemma seq_of_forall_finite_exists {γ : Type*} {P : γ → set γ → Prop} (h : ∀ t, finite t → ∃ c, P c t) : ∃ u : ℕ → γ, ∀ n, P (u n) (u '' Iio n) := ⟨λ n, @nat.strong_rec_on' (λ _, γ) n $ λ n ih, classical.some $ h (range $ λ m : Iio n, ih m.1 m.2) (finite_range _), λ n, begin classical, refine nat.strong_rec_on' n (λ n ih, _), rw nat.strong_rec_on_beta', convert classical.some_spec (h _ _), ext x, split, { rintros ⟨m, hmn, rfl⟩, exact ⟨⟨m, hmn⟩, rfl⟩ }, { rintros ⟨⟨m, hmn⟩, rfl⟩, exact ⟨m, hmn, rfl⟩ } end⟩ lemma finite_range_ite {p : α → Prop} [decidable_pred p] {f g : α → β} (hf : finite (range f)) (hg : finite (range g)) : finite (range (λ x, if p x then f x else g x)) := (hf.union hg).subset range_ite_subset lemma finite_range_const {c : β} : finite (range (λ x : α, c)) := (finite_singleton c).subset range_const_subset lemma range_find_greatest_subset {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ}: range (λ x, nat.find_greatest (P x) b) ⊆ ↑(finset.range (b + 1)) := by { rw range_subset_iff, assume x, simp [nat.lt_succ_iff, nat.find_greatest_le] } lemma finite_range_find_greatest {P : α → ℕ → Prop} [∀ x, decidable_pred (P x)] {b : ℕ} : finite (range (λ x, nat.find_greatest (P x) b)) := (finset.range (b + 1)).finite_to_set.subset range_find_greatest_subset lemma card_lt_card {s t : set α} [fintype s] [fintype t] (h : s ⊂ t) : fintype.card s < fintype.card t := begin rw [← s.coe_to_finset, ← t.coe_to_finset, finset.coe_ssubset] at h, rw [fintype.card_of_finset' _ (λ x, mem_to_finset), fintype.card_of_finset' _ (λ x, mem_to_finset)], exact finset.card_lt_card h, end lemma card_le_of_subset {s t : set α} [fintype s] [fintype t] (hsub : s ⊆ t) : fintype.card s ≤ fintype.card t := calc fintype.card s = s.to_finset.card : fintype.card_of_finset' _ (by simp) ... ≤ t.to_finset.card : finset.card_le_of_subset (λ x hx, by simp [set.subset_def, *] at *) ... = fintype.card t : eq.symm (fintype.card_of_finset' _ (by simp)) lemma eq_of_subset_of_card_le {s t : set α} [fintype s] [fintype t] (hsub : s ⊆ t) (hcard : fintype.card t ≤ fintype.card s) : s = t := (eq_or_ssubset_of_subset hsub).elim id (λ h, absurd hcard $ not_le_of_lt $ card_lt_card h) lemma card_range_of_injective [fintype α] {f : α → β} (hf : injective f) [fintype (range f)] : fintype.card (range f) = fintype.card α := eq.symm $ fintype.card_congr $ equiv.set.range f hf lemma finite.exists_maximal_wrt [partial_order β] (f : α → β) (s : set α) (h : set.finite s) : s.nonempty → ∃a∈s, ∀a'∈s, f a ≤ f a' → f a = f a' := begin classical, refine h.induction_on _ _, { assume h, exact absurd h empty_not_nonempty }, assume a s his _ ih _, cases s.eq_empty_or_nonempty with h h, { use a, simp [h] }, rcases ih h with ⟨b, hb, ih⟩, by_cases f b ≤ f a, { refine ⟨a, set.mem_insert _ _, assume c hc hac, le_antisymm hac _⟩, rcases set.mem_insert_iff.1 hc with rfl | hcs, { refl }, { rwa [← ih c hcs (le_trans h hac)] } }, { refine ⟨b, set.mem_insert_of_mem _ hb, assume c hc hbc, _⟩, rcases set.mem_insert_iff.1 hc with rfl | hcs, { exact (h hbc).elim }, { exact ih c hcs hbc } } end lemma finite.card_to_finset {s : set α} [fintype s] (h : s.finite) : h.to_finset.card = fintype.card s := by { rw [← finset.card_attach, finset.attach_eq_univ, ← fintype.card], congr' 2, funext, rw set.finite.mem_to_finset } section local attribute [instance, priority 1] classical.prop_decidable lemma to_finset_inter {α : Type*} [fintype α] (s t : set α) : (s ∩ t).to_finset = s.to_finset ∩ t.to_finset := by ext; simp end section variables [semilattice_sup α] [nonempty α] {s : set α} /--A finite set is bounded above.-/ protected lemma finite.bdd_above (hs : finite s) : bdd_above s := finite.induction_on hs bdd_above_empty $ λ a s _ _ h, h.insert a /--A finite union of sets which are all bounded above is still bounded above.-/ lemma finite.bdd_above_bUnion {I : set β} {S : β → set α} (H : finite I) : (bdd_above (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_above (S i)) := finite.induction_on H (by simp only [bUnion_empty, bdd_above_empty, ball_empty_iff]) (λ a s ha _ hs, by simp only [bUnion_insert, ball_insert_iff, bdd_above_union, hs]) end section variables [semilattice_inf α] [nonempty α] {s : set α} /--A finite set is bounded below.-/ protected lemma finite.bdd_below (hs : finite s) : bdd_below s := @finite.bdd_above (order_dual α) _ _ _ hs /--A finite union of sets which are all bounded below is still bounded below.-/ lemma finite.bdd_below_bUnion {I : set β} {S : β → set α} (H : finite I) : (bdd_below (⋃i∈I, S i)) ↔ (∀i ∈ I, bdd_below (S i)) := @finite.bdd_above_bUnion (order_dual α) _ _ _ _ _ H end end set namespace finset section preimage /-- Preimage of `s : finset β` under a map `f` injective of `f ⁻¹' s` as a `finset`. -/ noncomputable def preimage (s : finset β) (f : α → β) (hf : set.inj_on f (f ⁻¹' ↑s)) : finset α := (s.finite_to_set.preimage hf).to_finset @[simp] lemma mem_preimage {f : α → β} {s : finset β} {hf : set.inj_on f (f ⁻¹' ↑s)} {x : α} : x ∈ preimage s f hf ↔ f x ∈ s := set.finite.mem_to_finset @[simp, norm_cast] lemma coe_preimage {f : α → β} (s : finset β) (hf : set.inj_on f (f ⁻¹' ↑s)) : (↑(preimage s f hf) : set α) = f ⁻¹' ↑s := set.finite.coe_to_finset _ lemma monotone_preimage {f : α → β} (h : injective f) : monotone (λ s, preimage s f (h.inj_on _)) := λ s t hst x hx, mem_preimage.2 (hst $ mem_preimage.1 hx) lemma image_subset_iff_subset_preimage [decidable_eq β] {f : α → β} {s : finset α} {t : finset β} (hf : set.inj_on f (f ⁻¹' ↑t)) : s.image f ⊆ t ↔ s ⊆ t.preimage f hf := image_subset_iff.trans $ by simp only [subset_iff, mem_preimage] lemma map_subset_iff_subset_preimage {f : α ↪ β} {s : finset α} {t : finset β} : s.map f ⊆ t ↔ s ⊆ t.preimage f (f.injective.inj_on _) := by classical; rw [map_eq_image, image_subset_iff_subset_preimage] lemma image_preimage [decidable_eq β] (f : α → β) (s : finset β) [Π x, decidable (x ∈ set.range f)] (hf : set.inj_on f (f ⁻¹' ↑s)) : image f (preimage s f hf) = s.filter (λ x, x ∈ set.range f) := finset.coe_inj.1 $ by simp only [coe_image, coe_preimage, coe_filter, set.image_preimage_eq_inter_range, set.sep_mem_eq] lemma image_preimage_of_bij [decidable_eq β] (f : α → β) (s : finset β) (hf : set.bij_on f (f ⁻¹' ↑s) ↑s) : image f (preimage s f hf.inj_on) = s := finset.coe_inj.1 $ by simpa using hf.image_eq lemma sigma_preimage_mk {β : α → Type*} [decidable_eq α] (s : finset (Σ a, β a)) (t : finset α) : t.sigma (λ a, s.preimage (sigma.mk a) $ sigma_mk_injective.inj_on _) = s.filter (λ a, a.1 ∈ t) := by { ext x, simp [and_comm] } lemma sigma_preimage_mk_of_subset {β : α → Type*} [decidable_eq α] (s : finset (Σ a, β a)) {t : finset α} (ht : s.image sigma.fst ⊆ t) : t.sigma (λ a, s.preimage (sigma.mk a) $ sigma_mk_injective.inj_on _) = s := by rw [sigma_preimage_mk, filter_true_of_mem $ image_subset_iff.1 ht] lemma sigma_image_fst_preimage_mk {β : α → Type*} [decidable_eq α] (s : finset (Σ a, β a)) : (s.image sigma.fst).sigma (λ a, s.preimage (sigma.mk a) $ sigma_mk_injective.inj_on _) = s := s.sigma_preimage_mk_of_subset (subset.refl _) end preimage @[to_additive] lemma prod_preimage' [comm_monoid β] (f : α → γ) [decidable_pred $ λ x, x ∈ set.range f] (s : finset γ) (hf : set.inj_on f (f ⁻¹' ↑s)) (g : γ → β) : ∏ x in s.preimage f hf, g (f x) = ∏ x in s.filter (λ x, x ∈ set.range f), g x := by haveI := classical.dec_eq γ; calc ∏ x in preimage s f hf, g (f x) = ∏ x in image f (preimage s f hf), g x : eq.symm $ prod_image $ by simpa only [mem_preimage, inj_on] using hf ... = ∏ x in s.filter (λ x, x ∈ set.range f), g x : by rw [image_preimage] @[to_additive] lemma prod_preimage [comm_monoid β] (f : α → γ) (s : finset γ) (hf : set.inj_on f (f ⁻¹' ↑s)) (g : γ → β) (hg : ∀ x ∈ s, x ∉ set.range f → g x = 1) : ∏ x in s.preimage f hf, g (f x) = ∏ x in s, g x := by { classical, rw [prod_preimage', prod_filter_of_ne], exact λ x hx, not.imp_symm (hg x hx) } @[to_additive] lemma prod_preimage_of_bij [comm_monoid β] (f : α → γ) (s : finset γ) (hf : set.bij_on f (f ⁻¹' ↑s) ↑s) (g : γ → β) : ∏ x in s.preimage f hf.inj_on, g (f x) = ∏ x in s, g x := prod_preimage _ _ hf.inj_on g $ λ x hxs hxf, (hxf $ hf.subset_range hxs).elim /-- A finset is bounded above. -/ protected lemma bdd_above [semilattice_sup α] [nonempty α] (s : finset α) : bdd_above (↑s : set α) := s.finite_to_set.bdd_above /-- A finset is bounded below. -/ protected lemma bdd_below [semilattice_inf α] [nonempty α] (s : finset α) : bdd_below (↑s : set α) := s.finite_to_set.bdd_below end finset lemma fintype.exists_max [fintype α] [nonempty α] {β : Type*} [linear_order β] (f : α → β) : ∃ x₀ : α, ∀ x, f x ≤ f x₀ := begin rcases set.finite_univ.exists_maximal_wrt f _ univ_nonempty with ⟨x, _, hx⟩, exact ⟨x, λ y, (le_total (f x) (f y)).elim (λ h, ge_of_eq $ hx _ trivial h) id⟩ end
1409a48cf1e1901b85ce475f592a32a51d070fb5
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/functor_category.lean
0d4632c974ec24f8f486a19294f53c154195de75
[ "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
4,899
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tim Baumann, Stephen Morgan, Scott Morrison, Floris van Doorn -/ import category_theory.natural_transformation namespace category_theory universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation open nat_trans category category_theory.functor variables (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] local attribute [simp] vcomp_app /-- `functor.category C D` gives the category structure on functors and natural transformations between categories `C` and `D`. Notice that if `C` and `D` are both small categories at the same universe level, this is another small category at that level. However if `C` and `D` are both large categories at the same universe level, this is a small category at the next higher level. -/ instance functor.category : category.{(max u₁ v₂)} (C ⥤ D) := { hom := λ F G, nat_trans F G, id := λ F, nat_trans.id F, comp := λ _ _ _ α β, vcomp α β } variables {C D} {E : Type u₃} [category.{v₃} E] variables {F G H I : C ⥤ D} namespace nat_trans @[simp] lemma vcomp_eq_comp (α : F ⟶ G) (β : G ⟶ H) : vcomp α β = α ≫ β := rfl lemma vcomp_app' (α : F ⟶ G) (β : G ⟶ H) (X : C) : (α ≫ β).app X = (α.app X) ≫ (β.app X) := rfl lemma congr_app {α β : F ⟶ G} (h : α = β) (X : C) : α.app X = β.app X := by rw h @[simp] lemma id_app (F : C ⥤ D) (X : C) : (𝟙 F : F ⟶ F).app X = 𝟙 (F.obj X) := rfl @[simp] lemma comp_app {F G H : C ⥤ D} (α : F ⟶ G) (β : G ⟶ H) (X : C) : (α ≫ β).app X = α.app X ≫ β.app X := rfl lemma app_naturality {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (X : C) {Y Z : D} (f : Y ⟶ Z) : ((F.obj X).map f) ≫ ((T.app X).app Z) = ((T.app X).app Y) ≫ ((G.obj X).map f) := (T.app X).naturality f lemma naturality_app {F G : C ⥤ (D ⥤ E)} (T : F ⟶ G) (Z : D) {X Y : C} (f : X ⟶ Y) : ((F.map f).app Z) ≫ ((T.app Y).app Z) = ((T.app X).app Z) ≫ ((G.map f).app Z) := congr_fun (congr_arg app (T.naturality f)) Z /-- A natural transformation is a monomorphism if each component is. -/ lemma mono_app_of_mono (α : F ⟶ G) [∀ (X : C), mono (α.app X)] : mono α := ⟨λ H g h eq, by { ext X, rw [←cancel_mono (α.app X), ←comp_app, eq, comp_app] }⟩ /-- A natural transformation is an epimorphism if each component is. -/ lemma epi_app_of_epi (α : F ⟶ G) [∀ (X : C), epi (α.app X)] : epi α := ⟨λ H g h eq, by { ext X, rw [←cancel_epi (α.app X), ←comp_app, eq, comp_app] }⟩ /-- `hcomp α β` is the horizontal composition of natural transformations. -/ def hcomp {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) : (F ⋙ H) ⟶ (G ⋙ I) := { app := λ X : C, (β.app (F.obj X)) ≫ (I.map (α.app X)), naturality' := λ X Y f, begin rw [functor.comp_map, functor.comp_map, ←assoc, naturality, assoc, ←map_comp I, naturality, map_comp, assoc] end } infix ` ◫ `:80 := hcomp @[simp] lemma hcomp_app {H I : D ⥤ E} (α : F ⟶ G) (β : H ⟶ I) (X : C) : (α ◫ β).app X = (β.app (F.obj X)) ≫ (I.map (α.app X)) := rfl @[simp] lemma hcomp_id_app {H : D ⥤ E} (α : F ⟶ G) (X : C) : (α ◫ 𝟙 H).app X = H.map (α.app X) := by {dsimp, simp} -- See note [dsimp, simp]. lemma id_hcomp_app {H : E ⥤ C} (α : F ⟶ G) (X : E) : (𝟙 H ◫ α).app X = α.app _ := by simp -- Note that we don't yet prove a `hcomp_assoc` lemma here: even stating it is painful, because we -- need to use associativity of functor composition. (It's true without the explicit associator, -- because functor composition is definitionally associative, but relying on the definitional equality -- causes bad problems with elaboration later.) lemma exchange {I J K : D ⥤ E} (α : F ⟶ G) (β : G ⟶ H) (γ : I ⟶ J) (δ : J ⟶ K) : (α ≫ β) ◫ (γ ≫ δ) = (α ◫ γ) ≫ (β ◫ δ) := by ext; simp end nat_trans open nat_trans namespace functor /-- Flip the arguments of a bifunctor. See also `currying.lean`. -/ protected def flip (F : C ⥤ (D ⥤ E)) : D ⥤ (C ⥤ E) := { obj := λ k, { obj := λ j, (F.obj j).obj k, map := λ j j' f, (F.map f).app k, map_id' := λ X, begin rw category_theory.functor.map_id, refl end, map_comp' := λ X Y Z f g, by rw [map_comp, ←comp_app] }, map := λ c c' f, { app := λ j, (F.obj j).map f } }. @[simp] lemma flip_obj_obj (F : C ⥤ (D ⥤ E)) (c) (d) : (F.flip.obj d).obj c = (F.obj c).obj d := rfl @[simp] lemma flip_obj_map (F : C ⥤ (D ⥤ E)) {c c' : C} (f : c ⟶ c') (d : D) : (F.flip.obj d).map f = (F.map f).app d := rfl @[simp] lemma flip_map_app (F : C ⥤ (D ⥤ E)) {d d' : D} (f : d ⟶ d') (c : C) : (F.flip.map f).app c = (F.obj c).map f := rfl end functor end category_theory
38bfc79a0372743878983c59d3d8c2e4eef1188b
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/full_subcategory.lean
3be4a3958a199d71959a779844b41b34f5ab35c3
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
3,686
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton -/ import category_theory.fully_faithful namespace category_theory universes v u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation section induced /- Induced categories. Given a category D and a function F : C → D from a type C to the objects of D, there is an essentially unique way to give C a category structure such that F becomes a fully faithful functor, namely by taking Hom_C(X, Y) = Hom_D(FX, FY). We call this the category induced from D along F. As a special case, if C is a subtype of D, this produces the full subcategory of D on the objects belonging to C. In general the induced category is equivalent to the full subcategory of D on the image of F. -/ /- It looks odd to make D an explicit argument of `induced_category`, when it is determined by the argument F anyways. The reason to make D explicit is in order to control its syntactic form, so that instances like `induced_category.has_forget₂` (elsewhere) refer to the correct form of D. This is used to set up several algebraic categories like def CommMon : Type (u+1) := induced_category Mon (bundled.map @comm_monoid.to_monoid) -- not `induced_category (bundled monoid) (bundled.map @comm_monoid.to_monoid)`, -- even though `Mon = bundled monoid`! -/ variables {C : Type u₁} (D : Type u₂) [category.{v} D] variables (F : C → D) include F /-- `induced_category D F`, where `F : C → D`, is a typeclass synonym for `C`, which provides a category structure so that the morphisms `X ⟶ Y` are the morphisms in `D` from `F X` to `F Y`. -/ @[nolint has_inhabited_instance unused_arguments] def induced_category : Type u₁ := C variables {D} instance induced_category.has_coe_to_sort [has_coe_to_sort D] : has_coe_to_sort (induced_category D F) := ⟨_, λ c, ↥(F c)⟩ instance induced_category.category : category.{v} (induced_category D F) := { hom := λ X Y, F X ⟶ F Y, id := λ X, 𝟙 (F X), comp := λ _ _ _ f g, f ≫ g } /-- The forgetful functor from an induced category to the original category, forgetting the extra data. -/ @[simps] def induced_functor : induced_category D F ⥤ D := { obj := F, map := λ x y f, f } instance induced_category.full : full (induced_functor F) := { preimage := λ x y f, f } instance induced_category.faithful : faithful (induced_functor F) := {} end induced section full_subcategory /- A full subcategory is the special case of an induced category with F = subtype.val. -/ variables {C : Type u₂} [category.{v} C] variables (Z : C → Prop) /-- The category structure on a subtype; morphisms just ignore the property. See https://stacks.math.columbia.edu/tag/001D. We do not define 'strictly full' subcategories. -/ instance full_subcategory : category.{v} {X : C // Z X} := induced_category.category subtype.val /-- The forgetful functor from a full subcategory into the original category ("forgetting" the condition). -/ def full_subcategory_inclusion : {X : C // Z X} ⥤ C := induced_functor subtype.val @[simp] lemma full_subcategory_inclusion.obj {X} : (full_subcategory_inclusion Z).obj X = X.val := rfl @[simp] lemma full_subcategory_inclusion.map {X Y} {f : X ⟶ Y} : (full_subcategory_inclusion Z).map f = f := rfl instance full_subcategory.full : full (full_subcategory_inclusion Z) := induced_category.full subtype.val instance full_subcategory.faithful : faithful (full_subcategory_inclusion Z) := induced_category.faithful subtype.val end full_subcategory end category_theory
0c9b2d62be8598d10dda45be5c38ca01873046fc
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/calc_tac.lean
9d7657367ebf1fe9ce5f6a33ded880fff2990721
[ "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
362
lean
axiom addz {A : Type} [has_add A] [has_zero A] : ∀ a : A, a + 0 = a axiom addc {A : Type} [has_add A] : ∀ a b : A, a + b = b + a example {A : Type} [has_add A] [has_zero A] [has_one A] (a b c : A) : b = 0 → a + b + c = c + a := assume h, calc a + b + c = a + 0 + c : by rw h ... = a + c : by rw addz ... = c + a : by rw addc