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
66bd49dd888cdaefd51a4f2caea9b3c333d3d88a
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/lean/CompilerElimDeadBranches.lean
12e190918d1ba73964032acb8878e46d5fc67a98
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
1,375
lean
def someVal (_x : Nat) : Option Nat := some 0 /- This test demonstrates two things: 1. We eliminate all branches except the some, some one 2. We communicate correctly to the constant folder that the `n` and `m` are always 0 and can thus collapse the computation. -/ set_option trace.Compiler.elimDeadBranches true in set_option trace.Compiler.result true in def addSomeVal (x : Nat) := match someVal x, someVal x with | some n, some m => some (n + m) | _, _ => none def throwMyError (m n : Nat) : Except String Unit := throw s!"Ahhhh {m + n}" /- This demonstrates that the optimization does do good things to monadic code. In this snippet Lean would usually perform a cases on the result of `throwMyError` in order to figure out whether it has to: - raise an error and exit right now - jump to the the `return x + y` continuation Since the abstract interpreter knows that `throwMyError` always returns an `Except.error` it will drop the branch where we jump to the continuation. This will in turn allow the simplifier to drop the join point that represents the continuation, saving us more code size. -/ set_option trace.Compiler.elimDeadBranches true in set_option trace.Compiler.result true in def monadic (x y : Nat) : Except String Nat := do if let some m := addSomeVal x then if let some n := addSomeVal y then throwMyError m n return x + y
82b357402976077a0a3e36e7a6100b26819e68b5
12ba6fe891179eac82e287c24c8812a046221563
/src/compressions/UV.lean
549963efc4354df3dfd74c8624e54c017d78b51a
[]
no_license
b-mehta/combinatorics
eca586b4cb7b5e1fd22ec3288f5a2cb4ed6ce4dd
2a8b30709d35f124f3fc9baa5652d231389e9f63
refs/heads/master
1,653,708,057,336
1,626,885,184,000
1,626,885,184,000
228,850,404
19
0
null
null
null
null
UTF-8
Lean
false
false
19,035
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import data.finset import data.fintype.basic import to_mathlib import basic import shadows import colex /-! # compressions/UV UV compressions are immensely useful to prove the Kruskal-Katona theorem. The idea is that compressing a set family might decrease the size of its shadow, and so iterated compressions should hopefully minimise the shadow. The main result proved is: theorem compression_reduces_shadow {𝒜 : finset (finset α)} {U V : finset α} (h₁ : ∀ x ∈ U, ∃ y ∈ V, is_compressed (erase U x) (erase V y) 𝒜) (h₂ : U.card = V.card) : (∂ compress_family U V 𝒜).card ≤ (∂𝒜).card It says that the shadow size decreases under particular conditions - most importantly, that for every x ∈ U, there is y ∈ V such that the family is (U-x, V-y) compressed. ## Tags compression, UV-compression, shadow ## References * http://b-mehta.github.io/maths-notes/iii/mich/combinatorics.pdf -/ open fintype open finset variable {α : Type*} variables [decidable_eq α] variables {n : ℕ} -- The namespace here is useful to distinguish between other compressions. namespace UV /-- To UV-compress A, if it doesn't touch U and does contain V, we remove V and put U in. We'll only really use this when |U| = |V| and U ∩ V = ∅. -/ def compress (U V : finset α) (A : finset α) := if disjoint U A ∧ (V ⊆ A) then (A ∪ U) \ V else A /-- Compression doesn't change the size of a set. -/ lemma compress_size (U V : finset α) (A : finset α) (h₁ : U.card = V.card) : (compress U V A).card = A.card := begin rw compress, split_ifs, rw [card_sdiff (subset.trans h.2 (subset_union_left _ _)), card_disjoint_union h.1.symm, h₁, nat.add_sub_cancel], refl end /-- Compressing a set is idempotent. -/ lemma compress_idem (U V : finset α) (A : finset α) : compress U V (compress U V A) = compress U V A := begin simp only [compress], split_ifs, { suffices : U = ∅, { rw this, simp }, have : U \ V = U := sdiff_eq_self_of_disjoint (disjoint_of_subset_right h.2 h.1), rw ←disjoint_self_iff_empty, apply disjoint_of_subset_right (subset_union_right (A \ V) _), rw [union_sdiff_distrib, ‹U \ V = U›] at h_1, apply h_1.1 }, { refl }, { refl }, end /-- Part of the compressed family, where we keep sets whose compression is already present. -/ @[reducible] def compress_remains (U V : finset α) (𝒜 : finset (finset α)) := 𝒜.filter (λ A, compress U V A ∈ 𝒜) /-- Part of the compressed family, where we move the sets whose compression is not there. -/ @[reducible] def compress_motion (U V : finset α) (𝒜 : finset (finset α)) := (𝒜.filter (λ A, compress U V A ∉ 𝒜)).image (λ A, compress U V A) /-- To UV-compress a set family, we keep all the sets whose compression is present, and move all the sets whose compression is not there (and take this union). -/ def compress_family (U V : finset α) (𝒜 : finset (finset α)) := compress_motion U V 𝒜 ∪ compress_remains U V 𝒜 local notation `CC` := compress_family lemma mem_compress_remains {𝒜 : finset (finset α)} (U V A : finset α) : A ∈ compress_remains U V 𝒜 ↔ A ∈ 𝒜 ∧ compress U V A ∈ 𝒜 := by rw mem_filter lemma mem_compress_motion {𝒜 : finset (finset α)} (U V A : finset α) : A ∈ compress_motion U V 𝒜 ↔ A ∉ 𝒜 ∧ (∃ B ∈ 𝒜, compress U V B = A) := begin simp [compress_motion], split; rintro ⟨p, q, r⟩, exact ⟨r ▸ q.2, p, ⟨q.1, r⟩⟩, exact ⟨q, ⟨r.1, r.2.symm ▸ p⟩, r.2⟩, end /-- `A` is in the UV-compressed family iff it's in the original and its compression is in the original, or it's not in the original but it's the compression of something in the original -/ lemma mem_compress {𝒜 : finset (finset α)} (U V : finset α) {A : finset α} : A ∈ CC U V 𝒜 ↔ (A ∉ 𝒜 ∧ (∃ B ∈ 𝒜, compress U V B = A)) ∨ (A ∈ 𝒜 ∧ compress U V A ∈ 𝒜) := by rw [compress_family, mem_union, mem_compress_remains, mem_compress_motion] /-- `is_compressed U V 𝒜` expresses that 𝒜 is UV-compressed -/ @[reducible] def is_compressed (U V : finset α) (𝒜 : finset (finset α)) := CC U V 𝒜 = 𝒜 /-- The empty family is compressed. -/ lemma is_compressed_empty (𝒜 : finset (finset α)) : is_compressed ∅ ∅ 𝒜 := begin have q : ∀ (A : finset α), compress ∅ ∅ A = A, simp [compress], rw [is_compressed, compress_family, compress_motion, compress_remains], simp only [q, filter_congr_decidable, imp_self, forall_const, filter_true_of_mem, union_eq_right_iff_subset], intros A hA, simpa using hA, end /-- Compressing a set doesn't change it's size, so compressing a family keeps all sets the same size. -/ lemma compress_family_sized {r : ℕ} {𝒜 : finset (finset α)} {U V : finset α} (h₁ : U.card = V.card) (h₂ : all_sized 𝒜 r) : all_sized (CC U V 𝒜) r := begin intros A HA, rw mem_compress at HA, rcases HA with ⟨_, _, z₁, rfl⟩ | ⟨z₁, _⟩, rw compress_size _ _ _ h₁, all_goals {apply h₂ _ z₁} end /-- Compressing a family is idempotent. -/ lemma compress_family_idempotent (U V : finset α) (𝒜 : finset (finset α)) : CC U V (CC U V 𝒜) = CC U V 𝒜 := begin have: ∀ A ∈ CC U V 𝒜, compress U V A ∈ CC U V 𝒜, intros A HA, rw mem_compress at HA ⊢, simp [compress_idem], rcases HA with ⟨_, B, _, rfl⟩ | ⟨_, _⟩, left, refine ⟨_, B, ‹_›, _⟩; rwa compress_idem, right, assumption, have: filter (λ A, compress U V A ∉ CC U V 𝒜) (CC U V 𝒜) = ∅, rw ← filter_false (CC U V 𝒜), apply filter_congr, simpa, rw [compress_family, compress_motion, this, image_empty, union_comm, compress_remains, ← this], apply filter_union_filter_neg_eq _ (compress_family U V 𝒜) end lemma compress_disjoint {𝒜 : finset (finset α)} (U V : finset α) : disjoint (compress_motion U V 𝒜) (compress_remains U V 𝒜) := begin rw disjoint_left, intros A HA HB, rw mem_compress_remains at HB, rw mem_compress_motion at HA, exact HA.1 HB.1 end /-- Compression is kinda injective. -/ lemma inj_ish {U V : finset α} {A B : finset α} (hA : disjoint U A ∧ V ⊆ A) (hB : disjoint U B ∧ V ⊆ B) (Z : (A ∪ U) \ V = (B ∪ U) \ V) : A = B := begin ext x, split, all_goals { intro p, by_cases h₁: (x ∈ V), { exact hB.2 h₁ <|> exact hA.2 h₁ }, have := mem_sdiff.2 ⟨mem_union_left U ‹_›, h₁⟩, rw Z at this <|> rw ← Z at this, rw [mem_sdiff, mem_union] at this, suffices: x ∉ U, tauto, apply disjoint_right.1 ‹disjoint _ _ ∧ _›.1 p } end /-- Compressing a set family doesn't change its size. -/ lemma compressed_size {𝒜 : finset (finset α)} (U V : finset α) : (CC U V 𝒜).card = 𝒜.card := begin rw [compress_family, card_disjoint_union (compress_disjoint _ _), card_image_of_inj_on], rw [← card_disjoint_union, union_comm, filter_union_filter_neg_eq], rw [disjoint_iff_inter_eq_empty, inter_comm], apply filter_inter_filter_neg_eq, intros A HA B HB Z, dsimp only at Z, rw [mem_coe, mem_filter] at HA HB, rw [compress] at HA Z, split_ifs at HA Z, { rw compress at HB Z, split_ifs at HB Z, { exact inj_ish h h_1 Z }, tauto }, tauto end /-- If A is in the compressed family but V is a subset of A, A must have been in the original family. -/ lemma compress_held {𝒜 : finset (finset α)} {U V A : finset α} (h₁ : A ∈ CC U V 𝒜) (h₂ : V ⊆ A) (h₃ : U.card = V.card) : A ∈ 𝒜 := begin rw mem_compress at h₁, rcases h₁ with ⟨_, B, H, HB⟩ | _, rw compress at HB, split_ifs at HB, have: V = ∅, apply eq_empty_of_forall_not_mem, intros x xV, replace h₂ := h₂ xV, rw [← HB, mem_sdiff] at h₂, exact h₂.2 xV, have: U = ∅, rwa [← card_eq_zero, h₃, card_eq_zero], rw [‹U = ∅›, ‹V = ∅›, union_empty, sdiff_empty] at HB, rwa ← HB, rwa ← HB, tauto end /-- If A is not in the original family but is in the compressed family, then A has been compressed, and its original was in the original family. -/ lemma compress_moved {𝒜 : finset (finset α)} {U V A : finset α} (h₁ : A ∈ CC U V 𝒜) (h₂ : A ∉ 𝒜) : U ⊆ A ∧ disjoint V A ∧ (A ∪ V) \ U ∈ 𝒜 := begin rw mem_compress at h₁, rcases h₁ with ⟨_, B, H, HB⟩ | _, { rw compress at HB, split_ifs at HB, { rw ← HB at *, refine ⟨_, disjoint_sdiff, _⟩, have : disjoint U V := disjoint_of_subset_right h.2 h.1, rw union_sdiff_distrib, rw sdiff_eq_self_of_disjoint this, apply subset_union_right _ _, rwa [sdiff_union_of_subset, union_sdiff_self, sdiff_eq_self_of_disjoint h.1.symm], apply trans h.2 (subset_union_left _ _) }, { rw HB at *, tauto } }, tauto end /-- If A is in the compressed family and does move under compression, then the compressed version was in the original family. -/ lemma uncompressed_was_already_there {𝒜 : finset (finset α)} {U V A : finset α} (h₁ : A ∈ CC U V 𝒜) (h₂ : V ⊆ A) (h₃ : disjoint U A) : (A ∪ U) \ V ∈ 𝒜 := begin rw mem_compress at h₁, have: disjoint U A ∧ V ⊆ A := ⟨h₃, h₂⟩, rcases h₁ with ⟨_, B, B_in_A, cB_eq_A⟩ | ⟨_, cA_in_A⟩, { by_cases a: (A ∪ U) \ V = A, have: U \ V = U, apply sdiff_eq_self_of_disjoint, apply (disjoint_of_subset_right h₂ h₃), have: U = ∅, rw ← disjoint_self_iff_empty, suffices: disjoint U (U \ V), rw ‹U \ V = U› at this, assumption, apply disjoint_of_subset_right (subset_union_right (A \ V) _), rwa [← union_sdiff_distrib, a], have: V = ∅, rw ← disjoint_self_iff_empty, apply disjoint_of_subset_right h₂, rw ← a, apply disjoint_sdiff, simpa [a, cB_eq_A.symm, compress, ‹U = ∅›, ‹V = ∅›], have: compress U V A = (A ∪ U) \ V, rw compress, split_ifs, refl, exfalso, apply a, rw [← this, ← cB_eq_A, compress_idem] }, { rw compress at cA_in_A, split_ifs at cA_in_A, assumption } end example {α : Type*} [decidable_eq α] (x : α) (s : finset α) : insert x s = {x} ∪ s := begin rw insert_eq, end /-- Here's the key fact about compression for KK. If, for all `x ∈ U` there is `y ∈ V` such that `𝒜` is `(U-x,V-y)`-compressed, then UV-compression will reduce the size of `𝒜`'s shadow. -/ theorem compression_reduces_shadow {𝒜 : finset (finset α)} {U V : finset α} (h₁ : ∀ x ∈ U, ∃ y ∈ V, is_compressed (erase U x) (erase V y) 𝒜) (h₂ : U.card = V.card) : (∂ CC U V 𝒜).card ≤ (∂𝒜).card := begin set 𝒜' := compress_family U V 𝒜, suffices: (∂𝒜' \ ∂𝒜).card ≤ (∂𝒜 \ ∂𝒜').card, { suffices z : (∂𝒜' \ ∂𝒜 ∪ ∂𝒜' ∩ ∂𝒜).card ≤ (∂𝒜 \ ∂𝒜' ∪ ∂𝒜 ∩ ∂𝒜').card, rwa [sdiff_union_inter, sdiff_union_inter] at z, rw [card_disjoint_union, card_disjoint_union, inter_comm], apply add_le_add_right ‹_›, { apply disjoint_sdiff_inter }, { apply disjoint_sdiff_inter } }, -- We'll define an injection ∂𝒜' \ ∂𝒜 → ∂𝒜 \ ∂𝒜'. First, let's prove -- a few facts about things in the domain: have q₁: ∀ B ∈ ∂𝒜' \ ∂𝒜, U ⊆ B ∧ disjoint V B ∧ (B ∪ V) \ U ∈ ∂𝒜 \ ∂𝒜', intros B HB, obtain ⟨k, k'⟩: B ∈ ∂𝒜' ∧ B ∉ ∂𝒜 := mem_sdiff.1 HB, -- This is gonna be useful a couple of times so let's name it. have m: ∀ y ∉ B, insert y B ∉ 𝒜 := λ y H a, k' (mem_shadow'.2 ⟨y, H, a⟩), rcases mem_shadow'.1 k with ⟨x, _, _⟩, have q := compress_moved ‹insert x B ∈ 𝒜'› (m _ ‹x ∉ B›), have: disjoint V B := (disjoint_insert_right.1 q.2.1).2, have: disjoint V U := disjoint_of_subset_right q.1 q.2.1, have: V \ U = V := sdiff_eq_self_of_disjoint ‹_›, -- The first key part is that x ∉ U have: x ∉ U, intro a, rcases h₁ x ‹x ∈ U› with ⟨y, Hy, xy_comp⟩, -- If `x ∈ U`, we can get `y ∈ V` so that `𝒜` is `(U-x,V-y)`-compressed apply m y (disjoint_left.1 ‹disjoint V B› Hy), -- and we'll use this `y` to contradict `m`. rw is_compressed at xy_comp, have: (insert x B ∪ V) \ U ∈ compress_family (erase U x) (erase V y) 𝒜, rw xy_comp, exact q.2.2, -- So we'd like to show insert y B ∈ 𝒜. -- We do this by showing the below have: ((insert x B ∪ V) \ U ∪ erase U x) \ erase V y ∈ 𝒜, apply uncompressed_was_already_there this _, apply disjoint_of_subset_left (erase_subset _ _) disjoint_sdiff, rw [union_sdiff_distrib, ‹V \ U = V›], apply subset.trans (erase_subset _ _) (subset_union_right _ _), -- and then arguing that it's the same suffices: ((insert x B ∪ V) \ U ∪ erase U x) \ erase V y = insert y B, rwa ← this, have: x ∉ B ∪ V := λ z, disjoint_left.1 ‹disjoint V U› ((mem_union.1 z).resolve_left ‹x ∉ B›) ‹x ∈ U›, have: erase U x ⊆ insert x B ∪ V := trans (erase_subset x _) (trans q.1 (subset_union_left _ V)), -- which is just a pain. exact calc (((insert x B ∪ V) \ U) ∪ erase U x) \ erase V y = (((insert x B ∪ V) \ {x} ∪ erase U x) ∩ ((insert x B ∪ V) \ erase U x ∪ erase U x)) \ erase V y : by rw [← union_distrib_right, ← sdiff_union_distrib, ←insert_eq, insert_erase a] ... = (B ∪ V) \ erase V y : by rw [sdiff_union_of_subset ‹erase U x ⊆ _›, sdiff_singleton_eq_erase, insert_union, erase_insert ‹x ∉ B ∪ V›, insert_eq, union_comm, ← union_distrib_right, inter_singleton_of_not_mem (not_mem_erase _ _), empty_union] ... = (insert y B ∪ erase V y) \ erase V y : by rw [insert_eq, union_comm _ B, union_assoc, ←insert_eq, insert_erase ‹y ∈ V›] ... = insert y B : begin rw [union_sdiff_self, finset.sdiff_eq_self_iff_disjoint, disjoint_insert_left], refine ⟨not_mem_erase _ _, _⟩, apply disjoint_of_subset_right (erase_subset _ _), exact ‹disjoint V B›.symm end, -- Now that that's done, it's immediate that U ⊆ B have: U ⊆ B, rw [← erase_eq_of_not_mem ‹x ∉ U›, ← subset_insert_iff], exact q.1, -- and we already had that V and B are disjoint refine ⟨‹_›, ‹_›, _⟩, -- so it only remains to get (B ∪ V) \ U ∈ ∂𝒜 \ ∂𝒜' rw mem_sdiff, have: x ∉ V := disjoint_right.1 q.2.1 (mem_insert_self _ _), split, -- (B ∪ V) \ U ∈ ∂𝒜 is pretty direct: rw mem_shadow', refine ⟨x, _, _⟩, { simp [mem_sdiff, mem_union], tauto! }, convert q.2.2, rw [insert_eq, insert_eq, union_assoc, union_sdiff_distrib _ (B ∪ V), sdiff_eq_self_of_disjoint (singleton_disjoint.2 ‹x ∉ U›)], -- For (B ∪ V) \ U ∉ ∂𝒜', we split up based on w ∈ U rw mem_shadow', rintro ⟨w, a_h_w, a_h_h⟩, by_cases (w ∈ U), -- If w ∈ U, we find z ∈ V, and contradict m again rcases h₁ w ‹w ∈ U› with ⟨z, Hz, xy_comp⟩, apply m z (disjoint_left.1 ‹disjoint V B› Hz), have: insert w ((B ∪ V) \ U) ∈ 𝒜, { apply compress_held a_h_h _ h₂, apply subset.trans _ (subset_insert _ _), rw union_sdiff_distrib, rw ‹V \ U = V›, apply subset_union_right }, have: (insert w ((B ∪ V) \ U) ∪ erase U w) \ erase V z ∈ 𝒜, refine uncompressed_was_already_there _ _ _, rw is_compressed at xy_comp, rwa xy_comp, apply subset.trans (erase_subset _ _), apply subset.trans _ (subset_insert _ _), rw [union_sdiff_distrib, ‹V \ U = V›], apply subset_union_right, rw disjoint_insert_right, split, apply not_mem_erase, apply disjoint_of_subset_left (erase_subset _ _), apply disjoint_sdiff, have: (insert w ((B ∪ V) \ U) ∪ erase U w) \ erase V z = insert z B, rw [insert_union, ← union_insert, insert_erase h, sdiff_union_of_subset (subset.trans ‹U ⊆ B› (subset_union_left _ _)), union_sdiff_distrib, sdiff_eq_self_of_disjoint (disjoint_of_subset_right (erase_subset _ _) ‹disjoint V B›.symm), ← sdiff_singleton_eq_erase, sdiff_sdiff_self_left, inter_singleton_of_mem Hz, union_comm, ←insert_eq], rwa ← this, -- If w ∉ U, we contradict m again rw [mem_sdiff, ← not_imp, not_not] at a_h_w, have: w ∉ V := h ∘ a_h_w ∘ mem_union_right _, have: w ∉ B := h ∘ a_h_w ∘ mem_union_left _, apply m w this, have: (insert w ((B ∪ V) \ U) ∪ U) \ V ∈ 𝒜, refine uncompressed_was_already_there ‹insert w ((B ∪ V) \ U) ∈ 𝒜'› (trans _ (subset_insert _ _)) _, rw [union_sdiff_distrib, ‹V \ U = V›], apply subset_union_right, rw disjoint_insert_right, exact ⟨‹_›, disjoint_sdiff⟩, convert this, rw [insert_union, sdiff_union_of_subset (trans ‹U ⊆ B› (subset_union_left _ _)), ← insert_union, union_sdiff_self], symmetry, rw [finset.sdiff_eq_self_iff_disjoint, disjoint_insert_left], split, assumption, rwa disjoint.comm, apply card_le_card_of_inj_on (λ B, (B ∪ V) \ U) (λ B HB, (q₁ B HB).2.2), intros B₁ HB₁ B₂ HB₂ k, exact inj_ish ⟨(q₁ B₁ HB₁).2.1, (q₁ B₁ HB₁).1⟩ ⟨(q₁ B₂ HB₂).2.1, (q₁ B₂ HB₂).1⟩ k end end UV
d7f813caebb720839648ab3524b94ac5385c193a
367134ba5a65885e863bdc4507601606690974c1
/src/category_theory/limits/shapes/biproducts.lean
cd3e84f323129c7527a118c5e2ba0d77b5d28cb1
[ "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
37,803
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import category_theory.limits.shapes.finite_products import category_theory.limits.shapes.binary_products import category_theory.preadditive /-! # Biproducts and binary biproducts We introduce the notion of (finite) biproducts and binary biproducts. These are slightly unusual relative to the other shapes in the library, as they are simultaneously limits and colimits. (Zero objects are similar; they are "biterminal".) We treat first the case of a general category with zero morphisms, and subsequently the case of a preadditive category. In a category with zero morphisms, we model the (binary) biproduct of `P Q : C` using a `binary_bicone`, which has a cone point `X`, and morphisms `fst : X ⟶ P`, `snd : X ⟶ Q`, `inl : P ⟶ X` and `inr : X ⟶ Q`, such that `inl ≫ fst = 𝟙 P`, `inl ≫ snd = 0`, `inr ≫ fst = 0`, and `inr ≫ snd = 𝟙 Q`. Such a `binary_bicone` is a biproduct if the cone is a limit cone, and the cocone is a colimit cocone. In a preadditive category, * any `binary_biproduct` satisfies `total : fst ≫ inl + snd ≫ inr = 𝟙 X` * any `binary_product` is a `binary_biproduct` * any `binary_coproduct` is a `binary_biproduct` For biproducts indexed by a `fintype J`, a `bicone` again consists of a cone point `X` and morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`, such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. In a preadditive category, * any `biproduct` satisfies `total : ∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f)` * any `product` is a `biproduct` * any `coproduct` is a `biproduct` ## Notation As `⊕` is already taken for the sum of types, we introduce the notation `X ⊞ Y` for a binary biproduct. We introduce `⨁ f` for the indexed biproduct. -/ noncomputable theory universes v u open category_theory open category_theory.functor namespace category_theory.limits variables {J : Type v} [decidable_eq J] variables {C : Type u} [category.{v} C] [has_zero_morphisms C] /-- A `c : bicone F` is: * an object `c.X` and * morphisms `π j : X ⟶ F j` and `ι j : F j ⟶ X` for each `j`, * such that `ι j ≫ π j'` is the identity when `j = j'` and zero otherwise. -/ @[nolint has_inhabited_instance] structure bicone (F : J → C) := (X : C) (π : Π j, X ⟶ F j) (ι : Π j, F j ⟶ X) (ι_π : ∀ j j', ι j ≫ π j' = if h : j = j' then eq_to_hom (congr_arg F h) else 0) @[simp] lemma bicone_ι_π_self {F : J → C} (B : bicone F) (j : J) : B.ι j ≫ B.π j = 𝟙 (F j) := by simpa using B.ι_π j j @[simp] lemma bicone_ι_π_ne {F : J → C} (B : bicone F) {j j' : J} (h : j ≠ j') : B.ι j ≫ B.π j' = 0 := by simpa [h] using B.ι_π j j' variables {F : J → C} namespace bicone /-- Extract the cone from a bicone. -/ @[simps] def to_cone (B : bicone F) : cone (discrete.functor F) := { X := B.X, π := { app := λ j, B.π j }, } /-- Extract the cocone from a bicone. -/ @[simps] def to_cocone (B : bicone F) : cocone (discrete.functor F) := { X := B.X, ι := { app := λ j, B.ι j }, } end bicone /-- A bicone over `F : J → C`, which is both a limit cone and a colimit cocone. -/ @[nolint has_inhabited_instance] structure limit_bicone (F : J → C) := (bicone : bicone F) (is_limit : is_limit bicone.to_cone) (is_colimit : is_colimit bicone.to_cocone) /-- `has_biproduct F` expresses the mere existence of a bicone which is simultaneously a limit and a colimit of the diagram `F`. -/ class has_biproduct (F : J → C) : Prop := mk' :: (exists_biproduct : nonempty (limit_bicone F)) lemma has_biproduct.mk {F : J → C} (d : limit_bicone F) : has_biproduct F := ⟨nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `biproduct_data F` from `has_biproduct F`. -/ def get_biproduct_data (F : J → C) [has_biproduct F] : limit_bicone F := classical.choice has_biproduct.exists_biproduct /-- A bicone for `F` which is both a limit cone and a colimit cocone. -/ def biproduct.bicone (F : J → C) [has_biproduct F] : bicone F := (get_biproduct_data F).bicone /-- `biproduct.bicone F` is a limit cone. -/ def biproduct.is_limit (F : J → C) [has_biproduct F] : is_limit (biproduct.bicone F).to_cone := (get_biproduct_data F).is_limit /-- `biproduct.bicone F` is a colimit cocone. -/ def biproduct.is_colimit (F : J → C) [has_biproduct F] : is_colimit (biproduct.bicone F).to_cocone := (get_biproduct_data F).is_colimit @[priority 100] instance has_product_of_has_biproduct [has_biproduct F] : has_limit (discrete.functor F) := has_limit.mk { cone := (biproduct.bicone F).to_cone, is_limit := biproduct.is_limit F, } @[priority 100] instance has_coproduct_of_has_biproduct [has_biproduct F] : has_colimit (discrete.functor F) := has_colimit.mk { cocone := (biproduct.bicone F).to_cocone, is_colimit := biproduct.is_colimit F, } variables (J C) /-- `C` has biproducts of shape `J` if we have a limit and a colimit, with the same cone points, of every function `F : J → C`. -/ class has_biproducts_of_shape : Prop := (has_biproduct : Π F : J → C, has_biproduct F) attribute [instance, priority 100] has_biproducts_of_shape.has_biproduct /-- `has_finite_biproducts C` represents a choice of biproduct for every family of objects in `C` indexed by a finite type with decidable equality. -/ class has_finite_biproducts : Prop := (has_biproducts_of_shape : Π (J : Type v) [decidable_eq J] [fintype J], has_biproducts_of_shape J C) attribute [instance, priority 100] has_finite_biproducts.has_biproducts_of_shape @[priority 100] instance has_finite_products_of_has_finite_biproducts [has_finite_biproducts C] : has_finite_products C := λ J _ _, ⟨λ F, by exactI has_limit_of_iso discrete.nat_iso_functor.symm⟩ @[priority 100] instance has_finite_coproducts_of_has_finite_biproducts [has_finite_biproducts C] : has_finite_coproducts C := λ J _ _, ⟨λ F, by exactI has_colimit_of_iso discrete.nat_iso_functor⟩ variables {J C} /-- The isomorphism between the specified limit and the specified colimit for a functor with a bilimit. -/ def biproduct_iso (F : J → C) [has_biproduct F] : limits.pi_obj F ≅ limits.sigma_obj F := (is_limit.cone_point_unique_up_to_iso (limit.is_limit _) (biproduct.is_limit F)).trans $ is_colimit.cocone_point_unique_up_to_iso (biproduct.is_colimit F) (colimit.is_colimit _) end category_theory.limits namespace category_theory.limits variables {J : Type v} [decidable_eq J] variables {C : Type u} [category.{v} C] [has_zero_morphisms C] /-- `biproduct f` computes the biproduct of a family of elements `f`. (It is defined as an abbreviation for `limit (discrete.functor f)`, so for most facts about `biproduct f`, you will just use general facts about limits and colimits.) -/ abbreviation biproduct (f : J → C) [has_biproduct f] : C := (biproduct.bicone f).X notation `⨁ ` f:20 := biproduct f /-- The projection onto a summand of a biproduct. -/ abbreviation biproduct.π (f : J → C) [has_biproduct f] (b : J) : ⨁ f ⟶ f b := (biproduct.bicone f).π b @[simp] lemma biproduct.bicone_π (f : J → C) [has_biproduct f] (b : J) : (biproduct.bicone f).π b = biproduct.π f b := rfl /-- The inclusion into a summand of a biproduct. -/ abbreviation biproduct.ι (f : J → C) [has_biproduct f] (b : J) : f b ⟶ ⨁ f := (biproduct.bicone f).ι b @[simp] lemma biproduct.bicone_ι (f : J → C) [has_biproduct f] (b : J) : (biproduct.bicone f).ι b = biproduct.ι f b := rfl @[reassoc] lemma biproduct.ι_π (f : J → C) [has_biproduct f] (j j' : J) : biproduct.ι f j ≫ biproduct.π f j' = if h : j = j' then eq_to_hom (congr_arg f h) else 0 := (biproduct.bicone f).ι_π j j' @[simp,reassoc] lemma biproduct.ι_π_self (f : J → C) [has_biproduct f] (j : J) : biproduct.ι f j ≫ biproduct.π f j = 𝟙 _ := by simp [biproduct.ι_π] @[simp,reassoc] lemma biproduct.ι_π_ne (f : J → C) [has_biproduct f] {j j' : J} (h : j ≠ j') : biproduct.ι f j ≫ biproduct.π f j' = 0 := by simp [biproduct.ι_π, h] /-- Given a collection of maps into the summands, we obtain a map into the biproduct. -/ abbreviation biproduct.lift {f : J → C} [has_biproduct f] {P : C} (p : Π b, P ⟶ f b) : P ⟶ ⨁ f := (biproduct.is_limit f).lift (fan.mk P p) /-- Given a collection of maps out of the summands, we obtain a map out of the biproduct. -/ abbreviation biproduct.desc {f : J → C} [has_biproduct f] {P : C} (p : Π b, f b ⟶ P) : ⨁ f ⟶ P := (biproduct.is_colimit f).desc (cofan.mk P p) @[simp, reassoc] lemma biproduct.lift_π {f : J → C} [has_biproduct f] {P : C} (p : Π b, P ⟶ f b) (j : J) : biproduct.lift p ≫ biproduct.π f j = p j := (biproduct.is_limit f).fac _ _ @[simp, reassoc] lemma biproduct.ι_desc {f : J → C} [has_biproduct f] {P : C} (p : Π b, f b ⟶ P) (j : J) : biproduct.ι f j ≫ biproduct.desc p = p j := (biproduct.is_colimit f).fac _ _ /-- Given a collection of maps between corresponding summands of a pair of biproducts indexed by the same type, we obtain a map between the biproducts. -/ abbreviation biproduct.map [fintype J] {f g : J → C} [has_finite_biproducts C] (p : Π b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := is_limit.map (biproduct.bicone f).to_cone (biproduct.is_limit g) (discrete.nat_trans p) /-- An alternative to `biproduct.map` constructed via colimits. This construction only exists in order to show it is equal to `biproduct.map`. -/ abbreviation biproduct.map' [fintype J] {f g : J → C} [has_finite_biproducts C] (p : Π b, f b ⟶ g b) : ⨁ f ⟶ ⨁ g := is_colimit.map (biproduct.is_colimit f) (biproduct.bicone g).to_cocone (discrete.nat_trans p) @[ext] lemma biproduct.hom_ext {f : J → C} [has_biproduct f] {Z : C} (g h : Z ⟶ ⨁ f) (w : ∀ j, g ≫ biproduct.π f j = h ≫ biproduct.π f j) : g = h := (biproduct.is_limit f).hom_ext w @[ext] lemma biproduct.hom_ext' {f : J → C} [has_biproduct f] {Z : C} (g h : ⨁ f ⟶ Z) (w : ∀ j, biproduct.ι f j ≫ g = biproduct.ι f j ≫ h) : g = h := (biproduct.is_colimit f).hom_ext w lemma biproduct.map_eq_map' [fintype J] {f g : J → C} [has_finite_biproducts C] (p : Π b, f b ⟶ g b) : biproduct.map p = biproduct.map' p := begin ext j j', simp only [discrete.nat_trans_app, limits.is_colimit.ι_map, limits.is_limit.map_π, category.assoc, ←bicone.to_cone_π_app, ←biproduct.bicone_π, ←bicone.to_cocone_ι_app, ←biproduct.bicone_ι], simp only [biproduct.bicone_ι, biproduct.bicone_π, bicone.to_cocone_ι_app, bicone.to_cone_π_app], rw [biproduct.ι_π_assoc, biproduct.ι_π], split_ifs, { subst h, rw [eq_to_hom_refl, category.id_comp], erw category.comp_id, }, { simp, }, end instance biproduct.ι_mono (f : J → C) [has_biproduct f] (b : J) : split_mono (biproduct.ι f b) := { retraction := biproduct.desc $ λ b', if h : b' = b then eq_to_hom (congr_arg f h) else biproduct.ι f b' ≫ biproduct.π f b } instance biproduct.π_epi (f : J → C) [has_biproduct f] (b : J) : split_epi (biproduct.π f b) := { section_ := biproduct.lift $ λ b', if h : b = b' then eq_to_hom (congr_arg f h) else biproduct.ι f b ≫ biproduct.π f b' } @[simp, reassoc] lemma biproduct.map_π [fintype J] {f g : J → C} [has_finite_biproducts C] (p : Π j, f j ⟶ g j) (j : J) : biproduct.map p ≫ biproduct.π g j = biproduct.π f j ≫ p j := limits.is_limit.map_π _ _ _ _ @[simp, reassoc] lemma biproduct.ι_map [fintype J] {f g : J → C} [has_finite_biproducts C] (p : Π j, f j ⟶ g j) (j : J) : biproduct.ι f j ≫ biproduct.map p = p j ≫ biproduct.ι g j := begin rw biproduct.map_eq_map', convert limits.is_colimit.ι_map _ _ _ _; refl end variables {C} /-- A binary bicone for a pair of objects `P Q : C` consists of the cone point `X`, maps from `X` to both `P` and `Q`, and maps from both `P` and `Q` to `X`, so that `inl ≫ fst = 𝟙 P`, `inl ≫ snd = 0`, `inr ≫ fst = 0`, and `inr ≫ snd = 𝟙 Q` -/ @[nolint has_inhabited_instance] structure binary_bicone (P Q : C) := (X : C) (fst : X ⟶ P) (snd : X ⟶ Q) (inl : P ⟶ X) (inr : Q ⟶ X) (inl_fst' : inl ≫ fst = 𝟙 P . obviously) (inl_snd' : inl ≫ snd = 0 . obviously) (inr_fst' : inr ≫ fst = 0 . obviously) (inr_snd' : inr ≫ snd = 𝟙 Q . obviously) restate_axiom binary_bicone.inl_fst' restate_axiom binary_bicone.inl_snd' restate_axiom binary_bicone.inr_fst' restate_axiom binary_bicone.inr_snd' attribute [simp, reassoc] binary_bicone.inl_fst binary_bicone.inl_snd binary_bicone.inr_fst binary_bicone.inr_snd namespace binary_bicone variables {P Q : C} /-- Extract the cone from a binary bicone. -/ def to_cone (c : binary_bicone P Q) : cone (pair P Q) := binary_fan.mk c.fst c.snd @[simp] lemma to_cone_X (c : binary_bicone P Q) : c.to_cone.X = c.X := rfl @[simp] lemma to_cone_π_app_left (c : binary_bicone P Q) : c.to_cone.π.app (walking_pair.left) = c.fst := rfl @[simp] lemma to_cone_π_app_right (c : binary_bicone P Q) : c.to_cone.π.app (walking_pair.right) = c.snd := rfl /-- Extract the cocone from a binary bicone. -/ def to_cocone (c : binary_bicone P Q) : cocone (pair P Q) := binary_cofan.mk c.inl c.inr @[simp] lemma to_cocone_X (c : binary_bicone P Q) : c.to_cocone.X = c.X := rfl @[simp] lemma to_cocone_ι_app_left (c : binary_bicone P Q) : c.to_cocone.ι.app (walking_pair.left) = c.inl := rfl @[simp] lemma to_cocone_ι_app_right (c : binary_bicone P Q) : c.to_cocone.ι.app (walking_pair.right) = c.inr := rfl end binary_bicone namespace bicone /-- Convert a `bicone` over a function on `walking_pair` to a binary_bicone. -/ @[simps] def to_binary_bicone {X Y : C} (b : bicone (pair X Y).obj) : binary_bicone X Y := { X := b.X, fst := b.π walking_pair.left, snd := b.π walking_pair.right, inl := b.ι walking_pair.left, inr := b.ι walking_pair.right, inl_fst' := by { simp [bicone.ι_π], refl, }, inr_fst' := by simp [bicone.ι_π], inl_snd' := by simp [bicone.ι_π], inr_snd' := by { simp [bicone.ι_π], refl, }, } /-- If the cone obtained from a bicone over `pair X Y` is a limit cone, so is the cone obtained by converting that bicone to a binary_bicone, then to a cone. -/ def to_binary_bicone_is_limit {X Y : C} {b : bicone (pair X Y).obj} (c : is_limit (b.to_cone)) : is_limit (b.to_binary_bicone.to_cone) := { lift := λ s, c.lift s, fac' := λ s j, by { cases j; erw c.fac, }, uniq' := λ s m w, begin apply c.uniq s, rintro (⟨⟩|⟨⟩), exact w walking_pair.left, exact w walking_pair.right, end, } /-- If the cocone obtained from a bicone over `pair X Y` is a colimit cocone, so is the cocone obtained by converting that bicone to a binary_bicone, then to a cocone. -/ def to_binary_bicone_is_colimit {X Y : C} {b : bicone (pair X Y).obj} (c : is_colimit (b.to_cocone)) : is_colimit (b.to_binary_bicone.to_cocone) := { desc := λ s, c.desc s, fac' := λ s j, by { cases j; erw c.fac, }, uniq' := λ s m w, begin apply c.uniq s, rintro (⟨⟩|⟨⟩), exact w walking_pair.left, exact w walking_pair.right, end, } end bicone /-- A bicone over `P Q : C`, which is both a limit cone and a colimit cocone. -/ @[nolint has_inhabited_instance] structure binary_biproduct_data (P Q : C) := (bicone : binary_bicone P Q) (is_limit : is_limit bicone.to_cone) (is_colimit : is_colimit bicone.to_cocone) /-- `has_binary_biproduct P Q` expresses the mere existence of a bicone which is simultaneously a limit and a colimit of the diagram `pair P Q`. -/ class has_binary_biproduct (P Q : C) : Prop := mk' :: (exists_binary_biproduct : nonempty (binary_biproduct_data P Q)) lemma has_binary_biproduct.mk {P Q : C} (d : binary_biproduct_data P Q) : has_binary_biproduct P Q := ⟨nonempty.intro d⟩ /-- Use the axiom of choice to extract explicit `binary_biproduct_data F` from `has_binary_biproduct F`. -/ def get_binary_biproduct_data (P Q : C) [has_binary_biproduct P Q] : binary_biproduct_data P Q := classical.choice has_binary_biproduct.exists_binary_biproduct /-- A bicone for `P Q ` which is both a limit cone and a colimit cocone. -/ def binary_biproduct.bicone (P Q : C) [has_binary_biproduct P Q] : binary_bicone P Q := (get_binary_biproduct_data P Q).bicone /-- `binary_biproduct.bicone P Q` is a limit cone. -/ def binary_biproduct.is_limit (P Q : C) [has_binary_biproduct P Q] : is_limit (binary_biproduct.bicone P Q).to_cone := (get_binary_biproduct_data P Q).is_limit /-- `binary_biproduct.bicone P Q` is a colimit cocone. -/ def binary_biproduct.is_colimit (P Q : C) [has_binary_biproduct P Q] : is_colimit (binary_biproduct.bicone P Q).to_cocone := (get_binary_biproduct_data P Q).is_colimit section variable (C) /-- `has_binary_biproducts C` represents the existence of a bicone which is simultaneously a limit and a colimit of the diagram `pair P Q`, for every `P Q : C`. -/ class has_binary_biproducts : Prop := (has_binary_biproduct : Π (P Q : C), has_binary_biproduct P Q) attribute [instance, priority 100] has_binary_biproducts.has_binary_biproduct /-- A category with finite biproducts has binary biproducts. This is not an instance as typically in concrete categories there will be an alternative construction with nicer definitional properties. -/ lemma has_binary_biproducts_of_finite_biproducts [has_finite_biproducts C] : has_binary_biproducts C := { has_binary_biproduct := λ P Q, has_binary_biproduct.mk { bicone := (biproduct.bicone (pair P Q).obj).to_binary_bicone, is_limit := bicone.to_binary_bicone_is_limit (biproduct.is_limit _), is_colimit := bicone.to_binary_bicone_is_colimit (biproduct.is_colimit _) } } end variables {P Q : C} instance has_binary_biproduct.has_limit_pair [has_binary_biproduct P Q] : has_limit (pair P Q) := has_limit.mk ⟨_, binary_biproduct.is_limit P Q⟩ instance has_binary_biproduct.has_colimit_pair [has_binary_biproduct P Q] : has_colimit (pair P Q) := has_colimit.mk ⟨_, binary_biproduct.is_colimit P Q⟩ @[priority 100] instance has_binary_products_of_has_binary_biproducts [has_binary_biproducts C] : has_binary_products C := { has_limit := λ F, has_limit_of_iso (diagram_iso_pair F).symm } @[priority 100] instance has_binary_coproducts_of_has_binary_biproducts [has_binary_biproducts C] : has_binary_coproducts C := { has_colimit := λ F, has_colimit_of_iso (diagram_iso_pair F) } /-- The isomorphism between the specified binary product and the specified binary coproduct for a pair for a binary biproduct. -/ def biprod_iso (X Y : C) [has_binary_biproduct X Y] : limits.prod X Y ≅ limits.coprod X Y := (is_limit.cone_point_unique_up_to_iso (limit.is_limit _) (binary_biproduct.is_limit X Y)).trans $ is_colimit.cocone_point_unique_up_to_iso (binary_biproduct.is_colimit X Y) (colimit.is_colimit _) /-- An arbitrary choice of biproduct of a pair of objects. -/ abbreviation biprod (X Y : C) [has_binary_biproduct X Y] := (binary_biproduct.bicone X Y).X notation X ` ⊞ `:20 Y:20 := biprod X Y /-- The projection onto the first summand of a binary biproduct. -/ abbreviation biprod.fst {X Y : C} [has_binary_biproduct X Y] : X ⊞ Y ⟶ X := (binary_biproduct.bicone X Y).fst /-- The projection onto the second summand of a binary biproduct. -/ abbreviation biprod.snd {X Y : C} [has_binary_biproduct X Y] : X ⊞ Y ⟶ Y := (binary_biproduct.bicone X Y).snd /-- The inclusion into the first summand of a binary biproduct. -/ abbreviation biprod.inl {X Y : C} [has_binary_biproduct X Y] : X ⟶ X ⊞ Y := (binary_biproduct.bicone X Y).inl /-- The inclusion into the second summand of a binary biproduct. -/ abbreviation biprod.inr {X Y : C} [has_binary_biproduct X Y] : Y ⟶ X ⊞ Y := (binary_biproduct.bicone X Y).inr section variables {X Y : C} [has_binary_biproduct X Y] @[simp] lemma binary_biproduct.bicone_fst : (binary_biproduct.bicone X Y).fst = biprod.fst := rfl @[simp] lemma binary_biproduct.bicone_snd : (binary_biproduct.bicone X Y).snd = biprod.snd := rfl @[simp] lemma binary_biproduct.bicone_inl : (binary_biproduct.bicone X Y).inl = biprod.inl := rfl @[simp] lemma binary_biproduct.bicone_inr : (binary_biproduct.bicone X Y).inr = biprod.inr := rfl end @[simp,reassoc] lemma biprod.inl_fst {X Y : C} [has_binary_biproduct X Y] : (biprod.inl : X ⟶ X ⊞ Y) ≫ (biprod.fst : X ⊞ Y ⟶ X) = 𝟙 X := (binary_biproduct.bicone X Y).inl_fst @[simp,reassoc] lemma biprod.inl_snd {X Y : C} [has_binary_biproduct X Y] : (biprod.inl : X ⟶ X ⊞ Y) ≫ (biprod.snd : X ⊞ Y ⟶ Y) = 0 := (binary_biproduct.bicone X Y).inl_snd @[simp,reassoc] lemma biprod.inr_fst {X Y : C} [has_binary_biproduct X Y] : (biprod.inr : Y ⟶ X ⊞ Y) ≫ (biprod.fst : X ⊞ Y ⟶ X) = 0 := (binary_biproduct.bicone X Y).inr_fst @[simp,reassoc] lemma biprod.inr_snd {X Y : C} [has_binary_biproduct X Y] : (biprod.inr : Y ⟶ X ⊞ Y) ≫ (biprod.snd : X ⊞ Y ⟶ Y) = 𝟙 Y := (binary_biproduct.bicone X Y).inr_snd /-- Given a pair of maps into the summands of a binary biproduct, we obtain a map into the binary biproduct. -/ abbreviation biprod.lift {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) : W ⟶ X ⊞ Y := (binary_biproduct.is_limit X Y).lift (binary_fan.mk f g) /-- Given a pair of maps out of the summands of a binary biproduct, we obtain a map out of the binary biproduct. -/ abbreviation biprod.desc {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : X ⊞ Y ⟶ W := (binary_biproduct.is_colimit X Y).desc (binary_cofan.mk f g) @[simp, reassoc] lemma biprod.lift_fst {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) : biprod.lift f g ≫ biprod.fst = f := (binary_biproduct.is_limit X Y).fac _ walking_pair.left @[simp, reassoc] lemma biprod.lift_snd {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) : biprod.lift f g ≫ biprod.snd = g := (binary_biproduct.is_limit X Y).fac _ walking_pair.right @[simp, reassoc] lemma biprod.inl_desc {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : biprod.inl ≫ biprod.desc f g = f := (binary_biproduct.is_colimit X Y).fac _ walking_pair.left @[simp, reassoc] lemma biprod.inr_desc {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) : biprod.inr ≫ biprod.desc f g = g := (binary_biproduct.is_colimit X Y).fac _ walking_pair.right instance biprod.mono_lift_of_mono_left {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) [mono f] : mono (biprod.lift f g) := mono_of_mono_fac $ biprod.lift_fst _ _ instance biprod.mono_lift_of_mono_right {W X Y : C} [has_binary_biproduct X Y] (f : W ⟶ X) (g : W ⟶ Y) [mono g] : mono (biprod.lift f g) := mono_of_mono_fac $ biprod.lift_snd _ _ instance biprod.epi_desc_of_epi_left {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) [epi f] : epi (biprod.desc f g) := epi_of_epi_fac $ biprod.inl_desc _ _ instance biprod.epi_desc_of_epi_right {W X Y : C} [has_binary_biproduct X Y] (f : X ⟶ W) (g : Y ⟶ W) [epi g] : epi (biprod.desc f g) := epi_of_epi_fac $ biprod.inr_desc _ _ /-- Given a pair of maps between the summands of a pair of binary biproducts, we obtain a map between the binary biproducts. -/ abbreviation biprod.map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : W ⊞ X ⟶ Y ⊞ Z := is_limit.map (binary_biproduct.bicone W X).to_cone (binary_biproduct.is_limit Y Z) (@map_pair _ _ (pair W X) (pair Y Z) f g) /-- An alternative to `biprod.map` constructed via colimits. This construction only exists in order to show it is equal to `biprod.map`. -/ abbreviation biprod.map' {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : W ⊞ X ⟶ Y ⊞ Z := is_colimit.map (binary_biproduct.is_colimit W X) (binary_biproduct.bicone Y Z).to_cocone (@map_pair _ _ (pair W X) (pair Y Z) f g) @[ext] lemma biprod.hom_ext {X Y Z : C} [has_binary_biproduct X Y] (f g : Z ⟶ X ⊞ Y) (h₀ : f ≫ biprod.fst = g ≫ biprod.fst) (h₁ : f ≫ biprod.snd = g ≫ biprod.snd) : f = g := binary_fan.is_limit.hom_ext (binary_biproduct.is_limit X Y) h₀ h₁ @[ext] lemma biprod.hom_ext' {X Y Z : C} [has_binary_biproduct X Y] (f g : X ⊞ Y ⟶ Z) (h₀ : biprod.inl ≫ f = biprod.inl ≫ g) (h₁ : biprod.inr ≫ f = biprod.inr ≫ g) : f = g := binary_cofan.is_colimit.hom_ext (binary_biproduct.is_colimit X Y) h₀ h₁ lemma biprod.map_eq_map' {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : biprod.map f g = biprod.map' f g := begin ext, { simp only [map_pair_left, is_colimit.ι_map, is_limit.map_π, biprod.inl_fst_assoc, category.assoc, ←binary_bicone.to_cone_π_app_left, ←binary_biproduct.bicone_fst, ←binary_bicone.to_cocone_ι_app_left, ←binary_biproduct.bicone_inl], simp }, { simp only [map_pair_left, is_colimit.ι_map, is_limit.map_π, zero_comp, biprod.inl_snd_assoc, category.assoc, ←binary_bicone.to_cone_π_app_right, ←binary_biproduct.bicone_snd, ←binary_bicone.to_cocone_ι_app_left, ←binary_biproduct.bicone_inl], simp }, { simp only [map_pair_right, biprod.inr_fst_assoc, is_colimit.ι_map, is_limit.map_π, zero_comp, category.assoc, ←binary_bicone.to_cone_π_app_left, ←binary_biproduct.bicone_fst, ←binary_bicone.to_cocone_ι_app_right, ←binary_biproduct.bicone_inr], simp }, { simp only [map_pair_right, is_colimit.ι_map, is_limit.map_π, biprod.inr_snd_assoc, category.assoc, ←binary_bicone.to_cone_π_app_right, ←binary_biproduct.bicone_snd, ←binary_bicone.to_cocone_ι_app_right, ←binary_biproduct.bicone_inr], simp } end instance biprod.inl_mono {X Y : C} [has_binary_biproduct X Y] : split_mono (biprod.inl : X ⟶ X ⊞ Y) := { retraction := biprod.desc (𝟙 X) (biprod.inr ≫ biprod.fst) } instance biprod.inr_mono {X Y : C} [has_binary_biproduct X Y] : split_mono (biprod.inr : Y ⟶ X ⊞ Y) := { retraction := biprod.desc (biprod.inl ≫ biprod.snd) (𝟙 Y)} instance biprod.fst_epi {X Y : C} [has_binary_biproduct X Y] : split_epi (biprod.fst : X ⊞ Y ⟶ X) := { section_ := biprod.lift (𝟙 X) (biprod.inl ≫ biprod.snd) } instance biprod.snd_epi {X Y : C} [has_binary_biproduct X Y] : split_epi (biprod.snd : X ⊞ Y ⟶ Y) := { section_ := biprod.lift (biprod.inr ≫ biprod.fst) (𝟙 Y) } @[simp,reassoc] lemma biprod.map_fst {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : biprod.map f g ≫ biprod.fst = biprod.fst ≫ f := is_limit.map_π _ _ _ walking_pair.left @[simp,reassoc] lemma biprod.map_snd {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : biprod.map f g ≫ biprod.snd = biprod.snd ≫ g := is_limit.map_π _ _ _ walking_pair.right -- Because `biprod.map` is defined in terms of `lim` rather than `colim`, -- we need to provide additional `simp` lemmas. @[simp,reassoc] lemma biprod.inl_map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : biprod.inl ≫ biprod.map f g = f ≫ biprod.inl := begin rw biprod.map_eq_map', exact is_colimit.ι_map (binary_biproduct.is_colimit W X) _ _ walking_pair.left end @[simp,reassoc] lemma biprod.inr_map {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z] (f : W ⟶ Y) (g : X ⟶ Z) : biprod.inr ≫ biprod.map f g = g ≫ biprod.inr := begin rw biprod.map_eq_map', exact is_colimit.ι_map (binary_biproduct.is_colimit W X) _ _ walking_pair.right end /-- Given a pair of isomorphisms between the summands of a pair of binary biproducts, we obtain an isomorphism between the binary biproducts. -/ @[simps] def biprod.map_iso {W X Y Z : C} [has_binary_biproduct W X] [has_binary_biproduct Y Z] (f : W ≅ Y) (g : X ≅ Z) : W ⊞ X ≅ Y ⊞ Z := { hom := biprod.map f.hom g.hom, inv := biprod.map f.inv g.inv } section variables [has_binary_biproducts C] /-- The braiding isomorphism which swaps a binary biproduct. -/ @[simps] def biprod.braiding (P Q : C) : P ⊞ Q ≅ Q ⊞ P := { hom := biprod.lift biprod.snd biprod.fst, inv := biprod.lift biprod.snd biprod.fst } /-- An alternative formula for the braiding isomorphism which swaps a binary biproduct, using the fact that the biproduct is a coproduct. -/ @[simps] def biprod.braiding' (P Q : C) : P ⊞ Q ≅ Q ⊞ P := { hom := biprod.desc biprod.inr biprod.inl, inv := biprod.desc biprod.inr biprod.inl } lemma biprod.braiding'_eq_braiding {P Q : C} : biprod.braiding' P Q = biprod.braiding P Q := by tidy /-- The braiding isomorphism can be passed through a map by swapping the order. -/ @[reassoc] lemma biprod.braid_natural {W X Y Z : C} (f : X ⟶ Y) (g : Z ⟶ W) : biprod.map f g ≫ (biprod.braiding _ _).hom = (biprod.braiding _ _).hom ≫ biprod.map g f := by tidy @[reassoc] lemma biprod.braiding_map_braiding {W X Y Z : C} (f : W ⟶ Y) (g : X ⟶ Z) : (biprod.braiding X W).hom ≫ biprod.map f g ≫ (biprod.braiding Y Z).hom = biprod.map g f := by tidy @[simp, reassoc] lemma biprod.symmetry' (P Q : C) : biprod.lift biprod.snd biprod.fst ≫ biprod.lift biprod.snd biprod.fst = 𝟙 (P ⊞ Q) := by tidy /-- The braiding isomorphism is symmetric. -/ @[reassoc] lemma biprod.symmetry (P Q : C) : (biprod.braiding P Q).hom ≫ (biprod.braiding Q P).hom = 𝟙 _ := by simp end -- TODO: -- If someone is interested, they could provide the constructions: -- has_binary_biproducts ↔ has_finite_biproducts end category_theory.limits namespace category_theory.limits section preadditive variables {C : Type u} [category.{v} C] [preadditive C] variables {J : Type v} [decidable_eq J] [fintype J] open category_theory.preadditive open_locale big_operators /-- In a preadditive category, we can construct a biproduct for `f : J → C` from any bicone `b` for `f` satisfying `total : ∑ j : J, b.π j ≫ b.ι j = 𝟙 b.X`. (That is, such a bicone is a limit cone and a colimit cocone.) -/ lemma has_biproduct_of_total {f : J → C} (b : bicone f) (total : ∑ j : J, b.π j ≫ b.ι j = 𝟙 b.X) : has_biproduct f := has_biproduct.mk { bicone := b, is_limit := { lift := λ s, ∑ j, s.π.app j ≫ b.ι j, uniq' := λ s m h, begin erw [←category.comp_id m, ←total, comp_sum], apply finset.sum_congr rfl, intros j m, erw [reassoc_of (h j)], end, fac' := λ s j, begin simp only [sum_comp, category.assoc, bicone.to_cone_π_app, b.ι_π, comp_dite], -- See note [dsimp, simp]. dsimp, simp, end }, is_colimit := { desc := λ s, ∑ j, b.π j ≫ s.ι.app j, uniq' := λ s m h, begin erw [←category.id_comp m, ←total, sum_comp], apply finset.sum_congr rfl, intros j m, erw [category.assoc, h], end, fac' := λ s j, begin simp only [comp_sum, ←category.assoc, bicone.to_cocone_ι_app, b.ι_π, dite_comp], dsimp, simp, end } } /-- In a preadditive category, if the product over `f : J → C` exists, then the biproduct over `f` exists. -/ lemma has_biproduct.of_has_product (f : J → C) [has_product f] : has_biproduct f := has_biproduct_of_total { X := pi_obj f, π := limits.pi.π f, ι := λ j, pi.lift (λ j', if h : j = j' then eq_to_hom (congr_arg f h) else 0), ι_π := λ j j', by simp, } (by { ext, simp [sum_comp, comp_dite] }) /-- In a preadditive category, if the coproduct over `f : J → C` exists, then the biproduct over `f` exists. -/ lemma has_biproduct.of_has_coproduct (f : J → C) [has_coproduct f] : has_biproduct f := has_biproduct_of_total { X := sigma_obj f, π := λ j, sigma.desc (λ j', if h : j' = j then eq_to_hom (congr_arg f h) else 0), ι := limits.sigma.ι f, ι_π := λ j j', by simp, } begin ext, simp only [comp_sum, limits.colimit.ι_desc_assoc, eq_self_iff_true, limits.colimit.ι_desc, category.comp_id], dsimp, simp only [dite_comp, finset.sum_dite_eq, finset.mem_univ, if_true, category.id_comp, eq_to_hom_refl, zero_comp], end /-- A preadditive category with finite products has finite biproducts. -/ lemma has_finite_biproducts.of_has_finite_products [has_finite_products C] : has_finite_biproducts C := ⟨λ J _ _, { has_biproduct := λ F, by exactI has_biproduct.of_has_product _ }⟩ /-- A preadditive category with finite coproducts has finite biproducts. -/ lemma has_finite_biproducts.of_has_finite_coproducts [has_finite_coproducts C] : has_finite_biproducts C := ⟨λ J _ _, { has_biproduct := λ F, by exactI has_biproduct.of_has_coproduct _ }⟩ section variables {f : J → C} [has_biproduct f] /-- In any preadditive category, any biproduct satsifies `∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f)` -/ @[simp] lemma biproduct.total : ∑ j : J, biproduct.π f j ≫ biproduct.ι f j = 𝟙 (⨁ f) := begin ext j j', simp [comp_sum, sum_comp, biproduct.ι_π, comp_dite, dite_comp], end lemma biproduct.lift_eq {T : C} {g : Π j, T ⟶ f j} : biproduct.lift g = ∑ j, g j ≫ biproduct.ι f j := begin ext j, simp [sum_comp, biproduct.ι_π, comp_dite], end lemma biproduct.desc_eq {T : C} {g : Π j, f j ⟶ T} : biproduct.desc g = ∑ j, biproduct.π f j ≫ g j := begin ext j, simp [comp_sum, biproduct.ι_π_assoc, dite_comp], end @[simp, reassoc] lemma biproduct.lift_desc {T U : C} {g : Π j, T ⟶ f j} {h : Π j, f j ⟶ U} : biproduct.lift g ≫ biproduct.desc h = ∑ j : J, g j ≫ h j := by simp [biproduct.lift_eq, biproduct.desc_eq, comp_sum, sum_comp, biproduct.ι_π_assoc, comp_dite, dite_comp] lemma biproduct.map_eq [has_finite_biproducts C] {f g : J → C} {h : Π j, f j ⟶ g j} : biproduct.map h = ∑ j : J, biproduct.π f j ≫ h j ≫ biproduct.ι g j := begin ext, simp [biproduct.ι_π, biproduct.ι_π_assoc, comp_sum, sum_comp, comp_dite, dite_comp], end end /-- In a preadditive category, we can construct a binary biproduct for `X Y : C` from any binary bicone `b` satisfying `total : b.fst ≫ b.inl + b.snd ≫ b.inr = 𝟙 b.X`. (That is, such a bicone is a limit cone and a colimit cocone.) -/ lemma has_binary_biproduct_of_total {X Y : C} (b : binary_bicone X Y) (total : b.fst ≫ b.inl + b.snd ≫ b.inr = 𝟙 b.X) : has_binary_biproduct X Y := has_binary_biproduct.mk { bicone := b, is_limit := { lift := λ s, binary_fan.fst s ≫ b.inl + binary_fan.snd s ≫ b.inr, uniq' := λ s m h, by erw [←category.comp_id m, ←total, comp_add, reassoc_of (h walking_pair.left), reassoc_of (h walking_pair.right)], fac' := λ s j, by cases j; simp, }, is_colimit := { desc := λ s, b.fst ≫ binary_cofan.inl s + b.snd ≫ binary_cofan.inr s, uniq' := λ s m h, by erw [←category.id_comp m, ←total, add_comp, category.assoc, category.assoc, h walking_pair.left, h walking_pair.right], fac' := λ s j, by cases j; simp, } } /-- In a preadditive category, if the product of `X` and `Y` exists, then the binary biproduct of `X` and `Y` exists. -/ lemma has_binary_biproduct.of_has_binary_product (X Y : C) [has_binary_product X Y] : has_binary_biproduct X Y := has_binary_biproduct_of_total { X := X ⨯ Y, fst := category_theory.limits.prod.fst, snd := category_theory.limits.prod.snd, inl := prod.lift (𝟙 X) 0, inr := prod.lift 0 (𝟙 Y) } begin ext; simp [add_comp], end /-- In a preadditive category, if all binary products exist, then all binary biproducts exist. -/ lemma has_binary_biproducts.of_has_binary_products [has_binary_products C] : has_binary_biproducts C := { has_binary_biproduct := λ X Y, has_binary_biproduct.of_has_binary_product X Y, } /-- In a preadditive category, if the coproduct of `X` and `Y` exists, then the binary biproduct of `X` and `Y` exists. -/ lemma has_binary_biproduct.of_has_binary_coproduct (X Y : C) [has_binary_coproduct X Y] : has_binary_biproduct X Y := has_binary_biproduct_of_total { X := X ⨿ Y, fst := coprod.desc (𝟙 X) 0, snd := coprod.desc 0 (𝟙 Y), inl := category_theory.limits.coprod.inl, inr := category_theory.limits.coprod.inr } begin ext; simp [add_comp], end /-- In a preadditive category, if all binary coproducts exist, then all binary biproducts exist. -/ lemma has_binary_biproducts.of_has_binary_coproducts [has_binary_coproducts C] : has_binary_biproducts C := { has_binary_biproduct := λ X Y, has_binary_biproduct.of_has_binary_coproduct X Y, } section variables {X Y : C} [has_binary_biproduct X Y] /-- In any preadditive category, any binary biproduct satsifies `biprod.fst ≫ biprod.inl + biprod.snd ≫ biprod.inr = 𝟙 (X ⊞ Y)`. -/ @[simp] lemma biprod.total : biprod.fst ≫ biprod.inl + biprod.snd ≫ biprod.inr = 𝟙 (X ⊞ Y) := begin ext; simp [add_comp], end lemma biprod.lift_eq {T : C} {f : T ⟶ X} {g : T ⟶ Y} : biprod.lift f g = f ≫ biprod.inl + g ≫ biprod.inr := begin ext; simp [add_comp], end lemma biprod.desc_eq {T : C} {f : X ⟶ T} {g : Y ⟶ T} : biprod.desc f g = biprod.fst ≫ f + biprod.snd ≫ g := begin ext; simp [add_comp], end @[simp, reassoc] lemma biprod.lift_desc {T U : C} {f : T ⟶ X} {g : T ⟶ Y} {h : X ⟶ U} {i : Y ⟶ U} : biprod.lift f g ≫ biprod.desc h i = f ≫ h + g ≫ i := by simp [biprod.lift_eq, biprod.desc_eq] lemma biprod.map_eq [has_binary_biproducts C] {W X Y Z : C} {f : W ⟶ Y} {g : X ⟶ Z} : biprod.map f g = biprod.fst ≫ f ≫ biprod.inl + biprod.snd ≫ g ≫ biprod.inr := by apply biprod.hom_ext; apply biprod.hom_ext'; simp end end preadditive end category_theory.limits
18f0ea7b191376e40773d373414853d15594c8ac
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/tests/lean/congr2.lean
12820c15edf4d0ef3f00f0267243223323b42ac6
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
597
lean
import data.list #congr @add #congr @perm section variables p : nat → Prop variables q : nat → nat → Prop variables f : Π (x y : nat), p x → q x y → nat #congr f end constant p : Π {A : Type}, A → Prop constant q : Π {A : Type} (n m : A), p n → p m → Prop constant r : Π {A : Type} (n m : A) (H₁ : p n) (H₂ : p m), q n m H₁ H₂ → Prop constant h : Π (A : Type) (n m : A) (H₁ : p n) (H₂ : p m) (H₃ : q n n H₁ H₁) (H₄ : q n m H₁ H₂) (H₅ : r n m H₁ H₂ H₄) (H₆ : r n n H₁ H₁ H₃), A #congr h #congr @ite
357e4a8809c23a374b246a56a4d5273514cb6244
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/ppbug.lean
1d3cabb762ca798ed7325755355f65ec152479c3
[ "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
50
lean
set_option pp.metavar_args true check char.rec_on
b77b6125379b71297c53b5eb4fe3baad470ef902
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/topology/algebra/order/liminf_limsup.lean
75901a2fa3d0ff726dabd63f81813c7e49227841
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
13,542
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Yury Kudryashov -/ import order.liminf_limsup import topology.algebra.order.basic /-! # Lemmas about liminf and limsup in an order topology. -/ open filter open_locale topological_space classical universes u v variables {α : Type u} {β : Type v} section liminf_limsup section order_closed_topology variables [semilattice_sup α] [topological_space α] [order_topology α] lemma is_bounded_le_nhds (a : α) : (𝓝 a).is_bounded (≤) := (is_top_or_exists_gt a).elim (λ h, ⟨a, eventually_of_forall h⟩) (λ ⟨b, hb⟩, ⟨b, ge_mem_nhds hb⟩) lemma filter.tendsto.is_bounded_under_le {f : filter β} {u : β → α} {a : α} (h : tendsto u f (𝓝 a)) : f.is_bounded_under (≤) u := (is_bounded_le_nhds a).mono h lemma filter.tendsto.bdd_above_range_of_cofinite {u : β → α} {a : α} (h : tendsto u cofinite (𝓝 a)) : bdd_above (set.range u) := h.is_bounded_under_le.bdd_above_range_of_cofinite lemma filter.tendsto.bdd_above_range {u : ℕ → α} {a : α} (h : tendsto u at_top (𝓝 a)) : bdd_above (set.range u) := h.is_bounded_under_le.bdd_above_range lemma is_cobounded_ge_nhds (a : α) : (𝓝 a).is_cobounded (≥) := (is_bounded_le_nhds a).is_cobounded_flip lemma filter.tendsto.is_cobounded_under_ge {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : f.is_cobounded_under (≥) u := h.is_bounded_under_le.is_cobounded_flip end order_closed_topology section order_closed_topology variables [semilattice_inf α] [topological_space α] [order_topology α] lemma is_bounded_ge_nhds (a : α) : (𝓝 a).is_bounded (≥) := @is_bounded_le_nhds αᵒᵈ _ _ _ a lemma filter.tendsto.is_bounded_under_ge {f : filter β} {u : β → α} {a : α} (h : tendsto u f (𝓝 a)) : f.is_bounded_under (≥) u := (is_bounded_ge_nhds a).mono h lemma filter.tendsto.bdd_below_range_of_cofinite {u : β → α} {a : α} (h : tendsto u cofinite (𝓝 a)) : bdd_below (set.range u) := h.is_bounded_under_ge.bdd_below_range_of_cofinite lemma filter.tendsto.bdd_below_range {u : ℕ → α} {a : α} (h : tendsto u at_top (𝓝 a)) : bdd_below (set.range u) := h.is_bounded_under_ge.bdd_below_range lemma is_cobounded_le_nhds (a : α) : (𝓝 a).is_cobounded (≤) := (is_bounded_ge_nhds a).is_cobounded_flip lemma filter.tendsto.is_cobounded_under_le {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : f.is_cobounded_under (≤) u := h.is_bounded_under_ge.is_cobounded_flip end order_closed_topology section conditionally_complete_linear_order variables [conditionally_complete_linear_order α] theorem lt_mem_sets_of_Limsup_lt {f : filter α} {b} (h : f.is_bounded (≤)) (l : f.Limsup < b) : ∀ᶠ a in f, a < b := let ⟨c, (h : ∀ᶠ a in f, a ≤ c), hcb⟩ := exists_lt_of_cInf_lt h l in mem_of_superset h $ assume a hac, lt_of_le_of_lt hac hcb theorem gt_mem_sets_of_Liminf_gt : ∀ {f : filter α} {b}, f.is_bounded (≥) → b < f.Liminf → ∀ᶠ a in f, b < a := @lt_mem_sets_of_Limsup_lt αᵒᵈ _ variables [topological_space α] [order_topology α] /-- If the liminf and the limsup of a filter coincide, then this filter converges to their common value, at least if the filter is eventually bounded above and below. -/ theorem le_nhds_of_Limsup_eq_Liminf {f : filter α} {a : α} (hl : f.is_bounded (≤)) (hg : f.is_bounded (≥)) (hs : f.Limsup = a) (hi : f.Liminf = a) : f ≤ 𝓝 a := tendsto_order.2 $ and.intro (assume b hb, gt_mem_sets_of_Liminf_gt hg $ hi.symm ▸ hb) (assume b hb, lt_mem_sets_of_Limsup_lt hl $ hs.symm ▸ hb) theorem Limsup_nhds (a : α) : Limsup (𝓝 a) = a := cInf_eq_of_forall_ge_of_forall_gt_exists_lt (is_bounded_le_nhds a) (assume a' (h : {n : α | n ≤ a'} ∈ 𝓝 a), show a ≤ a', from @mem_of_mem_nhds α _ a _ h) (assume b (hba : a < b), show ∃c (h : {n : α | n ≤ c} ∈ 𝓝 a), c < b, from match dense_or_discrete a b with | or.inl ⟨c, hac, hcb⟩ := ⟨c, ge_mem_nhds hac, hcb⟩ | or.inr ⟨_, h⟩ := ⟨a, (𝓝 a).sets_of_superset (gt_mem_nhds hba) h, hba⟩ end) theorem Liminf_nhds : ∀ (a : α), Liminf (𝓝 a) = a := @Limsup_nhds αᵒᵈ _ _ _ /-- If a filter is converging, its limsup coincides with its limit. -/ theorem Liminf_eq_of_le_nhds {f : filter α} {a : α} [ne_bot f] (h : f ≤ 𝓝 a) : f.Liminf = a := have hb_ge : is_bounded (≥) f, from (is_bounded_ge_nhds a).mono h, have hb_le : is_bounded (≤) f, from (is_bounded_le_nhds a).mono h, le_antisymm (calc f.Liminf ≤ f.Limsup : Liminf_le_Limsup hb_le hb_ge ... ≤ (𝓝 a).Limsup : Limsup_le_Limsup_of_le h hb_ge.is_cobounded_flip (is_bounded_le_nhds a) ... = a : Limsup_nhds a) (calc a = (𝓝 a).Liminf : (Liminf_nhds a).symm ... ≤ f.Liminf : Liminf_le_Liminf_of_le h (is_bounded_ge_nhds a) hb_le.is_cobounded_flip) /-- If a filter is converging, its liminf coincides with its limit. -/ theorem Limsup_eq_of_le_nhds : ∀ {f : filter α} {a : α} [ne_bot f], f ≤ 𝓝 a → f.Limsup = a := @Liminf_eq_of_le_nhds αᵒᵈ _ _ _ /-- If a function has a limit, then its limsup coincides with its limit. -/ theorem filter.tendsto.limsup_eq {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : limsup f u = a := Limsup_eq_of_le_nhds h /-- If a function has a limit, then its liminf coincides with its limit. -/ theorem filter.tendsto.liminf_eq {f : filter β} {u : β → α} {a : α} [ne_bot f] (h : tendsto u f (𝓝 a)) : liminf f u = a := Liminf_eq_of_le_nhds h /-- If the liminf and the limsup of a function coincide, then the limit of the function exists and has the same value -/ theorem tendsto_of_liminf_eq_limsup {f : filter β} {u : β → α} {a : α} (hinf : liminf f u = a) (hsup : limsup f u = a) (h : f.is_bounded_under (≤) u . is_bounded_default) (h' : f.is_bounded_under (≥) u . is_bounded_default) : tendsto u f (𝓝 a) := le_nhds_of_Limsup_eq_Liminf h h' hsup hinf /-- If a number `a` is less than or equal to the `liminf` of a function `f` at some filter and is greater than or equal to the `limsup` of `f`, then `f` tends to `a` along this filter. -/ theorem tendsto_of_le_liminf_of_limsup_le {f : filter β} {u : β → α} {a : α} (hinf : a ≤ liminf f u) (hsup : limsup f u ≤ a) (h : f.is_bounded_under (≤) u . is_bounded_default) (h' : f.is_bounded_under (≥) u . is_bounded_default) : tendsto u f (𝓝 a) := if hf : f = ⊥ then hf.symm ▸ tendsto_bot else by haveI : ne_bot f := ⟨hf⟩; exact tendsto_of_liminf_eq_limsup (le_antisymm (le_trans (liminf_le_limsup h h') hsup) hinf) (le_antisymm hsup (le_trans hinf (liminf_le_limsup h h'))) h h' /-- Assume that, for any `a < b`, a sequence can not be infinitely many times below `a` and above `b`. If it is also ultimately bounded above and below, then it has to converge. This even works if `a` and `b` are restricted to a dense subset. -/ lemma tendsto_of_no_upcrossings [densely_ordered α] {f : filter β} {u : β → α} {s : set α} (hs : dense s) (H : ∀ (a ∈ s) (b ∈ s), a < b → ¬((∃ᶠ n in f, u n < a) ∧ (∃ᶠ n in f, b < u n))) (h : f.is_bounded_under (≤) u . is_bounded_default) (h' : f.is_bounded_under (≥) u . is_bounded_default) : ∃ (c : α), tendsto u f (𝓝 c) := begin by_cases hbot : f = ⊥, { rw hbot, exact ⟨Inf ∅, tendsto_bot⟩ }, haveI : ne_bot f := ⟨hbot⟩, refine ⟨limsup f u, _⟩, apply tendsto_of_le_liminf_of_limsup_le _ le_rfl h h', by_contra' hlt, obtain ⟨a, ⟨⟨la, au⟩, as⟩⟩ : ∃ a, (f.liminf u < a ∧ a < f.limsup u) ∧ a ∈ s := dense_iff_inter_open.1 hs (set.Ioo (f.liminf u) (f.limsup u)) is_open_Ioo (set.nonempty_Ioo.2 hlt), obtain ⟨b, ⟨⟨ab, bu⟩, bs⟩⟩ : ∃ b, (a < b ∧ b < f.limsup u) ∧ b ∈ s := dense_iff_inter_open.1 hs (set.Ioo a (f.limsup u)) is_open_Ioo (set.nonempty_Ioo.2 au), have A : ∃ᶠ n in f, u n < a := frequently_lt_of_liminf_lt (is_bounded.is_cobounded_ge h) la, have B : ∃ᶠ n in f, b < u n := frequently_lt_of_lt_limsup (is_bounded.is_cobounded_le h') bu, exact H a as b bs ab ⟨A, B⟩, end end conditionally_complete_linear_order end liminf_limsup section monotone variables {ι R S : Type*} {F : filter ι} [ne_bot F] [complete_linear_order R] [topological_space R] [order_topology R] [complete_linear_order S] [topological_space S] [order_topology S] /-- An antitone function between complete linear ordered spaces sends a `filter.Limsup` to the `filter.liminf` of the image if it is continuous at the `Limsup`. -/ lemma antitone.map_Limsup_of_continuous_at {F : filter R} [ne_bot F] {f : R → S} (f_decr : antitone f) (f_cont : continuous_at f (F.Limsup)) : f (F.Limsup) = F.liminf f := begin apply le_antisymm, { have A : {a : R | ∀ᶠ (n : R) in F, n ≤ a}.nonempty, from ⟨⊤, by simp⟩, rw [Limsup, (f_decr.map_Inf_of_continuous_at' f_cont A)], apply le_of_forall_lt, assume c hc, simp only [liminf, Liminf, lt_Sup_iff, eventually_map, set.mem_set_of_eq, exists_prop, set.mem_image, exists_exists_and_eq_and] at hc ⊢, rcases hc with ⟨d, hd, h'd⟩, refine ⟨f d, _, h'd⟩, filter_upwards [hd] with x hx using f_decr hx }, { rcases eq_or_lt_of_le (bot_le : ⊥ ≤ F.Limsup) with h|Limsup_ne_bot, { rw ← h, apply liminf_le_of_frequently_le, apply frequently_of_forall, assume x, exact f_decr bot_le }, by_cases h' : ∃ c, c < F.Limsup ∧ set.Ioo c F.Limsup = ∅, { rcases h' with ⟨c, c_lt, hc⟩, have B : ∃ᶠ n in F, F.Limsup ≤ n, { apply (frequently_lt_of_lt_Limsup (by is_bounded_default) c_lt).mono, assume x hx, by_contra', have : (set.Ioo c F.Limsup).nonempty := ⟨x, ⟨hx, this⟩⟩, simpa [hc] }, apply liminf_le_of_frequently_le, exact B.mono (λ x hx, f_decr hx) }, by_contra' H, obtain ⟨l, l_lt, h'l⟩ : ∃ l < F.Limsup, set.Ioc l F.Limsup ⊆ {x : R | f x < F.liminf f}, from exists_Ioc_subset_of_mem_nhds ((tendsto_order.1 f_cont.tendsto).2 _ H) ⟨⊥, Limsup_ne_bot⟩, obtain ⟨m, l_m, m_lt⟩ : (set.Ioo l F.Limsup).nonempty, { contrapose! h', refine ⟨l, l_lt, by rwa set.not_nonempty_iff_eq_empty at h'⟩ }, have B : F.liminf f ≤ f m, { apply liminf_le_of_frequently_le, apply (frequently_lt_of_lt_Limsup (by is_bounded_default) m_lt).mono, assume x hx, exact f_decr hx.le }, have I : f m < F.liminf f := h'l ⟨l_m, m_lt.le⟩, exact lt_irrefl _ (B.trans_lt I) } end /-- A continuous antitone function between complete linear ordered spaces sends a `filter.limsup` to the `filter.liminf` of the images. -/ lemma antitone.map_limsup_of_continuous_at {f : R → S} (f_decr : antitone f) (a : ι → R) (f_cont : continuous_at f (F.limsup a)) : f (F.limsup a) = F.liminf (f ∘ a) := f_decr.map_Limsup_of_continuous_at f_cont /-- An antitone function between complete linear ordered spaces sends a `filter.Liminf` to the `filter.limsup` of the image if it is continuous at the `Liminf`. -/ lemma antitone.map_Liminf_of_continuous_at {F : filter R} [ne_bot F] {f : R → S} (f_decr : antitone f) (f_cont : continuous_at f (F.Liminf)) : f (F.Liminf) = F.limsup f := @antitone.map_Limsup_of_continuous_at (order_dual R) (order_dual S) _ _ _ _ _ _ _ _ f f_decr.dual f_cont /-- A continuous antitone function between complete linear ordered spaces sends a `filter.liminf` to the `filter.limsup` of the images. -/ lemma antitone.map_liminf_of_continuous_at {f : R → S} (f_decr : antitone f) (a : ι → R) (f_cont : continuous_at f (F.liminf a)) : f (F.liminf a) = F.limsup (f ∘ a) := f_decr.map_Liminf_of_continuous_at f_cont /-- A monotone function between complete linear ordered spaces sends a `filter.Limsup` to the `filter.limsup` of the image if it is continuous at the `Limsup`. -/ lemma monotone.map_Limsup_of_continuous_at {F : filter R} [ne_bot F] {f : R → S} (f_incr : monotone f) (f_cont : continuous_at f (F.Limsup)) : f (F.Limsup) = F.limsup f := @antitone.map_Limsup_of_continuous_at R (order_dual S) _ _ _ _ _ _ _ _ f f_incr f_cont /-- A continuous monotone function between complete linear ordered spaces sends a `filter.limsup` to the `filter.limsup` of the images. -/ lemma monotone.map_limsup_of_continuous_at {f : R → S} (f_incr : monotone f) (a : ι → R) (f_cont : continuous_at f (F.limsup a)) : f (F.limsup a) = F.limsup (f ∘ a) := f_incr.map_Limsup_of_continuous_at f_cont /-- A monotone function between complete linear ordered spaces sends a `filter.Liminf` to the `filter.liminf` of the image if it is continuous at the `Liminf`. -/ lemma monotone.map_Liminf_of_continuous_at {F : filter R} [ne_bot F] {f : R → S} (f_incr : monotone f) (f_cont : continuous_at f (F.Liminf)) : f (F.Liminf) = F.liminf f := @antitone.map_Liminf_of_continuous_at R (order_dual S) _ _ _ _ _ _ _ _ f f_incr f_cont /-- A continuous monotone function between complete linear ordered spaces sends a `filter.liminf` to the `filter.liminf` of the images. -/ lemma monotone.map_liminf_of_continuous_at {f : R → S} (f_incr : monotone f) (a : ι → R) (f_cont : continuous_at f (F.liminf a)) : f (F.liminf a) = F.liminf (f ∘ a) := f_incr.map_Liminf_of_continuous_at f_cont end monotone
0cf05d3b2a24d0b067a63ebd6d643ac709a2037d
54d7e71c3616d331b2ec3845d31deb08f3ff1dea
/library/init/data/nat/div.lean
cc4cf74c23837c3107395b39e220e6be21ad15ab
[ "Apache-2.0" ]
permissive
pachugupta/lean
6f3305c4292288311cc4ab4550060b17d49ffb1d
0d02136a09ac4cf27b5c88361750e38e1f485a1a
refs/heads/master
1,611,110,653,606
1,493,130,117,000
1,493,167,649,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,176
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.wf init.data.nat.basic namespace nat private def div_rec_lemma {x y : nat} : 0 < y ∧ y ≤ x → x - y < x := λ h, and.rec (λ ypos ylex, sub_lt (nat.lt_of_lt_of_le ypos ylex) ypos) h private def div.F (x : nat) (f : Π x₁, x₁ < x → nat → nat) (y : nat) : nat := if h : 0 < y ∧ y ≤ x then f (x - y) (div_rec_lemma h) y + 1 else zero protected def div := well_founded.fix lt_wf div.F instance : has_div nat := ⟨nat.div⟩ lemma div_def_aux (x y : nat) : div x y = if h : 0 < y ∧ y ≤ x then div (x - y) y + 1 else 0 := congr_fun (well_founded.fix_eq lt_wf div.F x) y private def mod.F (x : nat) (f : Π x₁, x₁ < x → nat → nat) (y : nat) : nat := if h : 0 < y ∧ y ≤ x then f (x - y) (div_rec_lemma h) y else x protected def mod := well_founded.fix lt_wf mod.F instance : has_mod nat := ⟨nat.mod⟩ lemma mod_def_aux (x y : nat) : mod x y = if h : 0 < y ∧ y ≤ x then mod (x - y) y else x := congr_fun (well_founded.fix_eq lt_wf mod.F x) y end nat
2cd3775bad85dfc2b6cdcdaa5b648e06aefce75d
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/algebra/add_torsor.lean
d04276bbfb476fff7fa53bf10bf5aa0ddc6e667b
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
14,848
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Yury Kudryashov. -/ import algebra.group.prod import algebra.group.type_tags import algebra.group.pi import data.equiv.basic import data.set.finite /-! # Torsors of additive group actions This file defines torsors of additive group actions. ## Notations The group elements are referred to as acting on points. This file defines the notation `+ᵥ` for adding a group element to a point and `-ᵥ` for subtracting two points to produce a group element. ## Implementation notes Affine spaces are the motivating example of torsors of additive group actions. It may be appropriate to refactor in terms of the general definition of group actions, via `to_additive`, when there is a use for multiplicative torsors (currently mathlib only develops the theory of group actions for multiplicative group actions). The variable `G` is an explicit rather than implicit argument to lemmas because otherwise the elaborator sometimes has problems inferring appropriate types and type class instances. ## References * https://en.wikipedia.org/wiki/Principal_homogeneous_space * https://en.wikipedia.org/wiki/Affine_space -/ /-- Type class for the `+ᵥ` notation. -/ class has_vadd (G : Type*) (P : Type*) := (vadd : G → P → P) /-- Type class for the `-ᵥ` notation. -/ class has_vsub (G : out_param Type*) (P : Type*) := (vsub : P → P → G) infix ` +ᵥ `:65 := has_vadd.vadd infix ` -ᵥ `:65 := has_vsub.vsub section old_structure_cmd set_option old_structure_cmd true /-- Type class for additive monoid actions. -/ class add_action (G : Type*) (P : Type*) [add_monoid G] extends has_vadd G P := (zero_vadd' : ∀ p : P, (0 : G) +ᵥ p = p) (vadd_assoc' : ∀ (g1 g2 : G) (p : P), g1 +ᵥ (g2 +ᵥ p) = (g1 + g2) +ᵥ p) /-- An `add_torsor G P` gives a structure to the nonempty type `P`, acted on by an `add_group G` with a transitive and free action given by the `+ᵥ` operation and a corresponding subtraction given by the `-ᵥ` operation. In the case of a vector space, it is an affine space. -/ class add_torsor (G : out_param Type*) (P : Type*) [out_param $ add_group G] extends add_action G P, has_vsub G P := [nonempty : nonempty P] (vsub_vadd' : ∀ (p1 p2 : P), (p1 -ᵥ p2 : G) +ᵥ p2 = p1) (vadd_vsub' : ∀ (g : G) (p : P), g +ᵥ p -ᵥ p = g) attribute [instance, priority 100, nolint dangerous_instance] add_torsor.nonempty end old_structure_cmd /-- An `add_group G` is a torsor for itself. -/ @[nolint instance_priority] instance add_group_is_add_torsor (G : Type*) [add_group G] : add_torsor G G := { vadd := has_add.add, vsub := has_sub.sub, zero_vadd' := zero_add, vadd_assoc' := λ a b c, (add_assoc a b c).symm, vsub_vadd' := sub_add_cancel, vadd_vsub' := add_sub_cancel } /-- Simplify addition for a torsor for an `add_group G` over itself. -/ @[simp] lemma vadd_eq_add {G : Type*} [add_group G] (g1 g2 : G) : g1 +ᵥ g2 = g1 + g2 := rfl /-- Simplify subtraction for a torsor for an `add_group G` over itself. -/ @[simp] lemma vsub_eq_sub {G : Type*} [add_group G] (g1 g2 : G) : g1 -ᵥ g2 = g1 - g2 := rfl section general variables (G : Type*) {P : Type*} [add_monoid G] [A : add_action G P] include A /-- Adding the zero group element to a point gives the same point. -/ @[simp] lemma zero_vadd (p : P) : (0 : G) +ᵥ p = p := add_action.zero_vadd' p variables {G} /-- Adding two group elements to a point produces the same result as adding their sum. -/ lemma vadd_assoc (g1 g2 : G) (p : P) : g1 +ᵥ (g2 +ᵥ p) = (g1 + g2) +ᵥ p := add_action.vadd_assoc' g1 g2 p end general section comm variables (G : Type*) {P : Type*} [add_comm_monoid G] [A : add_action G P] include A /-- Adding two group elements to a point produces the same result in either order. -/ lemma vadd_comm (p : P) (g1 g2 : G) : g1 +ᵥ (g2 +ᵥ p) = g2 +ᵥ (g1 +ᵥ p) := by rw [vadd_assoc, vadd_assoc, add_comm] end comm section group variables {G : Type*} {P : Type*} [add_group G] [A : add_action G P] include A /-- If the same group element added to two points produces equal results, those points are equal. -/ lemma vadd_left_cancel {p1 p2 : P} (g : G) (h : g +ᵥ p1 = g +ᵥ p2) : p1 = p2 := begin have h2 : -g +ᵥ (g +ᵥ p1) = -g +ᵥ (g +ᵥ p2), { rw h }, rwa [vadd_assoc, vadd_assoc, add_left_neg, zero_vadd, zero_vadd] at h2 end @[simp] lemma vadd_left_cancel_iff {p₁ p₂ : P} (g : G) : g +ᵥ p₁ = g +ᵥ p₂ ↔ p₁ = p₂ := ⟨vadd_left_cancel g, λ h, h ▸ rfl⟩ variables (P) /-- Adding the group element `g` to a point is an injective function. -/ lemma vadd_left_injective (g : G) : function.injective ((+ᵥ) g : P → P) := λ p1 p2, vadd_left_cancel g end group section general variables {G : Type*} {P : Type*} [add_group G] [T : add_torsor G P] include T /-- Adding the result of subtracting from another point produces that point. -/ @[simp] lemma vsub_vadd (p1 p2 : P) : p1 -ᵥ p2 +ᵥ p2 = p1 := add_torsor.vsub_vadd' p1 p2 /-- Adding a group element then subtracting the original point produces that group element. -/ @[simp] lemma vadd_vsub (g : G) (p : P) : g +ᵥ p -ᵥ p = g := add_torsor.vadd_vsub' g p /-- If the same point added to two group elements produces equal results, those group elements are equal. -/ lemma vadd_right_cancel {g1 g2 : G} (p : P) (h : g1 +ᵥ p = g2 +ᵥ p) : g1 = g2 := by rw [←vadd_vsub g1, h, vadd_vsub] @[simp] lemma vadd_right_cancel_iff {g1 g2 : G} (p : P) : g1 +ᵥ p = g2 +ᵥ p ↔ g1 = g2 := ⟨vadd_right_cancel p, λ h, h ▸ rfl⟩ /-- Adding a group element to the point `p` is an injective function. -/ lemma vadd_right_injective (p : P) : function.injective ((+ᵥ p) : G → P) := λ g1 g2, vadd_right_cancel p /-- Adding a group element to a point, then subtracting another point, produces the same result as subtracting the points then adding the group element. -/ lemma vadd_vsub_assoc (g : G) (p1 p2 : P) : g +ᵥ p1 -ᵥ p2 = g + (p1 -ᵥ p2) := begin apply vadd_right_cancel p2, rw [vsub_vadd, ←vadd_assoc, vsub_vadd] end /-- Subtracting a point from itself produces 0. -/ @[simp] lemma vsub_self (p : P) : p -ᵥ p = (0 : G) := by rw [←zero_add (p -ᵥ p), ←vadd_vsub_assoc, vadd_vsub] /-- If subtracting two points produces 0, they are equal. -/ lemma eq_of_vsub_eq_zero {p1 p2 : P} (h : p1 -ᵥ p2 = (0 : G)) : p1 = p2 := by rw [←vsub_vadd p1 p2, h, zero_vadd] /-- Subtracting two points produces 0 if and only if they are equal. -/ @[simp] lemma vsub_eq_zero_iff_eq {p1 p2 : P} : p1 -ᵥ p2 = (0 : G) ↔ p1 = p2 := iff.intro eq_of_vsub_eq_zero (λ h, h ▸ vsub_self _) /-- Cancellation adding the results of two subtractions. -/ @[simp] lemma vsub_add_vsub_cancel (p1 p2 p3 : P) : p1 -ᵥ p2 + (p2 -ᵥ p3) = (p1 -ᵥ p3) := begin apply vadd_right_cancel p3, rw [←vadd_assoc, vsub_vadd, vsub_vadd, vsub_vadd] end /-- Subtracting two points in the reverse order produces the negation of subtracting them. -/ @[simp] lemma neg_vsub_eq_vsub_rev (p1 p2 : P) : -(p1 -ᵥ p2) = (p2 -ᵥ p1) := begin refine neg_eq_of_add_eq_zero (vadd_right_cancel p1 _), rw [vsub_add_vsub_cancel, vsub_self], end /-- Subtracting the result of adding a group element produces the same result as subtracting the points and subtracting that group element. -/ lemma vsub_vadd_eq_vsub_sub (p1 p2 : P) (g : G) : p1 -ᵥ (g +ᵥ p2) = (p1 -ᵥ p2) - g := by rw [←add_right_inj (p2 -ᵥ p1 : G), vsub_add_vsub_cancel, ←neg_vsub_eq_vsub_rev, vadd_vsub, ←add_sub_assoc, ←neg_vsub_eq_vsub_rev, neg_add_self, zero_sub] /-- Cancellation subtracting the results of two subtractions. -/ @[simp] lemma vsub_sub_vsub_cancel_right (p1 p2 p3 : P) : (p1 -ᵥ p3) - (p2 -ᵥ p3) = (p1 -ᵥ p2) := by rw [←vsub_vadd_eq_vsub_sub, vsub_vadd] /-- Convert between an equality with adding a group element to a point and an equality of a subtraction of two points with a group element. -/ lemma eq_vadd_iff_vsub_eq (p1 : P) (g : G) (p2 : P) : p1 = g +ᵥ p2 ↔ p1 -ᵥ p2 = g := ⟨λ h, h.symm ▸ vadd_vsub _ _, λ h, h ▸ (vsub_vadd _ _).symm⟩ /-- The pairwise differences of a set of points. -/ def vsub_set (s : set P) : set G := set.image2 (-ᵥ) s s /-- `vsub_set` of an empty set. -/ @[simp] lemma vsub_set_empty : vsub_set (∅ : set P) = ∅ := set.image2_empty_left /-- `vsub_set` of a single point. -/ @[simp] lemma vsub_set_singleton (p : P) : vsub_set ({p} : set P) = {(0:G)} := by simp [vsub_set] /-- `vsub_set` of a finite set is finite. -/ lemma vsub_set_finite_of_finite {s : set P} (h : set.finite s) : set.finite (vsub_set s) := h.image2 _ h /-- Each pairwise difference is in the `vsub_set`. -/ lemma vsub_mem_vsub_set {p1 p2 : P} {s : set P} (hp1 : p1 ∈ s) (hp2 : p2 ∈ s) : (p1 -ᵥ p2) ∈ vsub_set s := set.mem_image2_of_mem hp1 hp2 /-- `vsub_set` is contained in `vsub_set` of a larger set. -/ lemma vsub_set_mono {s1 s2 : set P} (h : s1 ⊆ s2) : vsub_set s1 ⊆ vsub_set s2 := set.image2_subset h h @[simp] lemma vadd_vsub_vadd_cancel_right (v₁ v₂ : G) (p : P) : (v₁ +ᵥ p) -ᵥ (v₂ +ᵥ p) = v₁ - v₂ := by rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, vsub_self, add_zero] /-- If the same point subtracted from two points produces equal results, those points are equal. -/ lemma vsub_left_cancel {p1 p2 p : P} (h : p1 -ᵥ p = p2 -ᵥ p) : p1 = p2 := by rwa [←sub_eq_zero, vsub_sub_vsub_cancel_right, vsub_eq_zero_iff_eq] at h /-- The same point subtracted from two points produces equal results if and only if those points are equal. -/ @[simp] lemma vsub_left_cancel_iff {p1 p2 p : P} : (p1 -ᵥ p) = p2 -ᵥ p ↔ p1 = p2 := ⟨vsub_left_cancel, λ h, h ▸ rfl⟩ /-- Subtracting the point `p` is an injective function. -/ lemma vsub_left_injective (p : P) : function.injective ((-ᵥ p) : P → G) := λ p2 p3, vsub_left_cancel /-- If subtracting two points from the same point produces equal results, those points are equal. -/ lemma vsub_right_cancel {p1 p2 p : P} (h : p -ᵥ p1 = p -ᵥ p2) : p1 = p2 := begin refine vadd_left_cancel (p -ᵥ p2) _, rw [vsub_vadd, ← h, vsub_vadd] end /-- Subtracting two points from the same point produces equal results if and only if those points are equal. -/ @[simp] lemma vsub_right_cancel_iff {p1 p2 p : P} : p -ᵥ p1 = p -ᵥ p2 ↔ p1 = p2 := ⟨vsub_right_cancel, λ h, h ▸ rfl⟩ /-- Subtracting a point from the point `p` is an injective function. -/ lemma vsub_right_injective (p : P) : function.injective ((-ᵥ) p : P → G) := λ p2 p3, vsub_right_cancel end general section comm variables {G : Type*} {P : Type*} [add_comm_group G] [add_torsor G P] /-- Cancellation subtracting the results of two subtractions. -/ @[simp] lemma vsub_sub_vsub_cancel_left (p1 p2 p3 : P) : (p3 -ᵥ p2 : G) - (p3 -ᵥ p1) = (p1 -ᵥ p2) := by rw [sub_eq_add_neg, neg_vsub_eq_vsub_rev, add_comm, vsub_add_vsub_cancel] @[simp] lemma vadd_vsub_vadd_cancel_left (v : G) (p1 p2 : P) : (v +ᵥ p1) -ᵥ (v +ᵥ p2) = p1 -ᵥ p2 := by rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub_cancel'] lemma vsub_vadd_comm (p1 p2 p3 : P) : (p1 -ᵥ p2 : G) +ᵥ p3 = p3 -ᵥ p2 +ᵥ p1 := begin rw [←@vsub_eq_zero_iff_eq G, vadd_vsub_assoc, vsub_vadd_eq_vsub_sub], simp end end comm namespace prod variables {G : Type*} {P : Type*} {G' : Type*} {P' : Type*} [add_group G] [add_group G'] [add_torsor G P] [add_torsor G' P'] instance : add_torsor (G × G') (P × P') := { vadd := λ v p, (v.1 +ᵥ p.1, v.2 +ᵥ p.2), zero_vadd' := λ p, by simp, vadd_assoc' := by simp [vadd_assoc], vsub := λ p₁ p₂, (p₁.1 -ᵥ p₂.1, p₁.2 -ᵥ p₂.2), nonempty := prod.nonempty, vsub_vadd' := λ p₁ p₂, show (p₁.1 -ᵥ p₂.1 +ᵥ p₂.1, _) = p₁, by simp, vadd_vsub' := λ v p, show (v.1 +ᵥ p.1 -ᵥ p.1, v.2 +ᵥ p.2 -ᵥ p.2) =v, by simp } @[simp] lemma fst_vadd (v : G × G') (p : P × P') : (v +ᵥ p).1 = v.1 +ᵥ p.1 := rfl @[simp] lemma snd_vadd (v : G × G') (p : P × P') : (v +ᵥ p).2 = v.2 +ᵥ p.2 := rfl @[simp] lemma mk_vadd_mk (v : G) (v' : G') (p : P) (p' : P') : (v, v') +ᵥ (p, p') = (v +ᵥ p, v' +ᵥ p') := rfl @[simp] lemma fst_vsub (p₁ p₂ : P × P') : (p₁ -ᵥ p₂ : G × G').1 = p₁.1 -ᵥ p₂.1 := rfl @[simp] lemma snd_vsub (p₁ p₂ : P × P') : (p₁ -ᵥ p₂ : G × G').2 = p₁.2 -ᵥ p₂.2 := rfl @[simp] lemma mk_vsub_mk (p₁ p₂ : P) (p₁' p₂' : P') : ((p₁, p₁') -ᵥ (p₂, p₂') : G × G') = (p₁ -ᵥ p₂, p₁' -ᵥ p₂') := rfl end prod namespace pi universes u v w variables {I : Type u} {fg : I → Type v} [∀ i, add_group (fg i)] {fp : I → Type w} open add_action add_torsor /-- A product of `add_torsor`s is an `add_torsor`. -/ instance [T : ∀ i, add_torsor (fg i) (fp i)] : add_torsor (Π i, fg i) (Π i, fp i) := { vadd := λ g p, λ i, g i +ᵥ p i, zero_vadd' := λ p, funext $ λ i, zero_vadd (fg i) (p i), vadd_assoc' := λ g₁ g₂ p, funext $ λ i, vadd_assoc (g₁ i) (g₂ i) (p i), vsub := λ p₁ p₂, λ i, p₁ i -ᵥ p₂ i, nonempty := ⟨λ i, classical.choice (T i).nonempty⟩, vsub_vadd' := λ p₁ p₂, funext $ λ i, vsub_vadd (p₁ i) (p₂ i), vadd_vsub' := λ g p, funext $ λ i, vadd_vsub (g i) (p i) } /-- Addition in a product of `add_torsor`s. -/ @[simp] lemma vadd_apply [T : ∀ i, add_torsor (fg i) (fp i)] (x : Π i, fg i) (y : Π i, fp i) {i : I} : (x +ᵥ y) i = x i +ᵥ y i := rfl end pi namespace equiv variables (G : Type*) {P : Type*} [add_group G] [add_torsor G P] open add_action add_torsor /-- `v ↦ v +ᵥ p` as an equivalence. -/ def vadd_const (p : P) : G ≃ P := { to_fun := λ v, v +ᵥ p, inv_fun := λ p', p' -ᵥ p, left_inv := λ v, vadd_vsub _ _, right_inv := λ p', vsub_vadd _ _ } @[simp] lemma coe_vadd_const (p : P) : ⇑(vadd_const G p) = λ v, v+ᵥ p := rfl @[simp] lemma coe_vadd_const_symm (p : P) : ⇑(vadd_const G p).symm = λ p', p' -ᵥ p := rfl variables {G} (P) /-- The permutation given by `p ↦ v +ᵥ p`. -/ def const_vadd (v : G) : equiv.perm P := { to_fun := (+ᵥ) v, inv_fun := (+ᵥ) (-v), left_inv := λ p, by simp [vadd_assoc], right_inv := λ p, by simp [vadd_assoc] } @[simp] lemma coe_const_vadd (v : G) : ⇑(const_vadd P v) = (+ᵥ) v := rfl variable (G) @[simp] lemma const_vadd_zero : const_vadd P (0:G) = 1 := ext $ zero_vadd G variable {G} @[simp] lemma const_vadd_add (v₁ v₂ : G) : const_vadd P (v₁ + v₂) = const_vadd P v₁ * const_vadd P v₂ := ext $ λ p, (vadd_assoc v₁ v₂ p).symm /-- `equiv.const_vadd` as a homomorphism from `multiplicative G` to `equiv.perm P` -/ def const_vadd_hom : multiplicative G →* equiv.perm P := { to_fun := λ v, const_vadd P v.to_add, map_one' := const_vadd_zero G P, map_mul' := const_vadd_add P } end equiv
db7e69a1510592ef718778c08c02a683488c6fc2
7cef822f3b952965621309e88eadf618da0c8ae9
/test/omega.lean
71e9bfc9e50b414167b383285bd2a6586914e380
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
2,861
lean
/- Test cases for omega. Most of the examples are from John Harrison's Handbook of Practical Logic and Automated Reasoning. -/ import tactic.omega example (n : ℤ) : n - 1 ≠ n := by omega example (x : int) : (x = 5 ∨ x = 7) → 2 < x := by omega example (x : int) : x ≤ -x → x ≤ 0 := by omega example : ∀ x y : int, (x ≤ 5 ∧ y ≤ 3) → x + y ≤ 8 := by omega example : ∀ (x y z : int), x < y → y < z → x < z := by omega example (x y z : int) : x - y ≤ x - z → z ≤ y:= by omega example (x : int) (h1 : x = -5 ∨ x = 7) (h2 : x = 0) : false := by omega example : ∀ x : int, 31 * x > 0 → x > 0 := by omega example (x y : int) : (-x - y < x - y) → (x - y < x + y) → (x > 0 ∧ y > 0) := by omega example : ∀ (x : int), (x ≥ -1 ∧ x ≤ 1) → (x = -1 ∨ x = 0 ∨ x = 1) := by omega example : ∀ (x : int), 5 * x = 5 → x = 1 := by omega example (x y : int) : ∀ z w v : int, x = y → y = z → x = z := by omega example : ∀ x : int, x < 349 ∨ x > 123 := by omega example : ∀ x y : int, x ≤ 3 * y → 3 * x ≤ 9 * y := by omega example (x : int) (h1 : x < 43 ∧ x > 513) : false := by omega example (x y z w : int) : x ≤ y → y ≤ z → z ≤ w → x ≤ w:= by omega example (x y z : int) : ∀ w v : int, 100 = x → x = y → y = z → z = w → w = v → v = 100 := by omega example (x : nat) : 31 * x > 0 → x > 0 := by omega example (x y : nat) : (x ≤ 5 ∧ y ≤ 3) → x + y ≤ 8 := by omega example : ∀ (x y z y : nat), ¬(2 * x + 1 = 2 * y) := by omega example : ∀ (x y : nat), x > 0 → x + y > 0 := by omega example : ∀ (x : nat), x < 349 ∨ x > 123 := by omega example : ∀ (x y : nat), (x = 2 ∨ x = 10) → (x = 3 * y) → false := by omega example (x y : nat) : x ≤ 3 * y → 3 * x ≤ 9 * y := by omega example (x y z : nat) : (x ≤ y) → (z > y) → (x - z = 0) := by omega example (x y z : nat) : x - 5 > 122 → y ≤ 127 → y < x := by omega example : ∀ (x y : nat), x ≤ y ↔ x - y = 0 := by omega example (k : nat) (h : 1 * 1 + 1 * 1 + 1 = 1 * 1 * k) : k = 3 := by omega example (a b : ℕ) (h : a < b + 1) (ha : a.prime) : a ≤ b := by omega example (a b c : ℕ) (h : a < b + 1) (ha : c.prime) : a ≤ b := by omega example (a b : ℕ) (h : a < b + 1) (p : fin a) : a ≤ b := by omega example : nat.zero = nat.zero := by omega example : 3 < 4 := by omega /- Use `omega manual` to disable automatic reverts, and `omega int` or `omega nat` to specify the domain. -/ example (i : int) (n : nat) (h1 : n = 0) (h2 : i < i) : false := by omega int example (i : int) (n : nat) (h1 : i = 0) (h2 : n < n) : false := by omega nat example (x y z w : int) (h1 : 3 * y ≥ x) (h2 : z > 19 * w) : 3 * x ≤ 9 * y := by {revert h1 x y, omega manual} example (n : nat) (h1 : n < 34) (i : int) (h2 : i * 9 = -72) : i = -8 := by {revert h2 i, omega manual int}
ba17adac856c8b631e5f0edfdde31f1c31f23950
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/analysis/normed_space/hahn_banach.lean
a8e8fa01b5204e725be4eaa4a0efb4f1ae85a9d8
[]
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
3,575
lean
/- Copyright (c) 2020 Yury Kudryashov All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Heather Macbeth -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.analysis.normed_space.operator_norm import Mathlib.analysis.normed_space.extend import Mathlib.analysis.convex.cone import Mathlib.data.complex.is_R_or_C import Mathlib.PostPort universes u_1 u_2 u v namespace Mathlib /-! # Hahn-Banach theorem In this file we prove a version of Hahn-Banach theorem for continuous linear functions on normed spaces over `ℝ` and `ℂ`. In order to state and prove its corollaries uniformly, we prove the statements for a field `𝕜` satisfying `is_R_or_C 𝕜`. In this setting, `exists_dual_vector` states that, for any nonzero `x`, there exists a continuous linear form `g` of norm `1` with `g x = ∥x∥` (where the norm has to be interpreted as an element of `𝕜`). -/ /-- The norm of `x` as an element of `𝕜` (a normed algebra over `ℝ`). This is needed in particular to state equalities of the form `g x = norm' 𝕜 x` when `g` is a linear function. For the concrete cases of `ℝ` and `𝕜`, this is just `∥x∥` and `↑∥x∥`, respectively. -/ def norm' (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] [normed_algebra ℝ 𝕜] {E : Type u_2} [normed_group E] (x : E) : 𝕜 := coe_fn (algebra_map ℝ 𝕜) (norm x) theorem norm'_def (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] [normed_algebra ℝ 𝕜] {E : Type u_2} [normed_group E] (x : E) : norm' 𝕜 x = coe_fn (algebra_map ℝ 𝕜) (norm x) := rfl theorem norm_norm' (𝕜 : Type u_1) [nondiscrete_normed_field 𝕜] [normed_algebra ℝ 𝕜] (A : Type u_2) [normed_group A] (x : A) : norm (norm' 𝕜 x) = norm x := sorry namespace real /-- Hahn-Banach theorem for continuous linear functions over `ℝ`. -/ theorem exists_extension_norm_eq {E : Type u_1} [normed_group E] [normed_space ℝ E] (p : subspace ℝ E) (f : continuous_linear_map ℝ ↥p ℝ) : ∃ (g : continuous_linear_map ℝ E ℝ), (∀ (x : ↥p), coe_fn g ↑x = coe_fn f x) ∧ norm g = norm f := sorry end real /-- Hahn-Banach theorem for continuous linear functions over `𝕜` satisyfing `is_R_or_C 𝕜`. -/ theorem exists_extension_norm_eq {𝕜 : Type u_1} [is_R_or_C 𝕜] {F : Type u_2} [normed_group F] [normed_space 𝕜 F] (p : subspace 𝕜 F) (f : continuous_linear_map 𝕜 (↥p) 𝕜) : ∃ (g : continuous_linear_map 𝕜 F 𝕜), (∀ (x : ↥p), coe_fn g ↑x = coe_fn f x) ∧ norm g = norm f := sorry theorem coord_norm' {𝕜 : Type v} [is_R_or_C 𝕜] {E : Type u} [normed_group E] [normed_space 𝕜 E] (x : E) (h : x ≠ 0) : norm (norm' 𝕜 x • continuous_linear_equiv.coord 𝕜 x h) = 1 := sorry /-- Corollary of Hahn-Banach. Given a nonzero element `x` of a normed space, there exists an element of the dual space, of norm `1`, whose value on `x` is `∥x∥`. -/ theorem exists_dual_vector {𝕜 : Type v} [is_R_or_C 𝕜] {E : Type u} [normed_group E] [normed_space 𝕜 E] (x : E) (h : x ≠ 0) : ∃ (g : continuous_linear_map 𝕜 E 𝕜), norm g = 1 ∧ coe_fn g x = norm' 𝕜 x := sorry /-- Variant of Hahn-Banach, eliminating the hypothesis that `x` be nonzero, and choosing the dual element arbitrarily when `x = 0`. -/ theorem exists_dual_vector' {𝕜 : Type v} [is_R_or_C 𝕜] {E : Type u} [normed_group E] [normed_space 𝕜 E] [nontrivial E] (x : E) : ∃ (g : continuous_linear_map 𝕜 E 𝕜), norm g = 1 ∧ coe_fn g x = norm' 𝕜 x := sorry
d21132a9b00588238a89a8ad0a65ce2fc69151da
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/blast_unit.lean
d2599d32811f0fac1681faf05d3297f4ba6359b4
[ "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
6,237
lean
variables {A₁ A₂ A₃ A₄ B₁ B₂ B₃ B₄ : Prop} meta def blast : tactic unit := using_smt $ return () example (b1 : B₁) (b2 : B₂) (H : ¬ A₁ → ¬ B₁ ∨ ¬ B₂) : A₁ := by blast example (b1 : B₁) (H : ¬ A₁ → ¬ B₁) : A₁ := by blast example (b1 : B₁) (n1 : ¬ A₂) (H : ¬ A₂ → ¬ A₁ → ¬ B₁) : A₁ := by blast -- H first, all pos example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₃) : B₄ := by blast example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₄) : B₃ := by blast example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬ B₁) (n3 : ¬ B₃) (n3 : ¬ B₄) : B₂ := by blast example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (a3 : A₃) (n2 : ¬ B₂) (n3 : ¬ B₃) (n3 : ¬ B₄) : B₁ := by blast example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a2 : A₂) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₃) (n3 : ¬ B₄) : ¬ A₃ := by blast example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a1 : A₁) (a3 : A₃) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₃) (n3 : ¬ B₄) : ¬ A₂ := by blast example (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) (a2 : A₂) (a3 : A₃) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₃) (n3 : ¬ B₄) : ¬ A₁ := by blast -- H last, all pos example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₃) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₄ := by blast example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₃ := by blast example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n1 : ¬ B₁) (n3 : ¬ B₃) (n3 : ¬ B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₂ := by blast example (a1 : A₁) (a2 : A₂) (a3 : A₃) (n2 : ¬ B₂) (n3 : ¬ B₃) (n3 : ¬ B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : B₁ := by blast example (a1 : A₁) (a2 : A₂) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₃) (n3 : ¬ B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬ A₃ := by blast example (a1 : A₁) (a3 : A₃) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₃) (n3 : ¬ B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬ A₂ := by blast example (a2 : A₂) (a3 : A₃) (n1 : ¬ B₁) (n2 : ¬ B₂) (n3 : ¬ B₃) (n3 : ¬ B₄) (H : A₁ → A₂ → A₃ → B₁ ∨ B₂ ∨ B₃ ∨ B₄) : ¬ A₁ := by blast -- H first, all neg example (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) (n1 : ¬ A₁) (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) : ¬ B₄ := by blast example (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) (n1 : ¬ A₁) (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b4 : B₄) : ¬ B₃ := by blast example (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) (n1 : ¬ A₁) (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b3 : B₃) (b4 : B₄) : ¬ B₂ := by blast example (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) (n1 : ¬ A₁) (n2 : ¬ A₂) (n3 : ¬ A₃) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬ B₁ := by blast example (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) (n1 : ¬ A₁) (n2 : ¬ A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬ ¬ A₃ := by blast example (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) (n1 : ¬ A₁) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬ ¬ A₂ := by blast example (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) : ¬ ¬ A₁ := by blast -- H last, all neg example (n1 : ¬ A₁) (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : ¬ B₄ := by blast example (n1 : ¬ A₁) (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : ¬ B₃ := by blast example (n1 : ¬ A₁) (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b3 : B₃) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : ¬ B₂ := by blast example (n1 : ¬ A₁) (n2 : ¬ A₂) (n3 : ¬ A₃) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : ¬ B₁ := by blast example (n1 : ¬ A₁) (n2 : ¬ A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : ¬ ¬ A₃ := by blast example (n1 : ¬ A₁) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : ¬ ¬ A₂ := by blast example (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : ¬ ¬ A₁ := by blast example (n1 : ¬ A₁) (n2 : ¬ A₂) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : A₃ := by blast example (n1 : ¬ A₁) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : A₂ := by blast example (n2 : ¬ A₂) (n3 : ¬ A₃) (b1 : B₁) (b2 : B₂) (b3 : B₃) (b4 : B₄) (H : ¬ A₁ → ¬ A₂ → ¬ A₃ → ¬ B₁ ∨ ¬ B₂ ∨ ¬ B₃ ∨ ¬ B₄) : A₁ := by blast
aee10cd6f312b0cb3705f68aba97864efcf863ef
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/uni_bug1.lean
4d839b544f66f5967b68b365c729de33d272aea5
[ "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
186
lean
import data.prod open nat prod constant R : nat → nat → Prop constant f (a b : nat) (H : R a b) : nat axiom Rtrue (a b : nat) : R a b check f 1 0 (Rtrue (pr1 (pair 1 (0:nat))) 0)
7856f9c0834d1aeca49bfb1e4163965d81e47511
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/topology/constructions.lean
ca6b51d16274ce8dcd7f8f890700c16a01ea47ea
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
31,823
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import topology.maps /-! # Constructions of new topological spaces from old ones This file constructs products, sums, subtypes and quotients of topological spaces and sets up their basic theory, such as criteria for maps into or out of these constructions to be continuous; descriptions of the open sets, neighborhood filters, and generators of these constructions; and their behavior with respect to embeddings and other specific classes of maps. ## Implementation note The constructed topologies are defined using induced and coinduced topologies along with the complete lattice structure on topologies. Their universal properties (for example, a map `X → Y × Z` is continuous if and only if both projections `X → Y`, `X → Z` are) follow easily using order-theoretic descriptions of continuity. With more work we can also extract descriptions of the open sets, neighborhood filters and so on. ## Tags product, sum, disjoint union, subspace, quotient space -/ noncomputable theory open topological_space set filter open_locale classical topological_space filter universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type x} section constructions instance {p : α → Prop} [t : topological_space α] : topological_space (subtype p) := induced coe t instance {r : α → α → Prop} [t : topological_space α] : topological_space (quot r) := coinduced (quot.mk r) t instance {s : setoid α} [t : topological_space α] : topological_space (quotient s) := coinduced quotient.mk t instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α × β) := induced prod.fst t₁ ⊓ induced prod.snd t₂ instance [t₁ : topological_space α] [t₂ : topological_space β] : topological_space (α ⊕ β) := coinduced sum.inl t₁ ⊔ coinduced sum.inr t₂ instance {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (sigma β) := ⨆a, coinduced (sigma.mk a) (t₂ a) instance Pi.topological_space {β : α → Type v} [t₂ : Πa, topological_space (β a)] : topological_space (Πa, β a) := ⨅a, induced (λf, f a) (t₂ a) lemma quotient_dense_of_dense [setoid α] [topological_space α] {s : set α} (H : ∀ x, x ∈ closure s) : closure (quotient.mk '' s) = univ := eq_univ_of_forall $ λ x, begin rw mem_closure_iff, intros U U_op x_in_U, let V := quotient.mk ⁻¹' U, cases quotient.exists_rep x with y y_x, have y_in_V : y ∈ V, by simp only [mem_preimage, y_x, x_in_U], have V_op : is_open V := U_op, obtain ⟨w, w_in_V, w_in_range⟩ : (V ∩ s).nonempty := mem_closure_iff.1 (H y) V V_op y_in_V, exact ⟨_, w_in_V, mem_image_of_mem quotient.mk w_in_range⟩ end instance {p : α → Prop} [topological_space α] [discrete_topology α] : discrete_topology (subtype p) := ⟨bot_unique $ assume s hs, ⟨subtype.val '' s, is_open_discrete _, (set.preimage_image_eq _ subtype.val_injective)⟩⟩ instance sum.discrete_topology [topological_space α] [topological_space β] [hα : discrete_topology α] [hβ : discrete_topology β] : discrete_topology (α ⊕ β) := ⟨by unfold sum.topological_space; simp [hα.eq_bot, hβ.eq_bot]⟩ instance sigma.discrete_topology {β : α → Type v} [Πa, topological_space (β a)] [h : Πa, discrete_topology (β a)] : discrete_topology (sigma β) := ⟨by { unfold sigma.topological_space, simp [λ a, (h a).eq_bot] }⟩ section topα variable [topological_space α] /- The 𝓝 filter and the subspace topology. -/ theorem mem_nhds_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) : t ∈ 𝓝 a ↔ ∃ u ∈ 𝓝 a.val, (@subtype.val α s) ⁻¹' u ⊆ t := mem_nhds_induced subtype.val a t theorem nhds_subtype (s : set α) (a : {x // x ∈ s}) : 𝓝 a = comap subtype.val (𝓝 a.val) := nhds_induced subtype.val a end topα end constructions section prod open topological_space variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] lemma continuous_fst : continuous (@prod.fst α β) := continuous_inf_dom_left continuous_induced_dom lemma continuous_at_fst {p : α × β} : continuous_at prod.fst p := continuous_fst.continuous_at lemma continuous_snd : continuous (@prod.snd α β) := continuous_inf_dom_right continuous_induced_dom lemma continuous_at_snd {p : α × β} : continuous_at prod.snd p := continuous_snd.continuous_at lemma continuous.prod_mk {f : γ → α} {g : γ → β} (hf : continuous f) (hg : continuous g) : continuous (λx, (f x, g x)) := continuous_inf_rng (continuous_induced_rng hf) (continuous_induced_rng hg) lemma continuous.prod_map {f : γ → α} {g : δ → β} (hf : continuous f) (hg : continuous g) : continuous (λ x : γ × δ, (f x.1, g x.2)) := (hf.comp continuous_fst).prod_mk (hg.comp continuous_snd) lemma filter.eventually.prod_inl_nhds {p : α → Prop} {a : α} (h : ∀ᶠ x in 𝓝 a, p x) (b : β) : ∀ᶠ x in 𝓝 (a, b), p (x : α × β).1 := continuous_at_fst h lemma filter.eventually.prod_inr_nhds {p : β → Prop} {b : β} (h : ∀ᶠ x in 𝓝 b, p x) (a : α) : ∀ᶠ x in 𝓝 (a, b), p (x : α × β).2 := continuous_at_snd h lemma filter.eventually.prod_mk_nhds {pa : α → Prop} {a} (ha : ∀ᶠ x in 𝓝 a, pa x) {pb : β → Prop} {b} (hb : ∀ᶠ y in 𝓝 b, pb y) : ∀ᶠ p in 𝓝 (a, b), pa (p : α × β).1 ∧ pb p.2 := (ha.prod_inl_nhds b).and (hb.prod_inr_nhds a) lemma continuous_swap : continuous (prod.swap : α × β → β × α) := continuous.prod_mk continuous_snd continuous_fst lemma is_open_prod {s : set α} {t : set β} (hs : is_open s) (ht : is_open t) : is_open (set.prod s t) := is_open_inter (continuous_fst s hs) (continuous_snd t ht) lemma nhds_prod_eq {a : α} {b : β} : 𝓝 (a, b) = filter.prod (𝓝 a) (𝓝 b) := by rw [filter.prod, prod.topological_space, nhds_inf, nhds_induced, nhds_induced] instance [discrete_topology α] [discrete_topology β] : discrete_topology (α × β) := ⟨eq_of_nhds_eq_nhds $ assume ⟨a, b⟩, by rw [nhds_prod_eq, nhds_discrete α, nhds_discrete β, nhds_bot, filter.prod_pure_pure]⟩ lemma prod_mem_nhds_sets {s : set α} {t : set β} {a : α} {b : β} (ha : s ∈ 𝓝 a) (hb : t ∈ 𝓝 b) : set.prod s t ∈ 𝓝 (a, b) := by rw [nhds_prod_eq]; exact prod_mem_prod ha hb lemma nhds_swap (a : α) (b : β) : 𝓝 (a, b) = (𝓝 (b, a)).map prod.swap := by rw [nhds_prod_eq, filter.prod_comm, nhds_prod_eq]; refl lemma filter.tendsto.prod_mk_nhds {γ} {a : α} {b : β} {f : filter γ} {ma : γ → α} {mb : γ → β} (ha : tendsto ma f (𝓝 a)) (hb : tendsto mb f (𝓝 b)) : tendsto (λc, (ma c, mb c)) f (𝓝 (a, b)) := by rw [nhds_prod_eq]; exact filter.tendsto.prod_mk ha hb lemma continuous_at.prod {f : α → β} {g : α → γ} {x : α} (hf : continuous_at f x) (hg : continuous_at g x) : continuous_at (λx, (f x, g x)) x := hf.prod_mk_nhds hg lemma continuous_at.prod_map {f : α → γ} {g : β → δ} {p : α × β} (hf : continuous_at f p.fst) (hg : continuous_at g p.snd) : continuous_at (λ p : α × β, (f p.1, g p.2)) p := (hf.comp continuous_fst.continuous_at).prod (hg.comp continuous_snd.continuous_at) lemma continuous_at.prod_map' {f : α → γ} {g : β → δ} {x : α} {y : β} (hf : continuous_at f x) (hg : continuous_at g y) : continuous_at (λ p : α × β, (f p.1, g p.2)) (x, y) := have hf : continuous_at f (x, y).fst, from hf, have hg : continuous_at g (x, y).snd, from hg, hf.prod_map hg lemma prod_generate_from_generate_from_eq {α : Type*} {β : Type*} {s : set (set α)} {t : set (set β)} (hs : ⋃₀ s = univ) (ht : ⋃₀ t = univ) : @prod.topological_space α β (generate_from s) (generate_from t) = generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} := let G := generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} in le_antisymm (le_generate_from $ assume g ⟨u, hu, v, hv, g_eq⟩, g_eq.symm ▸ @is_open_prod _ _ (generate_from s) (generate_from t) _ _ (generate_open.basic _ hu) (generate_open.basic _ hv)) (le_inf (coinduced_le_iff_le_induced.mp $ le_generate_from $ assume u hu, have (⋃v∈t, set.prod u v) = prod.fst ⁻¹' u, from calc (⋃v∈t, set.prod u v) = set.prod u univ : set.ext $ assume ⟨a, b⟩, by rw ← ht; simp [and.left_comm] {contextual:=tt} ... = prod.fst ⁻¹' u : by simp [set.prod, preimage], show G.is_open (prod.fst ⁻¹' u), from this ▸ @is_open_Union _ _ G _ $ assume v, @is_open_Union _ _ G _ $ assume hv, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩) (coinduced_le_iff_le_induced.mp $ le_generate_from $ assume v hv, have (⋃u∈s, set.prod u v) = prod.snd ⁻¹' v, from calc (⋃u∈s, set.prod u v) = set.prod univ v: set.ext $ assume ⟨a, b⟩, by rw [←hs]; by_cases b ∈ v; simp [h] {contextual:=tt} ... = prod.snd ⁻¹' v : by simp [set.prod, preimage], show G.is_open (prod.snd ⁻¹' v), from this ▸ @is_open_Union _ _ G _ $ assume u, @is_open_Union _ _ G _ $ assume hu, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩)) lemma prod_eq_generate_from : prod.topological_space = generate_from {g | ∃(s:set α) (t:set β), is_open s ∧ is_open t ∧ g = set.prod s t} := le_antisymm (le_generate_from $ assume g ⟨s, t, hs, ht, g_eq⟩, g_eq.symm ▸ is_open_prod hs ht) (le_inf (ball_image_of_ball $ λt ht, generate_open.basic _ ⟨t, univ, by simpa [set.prod_eq] using ht⟩) (ball_image_of_ball $ λt ht, generate_open.basic _ ⟨univ, t, by simpa [set.prod_eq] using ht⟩)) lemma is_open_prod_iff {s : set (α×β)} : is_open s ↔ (∀a b, (a, b) ∈ s → ∃u v, is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v ⊆ s) := begin rw [is_open_iff_nhds], simp [nhds_prod_eq, mem_prod_iff], simp [mem_nhds_sets_iff], exact forall_congr (assume a, ball_congr $ assume b h, ⟨assume ⟨u', ⟨u, us, uo, au⟩, v', ⟨v, vs, vo, bv⟩, h⟩, ⟨u, uo, v, vo, au, bv, subset.trans (set.prod_mono us vs) h⟩, assume ⟨u, uo, v, vo, au, bv, h⟩, ⟨u, ⟨u, subset.refl u, uo, au⟩, v, ⟨v, subset.refl v, vo, bv⟩, h⟩⟩) end /-- The first projection in a product of topological spaces sends open sets to open sets. -/ lemma is_open_map_fst : is_open_map (@prod.fst α β) := begin assume s hs, rw is_open_iff_forall_mem_open, assume x xs, rw mem_image_eq at xs, rcases xs with ⟨⟨y₁, y₂⟩, ys, yx⟩, rcases is_open_prod_iff.1 hs _ _ ys with ⟨o₁, o₂, o₁_open, o₂_open, yo₁, yo₂, ho⟩, simp at yx, rw yx at yo₁, refine ⟨o₁, _, o₁_open, yo₁⟩, assume z zs, rw mem_image_eq, exact ⟨(z, y₂), ho (by simp [zs, yo₂]), rfl⟩ end /-- The second projection in a product of topological spaces sends open sets to open sets. -/ lemma is_open_map_snd : is_open_map (@prod.snd α β) := begin /- This lemma could be proved by composing the fact that the first projection is open, and exchanging coordinates is a homeomorphism, hence open. As the `prod_comm` homeomorphism is defined later, we rather go for the direct proof, copy-pasting the proof for the first projection. -/ assume s hs, rw is_open_iff_forall_mem_open, assume x xs, rw mem_image_eq at xs, rcases xs with ⟨⟨y₁, y₂⟩, ys, yx⟩, rcases is_open_prod_iff.1 hs _ _ ys with ⟨o₁, o₂, o₁_open, o₂_open, yo₁, yo₂, ho⟩, simp at yx, rw yx at yo₂, refine ⟨o₂, _, o₂_open, yo₂⟩, assume z zs, rw mem_image_eq, exact ⟨(y₁, z), ho (by simp [zs, yo₁]), rfl⟩ end /-- A product set is open in a product space if and only if each factor is open, or one of them is empty -/ lemma is_open_prod_iff' {s : set α} {t : set β} : is_open (set.prod s t) ↔ (is_open s ∧ is_open t) ∨ (s = ∅) ∨ (t = ∅) := begin cases (set.prod s t).eq_empty_or_nonempty with h h, { simp [h, prod_eq_empty_iff.1 h] }, { have st : s.nonempty ∧ t.nonempty, from prod_nonempty_iff.1 h, split, { assume H : is_open (set.prod s t), refine or.inl ⟨_, _⟩, show is_open s, { rw ← fst_image_prod s st.2, exact is_open_map_fst _ H }, show is_open t, { rw ← snd_image_prod st.1 t, exact is_open_map_snd _ H } }, { assume H, simp [st.1.ne_empty, st.2.ne_empty] at H, exact is_open_prod H.1 H.2 } } end lemma closure_prod_eq {s : set α} {t : set β} : closure (set.prod s t) = set.prod (closure s) (closure t) := set.ext $ assume ⟨a, b⟩, have filter.prod (𝓝 a) (𝓝 b) ⊓ principal (set.prod s t) = filter.prod (𝓝 a ⊓ principal s) (𝓝 b ⊓ principal t), by rw [←prod_inf_prod, prod_principal_principal], by simp [closure_eq_nhds, nhds_prod_eq, this]; exact prod_ne_bot lemma mem_closure2 {s : set α} {t : set β} {u : set γ} {f : α → β → γ} {a : α} {b : β} (hf : continuous (λp:α×β, f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (hu : ∀a b, a ∈ s → b ∈ t → f a b ∈ u) : f a b ∈ closure u := have (a, b) ∈ closure (set.prod s t), by rw [closure_prod_eq]; from ⟨ha, hb⟩, show (λp:α×β, f p.1 p.2) (a, b) ∈ closure u, from mem_closure hf this $ assume ⟨a, b⟩ ⟨ha, hb⟩, hu a b ha hb lemma is_closed_prod {s₁ : set α} {s₂ : set β} (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (set.prod s₁ s₂) := closure_eq_iff_is_closed.mp $ by simp [h₁, h₂, closure_prod_eq, closure_eq_of_is_closed] lemma inducing.prod_mk {f : α → β} {g : γ → δ} (hf : inducing f) (hg : inducing g) : inducing (λx:α×γ, (f x.1, g x.2)) := ⟨by rw [prod.topological_space, prod.topological_space, hf.induced, hg.induced, induced_compose, induced_compose, induced_inf, induced_compose, induced_compose]⟩ lemma embedding.prod_mk {f : α → β} {g : γ → δ} (hf : embedding f) (hg : embedding g) : embedding (λx:α×γ, (f x.1, g x.2)) := { inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨hf.inj h₁, hg.inj h₂⟩, ..hf.to_inducing.prod_mk hg.to_inducing } protected lemma is_open_map.prod {f : α → β} {g : γ → δ} (hf : is_open_map f) (hg : is_open_map g) : is_open_map (λ p : α × γ, (f p.1, g p.2)) := begin rw [is_open_map_iff_nhds_le], rintros ⟨a, b⟩, rw [nhds_prod_eq, nhds_prod_eq, ← filter.prod_map_map_eq], exact filter.prod_mono (is_open_map_iff_nhds_le.1 hf a) (is_open_map_iff_nhds_le.1 hg b) end protected lemma open_embedding.prod {f : α → β} {g : γ → δ} (hf : open_embedding f) (hg : open_embedding g) : open_embedding (λx:α×γ, (f x.1, g x.2)) := open_embedding_of_embedding_open (hf.1.prod_mk hg.1) (hf.is_open_map.prod hg.is_open_map) lemma embedding_graph {f : α → β} (hf : continuous f) : embedding (λx, (x, f x)) := embedding_of_embedding_compose (continuous_id.prod_mk hf) continuous_fst embedding_id end prod section sum variables [topological_space α] [topological_space β] [topological_space γ] lemma continuous_inl : continuous (@sum.inl α β) := continuous_sup_rng_left continuous_coinduced_rng lemma continuous_inr : continuous (@sum.inr α β) := continuous_sup_rng_right continuous_coinduced_rng lemma continuous_sum_rec {f : α → γ} {g : β → γ} (hf : continuous f) (hg : continuous g) : @continuous (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) := continuous_sup_dom hf hg lemma embedding_inl : embedding (@sum.inl α β) := { induced := begin unfold sum.topological_space, apply le_antisymm, { rw ← coinduced_le_iff_le_induced, exact le_sup_left }, { intros u hu, existsi (sum.inl '' u), change (is_open (sum.inl ⁻¹' (@sum.inl α β '' u)) ∧ is_open (sum.inr ⁻¹' (@sum.inl α β '' u))) ∧ sum.inl ⁻¹' (sum.inl '' u) = u, have : sum.inl ⁻¹' (@sum.inl α β '' u) = u := preimage_image_eq u (λ _ _, sum.inl.inj_iff.mp), rw this, have : sum.inr ⁻¹' (@sum.inl α β '' u) = ∅ := eq_empty_iff_forall_not_mem.mpr (assume a ⟨b, _, h⟩, sum.inl_ne_inr h), rw this, exact ⟨⟨hu, is_open_empty⟩, rfl⟩ } end, inj := λ _ _, sum.inl.inj_iff.mp } lemma embedding_inr : embedding (@sum.inr α β) := { induced := begin unfold sum.topological_space, apply le_antisymm, { rw ← coinduced_le_iff_le_induced, exact le_sup_right }, { intros u hu, existsi (sum.inr '' u), change (is_open (sum.inl ⁻¹' (@sum.inr α β '' u)) ∧ is_open (sum.inr ⁻¹' (@sum.inr α β '' u))) ∧ sum.inr ⁻¹' (sum.inr '' u) = u, have : sum.inl ⁻¹' (@sum.inr α β '' u) = ∅ := eq_empty_iff_forall_not_mem.mpr (assume b ⟨a, _, h⟩, sum.inr_ne_inl h), rw this, have : sum.inr ⁻¹' (@sum.inr α β '' u) = u := preimage_image_eq u (λ _ _, sum.inr.inj_iff.mp), rw this, exact ⟨⟨is_open_empty, hu⟩, rfl⟩ } end, inj := λ _ _, sum.inr.inj_iff.mp } end sum section subtype variables [topological_space α] [topological_space β] [topological_space γ] {p : α → Prop} lemma embedding_subtype_val : embedding (@subtype.val α p) := ⟨⟨rfl⟩, subtype.val_injective⟩ lemma continuous_subtype_val : continuous (@subtype.val α p) := continuous_induced_dom lemma continuous_subtype_coe : continuous (coe : subtype p → α) := continuous_subtype_val lemma is_open.open_embedding_subtype_val {s : set α} (hs : is_open s) : open_embedding (subtype.val : s → α) := { induced := rfl, inj := subtype.val_injective, open_range := (subtype.val_range : range subtype.val = s).symm ▸ hs } lemma is_open.is_open_map_subtype_val {s : set α} (hs : is_open s) : is_open_map (subtype.val : s → α) := hs.open_embedding_subtype_val.is_open_map lemma is_open_map.restrict {f : α → β} (hf : is_open_map f) {s : set α} (hs : is_open s) : is_open_map (s.restrict f) := hf.comp hs.is_open_map_subtype_val lemma is_closed.closed_embedding_subtype_val {s : set α} (hs : is_closed s) : closed_embedding (subtype.val : {x // x ∈ s} → α) := { induced := rfl, inj := subtype.val_injective, closed_range := (subtype.val_range : range subtype.val = s).symm ▸ hs } lemma continuous_subtype_mk {f : β → α} (hp : ∀x, p (f x)) (h : continuous f) : continuous (λx, (⟨f x, hp x⟩ : subtype p)) := continuous_induced_rng h lemma continuous_inclusion {s t : set α} (h : s ⊆ t) : continuous (inclusion h) := continuous_subtype_mk _ continuous_subtype_val lemma continuous_at_subtype_val {p : α → Prop} {a : subtype p} : continuous_at subtype.val a := continuous_iff_continuous_at.mp continuous_subtype_val _ lemma map_nhds_subtype_val_eq {a : α} (ha : p a) (h : {a | p a} ∈ 𝓝 a) : map (@subtype.val α p) (𝓝 ⟨a, ha⟩) = 𝓝 a := map_nhds_induced_eq (by simp [subtype.val_image, h]) lemma nhds_subtype_eq_comap {a : α} {h : p a} : 𝓝 (⟨a, h⟩ : subtype p) = comap subtype.val (𝓝 a) := nhds_induced _ _ lemma tendsto_subtype_rng {β : Type*} {p : α → Prop} {b : filter β} {f : β → subtype p} : ∀{a:subtype p}, tendsto f b (𝓝 a) ↔ tendsto (λx, subtype.val (f x)) b (𝓝 a.val) | ⟨a, ha⟩ := by rw [nhds_subtype_eq_comap, tendsto_comap_iff] lemma continuous_subtype_nhds_cover {ι : Sort*} {f : α → β} {c : ι → α → Prop} (c_cover : ∀x:α, ∃i, {x | c i x} ∈ 𝓝 x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x.val)) : continuous f := continuous_iff_continuous_at.mpr $ assume x, let ⟨i, (c_sets : {x | c i x} ∈ 𝓝 x)⟩ := c_cover x in let x' : subtype (c i) := ⟨x, mem_of_nhds c_sets⟩ in calc map f (𝓝 x) = map f (map subtype.val (𝓝 x')) : congr_arg (map f) (map_nhds_subtype_val_eq _ $ c_sets).symm ... = map (λx:subtype (c i), f x.val) (𝓝 x') : rfl ... ≤ 𝓝 (f x) : continuous_iff_continuous_at.mp (f_cont i) x' lemma continuous_subtype_is_closed_cover {ι : Sort*} {f : α → β} (c : ι → α → Prop) (h_lf : locally_finite (λi, {x | c i x})) (h_is_closed : ∀i, is_closed {x | c i x}) (h_cover : ∀x, ∃i, c i x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x.val)) : continuous f := continuous_iff_is_closed.mpr $ assume s hs, have ∀i, is_closed (@subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), from assume i, embedding_is_closed embedding_subtype_val (by simp [subtype.val_range]; exact h_is_closed i) (continuous_iff_is_closed.mp (f_cont i) _ hs), have is_closed (⋃i, @subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), from is_closed_Union_of_locally_finite (locally_finite_subset h_lf $ assume i x ⟨⟨x', hx'⟩, _, heq⟩, heq ▸ hx') this, have f ⁻¹' s = (⋃i, @subtype.val α {x | c i x} '' (f ∘ subtype.val ⁻¹' s)), begin apply set.ext, have : ∀ (x : α), f x ∈ s ↔ ∃ (i : ι), c i x ∧ f x ∈ s := λ x, ⟨λ hx, let ⟨i, hi⟩ := h_cover x in ⟨i, hi, hx⟩, λ ⟨i, hi, hx⟩, hx⟩, simp [and.comm, and.left_comm], simpa [(∘)], end, by rwa [this] lemma closure_subtype {x : {a // p a}} {s : set {a // p a}}: x ∈ closure s ↔ x.val ∈ closure (subtype.val '' s) := closure_induced $ assume x y, subtype.eq end subtype section quotient variables [topological_space α] [topological_space β] [topological_space γ] variables {r : α → α → Prop} {s : setoid α} lemma quotient_map_quot_mk : quotient_map (@quot.mk α r) := ⟨quot.exists_rep, rfl⟩ lemma continuous_quot_mk : continuous (@quot.mk α r) := continuous_coinduced_rng lemma continuous_quot_lift {f : α → β} (hr : ∀ a b, r a b → f a = f b) (h : continuous f) : continuous (quot.lift f hr : quot r → β) := continuous_coinduced_dom h lemma quotient_map_quotient_mk : quotient_map (@quotient.mk α s) := quotient_map_quot_mk lemma continuous_quotient_mk : continuous (@quotient.mk α s) := continuous_coinduced_rng lemma continuous_quotient_lift {f : α → β} (hs : ∀ a b, a ≈ b → f a = f b) (h : continuous f) : continuous (quotient.lift f hs : quotient s → β) := continuous_coinduced_dom h end quotient section pi variables {ι : Type*} {π : ι → Type*} open topological_space lemma continuous_pi [topological_space α] [∀i, topological_space (π i)] {f : α → Πi:ι, π i} (h : ∀i, continuous (λa, f a i)) : continuous f := continuous_infi_rng $ assume i, continuous_induced_rng $ h i lemma continuous_apply [∀i, topological_space (π i)] (i : ι) : continuous (λp:Πi, π i, p i) := continuous_infi_dom continuous_induced_dom /-- Embedding a factor into a product space (by fixing arbitrarily all the other coordinates) is continuous. -/ lemma continuous_update [decidable_eq ι] [∀i, topological_space (π i)] {i : ι} {f : Πi:ι, π i} : continuous (λ x : π i, function.update f i x) := begin refine continuous_pi (λj, _), by_cases h : j = i, { rw h, simpa using continuous_id }, { simpa [h] using continuous_const } end lemma nhds_pi [t : ∀i, topological_space (π i)] {a : Πi, π i} : 𝓝 a = (⨅i, comap (λx, x i) (𝓝 (a i))) := calc 𝓝 a = (⨅i, @nhds _ (@topological_space.induced _ _ (λx:Πi, π i, x i) (t i)) a) : nhds_infi ... = (⨅i, comap (λx, x i) (𝓝 (a i))) : by simp [nhds_induced] lemma is_open_set_pi [∀a, topological_space (π a)] {i : set ι} {s : Πa, set (π a)} (hi : finite i) (hs : ∀a∈i, is_open (s a)) : is_open (pi i s) := by rw [pi_def]; exact (is_open_bInter hi $ assume a ha, continuous_apply a _ $ hs a ha) lemma pi_eq_generate_from [∀a, topological_space (π a)] : Pi.topological_space = generate_from {g | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, is_open (s a)) ∧ g = pi ↑i s} := le_antisymm (le_generate_from $ assume g ⟨s, i, hi, eq⟩, eq.symm ▸ is_open_set_pi (finset.finite_to_set _) hi) (le_infi $ assume a s ⟨t, ht, s_eq⟩, generate_open.basic _ $ ⟨function.update (λa, univ) a t, {a}, by simpa using ht, by ext f; simp [s_eq.symm, pi]⟩) lemma pi_generate_from_eq {g : Πa, set (set (π a))} : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} := let G := {t | ∃(s:Πa, set (π a)) (i : finset ι), (∀a∈i, s a ∈ g a) ∧ t = pi ↑i s} in begin rw [pi_eq_generate_from], refine le_antisymm (generate_from_mono _) (le_generate_from _), exact assume s ⟨t, i, ht, eq⟩, ⟨t, i, assume a ha, generate_open.basic _ (ht a ha), eq⟩, { rintros s ⟨t, i, hi, rfl⟩, rw [pi_def], apply is_open_bInter (finset.finite_to_set _), assume a ha, show ((generate_from G).coinduced (λf:Πa, π a, f a)).is_open (t a), refine le_generate_from _ _ (hi a ha), exact assume s hs, generate_open.basic _ ⟨function.update (λa, univ) a s, {a}, by simp [hs]⟩ } end lemma pi_generate_from_eq_fintype {g : Πa, set (set (π a))} [fintype ι] (hg : ∀a, ⋃₀ g a = univ) : @Pi.topological_space ι π (λa, generate_from (g a)) = generate_from {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} := let G := {t | ∃(s:Πa, set (π a)), (∀a, s a ∈ g a) ∧ t = pi univ s} in begin rw [pi_generate_from_eq], refine le_antisymm (generate_from_mono _) (le_generate_from _), exact assume s ⟨t, ht, eq⟩, ⟨t, finset.univ, by simp [ht, eq]⟩, { rintros s ⟨t, i, ht, rfl⟩, apply is_open_iff_forall_mem_open.2 _, assume f hf, choose c hc using show ∀a, ∃s, s ∈ g a ∧ f a ∈ s, { assume a, have : f a ∈ ⋃₀ g a, { rw [hg], apply mem_univ }, simpa }, refine ⟨pi univ (λa, if a ∈ i then t a else (c : Πa, set (π a)) a), _, _, _⟩, { simp [pi_if] }, { refine generate_open.basic _ ⟨_, assume a, _, rfl⟩, by_cases a ∈ i; simp [*, pi] at * }, { have : f ∈ pi {a | a ∉ i} c, { simp [*, pi] at * }, simpa [pi_if, hf] } } end end pi section sigma variables {ι : Type*} {σ : ι → Type*} [Π i, topological_space (σ i)] lemma continuous_sigma_mk {i : ι} : continuous (@sigma.mk ι σ i) := continuous_supr_rng continuous_coinduced_rng lemma is_open_sigma_iff {s : set (sigma σ)} : is_open s ↔ ∀ i, is_open (sigma.mk i ⁻¹' s) := by simp only [is_open_supr_iff, is_open_coinduced] lemma is_closed_sigma_iff {s : set (sigma σ)} : is_closed s ↔ ∀ i, is_closed (sigma.mk i ⁻¹' s) := is_open_sigma_iff lemma is_open_map_sigma_mk {i : ι} : is_open_map (@sigma.mk ι σ i) := begin intros s hs, rw is_open_sigma_iff, intro j, classical, by_cases h : i = j, { subst j, convert hs, exact set.preimage_image_eq _ injective_sigma_mk }, { convert is_open_empty, apply set.eq_empty_of_subset_empty, rintro x ⟨y, _, hy⟩, have : i = j, by cc, contradiction } end lemma is_open_range_sigma_mk {i : ι} : is_open (set.range (@sigma.mk ι σ i)) := by { rw ←set.image_univ, exact is_open_map_sigma_mk _ is_open_univ } lemma is_closed_map_sigma_mk {i : ι} : is_closed_map (@sigma.mk ι σ i) := begin intros s hs, rw is_closed_sigma_iff, intro j, classical, by_cases h : i = j, { subst j, convert hs, exact set.preimage_image_eq _ injective_sigma_mk }, { convert is_closed_empty, apply set.eq_empty_of_subset_empty, rintro x ⟨y, _, hy⟩, have : i = j, by cc, contradiction } end lemma is_closed_sigma_mk {i : ι} : is_closed (set.range (@sigma.mk ι σ i)) := by { rw ←set.image_univ, exact is_closed_map_sigma_mk _ is_closed_univ } lemma open_embedding_sigma_mk {i : ι} : open_embedding (@sigma.mk ι σ i) := open_embedding_of_continuous_injective_open continuous_sigma_mk injective_sigma_mk is_open_map_sigma_mk lemma closed_embedding_sigma_mk {i : ι} : closed_embedding (@sigma.mk ι σ i) := closed_embedding_of_continuous_injective_closed continuous_sigma_mk injective_sigma_mk is_closed_map_sigma_mk lemma embedding_sigma_mk {i : ι} : embedding (@sigma.mk ι σ i) := closed_embedding_sigma_mk.1 /-- A map out of a sum type is continuous if its restriction to each summand is. -/ lemma continuous_sigma [topological_space β] {f : sigma σ → β} (h : ∀ i, continuous (λ a, f ⟨i, a⟩)) : continuous f := continuous_supr_dom (λ i, continuous_coinduced_dom (h i)) lemma continuous_sigma_map {κ : Type*} {τ : κ → Type*} [Π k, topological_space (τ k)] {f₁ : ι → κ} {f₂ : Π i, σ i → τ (f₁ i)} (hf : ∀ i, continuous (f₂ i)) : continuous (sigma.map f₁ f₂) := continuous_sigma $ λ i, show continuous (λ a, sigma.mk (f₁ i) (f₂ i a)), from continuous_sigma_mk.comp (hf i) lemma is_open_map_sigma [topological_space β] {f : sigma σ → β} (h : ∀ i, is_open_map (λ a, f ⟨i, a⟩)) : is_open_map f := begin intros s hs, rw is_open_sigma_iff at hs, have : s = ⋃ i, sigma.mk i '' (sigma.mk i ⁻¹' s), { rw Union_image_preimage_sigma_mk_eq_self }, rw this, rw [image_Union], apply is_open_Union, intro i, rw [image_image], exact h i _ (hs i) end /-- The sum of embeddings is an embedding. -/ lemma embedding_sigma_map {τ : ι → Type*} [Π i, topological_space (τ i)] {f : Π i, σ i → τ i} (hf : ∀ i, embedding (f i)) : embedding (sigma.map id f) := begin refine ⟨⟨_⟩, injective_sigma_map function.injective_id (λ i, (hf i).inj)⟩, refine le_antisymm (continuous_iff_le_induced.mp (continuous_sigma_map (λ i, (hf i).continuous))) _, intros s hs, replace hs := is_open_sigma_iff.mp hs, have : ∀ i, ∃ t, is_open t ∧ f i ⁻¹' t = sigma.mk i ⁻¹' s, { intro i, apply is_open_induced_iff.mp, convert hs i, exact (hf i).induced.symm }, choose t ht using this, apply is_open_induced_iff.mpr, refine ⟨⋃ i, sigma.mk i '' t i, is_open_Union (λ i, is_open_map_sigma_mk _ (ht i).1), _⟩, ext p, rcases p with ⟨i, x⟩, change (sigma.mk i (f i x) ∈ ⋃ (i : ι), sigma.mk i '' t i) ↔ x ∈ sigma.mk i ⁻¹' s, rw [←(ht i).2, mem_Union], split, { rintro ⟨j, hj⟩, rw mem_image at hj, rcases hj with ⟨y, hy₁, hy₂⟩, rcases sigma.mk.inj_iff.mp hy₂ with ⟨rfl, hy⟩, replace hy := eq_of_heq hy, subst y, exact hy₁ }, { intro hx, use i, rw mem_image, exact ⟨f i x, hx, rfl⟩ } end end sigma lemma mem_closure_of_continuous [topological_space α] [topological_space β] {f : α → β} {a : α} {s : set α} {t : set β} (hf : continuous f) (ha : a ∈ closure s) (h : ∀a∈s, f a ∈ closure t) : f a ∈ closure t := calc f a ∈ f '' closure s : mem_image_of_mem _ ha ... ⊆ closure (f '' s) : image_closure_subset_closure_image hf ... ⊆ closure (closure t) : closure_mono $ image_subset_iff.mpr $ h ... ⊆ closure t : begin rw [closure_eq_of_is_closed], exact subset.refl _, exact is_closed_closure end lemma mem_closure_of_continuous2 [topological_space α] [topological_space β] [topological_space γ] {f : α → β → γ} {a : α} {b : β} {s : set α} {t : set β} {u : set γ} (hf : continuous (λp:α×β, f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (h : ∀a∈s, ∀b∈t, f a b ∈ closure u) : f a b ∈ closure u := have (a,b) ∈ closure (set.prod s t), by simp [closure_prod_eq, ha, hb], show f (a, b).1 (a, b).2 ∈ closure u, from @mem_closure_of_continuous (α×β) _ _ _ (λp:α×β, f p.1 p.2) (a,b) _ u hf this $ assume ⟨p₁, p₂⟩ ⟨h₁, h₂⟩, h p₁ h₁ p₂ h₂
aed8cc491b2b4d635db5bc658cb459536b6cbde6
206422fb9edabf63def0ed2aa3f489150fb09ccb
/src/topology/instances/ennreal.lean
a805391db59321121e6e968f0a461451bf76e24d
[ "Apache-2.0" ]
permissive
hamdysalah1/mathlib
b915f86b2503feeae268de369f1b16932321f097
95454452f6b3569bf967d35aab8d852b1ddf8017
refs/heads/master
1,677,154,116,545
1,611,797,994,000
1,611,797,994,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
44,180
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Johannes Hölzl -/ import topology.instances.nnreal /-! # Extended non-negative reals -/ noncomputable theory open classical set filter metric open_locale classical topological_space ennreal nnreal big_operators filter variables {α : Type*} {β : Type*} {γ : Type*} namespace ennreal variables {a b c d : ennreal} {r p q : ℝ≥0} variables {x y z : ennreal} {ε ε₁ ε₂ : ennreal} {s : set ennreal} section topological_space open topological_space /-- Topology on `ennreal`. Note: this is different from the `emetric_space` topology. The `emetric_space` topology has `is_open {⊤}`, while this topology doesn't have singleton elements. -/ instance : topological_space ennreal := preorder.topology ennreal instance : order_topology ennreal := ⟨rfl⟩ instance : t2_space ennreal := by apply_instance -- short-circuit type class inference instance : second_countable_topology ennreal := ⟨⟨⋃q ≥ (0:ℚ), {{a : ennreal | a < nnreal.of_real q}, {a : ennreal | ↑(nnreal.of_real q) < a}}, (countable_encodable _).bUnion $ assume a ha, (countable_singleton _).insert _, le_antisymm (le_generate_from $ by simp [or_imp_distrib, is_open_lt', is_open_gt'] {contextual := tt}) (le_generate_from $ λ s h, begin rcases h with ⟨a, hs | hs⟩; [ rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ a < nnreal.of_real q}, {b | ↑(nnreal.of_real q) < b}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn a b, and_assoc]), rw show s = ⋃q∈{q:ℚ | 0 ≤ q ∧ ↑(nnreal.of_real q) < a}, {b | b < ↑(nnreal.of_real q)}, from set.ext (assume b, by simp [hs, @ennreal.lt_iff_exists_rat_btwn b a, and_comm, and_assoc])]; { apply is_open_Union, intro q, apply is_open_Union, intro hq, exact generate_open.basic _ (mem_bUnion hq.1 $ by simp) } end)⟩⟩ lemma embedding_coe : embedding (coe : ℝ≥0 → ennreal) := ⟨⟨begin refine le_antisymm _ _, { rw [@order_topology.topology_eq_generate_intervals ennreal _, ← coinduced_le_iff_le_induced], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, show is_open {b : ℝ≥0 | a < ↑b}, { cases a; simp [none_eq_top, some_eq_coe, is_open_lt'] }, show is_open {b : ℝ≥0 | ↑b < a}, { cases a; simp [none_eq_top, some_eq_coe, is_open_gt', is_open_const] } }, { rw [@order_topology.topology_eq_generate_intervals ℝ≥0 _], refine le_generate_from (assume s ha, _), rcases ha with ⟨a, rfl | rfl⟩, exact ⟨Ioi a, is_open_Ioi, by simp [Ioi]⟩, exact ⟨Iio a, is_open_Iio, by simp [Iio]⟩ } end⟩, assume a b, coe_eq_coe.1⟩ lemma is_open_ne_top : is_open {a : ennreal | a ≠ ⊤} := is_open_ne lemma is_open_Ico_zero : is_open (Ico 0 b) := by { rw ennreal.Ico_eq_Iio, exact is_open_Iio} lemma coe_range_mem_nhds : range (coe : ℝ≥0 → ennreal) ∈ 𝓝 (r : ennreal) := have {a : ennreal | a ≠ ⊤} = range (coe : ℝ≥0 → ennreal), from set.ext $ assume a, by cases a; simp [none_eq_top, some_eq_coe], this ▸ mem_nhds_sets is_open_ne_top coe_ne_top @[norm_cast] lemma tendsto_coe {f : filter α} {m : α → ℝ≥0} {a : ℝ≥0} : tendsto (λa, (m a : ennreal)) f (𝓝 ↑a) ↔ tendsto m f (𝓝 a) := embedding_coe.tendsto_nhds_iff.symm lemma continuous_coe : continuous (coe : ℝ≥0 → ennreal) := embedding_coe.continuous lemma continuous_coe_iff {α} [topological_space α] {f : α → ℝ≥0} : continuous (λa, (f a : ennreal)) ↔ continuous f := embedding_coe.continuous_iff.symm lemma nhds_coe {r : ℝ≥0} : 𝓝 (r : ennreal) = (𝓝 r).map coe := by rw [embedding_coe.induced, map_nhds_induced_eq coe_range_mem_nhds] lemma nhds_coe_coe {r p : ℝ≥0} : 𝓝 ((r : ennreal), (p : ennreal)) = (𝓝 (r, p)).map (λp:ℝ≥0×ℝ≥0, (p.1, p.2)) := begin rw [(embedding_coe.prod_mk embedding_coe).map_nhds_eq], rw [← prod_range_range_eq], exact prod_mem_nhds_sets coe_range_mem_nhds coe_range_mem_nhds end lemma continuous_of_real : continuous ennreal.of_real := (continuous_coe_iff.2 continuous_id).comp nnreal.continuous_of_real lemma tendsto_of_real {f : filter α} {m : α → ℝ} {a : ℝ} (h : tendsto m f (𝓝 a)) : tendsto (λa, ennreal.of_real (m a)) f (𝓝 (ennreal.of_real a)) := tendsto.comp (continuous.tendsto continuous_of_real _) h lemma tendsto_to_nnreal {a : ennreal} : a ≠ ⊤ → tendsto (ennreal.to_nnreal) (𝓝 a) (𝓝 a.to_nnreal) := begin cases a; simp [some_eq_coe, none_eq_top, nhds_coe, tendsto_map'_iff, (∘)], exact tendsto_id end lemma continuous_on_to_nnreal : continuous_on ennreal.to_nnreal {a | a ≠ ∞} := continuous_on_iff_continuous_restrict.2 $ continuous_iff_continuous_at.2 $ λ x, (tendsto_to_nnreal x.2).comp continuous_at_subtype_coe lemma tendsto_to_real {a : ennreal} : a ≠ ⊤ → tendsto (ennreal.to_real) (𝓝 a) (𝓝 a.to_real) := λ ha, tendsto.comp ((@nnreal.tendsto_coe _ (𝓝 a.to_nnreal) id (a.to_nnreal)).2 tendsto_id) (tendsto_to_nnreal ha) /-- The set of finite `ennreal` numbers is homeomorphic to `ℝ≥0`. -/ def ne_top_homeomorph_nnreal : {a | a ≠ ∞} ≃ₜ ℝ≥0 := { continuous_to_fun := continuous_on_iff_continuous_restrict.1 continuous_on_to_nnreal, continuous_inv_fun := continuous_subtype_mk _ continuous_coe, .. ne_top_equiv_nnreal } /-- The set of finite `ennreal` numbers is homeomorphic to `ℝ≥0`. -/ def lt_top_homeomorph_nnreal : {a | a < ∞} ≃ₜ ℝ≥0 := by refine (homeomorph.set_congr $ set.ext $ λ x, _).trans ne_top_homeomorph_nnreal; simp only [mem_set_of_eq, lt_top_iff_ne_top] lemma nhds_top : 𝓝 ∞ = ⨅ a ≠ ∞, 𝓟 (Ioi a) := nhds_top_order.trans $ by simp [lt_top_iff_ne_top, Ioi] lemma nhds_top' : 𝓝 ∞ = ⨅ r : ℝ≥0, 𝓟 (Ioi r) := nhds_top.trans $ infi_ne_top _ lemma tendsto_nhds_top_iff_nnreal {m : α → ennreal} {f : filter α} : tendsto m f (𝓝 ⊤) ↔ ∀ x : ℝ≥0, ∀ᶠ a in f, ↑x < m a := by simp only [nhds_top', tendsto_infi, tendsto_principal, mem_Ioi] lemma tendsto_nhds_top_iff_nat {m : α → ennreal} {f : filter α} : tendsto m f (𝓝 ⊤) ↔ ∀ n : ℕ, ∀ᶠ a in f, ↑n < m a := tendsto_nhds_top_iff_nnreal.trans ⟨λ h n, by simpa only [ennreal.coe_nat] using h n, λ h x, let ⟨n, hn⟩ := exists_nat_gt x in (h n).mono (λ y, lt_trans $ by rwa [← ennreal.coe_nat, coe_lt_coe])⟩ lemma tendsto_nhds_top {m : α → ennreal} {f : filter α} (h : ∀ n : ℕ, ∀ᶠ a in f, ↑n < m a) : tendsto m f (𝓝 ⊤) := tendsto_nhds_top_iff_nat.2 h lemma tendsto_nat_nhds_top : tendsto (λ n : ℕ, ↑n) at_top (𝓝 ∞) := tendsto_nhds_top $ λ n, mem_at_top_sets.2 ⟨n+1, λ m hm, ennreal.coe_nat_lt_coe_nat.2 $ nat.lt_of_succ_le hm⟩ @[simp, norm_cast] lemma tendsto_coe_nhds_top {f : α → ℝ≥0} {l : filter α} : tendsto (λ x, (f x : ennreal)) l (𝓝 ∞) ↔ tendsto f l at_top := by rw [tendsto_nhds_top_iff_nnreal, at_top_basis_Ioi.tendsto_right_iff]; [simp, apply_instance, apply_instance] lemma nhds_zero : 𝓝 (0 : ennreal) = ⨅a ≠ 0, 𝓟 (Iio a) := nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot, Iio] @[instance] lemma nhds_within_Ioi_coe_ne_bot {r : ℝ≥0} : (𝓝[Ioi r] (r : ennreal)).ne_bot := nhds_within_Ioi_self_ne_bot' ennreal.coe_lt_top @[instance] lemma nhds_within_Ioi_zero_ne_bot : (𝓝[Ioi 0] (0 : ennreal)).ne_bot := nhds_within_Ioi_coe_ne_bot -- using Icc because -- • don't have 'Ioo (x - ε) (x + ε) ∈ 𝓝 x' unless x > 0 -- • (x - y ≤ ε ↔ x ≤ ε + y) is true, while (x - y < ε ↔ x < ε + y) is not lemma Icc_mem_nhds : x ≠ ⊤ → 0 < ε → Icc (x - ε) (x + ε) ∈ 𝓝 x := begin assume xt ε0, rw mem_nhds_sets_iff, by_cases x0 : x = 0, { use Iio (x + ε), have : Iio (x + ε) ⊆ Icc (x - ε) (x + ε), assume a, rw x0, simpa using le_of_lt, use this, exact ⟨is_open_Iio, mem_Iio_self_add xt ε0⟩ }, { use Ioo (x - ε) (x + ε), use Ioo_subset_Icc_self, exact ⟨is_open_Ioo, mem_Ioo_self_sub_add xt x0 ε0 ε0 ⟩ } end lemma nhds_of_ne_top : x ≠ ⊤ → 𝓝 x = ⨅ε > 0, 𝓟 (Icc (x - ε) (x + ε)) := begin assume xt, refine le_antisymm _ _, -- first direction simp only [le_infi_iff, le_principal_iff], assume ε ε0, exact Icc_mem_nhds xt ε0, -- second direction rw nhds_generate_from, refine le_infi (assume s, le_infi $ assume hs, _), simp only [mem_set_of_eq] at hs, rcases hs with ⟨xs, ⟨a, ha⟩⟩, cases ha, { rw ha at *, rcases exists_between xs with ⟨b, ⟨ab, bx⟩⟩, have xb_pos : x - b > 0 := zero_lt_sub_iff_lt.2 bx, have xxb : x - (x - b) = b := sub_sub_cancel (by rwa lt_top_iff_ne_top) (le_of_lt bx), refine infi_le_of_le (x - b) (infi_le_of_le xb_pos _), simp only [mem_principal_sets, le_principal_iff], assume y, rintros ⟨h₁, h₂⟩, rw xxb at h₁, calc a < b : ab ... ≤ y : h₁ }, { rw ha at *, rcases exists_between xs with ⟨b, ⟨xb, ba⟩⟩, have bx_pos : b - x > 0 := zero_lt_sub_iff_lt.2 xb, have xbx : x + (b - x) = b := add_sub_cancel_of_le (le_of_lt xb), refine infi_le_of_le (b - x) (infi_le_of_le bx_pos _), simp only [mem_principal_sets, le_principal_iff], assume y, rintros ⟨h₁, h₂⟩, rw xbx at h₂, calc y ≤ b : h₂ ... < a : ba }, end /-- Characterization of neighborhoods for `ennreal` numbers. See also `tendsto_order` for a version with strict inequalities. -/ protected theorem tendsto_nhds {f : filter α} {u : α → ennreal} {a : ennreal} (ha : a ≠ ⊤) : tendsto u f (𝓝 a) ↔ ∀ ε > 0, ∀ᶠ x in f, (u x) ∈ Icc (a - ε) (a + ε) := by simp only [nhds_of_ne_top ha, tendsto_infi, tendsto_principal, mem_Icc] protected lemma tendsto_at_top [nonempty β] [semilattice_sup β] {f : β → ennreal} {a : ennreal} (ha : a ≠ ⊤) : tendsto f at_top (𝓝 a) ↔ ∀ε>0, ∃N, ∀n≥N, (f n) ∈ Icc (a - ε) (a + ε) := by simp only [ennreal.tendsto_nhds ha, mem_at_top_sets, mem_set_of_eq, filter.eventually] instance : has_continuous_add ennreal := begin refine ⟨continuous_iff_continuous_at.2 _⟩, rintro ⟨(_|a), b⟩, { exact tendsto_nhds_top_mono' continuous_at_fst (λ p, le_add_right le_rfl) }, rcases b with (_|b), { exact tendsto_nhds_top_mono' continuous_at_snd (λ p, le_add_left le_rfl) }, simp only [continuous_at, some_eq_coe, nhds_coe_coe, ← coe_add, tendsto_map'_iff, (∘), tendsto_coe, tendsto_add] end protected lemma tendsto_mul (ha : a ≠ 0 ∨ b ≠ ⊤) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λp:ennreal×ennreal, p.1 * p.2) (𝓝 (a, b)) (𝓝 (a * b)) := have ht : ∀b:ennreal, b ≠ 0 → tendsto (λp:ennreal×ennreal, p.1 * p.2) (𝓝 ((⊤:ennreal), b)) (𝓝 ⊤), begin refine assume b hb, tendsto_nhds_top_iff_nnreal.2 $ assume n, _, rcases lt_iff_exists_nnreal_btwn.1 (pos_iff_ne_zero.2 hb) with ⟨ε, hε, hεb⟩, replace hε : 0 < ε, from coe_pos.1 hε, filter_upwards [prod_mem_nhds_sets (lt_mem_nhds $ @coe_lt_top (n / ε)) (lt_mem_nhds hεb)], rintros ⟨a₁, a₂⟩ ⟨h₁, h₂⟩, dsimp at h₁ h₂ ⊢, rw [← div_mul_cancel n hε.ne', coe_mul], exact mul_lt_mul h₁ h₂ end, begin cases a, {simp [none_eq_top] at hb, simp [none_eq_top, ht b hb, top_mul, hb] }, cases b, { simp [none_eq_top] at ha, simp [*, nhds_swap (a : ennreal) ⊤, none_eq_top, some_eq_coe, top_mul, tendsto_map'_iff, (∘), mul_comm] }, simp [some_eq_coe, nhds_coe_coe, tendsto_map'_iff, (∘)], simp only [coe_mul.symm, tendsto_coe, tendsto_mul] end protected lemma tendsto.mul {f : filter α} {ma : α → ennreal} {mb : α → ennreal} {a b : ennreal} (hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λa, ma a * mb a) f (𝓝 (a * b)) := show tendsto ((λp:ennreal×ennreal, p.1 * p.2) ∘ (λa, (ma a, mb a))) f (𝓝 (a * b)), from tendsto.comp (ennreal.tendsto_mul ha hb) (hma.prod_mk_nhds hmb) protected lemma tendsto.const_mul {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (𝓝 b)) (hb : b ≠ 0 ∨ a ≠ ⊤) : tendsto (λb, a * m b) f (𝓝 (a * b)) := by_cases (assume : a = 0, by simp [this, tendsto_const_nhds]) (assume ha : a ≠ 0, ennreal.tendsto.mul tendsto_const_nhds (or.inl ha) hm hb) protected lemma tendsto.mul_const {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ ⊤) : tendsto (λx, m x * b) f (𝓝 (a * b)) := by simpa only [mul_comm] using ennreal.tendsto.const_mul hm ha protected lemma continuous_at_const_mul {a b : ennreal} (h : a ≠ ⊤ ∨ b ≠ 0) : continuous_at ((*) a) b := tendsto.const_mul tendsto_id h.symm protected lemma continuous_at_mul_const {a b : ennreal} (h : a ≠ ⊤ ∨ b ≠ 0) : continuous_at (λ x, x * a) b := tendsto.mul_const tendsto_id h.symm protected lemma continuous_const_mul {a : ennreal} (ha : a ≠ ⊤) : continuous ((*) a) := continuous_iff_continuous_at.2 $ λ x, ennreal.continuous_at_const_mul (or.inl ha) protected lemma continuous_mul_const {a : ennreal} (ha : a ≠ ⊤) : continuous (λ x, x * a) := continuous_iff_continuous_at.2 $ λ x, ennreal.continuous_at_mul_const (or.inl ha) lemma le_of_forall_lt_one_mul_le {x y : ennreal} (h : ∀ a < 1, a * x ≤ y) : x ≤ y := begin have : tendsto (* x) (𝓝[Iio 1] 1) (𝓝 (1 * x)) := (ennreal.continuous_at_mul_const (or.inr one_ne_zero)).mono_left inf_le_left, rw one_mul at this, haveI : (𝓝[Iio 1] (1 : ennreal)).ne_bot := nhds_within_Iio_self_ne_bot' ennreal.zero_lt_one, exact le_of_tendsto this (eventually_nhds_within_iff.2 $ eventually_of_forall h) end lemma infi_mul_left {ι} [nonempty ι] {f : ι → ennreal} {a : ennreal} (h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) : (⨅ i, a * f i) = a * ⨅ i, f i := begin by_cases H : a = ⊤ ∧ (⨅ i, f i) = 0, { rcases h H.1 H.2 with ⟨i, hi⟩, rw [H.2, mul_zero, ← bot_eq_zero, infi_eq_bot], exact λ b hb, ⟨i, by rwa [hi, mul_zero, ← bot_eq_zero]⟩ }, { rw not_and_distrib at H, exact (map_infi_of_continuous_at_of_monotone' (ennreal.continuous_at_const_mul H) ennreal.mul_left_mono).symm } end lemma infi_mul_right {ι} [nonempty ι] {f : ι → ennreal} {a : ennreal} (h : a = ⊤ → (⨅ i, f i) = 0 → ∃ i, f i = 0) : (⨅ i, f i * a) = (⨅ i, f i) * a := by simpa only [mul_comm a] using infi_mul_left h protected lemma continuous_inv : continuous (has_inv.inv : ennreal → ennreal) := continuous_iff_continuous_at.2 $ λ a, tendsto_order.2 ⟨begin assume b hb, simp only [@ennreal.lt_inv_iff_lt_inv b], exact gt_mem_nhds (ennreal.lt_inv_iff_lt_inv.1 hb), end, begin assume b hb, simp only [gt_iff_lt, @ennreal.inv_lt_iff_inv_lt _ b], exact lt_mem_nhds (ennreal.inv_lt_iff_inv_lt.1 hb) end⟩ @[simp] protected lemma tendsto_inv_iff {f : filter α} {m : α → ennreal} {a : ennreal} : tendsto (λ x, (m x)⁻¹) f (𝓝 a⁻¹) ↔ tendsto m f (𝓝 a) := ⟨λ h, by simpa only [function.comp, ennreal.inv_inv] using (ennreal.continuous_inv.tendsto a⁻¹).comp h, (ennreal.continuous_inv.tendsto a).comp⟩ protected lemma tendsto.div {f : filter α} {ma : α → ennreal} {mb : α → ennreal} {a b : ennreal} (hma : tendsto ma f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) (hmb : tendsto mb f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) : tendsto (λa, ma a / mb a) f (𝓝 (a / b)) := by { apply tendsto.mul hma _ (ennreal.tendsto_inv_iff.2 hmb) _; simp [ha, hb] } protected lemma tendsto.const_div {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (𝓝 b)) (hb : b ≠ ⊤ ∨ a ≠ ⊤) : tendsto (λb, a / m b) f (𝓝 (a / b)) := by { apply tendsto.const_mul (ennreal.tendsto_inv_iff.2 hm), simp [hb] } protected lemma tendsto.div_const {f : filter α} {m : α → ennreal} {a b : ennreal} (hm : tendsto m f (𝓝 a)) (ha : a ≠ 0 ∨ b ≠ 0) : tendsto (λx, m x / b) f (𝓝 (a / b)) := by { apply tendsto.mul_const hm, simp [ha] } protected lemma tendsto_inv_nat_nhds_zero : tendsto (λ n : ℕ, (n : ennreal)⁻¹) at_top (𝓝 0) := ennreal.inv_top ▸ ennreal.tendsto_inv_iff.2 tendsto_nat_nhds_top lemma bsupr_add {ι} {s : set ι} (hs : s.nonempty) {f : ι → ennreal} : (⨆ i ∈ s, f i) + a = ⨆ i ∈ s, f i + a := begin simp only [← Sup_image], symmetry, rw [image_comp (+ a)], refine is_lub.Sup_eq (is_lub_of_is_lub_of_tendsto _ (is_lub_Sup _) (hs.image _) _), exacts [λ x _ y _ hxy, add_le_add hxy le_rfl, tendsto.add (tendsto_id' inf_le_left) tendsto_const_nhds] end lemma Sup_add {s : set ennreal} (hs : s.nonempty) : Sup s + a = ⨆b∈s, b + a := by rw [Sup_eq_supr, bsupr_add hs] lemma supr_add {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : supr s + a = ⨆b, s b + a := let ⟨x⟩ := h in calc supr s + a = Sup (range s) + a : by rw Sup_range ... = (⨆b∈range s, b + a) : Sup_add ⟨s x, x, rfl⟩ ... = _ : supr_range lemma add_supr {ι : Sort*} {s : ι → ennreal} [h : nonempty ι] : a + supr s = ⨆b, a + s b := by rw [add_comm, supr_add]; simp [add_comm] lemma supr_add_supr {ι : Sort*} {f g : ι → ennreal} (h : ∀i j, ∃k, f i + g j ≤ f k + g k) : supr f + supr g = (⨆ a, f a + g a) := begin by_cases hι : nonempty ι, { letI := hι, refine le_antisymm _ (supr_le $ λ a, add_le_add (le_supr _ _) (le_supr _ _)), simpa [add_supr, supr_add] using λ i j:ι, show f i + g j ≤ ⨆ a, f a + g a, from let ⟨k, hk⟩ := h i j in le_supr_of_le k hk }, { have : ∀f:ι → ennreal, (⨆i, f i) = 0 := λ f, bot_unique (supr_le $ assume i, (hι ⟨i⟩).elim), rw [this, this, this, zero_add] } end lemma supr_add_supr_of_monotone {ι : Sort*} [semilattice_sup ι] {f g : ι → ennreal} (hf : monotone f) (hg : monotone g) : supr f + supr g = (⨆ a, f a + g a) := supr_add_supr $ assume i j, ⟨i ⊔ j, add_le_add (hf $ le_sup_left) (hg $ le_sup_right)⟩ lemma finset_sum_supr_nat {α} {ι} [semilattice_sup ι] {s : finset α} {f : α → ι → ennreal} (hf : ∀a, monotone (f a)) : ∑ a in s, supr (f a) = (⨆ n, ∑ a in s, f a n) := begin refine finset.induction_on s _ _, { simp, exact (bot_unique $ supr_le $ assume i, le_refl ⊥).symm }, { assume a s has ih, simp only [finset.sum_insert has], rw [ih, supr_add_supr_of_monotone (hf a)], assume i j h, exact (finset.sum_le_sum $ assume a ha, hf a h) } end lemma mul_Sup {s : set ennreal} {a : ennreal} : a * Sup s = ⨆i∈s, a * i := begin by_cases hs : ∀x∈s, x = (0:ennreal), { have h₁ : Sup s = 0 := (bot_unique $ Sup_le $ assume a ha, (hs a ha).symm ▸ le_refl 0), have h₂ : (⨆i ∈ s, a * i) = 0 := (bot_unique $ supr_le $ assume a, supr_le $ assume ha, by simp [hs a ha]), rw [h₁, h₂, mul_zero] }, { simp only [not_forall] at hs, rcases hs with ⟨x, hx, hx0⟩, have s₁ : Sup s ≠ 0 := pos_iff_ne_zero.1 (lt_of_lt_of_le (pos_iff_ne_zero.2 hx0) (le_Sup hx)), have : Sup ((λb, a * b) '' s) = a * Sup s := is_lub.Sup_eq (is_lub_of_is_lub_of_tendsto (assume x _ y _ h, canonically_ordered_semiring.mul_le_mul (le_refl _) h) (is_lub_Sup _) ⟨x, hx⟩ (ennreal.tendsto.const_mul (tendsto_id' inf_le_left) (or.inl s₁))), rw [this.symm, Sup_image] } end lemma mul_supr {ι : Sort*} {f : ι → ennreal} {a : ennreal} : a * supr f = ⨆i, a * f i := by rw [← Sup_range, mul_Sup, supr_range] lemma supr_mul {ι : Sort*} {f : ι → ennreal} {a : ennreal} : supr f * a = ⨆i, f i * a := by rw [mul_comm, mul_supr]; congr; funext; rw [mul_comm] protected lemma tendsto_coe_sub : ∀{b:ennreal}, tendsto (λb:ennreal, ↑r - b) (𝓝 b) (𝓝 (↑r - b)) := begin refine (forall_ennreal.2 $ and.intro (assume a, _) _), { simp [@nhds_coe a, tendsto_map'_iff, (∘), tendsto_coe, coe_sub.symm], exact tendsto_const_nhds.sub tendsto_id }, simp, exact (tendsto.congr' (mem_sets_of_superset (lt_mem_nhds $ @coe_lt_top r) $ by simp [le_of_lt] {contextual := tt})) tendsto_const_nhds end lemma sub_supr {ι : Sort*} [hι : nonempty ι] {b : ι → ennreal} (hr : a < ⊤) : a - (⨆i, b i) = (⨅i, a - b i) := let ⟨i⟩ := hι in let ⟨r, eq, _⟩ := lt_iff_exists_coe.mp hr in have Inf ((λb, ↑r - b) '' range b) = ↑r - (⨆i, b i), from is_glb.Inf_eq $ is_glb_of_is_lub_of_tendsto (assume x _ y _, sub_le_sub (le_refl _)) is_lub_supr ⟨_, i, rfl⟩ (tendsto.comp ennreal.tendsto_coe_sub (tendsto_id' inf_le_left)), by rw [eq, ←this]; simp [Inf_image, infi_range, -mem_range]; exact le_refl _ lemma supr_eq_zero {ι : Sort*} {f : ι → ennreal} : (⨆ i, f i) = 0 ↔ ∀ i, f i = 0 := by simp_rw [← nonpos_iff_eq_zero, supr_le_iff] end topological_space section tsum variables {f g : α → ennreal} @[norm_cast] protected lemma has_sum_coe {f : α → ℝ≥0} {r : ℝ≥0} : has_sum (λa, (f a : ennreal)) ↑r ↔ has_sum f r := have (λs:finset α, ∑ a in s, ↑(f a)) = (coe : ℝ≥0 → ennreal) ∘ (λs:finset α, ∑ a in s, f a), from funext $ assume s, ennreal.coe_finset_sum.symm, by unfold has_sum; rw [this, tendsto_coe] protected lemma tsum_coe_eq {f : α → ℝ≥0} (h : has_sum f r) : ∑'a, (f a : ennreal) = r := (ennreal.has_sum_coe.2 h).tsum_eq protected lemma coe_tsum {f : α → ℝ≥0} : summable f → ↑(tsum f) = ∑'a, (f a : ennreal) | ⟨r, hr⟩ := by rw [hr.tsum_eq, ennreal.tsum_coe_eq hr] protected lemma has_sum : has_sum f (⨆s:finset α, ∑ a in s, f a) := tendsto_at_top_supr $ λ s t, finset.sum_le_sum_of_subset @[simp] protected lemma summable : summable f := ⟨_, ennreal.has_sum⟩ lemma tsum_coe_ne_top_iff_summable {f : β → ℝ≥0} : ∑' b, (f b:ennreal) ≠ ∞ ↔ summable f := begin refine ⟨λ h, _, λ h, ennreal.coe_tsum h ▸ ennreal.coe_ne_top⟩, lift (∑' b, (f b:ennreal)) to ℝ≥0 using h with a ha, refine ⟨a, ennreal.has_sum_coe.1 _⟩, rw ha, exact ennreal.summable.has_sum end protected lemma tsum_eq_supr_sum : ∑'a, f a = (⨆s:finset α, ∑ a in s, f a) := ennreal.has_sum.tsum_eq protected lemma tsum_eq_supr_sum' {ι : Type*} (s : ι → finset α) (hs : ∀ t, ∃ i, t ⊆ s i) : ∑' a, f a = ⨆ i, ∑ a in s i, f a := begin rw [ennreal.tsum_eq_supr_sum], symmetry, change (⨆i:ι, (λ t : finset α, ∑ a in t, f a) (s i)) = ⨆s:finset α, ∑ a in s, f a, exact (finset.sum_mono_set f).supr_comp_eq hs end protected lemma tsum_sigma {β : α → Type*} (f : Πa, β a → ennreal) : ∑'p:Σa, β a, f p.1 p.2 = ∑'a b, f a b := tsum_sigma' (assume b, ennreal.summable) ennreal.summable protected lemma tsum_sigma' {β : α → Type*} (f : (Σ a, β a) → ennreal) : ∑'p:(Σa, β a), f p = ∑'a b, f ⟨a, b⟩ := tsum_sigma' (assume b, ennreal.summable) ennreal.summable protected lemma tsum_prod {f : α → β → ennreal} : ∑'p:α×β, f p.1 p.2 = ∑'a, ∑'b, f a b := tsum_prod' ennreal.summable $ λ _, ennreal.summable protected lemma tsum_comm {f : α → β → ennreal} : ∑'a, ∑'b, f a b = ∑'b, ∑'a, f a b := tsum_comm' ennreal.summable (λ _, ennreal.summable) (λ _, ennreal.summable) protected lemma tsum_add : ∑'a, (f a + g a) = (∑'a, f a) + (∑'a, g a) := tsum_add ennreal.summable ennreal.summable protected lemma tsum_le_tsum (h : ∀a, f a ≤ g a) : ∑'a, f a ≤ ∑'a, g a := tsum_le_tsum h ennreal.summable ennreal.summable protected lemma sum_le_tsum {f : α → ennreal} (s : finset α) : ∑ x in s, f x ≤ ∑' x, f x := sum_le_tsum s (λ x hx, zero_le _) ennreal.summable protected lemma tsum_eq_supr_nat' {f : ℕ → ennreal} {N : ℕ → ℕ} (hN : tendsto N at_top at_top) : ∑'i:ℕ, f i = (⨆i:ℕ, ∑ a in finset.range (N i), f a) := ennreal.tsum_eq_supr_sum' _ $ λ t, let ⟨n, hn⟩ := t.exists_nat_subset_range, ⟨k, _, hk⟩ := exists_le_of_tendsto_at_top hN 0 n in ⟨k, finset.subset.trans hn (finset.range_mono hk)⟩ protected lemma tsum_eq_supr_nat {f : ℕ → ennreal} : ∑'i:ℕ, f i = (⨆i:ℕ, ∑ a in finset.range i, f a) := ennreal.tsum_eq_supr_sum' _ finset.exists_nat_subset_range protected lemma le_tsum (a : α) : f a ≤ ∑'a, f a := le_tsum' ennreal.summable a protected lemma tsum_eq_top_of_eq_top : (∃ a, f a = ∞) → ∑' a, f a = ∞ | ⟨a, ha⟩ := top_unique $ ha ▸ ennreal.le_tsum a protected lemma ne_top_of_tsum_ne_top (h : ∑' a, f a ≠ ∞) (a : α) : f a ≠ ∞ := λ ha, h $ ennreal.tsum_eq_top_of_eq_top ⟨a, ha⟩ protected lemma tsum_mul_left : ∑'i, a * f i = a * ∑'i, f i := if h : ∀i, f i = 0 then by simp [h] else let ⟨i, (hi : f i ≠ 0)⟩ := not_forall.mp h in have sum_ne_0 : ∑'i, f i ≠ 0, from ne_of_gt $ calc 0 < f i : lt_of_le_of_ne (zero_le _) hi.symm ... ≤ ∑'i, f i : ennreal.le_tsum _, have tendsto (λs:finset α, ∑ j in s, a * f j) at_top (𝓝 (a * ∑'i, f i)), by rw [← show (*) a ∘ (λs:finset α, ∑ j in s, f j) = λs, ∑ j in s, a * f j, from funext $ λ s, finset.mul_sum]; exact ennreal.tendsto.const_mul ennreal.summable.has_sum (or.inl sum_ne_0), has_sum.tsum_eq this protected lemma tsum_mul_right : (∑'i, f i * a) = (∑'i, f i) * a := by simp [mul_comm, ennreal.tsum_mul_left] @[simp] lemma tsum_supr_eq {α : Type*} (a : α) {f : α → ennreal} : ∑'b:α, (⨆ (h : a = b), f b) = f a := le_antisymm (by rw [ennreal.tsum_eq_supr_sum]; exact supr_le (assume s, calc (∑ b in s, ⨆ (h : a = b), f b) ≤ ∑ b in {a}, ⨆ (h : a = b), f b : finset.sum_le_sum_of_ne_zero $ assume b _ hb, suffices a = b, by simpa using this.symm, classical.by_contradiction $ assume h, by simpa [h] using hb ... = f a : by simp)) (calc f a ≤ (⨆ (h : a = a), f a) : le_supr (λh:a=a, f a) rfl ... ≤ (∑'b:α, ⨆ (h : a = b), f b) : ennreal.le_tsum _) lemma has_sum_iff_tendsto_nat {f : ℕ → ennreal} (r : ennreal) : has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := begin refine ⟨has_sum.tendsto_sum_nat, assume h, _⟩, rw [← supr_eq_of_tendsto _ h, ← ennreal.tsum_eq_supr_nat], { exact ennreal.summable.has_sum }, { exact assume s t hst, finset.sum_le_sum_of_subset (finset.range_subset.2 hst) } end lemma to_nnreal_apply_of_tsum_ne_top {α : Type*} {f : α → ennreal} (hf : ∑' i, f i ≠ ∞) (x : α) : (((ennreal.to_nnreal ∘ f) x : ℝ≥0) : ennreal) = f x := coe_to_nnreal $ ennreal.ne_top_of_tsum_ne_top hf _ lemma summable_to_nnreal_of_tsum_ne_top {α : Type*} {f : α → ennreal} (hf : ∑' i, f i ≠ ∞) : summable (ennreal.to_nnreal ∘ f) := by simpa only [←tsum_coe_ne_top_iff_summable, to_nnreal_apply_of_tsum_ne_top hf] using hf protected lemma tsum_apply {ι α : Type*} {f : ι → α → ennreal} {x : α} : (∑' i, f i) x = ∑' i, f i x := tsum_apply $ pi.summable.mpr $ λ _, ennreal.summable lemma tsum_sub {f : ℕ → ennreal} {g : ℕ → ennreal} (h₁ : ∑' i, g i < ∞) (h₂ : g ≤ f) : ∑' i, (f i - g i) = (∑' i, f i) - (∑' i, g i) := begin have h₃: ∑' i, (f i - g i) = ∑' i, (f i - g i + g i) - ∑' i, g i, { rw [ennreal.tsum_add, add_sub_self h₁]}, have h₄:(λ i, (f i - g i) + (g i)) = f, { ext n, rw ennreal.sub_add_cancel_of_le (h₂ n)}, rw h₄ at h₃, apply h₃, end end tsum end ennreal namespace nnreal open_locale nnreal /-- Comparison test of convergence of `ℝ≥0`-valued series. -/ lemma exists_le_has_sum_of_le {f g : β → ℝ≥0} {r : ℝ≥0} (hgf : ∀b, g b ≤ f b) (hfr : has_sum f r) : ∃p≤r, has_sum g p := have ∑'b, (g b : ennreal) ≤ r, begin refine has_sum_le (assume b, _) ennreal.summable.has_sum (ennreal.has_sum_coe.2 hfr), exact ennreal.coe_le_coe.2 (hgf _) end, let ⟨p, eq, hpr⟩ := ennreal.le_coe_iff.1 this in ⟨p, hpr, ennreal.has_sum_coe.1 $ eq ▸ ennreal.summable.has_sum⟩ /-- Comparison test of convergence of `ℝ≥0`-valued series. -/ lemma summable_of_le {f g : β → ℝ≥0} (hgf : ∀b, g b ≤ f b) : summable f → summable g | ⟨r, hfr⟩ := let ⟨p, _, hp⟩ := exists_le_has_sum_of_le hgf hfr in hp.summable /-- A series of non-negative real numbers converges to `r` in the sense of `has_sum` if and only if the sequence of partial sum converges to `r`. -/ lemma has_sum_iff_tendsto_nat {f : ℕ → ℝ≥0} {r : ℝ≥0} : has_sum f r ↔ tendsto (λn:ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := begin rw [← ennreal.has_sum_coe, ennreal.has_sum_iff_tendsto_nat], simp only [ennreal.coe_finset_sum.symm], exact ennreal.tendsto_coe end lemma not_summable_iff_tendsto_nat_at_top {f : ℕ → ℝ≥0} : ¬ summable f ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top := begin split, { intros h, refine ((tendsto_of_monotone _).resolve_right h).comp _, exacts [finset.sum_mono_set _, tendsto_finset_range] }, { rintros hnat ⟨r, hr⟩, exact not_tendsto_nhds_of_tendsto_at_top hnat _ (has_sum_iff_tendsto_nat.1 hr) } end lemma summable_iff_not_tendsto_nat_at_top {f : ℕ → ℝ≥0} : summable f ↔ ¬ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top := by rw [← not_iff_not, not_not, not_summable_iff_tendsto_nat_at_top] lemma summable_of_sum_range_le {f : ℕ → ℝ≥0} {c : ℝ≥0} (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : summable f := begin apply summable_iff_not_tendsto_nat_at_top.2 (λ H, _), rcases exists_lt_of_tendsto_at_top H 0 c with ⟨n, -, hn⟩, exact lt_irrefl _ (hn.trans_le (h n)), end lemma tsum_le_of_sum_range_le {f : ℕ → ℝ≥0} {c : ℝ≥0} (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : ∑' n, f n ≤ c := le_of_tendsto' (has_sum_iff_tendsto_nat.1 (summable_of_sum_range_le h).has_sum) h lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → ℝ≥0} (hf : summable f) {i : β → α} (hi : function.injective i) : ∑' x, f (i x) ≤ ∑' x, f x := tsum_le_tsum_of_inj i hi (λ c hc, zero_le _) (λ b, le_refl _) (summable_comp_injective hf hi) hf lemma summable_sigma {β : Π x : α, Type*} {f : (Σ x, β x) → ℝ≥0} : summable f ↔ (∀ x, summable (λ y, f ⟨x, y⟩)) ∧ summable (λ x, ∑' y, f ⟨x, y⟩) := begin split, { simp only [← nnreal.summable_coe, nnreal.coe_tsum], exact λ h, ⟨h.sigma_factor, h.sigma⟩ }, { rintro ⟨h₁, h₂⟩, simpa only [← ennreal.tsum_coe_ne_top_iff_summable, ennreal.tsum_sigma', ennreal.coe_tsum, h₁] using h₂ } end open finset /-- For `f : ℕ → ℝ≥0`, then `∑' k, f (k + i)` tends to zero. This does not require a summability assumption on `f`, as otherwise all sums are zero. -/ lemma tendsto_sum_nat_add (f : ℕ → ℝ≥0) : tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) := begin rw ← tendsto_coe, convert tendsto_sum_nat_add (λ i, (f i : ℝ)), norm_cast, end end nnreal namespace ennreal lemma tendsto_sum_nat_add (f : ℕ → ennreal) (hf : ∑' i, f i ≠ ∞) : tendsto (λ i, ∑' k, f (k + i)) at_top (𝓝 0) := begin have : ∀ i, ∑' k, (((ennreal.to_nnreal ∘ f) (k + i) : ℝ≥0) : ennreal) = (∑' k, (ennreal.to_nnreal ∘ f) (k + i) : ℝ≥0) := λ i, (ennreal.coe_tsum (nnreal.summable_nat_add _ (summable_to_nnreal_of_tsum_ne_top hf) _)).symm, simp only [λ x, (to_nnreal_apply_of_tsum_ne_top hf x).symm, ←ennreal.coe_zero, this, ennreal.tendsto_coe] { single_pass := tt }, exact nnreal.tendsto_sum_nat_add _ end end ennreal lemma tsum_comp_le_tsum_of_inj {β : Type*} {f : α → ℝ} (hf : summable f) (hn : ∀ a, 0 ≤ f a) {i : β → α} (hi : function.injective i) : tsum (f ∘ i) ≤ tsum f := begin let g : α → ℝ≥0 := λ a, ⟨f a, hn a⟩, have hg : summable g, by rwa ← nnreal.summable_coe, convert nnreal.coe_le_coe.2 (nnreal.tsum_comp_le_tsum_of_inj hg hi); { rw nnreal.coe_tsum, congr } end /-- Comparison test of convergence of series of non-negative real numbers. -/ lemma summable_of_nonneg_of_le {f g : β → ℝ} (hg : ∀b, 0 ≤ g b) (hgf : ∀b, g b ≤ f b) (hf : summable f) : summable g := let f' (b : β) : ℝ≥0 := ⟨f b, le_trans (hg b) (hgf b)⟩ in let g' (b : β) : ℝ≥0 := ⟨g b, hg b⟩ in have summable f', from nnreal.summable_coe.1 hf, have summable g', from nnreal.summable_of_le (assume b, (@nnreal.coe_le_coe (g' b) (f' b)).2 $ hgf b) this, show summable (λb, g' b : β → ℝ), from nnreal.summable_coe.2 this /-- A series of non-negative real numbers converges to `r` in the sense of `has_sum` if and only if the sequence of partial sum converges to `r`. -/ lemma has_sum_iff_tendsto_nat_of_nonneg {f : ℕ → ℝ} (hf : ∀i, 0 ≤ f i) (r : ℝ) : has_sum f r ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top (𝓝 r) := begin lift f to ℕ → ℝ≥0 using hf, simp only [has_sum, ← nnreal.coe_sum, nnreal.tendsto_coe'], exact exists_congr (λ hr, nnreal.has_sum_iff_tendsto_nat) end lemma ennreal.of_real_tsum_of_nonneg {f : α → ℝ} (hf_nonneg : ∀ n, 0 ≤ f n) (hf : summable f) : ennreal.of_real (∑' n, f n) = ∑' n, ennreal.of_real (f n) := by simp_rw [ennreal.of_real, ennreal.tsum_coe_eq (nnreal.has_sum_of_real_of_nonneg hf_nonneg hf)] lemma not_summable_iff_tendsto_nat_at_top_of_nonneg {f : ℕ → ℝ} (hf : ∀ n, 0 ≤ f n) : ¬ summable f ↔ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top := begin lift f to ℕ → ℝ≥0 using hf, exact_mod_cast nnreal.not_summable_iff_tendsto_nat_at_top end lemma summable_iff_not_tendsto_nat_at_top_of_nonneg {f : ℕ → ℝ} (hf : ∀ n, 0 ≤ f n) : summable f ↔ ¬ tendsto (λ n : ℕ, ∑ i in finset.range n, f i) at_top at_top := by rw [← not_iff_not, not_not, not_summable_iff_tendsto_nat_at_top_of_nonneg hf] lemma summable_sigma_of_nonneg {β : Π x : α, Type*} {f : (Σ x, β x) → ℝ} (hf : ∀ x, 0 ≤ f x) : summable f ↔ (∀ x, summable (λ y, f ⟨x, y⟩)) ∧ summable (λ x, ∑' y, f ⟨x, y⟩) := by { lift f to (Σ x, β x) → ℝ≥0 using hf, exact_mod_cast nnreal.summable_sigma } lemma summable_of_sum_range_le {f : ℕ → ℝ} {c : ℝ} (hf : ∀ n, 0 ≤ f n) (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : summable f := begin apply (summable_iff_not_tendsto_nat_at_top_of_nonneg hf).2 (λ H, _), rcases exists_lt_of_tendsto_at_top H 0 c with ⟨n, -, hn⟩, exact lt_irrefl _ (hn.trans_le (h n)), end lemma tsum_le_of_sum_range_le {f : ℕ → ℝ} {c : ℝ} (hf : ∀ n, 0 ≤ f n) (h : ∀ n, ∑ i in finset.range n, f i ≤ c) : ∑' n, f n ≤ c := le_of_tendsto' ((has_sum_iff_tendsto_nat_of_nonneg hf _).1 (summable_of_sum_range_le hf h).has_sum) h section variables [emetric_space β] open ennreal filter emetric /-- In an emetric ball, the distance between points is everywhere finite -/ lemma edist_ne_top_of_mem_ball {a : β} {r : ennreal} (x y : ball a r) : edist x.1 y.1 ≠ ⊤ := lt_top_iff_ne_top.1 $ calc edist x y ≤ edist a x + edist a y : edist_triangle_left x.1 y.1 a ... < r + r : by rw [edist_comm a x, edist_comm a y]; exact add_lt_add x.2 y.2 ... ≤ ⊤ : le_top /-- Each ball in an extended metric space gives us a metric space, as the edist is everywhere finite. -/ def metric_space_emetric_ball (a : β) (r : ennreal) : metric_space (ball a r) := emetric_space.to_metric_space edist_ne_top_of_mem_ball local attribute [instance] metric_space_emetric_ball lemma nhds_eq_nhds_emetric_ball (a x : β) (r : ennreal) (h : x ∈ ball a r) : 𝓝 x = map (coe : ball a r → β) (𝓝 ⟨x, h⟩) := (map_nhds_subtype_coe_eq _ $ mem_nhds_sets emetric.is_open_ball h).symm end section variable [emetric_space α] open emetric lemma tendsto_iff_edist_tendsto_0 {l : filter β} {f : β → α} {y : α} : tendsto f l (𝓝 y) ↔ tendsto (λ x, edist (f x) y) l (𝓝 0) := by simp only [emetric.nhds_basis_eball.tendsto_right_iff, emetric.mem_ball, @tendsto_order ennreal β _ _, forall_prop_of_false ennreal.not_lt_zero, forall_const, true_and] /-- Yet another metric characterization of Cauchy sequences on integers. This one is often the most efficient. -/ lemma emetric.cauchy_seq_iff_le_tendsto_0 [nonempty β] [semilattice_sup β] {s : β → α} : cauchy_seq s ↔ (∃ (b: β → ennreal), (∀ n m N : β, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N) ∧ (tendsto b at_top (𝓝 0))) := ⟨begin assume hs, rw emetric.cauchy_seq_iff at hs, /- `s` is Cauchy sequence. The sequence `b` will be constructed by taking the supremum of the distances between `s n` and `s m` for `n m ≥ N`-/ let b := λN, Sup ((λ(p : β × β), edist (s p.1) (s p.2))''{p | p.1 ≥ N ∧ p.2 ≥ N}), --Prove that it bounds the distances of points in the Cauchy sequence have C : ∀ n m N, N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N, { refine λm n N hm hn, le_Sup _, use (prod.mk m n), simp only [and_true, eq_self_iff_true, set.mem_set_of_eq], exact ⟨hm, hn⟩ }, --Prove that it tends to `0`, by using the Cauchy property of `s` have D : tendsto b at_top (𝓝 0), { refine tendsto_order.2 ⟨λa ha, absurd ha (ennreal.not_lt_zero), λε εpos, _⟩, rcases exists_between εpos with ⟨δ, δpos, δlt⟩, rcases hs δ δpos with ⟨N, hN⟩, refine filter.mem_at_top_sets.2 ⟨N, λn hn, _⟩, have : b n ≤ δ := Sup_le begin simp only [and_imp, set.mem_image, set.mem_set_of_eq, exists_imp_distrib, prod.exists], intros d p q hp hq hd, rw ← hd, exact le_of_lt (hN p q (le_trans hn hp) (le_trans hn hq)) end, simpa using lt_of_le_of_lt this δlt }, -- Conclude exact ⟨b, ⟨C, D⟩⟩ end, begin rintros ⟨b, ⟨b_bound, b_lim⟩⟩, /-b : ℕ → ℝ, b_bound : ∀ (n m N : ℕ), N ≤ n → N ≤ m → edist (s n) (s m) ≤ b N, b_lim : tendsto b at_top (𝓝 0)-/ refine emetric.cauchy_seq_iff.2 (λε εpos, _), have : ∀ᶠ n in at_top, b n < ε := (tendsto_order.1 b_lim ).2 _ εpos, rcases filter.mem_at_top_sets.1 this with ⟨N, hN⟩, exact ⟨N, λm n hm hn, calc edist (s m) (s n) ≤ b N : b_bound m n N hm hn ... < ε : (hN _ (le_refl N)) ⟩ end⟩ lemma continuous_of_le_add_edist {f : α → ennreal} (C : ennreal) (hC : C ≠ ⊤) (h : ∀x y, f x ≤ f y + C * edist x y) : continuous f := begin refine continuous_iff_continuous_at.2 (λx, tendsto_order.2 ⟨_, _⟩), show ∀e, e < f x → ∀ᶠ y in 𝓝 x, e < f y, { assume e he, let ε := min (f x - e) 1, have : ε < ⊤ := lt_of_le_of_lt (min_le_right _ _) (by simp [lt_top_iff_ne_top]), have : 0 < ε := by simp [ε, hC, he, ennreal.zero_lt_one], have : 0 < C⁻¹ * (ε/2) := bot_lt_iff_ne_bot.2 (by simp [hC, (ne_of_lt this).symm, mul_eq_zero]), have I : C * (C⁻¹ * (ε/2)) < ε, { by_cases C_zero : C = 0, { simp [C_zero, ‹0 < ε›] }, { calc C * (C⁻¹ * (ε/2)) = (C * C⁻¹) * (ε/2) : by simp [mul_assoc] ... = ε/2 : by simp [ennreal.mul_inv_cancel C_zero hC] ... < ε : ennreal.half_lt_self (‹0 < ε›.ne') (‹ε < ⊤›.ne) }}, have : ball x (C⁻¹ * (ε/2)) ⊆ {y : α | e < f y}, { rintros y hy, by_cases htop : f y = ⊤, { simp [htop, lt_top_iff_ne_top, ne_top_of_lt he] }, { simp at hy, have : e + ε < f y + ε := calc e + ε ≤ e + (f x - e) : add_le_add_left (min_le_left _ _) _ ... = f x : by simp [le_of_lt he] ... ≤ f y + C * edist x y : h x y ... = f y + C * edist y x : by simp [edist_comm] ... ≤ f y + C * (C⁻¹ * (ε/2)) : add_le_add_left (canonically_ordered_semiring.mul_le_mul (le_refl _) (le_of_lt hy)) _ ... < f y + ε : (ennreal.add_lt_add_iff_left (lt_top_iff_ne_top.2 htop)).2 I, show e < f y, from (ennreal.add_lt_add_iff_right ‹ε < ⊤›).1 this }}, apply filter.mem_sets_of_superset (ball_mem_nhds _ (‹0 < C⁻¹ * (ε/2)›)) this }, show ∀e, f x < e → ∀ᶠ y in 𝓝 x, f y < e, { assume e he, let ε := min (e - f x) 1, have : ε < ⊤ := lt_of_le_of_lt (min_le_right _ _) (by simp [lt_top_iff_ne_top]), have : 0 < ε := by simp [ε, he, ennreal.zero_lt_one], have : 0 < C⁻¹ * (ε/2) := bot_lt_iff_ne_bot.2 (by simp [hC, (ne_of_lt this).symm, mul_eq_zero]), have I : C * (C⁻¹ * (ε/2)) < ε, { by_cases C_zero : C = 0, simp [C_zero, ‹0 < ε›], calc C * (C⁻¹ * (ε/2)) = (C * C⁻¹) * (ε/2) : by simp [mul_assoc] ... = ε/2 : by simp [ennreal.mul_inv_cancel C_zero hC] ... < ε : ennreal.half_lt_self (‹0 < ε›.ne') (‹ε < ⊤›.ne) }, have : ball x (C⁻¹ * (ε/2)) ⊆ {y : α | f y < e}, { rintros y hy, have htop : f x ≠ ⊤ := ne_top_of_lt he, show f y < e, from calc f y ≤ f x + C * edist y x : h y x ... ≤ f x + C * (C⁻¹ * (ε/2)) : add_le_add_left (canonically_ordered_semiring.mul_le_mul (le_refl _) (le_of_lt hy)) _ ... < f x + ε : (ennreal.add_lt_add_iff_left (lt_top_iff_ne_top.2 htop)).2 I ... ≤ f x + (e - f x) : add_le_add_left (min_le_left _ _) _ ... = e : by simp [le_of_lt he] }, apply filter.mem_sets_of_superset (ball_mem_nhds _ (‹0 < C⁻¹ * (ε/2)›)) this }, end theorem continuous_edist : continuous (λp:α×α, edist p.1 p.2) := begin apply continuous_of_le_add_edist 2 (by norm_num), rintros ⟨x, y⟩ ⟨x', y'⟩, calc edist x y ≤ edist x x' + edist x' y' + edist y' y : edist_triangle4 _ _ _ _ ... = edist x' y' + (edist x x' + edist y y') : by simp [edist_comm]; cc ... ≤ edist x' y' + (edist (x, y) (x', y') + edist (x, y) (x', y')) : add_le_add_left (add_le_add (le_max_left _ _) (le_max_right _ _)) _ ... = edist x' y' + 2 * edist (x, y) (x', y') : by rw [← mul_two, mul_comm] end theorem continuous.edist [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : continuous (λb, edist (f b) (g b)) := continuous_edist.comp (hf.prod_mk hg : _) theorem filter.tendsto.edist {f g : β → α} {x : filter β} {a b : α} (hf : tendsto f x (𝓝 a)) (hg : tendsto g x (𝓝 b)) : tendsto (λx, edist (f x) (g x)) x (𝓝 (edist a b)) := (continuous_edist.tendsto (a, b)).comp (hf.prod_mk_nhds hg) lemma cauchy_seq_of_edist_le_of_tsum_ne_top {f : ℕ → α} (d : ℕ → ennreal) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) (hd : tsum d ≠ ∞) : cauchy_seq f := begin lift d to (ℕ → nnreal) using (λ i, ennreal.ne_top_of_tsum_ne_top hd i), rw ennreal.tsum_coe_ne_top_iff_summable at hd, exact cauchy_seq_of_edist_le_of_summable d hf hd end lemma emetric.is_closed_ball {a : α} {r : ennreal} : is_closed (closed_ball a r) := is_closed_le (continuous_id.edist continuous_const) continuous_const /-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ennreal`, then the distance from `f n` to the limit is bounded by `∑'_{k=n}^∞ d k`. -/ lemma edist_le_tsum_of_edist_le_of_tendsto {f : ℕ → α} (d : ℕ → ennreal) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) {a : α} (ha : tendsto f at_top (𝓝 a)) (n : ℕ) : edist (f n) a ≤ ∑' m, d (n + m) := begin refine le_of_tendsto (tendsto_const_nhds.edist ha) (mem_at_top_sets.2 ⟨n, λ m hnm, _⟩), refine le_trans (edist_le_Ico_sum_of_edist_le hnm (λ k _ _, hf k)) _, rw [finset.sum_Ico_eq_sum_range], exact sum_le_tsum _ (λ _ _, zero_le _) ennreal.summable end /-- If `edist (f n) (f (n+1))` is bounded above by a function `d : ℕ → ennreal`, then the distance from `f 0` to the limit is bounded by `∑'_{k=0}^∞ d k`. -/ lemma edist_le_tsum_of_edist_le_of_tendsto₀ {f : ℕ → α} (d : ℕ → ennreal) (hf : ∀ n, edist (f n) (f n.succ) ≤ d n) {a : α} (ha : tendsto f at_top (𝓝 a)) : edist (f 0) a ≤ ∑' m, d m := by simpa using edist_le_tsum_of_edist_le_of_tendsto d hf ha 0 end --section
c6d80571343ef4ddc84a2f9c2cdf47b1cb1b8890
8461211c55a0962f1c8b2e7537d535b4c68194a2
/theorem_proving_in_lean/props_and_proofs.lean
4fc5349fa28ccac9c62839226df2ca626f880dab
[]
no_license
alanhdu/lean-proofs
ba687a3d289c58cce9cf80a66f55bed2cdf4bdfb
a02cb9d0d2b6a6457f35247b89253d727f641531
refs/heads/master
1,598,769,649,392
1,575,379,585,000
1,575,379,585,000
218,293,755
1
0
null
null
null
null
UTF-8
Lean
false
false
5,565
lean
variables p q r s : Prop -- commutativity of ∧ and ∨ example : p ∧ q ↔ q ∧ p := iff.intro ( assume h : p ∧ q, show q ∧ p, from ⟨h.right, h.left⟩ ) ( assume h : q ∧ p, show p ∧ q, from ⟨h.right, h.left⟩ ) example : p ∨ q ↔ q ∨ p := iff.intro ( assume h: p ∨ q, show q ∨ p, from h.elim or.inr or.inl ) ( λ (h : q ∨ p), h.elim (assume hq: q, show p ∨ q, from or.inr hq) (assume hp: p, show p ∨ q, from or.inl hp) ) -- associativity of ∧ and ∨ example : (p ∧ q) ∧ r ↔ p ∧ (q ∧ r) := iff.intro ( assume h: (p ∧ q) ∧ r, show p ∧ (q ∧ r), from ⟨ h.left.left, ⟨h.left.right , h.right⟩ ⟩ ) ( assume h: p ∧ (q ∧ r), show (p ∧ q) ∧ r, from ⟨⟨h.left, h.right.left⟩ , h.right.right⟩ ) example : (p ∨ q) ∨ r ↔ p ∨ (q ∨ r) := iff.intro ( assume h: (p ∨ q) ∨ r, show p ∨ (q ∨ r), from h.elim (λ (hpq: p ∨ q), hpq.elim or.inl (λ hq: q, or.inr (or.inl hq))) (λ (hr: r), or.inr (or.inr hr)) ) ( assume h: p ∨ (q ∨ r), show (p ∨ q) ∨ r, from h.elim (assume hp: p, or.inl (or.inl hp)) (assume hqr: q ∨ r, hqr.elim (assume hq: q, or.inl (or.inr hq)) or.inr ) ) -- distributivity example : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := iff.intro ( assume h: p ∧ (q ∨ r), h.right.elim (assume hq: q, or.inl ⟨h.left, hq⟩) (assume hr: r, or.inr ⟨h.left, hr⟩) ) ( assume h: (p ∧ q) ∨ (p ∧ r), h.elim (assume hpq: p ∧ q, ⟨hpq.left, or.inl hpq.right⟩) (assume hpr: p ∧ r, ⟨hpr.left, or.inr hpr.right⟩) ) example : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (p ∨ r) := iff.intro ( assume h: p ∨ (q ∧ r), h.elim (assume hp: p, ⟨ or.inl hp, or.inl hp ⟩) (assume hqr: q ∧ r, ⟨ or.inr hqr.left, or.inr hqr.right ⟩) ) ( assume h: (p ∨ q) ∧ (p ∨ r), h.left.elim or.inl (assume hq: q, h.right.elim or.inl (assume hr: r, or.inr ⟨hq, hr⟩ ) ) ) -- other properties example : (p → (q → r)) ↔ (p ∧ q → r) := iff.intro ( assume hpqr: p → (q → r), assume hpq: p ∧ q, (hpqr hpq.left) hpq.right ) ( assume hpqr: (p ∧ q) → r, assume hp: p, assume hq: q, hpqr (⟨ hp, hq ⟩) ) example : ((p ∨ q) → r) ↔ (p → r) ∧ (q → r) := iff.intro ( assume hpqr: (p ∨ q) → r, ⟨ assume hp: p, show r, from hpqr (or.inl hp), assume hq: q, show r, from hpqr (or.inr hq) ⟩ ) ( assume hpqr: (p → r) ∧ (q → r), assume hpq: p ∨ q, hpq.elim (assume hp: p, hpqr.left hp) (assume hq: q, hpqr.right hq) ) example : ¬(p ∨ q) ↔ ¬p ∧ ¬q := iff.intro ( assume npq: ¬(p ∨ q), ⟨ assume p, show false, from npq (or.inl p), assume q, show false, from npq (or.inr q), ⟩ ) ( assume npq: ¬p ∧ ¬q, assume hpq: p ∨ q, hpq.elim npq.left npq.right ) example : ¬p ∨ ¬q → ¬(p ∧ q) := assume hpq: ¬p ∨ ¬q, assume pq: p ∧ q, hpq.elim (assume np: ¬p, np pq.left) (assume nq: ¬q, nq pq.right) example : ¬(p ∧ ¬p) := assume hp: p ∧ ¬p, hp.right hp.left example : p ∧ ¬q → ¬(p → q) := assume npq: p ∧ ¬q, assume hpq: p → q, npq.right (hpq npq.left) example : ¬p → (p → q) := assume np: ¬p, assume hp: p, absurd hp np example : (¬p ∨ q) → (p → q) := assume hpq: ¬p ∨ q, assume hp: p, hpq.elim (assume np: ¬p, absurd hp np) (assume hq: q, hq) example : p ∨ false ↔ p := iff.intro ( assume hpn: p ∨ false, hpn.elim id false.elim ) ( or.inl ) example : p ∧ false ↔ false := iff.intro and.right false.elim example : ¬(p ↔ ¬p) := assume pnp: p ↔ ¬p, have pf: p → false, from ( assume hp: p, (pnp.mp hp) hp ), have hp: p, from pnp.mpr pf, pf hp example : (p → q) → (¬q → ¬p) := assume hpq: p → q, assume nq: ¬q, assume hp: p, nq (hpq hp) -- these require classical reasoning example : (p → r ∨ s) → ((p → r) ∨ (p → s)) := assume prs: p → r ∨ s, (classical.em r).elim (assume hr: r, or.inl (assume hp: p, hr)) (assume nr: ¬r, or.inr ( assume hp: p, have rs: r ∨ s, from prs hp, show s, from (rs.elim (assume hr: r, absurd hr nr) (assume hs, hs)) )) example : ¬(p ∧ q) → ¬p ∨ ¬q := assume npq: ¬(p ∧ q), (classical.em p).elim ( assume hp: p, have nq: ¬q, from (assume hq: q, npq ⟨hp, hq⟩), or.inr nq ) ( or.inl ) example : ¬(p → q) → p ∧ ¬q := assume npq: ¬(p → q), (classical.em p).elim ( assume hp: p, have nq: ¬q, from (assume hq: q, npq (λ _, hq)), and.intro hp nq ) ( assume np: ¬p, have pq: p → q, from (assume hp: p, absurd hp np), absurd pq npq ) example : (p → q) → (¬p ∨ q) := assume hpq: p → q, (classical.em p).elim ( assume hp: p, have hq: q, from hpq hp, or.inr hq ) ( or.inl ) example : (¬q → ¬p) → (p → q) := assume nqp: ¬q → ¬p, assume hp: p, classical.by_contradiction ( assume nq: ¬q, (nqp nq) hp ) example : p ∨ ¬p := classical.em p example : (((p → q) → p) → p) := assume pqp: (p → q) → p, classical.by_contradiction ( assume np: ¬p, have pq: (p → q), from (assume hp: p, absurd hp np), have hp: p, from pqp pq, np hp )
70ad4d5891afdf12e980e82c41590459e7e44e99
9dc8cecdf3c4634764a18254e94d43da07142918
/src/linear_algebra/linear_independent.lean
06dce2df430b63893a912afdfee6a6d1bf0bfac3
[ "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
57,474
lean
/- Copyright (c) 2020 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Alexander Bentkamp, Anne Baanen -/ import algebra.big_operators.fin import linear_algebra.finsupp import linear_algebra.prod import set_theory.cardinal.basic /-! # Linear independence This file defines linear independence in a module or vector space. It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. We define `linear_independent R v` as `ker (finsupp.total ι M R v) = ⊥`. Here `finsupp.total` is the linear map sending a function `f : ι →₀ R` with finite support to the linear combination of vectors from `v` with these coefficients. Then we prove that several other statements are equivalent to this one, including injectivity of `finsupp.total ι M R v` and some versions with explicitly written linear combinations. ## Main definitions All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or vector space and `ι : Type*` is an arbitrary indexing type. * `linear_independent R v` states that the elements of the family `v` are linearly independent. * `linear_independent.repr hv x` returns the linear combination representing `x : span R (range v)` on the linearly independent vectors `v`, given `hv : linear_independent R v` (using classical choice). `linear_independent.repr hv` is provided as a linear map. ## Main statements We prove several specialized tests for linear independence of families of vectors and of sets of vectors. * `fintype.linear_independent_iff`: if `ι` is a finite type, then any function `f : ι → R` has finite support, so we can reformulate the statement using `∑ i : ι, f i • v i` instead of a sum over an auxiliary `s : finset ι`; * `linear_independent_empty_type`: a family indexed by an empty type is linearly independent; * `linear_independent_unique_iff`: if `ι` is a singleton, then `linear_independent K v` is equivalent to `v default ≠ 0`; * linear_independent_option`, `linear_independent_sum`, `linear_independent_fin_cons`, `linear_independent_fin_succ`: type-specific tests for linear independence of families of vector fields; * `linear_independent_insert`, `linear_independent_union`, `linear_independent_pair`, `linear_independent_singleton`: linear independence tests for set operations. In many cases we additionally provide dot-style operations (e.g., `linear_independent.union`) to make the linear independence tests usable as `hv.insert ha` etc. We also prove that, when working over a division ring, any family of vectors includes a linear independent subfamily spanning the same subspace. ## Implementation notes We use families instead of sets because it allows us to say that two identical vectors are linearly dependent. If you want to use sets, use the family `(λ x, x : s → M)` given a set `s : set M`. The lemmas `linear_independent.to_subtype_range` and `linear_independent.of_subtype_range` connect those two worlds. ## Tags linearly dependent, linear dependence, linearly independent, linear independence -/ noncomputable theory open function set submodule open_locale classical big_operators cardinal universes u variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*} variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*} section module variables {v : ι → M} variables [semiring R] [add_comm_monoid M] [add_comm_monoid M'] [add_comm_monoid M''] variables [module R M] [module R M'] [module R M''] variables {a b : R} {x y : M} variables (R) (v) /-- `linear_independent R v` states the family of vectors `v` is linearly independent over `R`. -/ def linear_independent : Prop := (finsupp.total ι M R v).ker = ⊥ variables {R} {v} theorem linear_independent_iff : linear_independent R v ↔ ∀l, finsupp.total ι M R v l = 0 → l = 0 := by simp [linear_independent, linear_map.ker_eq_bot'] theorem linear_independent_iff' : linear_independent R v ↔ ∀ s : finset ι, ∀ g : ι → R, ∑ i in s, g i • v i = 0 → ∀ i ∈ s, g i = 0 := linear_independent_iff.trans ⟨λ hf s g hg i his, have h : _ := hf (∑ i in s, finsupp.single i (g i)) $ by simpa only [linear_map.map_sum, finsupp.total_single] using hg, calc g i = (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single i (g i)) : by rw [finsupp.lapply_apply, finsupp.single_eq_same] ... = ∑ j in s, (finsupp.lapply i : (ι →₀ R) →ₗ[R] R) (finsupp.single j (g j)) : eq.symm $ finset.sum_eq_single i (λ j hjs hji, by rw [finsupp.lapply_apply, finsupp.single_eq_of_ne hji]) (λ hnis, hnis.elim his) ... = (∑ j in s, finsupp.single j (g j)) i : (finsupp.lapply i : (ι →₀ R) →ₗ[R] R).map_sum.symm ... = 0 : finsupp.ext_iff.1 h i, λ hf l hl, finsupp.ext $ λ i, classical.by_contradiction $ λ hni, hni $ hf _ _ hl _ $ finsupp.mem_support_iff.2 hni⟩ theorem linear_independent_iff'' : linear_independent R v ↔ ∀ (s : finset ι) (g : ι → R) (hg : ∀ i ∉ s, g i = 0), ∑ i in s, g i • v i = 0 → ∀ i, g i = 0 := linear_independent_iff'.trans ⟨λ H s g hg hv i, if his : i ∈ s then H s g hv i his else hg i his, λ H s g hg i hi, by { convert H s (λ j, if j ∈ s then g j else 0) (λ j hj, if_neg hj) (by simp_rw [ite_smul, zero_smul, finset.sum_extend_by_zero, hg]) i, exact (if_pos hi).symm }⟩ theorem not_linear_independent_iff : ¬ linear_independent R v ↔ ∃ s : finset ι, ∃ g : ι → R, (∑ i in s, g i • v i) = 0 ∧ (∃ i ∈ s, g i ≠ 0) := begin rw linear_independent_iff', simp only [exists_prop, not_forall], end theorem fintype.linear_independent_iff [fintype ι] : linear_independent R v ↔ ∀ g : ι → R, ∑ i, g i • v i = 0 → ∀ i, g i = 0 := begin refine ⟨λ H g, by simpa using linear_independent_iff'.1 H finset.univ g, λ H, linear_independent_iff''.2 $ λ s g hg hs i, H _ _ _⟩, rw ← hs, refine (finset.sum_subset (finset.subset_univ _) (λ i _ hi, _)).symm, rw [hg i hi, zero_smul] end /-- A finite family of vectors `v i` is linear independent iff the linear map that sends `c : ι → R` to `∑ i, c i • v i` has the trivial kernel. -/ theorem fintype.linear_independent_iff' [fintype ι] : linear_independent R v ↔ (linear_map.lsum R (λ i : ι, R) ℕ (λ i, linear_map.id.smul_right (v i))).ker = ⊥ := by simp [fintype.linear_independent_iff, linear_map.ker_eq_bot', funext_iff]; skip lemma fintype.not_linear_independent_iff [fintype ι] : ¬linear_independent R v ↔ ∃ g : ι → R, (∑ i, g i • v i) = 0 ∧ (∃ i, g i ≠ 0) := by simpa using (not_iff_not.2 fintype.linear_independent_iff) lemma linear_independent_empty_type [is_empty ι] : linear_independent R v := linear_independent_iff.mpr $ λ v hv, subsingleton.elim v 0 lemma linear_independent.ne_zero [nontrivial R] (i : ι) (hv : linear_independent R v) : v i ≠ 0 := λ h, @zero_ne_one R _ _ $ eq.symm begin suffices : (finsupp.single i 1 : ι →₀ R) i = 0, {simpa}, rw linear_independent_iff.1 hv (finsupp.single i 1), { simp }, { simp [h] } end /-- A subfamily of a linearly independent family (i.e., a composition with an injective map) is a linearly independent family. -/ lemma linear_independent.comp (h : linear_independent R v) (f : ι' → ι) (hf : injective f) : linear_independent R (v ∘ f) := begin rw [linear_independent_iff, finsupp.total_comp], intros l hl, have h_map_domain : ∀ x, (finsupp.map_domain f l) (f x) = 0, by rw linear_independent_iff.1 h (finsupp.map_domain f l) hl; simp, ext x, convert h_map_domain x, rw [finsupp.map_domain_apply hf] end lemma linear_independent.coe_range (i : linear_independent R v) : linear_independent R (coe : range v → M) := by simpa using i.comp _ (range_splitting_injective v) /-- If `v` is a linearly independent family of vectors and the kernel of a linear map `f` is disjoint with the submodule spanned by the vectors of `v`, then `f ∘ v` is a linearly independent family of vectors. See also `linear_independent.map'` for a special case assuming `ker f = ⊥`. -/ lemma linear_independent.map (hv : linear_independent R v) {f : M →ₗ[R] M'} (hf_inj : disjoint (span R (range v)) f.ker) : linear_independent R (f ∘ v) := begin rw [disjoint, ← set.image_univ, finsupp.span_image_eq_map_total, map_inf_eq_map_inf_comap, map_le_iff_le_comap, comap_bot, finsupp.supported_univ, top_inf_eq] at hf_inj, unfold linear_independent at hv ⊢, rw [hv, le_bot_iff] at hf_inj, haveI : inhabited M := ⟨0⟩, rw [finsupp.total_comp, @finsupp.lmap_domain_total _ _ R _ _ _ _ _ _ _ _ _ _ f, linear_map.ker_comp, hf_inj], exact λ _, rfl, end /-- An injective linear map sends linearly independent families of vectors to linearly independent families of vectors. See also `linear_independent.map` for a more general statement. -/ lemma linear_independent.map' (hv : linear_independent R v) (f : M →ₗ[R] M') (hf_inj : f.ker = ⊥) : linear_independent R (f ∘ v) := hv.map $ by simp [hf_inj] /-- If the image of a family of vectors under a linear map is linearly independent, then so is the original family. -/ lemma linear_independent.of_comp (f : M →ₗ[R] M') (hfv : linear_independent R (f ∘ v)) : linear_independent R v := linear_independent_iff'.2 $ λ s g hg i his, have ∑ (i : ι) in s, g i • f (v i) = 0, by simp_rw [← f.map_smul, ← f.map_sum, hg, f.map_zero], linear_independent_iff'.1 hfv s g this i his /-- If `f` is an injective linear map, then the family `f ∘ v` is linearly independent if and only if the family `v` is linearly independent. -/ protected lemma linear_map.linear_independent_iff (f : M →ₗ[R] M') (hf_inj : f.ker = ⊥) : linear_independent R (f ∘ v) ↔ linear_independent R v := ⟨λ h, h.of_comp f, λ h, h.map $ by simp only [hf_inj, disjoint_bot_right]⟩ @[nontriviality] lemma linear_independent_of_subsingleton [subsingleton R] : linear_independent R v := linear_independent_iff.2 (λ l hl, subsingleton.elim _ _) theorem linear_independent_equiv (e : ι ≃ ι') {f : ι' → M} : linear_independent R (f ∘ e) ↔ linear_independent R f := ⟨λ h, function.comp.right_id f ▸ e.self_comp_symm ▸ h.comp _ e.symm.injective, λ h, h.comp _ e.injective⟩ theorem linear_independent_equiv' (e : ι ≃ ι') {f : ι' → M} {g : ι → M} (h : f ∘ e = g) : linear_independent R g ↔ linear_independent R f := h ▸ linear_independent_equiv e theorem linear_independent_subtype_range {ι} {f : ι → M} (hf : injective f) : linear_independent R (coe : range f → M) ↔ linear_independent R f := iff.symm $ linear_independent_equiv' (equiv.of_injective f hf) rfl alias linear_independent_subtype_range ↔ linear_independent.of_subtype_range _ theorem linear_independent_image {ι} {s : set ι} {f : ι → M} (hf : set.inj_on f s) : linear_independent R (λ x : s, f x) ↔ linear_independent R (λ x : f '' s, (x : M)) := linear_independent_equiv' (equiv.set.image_of_inj_on _ _ hf) rfl lemma linear_independent_span (hs : linear_independent R v) : @linear_independent ι R (span R (range v)) (λ i : ι, ⟨v i, subset_span (mem_range_self i)⟩) _ _ _ := linear_independent.of_comp (span R (range v)).subtype hs /-- See `linear_independent.fin_cons` for a family of elements in a vector space. -/ lemma linear_independent.fin_cons' {m : ℕ} (x : M) (v : fin m → M) (hli : linear_independent R v) (x_ortho : (∀ (c : R) (y : submodule.span R (set.range v)), c • x + y = (0 : M) → c = 0)) : linear_independent R (fin.cons x v : fin m.succ → M) := begin rw fintype.linear_independent_iff at hli ⊢, rintros g total_eq j, simp_rw [fin.sum_univ_succ, fin.cons_zero, fin.cons_succ] at total_eq, have : g 0 = 0, { refine x_ortho (g 0) ⟨∑ (i : fin m), g i.succ • v i, _⟩ total_eq, exact sum_mem (λ i _, smul_mem _ _ (subset_span ⟨i, rfl⟩)) }, rw [this, zero_smul, zero_add] at total_eq, exact fin.cases this (hli _ total_eq) j, end /-- A set of linearly independent vectors in a module `M` over a semiring `K` is also linearly independent over a subring `R` of `K`. The implementation uses minimal assumptions about the relationship between `R`, `K` and `M`. The version where `K` is an `R`-algebra is `linear_independent.restrict_scalars_algebras`. -/ lemma linear_independent.restrict_scalars [semiring K] [smul_with_zero R K] [module K M] [is_scalar_tower R K M] (hinj : function.injective (λ r : R, r • (1 : K))) (li : linear_independent K v) : linear_independent R v := begin refine linear_independent_iff'.mpr (λ s g hg i hi, hinj (eq.trans _ (zero_smul _ _).symm)), refine (linear_independent_iff'.mp li : _) _ _ _ i hi, simp_rw [smul_assoc, one_smul], exact hg, end /-- Every finite subset of a linearly independent set is linearly independent. -/ lemma linear_independent_finset_map_embedding_subtype (s : set M) (li : linear_independent R (coe : s → M)) (t : finset s) : linear_independent R (coe : (finset.map (embedding.subtype s) t) → M) := begin let f : t.map (embedding.subtype s) → s := λ x, ⟨x.1, begin obtain ⟨x, h⟩ := x, rw [finset.mem_map] at h, obtain ⟨a, ha, rfl⟩ := h, simp only [subtype.coe_prop, embedding.coe_subtype], end⟩, convert linear_independent.comp li f _, rintros ⟨x, hx⟩ ⟨y, hy⟩, rw [finset.mem_map] at hx hy, obtain ⟨a, ha, rfl⟩ := hx, obtain ⟨b, hb, rfl⟩ := hy, simp only [imp_self, subtype.mk_eq_mk], end /-- If every finite set of linearly independent vectors has cardinality at most `n`, then the same is true for arbitrary sets of linearly independent vectors. -/ lemma linear_independent_bounded_of_finset_linear_independent_bounded {n : ℕ} (H : ∀ s : finset M, linear_independent R (λ i : s, (i : M)) → s.card ≤ n) : ∀ s : set M, linear_independent R (coe : s → M) → #s ≤ n := begin intros s li, apply cardinal.card_le_of, intro t, rw ← finset.card_map (embedding.subtype s), apply H, apply linear_independent_finset_map_embedding_subtype _ li, end section subtype /-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/ theorem linear_independent_comp_subtype {s : set ι} : linear_independent R (v ∘ coe : s → M) ↔ ∀ l ∈ (finsupp.supported R R s), (finsupp.total ι M R v) l = 0 → l = 0 := begin simp only [linear_independent_iff, (∘), finsupp.mem_supported, finsupp.total_apply, set.subset_def, finset.mem_coe], split, { intros h l hl₁ hl₂, have := h (l.subtype_domain s) ((finsupp.sum_subtype_domain_index hl₁).trans hl₂), exact (finsupp.subtype_domain_eq_zero_iff hl₁).1 this }, { intros h l hl, refine finsupp.emb_domain_eq_zero.1 (h (l.emb_domain $ function.embedding.subtype s) _ _), { suffices : ∀ i hi, ¬l ⟨i, hi⟩ = 0 → i ∈ s, by simpa, intros, assumption }, { rwa [finsupp.emb_domain_eq_map_domain, finsupp.sum_map_domain_index], exacts [λ _, zero_smul _ _, λ _ _ _, add_smul _ _ _] } } end lemma linear_dependent_comp_subtype' {s : set ι} : ¬ linear_independent R (v ∘ coe : s → M) ↔ ∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ finsupp.total ι M R v f = 0 ∧ f ≠ 0 := by simp [linear_independent_comp_subtype] /-- A version of `linear_dependent_comp_subtype'` with `finsupp.total` unfolded. -/ lemma linear_dependent_comp_subtype {s : set ι} : ¬ linear_independent R (v ∘ coe : s → M) ↔ ∃ f : ι →₀ R, f ∈ finsupp.supported R R s ∧ ∑ i in f.support, f i • v i = 0 ∧ f ≠ 0 := linear_dependent_comp_subtype' theorem linear_independent_subtype {s : set M} : linear_independent R (λ x, x : s → M) ↔ ∀ l ∈ (finsupp.supported R R s), (finsupp.total M M R id) l = 0 → l = 0 := by apply @linear_independent_comp_subtype _ _ _ id theorem linear_independent_comp_subtype_disjoint {s : set ι} : linear_independent R (v ∘ coe : s → M) ↔ disjoint (finsupp.supported R R s) (finsupp.total ι M R v).ker := by rw [linear_independent_comp_subtype, linear_map.disjoint_ker] theorem linear_independent_subtype_disjoint {s : set M} : linear_independent R (λ x, x : s → M) ↔ disjoint (finsupp.supported R R s) (finsupp.total M M R id).ker := by apply @linear_independent_comp_subtype_disjoint _ _ _ id theorem linear_independent_iff_total_on {s : set M} : linear_independent R (λ x, x : s → M) ↔ (finsupp.total_on M M R id s).ker = ⊥ := by rw [finsupp.total_on, linear_map.ker, linear_map.comap_cod_restrict, map_bot, comap_bot, linear_map.ker_comp, linear_independent_subtype_disjoint, disjoint, ← map_comap_subtype, map_le_iff_le_comap, comap_bot, ker_subtype, le_bot_iff] lemma linear_independent.restrict_of_comp_subtype {s : set ι} (hs : linear_independent R (v ∘ coe : s → M)) : linear_independent R (s.restrict v) := hs variables (R M) lemma linear_independent_empty : linear_independent R (λ x, x : (∅ : set M) → M) := by simp [linear_independent_subtype_disjoint] variables {R M} lemma linear_independent.mono {t s : set M} (h : t ⊆ s) : linear_independent R (λ x, x : s → M) → linear_independent R (λ x, x : t → M) := begin simp only [linear_independent_subtype_disjoint], exact (disjoint.mono_left (finsupp.supported_mono h)) end lemma linear_independent_of_finite (s : set M) (H : ∀ t ⊆ s, set.finite t → linear_independent R (λ x, x : t → M)) : linear_independent R (λ x, x : s → M) := linear_independent_subtype.2 $ λ l hl, linear_independent_subtype.1 (H _ hl (finset.finite_to_set _)) l (subset.refl _) lemma linear_independent_Union_of_directed {η : Type*} {s : η → set M} (hs : directed (⊆) s) (h : ∀ i, linear_independent R (λ x, x : s i → M)) : linear_independent R (λ x, x : (⋃ i, s i) → M) := begin by_cases hη : nonempty η, { resetI, refine linear_independent_of_finite (⋃ i, s i) (λ t ht ft, _), rcases finite_subset_Union ft ht with ⟨I, fi, hI⟩, rcases hs.finset_le fi.to_finset with ⟨i, hi⟩, exact (h i).mono (subset.trans hI $ Union₂_subset $ λ j hj, hi j (fi.mem_to_finset.2 hj)) }, { refine (linear_independent_empty _ _).mono _, rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩ } end lemma linear_independent_sUnion_of_directed {s : set (set M)} (hs : directed_on (⊆) s) (h : ∀ a ∈ s, linear_independent R (λ x, x : (a : set M) → M)) : linear_independent R (λ x, x : (⋃₀ s) → M) := by rw sUnion_eq_Union; exact linear_independent_Union_of_directed hs.directed_coe (by simpa using h) lemma linear_independent_bUnion_of_directed {η} {s : set η} {t : η → set M} (hs : directed_on (t ⁻¹'o (⊆)) s) (h : ∀a∈s, linear_independent R (λ x, x : t a → M)) : linear_independent R (λ x, x : (⋃a∈s, t a) → M) := by rw bUnion_eq_Union; exact linear_independent_Union_of_directed (directed_comp.2 $ hs.directed_coe) (by simpa using h) end subtype end module /-! ### Properties which require `ring R` -/ section module variables {v : ι → M} variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M''] variables [module R M] [module R M'] [module R M''] variables {a b : R} {x y : M} theorem linear_independent_iff_injective_total : linear_independent R v ↔ function.injective (finsupp.total ι M R v) := linear_independent_iff.trans (injective_iff_map_eq_zero (finsupp.total ι M R v).to_add_monoid_hom).symm alias linear_independent_iff_injective_total ↔ linear_independent.injective_total _ lemma linear_independent.injective [nontrivial R] (hv : linear_independent R v) : injective v := begin intros i j hij, let l : ι →₀ R := finsupp.single i (1 : R) - finsupp.single j 1, have h_total : finsupp.total ι M R v l = 0, { simp_rw [linear_map.map_sub, finsupp.total_apply], simp [hij] }, have h_single_eq : finsupp.single i (1 : R) = finsupp.single j 1, { rw linear_independent_iff at hv, simp [eq_add_of_sub_eq' (hv l h_total)] }, simpa [finsupp.single_eq_single_iff] using h_single_eq end theorem linear_independent.to_subtype_range {ι} {f : ι → M} (hf : linear_independent R f) : linear_independent R (coe : range f → M) := begin nontriviality R, exact (linear_independent_subtype_range hf.injective).2 hf end theorem linear_independent.to_subtype_range' {ι} {f : ι → M} (hf : linear_independent R f) {t} (ht : range f = t) : linear_independent R (coe : t → M) := ht ▸ hf.to_subtype_range theorem linear_independent.image_of_comp {ι ι'} (s : set ι) (f : ι → ι') (g : ι' → M) (hs : linear_independent R (λ x : s, g (f x))) : linear_independent R (λ x : f '' s, g x) := begin nontriviality R, have : inj_on f s, from inj_on_iff_injective.2 hs.injective.of_comp, exact (linear_independent_equiv' (equiv.set.image_of_inj_on f s this) rfl).1 hs end theorem linear_independent.image {ι} {s : set ι} {f : ι → M} (hs : linear_independent R (λ x : s, f x)) : linear_independent R (λ x : f '' s, (x : M)) := by convert linear_independent.image_of_comp s f id hs lemma linear_independent.group_smul {G : Type*} [hG : group G] [distrib_mul_action G R] [distrib_mul_action G M] [is_scalar_tower G R M] [smul_comm_class G R M] {v : ι → M} (hv : linear_independent R v) (w : ι → G) : linear_independent R (w • v) := begin rw linear_independent_iff'' at hv ⊢, intros s g hgs hsum i, refine (smul_eq_zero_iff_eq (w i)).1 _, refine hv s (λ i, w i • g i) (λ i hi, _) _ i, { dsimp only, exact (hgs i hi).symm ▸ smul_zero _ }, { rw [← hsum, finset.sum_congr rfl _], intros, erw [pi.smul_apply, smul_assoc, smul_comm] }, end -- This lemma cannot be proved with `linear_independent.group_smul` since the action of -- `Rˣ` on `R` is not commutative. lemma linear_independent.units_smul {v : ι → M} (hv : linear_independent R v) (w : ι → Rˣ) : linear_independent R (w • v) := begin rw linear_independent_iff'' at hv ⊢, intros s g hgs hsum i, rw ← (w i).mul_left_eq_zero, refine hv s (λ i, g i • w i) (λ i hi, _) _ i, { dsimp only, exact (hgs i hi).symm ▸ zero_smul _ _ }, { rw [← hsum, finset.sum_congr rfl _], intros, erw [pi.smul_apply, smul_assoc], refl } end section maximal universes v w /-- A linearly independent family is maximal if there is no strictly larger linearly independent family. -/ @[nolint unused_arguments] def linear_independent.maximal {ι : Type w} {R : Type u} [semiring R] {M : Type v} [add_comm_monoid M] [module R M] {v : ι → M} (i : linear_independent R v) : Prop := ∀ (s : set M) (i' : linear_independent R (coe : s → M)) (h : range v ≤ s), range v = s /-- An alternative characterization of a maximal linearly independent family, quantifying over types (in the same universe as `M`) into which the indexing family injects. -/ lemma linear_independent.maximal_iff {ι : Type w} {R : Type u} [ring R] [nontrivial R] {M : Type v} [add_comm_group M] [module R M] {v : ι → M} (i : linear_independent R v) : i.maximal ↔ ∀ (κ : Type v) (w : κ → M) (i' : linear_independent R w) (j : ι → κ) (h : w ∘ j = v), surjective j := begin fsplit, { rintros p κ w i' j rfl, specialize p (range w) i'.coe_range (range_comp_subset_range _ _), rw [range_comp, ←@image_univ _ _ w] at p, exact range_iff_surjective.mp (image_injective.mpr i'.injective p), }, { intros p w i' h, specialize p w (coe : w → M) i' (λ i, ⟨v i, range_subset_iff.mp h i⟩) (by { ext, simp, }), have q := congr_arg (λ s, (coe : w → M) '' s) p.range_eq, dsimp at q, rw [←image_univ, image_image] at q, simpa using q, }, end end maximal /-- Linear independent families are injective, even if you multiply either side. -/ lemma linear_independent.eq_of_smul_apply_eq_smul_apply {M : Type*} [add_comm_group M] [module R M] {v : ι → M} (li : linear_independent R v) (c d : R) (i j : ι) (hc : c ≠ 0) (h : c • v i = d • v j) : i = j := begin let l : ι →₀ R := finsupp.single i c - finsupp.single j d, have h_total : finsupp.total ι M R v l = 0, { simp_rw [linear_map.map_sub, finsupp.total_apply], simp [h] }, have h_single_eq : finsupp.single i c = finsupp.single j d, { rw linear_independent_iff at li, simp [eq_add_of_sub_eq' (li l h_total)] }, rcases (finsupp.single_eq_single_iff _ _ _ _).mp h_single_eq with ⟨this, _⟩ | ⟨hc, _⟩, { exact this }, { contradiction }, end section subtype /-! The following lemmas use the subtype defined by a set in `M` as the index set `ι`. -/ lemma linear_independent.disjoint_span_image (hv : linear_independent R v) {s t : set ι} (hs : disjoint s t) : disjoint (submodule.span R $ v '' s) (submodule.span R $ v '' t) := begin simp only [disjoint_def, finsupp.mem_span_image_iff_total], rintros _ ⟨l₁, hl₁, rfl⟩ ⟨l₂, hl₂, H⟩, rw [hv.injective_total.eq_iff] at H, subst l₂, have : l₁ = 0 := finsupp.disjoint_supported_supported hs (submodule.mem_inf.2 ⟨hl₁, hl₂⟩), simp [this] end lemma linear_independent.not_mem_span_image [nontrivial R] (hv : linear_independent R v) {s : set ι} {x : ι} (h : x ∉ s) : v x ∉ submodule.span R (v '' s) := begin have h' : v x ∈ submodule.span R (v '' {x}), { rw set.image_singleton, exact mem_span_singleton_self (v x), }, intro w, apply linear_independent.ne_zero x hv, refine disjoint_def.1 (hv.disjoint_span_image _) (v x) h' w, simpa using h, end lemma linear_independent.total_ne_of_not_mem_support [nontrivial R] (hv : linear_independent R v) {x : ι} (f : ι →₀ R) (h : x ∉ f.support) : finsupp.total ι M R v f ≠ v x := begin replace h : x ∉ (f.support : set ι) := h, have p := hv.not_mem_span_image h, intro w, rw ←w at p, rw finsupp.span_image_eq_map_total at p, simp only [not_exists, not_and, mem_map] at p, exact p f (f.mem_supported_support R) rfl, end lemma linear_independent_sum {v : ι ⊕ ι' → M} : linear_independent R v ↔ linear_independent R (v ∘ sum.inl) ∧ linear_independent R (v ∘ sum.inr) ∧ disjoint (submodule.span R (range (v ∘ sum.inl))) (submodule.span R (range (v ∘ sum.inr))) := begin rw [range_comp v, range_comp v], refine ⟨λ h, ⟨h.comp _ sum.inl_injective, h.comp _ sum.inr_injective, h.disjoint_span_image is_compl_range_inl_range_inr.1⟩, _⟩, rintro ⟨hl, hr, hlr⟩, rw [linear_independent_iff'] at *, intros s g hg i hi, have : ∑ i in s.preimage sum.inl (sum.inl_injective.inj_on _), (λ x, g x • v x) (sum.inl i) + ∑ i in s.preimage sum.inr (sum.inr_injective.inj_on _), (λ x, g x • v x) (sum.inr i) = 0, { rw [finset.sum_preimage', finset.sum_preimage', ← finset.sum_union, ← finset.filter_or], { simpa only [← mem_union, range_inl_union_range_inr, mem_univ, finset.filter_true] }, { exact finset.disjoint_filter.2 (λ x _ hx, disjoint_left.1 is_compl_range_inl_range_inr.1 hx) } }, { rw ← eq_neg_iff_add_eq_zero at this, rw [disjoint_def'] at hlr, have A := hlr _ (sum_mem $ λ i hi, _) _ (neg_mem $ sum_mem $ λ i hi, _) this, { cases i with i i, { exact hl _ _ A i (finset.mem_preimage.2 hi) }, { rw [this, neg_eq_zero] at A, exact hr _ _ A i (finset.mem_preimage.2 hi) } }, { exact smul_mem _ _ (subset_span ⟨sum.inl i, mem_range_self _, rfl⟩) }, { exact smul_mem _ _ (subset_span ⟨sum.inr i, mem_range_self _, rfl⟩) } } end lemma linear_independent.sum_type {v' : ι' → M} (hv : linear_independent R v) (hv' : linear_independent R v') (h : disjoint (submodule.span R (range v)) (submodule.span R (range v'))) : linear_independent R (sum.elim v v') := linear_independent_sum.2 ⟨hv, hv', h⟩ lemma linear_independent.union {s t : set M} (hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M)) (hst : disjoint (span R s) (span R t)) : linear_independent R (λ x, x : (s ∪ t) → M) := (hs.sum_type ht $ by simpa).to_subtype_range' $ by simp lemma linear_independent_Union_finite_subtype {ι : Type*} {f : ι → set M} (hl : ∀i, linear_independent R (λ x, x : f i → M)) (hd : ∀i, ∀t:set ι, t.finite → i ∉ t → disjoint (span R (f i)) (⨆i∈t, span R (f i))) : linear_independent R (λ x, x : (⋃i, f i) → M) := begin rw [Union_eq_Union_finset f], apply linear_independent_Union_of_directed, { apply directed_of_sup, exact (λ t₁ t₂ ht, Union_mono $ λ i, Union_subset_Union_const $ λ h, ht h) }, assume t, induction t using finset.induction_on with i s his ih, { refine (linear_independent_empty _ _).mono _, simp }, { rw [finset.set_bUnion_insert], refine (hl _).union ih _, rw span_Union₂, exact hd i s s.finite_to_set his } end lemma linear_independent_Union_finite {η : Type*} {ιs : η → Type*} {f : Π j : η, ιs j → M} (hindep : ∀j, linear_independent R (f j)) (hd : ∀i, ∀t:set η, t.finite → i ∉ t → disjoint (span R (range (f i))) (⨆i∈t, span R (range (f i)))) : linear_independent R (λ ji : Σ j, ιs j, f ji.1 ji.2) := begin nontriviality R, apply linear_independent.of_subtype_range, { rintros ⟨x₁, x₂⟩ ⟨y₁, y₂⟩ hxy, by_cases h_cases : x₁ = y₁, subst h_cases, { apply sigma.eq, rw linear_independent.injective (hindep _) hxy, refl }, { have h0 : f x₁ x₂ = 0, { apply disjoint_def.1 (hd x₁ {y₁} (finite_singleton y₁) (λ h, h_cases (eq_of_mem_singleton h))) (f x₁ x₂) (subset_span (mem_range_self _)), rw supr_singleton, simp only at hxy, rw hxy, exact (subset_span (mem_range_self y₂)) }, exact false.elim ((hindep x₁).ne_zero _ h0) } }, rw range_sigma_eq_Union_range, apply linear_independent_Union_finite_subtype (λ j, (hindep j).to_subtype_range) hd, end end subtype section repr variables (hv : linear_independent R v) /-- Canonical isomorphism between linear combinations and the span of linearly independent vectors. -/ @[simps {rhs_md := semireducible}] def linear_independent.total_equiv (hv : linear_independent R v) : (ι →₀ R) ≃ₗ[R] span R (range v) := begin apply linear_equiv.of_bijective (linear_map.cod_restrict (span R (range v)) (finsupp.total ι M R v) _), { rw [← linear_map.ker_eq_bot, linear_map.ker_cod_restrict], apply hv }, { rw [← linear_map.range_eq_top, linear_map.range_eq_map, linear_map.map_cod_restrict, ← linear_map.range_le_iff_comap, range_subtype, map_top], rw finsupp.range_total, exact le_rfl }, { intro l, rw ← finsupp.range_total, rw linear_map.mem_range, apply mem_range_self l } end /-- Linear combination representing a vector in the span of linearly independent vectors. Given a family of linearly independent vectors, we can represent any vector in their span as a linear combination of these vectors. These are provided by this linear map. It is simply one direction of `linear_independent.total_equiv`. -/ def linear_independent.repr (hv : linear_independent R v) : span R (range v) →ₗ[R] ι →₀ R := hv.total_equiv.symm @[simp] lemma linear_independent.total_repr (x) : finsupp.total ι M R v (hv.repr x) = x := subtype.ext_iff.1 (linear_equiv.apply_symm_apply hv.total_equiv x) lemma linear_independent.total_comp_repr : (finsupp.total ι M R v).comp hv.repr = submodule.subtype _ := linear_map.ext $ hv.total_repr lemma linear_independent.repr_ker : hv.repr.ker = ⊥ := by rw [linear_independent.repr, linear_equiv.ker] lemma linear_independent.repr_range : hv.repr.range = ⊤ := by rw [linear_independent.repr, linear_equiv.range] lemma linear_independent.repr_eq {l : ι →₀ R} {x} (eq : finsupp.total ι M R v l = ↑x) : hv.repr x = l := begin have : ↑((linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l) = finsupp.total ι M R v l := rfl, have : (linear_independent.total_equiv hv : (ι →₀ R) →ₗ[R] span R (range v)) l = x, { rw eq at this, exact subtype.ext_iff.2 this }, rw ←linear_equiv.symm_apply_apply hv.total_equiv l, rw ←this, refl, end lemma linear_independent.repr_eq_single (i) (x) (hx : ↑x = v i) : hv.repr x = finsupp.single i 1 := begin apply hv.repr_eq, simp [finsupp.total_single, hx] end lemma linear_independent.span_repr_eq [nontrivial R] (x) : span.repr R (set.range v) x = (hv.repr x).equiv_map_domain (equiv.of_injective _ hv.injective) := begin have p : (span.repr R (set.range v) x).equiv_map_domain (equiv.of_injective _ hv.injective).symm = hv.repr x, { apply (linear_independent.total_equiv hv).injective, ext, simp only [linear_independent.total_equiv_apply_coe, equiv.self_comp_of_injective_symm, linear_independent.total_repr, finsupp.total_equiv_map_domain, span.finsupp_total_repr], }, ext ⟨_, ⟨i, rfl⟩⟩, simp [←p], end -- TODO: why is this so slow? lemma linear_independent_iff_not_smul_mem_span : linear_independent R v ↔ (∀ (i : ι) (a : R), a • (v i) ∈ span R (v '' (univ \ {i})) → a = 0) := ⟨ λ hv i a ha, begin rw [finsupp.span_image_eq_map_total, mem_map] at ha, rcases ha with ⟨l, hl, e⟩, rw sub_eq_zero.1 (linear_independent_iff.1 hv (l - finsupp.single i a) (by simp [e])) at hl, by_contra hn, exact (not_mem_of_mem_diff (hl $ by simp [hn])) (mem_singleton _), end, λ H, linear_independent_iff.2 $ λ l hl, begin ext i, simp only [finsupp.zero_apply], by_contra hn, refine hn (H i _ _), refine (finsupp.mem_span_image_iff_total _).2 ⟨finsupp.single i (l i) - l, _, _⟩, { rw finsupp.mem_supported', intros j hj, have hij : j = i := not_not.1 (λ hij : j ≠ i, hj ((mem_diff _).2 ⟨mem_univ _, λ h, hij (eq_of_mem_singleton h)⟩)), simp [hij] }, { simp [hl] } end⟩ /-- See also `complete_lattice.independent_iff_linear_independent_of_ne_zero`. -/ lemma linear_independent.independent_span_singleton (hv : linear_independent R v) : complete_lattice.independent $ λ i, R ∙ v i := begin refine complete_lattice.independent_def.mp (λ i m hm, (mem_bot R).mpr _), simp only [mem_inf, mem_span_singleton, supr_subtype', ← span_range_eq_supr] at hm, obtain ⟨⟨r, rfl⟩, hm⟩ := hm, suffices : r = 0, { simp [this], }, apply linear_independent_iff_not_smul_mem_span.mp hv i, convert hm, ext, simp, end variable (R) lemma exists_maximal_independent' (s : ι → M) : ∃ I : set ι, linear_independent R (λ x : I, s x) ∧ ∀ J : set ι, I ⊆ J → linear_independent R (λ x : J, s x) → I = J := begin let indep : set ι → Prop := λ I, linear_independent R (s ∘ coe : I → M), let X := { I : set ι // indep I }, let r : X → X → Prop := λ I J, I.1 ⊆ J.1, have key : ∀ c : set X, is_chain r c → indep (⋃ (I : X) (H : I ∈ c), I), { intros c hc, dsimp [indep], rw [linear_independent_comp_subtype], intros f hsupport hsum, rcases eq_empty_or_nonempty c with rfl | hn, { simpa using hsupport }, haveI : is_refl X r := ⟨λ _, set.subset.refl _⟩, obtain ⟨I, I_mem, hI⟩ : ∃ I ∈ c, (f.support : set ι) ⊆ I := hc.directed_on.exists_mem_subset_of_finset_subset_bUnion hn hsupport, exact linear_independent_comp_subtype.mp I.2 f hI hsum }, have trans : transitive r := λ I J K, set.subset.trans, obtain ⟨⟨I, hli : indep I⟩, hmax : ∀ a, r ⟨I, hli⟩ a → r a ⟨I, hli⟩⟩ := @exists_maximal_of_chains_bounded _ r (λ c hc, ⟨⟨⋃ I ∈ c, (I : set ι), key c hc⟩, λ I, set.subset_bUnion_of_mem⟩) trans, exact ⟨I, hli, λ J hsub hli, set.subset.antisymm hsub (hmax ⟨J, hli⟩ hsub)⟩, end lemma exists_maximal_independent (s : ι → M) : ∃ I : set ι, linear_independent R (λ x : I, s x) ∧ ∀ i ∉ I, ∃ a : R, a ≠ 0 ∧ a • s i ∈ span R (s '' I) := begin classical, rcases exists_maximal_independent' R s with ⟨I, hIlinind, hImaximal⟩, use [I, hIlinind], intros i hi, specialize hImaximal (I ∪ {i}) (by simp), set J := I ∪ {i} with hJ, have memJ : ∀ {x}, x ∈ J ↔ x = i ∨ x ∈ I, by simp [hJ], have hiJ : i ∈ J := by simp, have h := mt hImaximal _, swap, { intro h2, rw h2 at hi, exact absurd hiJ hi }, obtain ⟨f, supp_f, sum_f, f_ne⟩ := linear_dependent_comp_subtype.mp h, have hfi : f i ≠ 0, { contrapose hIlinind, refine linear_dependent_comp_subtype.mpr ⟨f, _, sum_f, f_ne⟩, simp only [finsupp.mem_supported, hJ] at ⊢ supp_f, rintro x hx, refine (memJ.mp (supp_f hx)).resolve_left _, rintro rfl, exact hIlinind (finsupp.mem_support_iff.mp hx) }, use [f i, hfi], have hfi' : i ∈ f.support := finsupp.mem_support_iff.mpr hfi, rw [← finset.insert_erase hfi', finset.sum_insert (finset.not_mem_erase _ _), add_eq_zero_iff_eq_neg] at sum_f, rw sum_f, refine neg_mem (sum_mem (λ c hc, smul_mem _ _ (subset_span ⟨c, _, rfl⟩))), exact (memJ.mp (supp_f (finset.erase_subset _ _ hc))).resolve_left (finset.ne_of_mem_erase hc), end end repr lemma surjective_of_linear_independent_of_span [nontrivial R] (hv : linear_independent R v) (f : ι' ↪ ι) (hss : range v ⊆ span R (range (v ∘ f))) : surjective f := begin intros i, let repr : (span R (range (v ∘ f)) : Type*) → ι' →₀ R := (hv.comp f f.injective).repr, let l := (repr ⟨v i, hss (mem_range_self i)⟩).map_domain f, have h_total_l : finsupp.total ι M R v l = v i, { dsimp only [l], rw finsupp.total_map_domain, rw (hv.comp f f.injective).total_repr, { refl }, { exact f.injective } }, have h_total_eq : (finsupp.total ι M R v) l = (finsupp.total ι M R v) (finsupp.single i 1), by rw [h_total_l, finsupp.total_single, one_smul], have l_eq : l = _ := linear_map.ker_eq_bot.1 hv h_total_eq, dsimp only [l] at l_eq, rw ←finsupp.emb_domain_eq_map_domain at l_eq, rcases finsupp.single_of_emb_domain_single (repr ⟨v i, _⟩) f i (1 : R) zero_ne_one.symm l_eq with ⟨i', hi'⟩, use i', exact hi'.2 end lemma eq_of_linear_independent_of_span_subtype [nontrivial R] {s t : set M} (hs : linear_independent R (λ x, x : s → M)) (h : t ⊆ s) (hst : s ⊆ span R t) : s = t := begin let f : t ↪ s := ⟨λ x, ⟨x.1, h x.2⟩, λ a b hab, subtype.coe_injective (subtype.mk.inj hab)⟩, have h_surj : surjective f, { apply surjective_of_linear_independent_of_span hs f _, convert hst; simp [f, comp], }, show s = t, { apply subset.antisymm _ h, intros x hx, rcases h_surj ⟨x, hx⟩ with ⟨y, hy⟩, convert y.mem, rw ← subtype.mk.inj hy, refl } end open linear_map lemma linear_independent.image_subtype {s : set M} {f : M →ₗ[R] M'} (hs : linear_independent R (λ x, x : s → M)) (hf_inj : disjoint (span R s) f.ker) : linear_independent R (λ x, x : f '' s → M') := begin rw [← @subtype.range_coe _ s] at hf_inj, refine (hs.map hf_inj).to_subtype_range' _, simp [set.range_comp f] end lemma linear_independent.inl_union_inr {s : set M} {t : set M'} (hs : linear_independent R (λ x, x : s → M)) (ht : linear_independent R (λ x, x : t → M')) : linear_independent R (λ x, x : inl R M M' '' s ∪ inr R M M' '' t → M × M') := begin refine (hs.image_subtype _).union (ht.image_subtype _) _; [simp, simp, skip], simp only [span_image], simp [disjoint_iff, prod_inf_prod] end lemma linear_independent_inl_union_inr' {v : ι → M} {v' : ι' → M'} (hv : linear_independent R v) (hv' : linear_independent R v') : linear_independent R (sum.elim (inl R M M' ∘ v) (inr R M M' ∘ v')) := (hv.map' (inl R M M') ker_inl).sum_type (hv'.map' (inr R M M') ker_inr) $ begin refine is_compl_range_inl_inr.disjoint.mono _ _; simp only [span_le, range_coe, range_comp_subset_range], end /-- Dedekind's linear independence of characters -/ -- See, for example, Keith Conrad's note -- <https://kconrad.math.uconn.edu/blurbs/galoistheory/linearchar.pdf> theorem linear_independent_monoid_hom (G : Type*) [monoid G] (L : Type*) [comm_ring L] [no_zero_divisors L] : @linear_independent _ L (G → L) (λ f, f : (G →* L) → (G → L)) _ _ _ := by letI := classical.dec_eq (G →* L); letI : mul_action L L := distrib_mul_action.to_mul_action; -- We prove linear independence by showing that only the trivial linear combination vanishes. exact linear_independent_iff'.2 -- To do this, we use `finset` induction, (λ s, finset.induction_on s (λ g hg i, false.elim) $ λ a s has ih g hg, -- Here -- * `a` is a new character we will insert into the `finset` of characters `s`, -- * `ih` is the fact that only the trivial linear combination of characters in `s` is zero -- * `hg` is the fact that `g` are the coefficients of a linear combination summing to zero -- and it remains to prove that `g` vanishes on `insert a s`. -- We now make the key calculation: -- For any character `i` in the original `finset`, we have `g i • i = g i • a` as functions on the -- monoid `G`. have h1 : ∀ i ∈ s, (g i • i : G → L) = g i • a, from λ i his, funext $ λ x : G, -- We prove these expressions are equal by showing -- the differences of their values on each monoid element `x` is zero eq_of_sub_eq_zero $ ih (λ j, g j * j x - g j * a x) (funext $ λ y : G, calc -- After that, it's just a chase scene. (∑ i in s, ((g i * i x - g i * a x) • i : G → L)) y = ∑ i in s, (g i * i x - g i * a x) * i y : finset.sum_apply _ _ _ ... = ∑ i in s, (g i * i x * i y - g i * a x * i y) : finset.sum_congr rfl (λ _ _, sub_mul _ _ _) ... = ∑ i in s, g i * i x * i y - ∑ i in s, g i * a x * i y : finset.sum_sub_distrib ... = (g a * a x * a y + ∑ i in s, g i * i x * i y) - (g a * a x * a y + ∑ i in s, g i * a x * i y) : by rw add_sub_add_left_eq_sub ... = ∑ i in insert a s, g i * i x * i y - ∑ i in insert a s, g i * a x * i y : by rw [finset.sum_insert has, finset.sum_insert has] ... = ∑ i in insert a s, g i * i (x * y) - ∑ i in insert a s, a x * (g i * i y) : congr (congr_arg has_sub.sub (finset.sum_congr rfl $ λ i _, by rw [i.map_mul, mul_assoc])) (finset.sum_congr rfl $ λ _ _, by rw [mul_assoc, mul_left_comm]) ... = (∑ i in insert a s, (g i • i : G → L)) (x * y) - a x * (∑ i in insert a s, (g i • i : G → L)) y : by rw [finset.sum_apply, finset.sum_apply, finset.mul_sum]; refl ... = 0 - a x * 0 : by rw hg; refl ... = 0 : by rw [mul_zero, sub_zero]) i his, -- On the other hand, since `a` is not already in `s`, for any character `i ∈ s` -- there is some element of the monoid on which it differs from `a`. have h2 : ∀ i : G →* L, i ∈ s → ∃ y, i y ≠ a y, from λ i his, classical.by_contradiction $ λ h, have hia : i = a, from monoid_hom.ext $ λ y, classical.by_contradiction $ λ hy, h ⟨y, hy⟩, has $ hia ▸ his, -- From these two facts we deduce that `g` actually vanishes on `s`, have h3 : ∀ i ∈ s, g i = 0, from λ i his, let ⟨y, hy⟩ := h2 i his in have h : g i • i y = g i • a y, from congr_fun (h1 i his) y, or.resolve_right (mul_eq_zero.1 $ by rw [mul_sub, sub_eq_zero]; exact h) (sub_ne_zero_of_ne hy), -- And so, using the fact that the linear combination over `s` and over `insert a s` both vanish, -- we deduce that `g a = 0`. have h4 : g a = 0, from calc g a = g a * 1 : (mul_one _).symm ... = (g a • a : G → L) 1 : by rw ← a.map_one; refl ... = (∑ i in insert a s, (g i • i : G → L)) 1 : begin rw finset.sum_eq_single a, { intros i his hia, rw finset.mem_insert at his, rw [h3 i (his.resolve_left hia), zero_smul] }, { intros haas, exfalso, apply haas, exact finset.mem_insert_self a s } end ... = 0 : by rw hg; refl, -- Now we're done; the last two facts together imply that `g` vanishes on every element -- of `insert a s`. (finset.forall_mem_insert _ _ _).2 ⟨h4, h3⟩) lemma le_of_span_le_span [nontrivial R] {s t u: set M} (hl : linear_independent R (coe : u → M )) (hsu : s ⊆ u) (htu : t ⊆ u) (hst : span R s ≤ span R t) : s ⊆ t := begin have := eq_of_linear_independent_of_span_subtype (hl.mono (set.union_subset hsu htu)) (set.subset_union_right _ _) (set.union_subset (set.subset.trans subset_span hst) subset_span), rw ← this, apply set.subset_union_left end lemma span_le_span_iff [nontrivial R] {s t u: set M} (hl : linear_independent R (coe : u → M)) (hsu : s ⊆ u) (htu : t ⊆ u) : span R s ≤ span R t ↔ s ⊆ t := ⟨le_of_span_le_span hl hsu htu, span_mono⟩ end module section nontrivial variables [ring R] [nontrivial R] [add_comm_group M] [add_comm_group M'] variables [module R M] [no_zero_smul_divisors R M] [module R M'] variables {v : ι → M} {s t : set M} {x y z : M} lemma linear_independent_unique_iff (v : ι → M) [unique ι] : linear_independent R v ↔ v default ≠ 0 := begin simp only [linear_independent_iff, finsupp.total_unique, smul_eq_zero], refine ⟨λ h hv, _, λ hv l hl, finsupp.unique_ext $ hl.resolve_right hv⟩, have := h (finsupp.single default 1) (or.inr hv), exact one_ne_zero (finsupp.single_eq_zero.1 this) end alias linear_independent_unique_iff ↔ _ linear_independent_unique lemma linear_independent_singleton {x : M} (hx : x ≠ 0) : linear_independent R (λ x, x : ({x} : set M) → M) := linear_independent_unique coe hx end nontrivial /-! ### Properties which require `division_ring K` These can be considered generalizations of properties of linear independence in vector spaces. -/ section module variables [division_ring K] [add_comm_group V] [add_comm_group V'] variables [module K V] [module K V'] variables {v : ι → V} {s t : set V} {x y z : V} open submodule /- TODO: some of the following proofs can generalized with a zero_ne_one predicate type class (instead of a data containing type class) -/ lemma mem_span_insert_exchange : x ∈ span K (insert y s) → x ∉ span K s → y ∈ span K (insert x s) := begin simp [mem_span_insert], rintro a z hz rfl h, refine ⟨a⁻¹, -a⁻¹ • z, smul_mem _ _ hz, _⟩, have a0 : a ≠ 0, {rintro rfl, simp * at *}, simp [a0, smul_add, smul_smul] end lemma linear_independent_iff_not_mem_span : linear_independent K v ↔ (∀i, v i ∉ span K (v '' (univ \ {i}))) := begin apply linear_independent_iff_not_smul_mem_span.trans, split, { intros h i h_in_span, apply one_ne_zero (h i 1 (by simp [h_in_span])) }, { intros h i a ha, by_contradiction ha', exact false.elim (h _ ((smul_mem_iff _ ha').1 ha)) } end lemma linear_independent.insert (hs : linear_independent K (λ b, b : s → V)) (hx : x ∉ span K s) : linear_independent K (λ b, b : insert x s → V) := begin rw ← union_singleton, have x0 : x ≠ 0 := mt (by rintro rfl; apply zero_mem (span K s)) hx, apply hs.union (linear_independent_singleton x0), rwa [disjoint_span_singleton' x0] end lemma linear_independent_option' : linear_independent K (λ o, option.cases_on' o x v : option ι → V) ↔ linear_independent K v ∧ (x ∉ submodule.span K (range v)) := begin rw [← linear_independent_equiv (equiv.option_equiv_sum_punit ι).symm, linear_independent_sum, @range_unique _ punit, @linear_independent_unique_iff punit, disjoint_span_singleton], dsimp [(∘)], refine ⟨λ h, ⟨h.1, λ hx, h.2.1 $ h.2.2 hx⟩, λ h, ⟨h.1, _, λ hx, (h.2 hx).elim⟩⟩, rintro rfl, exact h.2 (zero_mem _) end lemma linear_independent.option (hv : linear_independent K v) (hx : x ∉ submodule.span K (range v)) : linear_independent K (λ o, option.cases_on' o x v : option ι → V) := linear_independent_option'.2 ⟨hv, hx⟩ lemma linear_independent_option {v : option ι → V} : linear_independent K v ↔ linear_independent K (v ∘ coe : ι → V) ∧ v none ∉ submodule.span K (range (v ∘ coe : ι → V)) := by simp only [← linear_independent_option', option.cases_on'_none_coe] theorem linear_independent_insert' {ι} {s : set ι} {a : ι} {f : ι → V} (has : a ∉ s) : linear_independent K (λ x : insert a s, f x) ↔ linear_independent K (λ x : s, f x) ∧ f a ∉ submodule.span K (f '' s) := by { rw [← linear_independent_equiv ((equiv.option_equiv_sum_punit _).trans (equiv.set.insert has).symm), linear_independent_option], simp [(∘), range_comp f] } theorem linear_independent_insert (hxs : x ∉ s) : linear_independent K (λ b : insert x s, (b : V)) ↔ linear_independent K (λ b : s, (b : V)) ∧ x ∉ submodule.span K s := (@linear_independent_insert' _ _ _ _ _ _ _ _ id hxs).trans $ by simp lemma linear_independent_pair {x y : V} (hx : x ≠ 0) (hy : ∀ a : K, a • x ≠ y) : linear_independent K (coe : ({x, y} : set V) → V) := pair_comm y x ▸ (linear_independent_singleton hx).insert $ mt mem_span_singleton.1 (not_exists.2 hy) lemma linear_independent_fin_cons {n} {v : fin n → V} : linear_independent K (fin.cons x v : fin (n + 1) → V) ↔ linear_independent K v ∧ x ∉ submodule.span K (range v) := begin rw [← linear_independent_equiv (fin_succ_equiv n).symm, linear_independent_option], convert iff.rfl, { ext, -- TODO: why doesn't simp use `fin_succ_equiv_symm_coe` here? rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] }, { ext, rw [comp_app, comp_app, fin_succ_equiv_symm_coe, fin.cons_succ] } end lemma linear_independent_fin_snoc {n} {v : fin n → V} : linear_independent K (fin.snoc v x : fin (n + 1) → V) ↔ linear_independent K v ∧ x ∉ submodule.span K (range v) := by rw [fin.snoc_eq_cons_rotate, linear_independent_equiv, linear_independent_fin_cons] /-- See `linear_independent.fin_cons'` for an uglier version that works if you only have a module over a semiring. -/ lemma linear_independent.fin_cons {n} {v : fin n → V} (hv : linear_independent K v) (hx : x ∉ submodule.span K (range v)) : linear_independent K (fin.cons x v : fin (n + 1) → V) := linear_independent_fin_cons.2 ⟨hv, hx⟩ lemma linear_independent_fin_succ {n} {v : fin (n + 1) → V} : linear_independent K v ↔ linear_independent K (fin.tail v) ∧ v 0 ∉ submodule.span K (range $ fin.tail v) := by rw [← linear_independent_fin_cons, fin.cons_self_tail] lemma linear_independent_fin_succ' {n} {v : fin (n + 1) → V} : linear_independent K v ↔ linear_independent K (fin.init v) ∧ v (fin.last _) ∉ submodule.span K (range $ fin.init v) := by rw [← linear_independent_fin_snoc, fin.snoc_init_self] lemma linear_independent_fin2 {f : fin 2 → V} : linear_independent K f ↔ f 1 ≠ 0 ∧ ∀ a : K, a • f 1 ≠ f 0 := by rw [linear_independent_fin_succ, linear_independent_unique_iff, range_unique, mem_span_singleton, not_exists, show fin.tail f default = f 1, by rw ← fin.succ_zero_eq_one; refl] lemma exists_linear_independent_extension (hs : linear_independent K (coe : s → V)) (hst : s ⊆ t) : ∃b⊆t, s ⊆ b ∧ t ⊆ span K b ∧ linear_independent K (coe : b → V) := begin rcases zorn_subset_nonempty {b | b ⊆ t ∧ linear_independent K (coe : b → V)} _ _ ⟨hst, hs⟩ with ⟨b, ⟨bt, bi⟩, sb, h⟩, { refine ⟨b, bt, sb, λ x xt, _, bi⟩, by_contra hn, apply hn, rw ← h _ ⟨insert_subset.2 ⟨xt, bt⟩, bi.insert hn⟩ (subset_insert _ _), exact subset_span (mem_insert _ _) }, { refine λ c hc cc c0, ⟨⋃₀ c, ⟨_, _⟩, λ x, _⟩, { exact sUnion_subset (λ x xc, (hc xc).1) }, { exact linear_independent_sUnion_of_directed cc.directed_on (λ x xc, (hc xc).2) }, { exact subset_sUnion_of_mem } } end variables (K t) lemma exists_linear_independent : ∃ b ⊆ t, span K b = span K t ∧ linear_independent K (coe : b → V) := begin obtain ⟨b, hb₁, -, hb₂, hb₃⟩ := exists_linear_independent_extension (linear_independent_empty K V) (set.empty_subset t), exact ⟨b, hb₁, (span_eq_of_le _ hb₂ (submodule.span_mono hb₁)).symm, hb₃⟩, end variables {K t} /-- `linear_independent.extend` adds vectors to a linear independent set `s ⊆ t` until it spans all elements of `t`. -/ noncomputable def linear_independent.extend (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : set V := classical.some (exists_linear_independent_extension hs hst) lemma linear_independent.extend_subset (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : hs.extend hst ⊆ t := let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hbt lemma linear_independent.subset_extend (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : s ⊆ hs.extend hst := let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hsb lemma linear_independent.subset_span_extend (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : t ⊆ span K (hs.extend hst) := let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in htb lemma linear_independent.linear_independent_extend (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ t) : linear_independent K (coe : hs.extend hst → V) := let ⟨hbt, hsb, htb, hli⟩ := classical.some_spec (exists_linear_independent_extension hs hst) in hli variables {K V} -- TODO(Mario): rewrite? lemma exists_of_linear_independent_of_finite_span {t : finset V} (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ (span K ↑t : submodule K V)) : ∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = t.card := have ∀t, ∀(s' : finset V), ↑s' ⊆ s → s ∩ ↑t = ∅ → s ⊆ (span K ↑(s' ∪ t) : submodule K V) → ∃t':finset V, ↑t' ⊆ s ∪ ↑t ∧ s ⊆ ↑t' ∧ t'.card = (s' ∪ t).card := assume t, finset.induction_on t (assume s' hs' _ hss', have s = ↑s', from eq_of_linear_independent_of_span_subtype hs hs' $ by simpa using hss', ⟨s', by simp [this]⟩) (assume b₁ t hb₁t ih s' hs' hst hss', have hb₁s : b₁ ∉ s, from assume h, have b₁ ∈ s ∩ ↑(insert b₁ t), from ⟨h, finset.mem_insert_self _ _⟩, by rwa [hst] at this, have hb₁s' : b₁ ∉ s', from assume h, hb₁s $ hs' h, have hst : s ∩ ↑t = ∅, from eq_empty_of_subset_empty $ subset.trans (by simp [inter_subset_inter, subset.refl]) (le_of_eq hst), classical.by_cases (assume : s ⊆ (span K ↑(s' ∪ t) : submodule K V), let ⟨u, hust, hsu, eq⟩ := ih _ hs' hst this in have hb₁u : b₁ ∉ u, from assume h, (hust h).elim hb₁s hb₁t, ⟨insert b₁ u, by simp [insert_subset_insert hust], subset.trans hsu (by simp), by simp [eq, hb₁t, hb₁s', hb₁u]⟩) (assume : ¬ s ⊆ (span K ↑(s' ∪ t) : submodule K V), let ⟨b₂, hb₂s, hb₂t⟩ := not_subset.mp this in have hb₂t' : b₂ ∉ s' ∪ t, from assume h, hb₂t $ subset_span h, have s ⊆ (span K ↑(insert b₂ s' ∪ t) : submodule K V), from assume b₃ hb₃, have ↑(s' ∪ insert b₁ t) ⊆ insert b₁ (insert b₂ ↑(s' ∪ t) : set V), by simp [insert_eq, -singleton_union, -union_singleton, union_subset_union, subset.refl, subset_union_right], have hb₃ : b₃ ∈ span K (insert b₁ (insert b₂ ↑(s' ∪ t) : set V)), from span_mono this (hss' hb₃), have s ⊆ (span K (insert b₁ ↑(s' ∪ t)) : submodule K V), by simpa [insert_eq, -singleton_union, -union_singleton] using hss', have hb₁ : b₁ ∈ span K (insert b₂ ↑(s' ∪ t)), from mem_span_insert_exchange (this hb₂s) hb₂t, by rw [span_insert_eq_span hb₁] at hb₃; simpa using hb₃, let ⟨u, hust, hsu, eq⟩ := ih _ (by simp [insert_subset, hb₂s, hs']) hst this in ⟨u, subset.trans hust $ union_subset_union (subset.refl _) (by simp [subset_insert]), hsu, by simp [eq, hb₂t', hb₁t, hb₁s']⟩)), begin have eq : t.filter (λx, x ∈ s) ∪ t.filter (λx, x ∉ s) = t, { ext1 x, by_cases x ∈ s; simp * }, apply exists.elim (this (t.filter (λx, x ∉ s)) (t.filter (λx, x ∈ s)) (by simp [set.subset_def]) (by simp [set.ext_iff] {contextual := tt}) (by rwa [eq])), intros u h, exact ⟨u, subset.trans h.1 (by simp [subset_def, and_imp, or_imp_distrib] {contextual:=tt}), h.2.1, by simp only [h.2.2, eq]⟩ end lemma exists_finite_card_le_of_finite_of_linear_independent_of_span (ht : t.finite) (hs : linear_independent K (λ x, x : s → V)) (hst : s ⊆ span K t) : ∃h : s.finite, h.to_finset.card ≤ ht.to_finset.card := have s ⊆ (span K ↑(ht.to_finset) : submodule K V), by simp; assumption, let ⟨u, hust, hsu, eq⟩ := exists_of_linear_independent_of_finite_span hs this in have s.finite, from u.finite_to_set.subset hsu, ⟨this, by rw [←eq]; exact (finset.card_le_of_subset $ finset.coe_subset.mp $ by simp [hsu])⟩ end module
4669208a2fca9df008ab8f7ea223b4558e681ba4
63abd62053d479eae5abf4951554e1064a4c45b4
/src/category_theory/equivalence.lean
dca8e4ad9054686c03c63596e486a4a4aca8fcd6
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
24,580
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.fully_faithful import category_theory.whiskering import tactic.slice /-! # Equivalence of categories An equivalence of categories `C` and `D` is a pair of functors `F : C ⥤ D` and `G : D ⥤ C` such that `η : 𝟭 C ≅ F ⋙ G` and `ε : G ⋙ F ≅ 𝟭 D`. In many situations, equivalences are a better notion of "sameness" of categories than the stricter isomorphims of categories. Recall that one way to express that two functors `F : C ⥤ D` and `G : D ⥤ C` are adjoint is using two natural transformations `η : 𝟭 C ⟶ F ⋙ G` and `ε : G ⋙ F ⟶ 𝟭 D`, called the unit and the counit, such that the compositions `F ⟶ FGF ⟶ F` and `G ⟶ GFG ⟶ G` are the identity. Unfortunately, it is not the case that the natural isomorphisms `η` and `ε` in the definition of an equivalence automatically give an adjunction. However, it is true that * if one of the two compositions is the identity, then so is the other, and * given an equivalence of categories, it is always possible to refine `η` in such a way that the identities are satisfied. For this reason, in mathlib we define an equivalence to be a "half-adjoint equivalence", which is a tuple `(F, G, η, ε)` as in the first paragraph such that the composite `F ⟶ FGF ⟶ F` is the identity. By the remark above, this already implies that the tuple is an "adjoint equivalence", i.e., that the composite `G ⟶ GFG ⟶ G` is also the identity. We also define essentially surjective functors and show that a functor is an equivalence if and only if it is full, faithful and essentially surjective. ## Main definitions * `equivalence`: bundled (half-)adjoint equivalences of categories * `is_equivalence`: type class on a functor `F` containing the data of the inverse `G` as well as the natural isomorphisms `η` and `ε`. * `ess_surj`: type class on a functor `F` containing the data of the preimages and the isomorphisms `F.obj (preimage d) ≅ d`. ## Main results * `equivalence.mk`: upgrade an equivalence to a (half-)adjoint equivalence * `equivalence_of_fully_faithfully_ess_surj`: a fully faithful essentially surjective functor is an equivalence. ## Notations We write `C ≌ D` (`\backcong`, not to be confused with `≅`/`\cong`) for a bundled equivalence. -/ namespace category_theory open category_theory.functor nat_iso category universes v₁ v₂ v₃ u₁ u₂ u₃ -- declare the `v`'s first; see `category_theory.category` for an explanation /-- We define an equivalence as a (half)-adjoint equivalence, a pair of functors with a unit and counit which are natural isomorphisms and the triangle law `Fη ≫ εF = 1`, or in other words the composite `F ⟶ FGF ⟶ F` is the identity. In `unit_inverse_comp`, we show that this is actually an adjoint equivalence, i.e., that the composite `G ⟶ GFG ⟶ G` is also the identity. The triangle equation is written as a family of equalities between morphisms, it is more complicated if we write it as an equality of natural transformations, because then we would have to insert natural transformations like `F ⟶ F1`. See https://stacks.math.columbia.edu/tag/001J -/ structure equivalence (C : Type u₁) [category.{v₁} C] (D : Type u₂) [category.{v₂} D] := mk' :: (functor : C ⥤ D) (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ functor ⋙ inverse) (counit_iso : inverse ⋙ functor ≅ 𝟭 D) (functor_unit_iso_comp' : ∀(X : C), functor.map ((unit_iso.hom : 𝟭 C ⟶ functor ⋙ inverse).app X) ≫ counit_iso.hom.app (functor.obj X) = 𝟙 (functor.obj X) . obviously) restate_axiom equivalence.functor_unit_iso_comp' infixr ` ≌ `:10 := equivalence variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] namespace equivalence /-- The unit of an equivalence of categories. -/ abbreviation unit (e : C ≌ D) : 𝟭 C ⟶ e.functor ⋙ e.inverse := e.unit_iso.hom /-- The counit of an equivalence of categories. -/ abbreviation counit (e : C ≌ D) : e.inverse ⋙ e.functor ⟶ 𝟭 D := e.counit_iso.hom /-- The inverse of the unit of an equivalence of categories. -/ abbreviation unit_inv (e : C ≌ D) : e.functor ⋙ e.inverse ⟶ 𝟭 C := e.unit_iso.inv /-- The inverse of the counit of an equivalence of categories. -/ abbreviation counit_inv (e : C ≌ D) : 𝟭 D ⟶ e.inverse ⋙ e.functor := e.counit_iso.inv /- While these abbreviations are convenient, they also cause some trouble, preventing structure projections from unfolding. -/ @[simp] lemma equivalence_mk'_unit (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit = unit_iso.hom := rfl @[simp] lemma equivalence_mk'_counit (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit = counit_iso.hom := rfl @[simp] lemma equivalence_mk'_unit_inv (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).unit_inv = unit_iso.inv := rfl @[simp] lemma equivalence_mk'_counit_inv (functor inverse unit_iso counit_iso f) : (⟨functor, inverse, unit_iso, counit_iso, f⟩ : C ≌ D).counit_inv = counit_iso.inv := rfl @[simp] lemma functor_unit_comp (e : C ≌ D) (X : C) : e.functor.map (e.unit.app X) ≫ e.counit.app (e.functor.obj X) = 𝟙 (e.functor.obj X) := e.functor_unit_iso_comp X @[simp] lemma counit_inv_functor_comp (e : C ≌ D) (X : C) : e.counit_inv.app (e.functor.obj X) ≫ e.functor.map (e.unit_inv.app X) = 𝟙 (e.functor.obj X) := begin erw [iso.inv_eq_inv (e.functor.map_iso (e.unit_iso.app X) ≪≫ e.counit_iso.app (e.functor.obj X)) (iso.refl _)], exact e.functor_unit_comp X end lemma counit_inv_app_functor (e : C ≌ D) (X : C) : e.counit_inv.app (e.functor.obj X) = e.functor.map (e.unit.app X) := by { symmetry, erw [←iso.comp_hom_eq_id (e.counit_iso.app _), functor_unit_comp], refl } lemma counit_app_functor (e : C ≌ D) (X : C) : e.counit.app (e.functor.obj X) = e.functor.map (e.unit_inv.app X) := by { erw [←iso.hom_comp_eq_id (e.functor.map_iso (e.unit_iso.app X)), functor_unit_comp], refl } /-- The other triangle equality. The proof follows the following proof in Globular: http://globular.science/1905.001 -/ @[simp] lemma unit_inverse_comp (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) ≫ e.inverse.map (e.counit.app Y) = 𝟙 (e.inverse.obj Y) := begin rw [←id_comp (e.inverse.map _), ←map_id e.inverse, ←counit_inv_functor_comp, map_comp, ←iso.hom_inv_id_assoc (e.unit_iso.app _) (e.inverse.map (e.functor.map _)), app_hom, app_inv], slice_lhs 2 3 { erw [e.unit.naturality] }, slice_lhs 1 2 { erw [e.unit.naturality] }, slice_lhs 4 4 { rw [←iso.hom_inv_id_assoc (e.inverse.map_iso (e.counit_iso.app _)) (e.unit_inv.app _)] }, slice_lhs 3 4 { erw [←map_comp e.inverse, e.counit.naturality], erw [(e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 2 3 { erw [←map_comp e.inverse, e.counit_iso.inv.naturality, map_comp] }, slice_lhs 3 4 { erw [e.unit_inv.naturality] }, slice_lhs 4 5 { erw [←map_comp (e.functor ⋙ e.inverse), (e.unit_iso.app _).hom_inv_id, map_id] }, erw [id_comp], slice_lhs 3 4 { erw [←e.unit_inv.naturality] }, slice_lhs 2 3 { erw [←map_comp e.inverse, ←e.counit_iso.inv.naturality, (e.counit_iso.app _).hom_inv_id, map_id] }, erw [id_comp, (e.unit_iso.app _).hom_inv_id], refl end @[simp] lemma inverse_counit_inv_comp (e : C ≌ D) (Y : D) : e.inverse.map (e.counit_inv.app Y) ≫ e.unit_inv.app (e.inverse.obj Y) = 𝟙 (e.inverse.obj Y) := begin erw [iso.inv_eq_inv (e.unit_iso.app (e.inverse.obj Y) ≪≫ e.inverse.map_iso (e.counit_iso.app Y)) (iso.refl _)], exact e.unit_inverse_comp Y end lemma unit_app_inverse (e : C ≌ D) (Y : D) : e.unit.app (e.inverse.obj Y) = e.inverse.map (e.counit_inv.app Y) := by { erw [←iso.comp_hom_eq_id (e.inverse.map_iso (e.counit_iso.app Y)), unit_inverse_comp], refl } lemma unit_inv_app_inverse (e : C ≌ D) (Y : D) : e.unit_inv.app (e.inverse.obj Y) = e.inverse.map (e.counit.app Y) := by { symmetry, erw [←iso.hom_comp_eq_id (e.unit_iso.app _), unit_inverse_comp], refl } @[simp] lemma fun_inv_map (e : C ≌ D) (X Y : D) (f : X ⟶ Y) : e.functor.map (e.inverse.map f) = e.counit.app X ≫ f ≫ e.counit_inv.app Y := (nat_iso.naturality_2 (e.counit_iso) f).symm @[simp] lemma inv_fun_map (e : C ≌ D) (X Y : C) (f : X ⟶ Y) : e.inverse.map (e.functor.map f) = e.unit_inv.app X ≫ f ≫ e.unit.app Y := (nat_iso.naturality_1 (e.unit_iso) f).symm section -- In this section we convert an arbitrary equivalence to a half-adjoint equivalence. variables {F : C ⥤ D} {G : D ⥤ C} (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) /-- If `η : 𝟭 C ≅ F ⋙ G` is part of a (not necessarily half-adjoint) equivalence, we can upgrade it to a refined natural isomorphism `adjointify_η η : 𝟭 C ≅ F ⋙ G` which exhibits the properties required for a half-adjoint equivalence. See `equivalence.mk`. -/ def adjointify_η : 𝟭 C ≅ F ⋙ G := calc 𝟭 C ≅ F ⋙ G : η ... ≅ F ⋙ (𝟭 D ⋙ G) : iso_whisker_left F (left_unitor G).symm ... ≅ F ⋙ ((G ⋙ F) ⋙ G) : iso_whisker_left F (iso_whisker_right ε.symm G) ... ≅ F ⋙ (G ⋙ (F ⋙ G)) : iso_whisker_left F (associator G F G) ... ≅ (F ⋙ G) ⋙ (F ⋙ G) : (associator F G (F ⋙ G)).symm ... ≅ 𝟭 C ⋙ (F ⋙ G) : iso_whisker_right η.symm (F ⋙ G) ... ≅ F ⋙ G : left_unitor (F ⋙ G) lemma adjointify_η_ε (X : C) : F.map ((adjointify_η η ε).hom.app X) ≫ ε.hom.app (F.obj X) = 𝟙 (F.obj X) := begin dsimp [adjointify_η], simp, have := ε.hom.naturality (F.map (η.inv.app X)), dsimp at this, rw [this], clear this, rw [←assoc _ _ (F.map _)], have := ε.hom.naturality (ε.inv.app $ F.obj X), dsimp at this, rw [this], clear this, have := (ε.app $ F.obj X).hom_inv_id, dsimp at this, rw [this], clear this, rw [id_comp], have := (F.map_iso $ η.app X).hom_inv_id, dsimp at this, rw [this] end end /-- Every equivalence of categories consisting of functors `F` and `G` such that `F ⋙ G` and `G ⋙ F` are naturally isomorphic to identity functors can be transformed into a half-adjoint equivalence without changing `F` or `G`. -/ protected definition mk (F : C ⥤ D) (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : C ≌ D := ⟨F, G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ /-- Equivalence of categories is reflexive. -/ @[refl, simps] def refl : C ≌ C := ⟨𝟭 C, 𝟭 C, iso.refl _, iso.refl _, λ X, category.id_comp _⟩ instance : inhabited (C ≌ C) := ⟨refl⟩ /-- Equivalence of categories is symmetric. -/ @[symm, simps] def symm (e : C ≌ D) : D ≌ C := ⟨e.inverse, e.functor, e.counit_iso.symm, e.unit_iso.symm, e.inverse_counit_inv_comp⟩ variables {E : Type u₃} [category.{v₃} E] /-- Equivalence of categories is transitive. -/ @[trans, simps] def trans (e : C ≌ D) (f : D ≌ E) : C ≌ E := { functor := e.functor ⋙ f.functor, inverse := f.inverse ⋙ e.inverse, unit_iso := begin refine iso.trans e.unit_iso _, exact iso_whisker_left e.functor (iso_whisker_right f.unit_iso e.inverse) , end, counit_iso := begin refine iso.trans _ f.counit_iso, exact iso_whisker_left f.inverse (iso_whisker_right e.counit_iso f.functor) end, -- We wouldn't have needed to give this proof if we'd used `equivalence.mk`, -- but we choose to avoid using that here, for the sake of good structure projection `simp` lemmas. functor_unit_iso_comp' := λ X, begin dsimp, rw [← f.functor.map_comp_assoc, e.functor.map_comp, ←counit_inv_app_functor, fun_inv_map, iso.inv_hom_id_app_assoc, assoc, iso.inv_hom_id_app, counit_app_functor, ← functor.map_comp], erw [comp_id, iso.hom_inv_id_app, functor.map_id], end } /-- Composing a functor with both functors of an equivalence yields a naturally isomorphic functor. -/ def fun_inv_id_assoc (e : C ≌ D) (F : C ⥤ E) : e.functor ⋙ e.inverse ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.unit_iso.symm F ≪≫ F.left_unitor @[simp] lemma fun_inv_id_assoc_hom_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).hom.app X = F.map (e.unit_inv.app X) := by { dsimp [fun_inv_id_assoc], tidy } @[simp] lemma fun_inv_id_assoc_inv_app (e : C ≌ D) (F : C ⥤ E) (X : C) : (fun_inv_id_assoc e F).inv.app X = F.map (e.unit.app X) := by { dsimp [fun_inv_id_assoc], tidy } /-- Composing a functor with both functors of an equivalence yields a naturally isomorphic functor. -/ def inv_fun_id_assoc (e : C ≌ D) (F : D ⥤ E) : e.inverse ⋙ e.functor ⋙ F ≅ F := (functor.associator _ _ _).symm ≪≫ iso_whisker_right e.counit_iso F ≪≫ F.left_unitor @[simp] lemma inv_fun_id_assoc_hom_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).hom.app X = F.map (e.counit.app X) := by { dsimp [inv_fun_id_assoc], tidy } @[simp] lemma inv_fun_id_assoc_inv_app (e : C ≌ D) (F : D ⥤ E) (X : D) : (inv_fun_id_assoc e F).inv.app X = F.map (e.counit_inv.app X) := by { dsimp [inv_fun_id_assoc], tidy } section cancellation_lemmas variables (e : C ≌ D) -- We need special forms of `cancel_nat_iso_hom_right(_assoc)` and `cancel_nat_iso_inv_right(_assoc)` -- for units and counits, because neither `simp` or `rw` will apply those lemmas in this -- setting without providing `e.unit_iso` (or similar) as an explicit argument. -- We also provide the lemmas for length four compositions, since they're occasionally useful. -- (e.g. in proving that equivalences take monos to monos) @[simp] lemma cancel_unit_right {X Y : C} (f f' : X ⟶ Y) : f ≫ e.unit.app Y = f' ≫ e.unit.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_unit_inv_right {X Y : C} (f f' : X ⟶ e.inverse.obj (e.functor.obj Y)) : f ≫ e.unit_inv.app Y = f' ≫ e.unit_inv.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_counit_right {X Y : D} (f f' : X ⟶ e.functor.obj (e.inverse.obj Y)) : f ≫ e.counit.app Y = f' ≫ e.counit.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_counit_inv_right {X Y : D} (f f' : X ⟶ Y) : f ≫ e.counit_inv.app Y = f' ≫ e.counit_inv.app Y ↔ f = f' := by simp only [cancel_mono] @[simp] lemma cancel_unit_right_assoc {W X X' Y : C} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) : f ≫ g ≫ e.unit.app Y = f' ≫ g' ≫ e.unit.app Y ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_counit_inv_right_assoc {W X X' Y : D} (f : W ⟶ X) (g : X ⟶ Y) (f' : W ⟶ X') (g' : X' ⟶ Y) : f ≫ g ≫ e.counit_inv.app Y = f' ≫ g' ≫ e.counit_inv.app Y ↔ f ≫ g = f' ≫ g' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_unit_right_assoc' {W X X' Y Y' Z : C} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) (f' : W ⟶ X') (g' : X' ⟶ Y') (h' : Y' ⟶ Z) : f ≫ g ≫ h ≫ e.unit.app Z = f' ≫ g' ≫ h' ≫ e.unit.app Z ↔ f ≫ g ≫ h = f' ≫ g' ≫ h' := by simp only [←category.assoc, cancel_mono] @[simp] lemma cancel_counit_inv_right_assoc' {W X X' Y Y' Z : D} (f : W ⟶ X) (g : X ⟶ Y) (h : Y ⟶ Z) (f' : W ⟶ X') (g' : X' ⟶ Y') (h' : Y' ⟶ Z) : f ≫ g ≫ h ≫ e.counit_inv.app Z = f' ≫ g' ≫ h' ≫ e.counit_inv.app Z ↔ f ≫ g ≫ h = f' ≫ g' ≫ h' := by simp only [←category.assoc, cancel_mono] end cancellation_lemmas section -- There's of course a monoid structure on `C ≌ C`, -- but let's not encourage using it. -- The power structure is nevertheless useful. /-- Powers of an auto-equivalence. -/ def pow (e : C ≌ C) : ℤ → (C ≌ C) | (int.of_nat 0) := equivalence.refl | (int.of_nat 1) := e | (int.of_nat (n+2)) := e.trans (pow (int.of_nat (n+1))) | (int.neg_succ_of_nat 0) := e.symm | (int.neg_succ_of_nat (n+1)) := e.symm.trans (pow (int.neg_succ_of_nat n)) instance : has_pow (C ≌ C) ℤ := ⟨pow⟩ @[simp] lemma pow_zero (e : C ≌ C) : e^(0 : ℤ) = equivalence.refl := rfl @[simp] lemma pow_one (e : C ≌ C) : e^(1 : ℤ) = e := rfl @[simp] lemma pow_minus_one (e : C ≌ C) : e^(-1 : ℤ) = e.symm := rfl -- TODO as necessary, add the natural isomorphisms `(e^a).trans e^b ≅ e^(a+b)`. -- At this point, we haven't even defined the category of equivalences. end end equivalence /-- A functor that is part of a (half) adjoint equivalence -/ class is_equivalence (F : C ⥤ D) := mk' :: (inverse : D ⥤ C) (unit_iso : 𝟭 C ≅ F ⋙ inverse) (counit_iso : inverse ⋙ F ≅ 𝟭 D) (functor_unit_iso_comp' : ∀ (X : C), F.map ((unit_iso.hom : 𝟭 C ⟶ F ⋙ inverse).app X) ≫ counit_iso.hom.app (F.obj X) = 𝟙 (F.obj X) . obviously) restate_axiom is_equivalence.functor_unit_iso_comp' namespace is_equivalence instance of_equivalence (F : C ≌ D) : is_equivalence F.functor := { ..F } instance of_equivalence_inverse (F : C ≌ D) : is_equivalence F.inverse := is_equivalence.of_equivalence F.symm open equivalence /-- To see that a functor is an equivalence, it suffices to provide an inverse functor `G` such that `F ⋙ G` and `G ⋙ F` are naturally isomorphic to identity functors. -/ protected definition mk {F : C ⥤ D} (G : D ⥤ C) (η : 𝟭 C ≅ F ⋙ G) (ε : G ⋙ F ≅ 𝟭 D) : is_equivalence F := ⟨G, adjointify_η η ε, ε, adjointify_η_ε η ε⟩ end is_equivalence namespace functor /-- Interpret a functor that is an equivalence as an equivalence. -/ def as_equivalence (F : C ⥤ D) [is_equivalence F] : C ≌ D := ⟨F, is_equivalence.inverse F, is_equivalence.unit_iso, is_equivalence.counit_iso, is_equivalence.functor_unit_iso_comp⟩ instance is_equivalence_refl : is_equivalence (𝟭 C) := is_equivalence.of_equivalence equivalence.refl /-- The inverse functor of a functor that is an equivalence. -/ def inv (F : C ⥤ D) [is_equivalence F] : D ⥤ C := is_equivalence.inverse F instance is_equivalence_inv (F : C ⥤ D) [is_equivalence F] : is_equivalence F.inv := is_equivalence.of_equivalence F.as_equivalence.symm @[simp] lemma as_equivalence_functor (F : C ⥤ D) [is_equivalence F] : F.as_equivalence.functor = F := rfl @[simp] lemma as_equivalence_inverse (F : C ⥤ D) [is_equivalence F] : F.as_equivalence.inverse = inv F := rfl @[simp] lemma inv_inv (F : C ⥤ D) [is_equivalence F] : inv (inv F) = F := rfl /-- The composition of functor that is an equivalence with its inverse is naturally isomorphic to the identity functor. -/ def fun_inv_id (F : C ⥤ D) [is_equivalence F] : F ⋙ F.inv ≅ 𝟭 C := is_equivalence.unit_iso.symm /-- The composition of functor that is an equivalence with its inverse is naturally isomorphic to the identity functor. -/ def inv_fun_id (F : C ⥤ D) [is_equivalence F] : F.inv ⋙ F ≅ 𝟭 D := is_equivalence.counit_iso variables {E : Type u₃} [category.{v₃} E] instance is_equivalence_trans (F : C ⥤ D) (G : D ⥤ E) [is_equivalence F] [is_equivalence G] : is_equivalence (F ⋙ G) := is_equivalence.of_equivalence (equivalence.trans (as_equivalence F) (as_equivalence G)) end functor namespace equivalence @[simp] lemma functor_inv (E : C ≌ D) : E.functor.inv = E.inverse := rfl @[simp] lemma inverse_inv (E : C ≌ D) : E.inverse.inv = E.functor := rfl @[simp] lemma functor_as_equivalence (E : C ≌ D) : E.functor.as_equivalence = E := by { cases E, congr, } @[simp] lemma inverse_as_equivalence (E : C ≌ D) : E.inverse.as_equivalence = E.symm := by { cases E, congr, } end equivalence namespace is_equivalence @[simp] lemma fun_inv_map (F : C ⥤ D) [is_equivalence F] (X Y : D) (f : X ⟶ Y) : F.map (F.inv.map f) = F.inv_fun_id.hom.app X ≫ f ≫ F.inv_fun_id.inv.app Y := begin erw [nat_iso.naturality_2], refl end @[simp] lemma inv_fun_map (F : C ⥤ D) [is_equivalence F] (X Y : C) (f : X ⟶ Y) : F.inv.map (F.map f) = F.fun_inv_id.hom.app X ≫ f ≫ F.fun_inv_id.inv.app Y := begin erw [nat_iso.naturality_2], refl end -- We should probably restate many of the lemmas about `equivalence` for `is_equivalence`, -- but these are the only ones I need for now. @[simp] lemma functor_unit_comp (E : C ⥤ D) [is_equivalence E] (Y) : E.map (E.fun_inv_id.inv.app Y) ≫ E.inv_fun_id.hom.app (E.obj Y) = 𝟙 _ := equivalence.functor_unit_comp E.as_equivalence Y @[simp] lemma inv_fun_id_inv_comp (E : C ⥤ D) [is_equivalence E] (Y) : E.inv_fun_id.inv.app (E.obj Y) ≫ E.map (E.fun_inv_id.hom.app Y) = 𝟙 _ := eq_of_inv_eq_inv (functor_unit_comp _ _) end is_equivalence /-- A functor `F : C ⥤ D` is essentially surjective if for every `d : D`, there is some `c : C` so `F.obj c ≅ D`. See https://stacks.math.columbia.edu/tag/001C. -/ -- TODO should we make this a `Prop` that merely asserts the existence of a preimage, -- rather than choosing one? class ess_surj (F : C ⥤ D) := (obj_preimage (d : D) : C) (iso' (d : D) : F.obj (obj_preimage d) ≅ d . obviously) restate_axiom ess_surj.iso' /-- Applying an essentially surjective functor to a preimage of `d` yields an object that is isomorphic to `d`. -/ add_decl_doc ess_surj.iso namespace functor /-- Given an essentially surjective functor, we can find a preimage for every object `d` in the codomain. Applying the functor to this preimage will yield an object isomorphic to `d`, see `fun_obj_preimage_iso`. -/ def obj_preimage (F : C ⥤ D) [ess_surj F] (d : D) : C := ess_surj.obj_preimage.{v₁ v₂} F d /-- Applying an essentially surjective functor to a preimage of `d` yields an object that is isomorphic to `d`. -/ def fun_obj_preimage_iso (F : C ⥤ D) [ess_surj F] (d : D) : F.obj (F.obj_preimage d) ≅ d := ess_surj.iso d end functor namespace equivalence /-- An equivalence is essentially surjective. See https://stacks.math.columbia.edu/tag/02C3. -/ def ess_surj_of_equivalence (F : C ⥤ D) [is_equivalence F] : ess_surj F := ⟨ λ Y : D, F.inv.obj Y, λ Y : D, (F.inv_fun_id.app Y) ⟩ /-- An equivalence is faithful. See https://stacks.math.columbia.edu/tag/02C3. -/ @[priority 100] -- see Note [lower instance priority] instance faithful_of_equivalence (F : C ⥤ D) [is_equivalence F] : faithful F := { map_injective' := λ X Y f g w, begin have p := congr_arg (@category_theory.functor.map _ _ _ _ F.inv _ _) w, simpa only [cancel_epi, cancel_mono, is_equivalence.inv_fun_map] using p end }. /-- An equivalence is full. See https://stacks.math.columbia.edu/tag/02C3. -/ @[priority 100] -- see Note [lower instance priority] instance full_of_equivalence (F : C ⥤ D) [is_equivalence F] : full F := { preimage := λ X Y f, F.fun_inv_id.inv.app X ≫ F.inv.map f ≫ F.fun_inv_id.hom.app Y, witness' := λ X Y f, F.inv.map_injective (by simpa only [is_equivalence.inv_fun_map, assoc, iso.hom_inv_id_app_assoc, iso.hom_inv_id_app] using comp_id _) } @[simp] private def equivalence_inverse (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : D ⥤ C := { obj := λ X, F.obj_preimage X, map := λ X Y f, F.preimage ((F.fun_obj_preimage_iso X).hom ≫ f ≫ (F.fun_obj_preimage_iso Y).inv), map_id' := λ X, begin apply F.map_injective, tidy end, map_comp' := λ X Y Z f g, by apply F.map_injective; simp } /-- A functor which is full, faithful, and essentially surjective is an equivalence. See https://stacks.math.columbia.edu/tag/02C3. -/ def equivalence_of_fully_faithfully_ess_surj (F : C ⥤ D) [full F] [faithful F] [ess_surj F] : is_equivalence F := is_equivalence.mk (equivalence_inverse F) (nat_iso.of_components (λ X, (preimage_iso $ F.fun_obj_preimage_iso $ F.obj X).symm) (λ X Y f, by { apply F.map_injective, obviously })) (nat_iso.of_components (λ Y, F.fun_obj_preimage_iso Y) (by obviously)) @[simp] lemma functor_map_inj_iff (e : C ≌ D) {X Y : C} (f g : X ⟶ Y) : e.functor.map f = e.functor.map g ↔ f = g := begin split, { intro w, apply e.functor.map_injective, exact w, }, { rintro ⟨rfl⟩, refl, } end @[simp] lemma inverse_map_inj_iff (e : C ≌ D) {X Y : D} (f g : X ⟶ Y) : e.inverse.map f = e.inverse.map g ↔ f = g := begin split, { intro w, apply e.inverse.map_injective, exact w, }, { rintro ⟨rfl⟩, refl, } end end equivalence end category_theory
b2919af4c6bb0b64851f5f9d80da326164cba16b
f3a5af2927397cf346ec0e24312bfff077f00425
/src/game/world5/level9.lean
cd5d47d66a932006a21e630791342d44b4c3704b
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/natural_number_game
05c39e1586408cfb563d1a12e1085a90726ab655
f29b6c2884299fc63fdfc81ae5d7daaa3219f9fd
refs/heads/master
1,688,570,964,990
1,636,908,242,000
1,636,908,242,000
195,403,790
277
84
Apache-2.0
1,694,547,955,000
1,562,328,792,000
Lean
UTF-8
Lean
false
false
1,151
lean
/- # Function world. ## Level 9: a big maze. I asked around on Zulip and apparently there is not a tactic for this, perhaps because this level is rather artificial. In world 6 we will see a variant of this example which can be solved by a tactic. It would be an interesting project to make a tactic which could solve this sort of level in Lean. You can of course work both forwards and backwards, or you could crack and draw a picture. -/ /- Definition Given a bunch of functions, we can define another one. -/ example (A B C D E F G H I J K L : Type) (f1 : A → B) (f2 : B → E) (f3 : E → D) (f4 : D → A) (f5 : E → F) (f6 : F → C) (f7 : B → C) (f8 : F → G) (f9 : G → J) (f10 : I → J) (f11 : J → I) (f12 : I → H) (f13 : E → H) (f14 : H → K) (f15 : I → L) : A → L := begin intro a, apply f15, apply f11, apply f9, apply f8, apply f5, apply f2, apply f1, assumption, end /- That's the end of Function World! Next it's Proposition world, and the tactics you've learnt in Function World are enough to solve all nine levels! In fact, the levels in Proposition world might look strangely familiar$\ldots$. -/
6db57cf61c3bc46d25ce8cc038e5a7cbf90fe25f
986c843ca00918283ebed5442e374c3f71aba78b
/src/falso/hyperprover.lean
5d6a9924e03fbb4a970bba11bcd8b18d4cd62edb
[ "MIT" ]
permissive
mjendrusch/lean-falso
c58b53b1b9e128426688ee2dbbca717fd4a001f5
bb61d1799785b316c4c8ed3f2ea3d5cf3af50e38
refs/heads/master
1,584,498,265,175
1,526,857,132,000
1,526,857,132,000
134,189,252
2
0
null
null
null
null
UTF-8
Lean
false
false
563
lean
-- -- The Falso HyperProver in Lean. Why bother with another axiom system? -- (c) Copyright 2018 Michael Jendrusch -- -- For details about the copyright, see the file -- LICENSE, included in this distribution. -- import init.meta.tactic namespace falso -- a minimal implementation of the Falso (TM) system in Lean. axiom explode : false -- The core of Falso proof technology: def prove (P : Prop) : P := explode.rec P -- The Falso tactic: meta def hyperprove : tactic unit := do t ← tactic.target, tactic.to_expr ``(prove %%t) >>= tactic.exact end falso
ab1c232b583f7e97f00caba15b93e131884cb957
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Elab/Tactic/Congr.lean
4d45e7977a60c735e4afa8279fb0b2b8419968ba
[ "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
596
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Meta.Tactic.Congr import Lean.Elab.Tactic.Basic namespace Lean.Elab.Tactic namespace Lean.Elab.Tactic @[builtin_tactic Parser.Tactic.congr] def evalCongr : Tactic := fun stx => match stx with | `(tactic| congr $[$n?]?) => let hugeDepth := 1000000 let depth := n?.map (·.getNat) |>.getD hugeDepth liftMetaTactic fun mvarId => mvarId.congrN depth | _ => throwUnsupportedSyntax end Lean.Elab.Tactic
6f49cf65747d7c0c0ae09a582ac914fa26a590b7
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/computability/epsilon_NFA.lean
79dc25c0d4bfcc3d40f6b27fb2df066d135c0478
[ "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
4,455
lean
/- Copyright (c) 2021 Fox Thomson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Fox Thomson -/ import computability.NFA /-! # Epsilon Nondeterministic Finite Automata This file contains the definition of an epsilon Nondeterministic Finite Automaton (`ε_NFA`), a state machine which determines whether a string (implemented as a list over an arbitrary alphabet) is in a regular set by evaluating the string over every possible path, also having access to ε-transitons, which can be followed without reading a character. Since this definition allows for automata with infinite states, a `fintype` instance must be supplied for true `ε_NFA`'s. -/ universes u v /-- An `ε_NFA` is a set of states (`σ`), a transition function from state to state labelled by the alphabet (`step`), a starting state (`start`) and a set of acceptance states (`accept`). Note the transition function sends a state to a `set` of states and can make ε-transitions by inputing `none`. Since this definition allows for Automata with infinite states, a `fintype` instance must be supplied for true `ε_NFA`'s.-/ structure ε_NFA (α : Type u) (σ : Type v) := (step : σ → option α → set σ) (start : set σ) (accept : set σ) variables {α : Type u} {σ σ' : Type v} (M : ε_NFA α σ) namespace ε_NFA instance : inhabited (ε_NFA α σ) := ⟨ ε_NFA.mk (λ _ _, ∅) ∅ ∅ ⟩ /-- The `ε_closure` of a set is the set of states which can be reached by taking a finite string of ε-transitions from an element of the set -/ inductive ε_closure : set σ → set σ | base : ∀ S (s ∈ S), ε_closure S s | step : ∀ S s (t ∈ M.step s none), ε_closure S s → ε_closure S t /-- `M.step_set S a` is the union of the ε-closure of `M.step s a` for all `s ∈ S`. -/ def step_set : set σ → α → set σ := λ S a, S >>= (λ s, M.ε_closure (M.step s a)) /-- `M.eval_from S x` computes all possible paths though `M` with input `x` starting at an element of `S`. -/ def eval_from (start : set σ) : list α → set σ := list.foldl M.step_set (M.ε_closure start) /-- `M.eval x` computes all possible paths though `M` with input `x` starting at an element of `M.start`. -/ def eval := M.eval_from M.start /-- `M.accepts` is the language of `x` such that there is an accept state in `M.eval x`. -/ def accepts : language α := λ x, ∃ S ∈ M.accept, S ∈ M.eval x /-- `M.to_NFA` is an `NFA` constructed from an `ε_NFA` `M`. -/ def to_NFA : NFA α σ := { step := λ S a, M.ε_closure (M.step S a), start := M.ε_closure M.start, accept := M.accept } @[simp] lemma to_NFA_eval_from_match (start : set σ) : M.to_NFA.eval_from (M.ε_closure start) = M.eval_from start := rfl @[simp] lemma to_NFA_correct : M.to_NFA.accepts = M.accepts := begin ext x, rw [accepts, NFA.accepts, eval, NFA.eval, ←to_NFA_eval_from_match], refl end lemma pumping_lemma [fintype σ] {x : list α} (hx : x ∈ M.accepts) (hlen : fintype.card (set σ) ≤ list.length x) : ∃ a b c, x = a ++ b ++ c ∧ a.length + b.length ≤ fintype.card (set σ) ∧ b ≠ [] ∧ {a} * language.star {b} * {c} ≤ M.accepts := begin rw ←to_NFA_correct at hx ⊢, exact M.to_NFA.pumping_lemma hx hlen end end ε_NFA namespace NFA /-- `M.to_ε_NFA` is an `ε_NFA` constructed from an `NFA` `M` by using the same start and accept states and transition functions. -/ def to_ε_NFA (M : NFA α σ) : ε_NFA α σ := { step := λ s a, a.cases_on' ∅ (λ a, M.step s a), start := M.start, accept := M.accept } @[simp] lemma to_ε_NFA_ε_closure (M : NFA α σ) (S : set σ) : M.to_ε_NFA.ε_closure S = S := begin ext a, split, { rintro ( ⟨ _, _, h ⟩ | ⟨ _, _, _, h, _ ⟩ ), exact h, cases h }, { intro h, apply ε_NFA.ε_closure.base, exact h } end @[simp] lemma to_ε_NFA_eval_from_match (M : NFA α σ) (start : set σ) : M.to_ε_NFA.eval_from start = M.eval_from start := begin rw [eval_from, ε_NFA.eval_from, step_set, ε_NFA.step_set, to_ε_NFA_ε_closure], congr, ext S s, simp only [exists_prop, set.mem_Union, set.bind_def], apply exists_congr, simp only [and.congr_right_iff], intros t ht, rw M.to_ε_NFA_ε_closure, refl end @[simp] lemma to_ε_NFA_correct (M : NFA α σ) : M.to_ε_NFA.accepts = M.accepts := begin rw [accepts, ε_NFA.accepts, eval, ε_NFA.eval, to_ε_NFA_eval_from_match], refl end end NFA
5f911ab876ba44323c10eaafd4f5f616102d8f16
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/calc.lean
f47db1384a07696e9ab710397813460b7c825ccd
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
356
lean
namespace foo constant le : num → num → Prop axiom le_trans {a b c : num} : le a b → le b c → le a c attribute le_trans [trans] infix `<<`:50 := le end foo namespace foo theorem T {a b c d : num} : a << b → b << c → c << d → a << d := assume H1 H2 H3, calc a << b : H1 ... << c : H2 ... << d : H3 end foo
a442bc478b64163e01b05f168103f209e8311091
dd0f5513e11c52db157d2fcc8456d9401a6cd9da
/04_Quantifiers_and_Equality.org.33.lean
8f96a517e4d4189ed0bdbfd7e2e0a7ee06bcd724
[]
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
196
lean
/- page 60 -/ import standard definition imp_self (p : Prop) : p → p := assume `p`, `p` print imp_self definition imp_self2 (p : Prop) : p → p → p := assume `p` `p`, `p` print imp_self2
2428abaab099ae6ce9a9967179eaa180cacbe61c
a45212b1526d532e6e83c44ddca6a05795113ddc
/src/data/string.lean
f11df72072b5503f5229f6382f4e4c1adf586417
[ "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
2,487
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 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 @[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 @[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 := (≤), le_antisymm := by simp; exact λ a b h₁ h₂, to_list_inj.1 (le_antisymm h₁ h₂), decidable_lt := by apply_instance, decidable_le := string.decidable_le, decidable_eq := by apply_instance, .. }; { simp [-not_le], introv, apply_field } end string
c199213003a94e70ebb724235f9b46282d6c33a7
492a7e27d49633a89f7ce6e1e28f676b062fcbc9
/src/monoidal_categories_reboot/drinfeld_centre.lean
18f4d579b2a4cf442e4382ad797a032a514b1052
[ "Apache-2.0" ]
permissive
semorrison/monoidal-categories-reboot
9edba30277de48a234b63813cf85b171772ce36f
48b5f1d535daba4e591672042a298ac36be2e6dd
refs/heads/master
1,642,472,396,149
1,560,587,477,000
1,560,587,477,000
156,465,626
0
1
null
1,541,549,278,000
1,541,549,278,000
null
UTF-8
Lean
false
false
4,129
lean
import .braided_monoidal_category import .pseudo_natural_transformation import category_theory.functor_category universes v v₁ v₂ u u₁ u₂ open category_theory namespace category_theory.monoidal variables {C : Type u} [𝒞 : monoidal_category.{v} C] include 𝒞 -- We give two versions, one abstract nonsense, as `(End (1 C))`, and the other concrete. -- They are not-so-far from definitionally equal. instance drinfeld_centre : braided_monoidal_category (pseudo_natural_transformation (monoidal_functor.id C) (monoidal_functor.id C)) := { braiding := λ X Y, -- As in the Eckmann-Hilton argument: by calc X ⊗ Y ≅ X.vcomp Y : by refl ... ≅ Y.vcomp X : sorry -- Argh, so many unitors. :-) ... ≅ Y ⊗ X : by refl, braiding_naturality' := sorry, hexagon_forward' := sorry, hexagon_reverse' := sorry } variables (C) structure Z := (X : C) (β : tensor_on_left.obj X ≅ tensor_on_right.obj X) variables {C} structure Z_hom (P Q : Z.{u v} C) := (hom : P.X ⟶ Q.X) (w' : ∀ Y : C, P.β.hom.app Y ≫ (𝟙 _ ⊗ hom) = (hom ⊗ 𝟙 _) ≫ Q.β.hom.app Y . obviously) restate_axiom Z_hom.w' attribute [search] Z_hom.w namespace Z_hom @[extensionality] lemma ext {P Q : Z.{u v} C} {f g : Z_hom P Q} (w : f.hom = g.hom) : f = g := begin cases f, cases g, congr, obviously, end def id (P : Z.{u v} C) : Z_hom P P := { hom := 𝟙 _ } @[simp] lemma id_hom (P : Z.{u v} C) : (id P).hom = 𝟙 P.X := rfl def comp {P Q R : Z.{u v} C} (f : Z_hom P Q) (g : Z_hom Q R) : Z_hom P R := { hom := f.hom ≫ g.hom } @[simp] lemma comp_hom {P Q R : Z.{u v} C} (f : Z_hom P Q) (g : Z_hom Q R) : (comp f g).hom = f.hom ≫ g.hom := rfl end Z_hom instance drinfeld_centre_category : category.{(max u v) v} (Z.{u v} C) := { hom := λ P Q, Z_hom P Q, id := λ P, Z_hom.id P, comp := λ P Q R f g, Z_hom.comp f g }. @[simp] lemma drinfeld_id_hom (P : Z.{u v} C) : (𝟙 P : Z_hom P P).hom = 𝟙 P.X := rfl @[simp] lemma drinfeld_comp_hom {P Q R : Z.{u v} C} (f : P ⟶ Q) (g : Q ⟶ R) : (f ≫ g).hom = f.hom ≫ g.hom := rfl -- TODO derive this from the faithful functor Z C ⥤ C instance Z_iso {P Q : Z.{u v} C} (f : P ⟶ Q) [is_iso f.hom] : is_iso f := sorry def Z_tensor_unit : Z.{u v} C := { X := tensor_unit C, β := iso.infer { app := λ Y, (left_unitor Y).hom ≫ (right_unitor Y).inv } }. set_option class.instance_max_depth 200 def Z_tensor_obj (P Q : Z.{u v} C) : Z.{u v} C := { X := P.X ⊗ Q.X, β := iso.infer { app := λ Y, (associator _ _ _).hom ≫ (𝟙 _ ⊗ (Q.β.hom.app Y)) ≫ (associator _ _ _).inv ≫ ((P.β.hom.app Y) ⊗ 𝟙 _) ≫ (associator _ _ _).hom, naturality' := begin tidy, sorry end } } @[simp] lemma Z_tensor_obj_β_hom_app (P Q : Z.{u v} C) (Y : C) : (Z_tensor_obj P Q).β.hom.app Y = (associator _ _ _).hom ≫ (𝟙 _ ⊗ (Q.β.hom.app Y)) ≫ (associator _ _ _).inv ≫ ((P.β.hom.app Y) ⊗ 𝟙 _) ≫ (associator _ _ _).hom := rfl def Z_tensor_hom {P Q R S : Z.{u v} C} (f : Z_hom P Q) (g : Z_hom R S) : Z_hom (Z_tensor_obj P R) (Z_tensor_obj Q S) := { hom := f.hom ⊗ g.hom, w' := begin tidy, sorry end }. @[simp] lemma Z_tensor_hom_hom {P Q R S : Z.{u v} C} (f : Z_hom P Q) (g : Z_hom R S) : (Z_tensor_hom f g).hom = f.hom ⊗ g.hom := rfl def Z_left_unitor (P : Z.{u v} C) : Z_tensor_obj Z_tensor_unit P ≅ P := iso.infer { hom := (left_unitor P.X).hom, w' := sorry /- works, but too slow -/ }. def Z_right_unitor (P : Z.{u v} C) : Z_tensor_obj P Z_tensor_unit ≅ P := iso.infer { hom := (right_unitor P.X).hom, w' := sorry /- works, but too slow -/ }. def Z_associator (P Q R : Z.{u v} C) : Z_tensor_obj (Z_tensor_obj P Q) R ≅ Z_tensor_obj P (Z_tensor_obj Q R) := iso.infer { hom := (associator P.X Q.X R.X).hom, w' := sorry /- times out :-( -/ }. instance : monoidal_category.{(max u v) v} (Z.{u v} C) := { tensor_unit := Z_tensor_unit, tensor_obj := Z_tensor_obj, tensor_hom := λ P Q R S f g, Z_tensor_hom f g, associator := λ P Q R, Z_associator P Q R, left_unitor := λ P, Z_left_unitor P, right_unitor := λ P, Z_right_unitor P, } end category_theory.monoidal
c8e1252998169c072deb7525cb40fac4a17820fe
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/category_theory/limits/preserves/basic_auto.lean
725e1a7e1bccac4a9c4cde3dc74d2c0ba9e51732
[]
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
46,685
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton, Bhavik Mehta -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.category_theory.limits.limits import Mathlib.PostPort universes v u₁ u₂ l u₃ namespace Mathlib /-! # Preservation and reflection of (co)limits. There are various distinct notions of "preserving limits". The one we aim to capture here is: A functor F : C → D "preserves limits" if it sends every limit cone in C to a limit cone in D. Informally, F preserves all the limits which exist in C. Note that: * Of course, we do not want to require F to *strictly* take chosen limit cones of C to chosen limit cones of D. Indeed, the above definition makes no reference to a choice of limit cones so it makes sense without any conditions on C or D. * Some diagrams in C may have no limit. In this case, there is no condition on the behavior of F on such diagrams. There are other notions (such as "flat functor") which impose conditions also on diagrams in C with no limits, but these are not considered here. In order to be able to express the property of preserving limits of a certain form, we say that a functor F preserves the limit of a diagram K if F sends every limit cone on K to a limit cone. This is vacuously satisfied when K does not admit a limit, which is consistent with the above definition of "preserves limits". -/ namespace category_theory.limits /-- A functor `F` preserves limits of `K` (written as `preserves_limit K F`) if `F` maps any limit cone over `K` to a limit cone. -/ class preserves_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) where preserves : {c : cone K} → is_limit c → is_limit (functor.map_cone F c) /-- A functor `F` preserves colimits of `K` (written as `preserves_colimit K F`) if `F` maps any colimit cocone over `K` to a colimit cocone. -/ class preserves_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) where preserves : {c : cocone K} → is_colimit c → is_colimit (functor.map_cocone F c) /-- We say that `F` preserves limits of shape `J` if `F` preserves limits for every diagram `K : J ⥤ C`, i.e., `F` maps limit cones over `K` to limit cones. -/ class preserves_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D) where preserves_limit : {K : J ⥤ C} → preserves_limit K F /-- We say that `F` preserves colimits of shape `J` if `F` preserves colimits for every diagram `K : J ⥤ C`, i.e., `F` maps colimit cocones over `K` to colimit cocones. -/ class preserves_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D) where preserves_colimit : {K : J ⥤ C} → preserves_colimit K F /-- We say that `F` preserves limits if it sends limit cones over any diagram to limit cones. -/ class preserves_limits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) where preserves_limits_of_shape : {J : Type v} → [𝒥 : small_category J] → preserves_limits_of_shape J F /-- We say that `F` preserves colimits if it sends colimit cocones over any diagram to colimit cocones.-/ class preserves_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) where preserves_colimits_of_shape : {J : Type v} → [𝒥 : small_category J] → preserves_colimits_of_shape J F /-- A convenience function for `preserves_limit`, which takes the functor as an explicit argument to guide typeclass resolution. -/ def is_limit_of_preserves {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} (F : C ⥤ D) {c : cone K} (t : is_limit c) [preserves_limit K F] : is_limit (functor.map_cone F c) := preserves_limit.preserves t /-- A convenience function for `preserves_colimit`, which takes the functor as an explicit argument to guide typeclass resolution. -/ def is_colimit_of_preserves {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} (F : C ⥤ D) {c : cocone K} (t : is_colimit c) [preserves_colimit K F] : is_colimit (functor.map_cocone F c) := preserves_colimit.preserves t protected instance preserves_limit_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) : subsingleton (preserves_limit K F) := subsingleton.intro fun (a b : preserves_limit K F) => preserves_limit.cases_on a fun (a : {c : cone K} → is_limit c → is_limit (functor.map_cone F c)) => preserves_limit.cases_on b fun (b : {c : cone K} → is_limit c → is_limit (functor.map_cone F c)) => (fun {K : J ⥤ C} {F : C ⥤ D} (preserves preserves_1 : {c : cone K} → is_limit c → is_limit (functor.map_cone F c)) => Eq.trans ((fun {K : J ⥤ C} {F : C ⥤ D} (preserves : {c : cone K} → is_limit c → is_limit (functor.map_cone F c)) => Eq.refl (preserves_limit.mk preserves)) preserves) (congr (Eq.refl preserves_limit.mk) (subsingleton.elim preserves preserves_1))) a b protected instance preserves_colimit_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) : subsingleton (preserves_colimit K F) := subsingleton.intro fun (a b : preserves_colimit K F) => preserves_colimit.cases_on a fun (a : {c : cocone K} → is_colimit c → is_colimit (functor.map_cocone F c)) => preserves_colimit.cases_on b fun (b : {c : cocone K} → is_colimit c → is_colimit (functor.map_cocone F c)) => (fun {K : J ⥤ C} {F : C ⥤ D} (preserves preserves_1 : {c : cocone K} → is_colimit c → is_colimit (functor.map_cocone F c)) => Eq.trans ((fun {K : J ⥤ C} {F : C ⥤ D} (preserves : {c : cocone K} → is_colimit c → is_colimit (functor.map_cocone F c)) => Eq.refl (preserves_colimit.mk preserves)) preserves) (congr (Eq.refl preserves_colimit.mk) (subsingleton.elim preserves preserves_1))) a b protected instance preserves_limits_of_shape_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D) : subsingleton (preserves_limits_of_shape J F) := subsingleton.intro fun (a b : preserves_limits_of_shape J F) => preserves_limits_of_shape.cases_on a fun (a : {K : J ⥤ C} → preserves_limit K F) => preserves_limits_of_shape.cases_on b fun (b : {K : J ⥤ C} → preserves_limit K F) => (fun [_inst_4 : small_category J] {F : C ⥤ D} (preserves_limit preserves_limit_1 : {K : J ⥤ C} → preserves_limit K F) => Eq.trans ((fun [_inst_4 : small_category J] {F : C ⥤ D} (preserves_limit : {K : J ⥤ C} → preserves_limit K F) => Eq.refl (preserves_limits_of_shape.mk preserves_limit)) preserves_limit) (congr (Eq.refl preserves_limits_of_shape.mk) (subsingleton.elim preserves_limit preserves_limit_1))) a b protected instance preserves_colimits_of_shape_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D) : subsingleton (preserves_colimits_of_shape J F) := subsingleton.intro fun (a b : preserves_colimits_of_shape J F) => preserves_colimits_of_shape.cases_on a fun (a : {K : J ⥤ C} → preserves_colimit K F) => preserves_colimits_of_shape.cases_on b fun (b : {K : J ⥤ C} → preserves_colimit K F) => (fun [_inst_4 : small_category J] {F : C ⥤ D} (preserves_colimit preserves_colimit_1 : {K : J ⥤ C} → preserves_colimit K F) => Eq.trans ((fun [_inst_4 : small_category J] {F : C ⥤ D} (preserves_colimit : {K : J ⥤ C} → preserves_colimit K F) => Eq.refl (preserves_colimits_of_shape.mk preserves_colimit)) preserves_colimit) (congr (Eq.refl preserves_colimits_of_shape.mk) (subsingleton.elim preserves_colimit preserves_colimit_1))) a b protected instance preserves_limits_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) : subsingleton (preserves_limits F) := subsingleton.intro fun (a b : preserves_limits F) => preserves_limits.cases_on a fun (a : {J : Type v} → [𝒥 : small_category J] → preserves_limits_of_shape J F) => preserves_limits.cases_on b fun (b : {J : Type v} → [𝒥 : small_category J] → preserves_limits_of_shape J F) => of_eq_true (eq_true_intro (eq_of_heq ((fun (preserves_limits_of_shape preserves_limits_of_shape' : {J : Type v} → [𝒥 : small_category J] → preserves_limits_of_shape J F) (e_0 : preserves_limits_of_shape = preserves_limits_of_shape') => Eq._oldrec (HEq.refl (preserves_limits.mk preserves_limits_of_shape)) e_0) a b (Eq.symm (subsingleton.elim b a))))) protected instance preserves_colimits_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) : subsingleton (preserves_colimits F) := subsingleton.intro fun (a b : preserves_colimits F) => preserves_colimits.cases_on a fun (a : {J : Type v} → [𝒥 : small_category J] → preserves_colimits_of_shape J F) => preserves_colimits.cases_on b fun (b : {J : Type v} → [𝒥 : small_category J] → preserves_colimits_of_shape J F) => of_eq_true (eq_true_intro (eq_of_heq ((fun (preserves_colimits_of_shape preserves_colimits_of_shape' : {J : Type v} → [𝒥 : small_category J] → preserves_colimits_of_shape J F) (e_0 : preserves_colimits_of_shape = preserves_colimits_of_shape') => Eq._oldrec (HEq.refl (preserves_colimits.mk preserves_colimits_of_shape)) e_0) a b (Eq.symm (subsingleton.elim b a))))) protected instance id_preserves_limits {C : Type u₁} [category C] : preserves_limits 𝟭 := preserves_limits.mk fun (J : Type v) (𝒥 : small_category J) => preserves_limits_of_shape.mk fun (K : J ⥤ C) => preserves_limit.mk fun (c : cone K) (h : is_limit c) => is_limit.mk fun (s : cone (K ⋙ 𝟭)) => is_limit.lift h (cone.mk (cone.X s) (nat_trans.mk fun (j : J) => nat_trans.app (cone.π s) j)) protected instance id_preserves_colimits {C : Type u₁} [category C] : preserves_colimits 𝟭 := preserves_colimits.mk fun (J : Type v) (𝒥 : small_category J) => preserves_colimits_of_shape.mk fun (K : J ⥤ C) => preserves_colimit.mk fun (c : cocone K) (h : is_colimit c) => is_colimit.mk fun (s : cocone (K ⋙ 𝟭)) => is_colimit.desc h (cocone.mk (cocone.X s) (nat_trans.mk fun (j : J) => nat_trans.app (cocone.ι s) j)) protected instance comp_preserves_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_limit K F] [preserves_limit (K ⋙ F) G] : preserves_limit K (F ⋙ G) := preserves_limit.mk fun (c : cone K) (h : is_limit c) => preserves_limit.preserves (preserves_limit.preserves h) protected instance comp_preserves_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_limits_of_shape J F] [preserves_limits_of_shape J G] : preserves_limits_of_shape J (F ⋙ G) := preserves_limits_of_shape.mk fun (K : J ⥤ C) => infer_instance protected instance comp_preserves_limits {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_limits F] [preserves_limits G] : preserves_limits (F ⋙ G) := preserves_limits.mk fun (J : Type v) (𝒥₁ : small_category J) => infer_instance protected instance comp_preserves_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_colimit K F] [preserves_colimit (K ⋙ F) G] : preserves_colimit K (F ⋙ G) := preserves_colimit.mk fun (c : cocone K) (h : is_colimit c) => preserves_colimit.preserves (preserves_colimit.preserves h) protected instance comp_preserves_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_colimits_of_shape J F] [preserves_colimits_of_shape J G] : preserves_colimits_of_shape J (F ⋙ G) := preserves_colimits_of_shape.mk fun (K : J ⥤ C) => infer_instance protected instance comp_preserves_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_colimits F] [preserves_colimits G] : preserves_colimits (F ⋙ G) := preserves_colimits.mk fun (J : Type v) (𝒥₁ : small_category J) => infer_instance /-- If F preserves one limit cone for the diagram K, then it preserves any limit cone for K. -/ def preserves_limit_of_preserves_limit_cone {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} {t : cone K} (h : is_limit t) (hF : is_limit (functor.map_cone F t)) : preserves_limit K F := preserves_limit.mk fun (t' : cone K) (h' : is_limit t') => is_limit.of_iso_limit hF (functor.map_iso (cones.functoriality K F) (is_limit.unique_up_to_iso h h')) /-- Transfer preservation of limits along a natural isomorphism in the diagram. -/ def preserves_limit_of_iso_diagram {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K₁ : J ⥤ C} {K₂ : J ⥤ C} (F : C ⥤ D) (h : K₁ ≅ K₂) [preserves_limit K₁ F] : preserves_limit K₂ F := preserves_limit.mk fun (c : cone K₂) (t : is_limit c) => coe_fn (is_limit.postcompose_inv_equiv (iso_whisker_right h F) (functor.map_cone F c)) (is_limit.of_iso_limit (is_limit_of_preserves F (coe_fn (equiv.symm (is_limit.postcompose_inv_equiv h c)) t)) (cones.ext (iso.refl (cone.X (functor.map_cone F (functor.obj (cones.postcompose (iso.inv h)) c)))) sorry)) /-- Transfer preservation of a limit along a natural isomorphism in the functor. -/ def preserves_limit_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [preserves_limit K F] : preserves_limit K G := preserves_limit.mk fun (c : cone K) (t : is_limit c) => is_limit.map_cone_equiv h (preserves_limit.preserves t) /-- Transfer preservation of limits of shape along a natural isomorphism in the functor. -/ def preserves_limits_of_shape_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [preserves_limits_of_shape J F] : preserves_limits_of_shape J G := preserves_limits_of_shape.mk fun (K : J ⥤ C) => preserves_limit_of_nat_iso K h /-- Transfer preservation of limits along a natural isomorphism in the functor. -/ def preserves_limits_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [preserves_limits F] : preserves_limits G := preserves_limits.mk fun (J : Type v) (𝒥₁ : small_category J) => preserves_limits_of_shape_of_nat_iso h /-- Transfer preservation of limits along a equivalence in the shape. -/ def preserves_limits_of_shape_of_equiv {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {J' : Type v} [small_category J'] (e : J ≌ J') (F : C ⥤ D) [preserves_limits_of_shape J F] : preserves_limits_of_shape J' F := preserves_limits_of_shape.mk fun (K : J' ⥤ C) => preserves_limit.mk fun (c : cone K) (t : is_limit c) => let equ : equivalence.inverse e ⋙ equivalence.functor e ⋙ K ⋙ F ≅ K ⋙ F := equivalence.inv_fun_id_assoc e (K ⋙ F); is_limit.of_iso_limit (coe_fn (equiv.symm (is_limit.postcompose_hom_equiv equ (cone.whisker (equivalence.functor (equivalence.symm e)) (functor.map_cone F (cone.whisker (equivalence.functor e) c))))) (is_limit.whisker_equivalence (is_limit_of_preserves F (is_limit.whisker_equivalence t e)) (equivalence.symm e))) (cones.ext (iso.refl (cone.X (functor.obj (cones.postcompose (iso.hom equ)) (cone.whisker (equivalence.functor (equivalence.symm e)) (functor.map_cone F (cone.whisker (equivalence.functor e) c)))))) sorry) /-- If F preserves one colimit cocone for the diagram K, then it preserves any colimit cocone for K. -/ def preserves_colimit_of_preserves_colimit_cocone {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {F : C ⥤ D} {t : cocone K} (h : is_colimit t) (hF : is_colimit (functor.map_cocone F t)) : preserves_colimit K F := preserves_colimit.mk fun (t' : cocone K) (h' : is_colimit t') => is_colimit.of_iso_colimit hF (functor.map_iso (cocones.functoriality K F) (is_colimit.unique_up_to_iso h h')) /-- Transfer preservation of colimits along a natural isomorphism in the shape. -/ def preserves_colimit_of_iso_diagram {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K₁ : J ⥤ C} {K₂ : J ⥤ C} (F : C ⥤ D) (h : K₁ ≅ K₂) [preserves_colimit K₁ F] : preserves_colimit K₂ F := preserves_colimit.mk fun (c : cocone K₂) (t : is_colimit c) => coe_fn (is_colimit.precompose_hom_equiv (iso_whisker_right h F) (functor.map_cocone F c)) (is_colimit.of_iso_colimit (is_colimit_of_preserves F (coe_fn (equiv.symm (is_colimit.precompose_hom_equiv h c)) t)) (cocones.ext (iso.refl (cocone.X (functor.map_cocone F (functor.obj (cocones.precompose (iso.hom h)) c)))) sorry)) /-- Transfer preservation of a colimit along a natural isomorphism in the functor. -/ def preserves_colimit_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [preserves_colimit K F] : preserves_colimit K G := preserves_colimit.mk fun (c : cocone K) (t : is_colimit c) => is_colimit.map_cocone_equiv h (preserves_colimit.preserves t) /-- Transfer preservation of colimits of shape along a natural isomorphism in the functor. -/ def preserves_colimits_of_shape_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [preserves_colimits_of_shape J F] : preserves_colimits_of_shape J G := preserves_colimits_of_shape.mk fun (K : J ⥤ C) => preserves_colimit_of_nat_iso K h /-- Transfer preservation of colimits along a natural isomorphism in the functor. -/ def preserves_colimits_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [preserves_colimits F] : preserves_colimits G := preserves_colimits.mk fun (J : Type v) (𝒥₁ : small_category J) => preserves_colimits_of_shape_of_nat_iso h /-- Transfer preservation of colimits along a equivalence in the shape. -/ def preserves_colimits_of_shape_of_equiv {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {J' : Type v} [small_category J'] (e : J ≌ J') (F : C ⥤ D) [preserves_colimits_of_shape J F] : preserves_colimits_of_shape J' F := sorry /-- A functor `F : C ⥤ D` reflects limits for `K : J ⥤ C` if whenever the image of a cone over `K` under `F` is a limit cone in `D`, the cone was already a limit cone in `C`. Note that we do not assume a priori that `D` actually has any limits. -/ class reflects_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) where reflects : {c : cone K} → is_limit (functor.map_cone F c) → is_limit c /-- A functor `F : C ⥤ D` reflects colimits for `K : J ⥤ C` if whenever the image of a cocone over `K` under `F` is a colimit cocone in `D`, the cocone was already a colimit cocone in `C`. Note that we do not assume a priori that `D` actually has any colimits. -/ class reflects_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) where reflects : {c : cocone K} → is_colimit (functor.map_cocone F c) → is_colimit c /-- A functor `F : C ⥤ D` reflects limits of shape `J` if whenever the image of a cone over some `K : J ⥤ C` under `F` is a limit cone in `D`, the cone was already a limit cone in `C`. Note that we do not assume a priori that `D` actually has any limits. -/ class reflects_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D) where reflects_limit : {K : J ⥤ C} → reflects_limit K F /-- A functor `F : C ⥤ D` reflects colimits of shape `J` if whenever the image of a cocone over some `K : J ⥤ C` under `F` is a colimit cocone in `D`, the cocone was already a colimit cocone in `C`. Note that we do not assume a priori that `D` actually has any colimits. -/ class reflects_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D) where reflects_colimit : {K : J ⥤ C} → reflects_colimit K F /-- A functor `F : C ⥤ D` reflects limits if whenever the image of a cone over some `K : J ⥤ C` under `F` is a limit cone in `D`, the cone was already a limit cone in `C`. Note that we do not assume a priori that `D` actually has any limits. -/ class reflects_limits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) where reflects_limits_of_shape : {J : Type v} → {𝒥 : small_category J} → reflects_limits_of_shape J F /-- A functor `F : C ⥤ D` reflects colimits if whenever the image of a cocone over some `K : J ⥤ C` under `F` is a colimit cocone in `D`, the cocone was already a colimit cocone in `C`. Note that we do not assume a priori that `D` actually has any colimits. -/ class reflects_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) where reflects_colimits_of_shape : {J : Type v} → {𝒥 : small_category J} → reflects_colimits_of_shape J F /-- A convenience function for `reflects_limit`, which takes the functor as an explicit argument to guide typeclass resolution. -/ def is_limit_of_reflects {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} (F : C ⥤ D) {c : cone K} (t : is_limit (functor.map_cone F c)) [reflects_limit K F] : is_limit c := reflects_limit.reflects t /-- A convenience function for `reflects_colimit`, which takes the functor as an explicit argument to guide typeclass resolution. -/ def is_colimit_of_reflects {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} (F : C ⥤ D) {c : cocone K} (t : is_colimit (functor.map_cocone F c)) [reflects_colimit K F] : is_colimit c := reflects_colimit.reflects t protected instance reflects_limit_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) : subsingleton (reflects_limit K F) := subsingleton.intro fun (a b : reflects_limit K F) => reflects_limit.cases_on a fun (a : {c : cone K} → is_limit (functor.map_cone F c) → is_limit c) => reflects_limit.cases_on b fun (b : {c : cone K} → is_limit (functor.map_cone F c) → is_limit c) => (fun {K : J ⥤ C} {F : C ⥤ D} (reflects reflects_1 : {c : cone K} → is_limit (functor.map_cone F c) → is_limit c) => Eq.trans ((fun {K : J ⥤ C} {F : C ⥤ D} (reflects : {c : cone K} → is_limit (functor.map_cone F c) → is_limit c) => Eq.refl (reflects_limit.mk reflects)) reflects) (congr (Eq.refl reflects_limit.mk) (subsingleton.elim reflects reflects_1))) a b protected instance reflects_colimit_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) : subsingleton (reflects_colimit K F) := subsingleton.intro fun (a b : reflects_colimit K F) => reflects_colimit.cases_on a fun (a : {c : cocone K} → is_colimit (functor.map_cocone F c) → is_colimit c) => reflects_colimit.cases_on b fun (b : {c : cocone K} → is_colimit (functor.map_cocone F c) → is_colimit c) => (fun {K : J ⥤ C} {F : C ⥤ D} (reflects reflects_1 : {c : cocone K} → is_colimit (functor.map_cocone F c) → is_colimit c) => Eq.trans ((fun {K : J ⥤ C} {F : C ⥤ D} (reflects : {c : cocone K} → is_colimit (functor.map_cocone F c) → is_colimit c) => Eq.refl (reflects_colimit.mk reflects)) reflects) (congr (Eq.refl reflects_colimit.mk) (subsingleton.elim reflects reflects_1))) a b protected instance reflects_limits_of_shape_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D) : subsingleton (reflects_limits_of_shape J F) := subsingleton.intro fun (a b : reflects_limits_of_shape J F) => reflects_limits_of_shape.cases_on a fun (a : {K : J ⥤ C} → reflects_limit K F) => reflects_limits_of_shape.cases_on b fun (b : {K : J ⥤ C} → reflects_limit K F) => (fun [_inst_4 : small_category J] {F : C ⥤ D} (reflects_limit reflects_limit_1 : {K : J ⥤ C} → reflects_limit K F) => Eq.trans ((fun [_inst_4 : small_category J] {F : C ⥤ D} (reflects_limit : {K : J ⥤ C} → reflects_limit K F) => Eq.refl (reflects_limits_of_shape.mk reflects_limit)) reflects_limit) (congr (Eq.refl reflects_limits_of_shape.mk) (subsingleton.elim reflects_limit reflects_limit_1))) a b protected instance reflects_colimits_of_shape_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] (J : Type v) [small_category J] (F : C ⥤ D) : subsingleton (reflects_colimits_of_shape J F) := subsingleton.intro fun (a b : reflects_colimits_of_shape J F) => reflects_colimits_of_shape.cases_on a fun (a : {K : J ⥤ C} → reflects_colimit K F) => reflects_colimits_of_shape.cases_on b fun (b : {K : J ⥤ C} → reflects_colimit K F) => (fun [_inst_4 : small_category J] {F : C ⥤ D} (reflects_colimit reflects_colimit_1 : {K : J ⥤ C} → reflects_colimit K F) => Eq.trans ((fun [_inst_4 : small_category J] {F : C ⥤ D} (reflects_colimit : {K : J ⥤ C} → reflects_colimit K F) => Eq.refl (reflects_colimits_of_shape.mk reflects_colimit)) reflects_colimit) (congr (Eq.refl reflects_colimits_of_shape.mk) (subsingleton.elim reflects_colimit reflects_colimit_1))) a b protected instance reflects_limits_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) : subsingleton (reflects_limits F) := subsingleton.intro fun (a b : reflects_limits F) => reflects_limits.cases_on a fun (a : {J : Type v} → {𝒥 : small_category J} → reflects_limits_of_shape J F) => reflects_limits.cases_on b fun (b : {J : Type v} → {𝒥 : small_category J} → reflects_limits_of_shape J F) => of_eq_true (eq_true_intro (eq_of_heq ((fun (reflects_limits_of_shape reflects_limits_of_shape' : {J : Type v} → {𝒥 : small_category J} → reflects_limits_of_shape J F) (e_0 : reflects_limits_of_shape = reflects_limits_of_shape') => Eq._oldrec (HEq.refl (reflects_limits.mk reflects_limits_of_shape)) e_0) a b (Eq.symm (subsingleton.elim b a))))) protected instance reflects_colimits_subsingleton {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) : subsingleton (reflects_colimits F) := subsingleton.intro fun (a b : reflects_colimits F) => reflects_colimits.cases_on a fun (a : {J : Type v} → {𝒥 : small_category J} → reflects_colimits_of_shape J F) => reflects_colimits.cases_on b fun (b : {J : Type v} → {𝒥 : small_category J} → reflects_colimits_of_shape J F) => of_eq_true (eq_true_intro (eq_of_heq ((fun (reflects_colimits_of_shape reflects_colimits_of_shape' : {J : Type v} → {𝒥 : small_category J} → reflects_colimits_of_shape J F) (e_0 : reflects_colimits_of_shape = reflects_colimits_of_shape') => Eq._oldrec (HEq.refl (reflects_colimits.mk reflects_colimits_of_shape)) e_0) a b (Eq.symm (subsingleton.elim b a))))) protected instance reflects_limit_of_reflects_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [H : reflects_limits_of_shape J F] : reflects_limit K F := reflects_limits_of_shape.reflects_limit protected instance reflects_colimit_of_reflects_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) (F : C ⥤ D) [H : reflects_colimits_of_shape J F] : reflects_colimit K F := reflects_colimits_of_shape.reflects_colimit protected instance reflects_limits_of_shape_of_reflects_limits {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (F : C ⥤ D) [H : reflects_limits F] : reflects_limits_of_shape J F := reflects_limits.reflects_limits_of_shape protected instance reflects_colimits_of_shape_of_reflects_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (F : C ⥤ D) [H : reflects_colimits F] : reflects_colimits_of_shape J F := reflects_colimits.reflects_colimits_of_shape protected instance id_reflects_limits {C : Type u₁} [category C] : reflects_limits 𝟭 := reflects_limits.mk fun (J : Type v) (𝒥 : small_category J) => reflects_limits_of_shape.mk fun (K : J ⥤ C) => reflects_limit.mk fun (c : cone K) (h : is_limit (functor.map_cone 𝟭 c)) => is_limit.mk fun (s : cone K) => is_limit.lift h (cone.mk (cone.X s) (nat_trans.mk fun (j : J) => nat_trans.app (cone.π s) j)) protected instance id_reflects_colimits {C : Type u₁} [category C] : reflects_colimits 𝟭 := reflects_colimits.mk fun (J : Type v) (𝒥 : small_category J) => reflects_colimits_of_shape.mk fun (K : J ⥤ C) => reflects_colimit.mk fun (c : cocone K) (h : is_colimit (functor.map_cocone 𝟭 c)) => is_colimit.mk fun (s : cocone K) => is_colimit.desc h (cocone.mk (cocone.X s) (nat_trans.mk fun (j : J) => nat_trans.app (cocone.ι s) j)) protected instance comp_reflects_limit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [reflects_limit K F] [reflects_limit (K ⋙ F) G] : reflects_limit K (F ⋙ G) := reflects_limit.mk fun (c : cone K) (h : is_limit (functor.map_cone (F ⋙ G) c)) => reflects_limit.reflects (reflects_limit.reflects h) protected instance comp_reflects_limits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [reflects_limits_of_shape J F] [reflects_limits_of_shape J G] : reflects_limits_of_shape J (F ⋙ G) := reflects_limits_of_shape.mk fun (K : J ⥤ C) => infer_instance protected instance comp_reflects_limits {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [reflects_limits F] [reflects_limits G] : reflects_limits (F ⋙ G) := reflects_limits.mk fun (J : Type v) (𝒥₁ : small_category J) => infer_instance protected instance comp_reflects_colimit {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [reflects_colimit K F] [reflects_colimit (K ⋙ F) G] : reflects_colimit K (F ⋙ G) := reflects_colimit.mk fun (c : cocone K) (h : is_colimit (functor.map_cocone (F ⋙ G) c)) => reflects_colimit.reflects (reflects_colimit.reflects h) protected instance comp_reflects_colimits_of_shape {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [reflects_colimits_of_shape J F] [reflects_colimits_of_shape J G] : reflects_colimits_of_shape J (F ⋙ G) := reflects_colimits_of_shape.mk fun (K : J ⥤ C) => infer_instance protected instance comp_reflects_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [reflects_colimits F] [reflects_colimits G] : reflects_colimits (F ⋙ G) := reflects_colimits.mk fun (J : Type v) (𝒥₁ : small_category J) => infer_instance /-- If `F ⋙ G` preserves limits for `K`, and `G` reflects limits for `K ⋙ F`, then `F` preserves limits for `K`. -/ def preserves_limit_of_reflects_of_preserves {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_limit K (F ⋙ G)] [reflects_limit (K ⋙ F) G] : preserves_limit K F := preserves_limit.mk fun (c : cone K) (h : is_limit c) => is_limit_of_reflects G (is_limit_of_preserves (F ⋙ G) h) /-- If `F ⋙ G` preserves limits of shape `J` and `G` reflects limits of shape `J`, then `F` preserves limits of shape `J`. -/ def preserves_limits_of_shape_of_reflects_of_preserves {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_limits_of_shape J (F ⋙ G)] [reflects_limits_of_shape J G] : preserves_limits_of_shape J F := preserves_limits_of_shape.mk fun (K : J ⥤ C) => preserves_limit_of_reflects_of_preserves F G /-- If `F ⋙ G` preserves limits and `G` reflects limits, then `F` preserves limits. -/ def preserves_limits_of_reflects_of_preserves {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_limits (F ⋙ G)] [reflects_limits G] : preserves_limits F := preserves_limits.mk fun (J : Type v) (𝒥₁ : small_category J) => preserves_limits_of_shape_of_reflects_of_preserves F G /-- Transfer reflection of a limit along a natural isomorphism in the functor. -/ def reflects_limit_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [reflects_limit K F] : reflects_limit K G := reflects_limit.mk fun (c : cone K) (t : is_limit (functor.map_cone G c)) => reflects_limit.reflects (is_limit.map_cone_equiv (iso.symm h) t) /-- Transfer reflection of limits of shape along a natural isomorphism in the functor. -/ def reflects_limits_of_shape_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [reflects_limits_of_shape J F] : reflects_limits_of_shape J G := reflects_limits_of_shape.mk fun (K : J ⥤ C) => reflects_limit_of_nat_iso K h /-- Transfer reflection of limits along a natural isomorphism in the functor. -/ def reflects_limits_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [reflects_limits F] : reflects_limits G := reflects_limits.mk fun (J : Type v) (𝒥₁ : small_category J) => reflects_limits_of_shape_of_nat_iso h /-- If the limit of `F` exists and `G` preserves it, then if `G` reflects isomorphisms then it reflects the limit of `F`. -/ def reflects_limit_of_reflects_isomorphisms {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (F : J ⥤ C) (G : C ⥤ D) [reflects_isomorphisms G] [has_limit F] [preserves_limit F G] : reflects_limit F G := reflects_limit.mk fun (c : cone F) (t : is_limit (functor.map_cone G c)) => is_limit.of_point_iso (limit.is_limit F) /-- If `C` has limits of shape `J` and `G` preserves them, then if `G` reflects isomorphisms then it reflects limits of shape `J`. -/ def reflects_limits_of_shape_of_reflects_isomorphisms {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {G : C ⥤ D} [reflects_isomorphisms G] [has_limits_of_shape J C] [preserves_limits_of_shape J G] : reflects_limits_of_shape J G := reflects_limits_of_shape.mk fun (F : J ⥤ C) => reflects_limit_of_reflects_isomorphisms F G /-- If `C` has limits and `G` preserves limits, then if `G` reflects isomorphisms then it reflects limits. -/ def reflects_limits_of_reflects_isomorphisms {C : Type u₁} [category C] {D : Type u₂} [category D] {G : C ⥤ D} [reflects_isomorphisms G] [has_limits C] [preserves_limits G] : reflects_limits G := reflects_limits.mk fun (J : Type v) (𝒥₁ : small_category J) => reflects_limits_of_shape_of_reflects_isomorphisms /-- If `F ⋙ G` preserves colimits for `K`, and `G` reflects colimits for `K ⋙ F`, then `F` preserves colimits for `K`. -/ def preserves_colimit_of_reflects_of_preserves {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {K : J ⥤ C} {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_colimit K (F ⋙ G)] [reflects_colimit (K ⋙ F) G] : preserves_colimit K F := preserves_colimit.mk fun (c : cocone K) (h : is_colimit c) => is_colimit_of_reflects G (is_colimit_of_preserves (F ⋙ G) h) /-- If `F ⋙ G` preserves colimits of shape `J` and `G` reflects colimits of shape `J`, then `F` preserves colimits of shape `J`. -/ def preserves_colimits_of_shape_of_reflects_of_preserves {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_colimits_of_shape J (F ⋙ G)] [reflects_colimits_of_shape J G] : preserves_colimits_of_shape J F := preserves_colimits_of_shape.mk fun (K : J ⥤ C) => preserves_colimit_of_reflects_of_preserves F G /-- If `F ⋙ G` preserves colimits and `G` reflects colimits, then `F` preserves colimits. -/ def preserves_colimits_of_reflects_of_preserves {C : Type u₁} [category C] {D : Type u₂} [category D] {E : Type u₃} [ℰ : category E] (F : C ⥤ D) (G : D ⥤ E) [preserves_colimits (F ⋙ G)] [reflects_colimits G] : preserves_colimits F := preserves_colimits.mk fun (J : Type v) (𝒥₁ : small_category J) => preserves_colimits_of_shape_of_reflects_of_preserves F G /-- Transfer reflection of a colimit along a natural isomorphism in the functor. -/ def reflects_colimit_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (K : J ⥤ C) {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [reflects_colimit K F] : reflects_colimit K G := reflects_colimit.mk fun (c : cocone K) (t : is_colimit (functor.map_cocone G c)) => reflects_colimit.reflects (is_colimit.map_cocone_equiv (iso.symm h) t) /-- Transfer reflection of colimits of shape along a natural isomorphism in the functor. -/ def reflects_colimits_of_shape_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [reflects_colimits_of_shape J F] : reflects_colimits_of_shape J G := reflects_colimits_of_shape.mk fun (K : J ⥤ C) => reflects_colimit_of_nat_iso K h /-- Transfer reflection of colimits along a natural isomorphism in the functor. -/ def reflects_colimits_of_nat_iso {C : Type u₁} [category C] {D : Type u₂} [category D] {F : C ⥤ D} {G : C ⥤ D} (h : F ≅ G) [reflects_colimits F] : reflects_colimits G := reflects_colimits.mk fun (J : Type v) (𝒥₁ : small_category J) => reflects_colimits_of_shape_of_nat_iso h /-- If the colimit of `F` exists and `G` preserves it, then if `G` reflects isomorphisms then it reflects the colimit of `F`. -/ def reflects_colimit_of_reflects_isomorphisms {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] (F : J ⥤ C) (G : C ⥤ D) [reflects_isomorphisms G] [has_colimit F] [preserves_colimit F G] : reflects_colimit F G := reflects_colimit.mk fun (c : cocone F) (t : is_colimit (functor.map_cocone G c)) => is_colimit.of_point_iso (colimit.is_colimit F) /-- If `C` has colimits of shape `J` and `G` preserves them, then if `G` reflects isomorphisms then it reflects colimits of shape `J`. -/ def reflects_colimits_of_shape_of_reflects_isomorphisms {C : Type u₁} [category C] {D : Type u₂} [category D] {J : Type v} [small_category J] {G : C ⥤ D} [reflects_isomorphisms G] [has_colimits_of_shape J C] [preserves_colimits_of_shape J G] : reflects_colimits_of_shape J G := reflects_colimits_of_shape.mk fun (F : J ⥤ C) => reflects_colimit_of_reflects_isomorphisms F G /-- If `C` has colimits and `G` preserves colimits, then if `G` reflects isomorphisms then it reflects colimits. -/ def reflects_colimits_of_reflects_isomorphisms {C : Type u₁} [category C] {D : Type u₂} [category D] {G : C ⥤ D} [reflects_isomorphisms G] [has_colimits C] [preserves_colimits G] : reflects_colimits G := reflects_colimits.mk fun (J : Type v) (𝒥₁ : small_category J) => reflects_colimits_of_shape_of_reflects_isomorphisms /-- A fully faithful functor reflects limits. -/ def fully_faithful_reflects_limits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) [full F] [faithful F] : reflects_limits F := reflects_limits.mk fun (J : Type v) (𝒥₁ : small_category J) => reflects_limits_of_shape.mk fun (K : J ⥤ C) => reflects_limit.mk fun (c : cone K) (t : is_limit (functor.map_cone F c)) => is_limit.mk_cone_morphism (fun (s : cone K) => functor.preimage (cones.functoriality K F) (is_limit.lift_cone_morphism t (functor.obj (cones.functoriality K F) s))) sorry /-- A fully faithful functor reflects colimits. -/ def fully_faithful_reflects_colimits {C : Type u₁} [category C] {D : Type u₂} [category D] (F : C ⥤ D) [full F] [faithful F] : reflects_colimits F := reflects_colimits.mk fun (J : Type v) (𝒥₁ : small_category J) => reflects_colimits_of_shape.mk fun (K : J ⥤ C) => reflects_colimit.mk fun (c : cocone K) (t : is_colimit (functor.map_cocone F c)) => is_colimit.mk_cocone_morphism (fun (s : cocone K) => functor.preimage (cocones.functoriality K F) (is_colimit.desc_cocone_morphism t (functor.obj (cocones.functoriality K F) s))) sorry end Mathlib
7a8199fe4133b31c7459ef590e1766c5ecfeea4b
75db7e3219bba2fbf41bf5b905f34fcb3c6ca3f2
/tests/lean/run/class3.lean
2b3b0f8076fed5d5769f1189ee26e50600df5e95
[ "Apache-2.0" ]
permissive
jroesch/lean
30ef0860fa905d35b9ad6f76de1a4f65c9af6871
3de4ec1a6ce9a960feb2a48eeea8b53246fa34f2
refs/heads/master
1,586,090,835,348
1,455,142,203,000
1,455,142,277,000
51,536,958
1
0
null
1,455,215,811,000
1,455,215,811,000
null
UTF-8
Lean
false
false
271
lean
import logic data.prod open prod inhabited section variable {A : Type} variable {B : Type} variable Ha : inhabited A variable Hb : inhabited B include Ha Hb theorem tst : inhabited (Prop × A × B) end reveal tst (* print(get_env():find("tst"):value()) *)
f502bc88cacd0027c849495f30d4445619b851b1
c777c32c8e484e195053731103c5e52af26a25d1
/src/analysis/sum_integral_comparisons.lean
611b475c6490d20f0c90b6193996326bbff147a1
[ "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
7,040
lean
/- Copyright (c) 2022 Kevin H. Wilson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin H. Wilson -/ import measure_theory.integral.interval_integral import data.set.function import analysis.special_functions.integrals /-! # Comparing sums and integrals ## Summary It is often the case that error terms in analysis can be computed by comparing an infinite sum to the improper integral of an antitone function. This file will eventually enable that. At the moment it contains four lemmas in this direction: `antitone_on.integral_le_sum`, `antitone_on.sum_le_integral` and versions for monotone functions, which can all be paired with a `filter.tendsto` to estimate some errors. `TODO`: Add more lemmas to the API to directly address limiting issues ## Main Results * `antitone_on.integral_le_sum`: The integral of an antitone function is at most the sum of its values at integer steps aligning with the left-hand side of the interval * `antitone_on.sum_le_integral`: The sum of an antitone function along integer steps aligning with the right-hand side of the interval is at most the integral of the function along that interval * `monotone_on.integral_le_sum`: The integral of a monotone function is at most the sum of its values at integer steps aligning with the right-hand side of the interval * `monotone_on.sum_le_integral`: The sum of a monotone function along integer steps aligning with the left-hand side of the interval is at most the integral of the function along that interval ## Tags analysis, comparison, asymptotics -/ open set measure_theory.measure_space open_locale big_operators variables {x₀ : ℝ} {a b : ℕ} {f : ℝ → ℝ} lemma antitone_on.integral_le_sum (hf : antitone_on f (Icc x₀ (x₀ + a))) : ∫ x in x₀..(x₀ + a), f x ≤ ∑ i in finset.range a, f (x₀ + i) := begin have hint : ∀ (k : ℕ), k < a → interval_integrable f volume (x₀+k) (x₀ + (k + 1 : ℕ)), { assume k hk, refine (hf.mono _).interval_integrable, rw uIcc_of_le, { apply Icc_subset_Icc, { simp only [le_add_iff_nonneg_right, nat.cast_nonneg] }, { simp only [add_le_add_iff_left, nat.cast_le, nat.succ_le_of_lt hk] } }, { simp only [add_le_add_iff_left, nat.cast_le, nat.le_succ] } }, calc ∫ x in x₀..(x₀ + a), f x = ∑ i in finset.range a, ∫ x in (x₀+i)..(x₀+(i+1 : ℕ)), f x : begin convert (interval_integral.sum_integral_adjacent_intervals hint).symm, simp only [nat.cast_zero, add_zero], end ... ≤ ∑ i in finset.range a, ∫ x in (x₀+i)..(x₀+(i+1 : ℕ)), f (x₀ + i) : begin apply finset.sum_le_sum (λ i hi, _), have ia : i < a := finset.mem_range.1 hi, refine interval_integral.integral_mono_on (by simp) (hint _ ia) (by simp) (λ x hx, _), apply hf _ _ hx.1, { simp only [ia.le, mem_Icc, le_add_iff_nonneg_right, nat.cast_nonneg, add_le_add_iff_left, nat.cast_le, and_self] }, { refine mem_Icc.2 ⟨le_trans (by simp) hx.1, le_trans hx.2 _⟩, simp only [add_le_add_iff_left, nat.cast_le, nat.succ_le_of_lt ia] }, end ... = ∑ i in finset.range a, f (x₀ + i) : by simp end lemma antitone_on.integral_le_sum_Ico (hab : a ≤ b) (hf : antitone_on f (set.Icc a b)) : ∫ x in a..b, f x ≤ ∑ x in finset.Ico a b, f x := begin rw [(nat.sub_add_cancel hab).symm, nat.cast_add], conv { congr, congr, skip, skip, rw add_comm, skip, skip, congr, congr, rw ←zero_add a, }, rw [← finset.sum_Ico_add, nat.Ico_zero_eq_range], conv { to_rhs, congr, skip, funext, rw nat.cast_add, }, apply antitone_on.integral_le_sum, simp only [hf, hab, nat.cast_sub, add_sub_cancel'_right], end lemma antitone_on.sum_le_integral (hf : antitone_on f (Icc x₀ (x₀ + a))) : ∑ i in finset.range a, f (x₀ + (i + 1 : ℕ)) ≤ ∫ x in x₀..(x₀ + a), f x := begin have hint : ∀ (k : ℕ), k < a → interval_integrable f volume (x₀+k) (x₀ + (k + 1 : ℕ)), { assume k hk, refine (hf.mono _).interval_integrable, rw uIcc_of_le, { apply Icc_subset_Icc, { simp only [le_add_iff_nonneg_right, nat.cast_nonneg] }, { simp only [add_le_add_iff_left, nat.cast_le, nat.succ_le_of_lt hk] } }, { simp only [add_le_add_iff_left, nat.cast_le, nat.le_succ] } }, calc ∑ i in finset.range a, f (x₀ + (i + 1 : ℕ)) = ∑ i in finset.range a, ∫ x in (x₀+i)..(x₀+(i+1:ℕ)), f (x₀ + (i + 1 : ℕ)) : by simp ... ≤ ∑ i in finset.range a, ∫ x in (x₀+i)..(x₀+(i+1:ℕ)), f x : begin apply finset.sum_le_sum (λ i hi, _), have ia : i + 1 ≤ a := finset.mem_range.1 hi, refine interval_integral.integral_mono_on (by simp) (by simp) (hint _ ia) (λ x hx, _), apply hf _ _ hx.2, { refine mem_Icc.2 ⟨le_trans ((le_add_iff_nonneg_right _).2 (nat.cast_nonneg _)) hx.1, le_trans hx.2 _⟩, simp only [nat.cast_le, add_le_add_iff_left, ia] }, { refine mem_Icc.2 ⟨(le_add_iff_nonneg_right _).2 (nat.cast_nonneg _), _⟩, simp only [add_le_add_iff_left, nat.cast_le, ia] }, end ... = ∫ x in x₀..(x₀ + a), f x : begin convert interval_integral.sum_integral_adjacent_intervals hint, simp only [nat.cast_zero, add_zero], end end lemma antitone_on.sum_le_integral_Ico (hab : a ≤ b) (hf : antitone_on f (set.Icc a b)) : ∑ i in finset.Ico a b, f (i + 1 : ℕ) ≤ ∫ x in a..b, f x := begin rw [(nat.sub_add_cancel hab).symm, nat.cast_add], conv { congr, congr, congr, rw ← zero_add a, skip, skip, skip, rw add_comm, }, rw [← finset.sum_Ico_add, nat.Ico_zero_eq_range], conv { to_lhs, congr, congr, skip, funext, rw [add_assoc, nat.cast_add], }, apply antitone_on.sum_le_integral, simp only [hf, hab, nat.cast_sub, add_sub_cancel'_right], end lemma monotone_on.sum_le_integral (hf : monotone_on f (Icc x₀ (x₀ + a))) : ∑ i in finset.range a, f (x₀ + i) ≤ ∫ x in x₀..(x₀ + a), f x := begin rw [← neg_le_neg_iff, ← finset.sum_neg_distrib, ← interval_integral.integral_neg], exact hf.neg.integral_le_sum, end lemma monotone_on.sum_le_integral_Ico (hab : a ≤ b) (hf : monotone_on f (set.Icc a b)) : ∑ x in finset.Ico a b, f x ≤ ∫ x in a..b, f x := begin rw [← neg_le_neg_iff, ← finset.sum_neg_distrib, ← interval_integral.integral_neg], exact hf.neg.integral_le_sum_Ico hab, end lemma monotone_on.integral_le_sum (hf : monotone_on f (Icc x₀ (x₀ + a))) : ∫ x in x₀..(x₀ + a), f x ≤ ∑ i in finset.range a, f (x₀ + (i + 1 : ℕ)) := begin rw [← neg_le_neg_iff, ← finset.sum_neg_distrib, ← interval_integral.integral_neg], exact hf.neg.sum_le_integral, end lemma monotone_on.integral_le_sum_Ico (hab : a ≤ b) (hf : monotone_on f (set.Icc a b)) : ∫ x in a..b, f x ≤ ∑ i in finset.Ico a b, f (i + 1 : ℕ) := begin rw [← neg_le_neg_iff, ← finset.sum_neg_distrib, ← interval_integral.integral_neg], exact hf.neg.sum_le_integral_Ico hab, end
28fd7b5751a5166deb93c181ebccaa0b61fc6ac4
e00ea76a720126cf9f6d732ad6216b5b824d20a7
/src/algebra/ring.lean
f08397bdf0ff0fb89b0022ae6f895a8e63ce6ce3
[ "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
28,526
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura, Floris van Doorn, Amelia Livingston -/ import algebra.group.with_one import deprecated.group import tactic.norm_cast /-! # Properties and homomorphisms of semirings and rings This file proves simple properties of semirings, rings and domains and their unit groups. It also defines homomorphisms of semirings and rings, both unbundled (e.g. `is_semiring_hom f`) and bundled (e.g. `ring_hom a β`, a.k.a. `α →+* β`). The unbundled ones are deprecated and the plan is to slowly remove them from mathlib. ## Main definitions is_semiring_hom (deprecated), is_ring_hom (deprecated), ring_hom, nonzero_comm_semiring, nonzero_comm_ring, domain ## Notations →+* for bundled ring homs (also use for semiring homs) ## Implementation notes There's a coercion from bundled homs to fun, and the canonical notation is to use the bundled hom as a function via this coercion. There is no `semiring_hom` -- the idea is that `ring_hom` is used. The constructor for a `ring_hom` between semirings needs a proof of `map_zero`, `map_one` and `map_add` as well as `map_mul`; a separate constructor `ring_hom.mk'` will construct ring homs between rings from monoid homs given only a proof that addition is preserved. Throughout the section on `ring_hom` implicit `{}` brackets are often used instead of type class `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type `ring_hom`. When they can be inferred from the type it is faster to use this method than to use type class inference. ## Tags is_ring_hom, is_semiring_hom, ring_hom, semiring_hom, semiring, comm_semiring, ring, comm_ring, domain, integral_domain, nonzero_comm_semiring, nonzero_comm_ring, units -/ universes u v w variable {α : Type u} section variable [semiring α] theorem mul_two (n : α) : n * 2 = n + n := (left_distrib n 1 1).trans (by simp) theorem bit0_eq_two_mul (n : α) : bit0 n = 2 * n := (two_mul _).symm @[to_additive] lemma mul_ite {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : a * (if P then b else c) = if P then a * b else a * c := by split_ifs; refl @[to_additive] lemma ite_mul {α} [has_mul α] (P : Prop) [decidable P] (a b c : α) : (if P then a else b) * c = if P then a * c else b * c := by split_ifs; refl -- We make `mul_ite` and `ite_mul` simp lemmas, -- but not `add_ite` or `ite_add`. -- The problem we're trying to avoid is dealing with -- summations of the form `s.sum (λ x, f x + ite P 1 0)`, -- in which `add_ite` followed by `sum_ite` would needlessly slice up -- the `f x` terms according to whether `P` holds at `x`. -- There doesn't appear to be a corresponding difficulty so far with -- `mul_ite` and `ite_mul`. attribute [simp] mul_ite ite_mul -- In this lemma and the next we need to use `congr` because -- `if_simp_congr`, the congruence lemma `simp` uses for rewriting inside `ite`, -- modifies the decidable instance. -- We expect in Lean 3.8 that this won't be necessary. @[simp] lemma mul_boole {α} [semiring α] (P : Prop) [decidable P] (a : α) : a * (if P then 1 else 0) = if P then a else 0 := by { simp, congr } @[simp] lemma boole_mul {α} [semiring α] (P : Prop) [decidable P] (a : α) : (if P then 1 else 0) * a = if P then a else 0 := by { simp, congr } variable (α) /-- Either zero and one are nonequal in a semiring, or the semiring is the zero ring. -/ lemma zero_ne_one_or_forall_eq_0 : (0 : α) ≠ 1 ∨ (∀a:α, a = 0) := by haveI := classical.dec; refine not_or_of_imp (λ h a, _); simpa using congr_arg ((*) a) h.symm /-- If zero equals one in a semiring, the semiring is the zero ring. -/ lemma eq_zero_of_zero_eq_one (h : (0 : α) = 1) : (∀a:α, a = 0) := (zero_ne_one_or_forall_eq_0 α).neg_resolve_left h /-- If zero equals one in a semiring, all elements of that semiring are equal. -/ theorem subsingleton_of_zero_eq_one (h : (0 : α) = 1) : subsingleton α := ⟨λa b, by rw [eq_zero_of_zero_eq_one α h a, eq_zero_of_zero_eq_one α h b]⟩ end namespace units variables [ring α] {a b : α} /-- Each element of the group of units of a ring has an additive inverse. -/ instance : has_neg (units α) := ⟨λu, ⟨-↑u, -↑u⁻¹, by simp, by simp⟩ ⟩ /-- Representing an element of a ring's unit group as an element of the ring commutes with mapping this element to its additive inverse. -/ @[simp] protected theorem coe_neg (u : units α) : (↑-u : α) = -u := rfl /-- Mapping an element of a ring's unit group to its inverse commutes with mapping this element to its additive inverse. -/ @[simp] protected theorem neg_inv (u : units α) : (-u)⁻¹ = -u⁻¹ := rfl /-- An element of a ring's unit group equals the additive inverse of its additive inverse. -/ @[simp] protected theorem neg_neg (u : units α) : - -u = u := units.ext $ neg_neg _ /-- Multiplication of elements of a ring's unit group commutes with mapping the first argument to its additive inverse. -/ @[simp] protected theorem neg_mul (u₁ u₂ : units α) : -u₁ * u₂ = -(u₁ * u₂) := units.ext $ neg_mul_eq_neg_mul_symm _ _ /-- Multiplication of elements of a ring's unit group commutes with mapping the second argument to its additive inverse. -/ @[simp] protected theorem mul_neg (u₁ u₂ : units α) : u₁ * -u₂ = -(u₁ * u₂) := units.ext $ (neg_mul_eq_mul_neg _ _).symm /-- Multiplication of the additive inverses of two elements of a ring's unit group equals multiplication of the two original elements. -/ @[simp] protected theorem neg_mul_neg (u₁ u₂ : units α) : -u₁ * -u₂ = u₁ * u₂ := by simp /-- The additive inverse of an element of a ring's unit group equals the additive inverse of one times the original element. -/ protected theorem neg_eq_neg_one_mul (u : units α) : -u = -1 * u := by simp end units instance [semiring α] : semiring (with_zero α) := { left_distrib := λ a b c, begin cases a with a, {refl}, cases b with b; cases c with c; try {refl}, exact congr_arg some (left_distrib _ _ _) end, right_distrib := λ a b c, begin cases c with c, { change (a + b) * 0 = a * 0 + b * 0, simp }, cases a with a; cases b with b; try {refl}, exact congr_arg some (right_distrib _ _ _) end, ..with_zero.add_comm_monoid, ..with_zero.mul_zero_class, ..with_zero.monoid } attribute [refl] dvd_refl attribute [trans] dvd.trans /-- Predicate for semiring homomorphisms (deprecated -- use the bundled `ring_hom` version). -/ class is_semiring_hom {α : Type u} {β : Type v} [semiring α] [semiring β] (f : α → β) : Prop := (map_zero : f 0 = 0) (map_one : f 1 = 1) (map_add : ∀ {x y}, f (x + y) = f x + f y) (map_mul : ∀ {x y}, f (x * y) = f x * f y) namespace is_semiring_hom variables {β : Type v} [semiring α] [semiring β] variables (f : α → β) [is_semiring_hom f] {x y : α} /-- The identity map is a semiring homomorphism. -/ instance id : is_semiring_hom (@id α) := by refine {..}; intros; refl /-- The composition of two semiring homomorphisms is a semiring homomorphism. -/ -- see Note [no instance on morphisms] lemma comp {γ} [semiring γ] (g : β → γ) [is_semiring_hom g] : is_semiring_hom (g ∘ f) := { map_zero := by simp [map_zero f]; exact map_zero g, map_one := by simp [map_one f]; exact map_one g, map_add := λ x y, by simp [map_add f]; rw map_add g; refl, map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl } /-- A semiring homomorphism is an additive monoid homomorphism. -/ @[priority 100] -- see Note [lower instance priority] instance : is_add_monoid_hom f := { ..‹is_semiring_hom f› } /-- A semiring homomorphism is a monoid homomorphism. -/ @[priority 100] -- see Note [lower instance priority] instance : is_monoid_hom f := { ..‹is_semiring_hom f› } end is_semiring_hom section variables [ring α] (a b c d e : α) /-- An element of a ring multiplied by the additive inverse of one is the element's additive inverse. -/ lemma mul_neg_one (a : α) : a * -1 = -a := by simp /-- The additive inverse of one multiplied by an element of a ring is the element's additive inverse. -/ lemma neg_one_mul (a : α) : -1 * a = -a := by simp /-- An iff statement following from right distributivity in rings and the definition of subtraction. -/ theorem mul_add_eq_mul_add_iff_sub_mul_add_eq : a * e + c = b * e + d ↔ (a - b) * e + c = d := calc a * e + c = b * e + d ↔ a * e + c = d + b * e : by simp [add_comm] ... ↔ a * e + c - b * e = d : iff.intro (λ h, begin rw h, simp end) (λ h, begin rw ← h, simp end) ... ↔ (a - b) * e + c = d : begin simp [sub_mul, sub_add_eq_add_sub] end /-- A simplification of one side of an equation exploiting right distributivity in rings and the definition of subtraction. -/ theorem sub_mul_add_eq_of_mul_add_eq_mul_add : a * e + c = b * e + d → (a - b) * e + c = d := assume h, calc (a - b) * e + c = (a * e + c) - b * e : begin simp [sub_mul, sub_add_eq_add_sub] end ... = d : begin rw h, simp [@add_sub_cancel α] end /-- If the product of two elements of a ring is nonzero, both elements are nonzero. -/ theorem ne_zero_and_ne_zero_of_mul_ne_zero {a b : α} (h : a * b ≠ 0) : a ≠ 0 ∧ b ≠ 0 := begin split, { intro ha, apply h, simp [ha] }, { intro hb, apply h, simp [hb] } end end /-- Given an element a of a commutative semiring, there exists another element whose product with zero equals a iff a equals zero. -/ @[simp] lemma zero_dvd_iff [comm_semiring α] {a : α} : 0 ∣ a ↔ a = 0 := ⟨eq_zero_of_zero_dvd, λ h, by rw h⟩ section comm_ring variable [comm_ring α] /-- Representation of a difference of two squares in a commutative ring as a product. -/ theorem mul_self_sub_mul_self (a b : α) : a * a - b * b = (a + b) * (a - b) := by rw [add_mul, mul_sub, mul_sub, mul_comm a b, sub_add_sub_cancel] /-- An element a of a commutative ring divides the additive inverse of an element b iff a divides b. -/ @[simp] lemma dvd_neg (a b : α) : (a ∣ -b) ↔ (a ∣ b) := ⟨dvd_of_dvd_neg, dvd_neg_of_dvd⟩ /-- The additive inverse of an element a of a commutative ring divides another element b iff a divides b. -/ @[simp] lemma neg_dvd (a b : α) : (-a ∣ b) ↔ (a ∣ b) := ⟨dvd_of_neg_dvd, neg_dvd_of_dvd⟩ /-- If an element a divides another element c in a commutative ring, a divides the sum of another element b with c iff a divides b. -/ theorem dvd_add_left {a b c : α} (h : a ∣ c) : a ∣ b + c ↔ a ∣ b := (dvd_add_iff_left h).symm /-- If an element a divides another element b in a commutative ring, a divides the sum of b and another element c iff a divides c. -/ theorem dvd_add_right {a b c : α} (h : a ∣ b) : a ∣ b + c ↔ a ∣ c := (dvd_add_iff_right h).symm /-- An element a divides the sum a + b if and only if a divides b.-/ @[simp] lemma dvd_add_self_left {a b : α} : a ∣ a + b ↔ a ∣ b := dvd_add_right (dvd_refl a) /-- An element a divides the sum b + a if and only if a divides b.-/ @[simp] lemma dvd_add_self_right {a b : α} : a ∣ b + a ↔ a ∣ b := dvd_add_left (dvd_refl a) /-- Vieta's formula for a quadratic equation, relating the coefficients of the polynomial with its roots. This particular version states that if we have a root `x` of a monic quadratic polynomial, then there is another root `y` such that `x + y` is negative the `a_1` coefficient and `x * y` is the `a_0` coefficient. -/ lemma Vieta_formula_quadratic {b c x : α} (h : x * x - b * x + c = 0) : ∃ y : α, y * y - b * y + c = 0 ∧ x + y = b ∧ x * y = c := begin have : c = -(x * x - b * x) := (neg_eq_of_add_eq_zero h).symm, have : c = x * (b - x), by subst this; simp [mul_sub, mul_comm], refine ⟨b - x, _, by simp, by rw this⟩, rw [this, sub_add, ← sub_mul, sub_self] end end comm_ring /-- Predicate for ring homomorphisms (deprecated -- use the bundled `ring_hom` version). -/ class is_ring_hom {α : Type u} {β : Type v} [ring α] [ring β] (f : α → β) : Prop := (map_one : f 1 = 1) (map_mul : ∀ {x y}, f (x * y) = f x * f y) (map_add : ∀ {x y}, f (x + y) = f x + f y) namespace is_ring_hom variables {β : Type v} [ring α] [ring β] /-- A map of rings that is a semiring homomorphism is also a ring homomorphism. -/ lemma of_semiring (f : α → β) [H : is_semiring_hom f] : is_ring_hom f := {..H} variables (f : α → β) [is_ring_hom f] {x y : α} /-- Ring homomorphisms map zero to zero. -/ lemma map_zero : f 0 = 0 := calc f 0 = f (0 + 0) - f 0 : by rw [map_add f]; simp ... = 0 : by simp /-- Ring homomorphisms preserve additive inverses. -/ lemma map_neg : f (-x) = -f x := calc f (-x) = f (-x + x) - f x : by rw [map_add f]; simp ... = -f x : by simp [map_zero f] /-- Ring homomorphisms preserve subtraction. -/ lemma map_sub : f (x - y) = f x - f y := by simp [sub_eq_add_neg, map_add f, map_neg f] /-- The identity map is a ring homomorphism. -/ instance id : is_ring_hom (@id α) := by refine {..}; intros; refl /-- The composition of two ring homomorphisms is a ring homomorphism. -/ -- see Note [no instance on morphisms] lemma comp {γ} [ring γ] (g : β → γ) [is_ring_hom g] : is_ring_hom (g ∘ f) := { map_add := λ x y, by simp [map_add f]; rw map_add g; refl, map_mul := λ x y, by simp [map_mul f]; rw map_mul g; refl, map_one := by simp [map_one f]; exact map_one g } /-- A ring homomorphism is also a semiring homomorphism. -/ @[priority 100] -- see Note [lower instance priority] instance : is_semiring_hom f := { map_zero := map_zero f, ..‹is_ring_hom f› } @[priority 100] -- see Note [lower instance priority] instance : is_add_group_hom f := { } end is_ring_hom set_option old_structure_cmd true section prio set_option default_priority 100 -- see Note [default priority] /-- Bundled semiring homomorphisms; use this for bundled ring homomorphisms too. -/ structure ring_hom (α : Type*) (β : Type*) [semiring α] [semiring β] extends monoid_hom α β, add_monoid_hom α β end prio infixr ` →+* `:25 := ring_hom instance {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} : has_coe_to_fun (α →+* β) := ⟨_, ring_hom.to_fun⟩ instance {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} : has_coe (α →+* β) (α →* β) := ⟨ring_hom.to_monoid_hom⟩ instance {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} : has_coe (α →+* β) (α →+ β) := ⟨ring_hom.to_add_monoid_hom⟩ @[squash_cast] lemma coe_monoid_hom {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} (f : α →+* β) (a : α) : ((f : α →* β) : α → β) a = (f : α → β) a := rfl @[squash_cast] lemma coe_add_monoid_hom {α : Type*} {β : Type*} {rα : semiring α} {rβ : semiring β} (f : α →+* β) (a : α) : ((f : α →+ β) : α → β) a = (f : α → β) a := rfl namespace ring_hom variables {β : Type v} {γ : Type w} [rα : semiring α] [rβ : semiring β] include rα rβ /-- Interpret `f : α → β` with `is_semiring_hom f` as a ring homomorphism. -/ def of (f : α → β) [is_semiring_hom f] : α →+* β := { to_fun := f, .. monoid_hom.of f, .. add_monoid_hom.of f } @[simp] lemma coe_of (f : α → β) [is_semiring_hom f] : ⇑(of f) = f := rfl @[simp] lemma coe_mk (f : α → β) (h₁ h₂ h₃ h₄) : ⇑(⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) = f := rfl variables (f : α →+* β) {x y : α} {rα rβ} theorem coe_inj ⦃f g : α →+* β⦄ (h : (f : α → β) = g) : f = g := by cases f; cases g; cases h; refl @[ext] theorem ext ⦃f g : α →+* β⦄ (h : ∀ x, f x = g x) : f = g := coe_inj (funext h) theorem ext_iff {f g : α →+* β} : f = g ↔ ∀ x, f x = g x := ⟨λ h x, h ▸ rfl, λ h, ext h⟩ /-- Ring homomorphisms map zero to zero. -/ @[simp] lemma map_zero (f : α →+* β) : f 0 = 0 := f.map_zero' /-- Ring homomorphisms map one to one. -/ @[simp] lemma map_one (f : α →+* β) : f 1 = 1 := f.map_one' /-- Ring homomorphisms preserve addition. -/ @[simp] lemma map_add (f : α →+* β) (a b : α) : f (a + b) = f a + f b := f.map_add' a b /-- Ring homomorphisms preserve multiplication. -/ @[simp] lemma map_mul (f : α →+* β) (a b : α) : f (a * b) = f a * f b := f.map_mul' a b instance (f : α →+* β) : is_semiring_hom f := { map_zero := f.map_zero, map_one := f.map_one, map_add := f.map_add, map_mul := f.map_mul } omit rα rβ instance {α γ} [ring α] [ring γ] (g : α →+* γ) : is_ring_hom g := is_ring_hom.of_semiring g /-- The identity ring homomorphism from a semiring to itself. -/ def id (α : Type*) [semiring α] : α →+* α := by refine {to_fun := id, ..}; intros; refl include rα @[simp] lemma id_apply : ring_hom.id α x = x := rfl variable {rγ : semiring γ} include rβ rγ /-- Composition of ring homomorphisms is a ring homomorphism. -/ def comp (hnp : β →+* γ) (hmn : α →+* β) : α →+* γ := { to_fun := hnp ∘ hmn, map_zero' := by simp, map_one' := by simp, map_add' := λ x y, by simp, map_mul' := λ x y, by simp} /-- Composition of semiring homomorphisms is associative. -/ lemma comp_assoc {δ} {rδ: semiring δ} (f : α →+* β) (g : β →+* γ) (h : γ →+* δ) : (h.comp g).comp f = h.comp (g.comp f) := rfl @[simp] lemma coe_comp (hnp : β →+* γ) (hmn : α →+* β) : (hnp.comp hmn : α → γ) = hnp ∘ hmn := rfl lemma comp_apply (hnp : β →+* γ) (hmn : α →+* β) (x : α) : (hnp.comp hmn : α → γ) x = (hnp (hmn x)) := rfl lemma cancel_right {g₁ g₂ : β →+* γ} {f : α →+* β} (hf : function.surjective f) : g₁.comp f = g₂.comp f ↔ g₁ = g₂ := ⟨λ h, ring_hom.ext $ (forall_iff_forall_surj hf).1 (ext_iff.1 h), λ h, h ▸ rfl⟩ lemma cancel_left {g : β →+* γ} {f₁ f₂ : α →+* β} (hg : function.injective g) : g.comp f₁ = g.comp f₂ ↔ f₁ = f₂ := ⟨λ h, ring_hom.ext $ λ x, hg $ by rw [← comp_apply, h, comp_apply], λ h, h ▸ rfl⟩ omit rα rβ rγ /-- Ring homomorphisms preserve additive inverse. -/ @[simp] theorem map_neg {α β} [ring α] [ring β] (f : α →+* β) (x : α) : f (-x) = -(f x) := eq_neg_of_add_eq_zero $ by rw [←f.map_add, neg_add_self, f.map_zero] /-- Ring homomorphisms preserve subtraction. -/ @[simp] theorem map_sub {α β} [ring α] [ring β] (f : α →+* β) (x y : α) : f (x - y) = (f x) - (f y) := by simp [sub_eq_add_neg] /-- A ring homomorphism is injective iff its kernel is trivial. -/ theorem injective_iff {α β} [ring α] [ring β] (f : α →+* β) : function.injective f ↔ (∀ a, f a = 0 → a = 0) := add_monoid_hom.injective_iff f.to_add_monoid_hom include rα /-- Makes a ring homomorphism from a monoid homomorphism of rings which preserves addition. -/ def mk' {γ} [ring γ] (f : α →* γ) (map_add : ∀ a b : α, f (a + b) = f a + f b) : α →+* γ := { to_fun := f, map_zero' := add_self_iff_eq_zero.1 $ by rw [←map_add, add_zero], map_one' := f.map_one, map_mul' := f.map_mul, map_add' := map_add } end ring_hom section prio set_option default_priority 100 -- see Note [default priority] /-- Predicate for commutative semirings in which zero does not equal one. -/ class nonzero_comm_semiring (α : Type*) extends comm_semiring α, zero_ne_one_class α /-- Predicate for commutative rings in which zero does not equal one. -/ class nonzero_comm_ring (α : Type*) extends comm_ring α, zero_ne_one_class α end prio /-- A nonzero commutative ring is a nonzero commutative semiring. -/ @[priority 100] -- see Note [lower instance priority] instance nonzero_comm_ring.to_nonzero_comm_semiring {α : Type*} [I : nonzero_comm_ring α] : nonzero_comm_semiring α := { zero_ne_one := by convert zero_ne_one, ..show comm_semiring α, by apply_instance } /-- An integral domain is a nonzero commutative ring. -/ @[priority 100] -- see Note [lower instance priority] instance integral_domain.to_nonzero_comm_ring (α : Type*) [id : integral_domain α] : nonzero_comm_ring α := { ..id } /-- An element of the unit group of a nonzero commutative semiring represented as an element of the semiring is nonzero. -/ lemma units.coe_ne_zero [nonzero_comm_semiring α] (u : units α) : (u : α) ≠ 0 := λ h : u.1 = 0, by simpa [h, zero_ne_one] using u.3 /-- Makes a nonzero commutative ring from a commutative ring containing at least two distinct elements. -/ def nonzero_comm_ring.of_ne [comm_ring α] {x y : α} (h : x ≠ y) : nonzero_comm_ring α := { one := 1, zero := 0, zero_ne_one := λ h01, h $ by rw [← one_mul x, ← one_mul y, ← h01, zero_mul, zero_mul], ..show comm_ring α, by apply_instance } /-- Makes a nonzero commutative semiring from a commutative semiring containing at least two distinct elements. -/ def nonzero_comm_semiring.of_ne [comm_semiring α] {x y : α} (h : x ≠ y) : nonzero_comm_semiring α := { one := 1, zero := 0, zero_ne_one := λ h01, h $ by rw [← one_mul x, ← one_mul y, ← h01, zero_mul, zero_mul], ..show comm_semiring α, by apply_instance } /-- this is needed for compatibility between Lean 3.4.2 and Lean 3.5.1c -/ def has_div_of_division_ring [division_ring α] : has_div α := division_ring_has_div section prio set_option default_priority 100 -- see Note [default priority] /-- A domain is a ring with no zero divisors, i.e. satisfying the condition `a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, a domain is an integral domain without assuming commutativity of multiplication. -/ class domain (α : Type u) extends ring α, no_zero_divisors α, zero_ne_one_class α end prio section domain variable [domain α] /-- Simplification theorems for the definition of a domain. -/ @[simp] theorem mul_eq_zero {a b : α} : a * b = 0 ↔ a = 0 ∨ b = 0 := ⟨eq_zero_or_eq_zero_of_mul_eq_zero, λo, or.elim o (λh, by rw h; apply zero_mul) (λh, by rw h; apply mul_zero)⟩ @[simp] theorem zero_eq_mul {a b : α} : 0 = a * b ↔ a = 0 ∨ b = 0 := by rw [eq_comm, mul_eq_zero] lemma mul_self_eq_zero {α} [domain α] {x : α} : x * x = 0 ↔ x = 0 := by simp lemma zero_eq_mul_self {α} [domain α] {x : α} : 0 = x * x ↔ x = 0 := by simp /-- The product of two nonzero elements of a domain is nonzero. -/ theorem mul_ne_zero' {a b : α} (h₁ : a ≠ 0) (h₂ : b ≠ 0) : a * b ≠ 0 := λ h, or.elim (eq_zero_or_eq_zero_of_mul_eq_zero h) h₁ h₂ /-- Right multiplication by a nonzero element in a domain is injective. -/ theorem domain.mul_right_inj {a b c : α} (ha : a ≠ 0) : b * a = c * a ↔ b = c := by rw [← sub_eq_zero, ← mul_sub_right_distrib, mul_eq_zero]; simp [ha]; exact sub_eq_zero /-- Left multiplication by a nonzero element in a domain is injective. -/ theorem domain.mul_left_inj {a b c : α} (ha : a ≠ 0) : a * b = a * c ↔ b = c := by rw [← sub_eq_zero, ← mul_sub_left_distrib, mul_eq_zero]; simp [ha]; exact sub_eq_zero /-- An element of a domain fixed by right multiplication by an element other than one must be zero. -/ theorem eq_zero_of_mul_eq_self_right' {a b : α} (h₁ : b ≠ 1) (h₂ : a * b = a) : a = 0 := by apply (mul_eq_zero.1 _).resolve_right (sub_ne_zero.2 h₁); rw [mul_sub_left_distrib, mul_one, sub_eq_zero, h₂] /-- An element of a domain fixed by left multiplication by an element other than one must be zero. -/ theorem eq_zero_of_mul_eq_self_left' {a b : α} (h₁ : b ≠ 1) (h₂ : b * a = a) : a = 0 := by apply (mul_eq_zero.1 _).resolve_left (sub_ne_zero.2 h₁); rw [mul_sub_right_distrib, one_mul, sub_eq_zero, h₂] /-- For elements a, b of a domain, if a*b is nonzero, so is b*a. -/ theorem mul_ne_zero_comm' {a b : α} (h : a * b ≠ 0) : b * a ≠ 0 := mul_ne_zero' (ne_zero_of_mul_ne_zero_left h) (ne_zero_of_mul_ne_zero_right h) end domain /- integral domains -/ section variables [s : integral_domain α] (a b c d e : α) include s /-- An integral domain is a domain. -/ @[priority 100] -- see Note [lower instance priority] instance integral_domain.to_domain : domain α := {..s} /-- Right multiplcation by a nonzero element of an integral domain is injective. -/ theorem eq_of_mul_eq_mul_right_of_ne_zero {a b c : α} (ha : a ≠ 0) (h : b * a = c * a) : b = c := have b * a - c * a = 0, by simp [h], have (b - c) * a = 0, by rw [mul_sub_right_distrib, this], have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right ha, eq_of_sub_eq_zero this /-- Left multiplication by a nonzero element of an integral domain is injective. -/ theorem eq_of_mul_eq_mul_left_of_ne_zero {a b c : α} (ha : a ≠ 0) (h : a * b = a * c) : b = c := have a * b - a * c = 0, by simp [h], have a * (b - c) = 0, by rw [mul_sub_left_distrib, this], have b - c = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_left ha, eq_of_sub_eq_zero this /-- Given two elements b, c of an integral domain and a nonzero element a, a*b divides a*c iff b divides c. -/ theorem mul_dvd_mul_iff_left {a b c : α} (ha : a ≠ 0) : a * b ∣ a * c ↔ b ∣ c := exists_congr $ λ d, by rw [mul_assoc, domain.mul_left_inj ha] /-- Given two elements a, b of an integral domain and a nonzero element c, a*c divides b*c iff a divides b. -/ theorem mul_dvd_mul_iff_right {a b c : α} (hc : c ≠ 0) : a * c ∣ b * c ↔ a ∣ b := exists_congr $ λ d, by rw [mul_right_comm, domain.mul_right_inj hc] /-- In the unit group of an integral domain, a unit is its own inverse iff the unit is one or one's additive inverse. -/ lemma units.inv_eq_self_iff (u : units α) : u⁻¹ = u ↔ u = 1 ∨ u = -1 := by conv {to_lhs, rw [inv_eq_iff_mul_eq_one, ← mul_one (1 : units α), units.ext_iff, units.coe_mul, units.coe_mul, mul_self_eq_mul_self_iff, ← units.ext_iff, ← units.coe_neg, ← units.ext_iff] } end /- units in various rings -/ namespace units section comm_semiring variables [comm_semiring α] (a b : α) (u : units α) /-- Elements of the unit group of a commutative semiring represented as elements of the semiring divide any element of the semiring. -/ @[simp] lemma coe_dvd : ↑u ∣ a := ⟨↑u⁻¹ * a, by simp⟩ /-- In a commutative semiring, an element a divides an element b iff a divides all associates of b. -/ @[simp] lemma dvd_coe_mul : a ∣ b * u ↔ a ∣ b := iff.intro (assume ⟨c, eq⟩, ⟨c * ↑u⁻¹, by rw [← mul_assoc, ← eq, units.mul_inv_cancel_right]⟩) (assume ⟨c, eq⟩, eq.symm ▸ dvd_mul_of_dvd_left (dvd_mul_right _ _) _) /-- An element of a commutative semiring divides a unit iff the element divides one. -/ @[simp] lemma dvd_coe : a ∣ ↑u ↔ a ∣ 1 := suffices a ∣ 1 * ↑u ↔ a ∣ 1, by simpa, dvd_coe_mul _ _ _ /-- In a commutative semiring, an element a divides an element b iff all associates of a divide b.-/ @[simp] lemma coe_mul_dvd : a * u ∣ b ↔ a ∣ b := iff.intro (assume ⟨c, eq⟩, ⟨c * ↑u, eq.symm ▸ by ac_refl⟩) (assume h, suffices a * ↑u ∣ b * 1, by simpa, mul_dvd_mul h (coe_dvd _ _)) end comm_semiring section domain variables [domain α] /-- Every unit in a domain is nonzero. -/ @[simp] theorem ne_zero : ∀(u : units α), (↑u : α) ≠ 0 | ⟨u, v, (huv : 0 * v = 1), hvu⟩ rfl := by simpa using huv end domain end units /-- A predicate to express that a ring is an integral domain. This is mainly useful because such a predicate does not contain data, and can therefore be easily transported along ring isomorphisms. -/ structure is_integral_domain (R : Type u) [ring R] : Prop := (mul_comm : ∀ (x y : R), x * y = y * x) (eq_zero_or_eq_zero_of_mul_eq_zero : ∀ x y : R, x * y = 0 → x = 0 ∨ y = 0) (zero_ne_one : (0 : R) ≠ 1) /-- Every integral domain satisfies the predicate for integral domains. -/ lemma integral_domain.to_is_integral_domain (R : Type u) [integral_domain R] : is_integral_domain R := { .. (‹_› : integral_domain R) } /-- If a ring satisfies the predicate for integral domains, then it can be endowed with an `integral_domain` instance whose data is definitionally equal to the existing data. -/ def is_integral_domain.to_integral_domain (R : Type u) [ring R] (h : is_integral_domain R) : integral_domain R := { .. (‹_› : ring R), .. (‹_› : is_integral_domain R) }
a23e8550ce407e642e4e5582ef3a2c4f20c3a4df
3aad12fe82645d2d3173fbedc2e5c2ba945a4d75
/src/tactic/inductive_decl.lean
f73856ab93c7cca2b854846ba807524ff34ed879
[]
no_license
seanpm2001/LeanProver-Community_MathLIB-Nursery
4f88d539cb18d73a94af983092896b851e6640b5
0479b31fa5b4d39f41e89b8584c9f5bf5271e8ec
refs/heads/master
1,688,730,786,645
1,572,070,026,000
1,572,070,026,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
7,212
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Simon Hudon -/ import tactic.nursery namespace tactic open expr @[derive has_reflect] meta structure type_cnstr := (name : name) (args : list expr) (result : list expr) @[derive has_reflect] meta structure inductive_type := (name : name) (u_names : list _root_.name) (params : list expr) (idx : list expr) (type : expr) (ctors : list type_cnstr) meta def inductive_type.u_params (decl : inductive_type) := decl.u_names.map level.param meta def inductive_type.mk_const (decl : inductive_type) (n : string) : expr := const (decl.name <.> n) $ decl.u_params meta def inductive_type.mk_name (decl : inductive_type) (n : string) : name := decl.name <.> n meta def inductive_type.is_constructor (decl : inductive_type) (n : name) : bool := n ∈ decl.ctors.map type_cnstr.name meta def fresh_univ (xs : list name) : name := (((list.iota (xs.length + 1)).map (`u).append_after).diff xs).head meta def mk_cases_on (decl : inductive_type) : tactic unit := do let u := fresh_univ decl.u_names, let idx := decl.idx, n ← mk_app decl.name (decl.params ++ idx) >>= mk_local_def `n, C ← pis (idx ++ [n]) (sort $ level.param u) >>= mk_local_def `C, cases ← decl.ctors.mmap $ λ c, do { cn ← get_unused_name "c", n ← mk_mapp c.name $ (decl.params ++ c.args).map some, pis c.args (C.mk_app $ c.result ++ [n]) >>= mk_local_def cn }, t ← pis ((decl.params ++ [C] ++ idx).map to_implicit ++ [n] ++ cases) (C.mk_app (idx ++ [n])), p ← mk_mapp (decl.name <.> "rec") $ (decl.params ++ [C] ++ cases ++ idx ++ [n]).map some, p ← lambdas (decl.params ++ [C] ++ idx ++ [n] ++ cases) p, d ← instantiate_mvars p, add_decl $ mk_definition (decl.name <.> "cases_on") (u :: decl.u_names) t d, updateex_env $ λ e, pure $ e.add_namespace decl.name, return () meta def inductive_type.mk_cases_on (decl : inductive_type) (C e : expr) (f : name → list expr → tactic expr) : tactic expr := do let cases_on_n := decl.mk_name "cases_on", cases_on ← mk_const cases_on_n, cs ← decl.ctors.mmap $ λ c, do { f c.name c.args >>= lambdas c.args }, unify_app cases_on (decl.params ++ [C] ++ decl.idx ++ [e] ++ cs) meta def inductive_type.mk_cases_on' (decl : inductive_type) (C e' : expr) (f : name → list expr → tactic unit) : tactic (list expr) := do let cases_on_n := decl.mk_name "cases_on", cases_on ← mk_const cases_on_n, cs ← decl.ctors.mmap $ λ c, do { n ← mk_mapp c.name $ (decl.params ++ c.args).map some, t ← pis (c.args) (C.mk_app $ c.result ++ [n]), mk_meta_var t }, gs ← get_goals, e ← unify_app cases_on (decl.params ++ [C] ++ decl.idx ++ [e'] ++ cs), mzip_with' (λ g (c : type_cnstr), do set_goals [g], intro_lst ( c.args.map expr.local_pp_name ) >>= f c.name ) cs decl.ctors, set_goals gs, apply e, (>>= list_meta_vars) <$> cs.mmap instantiate_mvars. meta def mk_no_confusion_type (decl : inductive_type) : tactic unit := do let t := (const decl.name $ decl.u_params).mk_app $ decl.params ++ decl.idx, let u := fresh_univ decl.u_names, let cases_on_n := decl.mk_name "cases_on", cases_on ← mk_const cases_on_n, v1 ← mk_local_def `v1 t, v2 ← mk_local_def `v2 t, P ← mk_local_def `P (expr.sort $ level.param u), C ← lambdas (decl.idx ++ [v1]) (sort $ level.param u), e ← decl.mk_cases_on C v1 $ λ c args, do { decl.mk_cases_on C v2 $ λ c' args', if c = c' then do hs ← mzip_with (λ x y, mk_app `eq [x,y] >>= mk_local_def `h) args args', h ← pis hs P, pure $ h.imp P else pure P }, let vs := (decl.params ++ decl.idx ++ [P,v1,v2]), e ← lambdas vs e, p ← pis vs (sort $ level.param u), infer_type e >>= unify p, e ← instantiate_mvars e, add_decl $ mk_definition (decl.mk_name "no_confusion_type") (u :: decl.u_names) p e run_cmd mk_simp_attr `pseudo_eqn meta def mk_no_confusion (decl : inductive_type) : tactic unit := do let t := (const decl.name $ decl.u_params).mk_app $ decl.params ++ decl.idx, let u := fresh_univ decl.u_names, v1 ← mk_local_def `v1 t, v2 ← mk_local_def `v2 t, P ← mk_local_def `P (expr.sort $ level.param u), type ← mk_const (decl.mk_name "no_confusion_type"), Heq ← mk_app `eq [v1,v2] >>= mk_local_def `Heq, let vs := decl.params ++ decl.idx ++ [P,v1,v2,Heq], p ← unify_app type (decl.params ++ decl.idx ++ [P,v1,v2]), (_,pr) ← solve_aux p $ do { intros, dunfold_target [decl.mk_name "no_confusion_type"], tgt ← target, cases_on ← mk_const ``eq.rec, C ← pis [Heq] tgt >>= lambdas ([v2]), t ← infer_type v1, g ← mk_mvar, unify_app cases_on ([t,v1,C,g,v2,Heq,Heq]) >>= exact, set_goals [g], intro1, tgt ← target, C ← lambdas (decl.idx ++ [v1]) tgt, cases_on ← mk_const (decl.mk_name "cases_on"), decl.mk_cases_on' C v1 $ λ _ _, do { dunfold_target [decl.mk_name "cases_on"], try $ `[simp only with pseudo_eqn], h ← intro `h, () <$ apply h; reflexivity }, done }, p ← pis vs p, p ← instantiate_mvars p, pr ← instantiate_mvars pr, pr ← lambdas vs pr, add_decl $ mk_definition (decl.mk_name "no_confusion") (u :: decl.u_names) p pr, pure () meta def inductive_type.get_constructor (c_name : name) : inductive_type → option type_cnstr | { ctors := ctors, .. } := ctors.find (λ c, c.name = c_name) meta def type_cnstr.type (decl : inductive_type) : type_cnstr → tactic expr | ⟨cn,vs,r⟩ := let sig_c : expr := const decl.name decl.u_params in tactic.pis (decl.params.map to_implicit ++ vs) $ sig_c.mk_app $ decl.params ++ r meta def mk_inductive : inductive_type → tactic unit | decl@{ u_names := u_names, params := params, ctors := ctors, .. } := do env ← get_env, let n := decl.name, sig_t ← pis (decl.params ++ decl.idx) decl.type, let sig_c : expr := const n decl.u_params, cs ← ctors.mmap $ λ c, do { t ← c.type decl, pure (c.name.update_prefix n,t) }, env ← env.add_inductive n u_names params.length sig_t cs ff, set_env env, mk_cases_on decl, mk_no_confusion_type decl, mk_no_confusion decl open interactive meta def inductive_type.of_decl (decl : inductive_decl) : tactic inductive_type := do d ← decl.decls.nth 0, (idx,t) ← infer_type d.sig >>= unpi, cs ← d.intros.mmap $ λ c : expr, do { t ← infer_type c, (vs,t) ← unpi t, pure (t.get_app_fn.const_name,{ type_cnstr . name := c.local_pp_name, args := vs, result := t.get_app_args.drop $ decl.params.length }) }, pure { name := (prod.fst <$> cs.nth 0).get_or_else d.sig.local_pp_name, u_names := decl.u_names, params := decl.params, idx := idx, type := t, ctors := cs.map prod.snd } end tactic
1ba6150786cd7b56e5cdb08cd75fefe95aefa6e9
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/data/sigma.lean
4f7abd9c0cd12bd6dd0b77429ce9601adc5d5709
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
1,831
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad, Floris van Doorn Sigma types, aka dependent sum. -/ import logic.cast open inhabited sigma.ops namespace sigma universe variables u v variables {A A' : Type.{u}} {B : A → Type.{v}} {B' : A' → Type.{v}} definition unpack {C : (Σa, B a) → Type} {u : Σa, B a} : C (sigma.mk u.1 u.2) → C u := sigma.rec_on u (λ u1 u2 H, H) theorem dpair_heq {a : A} {a' : A'} {b : B a} {b' : B' a'} (HB : B == B') (Ha : a == a') (Hb : b == b') : (sigma.mk a b) == (sigma.mk a' b') := hcongr_arg4 @mk (type_eq_of_heq Ha) HB Ha Hb protected theorem heq {p : Σa : A, B a} {p' : Σa' : A', B' a'} (HB : B == B') : ∀(H₁ : p.1 == p'.1) (H₂ : p.2 == p'.2), p == p' := destruct p (take a₁ b₁, destruct p' (take a₂ b₂ H₁ H₂, dpair_heq HB H₁ H₂)) attribute [instance] protected definition is_inhabited [H₁ : inhabited A] [H₂ : inhabited (B (default A))] : inhabited (sigma B) := inhabited.destruct H₁ (λa, inhabited.destruct H₂ (λb, inhabited.mk (sigma.mk (default A) b))) variables {C : Πa, B a → Type} {D : Πa b, C a b → Type} definition dtrip (a : A) (b : B a) (c : C a b) := (sigma.mk a (sigma.mk b c)) definition dquad (a : A) (b : B a) (c : C a b) (d : D a b c) := (sigma.mk a (sigma.mk b (sigma.mk c d))) attribute [reducible] definition pr1' (x : Σ a, B a) := x.1 attribute [reducible] definition pr2' (x : Σ a b, C a b) := x.2.1 attribute [reducible] definition pr3 (x : Σ a b, C a b) := x.2.2 attribute [reducible] definition pr3' (x : Σ a b c, D a b c) := x.2.2.1 attribute [reducible] definition pr4 (x : Σ a b c, D a b c) := x.2.2.2 end sigma
9e870b562166cf16dcb8e7233247e64fa2f97a62
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/order/symm_diff.lean
b5504aad8a822f5a26558c0f8936d515d4ddcc79
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,120
lean
/- Copyright (c) 2021 Bryan Gin-ge Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Adam Topaz, Bryan Gin-ge Chen -/ import order.boolean_algebra /-! # Symmetric difference The symmetric difference or disjunctive union of sets `A` and `B` is the set of elements that are in either `A` or `B` but not both. Translated into propositions, the symmetric difference is `xor`. The symmetric difference operator (`symm_diff`) is defined in this file for any type with `⊔` and `\` via the formula `(A \ B) ⊔ (B \ A)`, however the theorems proved about it only hold for `generalized_boolean_algebra`s and `boolean_algebra`s. The symmetric difference is the addition operator in the Boolean ring structure on Boolean algebras. ## Main declarations * `symm_diff`: the symmetric difference operator, defined as `(A \ B) ⊔ (B \ A)` In generalized Boolean algebras, the symmetric difference operator is: * `symm_diff_comm`: commutative, and * `symm_diff_assoc`: associative. ## Notations * `a Δ b`: `symm_diff a b` ## References The proof of associativity follows the note "Associativity of the Symmetric Difference of Sets: A Proof from the Book" by John McCuan: * <https://people.math.gatech.edu/~mccuan/courses/4317/symmetricdifference.pdf> ## Tags boolean ring, generalized boolean algebra, boolean algebra, symmetric differences -/ /-- The symmetric difference operator on a type with `⊔` and `\` is `(A \ B) ⊔ (B \ A)`. -/ def symm_diff {α : Type*} [has_sup α] [has_sdiff α] (A B : α) : α := (A \ B) ⊔ (B \ A) infix ` Δ `:100 := symm_diff lemma symm_diff_def {α : Type*} [has_sup α] [has_sdiff α] (A B : α) : A Δ B = (A \ B) ⊔ (B \ A) := rfl lemma symm_diff_eq_xor (p q : Prop) : p Δ q = xor p q := rfl section generalized_boolean_algebra variables {α : Type*} [generalized_boolean_algebra α] (a b c : α) lemma symm_diff_comm : a Δ b = b Δ a := by simp only [(Δ), sup_comm] instance symm_diff_is_comm : is_commutative α (Δ) := ⟨symm_diff_comm⟩ @[simp] lemma symm_diff_self : a Δ a = ⊥ := by rw [(Δ), sup_idem, sdiff_self] @[simp] lemma symm_diff_bot : a Δ ⊥ = a := by rw [(Δ), sdiff_bot, bot_sdiff, sup_bot_eq] @[simp] lemma bot_symm_diff : ⊥ Δ a = a := by rw [symm_diff_comm, symm_diff_bot] lemma symm_diff_eq_sup_sdiff_inf : a Δ b = (a ⊔ b) \ (a ⊓ b) := by simp [sup_sdiff, sdiff_inf, sup_comm, (Δ)] lemma disjoint_symm_diff_inf : disjoint (a Δ b) (a ⊓ b) := begin rw [symm_diff_eq_sup_sdiff_inf], exact disjoint_sdiff_self_left, end lemma symm_diff_le_sup : a Δ b ≤ a ⊔ b := by { rw symm_diff_eq_sup_sdiff_inf, exact sdiff_le } lemma sdiff_symm_diff : c \ (a Δ b) = (c ⊓ a ⊓ b) ⊔ ((c \ a) ⊓ (c \ b)) := by simp only [(Δ), sdiff_sdiff_sup_sdiff'] lemma sdiff_symm_diff' : c \ (a Δ b) = (c ⊓ a ⊓ b) ⊔ (c \ (a ⊔ b)) := by rw [sdiff_symm_diff, sdiff_sup, sup_comm] lemma symm_diff_sdiff : (a Δ b) \ c = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) := by rw [symm_diff_def, sup_sdiff, sdiff_sdiff_left, sdiff_sdiff_left] @[simp] lemma symm_diff_sdiff_left : (a Δ b) \ a = b \ a := by rw [symm_diff_def, sup_sdiff, sdiff_idem, sdiff_sdiff_self, bot_sup_eq] @[simp] lemma symm_diff_sdiff_right : (a Δ b) \ b = a \ b := by rw [symm_diff_comm, symm_diff_sdiff_left] @[simp] lemma sdiff_symm_diff_self : a \ (a Δ b) = a ⊓ b := by simp [sdiff_symm_diff] lemma symm_diff_eq_iff_sdiff_eq {a b c : α} (ha : a ≤ c) : a Δ b = c ↔ c \ a = b := begin split; intro h, { have hba : disjoint (a ⊓ b) c := begin rw [←h, disjoint.comm], exact disjoint_symm_diff_inf _ _, end, have hca : _ := congr_arg (\ a) h, rw [symm_diff_sdiff_left] at hca, rw [←hca, sdiff_eq_self_iff_disjoint], exact hba.of_disjoint_inf_of_le ha }, { have hd : disjoint a b := by { rw ←h, exact disjoint_sdiff_self_right }, rw [symm_diff_def, hd.sdiff_eq_left, hd.sdiff_eq_right, ←h, sup_sdiff_cancel_right ha] } end lemma disjoint.symm_diff_eq_sup {a b : α} (h : disjoint a b) : a Δ b = a ⊔ b := by rw [(Δ), h.sdiff_eq_left, h.sdiff_eq_right] lemma symm_diff_eq_sup : a Δ b = a ⊔ b ↔ disjoint a b := begin split; intro h, { rw [symm_diff_eq_sup_sdiff_inf, sdiff_eq_self_iff_disjoint] at h, exact h.of_disjoint_inf_of_le le_sup_left, }, { exact h.symm_diff_eq_sup, }, end lemma symm_diff_symm_diff_left : a Δ b Δ c = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔ (c \ (a ⊔ b)) ⊔ (a ⊓ b ⊓ c) := calc a Δ b Δ c = ((a Δ b) \ c) ⊔ (c \ (a Δ b)) : symm_diff_def _ _ ... = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔ ((c \ (a ⊔ b)) ⊔ (c ⊓ a ⊓ b)) : by rw [sdiff_symm_diff', @sup_comm _ _ (c ⊓ a ⊓ b), symm_diff_sdiff] ... = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔ (c \ (a ⊔ b)) ⊔ (a ⊓ b ⊓ c) : by ac_refl lemma symm_diff_symm_diff_right : a Δ (b Δ c) = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔ (c \ (a ⊔ b)) ⊔ (a ⊓ b ⊓ c) := calc a Δ (b Δ c) = (a \ (b Δ c)) ⊔ ((b Δ c) \ a) : symm_diff_def _ _ ... = (a \ (b ⊔ c)) ⊔ (a ⊓ b ⊓ c) ⊔ (b \ (c ⊔ a) ⊔ c \ (b ⊔ a)) : by rw [sdiff_symm_diff', @sup_comm _ _ (a ⊓ b ⊓ c), symm_diff_sdiff] ... = (a \ (b ⊔ c)) ⊔ (b \ (a ⊔ c)) ⊔ (c \ (a ⊔ b)) ⊔ (a ⊓ b ⊓ c) : by ac_refl lemma symm_diff_assoc : a Δ b Δ c = a Δ (b Δ c) := by rw [symm_diff_symm_diff_left, symm_diff_symm_diff_right] instance symm_diff_is_assoc : is_associative α (Δ) := ⟨symm_diff_assoc⟩ @[simp] lemma symm_diff_symm_diff_self : a Δ (a Δ b) = b := by simp [←symm_diff_assoc] @[simp] lemma symm_diff_symm_diff_self' : a Δ b Δ a = b := by rw [symm_diff_comm, ←symm_diff_assoc, symm_diff_self, bot_symm_diff] @[simp] lemma symm_diff_right_inj : a Δ b = a Δ c ↔ b = c := begin split; intro h, { have H1 := congr_arg ((Δ) a) h, rwa [symm_diff_symm_diff_self, symm_diff_symm_diff_self] at H1, }, { rw h, }, end @[simp] lemma symm_diff_left_inj : a Δ b = c Δ b ↔ a = c := by rw [symm_diff_comm a b, symm_diff_comm c b, symm_diff_right_inj] @[simp] lemma symm_diff_eq_left : a Δ b = a ↔ b = ⊥ := calc a Δ b = a ↔ a Δ b = a Δ ⊥ : by rw symm_diff_bot ... ↔ b = ⊥ : by rw symm_diff_right_inj @[simp] lemma symm_diff_eq_right : a Δ b = b ↔ a = ⊥ := by rw [symm_diff_comm, symm_diff_eq_left] @[simp] lemma symm_diff_eq_bot : a Δ b = ⊥ ↔ a = b := calc a Δ b = ⊥ ↔ a Δ b = a Δ a : by rw symm_diff_self ... ↔ a = b : by rw [symm_diff_right_inj, eq_comm] lemma disjoint.disjoint_symm_diff_of_disjoint {a b c : α} (ha : disjoint a c) (hb : disjoint b c) : disjoint (a Δ b) c := begin rw symm_diff_eq_sup_sdiff_inf, exact (ha.sup_left hb).disjoint_sdiff_left, end end generalized_boolean_algebra section boolean_algebra variables {α : Type*} [boolean_algebra α] (a b c : α) lemma symm_diff_eq : a Δ b = (a ⊓ bᶜ) ⊔ (b ⊓ aᶜ) := by simp only [(Δ), sdiff_eq] @[simp] lemma symm_diff_top : a Δ ⊤ = aᶜ := by simp [symm_diff_eq] @[simp] lemma top_symm_diff : ⊤ Δ a = aᶜ := by rw [symm_diff_comm, symm_diff_top] lemma compl_symm_diff : (a Δ b)ᶜ = (a ⊓ b) ⊔ (aᶜ ⊓ bᶜ) := by simp only [←top_sdiff, sdiff_symm_diff, top_inf_eq] lemma symm_diff_eq_top_iff : a Δ b = ⊤ ↔ is_compl a b := by rw [symm_diff_eq_iff_sdiff_eq (@le_top _ _ a), top_sdiff, compl_eq_iff_is_compl] lemma is_compl.symm_diff_eq_top (h : is_compl a b) : a Δ b = ⊤ := (symm_diff_eq_top_iff a b).2 h @[simp] lemma compl_symm_diff_self : aᶜ Δ a = ⊤ := by simp only [symm_diff_eq, compl_compl, inf_idem, compl_sup_eq_top] @[simp] lemma symm_diff_compl_self : a Δ aᶜ = ⊤ := by rw [symm_diff_comm, compl_symm_diff_self] lemma symm_diff_symm_diff_right' : a Δ (b Δ c) = (a ⊓ b ⊓ c) ⊔ (a ⊓ bᶜ ⊓ cᶜ) ⊔ (aᶜ ⊓ b ⊓ cᶜ) ⊔ (aᶜ ⊓ bᶜ ⊓ c) := calc a Δ (b Δ c) = (a ⊓ ((b ⊓ c) ⊔ (bᶜ ⊓ cᶜ))) ⊔ (((b ⊓ cᶜ) ⊔ (c ⊓ bᶜ)) ⊓ aᶜ) : by rw [symm_diff_eq, compl_symm_diff, symm_diff_eq] ... = (a ⊓ b ⊓ c) ⊔ (a ⊓ bᶜ ⊓ cᶜ) ⊔ (b ⊓ cᶜ ⊓ aᶜ) ⊔ (c ⊓ bᶜ ⊓ aᶜ) : by rw [inf_sup_left, inf_sup_right, ←sup_assoc, ←inf_assoc, ←inf_assoc] ... = (a ⊓ b ⊓ c) ⊔ (a ⊓ bᶜ ⊓ cᶜ) ⊔ (aᶜ ⊓ b ⊓ cᶜ) ⊔ (aᶜ ⊓ bᶜ ⊓ c) : begin congr' 1, { congr' 1, rw [inf_comm, inf_assoc], }, { apply inf_left_right_swap } end end boolean_algebra
4ea8d985f3113dc1bbefcaabf61ff4dff8eaa01e
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/ring_theory/artinian.lean
5f153c27ece09006d143b4bf8a7262f4ad645261
[ "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
17,670
lean
/- Copyright (c) 2021 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import linear_algebra.basic import linear_algebra.prod import linear_algebra.pi import data.set_like.fintype import linear_algebra.linear_independent import tactic.linarith import algebra.algebra.basic import ring_theory.noetherian import ring_theory.jacobson_ideal import ring_theory.nilpotent import ring_theory.nakayama /-! # Artinian rings and modules A module satisfying these equivalent conditions is said to be an *Artinian* R-module if every decreasing chain of submodules is eventually constant, or equivalently, if the relation `<` on submodules is well founded. A ring is an *Artinian ring* if it is Artinian as a module over itself. (Note that we do not assume yet that our rings are commutative, so perhaps this should be called "left Artinian". To avoid cumbersome names once we specialize to the commutative case, we don't make this explicit in the declaration names.) ## Main definitions Let `R` be a ring and let `M` and `P` be `R`-modules. Let `N` be an `R`-submodule of `M`. * `is_artinian R M` is the proposition that `M` is a Artinian `R`-module. It is a class, implemented as the predicate that the `<` relation on submodules is well founded. ## References * [M. F. Atiyah and I. G. Macdonald, *Introduction to commutative algebra*][atiyah-macdonald] * [samuel] ## Tags Artinian, artinian, Artinian ring, Artinian module, artinian ring, artinian module -/ open set open_locale big_operators pointwise /-- `is_artinian R M` is the proposition that `M` is an Artinian `R`-module, implemented as the well-foundedness of submodule inclusion. -/ class is_artinian (R M) [semiring R] [add_comm_monoid M] [module R M] : Prop := (well_founded_submodule_lt [] : well_founded ((<) : submodule R M → submodule R M → Prop)) section variables {R : Type*} {M : Type*} {P : Type*} {N : Type*} variables [ring R] [add_comm_group M] [add_comm_group P] [add_comm_group N] variables [module R M] [module R P] [module R N] open is_artinian include R theorem is_artinian_of_injective (f : M →ₗ[R] P) (h : function.injective f) [is_artinian R P] : is_artinian R M := ⟨subrelation.wf (λ A B hAB, show A.map f < B.map f, from submodule.map_strict_mono_of_injective h hAB) (inv_image.wf (submodule.map f) (is_artinian.well_founded_submodule_lt R P))⟩ instance is_artinian_submodule' [is_artinian R M] (N : submodule R M) : is_artinian R N := is_artinian_of_injective N.subtype subtype.val_injective lemma is_artinian_of_le {s t : submodule R M} [ht : is_artinian R t] (h : s ≤ t) : is_artinian R s := is_artinian_of_injective (submodule.of_le h) (submodule.of_le_injective h) variable (M) theorem is_artinian_of_surjective (f : M →ₗ[R] P) (hf : function.surjective f) [is_artinian R M] : is_artinian R P := ⟨subrelation.wf (λ A B hAB, show A.comap f < B.comap f, from submodule.comap_strict_mono_of_surjective hf hAB) (inv_image.wf (submodule.comap f) (is_artinian.well_founded_submodule_lt _ _))⟩ variable {M} theorem is_artinian_of_linear_equiv (f : M ≃ₗ[R] P) [is_artinian R M] : is_artinian R P := is_artinian_of_surjective _ f.to_linear_map f.to_equiv.surjective theorem is_artinian_of_range_eq_ker [is_artinian R M] [is_artinian R P] (f : M →ₗ[R] N) (g : N →ₗ[R] P) (hf : function.injective f) (hg : function.surjective g) (h : f.range = g.ker) : is_artinian R N := ⟨well_founded_lt_exact_sequence (is_artinian.well_founded_submodule_lt _ _) (is_artinian.well_founded_submodule_lt _ _) f.range (submodule.map f) (submodule.comap f) (submodule.comap g) (submodule.map g) (submodule.gci_map_comap hf) (submodule.gi_map_comap hg) (by simp [submodule.map_comap_eq, inf_comm]) (by simp [submodule.comap_map_eq, h])⟩ instance is_artinian_prod [is_artinian R M] [is_artinian R P] : is_artinian R (M × P) := is_artinian_of_range_eq_ker (linear_map.inl R M P) (linear_map.snd R M P) linear_map.inl_injective linear_map.snd_surjective (linear_map.range_inl R M P) @[instance, priority 100] lemma is_artinian_of_fintype [fintype M] : is_artinian R M := ⟨fintype.well_founded_of_trans_of_irrefl _⟩ local attribute [elab_as_eliminator] fintype.induction_empty_option instance is_artinian_pi {R ι : Type*} [fintype ι] : Π {M : ι → Type*} [ring R] [Π i, add_comm_group (M i)], by exactI Π [Π i, module R (M i)], by exactI Π [∀ i, is_artinian R (M i)], is_artinian R (Π i, M i) := fintype.induction_empty_option (begin introsI α β e hα M _ _ _ _, exact is_artinian_of_linear_equiv (linear_equiv.Pi_congr_left R M e) end) (by { introsI M _ _ _ _, apply_instance }) (begin introsI α _ ih M _ _ _ _, exact is_artinian_of_linear_equiv (linear_equiv.pi_option_equiv_prod R).symm, end) ι /-- A version of `is_artinian_pi` for non-dependent functions. We need this instance because sometimes Lean fails to apply the dependent version in non-dependent settings (e.g., it fails to prove that `ι → ℝ` is finite dimensional over `ℝ`). -/ instance is_artinian_pi' {R ι M : Type*} [ring R] [add_comm_group M] [module R M] [fintype ι] [is_artinian R M] : is_artinian R (ι → M) := is_artinian_pi end open is_artinian submodule function section variables {R M : Type*} [ring R] [add_comm_group M] [module R M] theorem is_artinian_iff_well_founded : is_artinian R M ↔ well_founded ((<) : submodule R M → submodule R M → Prop) := ⟨λ h, h.1, is_artinian.mk⟩ variables {R M} lemma is_artinian.finite_of_linear_independent [nontrivial R] [is_artinian R M] {s : set M} (hs : linear_independent R (coe : s → M)) : s.finite := begin refine classical.by_contradiction (λ hf, (rel_embedding.well_founded_iff_no_descending_seq.1 (well_founded_submodule_lt R M)).elim' _), have f : ℕ ↪ s, from @infinite.nat_embedding s ⟨λ f, hf ⟨f⟩⟩, have : ∀ n, (coe ∘ f) '' {m | n ≤ m} ⊆ s, { rintros n x ⟨y, hy₁, hy₂⟩, subst hy₂, exact (f y).2 }, have : ∀ a b : ℕ, a ≤ b ↔ span R ((coe ∘ f) '' {m | b ≤ m}) ≤ span R ((coe ∘ f) '' {m | a ≤ m}), { assume a b, rw [span_le_span_iff hs (this b) (this a), set.image_subset_image_iff (subtype.coe_injective.comp f.injective), set.subset_def], simp only [set.mem_set_of_eq], exact ⟨λ hab x, le_trans hab, λ h, (h _ le_rfl)⟩ }, exact ⟨⟨λ n, span R ((coe ∘ f) '' {m | n ≤ m}), λ x y, by simp [le_antisymm_iff, (this _ _).symm] {contextual := tt}⟩, begin intros a b, conv_rhs { rw [gt, lt_iff_le_not_le, this, this, ← lt_iff_le_not_le] }, simp end⟩ end /-- A module is Artinian iff every nonempty set of submodules has a minimal submodule among them. -/ theorem set_has_minimal_iff_artinian : (∀ a : set $ submodule R M, a.nonempty → ∃ M' ∈ a, ∀ I ∈ a, I ≤ M' → I = M') ↔ is_artinian R M := by rw [is_artinian_iff_well_founded, well_founded.well_founded_iff_has_min'] theorem is_artinian.set_has_minimal [is_artinian R M] (a : set $ submodule R M) (ha : a.nonempty) : ∃ M' ∈ a, ∀ I ∈ a, I ≤ M' → I = M' := set_has_minimal_iff_artinian.mpr ‹_› a ha /-- A module is Artinian iff every decreasing chain of submodules stabilizes. -/ theorem monotone_stabilizes_iff_artinian : (∀ (f : ℕ →o order_dual (submodule R M)), ∃ n, ∀ m, n ≤ m → f n = f m) ↔ is_artinian R M := by rw [is_artinian_iff_well_founded]; exact (well_founded.monotone_chain_condition (order_dual (submodule R M))).symm theorem is_artinian.monotone_stabilizes [is_artinian R M] (f : ℕ →o order_dual (submodule R M)) : ∃ n, ∀ m, n ≤ m → f n = f m := monotone_stabilizes_iff_artinian.mpr ‹_› f /-- If `∀ I > J, P I` implies `P J`, then `P` holds for all submodules. -/ lemma is_artinian.induction [is_artinian R M] {P : submodule R M → Prop} (hgt : ∀ I, (∀ J < I, P J) → P I) (I : submodule R M) : P I := well_founded.recursion (well_founded_submodule_lt R M) I hgt /-- For any endomorphism of a Artinian module, there is some nontrivial iterate with disjoint kernel and range. -/ theorem is_artinian.exists_endomorphism_iterate_ker_sup_range_eq_top [I : is_artinian R M] (f : M →ₗ[R] M) : ∃ n : ℕ, n ≠ 0 ∧ (f ^ n).ker ⊔ (f ^ n).range = ⊤ := begin obtain ⟨n, w⟩ := monotone_stabilizes_iff_artinian.mpr I (f.iterate_range.comp ⟨λ n, n+1, λ n m w, by linarith⟩), specialize w ((n + 1) + n) (by linarith), dsimp at w, refine ⟨n + 1, nat.succ_ne_zero _, _⟩, simp_rw [eq_top_iff', mem_sup], intro x, have : (f^(n + 1)) x ∈ (f ^ ((n + 1) + n + 1)).range, { rw ← w, exact mem_range_self _ }, rcases this with ⟨y, hy⟩, use x - (f ^ (n+1)) y, split, { rw [linear_map.mem_ker, linear_map.map_sub, ← hy, sub_eq_zero, pow_add], simp [iterate_add_apply], }, { use (f^ (n+1)) y, simp } end /-- Any injective endomorphism of an Artinian module is surjective. -/ theorem is_artinian.surjective_of_injective_endomorphism [is_artinian R M] (f : M →ₗ[R] M) (s : injective f) : surjective f := begin obtain ⟨n, ne, w⟩ := is_artinian.exists_endomorphism_iterate_ker_sup_range_eq_top f, rw [linear_map.ker_eq_bot.mpr (linear_map.iterate_injective s n), bot_sup_eq, linear_map.range_eq_top] at w, exact linear_map.surjective_of_iterate_surjective ne w, end /-- Any injective endomorphism of an Artinian module is bijective. -/ theorem is_artinian.bijective_of_injective_endomorphism [is_artinian R M] (f : M →ₗ[R] M) (s : injective f) : bijective f := ⟨s, is_artinian.surjective_of_injective_endomorphism f s⟩ /-- A sequence `f` of submodules of a artinian module, with the supremum `f (n+1)` and the infinum of `f 0`, ..., `f n` being ⊤, is eventually ⊤. -/ lemma is_artinian.disjoint_partial_infs_eventually_top [I : is_artinian R M] (f : ℕ → submodule R M) (h : ∀ n, disjoint (partial_sups (order_dual.to_dual ∘ f) n) (order_dual.to_dual (f (n+1)))) : ∃ n : ℕ, ∀ m, n ≤ m → f m = ⊤ := begin -- A little off-by-one cleanup first: suffices t : ∃ n : ℕ, ∀ m, n ≤ m → order_dual.to_dual f (m+1) = ⊤, { obtain ⟨n, w⟩ := t, use n+1, rintros (_|m) p, { cases p, }, { apply w, exact nat.succ_le_succ_iff.mp p }, }, obtain ⟨n, w⟩ := monotone_stabilizes_iff_artinian.mpr I (partial_sups (order_dual.to_dual ∘ f)), exact ⟨n, (λ m p, eq_bot_of_disjoint_absorbs (h m) ((eq.symm (w (m + 1) (le_add_right p))).trans (w m p)))⟩ end universe w variables {N : Type w} [add_comm_group N] [module R N] -- TODO: Prove this for artinian modules -- /-- -- If `M ⊕ N` embeds into `M`, for `M` noetherian over `R`, then `N` is trivial. -- -/ -- noncomputable def is_noetherian.equiv_punit_of_prod_injective [is_noetherian R M] -- (f : M × N →ₗ[R] M) (i : injective f) : N ≃ₗ[R] punit.{w+1} := -- begin -- apply nonempty.some, -- obtain ⟨n, w⟩ := is_noetherian.disjoint_partial_sups_eventually_bot (f.tailing i) -- (f.tailings_disjoint_tailing i), -- specialize w n (le_refl n), -- apply nonempty.intro, -- refine (f.tailing_linear_equiv i n).symm.trans _, -- rw w, -- exact submodule.bot_equiv_punit, -- end end /-- A ring is Artinian if it is Artinian as a module over itself. -/ class is_artinian_ring (R) [ring R] extends is_artinian R R : Prop theorem is_artinian_ring_iff {R} [ring R] : is_artinian_ring R ↔ is_artinian R R := ⟨λ h, h.1, @is_artinian_ring.mk _ _⟩ theorem ring.is_artinian_of_zero_eq_one {R} [ring R] (h01 : (0 : R) = 1) : is_artinian_ring R := by haveI := subsingleton_of_zero_eq_one h01; haveI := fintype.of_subsingleton (0:R); split; apply_instance theorem is_artinian_of_submodule_of_artinian (R M) [ring R] [add_comm_group M] [module R M] (N : submodule R M) (h : is_artinian R M) : is_artinian R N := by apply_instance theorem is_artinian_of_quotient_of_artinian (R) [ring R] (M) [add_comm_group M] [module R M] (N : submodule R M) (h : is_artinian R M) : is_artinian R (M ⧸ N) := is_artinian_of_surjective M (submodule.mkq N) (submodule.quotient.mk_surjective N) /-- If `M / S / R` is a scalar tower, and `M / R` is Artinian, then `M / S` is also Artinian. -/ theorem is_artinian_of_tower (R) {S M} [comm_ring R] [ring S] [add_comm_group M] [algebra R S] [module S M] [module R M] [is_scalar_tower R S M] (h : is_artinian R M) : is_artinian S M := begin rw is_artinian_iff_well_founded at h ⊢, refine (submodule.restrict_scalars_embedding R S M).well_founded h end theorem is_artinian_of_fg_of_artinian {R M} [ring R] [add_comm_group M] [module R M] (N : submodule R M) [is_artinian_ring R] (hN : N.fg) : is_artinian R N := let ⟨s, hs⟩ := hN in begin haveI := classical.dec_eq M, haveI := classical.dec_eq R, letI : is_artinian R R := by apply_instance, have : ∀ x ∈ s, x ∈ N, from λ x hx, hs ▸ submodule.subset_span hx, refine @@is_artinian_of_surjective ((↑s : set M) → R) _ _ _ (pi.module _ _ _) _ _ _ is_artinian_pi, { fapply linear_map.mk, { exact λ f, ⟨∑ i in s.attach, f i • i.1, N.sum_mem (λ c _, N.smul_mem _ $ this _ c.2)⟩ }, { intros f g, apply subtype.eq, change ∑ i in s.attach, (f i + g i) • _ = _, simp only [add_smul, finset.sum_add_distrib], refl }, { intros c f, apply subtype.eq, change ∑ i in s.attach, (c • f i) • _ = _, simp only [smul_eq_mul, mul_smul], exact finset.smul_sum.symm } }, rintro ⟨n, hn⟩, change n ∈ N at hn, rw [← hs, ← set.image_id ↑s, finsupp.mem_span_image_iff_total] at hn, rcases hn with ⟨l, hl1, hl2⟩, refine ⟨λ x, l x, subtype.ext _⟩, change ∑ i in s.attach, l i • (i : M) = n, rw [@finset.sum_attach M M s _ (λ i, l i • i), ← hl2, finsupp.total_apply, finsupp.sum, eq_comm], refine finset.sum_subset hl1 (λ x _ hx, _), rw [finsupp.not_mem_support_iff.1 hx, zero_smul] end lemma is_artinian_of_fg_of_artinian' {R M} [ring R] [add_comm_group M] [module R M] [is_artinian_ring R] (h : (⊤ : submodule R M).fg) : is_artinian R M := have is_artinian R (⊤ : submodule R M), from is_artinian_of_fg_of_artinian _ h, by exactI is_artinian_of_linear_equiv (linear_equiv.of_top (⊤ : submodule R M) rfl) /-- In a module over a artinian ring, the submodule generated by finitely many vectors is artinian. -/ theorem is_artinian_span_of_finite (R) {M} [ring R] [add_comm_group M] [module R M] [is_artinian_ring R] {A : set M} (hA : finite A) : is_artinian R (submodule.span R A) := is_artinian_of_fg_of_artinian _ (submodule.fg_def.mpr ⟨A, hA, rfl⟩) theorem is_artinian_ring_of_surjective (R) [comm_ring R] (S) [comm_ring S] (f : R →+* S) (hf : function.surjective f) [H : is_artinian_ring R] : is_artinian_ring S := begin rw [is_artinian_ring_iff, is_artinian_iff_well_founded] at H ⊢, exact order_embedding.well_founded (ideal.order_embedding_of_surjective f hf) H, end instance is_artinian_ring_range {R} [comm_ring R] {S} [comm_ring S] (f : R →+* S) [is_artinian_ring R] : is_artinian_ring f.range := is_artinian_ring_of_surjective R f.range f.range_restrict f.range_restrict_surjective theorem is_artinian_ring_of_ring_equiv (R) [comm_ring R] {S} [comm_ring S] (f : R ≃+* S) [is_artinian_ring R] : is_artinian_ring S := is_artinian_ring_of_surjective R S f.to_ring_hom f.to_equiv.surjective namespace is_artinian_ring open is_artinian variables {R : Type*} [comm_ring R] [is_artinian_ring R] lemma is_nilpotent_jacobson_bot : is_nilpotent (ideal.jacobson (⊥ : ideal R)) := begin let Jac := ideal.jacobson (⊥ : ideal R), let f : ℕ →o order_dual (ideal R) := ⟨λ n, Jac ^ n, λ _ _ h, ideal.pow_le_pow h⟩, obtain ⟨n, hn⟩ : ∃ n, ∀ m, n ≤ m → Jac ^ n = Jac ^ m := is_artinian.monotone_stabilizes f, refine ⟨n, _⟩, let J : ideal R := annihilator (Jac ^ n), suffices : J = ⊤, { have hJ : J • Jac ^ n = ⊥ := annihilator_smul (Jac ^ n), simpa only [this, top_smul, ideal.zero_eq_bot] using hJ }, by_contradiction hJ, change J ≠ ⊤ at hJ, rcases is_artinian.set_has_minimal {J' : ideal R | J < J'} ⟨⊤, hJ.lt_top⟩ with ⟨J', hJJ' : J < J', hJ' : ∀ I, J < I → I ≤ J' → I = J'⟩, rcases set_like.exists_of_lt hJJ' with ⟨x, hxJ', hxJ⟩, obtain rfl : J ⊔ ideal.span {x} = J', { refine hJ' (J ⊔ ideal.span {x}) _ _, { rw set_like.lt_iff_le_and_exists, exact ⟨le_sup_left, ⟨x, mem_sup_right (mem_span_singleton_self x), hxJ⟩⟩ }, { exact (sup_le hJJ'.le (span_le.2 (singleton_subset_iff.2 hxJ'))) } }, have : J ⊔ Jac • ideal.span {x} ≤ J ⊔ ideal.span {x}, from sup_le_sup_left (smul_le.2 (λ _ _ _, submodule.smul_mem _ _)) _, have : Jac * ideal.span {x} ≤ J, --Need version 4 of Nakayamas lemma on Stacks { classical, by_contradiction H, refine H (smul_sup_le_of_le_smul_of_le_jacobson_bot (fg_span_singleton _) le_rfl (hJ' _ _ this).ge), exact lt_of_le_of_ne le_sup_left (λ h, H $ h.symm ▸ le_sup_right) }, have : ideal.span {x} * Jac ^ (n + 1) ≤ ⊥, calc ideal.span {x} * Jac ^ (n + 1) = ideal.span {x} * Jac * Jac ^ n : by rw [pow_succ, ← mul_assoc] ... ≤ J * Jac ^ n : mul_le_mul (by rwa mul_comm) le_rfl ... = ⊥ : by simp [J], refine hxJ (mem_annihilator.2 (λ y hy, (mem_bot R).1 _)), refine this (mul_mem_mul (mem_span_singleton_self x) _), rwa [← hn (n + 1) (nat.le_succ _)] end end is_artinian_ring
e7ce99e0487fe7f0c21e2c0a0f9719affcd6165b
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/topology/algebra/group.lean
f650ac5b6357f11d2b23050b00610fc63254c189
[]
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
19,680
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.order.filter.pointwise import Mathlib.group_theory.quotient_group import Mathlib.topology.algebra.monoid import Mathlib.topology.homeomorph import Mathlib.PostPort universes w u l u_1 x u_2 namespace Mathlib /-! # Theory of topological groups This file defines the following typeclasses: * `topological_group`, `topological_add_group`: multiplicative and additive topological groups, i.e., groups with continuous `(*)` and `(⁻¹)` / `(+)` and `(-)`; * `has_continuous_sub G` means that `G` has a continuous subtraction operation. There is an instance deducing `has_continuous_sub` from `topological_group` but we use a separate typeclass because, e.g., `ℕ` and `ℝ≥0` have continuous subtraction but are not additive groups. We also define `homeomorph` versions of several `equiv`s: `homeomorph.mul_left`, `homeomorph.mul_right`, `homeomorph.inv`, and prove a few facts about neighbourhood filters in groups. ## Tags topological space, group, topological group -/ /-! ### Groups with continuous multiplication In this section we prove a few statements about groups with continuous `(*)`. -/ /-- Multiplication from the left in a topological group as a homeomorphism. -/ protected def homeomorph.add_left {G : Type w} [topological_space G] [add_group G] [has_continuous_add G] (a : G) : G ≃ₜ G := homeomorph.mk (equiv.mk (equiv.to_fun (equiv.add_left a)) (equiv.inv_fun (equiv.add_left a)) sorry sorry) @[simp] theorem homeomorph.coe_mul_left {G : Type w} [topological_space G] [group G] [has_continuous_mul G] (a : G) : ⇑(homeomorph.mul_left a) = Mul.mul a := rfl theorem homeomorph.add_left_symm {G : Type w} [topological_space G] [add_group G] [has_continuous_add G] (a : G) : homeomorph.symm (homeomorph.add_left a) = homeomorph.add_left (-a) := homeomorph.ext fun (x : G) => Eq.refl (coe_fn (homeomorph.symm (homeomorph.add_left a)) x) theorem is_open_map_mul_left {G : Type w} [topological_space G] [group G] [has_continuous_mul G] (a : G) : is_open_map fun (x : G) => a * x := homeomorph.is_open_map (homeomorph.mul_left a) theorem is_closed_map_mul_left {G : Type w} [topological_space G] [group G] [has_continuous_mul G] (a : G) : is_closed_map fun (x : G) => a * x := homeomorph.is_closed_map (homeomorph.mul_left a) /-- Multiplication from the right in a topological group as a homeomorphism. -/ protected def homeomorph.mul_right {G : Type w} [topological_space G] [group G] [has_continuous_mul G] (a : G) : G ≃ₜ G := homeomorph.mk (equiv.mk (equiv.to_fun (equiv.mul_right a)) (equiv.inv_fun (equiv.mul_right a)) sorry sorry) theorem is_open_map_add_right {G : Type w} [topological_space G] [add_group G] [has_continuous_add G] (a : G) : is_open_map fun (x : G) => x + a := homeomorph.is_open_map (homeomorph.add_right a) theorem is_closed_map_mul_right {G : Type w} [topological_space G] [group G] [has_continuous_mul G] (a : G) : is_closed_map fun (x : G) => x * a := homeomorph.is_closed_map (homeomorph.mul_right a) theorem is_open_map_div_right {G : Type w} [topological_space G] [group G] [has_continuous_mul G] (a : G) : is_open_map fun (x : G) => x / a := sorry theorem is_closed_map_div_right {G : Type w} [topological_space G] [group G] [has_continuous_mul G] (a : G) : is_closed_map fun (x : G) => x / a := sorry /-! ### Topological groups A topological group is a group in which the multiplication and inversion operations are continuous. Topological additive groups are defined in the same way. Equivalently, we can require that the division operation `λ x y, x * y⁻¹` (resp., subtraction) is continuous. -/ /-- A topological (additive) group is a group in which the addition and negation operations are continuous. -/ class topological_add_group (G : Type u) [topological_space G] [add_group G] extends has_continuous_add G where continuous_neg : continuous fun (a : G) => -a /-- A topological group is a group in which the multiplication and inversion operations are continuous. -/ class topological_group (G : Type u_1) [topological_space G] [group G] extends has_continuous_mul G where continuous_inv : continuous has_inv.inv theorem continuous_on_neg {G : Type w} [topological_space G] [add_group G] [topological_add_group G] {s : set G} : continuous_on Neg.neg s := continuous.continuous_on continuous_neg theorem continuous_within_at_neg {G : Type w} [topological_space G] [add_group G] [topological_add_group G] {s : set G} {x : G} : continuous_within_at Neg.neg s x := continuous.continuous_within_at continuous_neg theorem continuous_at_inv {G : Type w} [topological_space G] [group G] [topological_group G] {x : G} : continuous_at has_inv.inv x := continuous.continuous_at continuous_inv theorem tendsto_neg {G : Type w} [topological_space G] [add_group G] [topological_add_group G] (a : G) : filter.tendsto Neg.neg (nhds a) (nhds (-a)) := continuous_at_neg /-- If a function converges to a value in a multiplicative topological group, then its inverse converges to the inverse of this value. For the version in normed fields assuming additionally that the limit is nonzero, use `tendsto.inv'`. -/ theorem filter.tendsto.inv {α : Type u} {G : Type w} [topological_space G] [group G] [topological_group G] {f : α → G} {l : filter α} {y : G} (h : filter.tendsto f l (nhds y)) : filter.tendsto (fun (x : α) => f x⁻¹) l (nhds (y⁻¹)) := filter.tendsto.comp (continuous.tendsto continuous_inv y) h theorem continuous.inv {α : Type u} {G : Type w} [topological_space G] [group G] [topological_group G] [topological_space α] {f : α → G} (hf : continuous f) : continuous fun (x : α) => f x⁻¹ := continuous.comp continuous_inv hf theorem continuous_on.inv {α : Type u} {G : Type w} [topological_space G] [group G] [topological_group G] [topological_space α] {f : α → G} {s : set α} (hf : continuous_on f s) : continuous_on (fun (x : α) => f x⁻¹) s := continuous.comp_continuous_on continuous_inv hf theorem continuous_within_at.inv {α : Type u} {G : Type w} [topological_space G] [group G] [topological_group G] [topological_space α] {f : α → G} {s : set α} {x : α} (hf : continuous_within_at f s x) : continuous_within_at (fun (x : α) => f x⁻¹) s x := filter.tendsto.inv hf protected instance prod.topological_add_group {G : Type w} {H : Type x} [topological_space G] [add_group G] [topological_add_group G] [topological_space H] [add_group H] [topological_add_group H] : topological_add_group (G × H) := topological_add_group.mk (continuous.prod_map continuous_neg continuous_neg) /-- Inversion in a topological group as a homeomorphism. -/ protected def homeomorph.neg (G : Type w) [topological_space G] [add_group G] [topological_add_group G] : G ≃ₜ G := homeomorph.mk (equiv.mk (equiv.to_fun (equiv.neg G)) (equiv.inv_fun (equiv.neg G)) sorry sorry) theorem nhds_zero_symm (G : Type w) [topological_space G] [add_group G] [topological_add_group G] : filter.comap Neg.neg (nhds 0) = nhds 0 := Eq.trans (homeomorph.comap_nhds_eq (homeomorph.neg G) 0) (congr_arg nhds neg_zero) /-- The map `(x, y) ↦ (x, xy)` as a homeomorphism. This is a shear mapping. -/ protected def homeomorph.shear_add_right (G : Type w) [topological_space G] [add_group G] [topological_add_group G] : G × G ≃ₜ G × G := homeomorph.mk (equiv.mk (equiv.to_fun (equiv.prod_shear (equiv.refl G) equiv.add_left)) (equiv.inv_fun (equiv.prod_shear (equiv.refl G) equiv.add_left)) sorry sorry) @[simp] theorem homeomorph.shear_mul_right_coe (G : Type w) [topological_space G] [group G] [topological_group G] : ⇑(homeomorph.shear_mul_right G) = fun (z : G × G) => (prod.fst z, prod.fst z * prod.snd z) := rfl @[simp] theorem homeomorph.shear_mul_right_symm_coe (G : Type w) [topological_space G] [group G] [topological_group G] : ⇑(homeomorph.symm (homeomorph.shear_mul_right G)) = fun (z : G × G) => (prod.fst z, prod.fst z⁻¹ * prod.snd z) := rfl theorem inv_closure {G : Type w} [topological_space G] [group G] [topological_group G] (s : set G) : closure s⁻¹ = closure (s⁻¹) := homeomorph.preimage_closure (homeomorph.inv G) s theorem exists_nhds_half_neg {G : Type w} [topological_space G] [add_group G] [topological_add_group G] {s : set G} (hs : s ∈ nhds 0) : ∃ (V : set G), ∃ (H : V ∈ nhds 0), ∀ (v : G), v ∈ V → ∀ (w : G), w ∈ V → v - w ∈ s := sorry theorem nhds_translation_mul_inv {G : Type w} [topological_space G] [group G] [topological_group G] (x : G) : filter.comap (fun (y : G) => y * (x⁻¹)) (nhds 1) = nhds x := sorry @[simp] theorem map_mul_left_nhds {G : Type w} [topological_space G] [group G] [topological_group G] (x : G) (y : G) : filter.map (Mul.mul x) (nhds y) = nhds (x * y) := homeomorph.map_nhds_eq (homeomorph.mul_left x) y theorem map_mul_left_nhds_one {G : Type w} [topological_space G] [group G] [topological_group G] (x : G) : filter.map (Mul.mul x) (nhds 1) = nhds x := sorry theorem topological_group.ext {G : Type u_1} [group G] {t : topological_space G} {t' : topological_space G} (tg : topological_group G) (tg' : topological_group G) (h : nhds 1 = nhds 1) : t = t' := sorry theorem topological_group.of_nhds_aux {G : Type u_1} [group G] [topological_space G] (hinv : filter.tendsto (fun (x : G) => x⁻¹) (nhds 1) (nhds 1)) (hleft : ∀ (x₀ : G), nhds x₀ = filter.map (fun (x : G) => x₀ * x) (nhds 1)) (hconj : ∀ (x₀ : G), filter.map (fun (x : G) => x₀ * x * (x₀⁻¹)) (nhds 1) ≤ nhds 1) : continuous fun (x : G) => x⁻¹ := sorry theorem topological_add_group.of_nhds_zero' {G : Type (max u_1 u_2)} [add_group G] [topological_space G] (hmul : filter.tendsto (function.uncurry Add.add) (filter.prod (nhds 0) (nhds 0)) (nhds 0)) (hinv : filter.tendsto (fun (x : G) => -x) (nhds 0) (nhds 0)) (hleft : ∀ (x₀ : G), nhds x₀ = filter.map (fun (x : G) => x₀ + x) (nhds 0)) (hright : ∀ (x₀ : G), nhds x₀ = filter.map (fun (x : G) => x + x₀) (nhds 0)) : topological_add_group G := sorry theorem topological_add_group.of_nhds_zero {G : Type (max u_1 u_2)} [add_group G] [topological_space G] (hmul : filter.tendsto (function.uncurry Add.add) (filter.prod (nhds 0) (nhds 0)) (nhds 0)) (hinv : filter.tendsto (fun (x : G) => -x) (nhds 0) (nhds 0)) (hleft : ∀ (x₀ : G), nhds x₀ = filter.map (fun (x : G) => x₀ + x) (nhds 0)) (hconj : ∀ (x₀ : G), filter.tendsto (fun (x : G) => x₀ + x + -x₀) (nhds 0) (nhds 0)) : topological_add_group G := topological_add_group.mk (topological_add_group.of_nhds_aux hinv hleft hconj) theorem topological_add_group.of_comm_of_nhds_zero {G : Type (max u_1 u_2)} [add_comm_group G] [topological_space G] (hmul : filter.tendsto (function.uncurry Add.add) (filter.prod (nhds 0) (nhds 0)) (nhds 0)) (hinv : filter.tendsto (fun (x : G) => -x) (nhds 0) (nhds 0)) (hleft : ∀ (x₀ : G), nhds x₀ = filter.map (fun (x : G) => x₀ + x) (nhds 0)) : topological_add_group G := sorry protected instance quotient_group.quotient.topological_space {G : Type u_1} [group G] [topological_space G] (N : subgroup G) : topological_space (quotient_group.quotient N) := quotient.topological_space theorem quotient_group.is_open_map_coe {G : Type w} [topological_space G] [group G] [topological_group G] (N : subgroup G) : is_open_map coe := sorry protected instance topological_add_group_quotient {G : Type w} [topological_space G] [add_group G] [topological_add_group G] (N : add_subgroup G) [add_subgroup.normal N] : topological_add_group (quotient_add_group.quotient N) := topological_add_group.mk (eq.mpr ((fun (f f_1 : quotient_add_group.quotient N → quotient_add_group.quotient N) (e_3 : f = f_1) => congr_arg continuous e_3) Neg.neg (quotient.lift (coe ∘ fun (a : G) => -a) (quotient_add_group.div_inv_monoid._proof_5 N)) (Eq.refl Neg.neg)) (continuous_quotient_lift (quotient_add_group.div_inv_monoid._proof_5 N) (continuous.comp continuous_quot_mk continuous_neg))) /-- A typeclass saying that `λ p : G × G, p.1 - p.2` is a continuous function. This property automatically holds for topological additive groups but it also holds, e.g., for `ℝ≥0`. -/ class has_continuous_sub (G : Type u_1) [topological_space G] [Sub G] where continuous_sub : continuous fun (p : G × G) => prod.fst p - prod.snd p protected instance topological_add_group.to_has_continuous_sub {G : Type w} [topological_space G] [add_group G] [topological_add_group G] : has_continuous_sub G := has_continuous_sub.mk (eq.mpr (id ((fun (f f_1 : G × G → G) (e_3 : f = f_1) => congr_arg continuous e_3) (fun (p : G × G) => prod.fst p - prod.snd p) (fun (p : G × G) => prod.fst p + -prod.snd p) (funext fun (p : G × G) => sub_eq_add_neg (prod.fst p) (prod.snd p)))) (continuous.add continuous_fst (continuous.neg continuous_snd))) theorem filter.tendsto.sub {α : Type u} {G : Type w} [topological_space G] [Sub G] [has_continuous_sub G] {f : α → G} {g : α → G} {l : filter α} {a : G} {b : G} (hf : filter.tendsto f l (nhds a)) (hg : filter.tendsto g l (nhds b)) : filter.tendsto (fun (x : α) => f x - g x) l (nhds (a - b)) := filter.tendsto.comp (continuous.tendsto continuous_sub (a, b)) (filter.tendsto.prod_mk_nhds hf hg) theorem continuous.sub {α : Type u} {G : Type w} [topological_space G] [Sub G] [has_continuous_sub G] [topological_space α] {f : α → G} {g : α → G} (hf : continuous f) (hg : continuous g) : continuous fun (x : α) => f x - g x := continuous.comp continuous_sub (continuous.prod_mk hf hg) theorem continuous_within_at.sub {α : Type u} {G : Type w} [topological_space G] [Sub G] [has_continuous_sub G] [topological_space α] {f : α → G} {g : α → G} {s : set α} {x : α} (hf : continuous_within_at f s x) (hg : continuous_within_at g s x) : continuous_within_at (fun (x : α) => f x - g x) s x := filter.tendsto.sub hf hg theorem continuous_on.sub {α : Type u} {G : Type w} [topological_space G] [Sub G] [has_continuous_sub G] [topological_space α] {f : α → G} {g : α → G} {s : set α} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (fun (x : α) => f x - g x) s := fun (x : α) (hx : x ∈ s) => continuous_within_at.sub (hf x hx) (hg x hx) theorem nhds_translation {G : Type w} [topological_space G] [add_group G] [topological_add_group G] (x : G) : filter.comap (fun (y : G) => y - x) (nhds 0) = nhds x := sorry /-- additive group with a neighbourhood around 0. Only used to construct a topology and uniform space. This is currently only available for commutative groups, but it can be extended to non-commutative groups too. -/ class add_group_with_zero_nhd (G : Type u) extends add_comm_group G where Z : filter G zero_Z : pure 0 ≤ Z sub_Z : filter.tendsto (fun (p : G × G) => prod.fst p - prod.snd p) (filter.prod Z Z) Z namespace add_group_with_zero_nhd protected instance topological_space (G : Type w) [add_group_with_zero_nhd G] : topological_space G := topological_space.mk_of_nhds fun (a : G) => filter.map (fun (x : G) => x + a) (Z G) theorem neg_Z {G : Type w} [add_group_with_zero_nhd G] : filter.tendsto (fun (a : G) => -a) (Z G) (Z G) := sorry theorem add_Z {G : Type w} [add_group_with_zero_nhd G] : filter.tendsto (fun (p : G × G) => prod.fst p + prod.snd p) (filter.prod (Z G) (Z G)) (Z G) := sorry theorem exists_Z_half {G : Type w} [add_group_with_zero_nhd G] {s : set G} (hs : s ∈ Z G) : ∃ (V : set G), ∃ (H : V ∈ Z G), ∀ (v : G), v ∈ V → ∀ (w : G), w ∈ V → v + w ∈ s := sorry theorem nhds_eq {G : Type w} [add_group_with_zero_nhd G] (a : G) : nhds a = filter.map (fun (x : G) => x + a) (Z G) := sorry theorem nhds_zero_eq_Z {G : Type w} [add_group_with_zero_nhd G] : nhds 0 = Z G := sorry protected instance has_continuous_add {G : Type w} [add_group_with_zero_nhd G] : has_continuous_add G := has_continuous_add.mk (iff.mpr continuous_iff_continuous_at fun (_x : G × G) => sorry) protected instance topological_add_group {G : Type w} [add_group_with_zero_nhd G] : topological_add_group G := sorry end add_group_with_zero_nhd theorem is_open.add_left {G : Type w} [topological_space G] [add_group G] [topological_add_group G] {s : set G} {t : set G} : is_open t → is_open (s + t) := fun (ht : is_open t) => eq.mpr (id (Eq._oldrec (Eq.refl (is_open (s + t))) (Eq.symm set.Union_add_left_image))) (is_open_Union fun (a : G) => is_open_Union fun (ha : a ∈ s) => (fun (a : G) => is_open_map_add_left a t ht) a) theorem is_open.add_right {G : Type w} [topological_space G] [add_group G] [topological_add_group G] {s : set G} {t : set G} : is_open s → is_open (s + t) := fun (hs : is_open s) => eq.mpr (id (Eq._oldrec (Eq.refl (is_open (s + t))) (Eq.symm set.Union_add_right_image))) (is_open_Union fun (a : G) => is_open_Union fun (ha : a ∈ t) => (fun (a : G) => is_open_map_add_right a s hs) a) theorem topological_group.t1_space (G : Type w) [topological_space G] [group G] [topological_group G] (h : is_closed (singleton 1)) : t1_space G := sorry theorem topological_group.regular_space (G : Type w) [topological_space G] [group G] [topological_group G] [t1_space G] : regular_space G := sorry theorem topological_group.t2_space (G : Type w) [topological_space G] [group G] [topological_group G] [t1_space G] : t2_space G := regular_space.t2_space G /-! Some results about an open set containing the product of two sets in a topological group. -/ /-- Given a compact set `K` inside an open set `U`, there is a open neighborhood `V` of `1` such that `KV ⊆ U`. -/ theorem compact_open_separated_add {G : Type w} [topological_space G] [add_group G] [topological_add_group G] {K : set G} {U : set G} (hK : is_compact K) (hU : is_open U) (hKU : K ⊆ U) : ∃ (V : set G), is_open V ∧ 0 ∈ V ∧ K + V ⊆ U := sorry /-- A compact set is covered by finitely many left multiplicative translates of a set with non-empty interior. -/ theorem compact_covered_by_add_left_translates {G : Type w} [topological_space G] [add_group G] [topological_add_group G] {K : set G} {V : set G} (hK : is_compact K) (hV : set.nonempty (interior V)) : ∃ (t : finset G), K ⊆ set.Union fun (g : G) => set.Union fun (H : g ∈ t) => (fun (h : G) => g + h) ⁻¹' V := sorry /-- Every locally compact separable topological group is σ-compact. Note: this is not true if we drop the topological group hypothesis. -/ protected instance separable_locally_compact_group.sigma_compact_space {G : Type w} [topological_space G] [group G] [topological_group G] [topological_space.separable_space G] [locally_compact_space G] : sigma_compact_space G := sorry theorem nhds_add {G : Type w} [topological_space G] [add_comm_group G] [topological_add_group G] (x : G) (y : G) : nhds (x + y) = nhds x + nhds y := sorry theorem nhds_is_mul_hom {G : Type w} [topological_space G] [comm_group G] [topological_group G] : is_mul_hom fun (x : G) => nhds x := is_mul_hom.mk fun (_x _x_1 : G) => nhds_mul _x _x_1 protected instance additive.topological_add_group {G : Type u_1} [h : topological_space G] [group G] [topological_group G] : topological_add_group (additive G) := topological_add_group.mk continuous_inv protected instance multiplicative.topological_group {G : Type u_1} [h : topological_space G] [add_group G] [topological_add_group G] : topological_group (multiplicative G) := topological_group.mk continuous_neg
a1a50bc0c1f7141adb73139ae14f2e7a9f9a2177
21871395eaf77834250a3f1b20624be105ae17be
/prelim.lean
e55ea0789de560ad090eaa7bc7ecc3e3a3384c26
[]
no_license
daniel-carranza/2-adj-lean-workspace
a60a16d99993351a08860575831ec7c5b801c39f
e3e9b99883e5056d2de1856b8adcb2f4625546e6
refs/heads/master
1,666,176,695,426
1,592,006,470,000
1,592,006,470,000
271,910,711
0
0
null
null
null
null
UTF-8
Lean
false
false
1,761
lean
/- Authors: Daniel Carranza, Jonathon Chang, Ryan Sandford Under the supervision of Chris Kapulkin Some auxiliary lemmas used in adj.lean and two_adj.lean Last updated: 2020-06-12 -/ import hott.init hott.types.equiv open hott hott_theory universes u v namespace pi variable {A : Type u} variable {B : Type v} variable {C : Type _} @[hott] def precompose (f : A → B) : (B → C) → (A → C) := λg, g ∘ f @[hott] def precompose_equiv_is_equiv (f : A → B) [H: is_equiv f] : is_equiv (@precompose A B C f) := is_equiv.adjointify (precompose f) (λg: A → C, g ∘ f⁻¹ᶠ) (λg, eq_of_homotopy (λx, ap g (is_equiv.left_inv f x) )) (λg, eq_of_homotopy (λx, ap g (is_equiv.right_inv f x))) @[hott] def precompose_equiv (f : A → B) [H : is_equiv f] : (B → C) ≃ (A → C) := equiv.mk (precompose f) (precompose_equiv_is_equiv f) @[hott] def postcompose (f : B → C) : (A → B) → (A → C) := λg, f ∘ g @[hott] def postcompose_equiv_is_equiv (f : B → C) [H : is_equiv f] : is_equiv (@postcompose A B C f) := is_equiv.adjointify (postcompose f) (λg : A → C, f⁻¹ᶠ ∘ g) (λg, eq_of_homotopy (λx, is_equiv.right_inv f _)) (λg, eq_of_homotopy (λx, is_equiv.left_inv f _)) @[hott] def postcompose_equiv (f : B → C) [H : is_equiv f] : (A → B) ≃ (A → C) := equiv.mk (postcompose f) (postcompose_equiv_is_equiv f) end pi namespace eq @[hott] def inv_inv_htpy {A : Type u} {P : A → Type _} {f g : Π(x : A), P x} (H : f ~ g) : (H⁻¹ʰᵗʸ)⁻¹ʰᵗʸ = H := eq_of_homotopy (λx, inv_inv (H x)) end eq
41c8a4bb4dfa45e5a785336cf8e2e7e3b0ce342f
4727251e0cd73359b15b664c3170e5d754078599
/src/data/subtype.lean
7347ff7aa6a5d90ef6519f6219d83a80849f8036
[ "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
7,994
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import logic.function.basic import tactic.ext import tactic.lint import tactic.simps /-! # Subtypes This file provides basic API for subtypes, which are defined in core. A subtype is a type made from restricting another type, say `α`, to its elements that satisfy some predicate, say `p : α → Prop`. Specifically, it is the type of pairs `⟨val, property⟩` where `val : α` and `property : p val`. It is denoted `subtype p` and notation `{val : α // p val}` is available. A subtype has a natural coercion to the parent type, by coercing `⟨val, property⟩` to `val`. As such, subtypes can be thought of as bundled sets, the difference being that elements of a set are still of type `α` while elements of a subtype aren't. -/ open function namespace subtype variables {α β γ : Sort*} {p q : α → Prop} /-- See Note [custom simps projection] -/ def simps.coe (x : subtype p) : α := x initialize_simps_projections subtype (val → coe) /-- A version of `x.property` or `x.2` where `p` is syntactically applied to the coercion of `x` instead of `x.1`. A similar result is `subtype.mem` in `data.set.basic`. -/ lemma prop (x : subtype p) : p x := x.2 @[simp] lemma val_eq_coe {x : subtype p} : x.1 = ↑x := rfl @[simp] protected theorem «forall» {q : {a // p a} → Prop} : (∀ x, q x) ↔ (∀ a b, q ⟨a, b⟩) := ⟨assume h a b, h ⟨a, b⟩, assume h ⟨a, b⟩, h a b⟩ /-- An alternative version of `subtype.forall`. This one is useful if Lean cannot figure out `q` when using `subtype.forall` from right to left. -/ protected theorem forall' {q : ∀ x, p x → Prop} : (∀ x h, q x h) ↔ (∀ x : {a // p a}, q x x.2) := (@subtype.forall _ _ (λ x, q x.1 x.2)).symm @[simp] protected theorem «exists» {q : {a // p a} → Prop} : (∃ x, q x) ↔ (∃ a b, q ⟨a, b⟩) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ /-- An alternative version of `subtype.exists`. This one is useful if Lean cannot figure out `q` when using `subtype.exists` from right to left. -/ protected theorem exists' {q : ∀x, p x → Prop} : (∃ x h, q x h) ↔ (∃ x : {a // p a}, q x x.2) := (@subtype.exists _ _ (λ x, q x.1 x.2)).symm @[ext] protected lemma ext : ∀ {a1 a2 : {x // p x}}, (a1 : α) = (a2 : α) → a1 = a2 | ⟨x, h1⟩ ⟨.(x), h2⟩ rfl := rfl lemma ext_iff {a1 a2 : {x // p x}} : a1 = a2 ↔ (a1 : α) = (a2 : α) := ⟨congr_arg _, subtype.ext⟩ lemma heq_iff_coe_eq (h : ∀ x, p x ↔ q x) {a1 : {x // p x}} {a2 : {x // q x}} : a1 == a2 ↔ (a1 : α) = (a2 : α) := eq.rec (λ a2', heq_iff_eq.trans ext_iff) (funext $ λ x, propext (h x)) a2 lemma heq_iff_coe_heq {α β : Sort*} {p : α → Prop} {q : β → Prop} {a : {x // p x}} {b : {y // q y}} (h : α = β) (h' : p == q) : a == b ↔ (a : α) == (b : β) := by { subst h, subst h', rw [heq_iff_eq, heq_iff_eq, ext_iff] } lemma ext_val {a1 a2 : {x // p x}} : a1.1 = a2.1 → a1 = a2 := subtype.ext lemma ext_iff_val {a1 a2 : {x // p x}} : a1 = a2 ↔ a1.1 = a2.1 := ext_iff @[simp] theorem coe_eta (a : {a // p a}) (h : p a) : mk ↑a h = a := subtype.ext rfl @[simp] theorem coe_mk (a h) : (@mk α p a h : α) = a := rfl @[simp, nolint simp_nf] -- built-in reduction doesn't always work theorem mk_eq_mk {a h a' h'} : @mk α p a h = @mk α p a' h' ↔ a = a' := ext_iff theorem coe_eq_iff {a : {a // p a}} {b : α} : ↑a = b ↔ ∃ h, a = ⟨b, h⟩ := ⟨λ h, h ▸ ⟨a.2, (coe_eta _ _).symm⟩, λ ⟨hb, ha⟩, ha.symm ▸ rfl⟩ lemma coe_injective : injective (coe : subtype p → α) := λ a b, subtype.ext lemma val_injective : injective (@val _ p) := coe_injective lemma coe_inj {a b : subtype p} : (a : α) = b ↔ a = b := coe_injective.eq_iff lemma val_inj {a b : subtype p} : a.val = b.val ↔ a = b := coe_inj @[simp] lemma _root_.exists_eq_subtype_mk_iff {a : subtype p} {b : α} : (∃ h : p b, a = subtype.mk b h) ↔ ↑a = b := coe_eq_iff.symm @[simp] lemma _root_.exists_subtype_mk_eq_iff {a : subtype p} {b : α} : (∃ h : p b, subtype.mk b h = a) ↔ b = a := by simp only [@eq_comm _ b, exists_eq_subtype_mk_iff, @eq_comm _ _ a] /-- Restrict a (dependent) function to a subtype -/ def restrict {α} {β : α → Type*} (p : α → Prop) (f : Π x, β x) (x : subtype p) : β x.1 := f x lemma restrict_apply {α} {β : α → Type*} (f : Π x, β x) (p : α → Prop) (x : subtype p) : restrict p f x = f x.1 := by refl lemma restrict_def {α β} (f : α → β) (p : α → Prop) : restrict p f = f ∘ coe := by refl lemma restrict_injective {α β} {f : α → β} (p : α → Prop) (h : injective f) : injective (restrict p f) := h.comp coe_injective lemma surjective_restrict {α} {β : α → Type*} [ne : Π a, nonempty (β a)] (p : α → Prop) : surjective (λ f : Π x, β x, restrict p f) := begin letI := classical.dec_pred p, refine λ f, ⟨λ x, if h : p x then f ⟨x, h⟩ else nonempty.some (ne x), funext $ _⟩, rintro ⟨x, hx⟩, exact dif_pos hx end /-- Defining a map into a subtype, this can be seen as an "coinduction principle" of `subtype`-/ @[simps] def coind {α β} (f : α → β) {p : β → Prop} (h : ∀ a, p (f a)) : α → subtype p := λ a, ⟨f a, h a⟩ theorem coind_injective {α β} {f : α → β} {p : β → Prop} (h : ∀ a, p (f a)) (hf : injective f) : injective (coind f h) := λ x y hxy, hf $ by apply congr_arg subtype.val hxy theorem coind_surjective {α β} {f : α → β} {p : β → Prop} (h : ∀ a, p (f a)) (hf : surjective f) : surjective (coind f h) := λ x, let ⟨a, ha⟩ := hf x in ⟨a, coe_injective ha⟩ theorem coind_bijective {α β} {f : α → β} {p : β → Prop} (h : ∀ a, p (f a)) (hf : bijective f) : bijective (coind f h) := ⟨coind_injective h hf.1, coind_surjective h hf.2⟩ /-- Restriction of a function to a function on subtypes. -/ @[simps] def map {p : α → Prop} {q : β → Prop} (f : α → β) (h : ∀ a, p a → q (f a)) : subtype p → subtype q := λ x, ⟨f x, h x x.prop⟩ theorem map_comp {p : α → Prop} {q : β → Prop} {r : γ → Prop} {x : subtype p} (f : α → β) (h : ∀ a, p a → q (f a)) (g : β → γ) (l : ∀ a, q a → r (g a)) : map g l (map f h x) = map (g ∘ f) (assume a ha, l (f a) $ h a ha) x := rfl theorem map_id {p : α → Prop} {h : ∀ a, p a → p (id a)} : map (@id α) h = id := funext $ assume ⟨v, h⟩, rfl lemma map_injective {p : α → Prop} {q : β → Prop} {f : α → β} (h : ∀ a, p a → q (f a)) (hf : injective f) : injective (map f h) := coind_injective _ $ hf.comp coe_injective lemma map_involutive {p : α → Prop} {f : α → α} (h : ∀ a, p a → p (f a)) (hf : involutive f) : involutive (map f h) := λ x, subtype.ext (hf x) instance [has_equiv α] (p : α → Prop) : has_equiv (subtype p) := ⟨λ s t, (s : α) ≈ (t : α)⟩ theorem equiv_iff [has_equiv α] {p : α → Prop} {s t : subtype p} : s ≈ t ↔ (s : α) ≈ (t : α) := iff.rfl variables [setoid α] protected theorem refl (s : subtype p) : s ≈ s := setoid.refl ↑s protected theorem symm {s t : subtype p} (h : s ≈ t) : t ≈ s := setoid.symm h protected theorem trans {s t u : subtype p} (h₁ : s ≈ t) (h₂ : t ≈ u) : s ≈ u := setoid.trans h₁ h₂ theorem equivalence (p : α → Prop) : equivalence (@has_equiv.equiv (subtype p) _) := mk_equivalence _ subtype.refl (@subtype.symm _ p _) (@subtype.trans _ p _) instance (p : α → Prop) : setoid (subtype p) := setoid.mk (≈) (equivalence p) end subtype namespace subtype /-! Some facts about sets, which require that `α` is a type. -/ variables {α β γ : Type*} {p : α → Prop} @[simp] lemma coe_prop {S : set α} (a : {a // a ∈ S}) : ↑a ∈ S := a.prop lemma val_prop {S : set α} (a : {a // a ∈ S}) : a.val ∈ S := a.property end subtype
41d78bb63f8823498e18902bba70e388909940bc
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch5/ex0410.lean
4c193c8aa8e3d87e20dc9270d3cfb07f9d8a6f74
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
236
lean
example (p q r : Prop) : p ∧ (q ∨ r) → (p ∧ q) ∨ (p ∧ r) := begin intro h, have hp : p := h.left, have hqr : q ∨ r := h.right, cases hqr with hq hr, exact or.inl ⟨hp, hq⟩, exact or.inr ⟨hp, hr⟩ end
0c9893590326cf70b38982df8648e1effffba814
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/tensor_product.lean
f769f1c12deeb9dc77c71ce1006c77f49cbf9ffe
[ "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
36,922
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johan Commelin -/ import linear_algebra.tensor_product_basis import ring_theory.adjoin.basic /-! # The tensor product of R-algebras Let `R` be a (semi)ring and `A` an `R`-algebra. In this file we: - Define the `A`-module structure on `A ⊗ M`, for an `R`-module `M`. - Define the `R`-algebra structure on `A ⊗ B`, for another `R`-algebra `B`. and provide the structure isomorphisms * `R ⊗[R] A ≃ₐ[R] A` * `A ⊗[R] R ≃ₐ[R] A` * `A ⊗[R] B ≃ₐ[R] B ⊗[R] A` * `((A ⊗[R] B) ⊗[R] C) ≃ₐ[R] (A ⊗[R] (B ⊗[R] C))` ## Main declaration - `linear_map.base_change A f` is the `A`-linear map `A ⊗ f`, for an `R`-linear map `f`. ## Implementation notes The heterobasic definitions below such as: * `tensor_product.algebra_tensor_module.curry` * `tensor_product.algebra_tensor_module.uncurry` * `tensor_product.algebra_tensor_module.lcurry` * `tensor_product.algebra_tensor_module.lift` * `tensor_product.algebra_tensor_module.lift.equiv` * `tensor_product.algebra_tensor_module.mk` * `tensor_product.algebra_tensor_module.assoc` are just more general versions of the definitions already in `linear_algebra/tensor_product`. We could thus consider replacing the less general definitions with these ones. If we do this, we probably should still implement the less general ones as abbreviations to the more general ones with fewer type arguments. -/ universes u v₁ v₂ v₃ v₄ open_locale tensor_product open tensor_product namespace tensor_product variables {R A M N P : Type*} /-! ### The `A`-module structure on `A ⊗[R] M` -/ open linear_map open algebra (lsmul) namespace algebra_tensor_module section semiring variables [comm_semiring R] [semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables [add_comm_monoid N] [module R N] variables [add_comm_monoid P] [module R P] [module A P] [is_scalar_tower R A P] lemma smul_eq_lsmul_rtensor (a : A) (x : M ⊗[R] N) : a • x = (lsmul R M a).rtensor N x := rfl /-- Heterobasic version of `tensor_product.curry`: Given a linear map `M ⊗[R] N →[A] P`, compose it with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` to form a bilinear map `M →[A] N →[R] P`. -/ @[simps] def curry (f : (M ⊗[R] N) →ₗ[A] P) : M →ₗ[A] (N →ₗ[R] P) := { map_smul' := λ c x, linear_map.ext $ λ y, f.map_smul c (x ⊗ₜ y), .. curry (f.restrict_scalars R) } lemma restrict_scalars_curry (f : (M ⊗[R] N) →ₗ[A] P) : restrict_scalars R (curry f) = curry (f.restrict_scalars R) := rfl /-- Just as `tensor_product.ext` is marked `ext` instead of `tensor_product.ext'`, this is a better `ext` lemma than `tensor_product.algebra_tensor_module.ext` below. See note [partially-applied ext lemmas]. -/ @[ext] lemma curry_injective : function.injective (curry : (M ⊗ N →ₗ[A] P) → (M →ₗ[A] N →ₗ[R] P)) := λ x y h, linear_map.restrict_scalars_injective R $ curry_injective $ (congr_arg (linear_map.restrict_scalars R) h : _) theorem ext {g h : (M ⊗[R] N) →ₗ[A] P} (H : ∀ x y, g (x ⊗ₜ y) = h (x ⊗ₜ y)) : g = h := curry_injective $ linear_map.ext₂ H end semiring section comm_semiring variables [comm_semiring R] [comm_semiring A] [algebra R A] variables [add_comm_monoid M] [module R M] [module A M] [is_scalar_tower R A M] variables [add_comm_monoid N] [module R N] variables [add_comm_monoid P] [module R P] [module A P] [is_scalar_tower R A P] /-- Heterobasic version of `tensor_product.lift`: Constructing a linear map `M ⊗[R] N →[A] P` given a bilinear map `M →[A] N →[R] P` with the property that its composition with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` is the given bilinear map `M →[A] N →[R] P`. -/ @[simps] def lift (f : M →ₗ[A] (N →ₗ[R] P)) : (M ⊗[R] N) →ₗ[A] P := { map_smul' := λ c, show ∀ x : M ⊗[R] N, (lift (f.restrict_scalars R)).comp (lsmul R _ c) x = (lsmul R _ c).comp (lift (f.restrict_scalars R)) x, from ext_iff.1 $ tensor_product.ext' $ λ x y, by simp only [comp_apply, algebra.lsmul_coe, smul_tmul', lift.tmul, coe_restrict_scalars_eq_coe, f.map_smul, smul_apply], .. lift (f.restrict_scalars R) } @[simp] lemma lift_tmul (f : M →ₗ[A] (N →ₗ[R] P)) (x : M) (y : N) : lift f (x ⊗ₜ y) = f x y := lift.tmul' x y variables (R A M N P) /-- Heterobasic version of `tensor_product.uncurry`: Linearly constructing a linear map `M ⊗[R] N →[A] P` given a bilinear map `M →[A] N →[R] P` with the property that its composition with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` is the given bilinear map `M →[A] N →[R] P`. -/ @[simps] def uncurry : (M →ₗ[A] (N →ₗ[R] P)) →ₗ[A] ((M ⊗[R] N) →ₗ[A] P) := { to_fun := lift, map_add' := λ f g, ext $ λ x y, by simp only [lift_tmul, add_apply], map_smul' := λ c f, ext $ λ x y, by simp only [lift_tmul, smul_apply, ring_hom.id_apply] } /-- Heterobasic version of `tensor_product.lcurry`: Given a linear map `M ⊗[R] N →[A] P`, compose it with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` to form a bilinear map `M →[A] N →[R] P`. -/ @[simps] def lcurry : ((M ⊗[R] N) →ₗ[A] P) →ₗ[A] (M →ₗ[A] (N →ₗ[R] P)) := { to_fun := curry, map_add' := λ f g, rfl, map_smul' := λ c f, rfl } /-- Heterobasic version of `tensor_product.lift.equiv`: A linear equivalence constructing a linear map `M ⊗[R] N →[A] P` given a bilinear map `M →[A] N →[R] P` with the property that its composition with the canonical bilinear map `M →[A] N →[R] M ⊗[R] N` is the given bilinear map `M →[A] N →[R] P`. -/ def lift.equiv : (M →ₗ[A] (N →ₗ[R] P)) ≃ₗ[A] ((M ⊗[R] N) →ₗ[A] P) := linear_equiv.of_linear (uncurry R A M N P) (lcurry R A M N P) (linear_map.ext $ λ f, ext $ λ x y, lift_tmul _ x y) (linear_map.ext $ λ f, linear_map.ext $ λ x, linear_map.ext $ λ y, lift_tmul f x y) variables (R A M N P) /-- Heterobasic version of `tensor_product.mk`: The canonical bilinear map `M →[A] N →[R] M ⊗[R] N`. -/ @[simps] def mk : M →ₗ[A] N →ₗ[R] M ⊗[R] N := { map_smul' := λ c x, rfl, .. mk R M N } local attribute [ext] tensor_product.ext /-- Heterobasic version of `tensor_product.assoc`: Linear equivalence between `(M ⊗[A] N) ⊗[R] P` and `M ⊗[A] (N ⊗[R] P)`. -/ def assoc : ((M ⊗[A] P) ⊗[R] N) ≃ₗ[A] (M ⊗[A] (P ⊗[R] N)) := linear_equiv.of_linear (lift $ tensor_product.uncurry A _ _ _ $ comp (lcurry R A _ _ _) $ tensor_product.mk A M (P ⊗[R] N)) (tensor_product.uncurry A _ _ _ $ comp (uncurry R A _ _ _) $ by { apply tensor_product.curry, exact (mk R A _ _) }) (by { ext, refl, }) (by { ext, simp only [curry_apply, tensor_product.curry_apply, mk_apply, tensor_product.mk_apply, uncurry_apply, tensor_product.uncurry_apply, id_apply, lift_tmul, compr₂_apply, restrict_scalars_apply, function.comp_app, to_fun_eq_coe, lcurry_apply, linear_map.comp_apply] }) end comm_semiring end algebra_tensor_module end tensor_product namespace linear_map open tensor_product /-! ### The base-change of a linear map of `R`-modules to a linear map of `A`-modules -/ section semiring variables {R A B M N : Type*} [comm_semiring R] variables [semiring A] [algebra R A] [semiring B] [algebra R B] variables [add_comm_monoid M] [module R M] [add_comm_monoid N] [module R N] variables (r : R) (f g : M →ₗ[R] N) variables (A) /-- `base_change A f` for `f : M →ₗ[R] N` is the `A`-linear map `A ⊗[R] M →ₗ[A] A ⊗[R] N`. -/ def base_change (f : M →ₗ[R] N) : A ⊗[R] M →ₗ[A] A ⊗[R] N := { to_fun := f.ltensor A, map_add' := (f.ltensor A).map_add, map_smul' := λ a x, show (f.ltensor A) (rtensor M (linear_map.mul R A a) x) = (rtensor N ((linear_map.mul R A) a)) ((ltensor A f) x), by { rw [← comp_apply, ← comp_apply], simp only [ltensor_comp_rtensor, rtensor_comp_ltensor] } } variables {A} @[simp] lemma base_change_tmul (a : A) (x : M) : f.base_change A (a ⊗ₜ x) = a ⊗ₜ (f x) := rfl lemma base_change_eq_ltensor : (f.base_change A : A ⊗ M → A ⊗ N) = f.ltensor A := rfl @[simp] lemma base_change_add : (f + g).base_change A = f.base_change A + g.base_change A := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_zero : base_change A (0 : M →ₗ[R] N) = 0 := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_smul : (r • f).base_change A = r • (f.base_change A) := by { ext, simp [base_change_tmul], } variables (R A M N) /-- `base_change` as a linear map. -/ @[simps] def base_change_hom : (M →ₗ[R] N) →ₗ[R] A ⊗[R] M →ₗ[A] A ⊗[R] N := { to_fun := base_change A, map_add' := base_change_add, map_smul' := base_change_smul } end semiring section ring variables {R A B M N : Type*} [comm_ring R] variables [ring A] [algebra R A] [ring B] [algebra R B] variables [add_comm_group M] [module R M] [add_comm_group N] [module R N] variables (f g : M →ₗ[R] N) @[simp] lemma base_change_sub : (f - g).base_change A = f.base_change A - g.base_change A := by { ext, simp [base_change_eq_ltensor], } @[simp] lemma base_change_neg : (-f).base_change A = -(f.base_change A) := by { ext, simp [base_change_eq_ltensor], } end ring end linear_map namespace algebra namespace tensor_product section semiring variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] /-! ### The `R`-algebra structure on `A ⊗[R] B` -/ /-- (Implementation detail) The multiplication map on `A ⊗[R] B`, for a fixed pure tensor in the first argument, as an `R`-linear map. -/ def mul_aux (a₁ : A) (b₁ : B) : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) := tensor_product.map (linear_map.mul_left R a₁) (linear_map.mul_left R b₁) @[simp] lemma mul_aux_apply (a₁ a₂ : A) (b₁ b₂ : B) : (mul_aux a₁ b₁) (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl /-- (Implementation detail) The multiplication map on `A ⊗[R] B`, as an `R`-bilinear map. -/ def mul : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) := tensor_product.lift $ linear_map.mk₂ R mul_aux (λ x₁ x₂ y, tensor_product.ext' $ λ x' y', by simp only [mul_aux_apply, linear_map.add_apply, add_mul, add_tmul]) (λ c x y, tensor_product.ext' $ λ x' y', by simp only [mul_aux_apply, linear_map.smul_apply, smul_tmul', smul_mul_assoc]) (λ x y₁ y₂, tensor_product.ext' $ λ x' y', by simp only [mul_aux_apply, linear_map.add_apply, add_mul, tmul_add]) (λ c x y, tensor_product.ext' $ λ x' y', by simp only [mul_aux_apply, linear_map.smul_apply, smul_tmul, smul_tmul', smul_mul_assoc]) @[simp] lemma mul_apply (a₁ a₂ : A) (b₁ b₂ : B) : mul (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl lemma mul_assoc' (mul : (A ⊗[R] B) →ₗ[R] (A ⊗[R] B) →ₗ[R] (A ⊗[R] B)) (h : ∀ (a₁ a₂ a₃ : A) (b₁ b₂ b₃ : B), mul (mul (a₁ ⊗ₜ[R] b₁) (a₂ ⊗ₜ[R] b₂)) (a₃ ⊗ₜ[R] b₃) = mul (a₁ ⊗ₜ[R] b₁) (mul (a₂ ⊗ₜ[R] b₂) (a₃ ⊗ₜ[R] b₃))) : ∀ (x y z : A ⊗[R] B), mul (mul x y) z = mul x (mul y z) := begin intros, apply tensor_product.induction_on x, { simp only [linear_map.map_zero, linear_map.zero_apply], }, apply tensor_product.induction_on y, { simp only [linear_map.map_zero, forall_const, linear_map.zero_apply], }, apply tensor_product.induction_on z, { simp only [linear_map.map_zero, forall_const], }, { intros, simp only [h], }, { intros, simp only [linear_map.map_add, *], }, { intros, simp only [linear_map.map_add, *, linear_map.add_apply], }, { intros, simp only [linear_map.map_add, *, linear_map.add_apply], }, end lemma mul_assoc (x y z : A ⊗[R] B) : mul (mul x y) z = mul x (mul y z) := mul_assoc' mul (by { intros, simp only [mul_apply, mul_assoc], }) x y z lemma one_mul (x : A ⊗[R] B) : mul (1 ⊗ₜ 1) x = x := begin apply tensor_product.induction_on x; simp {contextual := tt}, end lemma mul_one (x : A ⊗[R] B) : mul x (1 ⊗ₜ 1) = x := begin apply tensor_product.induction_on x; simp {contextual := tt}, end instance : has_one (A ⊗[R] B) := { one := 1 ⊗ₜ 1 } instance : add_monoid_with_one (A ⊗[R] B) := add_monoid_with_one.unary instance : semiring (A ⊗[R] B) := { zero := 0, add := (+), one := 1, mul := λ a b, mul a b, one_mul := one_mul, mul_one := mul_one, mul_assoc := mul_assoc, zero_mul := by simp, mul_zero := by simp, left_distrib := by simp, right_distrib := by simp, .. (by apply_instance : add_monoid_with_one (A ⊗[R] B)), .. (by apply_instance : add_comm_monoid (A ⊗[R] B)) }. lemma one_def : (1 : A ⊗[R] B) = (1 : A) ⊗ₜ (1 : B) := rfl @[simp] lemma tmul_mul_tmul (a₁ a₂ : A) (b₁ b₂ : B) : (a₁ ⊗ₜ[R] b₁) * (a₂ ⊗ₜ[R] b₂) = (a₁ * a₂) ⊗ₜ[R] (b₁ * b₂) := rfl @[simp] lemma tmul_pow (a : A) (b : B) (k : ℕ) : (a ⊗ₜ[R] b)^k = (a^k) ⊗ₜ[R] (b^k) := begin induction k with k ih, { simp [one_def], }, { simp [pow_succ, ih], } end /-- The ring morphism `A →+* A ⊗[R] B` sending `a` to `a ⊗ₜ 1`. -/ @[simps] def include_left_ring_hom : A →+* A ⊗[R] B := { to_fun := λ a, a ⊗ₜ 1, map_zero' := by simp, map_add' := by simp [add_tmul], map_one' := rfl, map_mul' := by simp } variables {S : Type*} [comm_semiring S] [algebra R S] [algebra S A] [is_scalar_tower R S A] instance left_algebra : algebra S (A ⊗[R] B) := { commutes' := λ r x, begin apply tensor_product.induction_on x, { simp, }, { intros a b, dsimp, rw [algebra.commutes, _root_.mul_one, _root_.one_mul], }, { intros y y' h h', dsimp at h h' ⊢, simp only [mul_add, add_mul, h, h'], }, end, smul_def' := λ r x, begin apply tensor_product.induction_on x, { simp [smul_zero], }, { intros a b, dsimp, rw [tensor_product.smul_tmul', algebra.smul_def r a, _root_.one_mul] }, { intros, dsimp, simp [smul_add, mul_add, *], }, end, .. tensor_product.include_left_ring_hom.comp (algebra_map S A), .. (by apply_instance : module S (A ⊗[R] B)) }. -- This is for the `undergrad.yaml` list. /-- The tensor product of two `R`-algebras is an `R`-algebra. -/ instance : algebra R (A ⊗[R] B) := infer_instance @[simp] lemma algebra_map_apply (r : S) : (algebra_map S (A ⊗[R] B)) r = ((algebra_map S A) r) ⊗ₜ 1 := rfl instance : is_scalar_tower R S (A ⊗[R] B) := ⟨λ a b c, by simp⟩ variables {C : Type v₃} [semiring C] [algebra R C] @[ext] theorem ext {g h : (A ⊗[R] B) →ₐ[R] C} (H : ∀ a b, g (a ⊗ₜ b) = h (a ⊗ₜ b)) : g = h := begin apply @alg_hom.to_linear_map_injective R (A ⊗[R] B) C _ _ _ _ _ _ _ _, ext, simp [H], end /-- The `R`-algebra morphism `A →ₐ[R] A ⊗[R] B` sending `a` to `a ⊗ₜ 1`. -/ def include_left : A →ₐ[R] A ⊗[R] B := { commutes' := by simp, ..include_left_ring_hom } @[simp] lemma include_left_apply (a : A) : (include_left : A →ₐ[R] A ⊗[R] B) a = a ⊗ₜ 1 := rfl /-- The algebra morphism `B →ₐ[R] A ⊗[R] B` sending `b` to `1 ⊗ₜ b`. -/ def include_right : B →ₐ[R] A ⊗[R] B := { to_fun := λ b, 1 ⊗ₜ b, map_zero' := by simp, map_add' := by simp [tmul_add], map_one' := rfl, map_mul' := by simp, commutes' := λ r, begin simp only [algebra_map_apply], transitivity r • ((1 : A) ⊗ₜ[R] (1 : B)), { rw [←tmul_smul, algebra.smul_def], simp, }, { simp [algebra.smul_def], }, end, } @[simp] lemma include_right_apply (b : B) : (include_right : B →ₐ[R] A ⊗[R] B) b = 1 ⊗ₜ b := rfl lemma include_left_comp_algebra_map {R S T : Type*} [comm_ring R] [comm_ring S] [comm_ring T] [algebra R S] [algebra R T] : (include_left.to_ring_hom.comp (algebra_map R S) : R →+* S ⊗[R] T) = include_right.to_ring_hom.comp (algebra_map R T) := by { ext, simp } end semiring section ring variables {R : Type u} [comm_ring R] variables {A : Type v₁} [ring A] [algebra R A] variables {B : Type v₂} [ring B] [algebra R B] instance : ring (A ⊗[R] B) := { .. (by apply_instance : add_comm_group (A ⊗[R] B)), .. (by apply_instance : semiring (A ⊗[R] B)) }. end ring section comm_ring variables {R : Type u} [comm_ring R] variables {A : Type v₁} [comm_ring A] [algebra R A] variables {B : Type v₂} [comm_ring B] [algebra R B] instance : comm_ring (A ⊗[R] B) := { mul_comm := λ x y, begin apply tensor_product.induction_on x, { simp, }, { intros a₁ b₁, apply tensor_product.induction_on y, { simp, }, { intros a₂ b₂, simp [mul_comm], }, { intros a₂ b₂ ha hb, simp [mul_add, add_mul, ha, hb], }, }, { intros x₁ x₂ h₁ h₂, simp [mul_add, add_mul, h₁, h₂], }, end .. (by apply_instance : ring (A ⊗[R] B)) }. end comm_ring /-- Verify that typeclass search finds the ring structure on `A ⊗[ℤ] B` when `A` and `B` are merely rings, by treating both as `ℤ`-algebras. -/ example {A : Type v₁} [ring A] {B : Type v₂} [ring B] : ring (A ⊗[ℤ] B) := by apply_instance /-- Verify that typeclass search finds the comm_ring structure on `A ⊗[ℤ] B` when `A` and `B` are merely comm_rings, by treating both as `ℤ`-algebras. -/ example {A : Type v₁} [comm_ring A] {B : Type v₂} [comm_ring B] : comm_ring (A ⊗[ℤ] B) := by apply_instance /-! We now build the structure maps for the symmetric monoidal category of `R`-algebras. -/ section monoidal section variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] variables {C : Type v₃} [semiring C] [algebra R C] variables {D : Type v₄} [semiring D] [algebra R D] /-- Build an algebra morphism from a linear map out of a tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_hom_of_linear_map_tensor_product (f : A ⊗[R] B →ₗ[R] C) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (w₂ : ∀ r, f ((algebra_map R A) r ⊗ₜ[R] 1) = (algebra_map R C) r): A ⊗[R] B →ₐ[R] C := { map_one' := by rw [←(algebra_map R C).map_one, ←w₂, (algebra_map R A).map_one]; refl, map_zero' := by rw [linear_map.to_fun_eq_coe, map_zero], map_mul' := λ x y, by { rw linear_map.to_fun_eq_coe, apply tensor_product.induction_on x, { rw [zero_mul, map_zero, zero_mul] }, { intros a₁ b₁, apply tensor_product.induction_on y, { rw [mul_zero, map_zero, mul_zero] }, { intros a₂ b₂, rw [tmul_mul_tmul, w₁] }, { intros x₁ x₂ h₁ h₂, rw [mul_add, map_add, map_add, mul_add, h₁, h₂] } }, { intros x₁ x₂ h₁ h₂, rw [add_mul, map_add, map_add, add_mul, h₁, h₂] } }, commutes' := λ r, by rw [linear_map.to_fun_eq_coe, algebra_map_apply, w₂], .. f } @[simp] lemma alg_hom_of_linear_map_tensor_product_apply (f w₁ w₂ x) : (alg_hom_of_linear_map_tensor_product f w₁ w₂ : A ⊗[R] B →ₐ[R] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_equiv_of_linear_equiv_tensor_product (f : A ⊗[R] B ≃ₗ[R] C) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂)) = f (a₁ ⊗ₜ b₁) * f (a₂ ⊗ₜ b₂)) (w₂ : ∀ r, f ((algebra_map R A) r ⊗ₜ[R] 1) = (algebra_map R C) r): A ⊗[R] B ≃ₐ[R] C := { .. alg_hom_of_linear_map_tensor_product (f : A ⊗[R] B →ₗ[R] C) w₁ w₂, .. f } @[simp] lemma alg_equiv_of_linear_equiv_tensor_product_apply (f w₁ w₂ x) : (alg_equiv_of_linear_equiv_tensor_product f w₁ w₂ : A ⊗[R] B ≃ₐ[R] C) x = f x := rfl /-- Build an algebra equivalence from a linear equivalence out of a triple tensor product, and evidence of multiplicativity on pure tensors. -/ def alg_equiv_of_linear_equiv_triple_tensor_product (f : ((A ⊗[R] B) ⊗[R] C) ≃ₗ[R] D) (w₁ : ∀ (a₁ a₂ : A) (b₁ b₂ : B) (c₁ c₂ : C), f ((a₁ * a₂) ⊗ₜ (b₁ * b₂) ⊗ₜ (c₁ * c₂)) = f (a₁ ⊗ₜ b₁ ⊗ₜ c₁) * f (a₂ ⊗ₜ b₂ ⊗ₜ c₂)) (w₂ : ∀ r, f (((algebra_map R A) r ⊗ₜ[R] (1 : B)) ⊗ₜ[R] (1 : C)) = (algebra_map R D) r) : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] D := { to_fun := f, map_mul' := λ x y, begin apply tensor_product.induction_on x, { simp only [map_zero, zero_mul] }, { intros ab₁ c₁, apply tensor_product.induction_on y, { simp only [map_zero, mul_zero] }, { intros ab₂ c₂, apply tensor_product.induction_on ab₁, { simp only [zero_tmul, map_zero, zero_mul] }, { intros a₁ b₁, apply tensor_product.induction_on ab₂, { simp only [zero_tmul, map_zero, mul_zero] }, { intros, simp only [tmul_mul_tmul, w₁] }, { intros x₁ x₂ h₁ h₂, simp only [tmul_mul_tmul] at h₁ h₂, simp only [tmul_mul_tmul, mul_add, add_tmul, map_add, h₁, h₂] } }, { intros x₁ x₂ h₁ h₂, simp only [tmul_mul_tmul] at h₁ h₂, simp only [tmul_mul_tmul, add_mul, add_tmul, map_add, h₁, h₂] } }, { intros x₁ x₂ h₁ h₂, simp only [tmul_mul_tmul, map_add, mul_add, add_mul, h₁, h₂], }, }, { intros x₁ x₂ h₁ h₂, simp only [tmul_mul_tmul, map_add, mul_add, add_mul, h₁, h₂], } end, commutes' := λ r, by simp [w₂], .. f } @[simp] lemma alg_equiv_of_linear_equiv_triple_tensor_product_apply (f w₁ w₂ x) : (alg_equiv_of_linear_equiv_triple_tensor_product f w₁ w₂ : (A ⊗[R] B) ⊗[R] C ≃ₐ[R] D) x = f x := rfl end variables {R : Type u} [comm_semiring R] variables {A : Type v₁} [semiring A] [algebra R A] variables {B : Type v₂} [semiring B] [algebra R B] variables {C : Type v₃} [semiring C] [algebra R C] variables {D : Type v₄} [semiring D] [algebra R D] section variables (R A) /-- The base ring is a left identity for the tensor product of algebra, up to algebra isomorphism. -/ protected def lid : R ⊗[R] A ≃ₐ[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.lid R A) (by simp [mul_smul]) (by simp [algebra.smul_def]) @[simp] theorem lid_tmul (r : R) (a : A) : (tensor_product.lid R A : (R ⊗ A → A)) (r ⊗ₜ a) = r • a := by simp [tensor_product.lid] /-- The base ring is a right identity for the tensor product of algebra, up to algebra isomorphism. -/ protected def rid : A ⊗[R] R ≃ₐ[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.rid R A) (by simp [mul_smul]) (by simp [algebra.smul_def]) @[simp] theorem rid_tmul (r : R) (a : A) : (tensor_product.rid R A : (A ⊗ R → A)) (a ⊗ₜ r) = r • a := by simp [tensor_product.rid] section variables (R A B) /-- The tensor product of R-algebras is commutative, up to algebra isomorphism. -/ protected def comm : A ⊗[R] B ≃ₐ[R] B ⊗[R] A := alg_equiv_of_linear_equiv_tensor_product (tensor_product.comm R A B) (by simp) (λ r, begin transitivity r • ((1 : B) ⊗ₜ[R] (1 : A)), { rw [←tmul_smul, algebra.smul_def], simp, }, { simp [algebra.smul_def], }, end) @[simp] theorem comm_tmul (a : A) (b : B) : (tensor_product.comm R A B : (A ⊗[R] B → B ⊗[R] A)) (a ⊗ₜ b) = (b ⊗ₜ a) := by simp [tensor_product.comm] lemma adjoin_tmul_eq_top : adjoin R {t : A ⊗[R] B | ∃ a b, a ⊗ₜ[R] b = t} = ⊤ := top_le_iff.mp $ (top_le_iff.mpr $ span_tmul_eq_top R A B).trans (span_le_adjoin R _) end section variables {R A B C} lemma assoc_aux_1 (a₁ a₂ : A) (b₁ b₂ : B) (c₁ c₂ : C) : (tensor_product.assoc R A B C) (((a₁ * a₂) ⊗ₜ[R] (b₁ * b₂)) ⊗ₜ[R] (c₁ * c₂)) = (tensor_product.assoc R A B C) ((a₁ ⊗ₜ[R] b₁) ⊗ₜ[R] c₁) * (tensor_product.assoc R A B C) ((a₂ ⊗ₜ[R] b₂) ⊗ₜ[R] c₂) := rfl lemma assoc_aux_2 (r : R) : (tensor_product.assoc R A B C) (((algebra_map R A) r ⊗ₜ[R] 1) ⊗ₜ[R] 1) = (algebra_map R (A ⊗ (B ⊗ C))) r := rfl variables (R A B C) /-- The associator for tensor product of R-algebras, as an algebra isomorphism. -/ protected def assoc : ((A ⊗[R] B) ⊗[R] C) ≃ₐ[R] (A ⊗[R] (B ⊗[R] C)) := alg_equiv_of_linear_equiv_triple_tensor_product (tensor_product.assoc.{u v₁ v₂ v₃} R A B C : (A ⊗ B ⊗ C) ≃ₗ[R] (A ⊗ (B ⊗ C))) (@algebra.tensor_product.assoc_aux_1.{u v₁ v₂ v₃} R _ A _ _ B _ _ C _ _) (@algebra.tensor_product.assoc_aux_2.{u v₁ v₂ v₃} R _ A _ _ B _ _ C _ _) variables {R A B C} @[simp] theorem assoc_tmul (a : A) (b : B) (c : C) : ((tensor_product.assoc R A B C) : (A ⊗[R] B) ⊗[R] C → A ⊗[R] (B ⊗[R] C)) ((a ⊗ₜ b) ⊗ₜ c) = a ⊗ₜ (b ⊗ₜ c) := rfl end variables {R A B C D} /-- The tensor product of a pair of algebra morphisms. -/ def map (f : A →ₐ[R] B) (g : C →ₐ[R] D) : A ⊗[R] C →ₐ[R] B ⊗[R] D := alg_hom_of_linear_map_tensor_product (tensor_product.map f.to_linear_map g.to_linear_map) (by simp) (by simp [alg_hom.commutes]) @[simp] theorem map_tmul (f : A →ₐ[R] B) (g : C →ₐ[R] D) (a : A) (c : C) : map f g (a ⊗ₜ c) = f a ⊗ₜ g c := rfl @[simp] lemma map_comp_include_left (f : A →ₐ[R] B) (g : C →ₐ[R] D) : (map f g).comp include_left = include_left.comp f := alg_hom.ext $ by simp @[simp] lemma map_comp_include_right (f : A →ₐ[R] B) (g : C →ₐ[R] D) : (map f g).comp include_right = include_right.comp g := alg_hom.ext $ by simp lemma map_range (f : A →ₐ[R] B) (g : C →ₐ[R] D) : (map f g).range = (include_left.comp f).range ⊔ (include_right.comp g).range := begin apply le_antisymm, { rw [←map_top, ←adjoin_tmul_eq_top, ←adjoin_image, adjoin_le_iff], rintros _ ⟨_, ⟨a, b, rfl⟩, rfl⟩, rw [map_tmul, ←_root_.mul_one (f a), ←_root_.one_mul (g b), ←tmul_mul_tmul], exact mul_mem_sup (alg_hom.mem_range_self _ a) (alg_hom.mem_range_self _ b) }, { rw [←map_comp_include_left f g, ←map_comp_include_right f g], exact sup_le (alg_hom.range_comp_le_range _ _) (alg_hom.range_comp_le_range _ _) }, end /-- Construct an isomorphism between tensor products of R-algebras from isomorphisms between the tensor factors. -/ def congr (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) : A ⊗[R] C ≃ₐ[R] B ⊗[R] D := alg_equiv.of_alg_hom (map f g) (map f.symm g.symm) (ext $ λ b d, by simp) (ext $ λ a c, by simp) @[simp] lemma congr_apply (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) (x) : congr f g x = (map (f : A →ₐ[R] B) (g : C →ₐ[R] D)) x := rfl @[simp] lemma congr_symm_apply (f : A ≃ₐ[R] B) (g : C ≃ₐ[R] D) (x) : (congr f g).symm x = (map (f.symm : B →ₐ[R] A) (g.symm : D →ₐ[R] C)) x := rfl end end monoidal section variables {R A B S : Type*} [comm_semiring R] [semiring A] [semiring B] [comm_semiring S] variables [algebra R A] [algebra R B] [algebra R S] variables (f : A →ₐ[R] S) (g : B →ₐ[R] S) variables (R) /-- `linear_map.mul'` is an alg_hom on commutative rings. -/ def lmul' : S ⊗[R] S →ₐ[R] S := alg_hom_of_linear_map_tensor_product (linear_map.mul' R S) (λ a₁ a₂ b₁ b₂, by simp only [linear_map.mul'_apply, mul_mul_mul_comm]) (λ r, by simp only [linear_map.mul'_apply, _root_.mul_one]) variables {R} lemma lmul'_to_linear_map : (lmul' R : _ →ₐ[R] S).to_linear_map = linear_map.mul' R S := rfl @[simp] lemma lmul'_apply_tmul (a b : S) : lmul' R (a ⊗ₜ[R] b) = a * b := linear_map.mul'_apply @[simp] lemma lmul'_comp_include_left : (lmul' R : _ →ₐ[R] S).comp include_left = alg_hom.id R S := alg_hom.ext $ λ _, (lmul'_apply_tmul _ _).trans (_root_.mul_one _) @[simp] lemma lmul'_comp_include_right : (lmul' R : _ →ₐ[R] S).comp include_right = alg_hom.id R S := alg_hom.ext $ λ _, (lmul'_apply_tmul _ _).trans (_root_.one_mul _) /-- If `S` is commutative, for a pair of morphisms `f : A →ₐ[R] S`, `g : B →ₐ[R] S`, We obtain a map `A ⊗[R] B →ₐ[R] S` that commutes with `f`, `g` via `a ⊗ b ↦ f(a) * g(b)`. -/ def product_map : A ⊗[R] B →ₐ[R] S := (lmul' R).comp (tensor_product.map f g) @[simp] lemma product_map_apply_tmul (a : A) (b : B) : product_map f g (a ⊗ₜ b) = f a * g b := by { unfold product_map lmul', simp } lemma product_map_left_apply (a : A) : product_map f g ((include_left : A →ₐ[R] A ⊗ B) a) = f a := by simp @[simp] lemma product_map_left : (product_map f g).comp include_left = f := alg_hom.ext $ by simp lemma product_map_right_apply (b : B) : product_map f g (include_right b) = g b := by simp @[simp] lemma product_map_right : (product_map f g).comp include_right = g := alg_hom.ext $ by simp lemma product_map_range : (product_map f g).range = f.range ⊔ g.range := by rw [product_map, alg_hom.range_comp, map_range, map_sup, ←alg_hom.range_comp, ←alg_hom.range_comp, ←alg_hom.comp_assoc, ←alg_hom.comp_assoc, lmul'_comp_include_left, lmul'_comp_include_right, alg_hom.id_comp, alg_hom.id_comp] end section variables {R A A' B S : Type*} variables [comm_semiring R] [comm_semiring A] [semiring A'] [semiring B] [comm_semiring S] variables [algebra R A] [algebra R A'] [algebra A A'] [is_scalar_tower R A A'] [algebra R B] variables [algebra R S] [algebra A S] [is_scalar_tower R A S] /-- If `A`, `B` are `R`-algebras, `A'` is an `A`-algebra, then the product map of `f : A' →ₐ[A] S` and `g : B →ₐ[R] S` is an `A`-algebra homomorphism. -/ @[simps] def product_left_alg_hom (f : A' →ₐ[A] S) (g : B →ₐ[R] S) : A' ⊗[R] B →ₐ[A] S := { commutes' := λ r, by { dsimp, simp }, ..(product_map (f.restrict_scalars R) g).to_ring_hom } end section basis variables {k : Type*} [comm_ring k] (R : Type*) [ring R] [algebra k R] {M : Type*} [add_comm_monoid M] [module k M] {ι : Type*} (b : basis ι k M) /-- Given a `k`-algebra `R` and a `k`-basis of `M,` this is a `k`-linear isomorphism `R ⊗[k] M ≃ (ι →₀ R)` (which is in fact `R`-linear). -/ noncomputable def basis_aux : R ⊗[k] M ≃ₗ[k] (ι →₀ R) := (_root_.tensor_product.congr (finsupp.linear_equiv.finsupp_unique k R punit).symm b.repr) ≪≫ₗ (finsupp_tensor_finsupp k R k punit ι).trans (finsupp.lcongr (equiv.unique_prod ι punit) (_root_.tensor_product.rid k R)) variables {R} lemma basis_aux_tmul (r : R) (m : M) : basis_aux R b (r ⊗ₜ m) = r • (finsupp.map_range (algebra_map k R) (map_zero _) (b.repr m)) := begin ext, simp [basis_aux, ←algebra.commutes, algebra.smul_def], end lemma basis_aux_map_smul (r : R) (x : R ⊗[k] M) : basis_aux R b (r • x) = r • basis_aux R b x := tensor_product.induction_on x (by simp) (λ x y, by simp only [tensor_product.smul_tmul', basis_aux_tmul, smul_assoc]) (λ x y hx hy, by simp [hx, hy]) variables (R) /-- Given a `k`-algebra `R`, this is the `R`-basis of `R ⊗[k] M` induced by a `k`-basis of `M`. -/ noncomputable def basis : basis ι R (R ⊗[k] M) := { repr := { map_smul' := basis_aux_map_smul b, .. basis_aux R b } } variables {R} @[simp] lemma basis_repr_tmul (r : R) (m : M) : (basis R b).repr (r ⊗ₜ m) = r • (finsupp.map_range (algebra_map k R) (map_zero _) (b.repr m)) := basis_aux_tmul _ _ _ @[simp] lemma basis_repr_symm_apply (r : R) (i : ι) : (basis R b).repr.symm (finsupp.single i r) = r ⊗ₜ b.repr.symm (finsupp.single i 1) := by simp [basis, equiv.unique_prod_symm_apply, basis_aux] end basis end tensor_product end algebra namespace module variables {R M N : Type*} [comm_semiring R] variables [add_comm_monoid M] [add_comm_monoid N] variables [module R M] [module R N] /-- The algebra homomorphism from `End M ⊗ End N` to `End (M ⊗ N)` sending `f ⊗ₜ g` to the `tensor_product.map f g`, the tensor product of the two maps. -/ def End_tensor_End_alg_hom : (End R M) ⊗[R] (End R N) →ₐ[R] End R (M ⊗[R] N) := begin refine algebra.tensor_product.alg_hom_of_linear_map_tensor_product (hom_tensor_hom_map R M N M N) _ _, { intros f₁ f₂ g₁ g₂, simp only [hom_tensor_hom_map_apply, tensor_product.map_mul] }, { intro r, simp only [hom_tensor_hom_map_apply], ext m n, simp [smul_tmul] } end lemma End_tensor_End_alg_hom_apply (f : End R M) (g : End R N) : End_tensor_End_alg_hom (f ⊗ₜ[R] g) = tensor_product.map f g := by simp only [End_tensor_End_alg_hom, algebra.tensor_product.alg_hom_of_linear_map_tensor_product_apply, hom_tensor_hom_map_apply] end module lemma subalgebra.finite_dimensional_sup {K L : Type*} [field K] [comm_ring L] [algebra K L] (E1 E2 : subalgebra K L) [finite_dimensional K E1] [finite_dimensional K E2] : finite_dimensional K ↥(E1 ⊔ E2) := begin rw [←E1.range_val, ←E2.range_val, ←algebra.tensor_product.product_map_range], exact (algebra.tensor_product.product_map E1.val E2.val).to_linear_map.finite_dimensional_range, end namespace tensor_product.algebra variables {R A B M : Type*} variables [comm_semiring R] [add_comm_monoid M] [module R M] variables [semiring A] [semiring B] [module A M] [module B M] variables [algebra R A] [algebra R B] variables [is_scalar_tower R A M] [is_scalar_tower R B M] /-- An auxiliary definition, used for constructing the `module (A ⊗[R] B) M` in `tensor_product.algebra.module` below. -/ def module_aux : A ⊗[R] B →ₗ[R] M →ₗ[R] M := tensor_product.lift { to_fun := λ a, a • (algebra.lsmul R M : B →ₐ[R] module.End R M).to_linear_map, map_add' := λ r t, by { ext, simp only [add_smul, linear_map.add_apply] }, map_smul' := λ n r, by { ext, simp only [ring_hom.id_apply, linear_map.smul_apply, smul_assoc] } } lemma module_aux_apply (a : A) (b : B) (m : M) : module_aux (a ⊗ₜ[R] b) m = a • b • m := by simp [module_aux] variables [smul_comm_class A B M] /-- If `M` is a representation of two different `R`-algebras `A` and `B` whose actions commute, then it is a representation the `R`-algebra `A ⊗[R] B`. An important example arises from a semiring `S`; allowing `S` to act on itself via left and right multiplication, the roles of `R`, `A`, `B`, `M` are played by `ℕ`, `S`, `Sᵐᵒᵖ`, `S`. This example is important because a submodule of `S` as a `module` over `S ⊗[ℕ] Sᵐᵒᵖ` is a two-sided ideal. NB: This is not an instance because in the case `B = A` and `M = A ⊗[R] A` we would have a diamond of `smul` actions. Furthermore, this would not be a mere definitional diamond but a true mathematical diamond in which `A ⊗[R] A` had two distinct scalar actions on itself: one from its multiplication, and one from this would-be instance. Arguably we could live with this but in any case the real fix is to address the ambiguity in notation, probably along the lines outlined here: https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.234773.20base.20change/near/240929258 -/ protected def module : module (A ⊗[R] B) M := { smul := λ x m, module_aux x m, zero_smul := λ m, by simp only [map_zero, linear_map.zero_apply], smul_zero := λ x, by simp only [map_zero], smul_add := λ x m₁ m₂, by simp only [map_add], add_smul := λ x y m, by simp only [map_add, linear_map.add_apply], one_smul := λ m, by simp only [module_aux_apply, algebra.tensor_product.one_def, one_smul], mul_smul := λ x y m, begin apply tensor_product.induction_on x; apply tensor_product.induction_on y, { simp only [mul_zero, map_zero, linear_map.zero_apply], }, { intros a b, simp only [zero_mul, map_zero, linear_map.zero_apply], }, { intros z w hz hw, simp only [zero_mul, map_zero, linear_map.zero_apply], }, { intros a b, simp only [mul_zero, map_zero, linear_map.zero_apply], }, { intros a₁ b₁ a₂ b₂, simp only [module_aux_apply, mul_smul, smul_comm a₁ b₂, algebra.tensor_product.tmul_mul_tmul, linear_map.mul_apply], }, { intros z w hz hw a b, simp only at hz hw, simp only [mul_add, hz, hw, map_add, linear_map.add_apply], }, { intros z w hz hw, simp only [mul_zero, map_zero, linear_map.zero_apply], }, { intros a b z w hz hw, simp only at hz hw, simp only [map_add, add_mul, linear_map.add_apply, hz, hw], }, { intros u v hu hv z w hz hw, simp only at hz hw, simp only [add_mul, hz, hw, map_add, linear_map.add_apply], }, end } local attribute [instance] tensor_product.algebra.module lemma smul_def (a : A) (b : B) (m : M) : (a ⊗ₜ[R] b) • m = a • b • m := module_aux_apply a b m end tensor_product.algebra
54da286d6d542f1386795760be4d8bf993a60726
ea5678cc400c34ff95b661fa26d15024e27ea8cd
/cycles.lean
b56a9866aa896b2b97eed03b99a481e7c295348f
[]
no_license
ChrisHughes24/leanstuff
dca0b5349c3ed893e8792ffbd98cbcadaff20411
9efa85f72efaccd1d540385952a6acc18fce8687
refs/heads/master
1,654,883,241,759
1,652,873,885,000
1,652,873,885,000
134,599,537
1
0
null
null
null
null
UTF-8
Lean
false
false
8,976
lean
import group_theory.perm group_theory.order_of_element variables {α : Type*} [decidable_eq α] open finset function namespace equiv namespace perm def disjoint (f g : perm α) := ∀ x : α, f x = x ∨ g x = x def cycle_of_aux (f : perm α) : Π (n : ℕ) (y : α), list α | 0 y := [] | (n+1) y := y :: (cycle_of_aux n (f y)) lemma length_cycle_of_aux (f : perm α) (n : ℕ) : ∀ y : α, (cycle_of_aux f n y).length = n := by induction n; simp [cycle_of_aux, *] @[simp] lemma nth_cycle_of_aux [fintype α] (f : perm α) : ∀ {n m : ℕ} (y : α) (hn : nat.succ m < (cycle_of_aux f n y).length), list.nth_le (cycle_of_aux f n y) (nat.succ m) hn = f (list.nth_le (cycle_of_aux f n y) m (nat.lt_of_succ_lt hn)) | 0 m y hn := (nat.not_lt_zero _ hn).elim | 1 0 y hn := (lt_irrefl _ hn).elim | (n+2) 0 y hn := rfl | (n+1) (m+1) y hn := begin unfold cycle_of_aux list.nth_le, rw nth_cycle_of_aux, refl, end @[simp] lemma nth_cycle_of_aux_eq_pow [fintype α] (f : perm α) : ∀ {n m : ℕ} (x : α) (hm : m < (cycle_of_aux f n x).length), (cycle_of_aux f n x).nth_le m hm = (f ^ m) x | 0 m := λ _ hm, absurd hm dec_trivial | (n+1) 0 := λ _ _, rfl | (n+1) (m+1) := λ x hm, by unfold cycle_of_aux list.nth_le; rw [nth_cycle_of_aux_eq_pow, pow_succ', mul_apply] lemma pow_apply_eq_self_of_apply_eq_self_nat {f : perm α} {x : α} (n : ℕ) (hfx : f x = x) : (f ^ n) x = x := by induction n; [refl, rw [pow_succ, mul_apply, n_ih, hfx]] def support_equiv_gpowers [fintype α] {f : perm α} (hf : is_cycle f) : gpowers f ≃ {x | x ∈ f.support} := let ⟨x, hx⟩ := classical.indefinite_description _ hf in { to_fun := λ g, let i := classical.some g.2 in ⟨(f ^ i) x, mem_support.2 (mt (injective.eq_iff (f ^ -i).bijective.1).2 $ begin rw [← mul_apply, ← mul_apply, ← mul_apply, ← gpow_add, neg_add_self, gpow_zero]; rw [← gpow_one f] {occs := occurrences.pos [2]}, rw [← gpow_add, ← gpow_add, neg_add_eq_sub, sub_add_cancel, gpow_one], exact hx.1 end)⟩, inv_fun := λ x, begin end } lemma order_of_eq_card_support [fintype α] (f : perm α) : order_of f = f.support.card := begin rw [order_eq_card_gpowers, ← set.card_fintype_of_finset f.support (@mem_support _ _ _ f)], refine @fintype.card_congr _ _ (id _) (id _) { to_fun := λ ⟨g, h⟩, inv_fun := _, left_inv := _, right_inv := _ }, end def cycle_of_list [fintype α] (f : perm α) (x : α) : list α := cycle_of_aux f (nat.find (exists_pow_apply_eq_self f x)) x lemma cycle_of_list_eq_cons [fintype α] (f : perm α) (x : α) : cycle_of_list f x = x :: cycle_of_aux f (nat.find (exists_pow_apply_eq_self f x) - 1) (f x) := begin unfold cycle_of_list, cases h : nat.find (exists_pow_apply_eq_self f x), { exact absurd h (nat.pos_iff_ne_zero.1 $ (nat.find_spec (exists_pow_apply_eq_self f x)).1) }, { simp [cycle_of_aux] } end def cycle_length [fintype α] (f : perm α) (x : α) : ℕ := (cycle_of_list f x).length lemma cycle_length_pos [fintype α] (f : perm α) (x : α) : cycle_length f x > 0 := by rw [cycle_length, cycle_of_list, length_cycle_of_aux]; exact (nat.find_spec (exists_pow_apply_eq_self f x)).1 @[simp] lemma pow_cycle_length_apply [fintype α] (f : perm α) (x : α) : (f ^ cycle_length f x) x = x := by rw [cycle_length, cycle_of_list, length_cycle_of_aux]; exact (nat.find_spec (exists_pow_apply_eq_self f x)).2 @[simp] lemma length_cycle_of_list [fintype α] (f : perm α) (x : α) : (cycle_of_list f x).length = cycle_length f x := rfl @[simp] lemma nth_cycle_of_list_eq_pow [fintype α] (f : perm α) (x : α) {n : ℕ} (hn : n < cycle_length f x) : (cycle_of_list f x).nth_le n hn = (f ^ n) x := nth_cycle_of_aux_eq_pow _ _ hn open list def cycle_list_factors [fintype α] (f : perm α) : Π l : list α, list (list α) | [] := [] | (x :: l) := let m := cycle_of_list f x in have wf : ((x :: l : list α).diff (cycle_of_list f x)).length < (x :: l : list α).length := calc ((x :: l : list α).diff (cycle_of_list f x)).length = list.length (list.diff l (cycle_of_aux f (nat.find _ - 1) (f x))) : by rw [cycle_of_list_eq_cons, list.diff_cons, list.erase_cons_head] ... ≤ l.length : list.length_le_of_sublist (diff_sublist _ _) ... < (x :: l : list α).length : nat.lt_succ_self _, if f x = x then cycle_list_factors ((x :: l : list α).diff m) else m :: cycle_list_factors ((x :: l : list α).diff m) using_well_founded {dec_tac := tactic.assumption, rel_tac := λ _ _ , `[exact ⟨_, measure_wf list.length⟩]} meta instance [fintype α] [has_repr α] : has_repr (perm α) := ⟨λ f, repr (cycle_list_factors f (quot.unquot (@univ α _).1))⟩ instance zmod_has_repr (n : ℕ+) : has_repr (zmod n) := fin.has_repr n -- instance (n : ℕ+) : has_coe ℕ (zmod n) := by apply_instance #eval (let p := 11 in let q := 3 in ({ to_fun := λ x : zmod ⟨p * q, dec_trivial⟩, (↑((x.1 % p * q) % (p * q) + x.1 / p : ℕ) : zmod ⟨p * q, dec_trivial⟩), inv_fun := sorry, left_inv := sorry, right_inv := sorry } : perm (zmod ⟨p * q, dec_trivial⟩))) def list_to_perm : Π (l : list α), perm α | [] := 1 | [x] := 1 | (x::y::l) := swap x y * list_to_perm (y :: l) @[simp] lemma list_to_perm_eq_swap (x y : α) : list_to_perm [x,y] = swap x y := rfl lemma list_to_perm_pow {l : list α} {x : α} (h : (x :: l : list α).nodup) {n : ℕ} (hn : n < (x::l : list α).length) : (list_to_perm (x :: l) ^ n) x = (x :: l : list α).nth_le n hn := begin induction l with y l ih, { have hn : n = 0 := nat.le_zero_iff.1 (nat.le_of_lt_succ hn), subst hn, rw [list_to_perm, one_pow]; refl }, rw [list_to_perm], end def cycle_of [fintype α] (f : perm α) (x : α) : perm α := list_to_perm (cycle_of_list f x) @[simp] lemma cycle_of_apply_self [fintype α] (f : perm α) (x : α) : cycle_of f x x = f x := begin rw cycle_of, end lemma cycle_of_is_cycle [fintype α] (f : perm α) (x : α) (h : f x ≠ x) : is_cycle (cycle_of f x) := ⟨begin end, begin end⟩ #eval list_to_perm ([1,2,3,4,3] : list (fin 5)) def list_to_cycle_append : Π (l₁ l₂ : list α), list_to_cycle_aux (l₁ ++ l₂) = list_to_cycle_aux l₁ ∘ list_to_cycle_aux l₂ | [] l₂ := rfl | [a] l₂ := begin end lemma list_to_cycle_reverse : ∀ (l : list α) (x y w z : α), z ≠ x → z ≠ y → z ≠ w → list_to_cycle_aux (x :: l ++ [y]) (list_to_cycle_aux (x :: l ++ [w] : list α).reverse z) = z | [] x y w z := λ hzx hzy, by simp [list_to_cycle_aux]; split_ifs; cc | (a::l) x y w z := λ hzx hzy hzw, list.reverse_rec_on l (by simp [list_to_cycle_aux]; split_ifs; cc) (λ l b _, begin simp only [reverse_cons, reverse_append, nil_append, reverse_nil, cons_append], unfold list_to_cycle_aux, split_ifs, cc <|> simp [*, list_to_cycle_aux], end) -- def list_to_cycle' (l : list α) : l.nodup → perm α := -- list.reverse_rec_on l (λ _, 1) (λ l, list.cases_on l (λ _ _ _, 1) -- $ λ x l y _ h, -- have hx : (x::l : list α).nodup := (list.nodup_of_nodup_append_left h), -- have hy : (l ++ [y]).nodup := by rw list.cons_append at h; -- exact list.nodup_of_nodup_cons h, -- let f := list_to_cycle_aux (x :: l ++ [y]) in -- let g := list_to_cycle_aux (x :: l ++ [y]).reverse in -- { to_fun := λ n, if n = y then x else f n, -- inv_fun := λ n, if n = x then y else g n, -- left_inv := λ n, begin dsimp, split_ifs, cc, end, -- right_inv := sorry }) -- #eval ((list_to_cycle' ([1,2,3] : list (fin 6)) sorry) * list_to_cycle' [2,3,0,4] sorry) * swap 0 5 -- def list_to_cycle2 : Π l : list α, l.nodup → perm α -- | [] := λ h, 1 -- | (x::l) := λ h, -- { to_fun := λ x, list.nth_le (x::l) (((list.index_of x l : ℕ) : zmod l.length.succ) + 1).val -- (((list.index_of x l : ℕ) : zmod l.length.succ) + 1).2, -- inv_fun := λ x, list.nth_le (x::l) ((list.index_of x l : zmod l.length.succ) - 1).val -- ((list.index_of x l : zmod l.length.succ) - 1).2, -- left_inv := λ x, begin dsimp, rw index_of_nth_le end, -- right_inv := sorry } -- def list_to_cycle (l : list α) (hl : l.nodup) : perm α := -- { to_fun := λ x, if h : x ∈ l then begin -- have := list.index_of x l, -- have := list.ind -- end else x, -- inv_fun := λ x, if h : x ∈ l then begin end else x, -- left_inv := sorry, -- right_inv := sorry } def list_to_cycle (l : list α) : l.nodup → perm α := list.cases_on l (λ _, 1) (λ x l, list.reverse_rec_on l (λ _ , 1) (λ m y, list.reverse_rec_on m (λ _ _, swap x y) (λ n z f h ih, have hyx : y ≠ x := sorry, let f := h sorry in have hfax : ∀ a : α, f a = x ↔ a = z := sorry, { to_fun := λ a, if a = z then y else if a = y then x else f a, inv_fun := λ a, if a = x then y else if a = y then z else f⁻¹ a, left_inv := λ a, begin simp, split_ifs, end, right_inv := sorry }))) end equiv.perm
a0cd87f53952f79492e1cc9c51d5beddc4f55dfc
947b78d97130d56365ae2ec264df196ce769371a
/src/Lean/PrettyPrinter/Parenthesizer.lean
77a566a09a52fee4852affa084b9ab417449941b
[ "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
25,320
lean
/- Copyright (c) 2020 Sebastian Ullrich. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sebastian Ullrich -/ /-! The parenthesizer inserts parentheses into a `Syntax` object where syntactically necessary, usually as an intermediary step between the delaborator and the formatter. While the delaborator outputs structurally well-formed syntax trees that can be re-elaborated without post-processing, this tree structure is lost in the formatter and thus needs to be preserved by proper insertion of parentheses. # The abstract problem & solution The Lean 4 grammar is unstructured and extensible with arbitrary new parsers, so in general it is undecidable whether parentheses are necessary or even allowed at any point in the syntax tree. Parentheses for different categories, e.g. terms and levels, might not even have the same structure. In this module, we focus on the correct parenthesization of parsers defined via `Lean.Parser.prattParser`, which includes both aforementioned built-in categories. Custom parenthesizers can be added for new node kinds, but the data collected in the implementation below might not be appropriate for other parenthesization strategies. Usages of a parser defined via `prattParser` in general have the form `p prec`, where `prec` is the minimum precedence or binding power. Recall that a Pratt parser greedily runs a leading parser with precedence at least `prec` (otherwise it fails) followed by zero or more trailing parsers with precedence at least `prec`; the precedence of a parser is encoded in the call to `leadingNode/trailingNode`, respectively. Thus we should parenthesize a syntax node `stx` supposedly produced by `p prec` if 1. the leading/any trailing parser involved in `stx` has precedence < `prec` (because without parentheses, `p prec` would not produce all of `stx`), or 2. the trailing parser parsing the input to *the right of* `stx`, if any, has precedence >= `prec` (because without parentheses, `p prec` would have parsed it as well and made it a part of `stx`). We also check that the two parsers are from the same syntax category. Note that in case 2, it is also sufficient to parenthesize a *parent* node as long as the offending parser is still to the right of that node. For example, imagine the tree structure of `(f $ fun x => x) y` without parentheses. We need to insert *some* parentheses between `x` and `y` since the lambda body is parsed with precedence 0, while the identifier parser for `y` has precedence `maxPrec`. But we need to parenthesize the `$` node anyway since the precedence of its RHS (0) again is smaller than that of `y`. So it's better to only parenthesize the outer node than ending up with `(f $ (fun x => x)) y`. # Implementation We transform the syntax tree and collect the necessary precedence information for that in a single traversal. The traversal is right-to-left to cover case 2. More specifically, for every Pratt parser call, we store as monadic state the precedence of the left-most trailing parser and the minimum precedence of any parser (`contPrec`/`minPrec`) in this call, if any, and the precedence of the nested trailing Pratt parser call (`trailPrec`), if any. If `stP` is the state resulting from the traversal of a Pratt parser call `p prec`, and `st` is the state of the surrounding call, we parenthesize if `prec > stP.minPrec` (case 1) or if `stP.trailPrec <= st.contPrec` (case 2). The traversal can be customized for each `[*Parser]` parser declaration `c` (more specifically, each `SyntaxNodeKind` `c`) using the `[parenthesizer c]` attribute. Otherwise, a default parenthesizer will be synthesized from the used parser combinators by recursively replacing them with declarations tagged as `[combinatorParenthesizer]` for the respective combinator. If a called function does not have a registered combinator parenthesizer and is not reducible, the synthesizer fails. This happens mostly at the `Parser.mk` decl, which is irreducible, when some parser primitive has not been handled yet. The traversal over the `Syntax` object is complicated by the fact that a parser does not produce exactly one syntax node, but an arbitrary (but constant, for each parser) amount that it pushes on top of the parser stack. This amount can even be zero for parsers such as `checkWsBefore`. Thus we cannot simply pass and return a `Syntax` object to and from `visit`. Instead, we use a `Syntax.Traverser` that allows arbitrary movement and modification inside the syntax tree. Our traversal invariant is that a parser interpreter should stop at the syntax object to the *left* of all syntax objects its parser produced, except when it is already at the left-most child. This special case is not an issue in practice since if there is another parser to the left that produced zero nodes in this case, it should always do so, so there is no danger of the left-most child being processed multiple times. Ultimately, most parenthesizers are implemented via three primitives that do all the actual syntax traversal: `maybeParenthesize mkParen prec x` runs `x` and afterwards transforms it with `mkParen` if the above condition for `p prec` is fulfilled. `visitToken` advances to the preceding sibling and is used on atoms. `visitArgs x` executes `x` on the last child of the current node and then advances to the preceding sibling (of the original current node). -/ import Lean.CoreM import Lean.KeyedDeclsAttribute import Lean.Parser.Extension import Lean.ParserCompiler.Attribute import Lean.PrettyPrinter.Backtrack namespace Lean namespace PrettyPrinter namespace Parenthesizer structure Context := -- We need to store this `categoryParser` argument to deal with the implicit Pratt parser call in `trailingNode.parenthesizer`. (cat : Name := Name.anonymous) structure State := (stxTrav : Syntax.Traverser) --- precedence and category of the current left-most trailing parser, if any; see module doc for details (contPrec : Option Nat := none) (contCat := Name.anonymous) -- current minimum precedence in this Pratt parser call, if any; see module doc for details (minPrec : Option Nat := none) -- precedence of the trailing Pratt parser call if any; see module doc for details (trailPrec : Option Nat := none) -- true iff we have already visited a token on this parser level; used for detecting trailing parsers (visitedToken : Bool := false) end Parenthesizer abbrev ParenthesizerM := ReaderT Parenthesizer.Context $ StateRefT Parenthesizer.State $ CoreM abbrev Parenthesizer := ParenthesizerM Unit @[inline] def ParenthesizerM.orelse {α} (p₁ p₂ : ParenthesizerM α) : ParenthesizerM α := do s ← get; catchInternalId backtrackExceptionId p₁ (fun _ => do set s; p₂) instance Parenthesizer.orelse {α} : HasOrelse (ParenthesizerM α) := ⟨ParenthesizerM.orelse⟩ unsafe def mkParenthesizerAttribute : IO (KeyedDeclsAttribute Parenthesizer) := KeyedDeclsAttribute.init { builtinName := `builtinParenthesizer, name := `parenthesizer, descr := "Register a parenthesizer for a parser. [parenthesizer k] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `SyntaxNodeKind` `k`.", valueTypeName := `Lean.PrettyPrinter.Parenthesizer, evalKey := fun builtin args => do env ← getEnv; match attrParamSyntaxToIdentifier args with | some id => -- `isValidSyntaxNodeKind` is updated only in the next stage for new `[builtin*Parser]`s, but we try to -- synthesize a parenthesizer for it immediately, so we just check for a declaration in this case if (builtin && (env.find? id).isSome) || Parser.isValidSyntaxNodeKind env id then pure id else throwError ("invalid [parenthesizer] argument, unknown syntax kind '" ++ toString id ++ "'") | none => throwError "invalid [parenthesizer] argument, expected identifier" } `Lean.PrettyPrinter.parenthesizerAttribute @[init mkParenthesizerAttribute] constant parenthesizerAttribute : KeyedDeclsAttribute Parenthesizer := arbitrary _ abbrev CategoryParenthesizer := forall (prec : Nat), Parenthesizer unsafe def mkCategoryParenthesizerAttribute : IO (KeyedDeclsAttribute CategoryParenthesizer) := KeyedDeclsAttribute.init { builtinName := `builtinCategoryParenthesizer, name := `categoryParenthesizer, descr := "Register a parenthesizer for a syntax category. [parenthesizer cat] registers a declaration of type `Lean.PrettyPrinter.CategoryParenthesizer` for the category `cat`, which is used when parenthesizing calls of `categoryParser cat prec`. Implementations should call `maybeParenthesize` with the precedence and `cat`. If no category parenthesizer is registered, the category will never be parenthesized, but still be traversed for parenthesizing nested categories.", valueTypeName := `Lean.PrettyPrinter.CategoryParenthesizer, evalKey := fun _ args => do env ← getEnv; match attrParamSyntaxToIdentifier args with | some id => if Parser.isParserCategory env id then pure id else throwError ("invalid [parenthesizer] argument, unknown parser category '" ++ toString id ++ "'") | none => throwError "invalid [parenthesizer] argument, expected identifier" } `Lean.PrettyPrinter.categoryParenthesizerAttribute @[init mkCategoryParenthesizerAttribute] constant categoryParenthesizerAttribute : KeyedDeclsAttribute CategoryParenthesizer := arbitrary _ unsafe def mkCombinatorParenthesizerAttribute : IO ParserCompiler.CombinatorAttribute := ParserCompiler.registerCombinatorAttribute `combinatorParenthesizer "Register a parenthesizer for a parser combinator. [combinatorParenthesizer c] registers a declaration of type `Lean.PrettyPrinter.Parenthesizer` for the `Parser` declaration `c`. Note that, unlike with [parenthesizer], this is not a node kind since combinators usually do not introduce their own node kinds. The tagged declaration may optionally accept parameters corresponding to (a prefix of) those of `c`, where `Parser` is replaced with `Parenthesizer` in the parameter types." @[init mkCombinatorParenthesizerAttribute] constant combinatorParenthesizerAttribute : ParserCompiler.CombinatorAttribute := arbitrary _ namespace Parenthesizer open Lean.Core open Lean.Format def throwBacktrack {α} : ParenthesizerM α := throw $ Exception.internal backtrackExceptionId instance ParenthesizerM.monadTraverser : Syntax.MonadTraverser ParenthesizerM := ⟨{ get := State.stxTrav <$> get, set := fun t => modify (fun st => { st with stxTrav := t }), modifyGet := fun _ f => modifyGet (fun st => let (a, t) := f st.stxTrav; (a, { st with stxTrav := t })) }⟩ open Syntax.MonadTraverser def addPrecCheck (prec : Nat) : ParenthesizerM Unit := modify $ fun st => { st with contPrec := Nat.min (st.contPrec.getD prec) prec, minPrec := Nat.min (st.minPrec.getD prec) prec } /-- Execute `x` at the right-most child of the current node, if any, then advance to the left. -/ def visitArgs (x : ParenthesizerM Unit) : ParenthesizerM Unit := do stx ← getCur; when (stx.getArgs.size > 0) $ goDown (stx.getArgs.size - 1) *> x <* goUp; goLeft -- Macro scopes in the parenthesizer output are ultimately ignored by the pretty printer, -- so give a trivial implementation. instance monadQuotation : MonadQuotation ParenthesizerM := { getCurrMacroScope := pure $ arbitrary _, getMainModule := pure $ arbitrary _, withFreshMacroScope := fun α x => x, } /-- Run `x` and parenthesize the result using `mkParen` if necessary. -/ def maybeParenthesize (cat : Name) (mkParen : Syntax → Syntax) (prec : Nat) (x : ParenthesizerM Unit) : ParenthesizerM Unit := do stx ← getCur; idx ← getIdx; st ← get; -- reset precs for the recursive call set { stxTrav := st.stxTrav : State }; trace! `PrettyPrinter.parenthesize ("parenthesizing (cont := " ++ toString (st.contPrec, st.contCat) ++ ")" ++ MessageData.nest 2 (line ++ stx)); x; { minPrec := some minPrec, trailPrec := trailPrec, .. } ← get | panic! "maybeParenthesize: visited a syntax tree without precedences?!"; trace! `PrettyPrinter.parenthesize ("...precedences are " ++ fmt prec ++ " >? " ++ fmt minPrec ++ ", " ++ fmt (trailPrec, cat) ++ " <=? " ++ fmt (st.contPrec, st.contCat)); -- Should we parenthesize? when (prec > minPrec || match trailPrec, st.contPrec with some trailPrec, some contPrec => cat == st.contCat && trailPrec <= contPrec | _, _ => false) $ do { -- The recursive `visit` call, by the invariant, has moved to the preceding node. In order to parenthesize -- the original node, we must first move to the right, except if we already were at the left-most child in the first -- place. when (idx > 0) goRight; stx ← getCur; match stx.getHeadInfo, stx.getTailInfo with | some hi, some ti => -- Move leading/trailing whitespace of `stx` outside of parentheses let stx := (stx.setHeadInfo { hi with leading := "".toSubstring }).setTailInfo { ti with trailing := "".toSubstring }; let stx := mkParen stx; let stx := (stx.setHeadInfo { hi with trailing := "".toSubstring }).setTailInfo { ti with leading := "".toSubstring }; setCur stx | _, _ => setCur (mkParen stx); stx ← getCur; trace! `PrettyPrinter.parenthesize ("parenthesized: " ++ stx.formatStx none); goLeft; -- after parenthesization, there is no more trailing parser modify (fun st => { st with contPrec := Parser.maxPrec, contCat := cat, trailPrec := none }) }; { trailPrec := trailPrec, .. } ← get; -- If we already had a token at this level, keep the trailing parser. Otherwise, use the minimum of -- `prec` and `trailPrec`. let trailPrec := if st.visitedToken then st.trailPrec else match trailPrec with | some trailPrec => some (Nat.min trailPrec prec) | _ => some prec; modify (fun stP => { stP with minPrec := st.minPrec, trailPrec := trailPrec }) /-- Adjust state and advance. -/ def visitToken : Parenthesizer := do modify (fun st => { st with contPrec := none, contCat := Name.anonymous, visitedToken := true }); goLeft @[combinatorParenthesizer Lean.Parser.orelse] def orelse.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := do st ← get; -- HACK: We have no (immediate) information on which side of the orelse could have produced the current node, so try -- them in turn. Uses the syntax traverser non-linearly! p1 <|> p2 -- `mkAntiquot` is quite complex, so we'd rather have its parenthesizer synthesized below the actual parser definition. -- Note that there is a mutual recursion -- `categoryParser -> mkAntiquot -> termParser -> categoryParser`, so we need to introduce an indirection somewhere -- anyway. @[extern 8 "lean_mk_antiquot_parenthesizer"] constant mkAntiquot.parenthesizer' (name : String) (kind : Option SyntaxNodeKind) (anonymous := true) : Parenthesizer := arbitrary _ @[inline] def liftCoreM {α} (x : CoreM α) : ParenthesizerM α := liftM x def throwError {α} (msg : MessageData) : ParenthesizerM α := liftCoreM $ throwError msg def parenthesizerForKind (k : SyntaxNodeKind) : Parenthesizer := do env ← getEnv; p::_ ← pure $ parenthesizerAttribute.getValues env k | throwError $ "no known parenthesizer for kind '" ++ toString k ++ "'"; p @[combinatorParenthesizer Lean.Parser.withAntiquot] def withAntiquot.parenthesizer (antiP p : Parenthesizer) : Parenthesizer := -- TODO: could be optimized using `isAntiquot` (which would have to be moved), but I'd rather -- fix the backtracking hack outright. orelse.parenthesizer antiP p def parenthesizeCategoryCore (cat : Name) (prec : Nat) : Parenthesizer := adaptReader (fun (ctx : Context) => { ctx with cat := cat }) do stx ← getCur; if stx.getKind == `choice then visitArgs $ stx.getArgs.size.forM $ fun _ => do stx ← getCur; parenthesizerForKind stx.getKind else withAntiquot.parenthesizer (mkAntiquot.parenthesizer' cat.toString none) (parenthesizerForKind stx.getKind); modify fun st => { st with contCat := cat } @[combinatorParenthesizer Lean.Parser.categoryParser] def categoryParser.parenthesizer (cat : Name) (prec : Nat) : Parenthesizer := do env ← getEnv; match categoryParenthesizerAttribute.getValues env cat with | p::_ => p prec -- Fall back to the generic parenthesizer. -- In this case this node will never be parenthesized since we don't know which parentheses to use. | _ => parenthesizeCategoryCore cat prec @[combinatorParenthesizer Lean.Parser.categoryParserOfStack] def categoryParserOfStack.parenthesizer (offset : Nat) (prec : Nat) : Parenthesizer := do st ← get; let stx := st.stxTrav.parents.back.getArg (st.stxTrav.idxs.back - offset); categoryParser.parenthesizer stx.getId prec @[builtinCategoryParenthesizer term] def term.parenthesizer : CategoryParenthesizer | prec => do stx ← getCur; -- this can happen at `termParser <|> many1 commandParser` in `Term.stxQuot` if stx.getKind == nullKind then throwBacktrack else do maybeParenthesize `term (fun stx => Unhygienic.run `(($stx))) prec $ parenthesizeCategoryCore `term prec @[builtinCategoryParenthesizer tactic] def tactic.parenthesizer : CategoryParenthesizer | prec => do maybeParenthesize `tactic (fun stx => Unhygienic.run `(tactic|($stx))) prec $ parenthesizeCategoryCore `tactic prec @[builtinCategoryParenthesizer level] def level.parenthesizer : CategoryParenthesizer | prec => do maybeParenthesize `level (fun stx => Unhygienic.run `(level|($stx))) prec $ parenthesizeCategoryCore `level prec @[combinatorParenthesizer Lean.Parser.try] def try.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.lookahead] def lookahead.parenthesizer (p : Parenthesizer) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.notFollowedBy] def notFollowedBy.parenthesizer (p : Parenthesizer) : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.andthen] def andthen.parenthesizer (p1 p2 : Parenthesizer) : Parenthesizer := p2 *> p1 @[combinatorParenthesizer Lean.Parser.node] def node.parenthesizer (k : SyntaxNodeKind) (p : Parenthesizer) : Parenthesizer := do stx ← getCur; when (k != stx.getKind) $ do { trace! `PrettyPrinter.parenthesize.backtrack ("unexpected node kind '" ++ toString stx.getKind ++ "', expected '" ++ toString k ++ "'"); -- HACK; see `orelse.parenthesizer` throwBacktrack }; visitArgs p @[combinatorParenthesizer Lean.Parser.checkPrec] def checkPrec.parenthesizer (prec : Nat) : Parenthesizer := addPrecCheck prec @[combinatorParenthesizer Lean.Parser.leadingNode] def leadingNode.parenthesizer (k : SyntaxNodeKind) (prec : Nat) (p : Parenthesizer) : Parenthesizer := do node.parenthesizer k p; addPrecCheck prec; -- Limit `cont` precedence to `maxPrec-1`. -- This is because `maxPrec-1` is the precedence of function application, which is the only way to turn a leading parser -- into a trailing one. modify fun st => { st with contPrec := Nat.min (Parser.maxPrec-1) prec } @[combinatorParenthesizer Lean.Parser.trailingNode] def trailingNode.parenthesizer (k : SyntaxNodeKind) (prec : Nat) (p : Parenthesizer) : Parenthesizer := do stx ← getCur; when (k != stx.getKind) $ do { trace! `PrettyPrinter.parenthesize.backtrack ("unexpected node kind '" ++ toString stx.getKind ++ "', expected '" ++ toString k ++ "'"); -- HACK; see `orelse.parenthesizer` throwBacktrack }; visitArgs $ do { p; addPrecCheck prec; ctx ← read; modify fun st => { st with contCat := ctx.cat }; -- After visiting the nodes actually produced by the parser passed to `trailingNode`, we are positioned on the -- left-most child, which is the term injected by `trailingNode` in place of the recursion. Left recursion is not an -- issue for the parenthesizer, so we can think of this child being produced by `termParser 0`, or whichever Pratt -- parser is calling us. categoryParser.parenthesizer ctx.cat 0 } @[combinatorParenthesizer Lean.Parser.symbol] def symbol.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.symbolNoWs] def symbolNoWs.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.unicodeSymbol] def unicodeSymbol.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.identNoAntiquot] def identNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.rawIdent] def rawIdent.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.identEq] def identEq.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.nonReservedSymbol] def nonReservedSymbol.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.charLitNoAntiquot] def charLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.strLitNoAntiquot] def strLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.nameLitNoAntiquot] def nameLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.numLitNoAntiquot] def numLitNoAntiquot.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.fieldIdx] def fieldIdx.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.many] def many.parenthesizer (p : Parenthesizer) : Parenthesizer := do stx ← getCur; visitArgs $ stx.getArgs.size.forM fun _ => p @[combinatorParenthesizer Lean.Parser.many1] def many1.parenthesizer (p : Parenthesizer) : Parenthesizer := do many.parenthesizer p @[combinatorParenthesizer Lean.Parser.many1Unbox] def many1Unbox.parenthesizer (p : Parenthesizer) : Parenthesizer := do stx ← getCur; if stx.getKind == nullKind then many.parenthesizer p else p @[combinatorParenthesizer Lean.Parser.optional] def optional.parenthesizer (p : Parenthesizer) : Parenthesizer := do visitArgs p @[combinatorParenthesizer Lean.Parser.sepBy] def sepBy.parenthesizer (p pSep : Parenthesizer) : Parenthesizer := do stx ← getCur; visitArgs $ (List.range stx.getArgs.size).reverse.forM $ fun i => if i % 2 == 0 then p else pSep @[combinatorParenthesizer Lean.Parser.sepBy1] def sepBy1.parenthesizer := sepBy.parenthesizer @[combinatorParenthesizer Lean.Parser.nodeSepBy1Unbox] def nodeSepBy1Unbox.parenthesizer (k : SyntaxNodeKind) (p pSep : Parenthesizer) : Parenthesizer := do stx ← getCur; if stx.getKind == k then node.parenthesizer k $ sepBy.parenthesizer p pSep else p @[combinatorParenthesizer Lean.Parser.withPosition] def withPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := do p @[combinatorParenthesizer Lean.Parser.withoutPosition] def withoutPosition.parenthesizer (p : Parenthesizer) : Parenthesizer := do p @[combinatorParenthesizer Lean.Parser.setExpected] def setExpected.parenthesizer (expected : List String) (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.toggleInsideQuot] def toggleInsideQuot.parenthesizer (p : Parenthesizer) : Parenthesizer := p @[combinatorParenthesizer Lean.Parser.checkStackTop] def checkStackTop.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkWsBefore] def checkWsBefore.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkNoWsBefore] def checkNoWsBefore.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkTailWs] def checkTailWs.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkColGe] def checkColGe.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkColGt] def checkColGt.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.eoi] def eoi.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.notFollowedByCategoryToken] def notFollowedByCategoryToken.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkNoImmediateColon] def checkNoImmediateColon.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkInsideQuot] def checkInsideQuot.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.checkOutsideQuot] def checkOutsideQuot.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.skip] def skip.parenthesizer : Parenthesizer := pure () @[combinatorParenthesizer Lean.Parser.pushNone] def pushNone.parenthesizer : Parenthesizer := goLeft @[combinatorParenthesizer Lean.Parser.quotedSymbol] def quotedSymbol.parenthesizer := visitToken @[combinatorParenthesizer Lean.Parser.unquotedSymbol] def unquotedSymbol.parenthesizer := visitToken @[combinatorParenthesizer ite, macroInline] def ite {α : Type} (c : Prop) [h : Decidable c] (t e : Parenthesizer) : Parenthesizer := if c then t else e end Parenthesizer open Parenthesizer /-- Add necessary parentheses in `stx` parsed by `parser`. -/ def parenthesize (parenthesizer : Parenthesizer) (stx : Syntax) : CoreM Syntax := catchInternalId backtrackExceptionId (do (_, st) ← (parenthesizer {}).run { stxTrav := Syntax.Traverser.fromSyntax stx }; pure st.stxTrav.cur) (fun _ => throwError "parenthesize: uncaught backtrack exception") def parenthesizeTerm := parenthesize $ categoryParser.parenthesizer `term 0 def parenthesizeCommand := parenthesize $ categoryParser.parenthesizer `command 0 @[init] private def regTraceClasses : IO Unit := do registerTraceClass `PrettyPrinter.parenthesize; pure () end PrettyPrinter end Lean
d73123c29c11814e7e3b4383c5c2d1069d19db8e
08bd4ba4ca87dba1f09d2c96a26f5d65da81f4b4
/src/Lean/Util/FindExpr.lean
db6d64896d78b6db5a682957a926e1b7c48ade25
[ "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", "Apache-2.0", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
gebner/lean4
d51c4922640a52a6f7426536ea669ef18a1d9af5
8cd9ce06843c9d42d6d6dc43d3e81e3b49dfc20f
refs/heads/master
1,685,732,780,391
1,672,962,627,000
1,673,459,398,000
373,307,283
0
0
Apache-2.0
1,691,316,730,000
1,622,669,271,000
Lean
UTF-8
Lean
false
false
3,740
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.Expr namespace Lean namespace Expr namespace FindImpl abbrev cacheSize : USize := 8192 - 1 structure State where keys : Array Expr -- Remark: our "unsafe" implementation relies on the fact that `()` is not a valid Expr abbrev FindM := StateT State Id unsafe def visited (e : Expr) (size : USize) : FindM Bool := do let s ← get let h := ptrAddrUnsafe e let i := h % size let k := s.keys.uget i lcProof if ptrAddrUnsafe k == h then pure true else modify fun s => { keys := s.keys.uset i e lcProof } pure false unsafe def findM? (p : Expr → Bool) (size : USize) (e : Expr) : OptionT FindM Expr := let rec visit (e : Expr) := do if (← visited e size) then failure else if p e then pure e else match e with | Expr.forallE _ d b _ => visit d <|> visit b | Expr.lam _ d b _ => visit d <|> visit b | Expr.mdata _ b => visit b | Expr.letE _ t v b _ => visit t <|> visit v <|> visit b | Expr.app f a => visit f <|> visit a | Expr.proj _ _ b => visit b | _ => failure visit e unsafe def initCache : State := { keys := mkArray cacheSize.toNat (cast lcProof ()) } unsafe def findUnsafe? (p : Expr → Bool) (e : Expr) : Option Expr := Id.run <| findM? p cacheSize e |>.run' initCache end FindImpl @[implemented_by FindImpl.findUnsafe?] def find? (p : Expr → Bool) (e : Expr) : Option Expr := /- This is a reference implementation for the unsafe one above -/ if p e then some e else match e with | Expr.forallE _ d b _ => find? p d <|> find? p b | Expr.lam _ d b _ => find? p d <|> find? p b | Expr.mdata _ b => find? p b | Expr.letE _ t v b _ => find? p t <|> find? p v <|> find? p b | Expr.app f a => find? p f <|> find? p a | Expr.proj _ _ b => find? p b | _ => none /-- Return true if `e` occurs in `t` -/ def occurs (e : Expr) (t : Expr) : Bool := (t.find? fun s => s == e).isSome /-- Return type for `findExt?` function argument. -/ inductive FindStep where /-- Found desired subterm -/ | found /-- Search subterms -/ | visit /-- Do not search subterms -/ | done namespace FindExtImpl unsafe def findM? (p : Expr → FindStep) (size : USize) (e : Expr) : OptionT FindImpl.FindM Expr := visit e where visitApp (e : Expr) := match e with | Expr.app f a .. => visitApp f <|> visit a | e => visit e visit (e : Expr) := do if (← FindImpl.visited e size) then failure else match p e with | FindStep.done => failure | FindStep.found => pure e | FindStep.visit => match e with | Expr.forallE _ d b _ => visit d <|> visit b | Expr.lam _ d b _ => visit d <|> visit b | Expr.mdata _ b => visit b | Expr.letE _ t v b _ => visit t <|> visit v <|> visit b | Expr.app .. => visitApp e | Expr.proj _ _ b => visit b | _ => failure unsafe def findUnsafe? (p : Expr → FindStep) (e : Expr) : Option Expr := Id.run <| findM? p FindImpl.cacheSize e |>.run' FindImpl.initCache end FindExtImpl /-- Similar to `find?`, but `p` can return `FindStep.done` to interrupt the search on subterms. Remark: Differently from `find?`, we do not invoke `p` for partial applications of an application. -/ @[implemented_by FindExtImpl.findUnsafe?] opaque findExt? (p : Expr → FindStep) (e : Expr) : Option Expr end Expr end Lean
a7abc722c00fafd24d2ffb4f1f6449ccd46603c8
82e44445c70db0f03e30d7be725775f122d72f3e
/src/analysis/complex/circle.lean
d7da1c09cd78e16a239046c0ba3c4383f160890f
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
3,910
lean
/- Copyright (c) 2021 Heather Macbeth. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Heather Macbeth -/ import analysis.complex.basic import data.complex.exponential /-! # The circle This file defines `circle` to be the metric sphere (`metric.sphere`) in `ℂ` centred at `0` of radius `1`. We equip it with the following structure: * a submonoid of `ℂ` * a group * a topological group We furthermore define `exp_map_circle` to be the natural map `λ t, exp (t * I)` from `ℝ` to `circle`, and show that this map is a group homomorphism. ## Implementation notes Because later (in `geometry.manifold.instances.sphere`) one wants to equip the circle with a smooth manifold structure borrowed from `metric.sphere`, the underlying set is `{z : ℂ | abs (z - 0) = 1}`. This prevents certain algebraic facts from working definitionally -- for example, the circle is not defeq to `{z : ℂ | abs z = 1}`, which is the kernel of `complex.abs` considered as a homomorphism from `ℂ` to `ℝ`, nor is it defeq to `{z : ℂ | norm_sq z = 1}`, which is the kernel of the homomorphism `complex.norm_sq` from `ℂ` to `ℝ`. -/ noncomputable theory open complex metric /-- The unit circle in `ℂ`, here given the structure of a submonoid of `ℂ`. -/ def circle : submonoid ℂ := { carrier := sphere (0:ℂ) 1, one_mem' := by simp, mul_mem' := λ a b, begin simp only [norm_eq_abs, mem_sphere_zero_iff_norm], intros ha hb, simp [ha, hb], end } @[simp] lemma mem_circle_iff_abs (z : ℂ) : z ∈ circle ↔ abs z = 1 := mem_sphere_zero_iff_norm lemma circle_def : ↑circle = {z : ℂ | abs z = 1} := by { ext, simp } @[simp] lemma abs_eq_of_mem_circle (z : circle) : abs z = 1 := by { convert z.2, simp } @[simp] lemma norm_sq_eq_of_mem_circle (z : circle) : norm_sq z = 1 := by simp [norm_sq_eq_abs] lemma nonzero_of_mem_circle (z : circle) : (z:ℂ) ≠ 0 := nonzero_of_mem_unit_sphere z instance : group circle := { inv := λ z, ⟨conj z, by simp⟩, mul_left_inv := λ z, subtype.ext $ by { simp [has_inv.inv, ← norm_sq_eq_conj_mul_self, ← mul_self_abs] }, .. circle.to_monoid } lemma coe_inv_circle_eq_conj (z : circle) : ↑(z⁻¹) = conj z := rfl @[simp] lemma coe_inv_circle (z : circle) : ↑(z⁻¹) = (z : ℂ)⁻¹ := begin rw coe_inv_circle_eq_conj, apply eq_inv_of_mul_right_eq_one, rw [mul_comm, ← complex.norm_sq_eq_conj_mul_self], simp, end @[simp] lemma coe_div_circle (z w : circle) : ↑(z / w) = (z:ℂ) / w := show ↑(z * w⁻¹) = (z:ℂ) * w⁻¹, by simp instance : compact_space circle := metric.sphere.compact_space _ _ -- the following result could instead be deduced from the Lie group structure on the circle using -- `topological_group_of_lie_group`, but that seems a little awkward since one has to first provide -- and then forget the model space instance : topological_group circle := { continuous_mul := let h : continuous (λ x : circle, (x : ℂ)) := continuous_subtype_coe in continuous_induced_rng (continuous_mul.comp (h.prod_map h)), continuous_inv := continuous_induced_rng $ complex.conj_cle.continuous.comp continuous_subtype_coe } /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ`. -/ def exp_map_circle (t : ℝ) : circle := ⟨exp (t * I), by simp [exp_mul_I, abs_cos_add_sin_mul_I]⟩ @[simp] lemma exp_map_circle_apply (t : ℝ) : ↑(exp_map_circle t) = complex.exp (t * complex.I) := rfl /-- The map `λ t, exp (t * I)` from `ℝ` to the unit circle in `ℂ`, considered as a homomorphism of groups. -/ def exp_map_circle_hom : ℝ →+ (additive circle) := { to_fun := exp_map_circle, map_zero' := by { rw exp_map_circle, convert of_mul_one, simp }, map_add' := λ x y, show exp_map_circle (x + y) = (exp_map_circle x) * (exp_map_circle y), from subtype.ext $ by simp [exp_map_circle, exp_add, add_mul] }
99a2fbd2bc5ef4a7c5c7162b964687e11fff09d0
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/algebra/polynomial/group_ring_action.lean
35c3dcb56b2c8dff2f997d9e2d9266fe071e09f0
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
4,922
lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import algebra.group_ring_action.basic import algebra.hom.group_action import data.polynomial.algebra_map import data.polynomial.monic import group_theory.group_action.quotient /-! # Group action on rings applied to polynomials This file contains instances and definitions relating `mul_semiring_action` to `polynomial`. -/ variables (M : Type*) [monoid M] open_locale polynomial namespace polynomial variables (R : Type*) [semiring R] variables {M} lemma smul_eq_map [mul_semiring_action M R] (m : M) : ((•) m) = map (mul_semiring_action.to_ring_hom M R m) := begin suffices : distrib_mul_action.to_add_monoid_hom R[X] m = (map_ring_hom (mul_semiring_action.to_ring_hom M R m)).to_add_monoid_hom, { ext1 r, exact add_monoid_hom.congr_fun this r, }, ext n r : 2, change m • monomial n r = map (mul_semiring_action.to_ring_hom M R m) (monomial n r), simpa only [polynomial.map_monomial, polynomial.smul_monomial], end variables (M) noncomputable instance [mul_semiring_action M R] : mul_semiring_action M R[X] := { smul := (•), smul_one := λ m, (smul_eq_map R m).symm ▸ polynomial.map_one (mul_semiring_action.to_ring_hom M R m), smul_mul := λ m p q, (smul_eq_map R m).symm ▸ polynomial.map_mul (mul_semiring_action.to_ring_hom M R m), ..polynomial.distrib_mul_action } variables {M R} variables [mul_semiring_action M R] @[simp] lemma smul_X (m : M) : (m • X : R[X]) = X := (smul_eq_map R m).symm ▸ map_X _ variables (S : Type*) [comm_semiring S] [mul_semiring_action M S] theorem smul_eval_smul (m : M) (f : S[X]) (x : S) : (m • f).eval (m • x) = m • f.eval x := polynomial.induction_on f (λ r, by rw [smul_C, eval_C, eval_C]) (λ f g ihf ihg, by rw [smul_add, eval_add, ihf, ihg, eval_add, smul_add]) (λ n r ih, by rw [smul_mul', smul_pow', smul_C, smul_X, eval_mul, eval_C, eval_pow, eval_X, eval_mul, eval_C, eval_pow, eval_X, smul_mul', smul_pow']) variables (G : Type*) [group G] theorem eval_smul' [mul_semiring_action G S] (g : G) (f : S[X]) (x : S) : f.eval (g • x) = g • (g⁻¹ • f).eval x := by rw [← smul_eval_smul, smul_inv_smul] theorem smul_eval [mul_semiring_action G S] (g : G) (f : S[X]) (x : S) : (g • f).eval x = g • f.eval (g⁻¹ • x) := by rw [← smul_eval_smul, smul_inv_smul] end polynomial section comm_ring variables (G : Type*) [group G] [fintype G] variables (R : Type*) [comm_ring R] [mul_semiring_action G R] open mul_action open_locale classical /-- the product of `(X - g • x)` over distinct `g • x`. -/ noncomputable def prod_X_sub_smul (x : R) : R[X] := (finset.univ : finset (G ⧸ mul_action.stabilizer G x)).prod $ λ g, polynomial.X - polynomial.C (of_quotient_stabilizer G x g) theorem prod_X_sub_smul.monic (x : R) : (prod_X_sub_smul G R x).monic := polynomial.monic_prod_of_monic _ _ $ λ g _, polynomial.monic_X_sub_C _ theorem prod_X_sub_smul.eval (x : R) : (prod_X_sub_smul G R x).eval x = 0 := (monoid_hom.map_prod ((polynomial.aeval x).to_ring_hom.to_monoid_hom : R[X] →* R) _ _).trans $ finset.prod_eq_zero (finset.mem_univ $ quotient_group.mk 1) $ by simp theorem prod_X_sub_smul.smul (x : R) (g : G) : g • prod_X_sub_smul G R x = prod_X_sub_smul G R x := finset.smul_prod.trans $ fintype.prod_bijective _ (mul_action.bijective g) _ _ (λ g', by rw [of_quotient_stabilizer_smul, smul_sub, polynomial.smul_X, polynomial.smul_C]) theorem prod_X_sub_smul.coeff (x : R) (g : G) (n : ℕ) : g • (prod_X_sub_smul G R x).coeff n = (prod_X_sub_smul G R x).coeff n := by rw [← polynomial.coeff_smul, prod_X_sub_smul.smul] end comm_ring namespace mul_semiring_action_hom variables {M} variables {P : Type*} [comm_semiring P] [mul_semiring_action M P] variables {Q : Type*} [comm_semiring Q] [mul_semiring_action M Q] open polynomial /-- An equivariant map induces an equivariant map on polynomials. -/ protected noncomputable def polynomial (g : P →+*[M] Q) : P[X] →+*[M] Q[X] := { to_fun := map g, map_smul' := λ m p, polynomial.induction_on p (λ b, by rw [smul_C, map_C, coe_fn_coe, g.map_smul, map_C, coe_fn_coe, smul_C]) (λ p q ihp ihq, by rw [smul_add, polynomial.map_add, ihp, ihq, polynomial.map_add, smul_add]) (λ n b ih, by rw [smul_mul', smul_C, smul_pow', smul_X, polynomial.map_mul, map_C, polynomial.map_pow, map_X, coe_fn_coe, g.map_smul, polynomial.map_mul, map_C, polynomial.map_pow, map_X, smul_mul', smul_C, smul_pow', smul_X, coe_fn_coe]), map_zero' := polynomial.map_zero g, map_add' := λ p q, polynomial.map_add g, map_one' := polynomial.map_one g, map_mul' := λ p q, polynomial.map_mul g } @[simp] theorem coe_polynomial (g : P →+*[M] Q) : (g.polynomial : P[X] → Q[X]) = map g := rfl end mul_semiring_action_hom
ce07eaf0f2aa0c327b4e32ff93cb4bb66ac718e4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/analysis/normed_space/star/matrix.lean
0de74bb3ddbeb2f9c520478a6a8fd791b5702055
[ "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,585
lean
/- Copyright (c) 2022 Hans Parshall. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Hans Parshall -/ import analysis.matrix import analysis.normed_space.basic import data.is_R_or_C.basic import linear_algebra.unitary_group /-! # Unitary matrices > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file collects facts about the unitary matrices over `𝕜` (either `ℝ` or `ℂ`). -/ open_locale big_operators matrix variables {𝕜 m n E : Type*} section entrywise_sup_norm variables [is_R_or_C 𝕜] [fintype n] [decidable_eq n] lemma entry_norm_bound_of_unitary {U : matrix n n 𝕜} (hU : U ∈ matrix.unitary_group n 𝕜) (i j : n): ‖U i j‖ ≤ 1 := begin -- The norm squared of an entry is at most the L2 norm of its row. have norm_sum : ‖ U i j ‖^2 ≤ (∑ x, ‖ U i x ‖^2), { apply multiset.single_le_sum, { intros x h_x, rw multiset.mem_map at h_x, cases h_x with a h_a, rw ← h_a.2, apply sq_nonneg }, { rw multiset.mem_map, use j, simp only [eq_self_iff_true, finset.mem_univ_val, and_self, sq_eq_sq] } }, -- The L2 norm of a row is a diagonal entry of U ⬝ Uᴴ have diag_eq_norm_sum : (U ⬝ Uᴴ) i i = ∑ (x : n), ‖ U i x ‖^2, { simp only [matrix.mul_apply, matrix.conj_transpose_apply, ←star_ring_end_apply, is_R_or_C.mul_conj, is_R_or_C.norm_sq_eq_def', is_R_or_C.of_real_pow] }, -- The L2 norm of a row is a diagonal entry of U ⬝ Uᴴ, real part have re_diag_eq_norm_sum : is_R_or_C.re ((U ⬝ Uᴴ) i i) = ∑ (x : n), ‖ U i x ‖^2, { rw is_R_or_C.ext_iff at diag_eq_norm_sum, rw diag_eq_norm_sum.1, norm_cast }, -- Since U is unitary, the diagonal entries of U ⬝ Uᴴ are all 1 have mul_eq_one : (U ⬝ Uᴴ) = 1, from unitary.mul_star_self_of_mem hU, have diag_eq_one : is_R_or_C.re ((U ⬝ Uᴴ) i i) = 1, { simp only [mul_eq_one, eq_self_iff_true, matrix.one_apply_eq, is_R_or_C.one_re] }, -- Putting it all together rw [← sq_le_one_iff (norm_nonneg (U i j)), ← diag_eq_one, re_diag_eq_norm_sum], exact norm_sum, end local attribute [instance] matrix.normed_add_comm_group /-- The entrywise sup norm of a unitary matrix is at most 1. -/ lemma entrywise_sup_norm_bound_of_unitary {U : matrix n n 𝕜} (hU : U ∈ matrix.unitary_group n 𝕜) : ‖ U ‖ ≤ 1 := begin simp_rw pi_norm_le_iff_of_nonneg zero_le_one, intros i j, exact entry_norm_bound_of_unitary hU _ _ end end entrywise_sup_norm
25eb17a8940ebc00e8f91683265d92adc6715a2f
63abd62053d479eae5abf4951554e1064a4c45b4
/src/data/set/intervals/basic.lean
93529d45c3938350dab7695e29ac1f57799e52ec
[ "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
37,968
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov -/ import algebra.ordered_group import data.set.basic /-! # Intervals In any preorder `α`, we define intervals (which on each side can be either infinite, open, or closed) using the following naming conventions: - `i`: infinite - `o`: open - `c`: closed Each interval has the name `I` + letter for left side + letter for right side. For instance, `Ioc a b` denotes the inverval `(a, b]`. This file contains these definitions, and basic facts on inclusion, intersection, difference of intervals (where the precise statements may depend on the properties of the order, in particular for some statements it should be `linear_order` or `densely_ordered`). TODO: This is just the beginning; a lot of rules are missing -/ universe u namespace set open set section intervals variables {α : Type u} [preorder α] {a a₁ a₂ b b₁ b₂ x : α} /-- Left-open right-open interval -/ def Ioo (a b : α) := {x | a < x ∧ x < b} /-- Left-closed right-open interval -/ def Ico (a b : α) := {x | a ≤ x ∧ x < b} /-- Left-infinite right-open interval -/ def Iio (a : α) := {x | x < a} /-- Left-closed right-closed interval -/ def Icc (a b : α) := {x | a ≤ x ∧ x ≤ b} /-- Left-infinite right-closed interval -/ def Iic (b : α) := {x | x ≤ b} /-- Left-open right-closed interval -/ def Ioc (a b : α) := {x | a < x ∧ x ≤ b} /-- Left-closed right-infinite interval -/ def Ici (a : α) := {x | a ≤ x} /-- Left-open right-infinite interval -/ def Ioi (a : α) := {x | a < x} lemma Ioo_def (a b : α) : {x | a < x ∧ x < b} = Ioo a b := rfl lemma Ico_def (a b : α) : {x | a ≤ x ∧ x < b} = Ico a b := rfl lemma Iio_def (a : α) : {x | x < a} = Iio a := rfl lemma Icc_def (a b : α) : {x | a ≤ x ∧ x ≤ b} = Icc a b := rfl lemma Iic_def (b : α) : {x | x ≤ b} = Iic b := rfl lemma Ioc_def (a b : α) : {x | a < x ∧ x ≤ b} = Ioc a b := rfl lemma Ici_def (a : α) : {x | a ≤ x} = Ici a := rfl lemma Ioi_def (a : α) : {x | a < x} = Ioi a := rfl @[simp] lemma mem_Ioo : x ∈ Ioo a b ↔ a < x ∧ x < b := iff.rfl @[simp] lemma mem_Ico : x ∈ Ico a b ↔ a ≤ x ∧ x < b := iff.rfl @[simp] lemma mem_Iio : x ∈ Iio b ↔ x < b := iff.rfl @[simp] lemma mem_Icc : x ∈ Icc a b ↔ a ≤ x ∧ x ≤ b := iff.rfl @[simp] lemma mem_Iic : x ∈ Iic b ↔ x ≤ b := iff.rfl @[simp] lemma mem_Ioc : x ∈ Ioc a b ↔ a < x ∧ x ≤ b := iff.rfl @[simp] lemma mem_Ici : x ∈ Ici a ↔ a ≤ x := iff.rfl @[simp] lemma mem_Ioi : x ∈ Ioi a ↔ a < x := iff.rfl @[simp] lemma left_mem_Ioo : a ∈ Ioo a b ↔ false := by simp [lt_irrefl] @[simp] lemma left_mem_Ico : a ∈ Ico a b ↔ a < b := by simp [le_refl] @[simp] lemma left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := by simp [le_refl] @[simp] lemma left_mem_Ioc : a ∈ Ioc a b ↔ false := by simp [lt_irrefl] lemma left_mem_Ici : a ∈ Ici a := by simp @[simp] lemma right_mem_Ioo : b ∈ Ioo a b ↔ false := by simp [lt_irrefl] @[simp] lemma right_mem_Ico : b ∈ Ico a b ↔ false := by simp [lt_irrefl] @[simp] lemma right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := by simp [le_refl] @[simp] lemma right_mem_Ioc : b ∈ Ioc a b ↔ a < b := by simp [le_refl] lemma right_mem_Iic : a ∈ Iic a := by simp @[simp] lemma dual_Ici : @Ici (order_dual α) _ a = @Iic α _ a := rfl @[simp] lemma dual_Iic : @Iic (order_dual α) _ a = @Ici α _ a := rfl @[simp] lemma dual_Ioi : @Ioi (order_dual α) _ a = @Iio α _ a := rfl @[simp] lemma dual_Iio : @Iio (order_dual α) _ a = @Ioi α _ a := rfl @[simp] lemma dual_Icc : @Icc (order_dual α) _ a b = @Icc α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ioc : @Ioc (order_dual α) _ a b = @Ico α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ico : @Ico (order_dual α) _ a b = @Ioc α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma dual_Ioo : @Ioo (order_dual α) _ a b = @Ioo α _ b a := set.ext $ λ x, and_comm _ _ @[simp] lemma nonempty_Icc : (Icc a b).nonempty ↔ a ≤ b := ⟨λ ⟨x, hx⟩, le_trans hx.1 hx.2, λ h, ⟨a, left_mem_Icc.2 h⟩⟩ @[simp] lemma nonempty_Ico : (Ico a b).nonempty ↔ a < b := ⟨λ ⟨x, hx⟩, lt_of_le_of_lt hx.1 hx.2, λ h, ⟨a, left_mem_Ico.2 h⟩⟩ @[simp] lemma nonempty_Ioc : (Ioc a b).nonempty ↔ a < b := ⟨λ ⟨x, hx⟩, lt_of_lt_of_le hx.1 hx.2, λ h, ⟨b, right_mem_Ioc.2 h⟩⟩ @[simp] lemma nonempty_Ici : (Ici a).nonempty := ⟨a, left_mem_Ici⟩ @[simp] lemma nonempty_Iic : (Iic a).nonempty := ⟨a, right_mem_Iic⟩ @[simp] lemma nonempty_Ioo [densely_ordered α] : (Ioo a b).nonempty ↔ a < b := ⟨λ ⟨x, ha, hb⟩, lt_trans ha hb, exists_between⟩ @[simp] lemma nonempty_Ioi [no_top_order α] : (Ioi a).nonempty := no_top a @[simp] lemma nonempty_Iio [no_bot_order α] : (Iio a).nonempty := no_bot a lemma nonempty_Icc_subtype (h : a ≤ b) : nonempty (Icc a b) := nonempty.to_subtype (nonempty_Icc.mpr h) lemma nonempty_Ico_subtype (h : a < b) : nonempty (Ico a b) := nonempty.to_subtype (nonempty_Ico.mpr h) lemma nonempty_Ioc_subtype (h : a < b) : nonempty (Ioc a b) := nonempty.to_subtype (nonempty_Ioc.mpr h) /-- An interval `Ici a` is nonempty. -/ instance nonempty_Ici_subtype : nonempty (Ici a) := nonempty.to_subtype nonempty_Ici /-- An interval `Iic a` is nonempty. -/ instance nonempty_Iic_subtype : nonempty (Iic a) := nonempty.to_subtype nonempty_Iic lemma nonempty_Ioo_subtype [densely_ordered α] (h : a < b) : nonempty (Ioo a b) := nonempty.to_subtype (nonempty_Ioo.mpr h) /-- In a `no_top_order`, the intervals `Ioi` are nonempty. -/ instance nonempty_Ioi_subtype [no_top_order α] : nonempty (Ioi a) := nonempty.to_subtype nonempty_Ioi /-- In a `no_bot_order`, the intervals `Iio` are nonempty. -/ instance nonempty_Iio_subtype [no_bot_order α] : nonempty (Iio a) := nonempty.to_subtype nonempty_Iio @[simp] lemma Ioo_eq_empty (h : b ≤ a) : Ioo a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_le_of_lt (lt_trans h₁ h₂) h @[simp] lemma Ico_eq_empty (h : b ≤ a) : Ico a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_le_of_lt (lt_of_le_of_lt h₁ h₂) h @[simp] lemma Icc_eq_empty (h : b < a) : Icc a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_lt_of_le (le_trans h₁ h₂) h @[simp] lemma Ioc_eq_empty (h : b ≤ a) : Ioc a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x ⟨h₁, h₂⟩, not_lt_of_le (le_trans h₂ h) h₁ @[simp] lemma Ioo_self (a : α) : Ioo a a = ∅ := Ioo_eq_empty $ le_refl _ @[simp] lemma Ico_self (a : α) : Ico a a = ∅ := Ico_eq_empty $ le_refl _ @[simp] lemma Ioc_self (a : α) : Ioc a a = ∅ := Ioc_eq_empty $ le_refl _ lemma Ici_subset_Ici : Ici a ⊆ Ici b ↔ b ≤ a := ⟨λ h, h $ left_mem_Ici, λ h x hx, le_trans h hx⟩ lemma Iic_subset_Iic : Iic a ⊆ Iic b ↔ a ≤ b := @Ici_subset_Ici (order_dual α) _ _ _ lemma Ici_subset_Ioi : Ici a ⊆ Ioi b ↔ b < a := ⟨λ h, h left_mem_Ici, λ h x hx, lt_of_lt_of_le h hx⟩ lemma Iic_subset_Iio : Iic a ⊆ Iio b ↔ a < b := ⟨λ h, h right_mem_Iic, λ h x hx, lt_of_le_of_lt hx h⟩ lemma Ioo_subset_Ioo (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨lt_of_le_of_lt h₁ hx₁, lt_of_lt_of_le hx₂ h₂⟩ lemma Ioo_subset_Ioo_left (h : a₁ ≤ a₂) : Ioo a₂ b ⊆ Ioo a₁ b := Ioo_subset_Ioo h (le_refl _) lemma Ioo_subset_Ioo_right (h : b₁ ≤ b₂) : Ioo a b₁ ⊆ Ioo a b₂ := Ioo_subset_Ioo (le_refl _) h lemma Ico_subset_Ico (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ico a₁ b₁ ⊆ Ico a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨le_trans h₁ hx₁, lt_of_lt_of_le hx₂ h₂⟩ lemma Ico_subset_Ico_left (h : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b := Ico_subset_Ico h (le_refl _) lemma Ico_subset_Ico_right (h : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ := Ico_subset_Ico (le_refl _) h lemma Icc_subset_Icc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Icc a₁ b₁ ⊆ Icc a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨le_trans h₁ hx₁, le_trans hx₂ h₂⟩ lemma Icc_subset_Icc_left (h : a₁ ≤ a₂) : Icc a₂ b ⊆ Icc a₁ b := Icc_subset_Icc h (le_refl _) lemma Icc_subset_Icc_right (h : b₁ ≤ b₂) : Icc a b₁ ⊆ Icc a b₂ := Icc_subset_Icc (le_refl _) h lemma Icc_subset_Ioo (ha : a₂ < a₁) (hb : b₁ < b₂) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ := λ x hx, ⟨lt_of_lt_of_le ha hx.1, lt_of_le_of_lt hx.2 hb⟩ lemma Icc_subset_Ici_self : Icc a b ⊆ Ici a := λ x, and.left lemma Icc_subset_Iic_self : Icc a b ⊆ Iic b := λ x, and.right lemma Ioc_subset_Iic_self : Ioc a b ⊆ Iic b := λ x, and.right lemma Ioc_subset_Ioc (h₁ : a₂ ≤ a₁) (h₂ : b₁ ≤ b₂) : Ioc a₁ b₁ ⊆ Ioc a₂ b₂ := λ x ⟨hx₁, hx₂⟩, ⟨lt_of_le_of_lt h₁ hx₁, le_trans hx₂ h₂⟩ lemma Ioc_subset_Ioc_left (h : a₁ ≤ a₂) : Ioc a₂ b ⊆ Ioc a₁ b := Ioc_subset_Ioc h (le_refl _) lemma Ioc_subset_Ioc_right (h : b₁ ≤ b₂) : Ioc a b₁ ⊆ Ioc a b₂ := Ioc_subset_Ioc (le_refl _) h lemma Ico_subset_Ioo_left (h₁ : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b := λ x, and.imp_left $ lt_of_lt_of_le h₁ lemma Ioc_subset_Ioo_right (h : b₁ < b₂) : Ioc a b₁ ⊆ Ioo a b₂ := λ x, and.imp_right $ λ h', lt_of_le_of_lt h' h lemma Icc_subset_Ico_right (h₁ : b₁ < b₂) : Icc a b₁ ⊆ Ico a b₂ := λ x, and.imp_right $ λ h₂, lt_of_le_of_lt h₂ h₁ lemma Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := λ x, and.imp_left le_of_lt lemma Ioo_subset_Ioc_self : Ioo a b ⊆ Ioc a b := λ x, and.imp_right le_of_lt lemma Ico_subset_Icc_self : Ico a b ⊆ Icc a b := λ x, and.imp_right le_of_lt lemma Ioc_subset_Icc_self : Ioc a b ⊆ Icc a b := λ x, and.imp_left le_of_lt lemma Ioo_subset_Icc_self : Ioo a b ⊆ Icc a b := subset.trans Ioo_subset_Ico_self Ico_subset_Icc_self lemma Ico_subset_Iio_self : Ico a b ⊆ Iio b := λ x, and.right lemma Ioo_subset_Iio_self : Ioo a b ⊆ Iio b := λ x, and.right lemma Ioc_subset_Ioi_self : Ioc a b ⊆ Ioi a := λ x, and.left lemma Ioo_subset_Ioi_self : Ioo a b ⊆ Ioi a := λ x, and.left lemma Ioi_subset_Ici_self : Ioi a ⊆ Ici a := λx hx, le_of_lt hx lemma Iio_subset_Iic_self : Iio a ⊆ Iic a := λx hx, le_of_lt hx lemma Ico_subset_Ici_self : Ico a b ⊆ Ici a := λ x, and.left lemma Icc_subset_Icc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Icc a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨le_trans h hx, le_trans hx' h'⟩⟩ lemma Icc_subset_Ioo_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ < a₁ ∧ b₁ < b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨lt_of_lt_of_le h hx, lt_of_le_of_lt hx' h'⟩⟩ lemma Icc_subset_Ico_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ < b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨le_trans h hx, lt_of_le_of_lt hx' h'⟩⟩ lemma Icc_subset_Ioc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ a₂ < a₁ ∧ b₁ ≤ b₂ := ⟨λ h, ⟨(h ⟨le_refl _, h₁⟩).1, (h ⟨h₁, le_refl _⟩).2⟩, λ ⟨h, h'⟩ x ⟨hx, hx'⟩, ⟨lt_of_lt_of_le h hx, le_trans hx' h'⟩⟩ lemma Icc_subset_Iio_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iio b₂ ↔ b₁ < b₂ := ⟨λ h, h ⟨h₁, le_refl _⟩, λ h x ⟨hx, hx'⟩, lt_of_le_of_lt hx' h⟩ lemma Icc_subset_Ioi_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioi a₂ ↔ a₂ < a₁ := ⟨λ h, h ⟨le_refl _, h₁⟩, λ h x ⟨hx, hx'⟩, lt_of_lt_of_le h hx⟩ lemma Icc_subset_Iic_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Iic b₂ ↔ b₁ ≤ b₂ := ⟨λ h, h ⟨h₁, le_refl _⟩, λ h x ⟨hx, hx'⟩, le_trans hx' h⟩ lemma Icc_subset_Ici_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ici a₂ ↔ a₂ ≤ a₁ := ⟨λ h, h ⟨le_refl _, h₁⟩, λ h x ⟨hx, hx'⟩, le_trans h hx⟩ /-- If `a ≤ b`, then `(b, +∞) ⊆ (a, +∞)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Ioi_subset_Ioi_iff`. -/ lemma Ioi_subset_Ioi (h : a ≤ b) : Ioi b ⊆ Ioi a := λx hx, lt_of_le_of_lt h hx /-- If `a ≤ b`, then `(b, +∞) ⊆ [a, +∞)`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Ioi_subset_Ici_iff`. -/ lemma Ioi_subset_Ici (h : a ≤ b) : Ioi b ⊆ Ici a := subset.trans (Ioi_subset_Ioi h) Ioi_subset_Ici_self /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b)`. In preorders, this is just an implication. If you need the equivalence in linear orders, use `Iio_subset_Iio_iff`. -/ lemma Iio_subset_Iio (h : a ≤ b) : Iio a ⊆ Iio b := λx hx, lt_of_lt_of_le hx h /-- If `a ≤ b`, then `(-∞, a) ⊆ (-∞, b]`. In preorders, this is just an implication. If you need the equivalence in dense linear orders, use `Iio_subset_Iic_iff`. -/ lemma Iio_subset_Iic (h : a ≤ b) : Iio a ⊆ Iic b := subset.trans (Iio_subset_Iio h) Iio_subset_Iic_self lemma Ici_inter_Iic : Ici a ∩ Iic b = Icc a b := rfl lemma Ici_inter_Iio : Ici a ∩ Iio b = Ico a b := rfl lemma Ioi_inter_Iic : Ioi a ∩ Iic b = Ioc a b := rfl lemma Ioi_inter_Iio : Ioi a ∩ Iio b = Ioo a b := rfl end intervals section partial_order variables {α : Type u} [partial_order α] {a b : α} @[simp] lemma Icc_self (a : α) : Icc a a = {a} := set.ext $ by simp [Icc, le_antisymm_iff, and_comm] @[simp] lemma Icc_diff_left : Icc a b \ {a} = Ioc a b := ext $ λ x, by simp [lt_iff_le_and_ne, eq_comm, and.right_comm] @[simp] lemma Icc_diff_right : Icc a b \ {b} = Ico a b := ext $ λ x, by simp [lt_iff_le_and_ne, and_assoc] @[simp] lemma Ico_diff_left : Ico a b \ {a} = Ioo a b := ext $ λ x, by simp [and.right_comm, ← lt_iff_le_and_ne, eq_comm] @[simp] lemma Ioc_diff_right : Ioc a b \ {b} = Ioo a b := ext $ λ x, by simp [and_assoc, ← lt_iff_le_and_ne] @[simp] lemma Icc_diff_both : Icc a b \ {a, b} = Ioo a b := by rw [insert_eq, ← diff_diff, Icc_diff_left, Ioc_diff_right] @[simp] lemma Ici_diff_left : Ici a \ {a} = Ioi a := ext $ λ x, by simp [lt_iff_le_and_ne, eq_comm] @[simp] lemma Iic_diff_right : Iic a \ {a} = Iio a := ext $ λ x, by simp [lt_iff_le_and_ne] @[simp] lemma Ico_diff_Ioo_same (h : a < b) : Ico a b \ Ioo a b = {a} := by rw [← Ico_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 $ left_mem_Ico.2 h)] @[simp] lemma Ioc_diff_Ioo_same (h : a < b) : Ioc a b \ Ioo a b = {b} := by rw [← Ioc_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 $ right_mem_Ioc.2 h)] @[simp] lemma Icc_diff_Ico_same (h : a ≤ b) : Icc a b \ Ico a b = {b} := by rw [← Icc_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 $ right_mem_Icc.2 h)] @[simp] lemma Icc_diff_Ioc_same (h : a ≤ b) : Icc a b \ Ioc a b = {a} := by rw [← Icc_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 $ left_mem_Icc.2 h)] @[simp] lemma Icc_diff_Ioo_same (h : a ≤ b) : Icc a b \ Ioo a b = {a, b} := by { rw [← Icc_diff_both, diff_diff_cancel_left], simp [insert_subset, h] } @[simp] lemma Ici_diff_Ioi_same : Ici a \ Ioi a = {a} := by rw [← Ici_diff_left, diff_diff_cancel_left (singleton_subset_iff.2 left_mem_Ici)] @[simp] lemma Iic_diff_Iio_same : Iic a \ Iio a = {a} := by rw [← Iic_diff_right, diff_diff_cancel_left (singleton_subset_iff.2 right_mem_Iic)] @[simp] lemma Ioi_union_left : Ioi a ∪ {a} = Ici a := ext $ λ x, by simp [eq_comm, le_iff_eq_or_lt] @[simp] lemma Iio_union_right : Iio a ∪ {a} = Iic a := ext $ λ x, le_iff_lt_or_eq.symm lemma Ioo_union_left (hab : a < b) : Ioo a b ∪ {a} = Ico a b := by rw [← Ico_diff_left, diff_union_self, union_eq_self_of_subset_right (singleton_subset_iff.2 $ left_mem_Ico.2 hab)] lemma Ioo_union_right (hab : a < b) : Ioo a b ∪ {b} = Ioc a b := by simpa only [dual_Ioo, dual_Ico] using @Ioo_union_left (order_dual α) _ b a hab lemma Ioc_union_left (hab : a ≤ b) : Ioc a b ∪ {a} = Icc a b := by rw [← Icc_diff_left, diff_union_self, union_eq_self_of_subset_right (singleton_subset_iff.2 $ left_mem_Icc.2 hab)] lemma Ico_union_right (hab : a ≤ b) : Ico a b ∪ {b} = Icc a b := by simpa only [dual_Ioc, dual_Icc] using @Ioc_union_left (order_dual α) _ b a hab lemma mem_Ici_Ioi_of_subset_of_subset {s : set α} (ho : Ioi a ⊆ s) (hc : s ⊆ Ici a) : s ∈ ({Ici a, Ioi a} : set (set α)) := classical.by_cases (λ h : a ∈ s, or.inl $ subset.antisymm hc $ by rw [← Ioi_union_left, union_subset_iff]; simp *) (λ h, or.inr $ subset.antisymm (λ x hx, lt_of_le_of_ne (hc hx) (λ heq, h $ heq.symm ▸ hx)) ho) lemma mem_Iic_Iio_of_subset_of_subset {s : set α} (ho : Iio a ⊆ s) (hc : s ⊆ Iic a) : s ∈ ({Iic a, Iio a} : set (set α)) := @mem_Ici_Ioi_of_subset_of_subset (order_dual α) _ a s ho hc lemma mem_Icc_Ico_Ioc_Ioo_of_subset_of_subset {s : set α} (ho : Ioo a b ⊆ s) (hc : s ⊆ Icc a b) : s ∈ ({Icc a b, Ico a b, Ioc a b, Ioo a b} : set (set α)) := begin classical, by_cases ha : a ∈ s; by_cases hb : b ∈ s, { refine or.inl (subset.antisymm hc _), rwa [← Ico_diff_left, diff_singleton_subset_iff, insert_eq_of_mem ha, ← Icc_diff_right, diff_singleton_subset_iff, insert_eq_of_mem hb] at ho }, { refine (or.inr $ or.inl $ subset.antisymm _ _), { rw [← Icc_diff_right], exact subset_diff_singleton hc hb }, { rwa [← Ico_diff_left, diff_singleton_subset_iff, insert_eq_of_mem ha] at ho } }, { refine (or.inr $ or.inr $ or.inl $ subset.antisymm _ _), { rw [← Icc_diff_left], exact subset_diff_singleton hc ha }, { rwa [← Ioc_diff_right, diff_singleton_subset_iff, insert_eq_of_mem hb] at ho } }, { refine (or.inr $ or.inr $ or.inr $ subset.antisymm _ ho), rw [← Ico_diff_left, ← Icc_diff_right], apply_rules [subset_diff_singleton] } end lemma mem_Ioo_or_eq_endpoints_of_mem_Icc {x : α} (hmem : x ∈ Icc a b) : x = a ∨ x = b ∨ x ∈ Ioo a b := begin rw [mem_Icc, le_iff_lt_or_eq, le_iff_lt_or_eq] at hmem, rcases hmem with ⟨hxa | hxa, hxb | hxb⟩, { exact or.inr (or.inr ⟨hxa, hxb⟩) }, { exact or.inr (or.inl hxb) }, all_goals { exact or.inl hxa.symm } end lemma mem_Ioo_or_eq_left_of_mem_Ico {x : α} (hmem : x ∈ Ico a b) : x = a ∨ x ∈ Ioo a b := begin rw [mem_Ico, le_iff_lt_or_eq] at hmem, rcases hmem with ⟨hxa | hxa, hxb⟩, { exact or.inr ⟨hxa, hxb⟩ }, { exact or.inl hxa.symm } end lemma mem_Ioo_or_eq_right_of_mem_Ioc {x : α} (hmem : x ∈ Ioc a b) : x = b ∨ x ∈ Ioo a b := begin have := @mem_Ioo_or_eq_left_of_mem_Ico (order_dual α) _ b a x, rw [dual_Ioo, dual_Ico] at this, exact this hmem end lemma Ici_singleton_of_top {a : α} (h_top : ∀ x, x ≤ a) : Ici a = {a} := begin ext, exact ⟨λ h, le_antisymm (h_top _) h, λ h, le_of_eq h.symm⟩, end lemma Iic_singleton_of_bot {a : α} (h_bot : ∀ x, a ≤ x) : Iic a = {a} := @Ici_singleton_of_top (order_dual α) _ a h_bot end partial_order section order_top variables {α : Type u} [order_top α] {a : α} @[simp] lemma Ici_top : Ici (⊤ : α) = {⊤} := Ici_singleton_of_top (λ _, le_top) @[simp] lemma Iic_top : Iic (⊤ : α) = univ := eq_univ_of_forall $ λ x, le_top @[simp] lemma Icc_top : Icc a ⊤ = Ici a := by simp [← Ici_inter_Iic] @[simp] lemma Ioc_top : Ioc a ⊤ = Ioi a := by simp [← Ioi_inter_Iic] end order_top section order_bot variables {α : Type u} [order_bot α] {a : α} @[simp] lemma Iic_bot : Iic (⊥ : α) = {⊥} := Iic_singleton_of_bot (λ _, bot_le) @[simp] lemma Ici_bot : Ici (⊥ : α) = univ := @Iic_top (order_dual α) _ @[simp] lemma Icc_bot : Icc ⊥ a = Iic a := by simp [← Ici_inter_Iic] @[simp] lemma Ico_bot : Ico ⊥ a = Iio a := by simp [← Ici_inter_Iio] end order_bot section linear_order variables {α : Type u} [linear_order α] {a a₁ a₂ b b₁ b₂ : α} @[simp] lemma compl_Iic : (Iic a)ᶜ = Ioi a := ext $ λ _, not_le @[simp] lemma compl_Ici : (Ici a)ᶜ = Iio a := ext $ λ _, not_le @[simp] lemma compl_Iio : (Iio a)ᶜ = Ici a := ext $ λ _, not_lt @[simp] lemma compl_Ioi : (Ioi a)ᶜ = Iic a := ext $ λ _, not_lt @[simp] lemma Ici_diff_Ici : Ici a \ Ici b = Ico a b := by rw [diff_eq, compl_Ici, Ici_inter_Iio] @[simp] lemma Ici_diff_Ioi : Ici a \ Ioi b = Icc a b := by rw [diff_eq, compl_Ioi, Ici_inter_Iic] @[simp] lemma Ioi_diff_Ioi : Ioi a \ Ioi b = Ioc a b := by rw [diff_eq, compl_Ioi, Ioi_inter_Iic] @[simp] lemma Ioi_diff_Ici : Ioi a \ Ici b = Ioo a b := by rw [diff_eq, compl_Ici, Ioi_inter_Iio] @[simp] lemma Iic_diff_Iic : Iic b \ Iic a = Ioc a b := by rw [diff_eq, compl_Iic, inter_comm, Ioi_inter_Iic] @[simp] lemma Iio_diff_Iic : Iio b \ Iic a = Ioo a b := by rw [diff_eq, compl_Iic, inter_comm, Ioi_inter_Iio] @[simp] lemma Iic_diff_Iio : Iic b \ Iio a = Icc a b := by rw [diff_eq, compl_Iio, inter_comm, Ici_inter_Iic] @[simp] lemma Iio_diff_Iio : Iio b \ Iio a = Ico a b := by rw [diff_eq, compl_Iio, inter_comm, Ici_inter_Iio] lemma Ioo_eq_empty_iff [densely_ordered α] : Ioo a b = ∅ ↔ b ≤ a := ⟨λ eq, le_of_not_lt $ λ h, let ⟨x, h₁, h₂⟩ := exists_between h in eq_empty_iff_forall_not_mem.1 eq x ⟨h₁, h₂⟩, Ioo_eq_empty⟩ lemma Ico_eq_empty_iff : Ico a b = ∅ ↔ b ≤ a := ⟨λ eq, le_of_not_lt $ λ h, eq_empty_iff_forall_not_mem.1 eq a ⟨le_refl _, h⟩, Ico_eq_empty⟩ lemma Icc_eq_empty_iff : Icc a b = ∅ ↔ b < a := ⟨λ eq, lt_of_not_ge $ λ h, eq_empty_iff_forall_not_mem.1 eq a ⟨le_refl _, h⟩, Icc_eq_empty⟩ lemma Ico_subset_Ico_iff (h₁ : a₁ < b₁) : Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, have a₂ ≤ a₁ ∧ a₁ < b₂ := h ⟨le_refl _, h₁⟩, ⟨this.1, le_of_not_lt $ λ h', lt_irrefl b₂ (h ⟨le_of_lt this.2, h'⟩).2⟩, λ ⟨h₁, h₂⟩, Ico_subset_Ico h₁ h₂⟩ lemma Ioo_subset_Ioo_iff [densely_ordered α] (h₁ : a₁ < b₁) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := ⟨λ h, begin rcases exists_between h₁ with ⟨x, xa, xb⟩, split; refine le_of_not_lt (λ h', _), { have ab := lt_trans (h ⟨xa, xb⟩).1 xb, exact lt_irrefl _ (h ⟨h', ab⟩).1 }, { have ab := lt_trans xa (h ⟨xa, xb⟩).2, exact lt_irrefl _ (h ⟨ab, h'⟩).2 } end, λ ⟨h₁, h₂⟩, Ioo_subset_Ioo h₁ h₂⟩ lemma Ico_eq_Ico_iff (h : a₁ < b₁ ∨ a₂ < b₂) : Ico a₁ b₁ = Ico a₂ b₂ ↔ a₁ = a₂ ∧ b₁ = b₂ := ⟨λ e, begin simp [subset.antisymm_iff] at e, simp [le_antisymm_iff], cases h; simp [Ico_subset_Ico_iff h] at e; [ rcases e with ⟨⟨h₁, h₂⟩, e'⟩, rcases e with ⟨e', ⟨h₁, h₂⟩⟩ ]; have := (Ico_subset_Ico_iff (lt_of_le_of_lt h₁ $ lt_of_lt_of_le h h₂)).1 e'; tauto end, λ ⟨h₁, h₂⟩, by rw [h₁, h₂]⟩ open_locale classical @[simp] lemma Ioi_subset_Ioi_iff : Ioi b ⊆ Ioi a ↔ a ≤ b := begin refine ⟨λh, _, λh, Ioi_subset_Ioi h⟩, by_contradiction ba, exact lt_irrefl _ (h (not_le.mp ba)) end @[simp] lemma Ioi_subset_Ici_iff [densely_ordered α] : Ioi b ⊆ Ici a ↔ a ≤ b := begin refine ⟨λh, _, λh, Ioi_subset_Ici h⟩, by_contradiction ba, obtain ⟨c, bc, ca⟩ : ∃c, b < c ∧ c < a := exists_between (not_le.mp ba), exact lt_irrefl _ (lt_of_lt_of_le ca (h bc)) end @[simp] lemma Iio_subset_Iio_iff : Iio a ⊆ Iio b ↔ a ≤ b := begin refine ⟨λh, _, λh, Iio_subset_Iio h⟩, by_contradiction ab, exact lt_irrefl _ (h (not_le.mp ab)) end @[simp] lemma Iio_subset_Iic_iff [densely_ordered α] : Iio a ⊆ Iic b ↔ a ≤ b := by rw [← diff_eq_empty, Iio_diff_Iic, Ioo_eq_empty_iff] /-! ### Unions of adjacent intervals -/ /-! #### Two infinite intervals -/ @[simp] lemma Iic_union_Ici : Iic a ∪ Ici a = univ := eq_univ_of_forall (λ x, le_total x a) @[simp] lemma Iio_union_Ici : Iio a ∪ Ici a = univ := eq_univ_of_forall (λ x, lt_or_le x a) @[simp] lemma Iic_union_Ioi : Iic a ∪ Ioi a = univ := eq_univ_of_forall (λ x, le_or_lt x a) /-! #### A finite and an infinite interval -/ lemma Ioi_subset_Ioo_union_Ici : Ioi a ⊆ Ioo a b ∪ Ici b := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb) @[simp] lemma Ioo_union_Ici_eq_Ioi (h : a < b) : Ioo a b ∪ Ici b = Ioi a := subset.antisymm (λ x hx, hx.elim and.left (lt_of_lt_of_le h)) Ioi_subset_Ioo_union_Ici lemma Ici_subset_Ico_union_Ici : Ici a ⊆ Ico a b ∪ Ici b := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb) @[simp] lemma Ico_union_Ici_eq_Ici (h : a ≤ b) : Ico a b ∪ Ici b = Ici a := subset.antisymm (λ x hx, hx.elim and.left (le_trans h)) Ici_subset_Ico_union_Ici lemma Ioi_subset_Ioc_union_Ioi : Ioi a ⊆ Ioc a b ∪ Ioi b := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb) @[simp] lemma Ioc_union_Ioi_eq_Ioi (h : a ≤ b) : Ioc a b ∪ Ioi b = Ioi a := subset.antisymm (λ x hx, hx.elim and.left (lt_of_le_of_lt h)) Ioi_subset_Ioc_union_Ioi lemma Ici_subset_Icc_union_Ioi : Ici a ⊆ Icc a b ∪ Ioi b := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx, hxb⟩) (λ hxb, or.inr hxb) @[simp] lemma Icc_union_Ioi_eq_Ici (h : a ≤ b) : Icc a b ∪ Ioi b = Ici a := subset.antisymm (λ x hx, hx.elim and.left (λ hx, le_trans h (le_of_lt hx))) Ici_subset_Icc_union_Ioi lemma Ioi_subset_Ioc_union_Ici : Ioi a ⊆ Ioc a b ∪ Ici b := subset.trans Ioi_subset_Ioo_union_Ici (union_subset_union_left _ Ioo_subset_Ioc_self) @[simp] lemma Ioc_union_Ici_eq_Ioi (h : a < b) : Ioc a b ∪ Ici b = Ioi a := subset.antisymm (λ x hx, hx.elim and.left (lt_of_lt_of_le h)) Ioi_subset_Ioc_union_Ici lemma Ici_subset_Icc_union_Ici : Ici a ⊆ Icc a b ∪ Ici b := subset.trans Ici_subset_Ico_union_Ici (union_subset_union_left _ Ico_subset_Icc_self) @[simp] lemma Icc_union_Ici_eq_Ici (h : a ≤ b) : Icc a b ∪ Ici b = Ici a := subset.antisymm (λ x hx, hx.elim and.left (le_trans h)) Ici_subset_Icc_union_Ici /-! #### An infinite and a finite interval -/ lemma Iic_subset_Iio_union_Icc : Iic b ⊆ Iio a ∪ Icc a b := λ x hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩) @[simp] lemma Iio_union_Icc_eq_Iic (h : a ≤ b) : Iio a ∪ Icc a b = Iic b := subset.antisymm (λ x hx, hx.elim (λ hx, le_trans (le_of_lt hx) h) and.right) Iic_subset_Iio_union_Icc lemma Iio_subset_Iio_union_Ico : Iio b ⊆ Iio a ∪ Ico a b := λ x hx, (lt_or_le x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩) @[simp] lemma Iio_union_Ico_eq_Iio (h : a ≤ b) : Iio a ∪ Ico a b = Iio b := subset.antisymm (λ x hx, hx.elim (λ hx, lt_of_lt_of_le hx h) and.right) Iio_subset_Iio_union_Ico lemma Iic_subset_Iic_union_Ioc : Iic b ⊆ Iic a ∪ Ioc a b := λ x hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩) @[simp] lemma Iic_union_Ioc_eq_Iic (h : a ≤ b) : Iic a ∪ Ioc a b = Iic b := subset.antisymm (λ x hx, hx.elim (λ hx, le_trans hx h) and.right) Iic_subset_Iic_union_Ioc lemma Iio_subset_Iic_union_Ioo : Iio b ⊆ Iic a ∪ Ioo a b := λ x hx, (le_or_lt x a).elim (λ hxa, or.inl hxa) (λ hxa, or.inr ⟨hxa, hx⟩) @[simp] lemma Iic_union_Ioo_eq_Iio (h : a < b) : Iic a ∪ Ioo a b = Iio b := subset.antisymm (λ x hx, hx.elim (λ hx, lt_of_le_of_lt hx h) and.right) Iio_subset_Iic_union_Ioo lemma Iic_subset_Iic_union_Icc : Iic b ⊆ Iic a ∪ Icc a b := subset.trans Iic_subset_Iic_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self) @[simp] lemma Iic_union_Icc_eq_Iic (h : a ≤ b) : Iic a ∪ Icc a b = Iic b := subset.antisymm (λ x hx, hx.elim (λ hx, le_trans hx h) and.right) Iic_subset_Iic_union_Icc lemma Iio_subset_Iic_union_Ico : Iio b ⊆ Iic a ∪ Ico a b := subset.trans Iio_subset_Iic_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self) @[simp] lemma Iic_union_Ico_eq_Iio (h : a < b) : Iic a ∪ Ico a b = Iio b := subset.antisymm (λ x hx, hx.elim (λ hx, lt_of_le_of_lt hx h) and.right) Iio_subset_Iic_union_Ico /-! #### Two finite intervals, `I?o` and `Ic?` -/ variable {c : α} lemma Ioo_subset_Ioo_union_Ico : Ioo a c ⊆ Ioo a b ∪ Ico b c := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ioo_union_Ico_eq_Ioo (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Ico b c = Ioo a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, lt_of_lt_of_le hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩)) Ioo_subset_Ioo_union_Ico lemma Ico_subset_Ico_union_Ico : Ico a c ⊆ Ico a b ∪ Ico b c := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ico_union_Ico_eq_Ico (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Ico b c = Ico a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, lt_of_lt_of_le hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩)) Ico_subset_Ico_union_Ico lemma Icc_subset_Ico_union_Icc : Icc a c ⊆ Ico a b ∪ Icc b c := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ico_union_Icc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Ico a b ∪ Icc b c = Icc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, le_trans (le_of_lt hx.2) h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩)) Icc_subset_Ico_union_Icc lemma Ioc_subset_Ioo_union_Icc : Ioc a c ⊆ Ioo a b ∪ Icc b c := λ x hx, (lt_or_le x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ioo_union_Icc_eq_Ioc (h₁ : a < b) (h₂ : b ≤ c) : Ioo a b ∪ Icc b c = Ioc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, le_trans (le_of_lt hx.2) h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩)) Ioc_subset_Ioo_union_Icc /-! #### Two finite intervals, `I?c` and `Io?` -/ lemma Ioo_subset_Ioc_union_Ioo : Ioo a c ⊆ Ioc a b ∪ Ioo b c := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ioc_union_Ioo_eq_Ioo (h₁ : a ≤ b) (h₂ : b < c) : Ioc a b ∪ Ioo b c = Ioo a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨lt_of_le_of_lt h₁ hx.1, hx.2⟩)) Ioo_subset_Ioc_union_Ioo lemma Ico_subset_Icc_union_Ioo : Ico a c ⊆ Icc a b ∪ Ioo b c := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Icc_union_Ioo_eq_Ico (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ioo b c = Ico a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ (le_of_lt hx.1), hx.2⟩)) Ico_subset_Icc_union_Ioo lemma Icc_subset_Icc_union_Ioc : Icc a c ⊆ Icc a b ∪ Ioc b c := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Icc_union_Ioc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Ioc b c = Icc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ (le_of_lt hx.1), hx.2⟩)) Icc_subset_Icc_union_Ioc lemma Ioc_subset_Ioc_union_Ioc : Ioc a c ⊆ Ioc a b ∪ Ioc b c := λ x hx, (le_or_lt x b).elim (λ hxb, or.inl ⟨hx.1, hxb⟩) (λ hxb, or.inr ⟨hxb, hx.2⟩) @[simp] lemma Ioc_union_Ioc_eq_Ioc (h₁ : a ≤ b) (h₂ : b ≤ c) : Ioc a b ∪ Ioc b c = Ioc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨lt_of_le_of_lt h₁ hx.1, hx.2⟩)) Ioc_subset_Ioc_union_Ioc /-! #### Two finite intervals with a common point -/ lemma Ioo_subset_Ioc_union_Ico : Ioo a c ⊆ Ioc a b ∪ Ico b c := subset.trans Ioo_subset_Ioc_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self) @[simp] lemma Ioc_union_Ico_eq_Ioo (h₁ : a < b) (h₂ : b < c) : Ioc a b ∪ Ico b c = Ioo a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩)) Ioo_subset_Ioc_union_Ico lemma Ico_subset_Icc_union_Ico : Ico a c ⊆ Icc a b ∪ Ico b c := subset.trans Ico_subset_Icc_union_Ioo (union_subset_union_right _ Ioo_subset_Ico_self) @[simp] lemma Icc_union_Ico_eq_Ico (h₁ : a ≤ b) (h₂ : b < c) : Icc a b ∪ Ico b c = Ico a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, lt_of_le_of_lt hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩)) Ico_subset_Icc_union_Ico lemma Icc_subset_Icc_union_Icc : Icc a c ⊆ Icc a b ∪ Icc b c := subset.trans Icc_subset_Icc_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self) @[simp] lemma Icc_union_Icc_eq_Icc (h₁ : a ≤ b) (h₂ : b ≤ c) : Icc a b ∪ Icc b c = Icc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨le_trans h₁ hx.1, hx.2⟩)) Icc_subset_Icc_union_Icc lemma Ioc_subset_Ioc_union_Icc : Ioc a c ⊆ Ioc a b ∪ Icc b c := subset.trans Ioc_subset_Ioc_union_Ioc (union_subset_union_right _ Ioc_subset_Icc_self) @[simp] lemma Ioc_union_Icc_eq_Ioc (h₁ : a < b) (h₂ : b ≤ c) : Ioc a b ∪ Icc b c = Ioc a c := subset.antisymm (λ x hx, hx.elim (λ hx, ⟨hx.1, le_trans hx.2 h₂⟩) (λ hx, ⟨lt_of_lt_of_le h₁ hx.1, hx.2⟩)) Ioc_subset_Ioc_union_Icc end linear_order section lattice section inf variables {α : Type u} [semilattice_inf α] @[simp] lemma Iic_inter_Iic {a b : α} : Iic a ∩ Iic b = Iic (a ⊓ b) := by { ext x, simp [Iic] } @[simp] lemma Iio_inter_Iio [is_total α (≤)] {a b : α} : Iio a ∩ Iio b = Iio (a ⊓ b) := by { ext x, simp [Iio] } end inf section sup variables {α : Type u} [semilattice_sup α] @[simp] lemma Ici_inter_Ici {a b : α} : Ici a ∩ Ici b = Ici (a ⊔ b) := by { ext x, simp [Ici] } @[simp] lemma Ioi_inter_Ioi [is_total α (≤)] {a b : α} : Ioi a ∩ Ioi b = Ioi (a ⊔ b) := by { ext x, simp [Ioi] } end sup section both variables {α : Type u} [lattice α] [ht : is_total α (≤)] {a b c a₁ a₂ b₁ b₂ : α} lemma Icc_inter_Icc : Icc a₁ b₁ ∩ Icc a₂ b₂ = Icc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iic.symm, Ici_inter_Ici.symm, Iic_inter_Iic.symm]; ac_refl @[simp] lemma Icc_inter_Icc_eq_singleton (hab : a ≤ b) (hbc : b ≤ c) : Icc a b ∩ Icc b c = {b} := by rw [Icc_inter_Icc, sup_of_le_right hab, inf_of_le_left hbc, Icc_self] include ht lemma Ico_inter_Ico : Ico a₁ b₁ ∩ Ico a₂ b₂ = Ico (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ici_inter_Iio.symm, Ici_inter_Ici.symm, Iio_inter_Iio.symm]; ac_refl lemma Ioc_inter_Ioc : Ioc a₁ b₁ ∩ Ioc a₂ b₂ = Ioc (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ioi_inter_Iic.symm, Ioi_inter_Ioi.symm, Iic_inter_Iic.symm]; ac_refl lemma Ioo_inter_Ioo : Ioo a₁ b₁ ∩ Ioo a₂ b₂ = Ioo (a₁ ⊔ a₂) (b₁ ⊓ b₂) := by simp only [Ioi_inter_Iio.symm, Ioi_inter_Ioi.symm, Iio_inter_Iio.symm]; ac_refl end both end lattice section linear_order variables {α : Type u} [linear_order α] {a a₁ a₂ b b₁ b₂ c d : α} lemma Ioc_inter_Ioo_of_left_lt (h : b₁ < b₂) : Ioc a₁ b₁ ∩ Ioo a₂ b₂ = Ioc (max a₁ a₂) b₁ := ext $ λ x, by simp [and_assoc, @and.left_comm (x ≤ _), and_iff_left_iff_imp.2 (λ h', lt_of_le_of_lt h' h)] lemma Ioc_inter_Ioo_of_right_le (h : b₂ ≤ b₁) : Ioc a₁ b₁ ∩ Ioo a₂ b₂ = Ioo (max a₁ a₂) b₂ := ext $ λ x, by simp [and_assoc, @and.left_comm (x ≤ _), and_iff_right_iff_imp.2 (λ h', (le_trans (le_of_lt h') h))] lemma Ioo_inter_Ioc_of_left_le (h : b₁ ≤ b₂) : Ioo a₁ b₁ ∩ Ioc a₂ b₂ = Ioo (max a₁ a₂) b₁ := by rw [inter_comm, Ioc_inter_Ioo_of_right_le h, max_comm] lemma Ioo_inter_Ioc_of_right_lt (h : b₂ < b₁) : Ioo a₁ b₁ ∩ Ioc a₂ b₂ = Ioc (max a₁ a₂) b₂ := by rw [inter_comm, Ioc_inter_Ioo_of_left_lt h, max_comm] @[simp] lemma Ico_diff_Iio : Ico a b \ Iio c = Ico (max a c) b := ext $ by simp [Ico, Iio, iff_def, max_le_iff] {contextual:=tt} @[simp] lemma Ico_inter_Iio : Ico a b ∩ Iio c = Ico a (min b c) := ext $ by simp [Ico, Iio, iff_def, lt_min_iff] {contextual:=tt} lemma Ioc_union_Ioc (h₁ : min a b ≤ max c d) (h₂ : min c d ≤ max a b) : Ioc a b ∪ Ioc c d = Ioc (min a c) (max b d) := begin cases le_total a b with hab hab; cases le_total c d with hcd hcd; simp [hab, hcd] at h₁ h₂, { ext x, simp [iff_def, and_imp, or_imp_distrib, (h₂.lt_or_le x).symm, h₁.lt_or_le] { contextual := tt } }, all_goals { simp [*] } end @[simp] lemma Ioc_union_Ioc_right : Ioc a b ∪ Ioc a c = Ioc a (max b c) := by rw [Ioc_union_Ioc, min_self]; exact (min_le_left _ _).trans (le_max_left _ _) @[simp] lemma Ioc_union_Ioc_left : Ioc a c ∪ Ioc b c = Ioc (min a b) c := by rw [Ioc_union_Ioc, max_self]; exact (min_le_right _ _).trans (le_max_right _ _) @[simp] lemma Ioc_union_Ioc_symm : Ioc a b ∪ Ioc b a = Ioc (min a b) (max a b) := by { rw max_comm, apply Ioc_union_Ioc; rw max_comm; exact min_le_max } @[simp] lemma Ioc_union_Ioc_union_Ioc_cycle : Ioc a b ∪ Ioc b c ∪ Ioc c a = Ioc (min a (min b c)) (max a (max b c)) := begin rw [Ioc_union_Ioc, Ioc_union_Ioc], ac_refl, all_goals { solve_by_elim [min_le_left_of_le, min_le_right_of_le, le_max_left_of_le, le_max_right_of_le, le_refl] { max_depth := 5 }} end end linear_order section linear_ordered_add_comm_group variables {α : Type u} [linear_ordered_add_comm_group α] /-- If we remove a smaller interval from a larger, the result is nonempty -/ lemma nonempty_Ico_sdiff {x dx y dy : α} (h : dy < dx) (hx : 0 < dx) : nonempty ↥(Ico x (x + dx) \ Ico y (y + dy)) := begin cases lt_or_le x y with h' h', { use x, simp* }, { use max x (x + dy), simp [*, le_refl] } end end linear_ordered_add_comm_group end set
6a1b46b9b2fbc5c57d270e84cb3adbdb826535e6
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/data/rbtree/main.lean
4a4c1e471b5c8be95f7710beed6ecbaf5c50f283
[]
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
9,517
lean
/- Copyright (c) 2017 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.default import Mathlib.Lean3Lib.data.rbtree.find import Mathlib.Lean3Lib.data.rbtree.insert import Mathlib.Lean3Lib.data.rbtree.min_max universes u namespace Mathlib namespace rbnode theorem is_searchable_of_well_formed {α : Type u} {lt : α → α → Prop} {t : rbnode α} [is_strict_weak_order α lt] : well_formed lt t → is_searchable lt t none none := sorry theorem is_red_black_of_well_formed {α : Type u} {lt : α → α → Prop} {t : rbnode α} : well_formed lt t → ∃ (c : color), ∃ (n : ℕ), is_red_black t c n := sorry end rbnode namespace rbtree theorem balanced {α : Type u} {lt : α → α → Prop} (t : rbtree α) : bit0 1 * depth min t + 1 ≥ depth max t := sorry theorem not_mem_mk_rbtree {α : Type u} {lt : α → α → Prop} (a : α) : ¬a ∈ mk_rbtree α := sorry theorem not_mem_of_empty {α : Type u} {lt : α → α → Prop} {t : rbtree α} (a : α) : empty t = tt → ¬a ∈ t := sorry theorem mem_of_mem_of_eqv {α : Type u} {lt : α → α → Prop} [is_strict_weak_order α lt] {t : rbtree α} {a : α} {b : α} : a ∈ t → strict_weak_order.equiv a b → b ∈ t := sorry theorem insert_ne_mk_rbtree {α : Type u} {lt : α → α → Prop} [DecidableRel lt] (t : rbtree α) (a : α) : insert t a ≠ mk_rbtree α := sorry theorem find_correct {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] (a : α) (t : rbtree α) : a ∈ t ↔ ∃ (b : α), find t a = some b ∧ strict_weak_order.equiv a b := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.find_correct (rbnode.is_searchable_of_well_formed t_property) theorem find_correct_of_total {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_total_order α lt] (a : α) (t : rbtree α) : a ∈ t ↔ find t a = some a := sorry theorem find_correct_exact {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_total_order α lt] (a : α) (t : rbtree α) : mem_exact a t ↔ find t a = some a := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.find_correct_exact (rbnode.is_searchable_of_well_formed t_property) theorem find_insert_of_eqv {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] (t : rbtree α) {x : α} {y : α} : strict_weak_order.equiv x y → find (insert t x) y = some x := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) (h : strict_weak_order.equiv x y) => rbnode.find_insert_of_eqv lt h (rbnode.is_searchable_of_well_formed t_property) theorem find_insert {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] (t : rbtree α) (x : α) : find (insert t x) x = some x := find_insert_of_eqv t (refl x) theorem find_insert_of_disj {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} (t : rbtree α) : lt x y ∨ lt y x → find (insert t x) y = find t y := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) (h : lt x y ∨ lt y x) => rbnode.find_insert_of_disj lt h (rbnode.is_searchable_of_well_formed t_property) theorem find_insert_of_not_eqv {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} (t : rbtree α) : ¬strict_weak_order.equiv x y → find (insert t x) y = find t y := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) (h : ¬strict_weak_order.equiv x y) => rbnode.find_insert_of_not_eqv lt h (rbnode.is_searchable_of_well_formed t_property) theorem find_insert_of_ne {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_total_order α lt] {x : α} {y : α} (t : rbtree α) : x ≠ y → find (insert t x) y = find t y := sorry theorem not_mem_of_find_none {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {t : rbtree α} : find t a = none → ¬a ∈ t := sorry theorem eqv_of_find_some {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {b : α} {t : rbtree α} : find t a = some b → strict_weak_order.equiv a b := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.eqv_of_find_some (rbnode.is_searchable_of_well_formed t_property) theorem eq_of_find_some {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_total_order α lt] {a : α} {b : α} {t : rbtree α} : find t a = some b → a = b := fun (h : find t a = some b) => (fun (this : strict_weak_order.equiv a b) => eq_of_eqv_lt this) (eqv_of_find_some h) theorem mem_of_find_some {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {b : α} {t : rbtree α} : find t a = some b → a ∈ t := fun (h : find t a = some b) => iff.mpr (find_correct a t) (Exists.intro b { left := h, right := eqv_of_find_some h }) theorem find_eq_find_of_eqv {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {b : α} (t : rbtree α) : strict_weak_order.equiv a b → find t a = find t b := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.find_eq_find_of_eqv (rbnode.is_searchable_of_well_formed t_property) theorem contains_correct {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] (a : α) (t : rbtree α) : a ∈ t ↔ contains t a = tt := sorry theorem mem_insert_of_incomp {α : Type u} {lt : α → α → Prop} [DecidableRel lt] {a : α} {b : α} (t : rbtree α) : ¬lt a b ∧ ¬lt b a → a ∈ insert t b := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.mem_insert_of_incomp lt t_val theorem mem_insert {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_irrefl α lt] (a : α) (t : rbtree α) : a ∈ insert t a := mem_insert_of_incomp t { left := irrefl_of lt a, right := irrefl_of lt a } theorem mem_insert_of_equiv {α : Type u} {lt : α → α → Prop} [DecidableRel lt] {a : α} {b : α} (t : rbtree α) : strict_weak_order.equiv a b → a ∈ insert t b := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.mem_insert_of_incomp lt t_val theorem mem_insert_of_mem {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {t : rbtree α} (b : α) : a ∈ t → a ∈ insert t b := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.mem_insert_of_mem lt b theorem equiv_or_mem_of_mem_insert {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {b : α} {t : rbtree α} : a ∈ insert t b → strict_weak_order.equiv a b ∨ a ∈ t := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.equiv_or_mem_of_mem_insert lt theorem incomp_or_mem_of_mem_ins {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {b : α} {t : rbtree α} : a ∈ insert t b → ¬lt a b ∧ ¬lt b a ∨ a ∈ t := equiv_or_mem_of_mem_insert theorem eq_or_mem_of_mem_ins {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_total_order α lt] {a : α} {b : α} {t : rbtree α} : a ∈ insert t b → a = b ∨ a ∈ t := sorry theorem mem_of_min_eq {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_irrefl α lt] {a : α} {t : rbtree α} : rbtree.min t = some a → a ∈ t := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.mem_of_min_eq lt theorem mem_of_max_eq {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_irrefl α lt] {a : α} {t : rbtree α} : rbtree.max t = some a → a ∈ t := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.mem_of_max_eq lt theorem eq_leaf_of_min_eq_none {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {t : rbtree α} : rbtree.min t = none → t = mk_rbtree α := sorry theorem eq_leaf_of_max_eq_none {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {t : rbtree α} : rbtree.max t = none → t = mk_rbtree α := sorry theorem min_is_minimal {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {t : rbtree α} : rbtree.min t = some a → ∀ {b : α}, b ∈ t → strict_weak_order.equiv a b ∨ lt a b := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.min_is_minimal (rbnode.is_searchable_of_well_formed t_property) theorem max_is_maximal {α : Type u} {lt : α → α → Prop} [DecidableRel lt] [is_strict_weak_order α lt] {a : α} {t : rbtree α} : rbtree.max t = some a → ∀ {b : α}, b ∈ t → strict_weak_order.equiv a b ∨ lt b a := subtype.cases_on t fun (t_val : rbnode α) (t_property : rbnode.well_formed lt t_val) => rbnode.max_is_maximal (rbnode.is_searchable_of_well_formed t_property)
2c93abef48c1bd89b1e6c7b21c4ca83511f028d5
94e33a31faa76775069b071adea97e86e218a8ee
/src/algebraic_topology/dold_kan/homotopies.lean
cd8904d0302b505950d13c35f6adf3c6526329a2
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
7,876
lean
/- Copyright (c) 2022 Joël Riou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joël Riou -/ import algebra.homology.homotopy import algebraic_topology.dold_kan.notations /-! # Construction of homotopies for the Dold-Kan correspondence TODO (@joelriou) continue adding the various files references below (The general strategy of proof of the Dold-Kan correspondence is explained in `equivalence.lean`.) The purpose of the files `homotopies.lean`, `faces.lean`, `projections.lean` and `p_infty.lean` is to construct an idempotent endomorphism `P_infty : K[X] ⟶ K[X]` of the alternating face map complex for each `X : simplicial_object C` when `C` is a preadditive category. In the case `C` is abelian, this `P_infty` shall be the projection on the normalized Moore subcomplex of `K[X]` associated to the decomposition of the complex `K[X]` as a direct sum of this normalized subcomplex and of the degenerate subcomplex. In `p_infty.lean`, this endomorphism `P_infty` shall be obtained by passing to the limit idempotent endomorphisms `P q` for all `(q : ℕ)`. These endomorphisms `P q` are defined by induction. The idea is to start from the identity endomorphism `P 0` of `K[X]` and to ensure by induction that the `q` higher face maps (except $d_0$) vanish on the image of `P q`. Then, in a certain degree `n`, the image of `P q` for a big enough `q` will be contained in the normalized subcomplex. This construction is done in `projections.lean`. It would be easy to define the `P q` degreewise (similarly as it is done in *Simplicial Homotopy Theory* by Goerrs-Jardine p. 149), but then we would have to prove that they are compatible with the differential (i.e. they are chain complex maps), and also that they are homotopic to the identity. These two verifications are quite technical. In order to reduce the number of such technical lemmas, the strategy that is followed here is to define a series of null homotopic maps `Hσ q` (attached to families of maps `hσ`) and use these in order to construct `P q` : the endomorphisms `P q` shall basically be obtained by altering the identity endomorphism by adding null homotopic maps, so that we get for free that they are morphisms of chain complexes and that they are homotopic to the identity. The most technical verifications that are needed about the null homotopic maps `Hσ` are obtained in `faces.lean`. In this file `homotopies.lean`, we define the null homotopic maps `Hσ q : K[X] ⟶ K[X]`, show that they are natural (see `nat_trans_Hσ`) and compatible the application of additive functors (see `map_Hσ`). ## References * [Albrecht Dold, *Homology of Symmetric Products and Other Functors of Complexes*][dold1958] * [Paul G. Goerss, John F. Jardine, *Simplical Homotopy Theory*][goerss-jardine-2009] -/ open category_theory open category_theory.category open category_theory.limits open category_theory.preadditive open category_theory.simplicial_object open homotopy open opposite open_locale simplicial dold_kan noncomputable theory namespace algebraic_topology namespace dold_kan variables {C : Type*} [category C] [preadditive C] variables {X : simplicial_object C} /-- As we are using chain complexes indexed by `ℕ`, we shall need the relation `c` such `c m n` if and only if `n+1=m`. -/ abbreviation c := complex_shape.down ℕ /-- Helper when we need some `c.rel i j` (i.e. `complex_shape.down ℕ`), e.g. `c_mk n (n+1) rfl` -/ lemma c_mk (i j : ℕ) (h : j+1 = i) : c.rel i j := complex_shape.down_mk i j h /-- This lemma is meant to be used with `null_homotopic_map'_f_of_not_rel_left` -/ lemma cs_down_0_not_rel_left (j : ℕ) : ¬c.rel 0 j := begin intro hj, dsimp at hj, apply nat.not_succ_le_zero j, rw [nat.succ_eq_add_one, hj], end /-- The sequence of maps which gives the null homotopic maps `Hσ` that shall be in the inductive construction of the projections `P q : K[X] ⟶ K[X]` -/ def hσ (q : ℕ) (n : ℕ) : X _[n] ⟶ X _[n+1] := if n<q then 0 else (-1 : ℤ)^(n-q) • X.σ ⟨n-q, nat.sub_lt_succ n q⟩ /-- We can turn `hσ` into a datum that can be passed to `null_homotopic_map'`. -/ def hσ' (q : ℕ) : Π n m, c.rel m n → (K[X].X n ⟶ K[X].X m) := λ n m hnm, (hσ q n) ≫ eq_to_hom (by congr') lemma hσ'_eq_zero {q n m : ℕ} (hnq : n<q) (hnm : c.rel m n) : (hσ' q n m hnm : X _[n] ⟶ X _[m])= 0 := by { simp only [hσ', hσ], split_ifs, exact zero_comp, } lemma hσ'_eq {q n a m : ℕ} (ha : n=a+q) (hnm : c.rel m n) : (hσ' q n m hnm : X _[n] ⟶ X _[m]) = ((-1 : ℤ)^a • X.σ ⟨a, nat.lt_succ_iff.mpr (nat.le.intro (eq.symm ha))⟩) ≫ eq_to_hom (by congr') := begin simp only [hσ', hσ], split_ifs, { exfalso, linarith, }, { have h' := tsub_eq_of_eq_add ha, congr', } end /-- The null homotopic map $(hσ q) ∘ d + d ∘ (hσ q)$ -/ def Hσ (q : ℕ) : K[X] ⟶ K[X] := null_homotopic_map' (hσ' q) /-- `Hσ` is null homotopic -/ def homotopy_Hσ_to_zero (q : ℕ) : homotopy (Hσ q : K[X] ⟶ K[X]) 0 := null_homotopy' (hσ' q) /-- In degree `0`, the null homotopic map `Hσ` is zero. -/ lemma Hσ_eq_zero (q : ℕ) : (Hσ q : K[X] ⟶ K[X]).f 0 = 0 := begin unfold Hσ, rw null_homotopic_map'_f_of_not_rel_left (c_mk 1 0 rfl) cs_down_0_not_rel_left, cases q, { rw hσ'_eq (show 0=0+0, by refl) (c_mk 1 0 rfl), simp only [pow_zero, fin.mk_zero, one_zsmul, eq_to_hom_refl, category.comp_id], erw chain_complex.of_d, simp only [alternating_face_map_complex.obj_d, fin.sum_univ_two, fin.coe_zero, pow_zero, one_zsmul, fin.coe_one, pow_one, comp_add, neg_smul, one_zsmul, comp_neg, add_neg_eq_zero], erw [δ_comp_σ_self, δ_comp_σ_succ], }, { rw [hσ'_eq_zero (nat.succ_pos q) (c_mk 1 0 rfl), zero_comp], }, end /-- The maps `hσ' q n m hnm` are natural on the simplicial object -/ lemma hσ'_naturality (q : ℕ) (n m : ℕ) (hnm : c.rel m n) {X Y : simplicial_object C} (f : X ⟶ Y) : f.app (op [n]) ≫ hσ' q n m hnm = hσ' q n m hnm ≫ f.app (op [m]) := begin have h : n+1 = m := hnm, subst h, simp only [hσ', eq_to_hom_refl, comp_id], unfold hσ, split_ifs, { rw [zero_comp, comp_zero], }, { simp only [zsmul_comp, comp_zsmul], erw f.naturality, refl, }, end /-- For each q, `Hσ q` is a natural transformation. -/ def nat_trans_Hσ (q : ℕ) : alternating_face_map_complex C ⟶ alternating_face_map_complex C := { app := λ X, Hσ q, naturality' := λ X Y f, begin unfold Hσ, rw [null_homotopic_map'_comp, comp_null_homotopic_map'], congr, ext n m hnm, simp only [alternating_face_map_complex_map, alternating_face_map_complex.map, chain_complex.of_hom_f, hσ'_naturality], end, } /-- The maps `hσ' q n m hnm` are compatible with the application of additive functors. -/ lemma map_hσ' {D : Type*} [category D] [preadditive D] (G : C ⥤ D) [G.additive] (X : simplicial_object C) (q n m : ℕ) (hnm : c.rel m n) : (hσ' q n m hnm : K[((whiskering _ _).obj G).obj X].X n ⟶ _) = G.map (hσ' q n m hnm : K[X].X n ⟶ _) := begin unfold hσ' hσ, split_ifs, { simp only [functor.map_zero, zero_comp], }, { simpa only [eq_to_hom_map, functor.map_comp, functor.map_zsmul], }, end /-- The null homotopic maps `Hσ` are compatible with the application of additive functors. -/ lemma map_Hσ {D : Type*} [category D] [preadditive D] (G : C ⥤ D) [G.additive] (X : simplicial_object C) (q n : ℕ) : (Hσ q : K[((whiskering C D).obj G).obj X] ⟶ _).f n = G.map ((Hσ q : K[X] ⟶ _).f n) := begin unfold Hσ, have eq := homological_complex.congr_hom (map_null_homotopic_map' G (hσ' q)) n, simp only [functor.map_homological_complex_map_f, ← map_hσ'] at eq, rw eq, let h := (functor.congr_obj (map_alternating_face_map_complex G) X).symm, congr', end end dold_kan end algebraic_topology
5e20307fe1c35af6fdfa58f1da70d917614babae
d1a52c3f208fa42c41df8278c3d280f075eb020c
/stage0/src/Lean/Parser/Command.lean
fec4f33a7c598d9f94da7d16cf1b09ab604f5bc8
[ "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
12,258
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Sebastian Ullrich -/ import Lean.Parser.Term import Lean.Parser.Do namespace Lean namespace Parser /-- Syntax quotation for terms and (lists of) commands. We prefer terms, so ambiguous quotations like `` `($x $y) `` will be parsed as an application, not two commands. Use `` `($x:command $y:command) `` instead. Multiple command will be put in a `` `null `` node, but a single command will not (so that you can directly match against a quotation in a command kind's elaborator). -/ -- TODO: use two separate quotation parsers with parser priorities instead @[builtinTermParser] def Term.quot := leading_parser "`(" >> incQuotDepth (termParser <|> many1Unbox commandParser) >> ")" @[builtinTermParser] def Term.precheckedQuot := leading_parser "`" >> Term.quot namespace Command /-- A mutual block may be broken in different cliques, we identify them using an `ident` (an element of the clique) We provide two kinds of hints to the termination checker: 1- A wellfounded relation (`p` is `termParser`) 2- A tactic for proving the recursive applications are "decreasing" (`p` is `tacticSeq`) -/ def terminationHintMany (p : Parser) := leading_parser atomic (lookahead (ident >> " => ")) >> many1Indent (group (ppLine >> ident >> " => " >> p >> optional ";")) def terminationHint1 (p : Parser) := leading_parser p def terminationHint (p : Parser) := terminationHintMany p <|> terminationHint1 p def terminationBy := leading_parser "termination_by " >> terminationHint termParser def decreasingBy := leading_parser "decreasing_by " >> terminationHint Tactic.tacticSeq def terminationSuffix := optional terminationBy >> optional decreasingBy @[builtinCommandParser] def moduleDoc := leading_parser ppDedent $ "/-!" >> commentBody >> ppLine def namedPrio := leading_parser (atomic ("(" >> nonReservedSymbol "priority") >> " := " >> priorityParser >> ")") def optNamedPrio := optional (ppSpace >> namedPrio) def «private» := leading_parser "private " def «protected» := leading_parser "protected " def visibility := «private» <|> «protected» def «noncomputable» := leading_parser "noncomputable " def «unsafe» := leading_parser "unsafe " def «partial» := leading_parser "partial " def «nonrec» := leading_parser "nonrec " def declModifiers (inline : Bool) := leading_parser optional docComment >> optional (Term.«attributes» >> if inline then skip else ppDedent ppLine) >> optional visibility >> optional «noncomputable» >> optional «unsafe» >> optional («partial» <|> «nonrec») def declId := leading_parser ident >> optional (".{" >> sepBy1 ident ", " >> "}") def declSig := leading_parser many (ppSpace >> (Term.simpleBinderWithoutType <|> Term.bracketedBinder)) >> Term.typeSpec def optDeclSig := leading_parser many (ppSpace >> (Term.simpleBinderWithoutType <|> Term.bracketedBinder)) >> Term.optType def declValSimple := leading_parser " :=" >> ppHardLineUnlessUngrouped >> termParser >> optional Term.whereDecls def declValEqns := leading_parser Term.matchAltsWhereDecls def whereStructField := leading_parser Term.letDecl def whereStructInst := leading_parser " where" >> many1Indent (ppLine >> ppGroup (group (whereStructField >> optional ";"))) /- Remark: we should not use `Term.whereDecls` at `declVal` because `Term.whereDecls` is defined using `Term.letRecDecl` which may contain attributes. Issue #753 showns an example that fails to be parsed when we used `Term.whereDecls`. -/ def declVal := declValSimple <|> declValEqns <|> whereStructInst def «abbrev» := leading_parser "abbrev " >> declId >> ppIndent optDeclSig >> declVal def optDefDeriving := optional (atomic ("deriving " >> notSymbol "instance") >> sepBy1 ident ", ") def «def» := leading_parser "def " >> declId >> ppIndent optDeclSig >> declVal >> optDefDeriving >> terminationSuffix def «theorem» := leading_parser "theorem " >> declId >> ppIndent declSig >> declVal >> terminationSuffix def «constant» := leading_parser "constant " >> declId >> ppIndent declSig >> optional declValSimple /- As `declSig` starts with a space, "instance" does not need a trailing space if we put `ppSpace` in the optional fragments. -/ def «instance» := leading_parser Term.attrKind >> "instance" >> optNamedPrio >> optional (ppSpace >> declId) >> ppIndent declSig >> declVal >> terminationSuffix def «axiom» := leading_parser "axiom " >> declId >> ppIndent declSig /- As `declSig` starts with a space, "example" does not need a trailing space. -/ def «example» := leading_parser "example" >> ppIndent declSig >> declVal def inferMod := leading_parser ppSpace >> atomic (symbol "{" >> "}") def ctor := leading_parser "\n| " >> ppIndent (declModifiers true >> ident >> optional inferMod >> optDeclSig) def derivingClasses := sepBy1 (group (ident >> optional (" with " >> Term.structInst))) ", " def optDeriving := leading_parser optional (ppLine >> atomic ("deriving " >> notSymbol "instance") >> derivingClasses) def «inductive» := leading_parser "inductive " >> declId >> optDeclSig >> optional (symbol " :=" <|> " where") >> many ctor >> optDeriving def classInductive := leading_parser atomic (group (symbol "class " >> "inductive ")) >> declId >> ppIndent optDeclSig >> optional (symbol " :=" <|> " where") >> many ctor >> optDeriving def structExplicitBinder := leading_parser atomic (declModifiers true >> "(") >> many1 ident >> optional inferMod >> ppIndent optDeclSig >> optional (Term.binderTactic <|> Term.binderDefault) >> ")" def structImplicitBinder := leading_parser atomic (declModifiers true >> "{") >> many1 ident >> optional inferMod >> declSig >> "}" def structInstBinder := leading_parser atomic (declModifiers true >> "[") >> many1 ident >> optional inferMod >> declSig >> "]" def structSimpleBinder := leading_parser atomic (declModifiers true >> ident) >> optional inferMod >> optDeclSig >> optional (Term.binderTactic <|> Term.binderDefault) def structFields := leading_parser manyIndent (ppLine >> checkColGe >> ppGroup (structExplicitBinder <|> structImplicitBinder <|> structInstBinder <|> structSimpleBinder)) def structCtor := leading_parser atomic (declModifiers true >> ident >> optional inferMod >> " :: ") def structureTk := leading_parser "structure " def classTk := leading_parser "class " def «extends» := leading_parser " extends " >> sepBy1 termParser ", " def «structure» := leading_parser (structureTk <|> classTk) >> declId >> many (ppSpace >> Term.bracketedBinder) >> optional «extends» >> Term.optType >> optional ((symbol " := " <|> " where ") >> optional structCtor >> structFields) >> optDeriving @[builtinCommandParser] def declaration := leading_parser declModifiers false >> («abbrev» <|> «def» <|> «theorem» <|> «constant» <|> «instance» <|> «axiom» <|> «example» <|> «inductive» <|> classInductive <|> «structure») @[builtinCommandParser] def «deriving» := leading_parser "deriving " >> "instance " >> derivingClasses >> " for " >> sepBy1 ident ", " @[builtinCommandParser] def noncomputableSection := leading_parser "noncomputable " >> "section " >> optional ident @[builtinCommandParser] def «section» := leading_parser "section " >> optional ident @[builtinCommandParser] def «namespace» := leading_parser "namespace " >> ident @[builtinCommandParser] def «end» := leading_parser "end " >> optional ident @[builtinCommandParser] def «variable» := leading_parser "variable" >> many1 (ppSpace >> Term.bracketedBinder) @[builtinCommandParser] def «universe» := leading_parser "universe " >> many1 ident @[builtinCommandParser] def check := leading_parser "#check " >> termParser @[builtinCommandParser] def check_failure := leading_parser "#check_failure " >> termParser -- Like `#check`, but succeeds only if term does not type check @[builtinCommandParser] def reduce := leading_parser "#reduce " >> termParser @[builtinCommandParser] def eval := leading_parser "#eval " >> termParser @[builtinCommandParser] def synth := leading_parser "#synth " >> termParser @[builtinCommandParser] def exit := leading_parser "#exit" @[builtinCommandParser] def print := leading_parser "#print " >> (ident <|> strLit) @[builtinCommandParser] def printAxioms := leading_parser "#print " >> nonReservedSymbol "axioms " >> ident @[builtinCommandParser] def «resolve_name» := leading_parser "#resolve_name " >> ident @[builtinCommandParser] def «init_quot» := leading_parser "init_quot" def optionValue := nonReservedSymbol "true" <|> nonReservedSymbol "false" <|> strLit <|> numLit @[builtinCommandParser] def «set_option» := leading_parser "set_option " >> ident >> ppSpace >> optionValue def eraseAttr := leading_parser "-" >> rawIdent @[builtinCommandParser] def «attribute» := leading_parser "attribute " >> "[" >> sepBy1 (eraseAttr <|> Term.attrInstance) ", " >> "] " >> many1 ident @[builtinCommandParser] def «export» := leading_parser "export " >> ident >> " (" >> many1 ident >> ")" def openHiding := leading_parser atomic (ident >> "hiding") >> many1 (checkColGt >> ident) def openRenamingItem := leading_parser ident >> unicodeSymbol " → " " -> " >> checkColGt >> ident def openRenaming := leading_parser atomic (ident >> "renaming") >> sepBy1 openRenamingItem ", " def openOnly := leading_parser atomic (ident >> " (") >> many1 ident >> ")" def openSimple := leading_parser many1 (checkColGt >> ident) def openScoped := leading_parser "scoped " >> many1 (checkColGt >> ident) def openDecl := openHiding <|> openRenaming <|> openOnly <|> openSimple <|> openScoped @[builtinCommandParser] def «open» := leading_parser withPosition ("open " >> openDecl) @[builtinCommandParser] def «mutual» := leading_parser "mutual " >> many1 (ppLine >> notSymbol "end" >> commandParser) >> ppDedent (ppLine >> "end") >> terminationSuffix @[builtinCommandParser] def «initialize» := leading_parser optional visibility >> "initialize " >> optional (atomic (ident >> Term.typeSpec >> Term.leftArrow)) >> Term.doSeq @[builtinCommandParser] def «builtin_initialize» := leading_parser optional visibility >> "builtin_initialize " >> optional (atomic (ident >> Term.typeSpec >> Term.leftArrow)) >> Term.doSeq @[builtinCommandParser] def «in» := trailing_parser withOpen (" in " >> commandParser) /- This is an auxiliary command for generation constructor injectivity theorems for inductive types defined at `Prelude.lean`. It is meant for bootstrapping purposes only. -/ @[builtinCommandParser] def genInjectiveTheorems := leading_parser "gen_injective_theorems% " >> ident @[runBuiltinParserAttributeHooks] abbrev declModifiersF := declModifiers false @[runBuiltinParserAttributeHooks] abbrev declModifiersT := declModifiers true builtin_initialize register_parser_alias "declModifiers" declModifiersF register_parser_alias "nestedDeclModifiers" declModifiersT register_parser_alias declId register_parser_alias declSig register_parser_alias declVal register_parser_alias optDeclSig register_parser_alias openDecl end Command namespace Term @[builtinTermParser] def «open» := leading_parser:leadPrec "open " >> Command.openDecl >> withOpenDecl (" in " >> termParser) @[builtinTermParser] def «set_option» := leading_parser:leadPrec "set_option " >> ident >> ppSpace >> Command.optionValue >> " in " >> termParser end Term namespace Tactic @[builtinTacticParser] def «open» := leading_parser:leadPrec "open " >> Command.openDecl >> withOpenDecl (" in " >> tacticSeq) @[builtinTacticParser] def «set_option» := leading_parser:leadPrec "set_option " >> ident >> ppSpace >> Command.optionValue >> " in " >> tacticSeq end Tactic end Parser end Lean
2c747a4b9a6b7872d79c729850ebafa4564e8617
302c785c90d40ad3d6be43d33bc6a558354cc2cf
/src/data/rat/order.lean
77aa05ef8b54be13071e860218cbaa5934e7f897
[ "Apache-2.0" ]
permissive
ilitzroth/mathlib
ea647e67f1fdfd19a0f7bdc5504e8acec6180011
5254ef14e3465f6504306132fe3ba9cec9ffff16
refs/heads/master
1,680,086,661,182
1,617,715,647,000
1,617,715,647,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,615
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import data.rat.basic /-! # Order for Rational Numbers ## Summary We define the order on `ℚ`, prove that `ℚ` is a discrete, linearly ordered field, and define functions such as `abs` and `sqrt` that depend on this order. ## Notations - `/.` is infix notation for `rat.mk`. ## Tags rat, rationals, field, ℚ, numerator, denominator, num, denom, order, ordering, sqrt, abs -/ namespace rat variables (a b c : ℚ) open_locale rat /-- A rational number is called nonnegative if its numerator is nonnegative. -/ protected def nonneg (r : ℚ) : Prop := 0 ≤ r.num @[simp] theorem mk_nonneg (a : ℤ) {b : ℤ} (h : 0 < b) : (a /. b).nonneg ↔ 0 ≤ a := begin generalize ha : a /. b = x, cases x with n₁ d₁ h₁ c₁, rw num_denom' at ha, simp [rat.nonneg], have d0 := int.coe_nat_lt.2 h₁, have := (mk_eq (ne_of_gt h) (ne_of_gt d0)).1 ha, constructor; intro h₂, { apply nonneg_of_mul_nonneg_right _ d0, rw this, exact mul_nonneg h₂ (le_of_lt h) }, { apply nonneg_of_mul_nonneg_right _ h, rw ← this, exact mul_nonneg h₂ (int.coe_zero_le _) }, end protected lemma nonneg_add {a b} : rat.nonneg a → rat.nonneg b → rat.nonneg (a + b) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, begin have d₁0 : 0 < (d₁:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₁), have d₂0 : 0 < (d₂:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₂), simp [d₁0, d₂0, h₁, h₂, mul_pos d₁0 d₂0], intros n₁0 n₂0, apply add_nonneg; apply mul_nonneg; {assumption <|> apply int.coe_zero_le}, end protected lemma nonneg_mul {a b} : rat.nonneg a → rat.nonneg b → rat.nonneg (a * b) := num_denom_cases_on' a $ λ n₁ d₁ h₁, num_denom_cases_on' b $ λ n₂ d₂ h₂, begin have d₁0 : 0 < (d₁:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₁), have d₂0 : 0 < (d₂:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h₂), simp [d₁0, d₂0, h₁, h₂, mul_pos d₁0 d₂0, mul_nonneg] { contextual := tt } end protected lemma nonneg_antisymm {a} : rat.nonneg a → rat.nonneg (-a) → a = 0 := num_denom_cases_on' a $ λ n d h, begin have d0 : 0 < (d:ℤ) := int.coe_nat_pos.2 (nat.pos_of_ne_zero h), simp [d0, h], exact λ h₁ h₂, le_antisymm h₂ h₁ end protected lemma nonneg_total : rat.nonneg a ∨ rat.nonneg (-a) := by cases a with n; exact or.imp_right neg_nonneg_of_nonpos (le_total 0 n) instance decidable_nonneg : decidable (rat.nonneg a) := by cases a; unfold rat.nonneg; apply_instance /-- Relation `a ≤ b` on `ℚ` defined as `a ≤ b ↔ rat.nonneg (b - a)`. Use `a ≤ b` instead of `rat.le a b`. -/ protected def le (a b : ℚ) := rat.nonneg (b - a) instance : has_le ℚ := ⟨rat.le⟩ instance decidable_le : decidable_rel ((≤) : ℚ → ℚ → Prop) | a b := show decidable (rat.nonneg (b - a)), by apply_instance protected theorem le_def {a b c d : ℤ} (b0 : 0 < b) (d0 : 0 < d) : a /. b ≤ c /. d ↔ a * d ≤ c * b := begin show rat.nonneg _ ↔ _, rw ← sub_nonneg, simp [sub_eq_add_neg, ne_of_gt b0, ne_of_gt d0, mul_pos d0 b0] end protected theorem le_refl : a ≤ a := show rat.nonneg (a - a), by rw sub_self; exact le_refl (0 : ℤ) protected theorem le_total : a ≤ b ∨ b ≤ a := by have := rat.nonneg_total (b - a); rwa neg_sub at this protected theorem le_antisymm {a b : ℚ} (hab : a ≤ b) (hba : b ≤ a) : a = b := by { have := eq_neg_of_add_eq_zero (rat.nonneg_antisymm hba $ by rwa [← sub_eq_add_neg, neg_sub]), rwa neg_neg at this } protected theorem le_trans {a b c : ℚ} (hab : a ≤ b) (hbc : b ≤ c) : a ≤ c := have rat.nonneg (b - a + (c - b)), from rat.nonneg_add hab hbc, by simpa [sub_eq_add_neg, add_comm, add_left_comm] instance : linear_order ℚ := { le := rat.le, le_refl := rat.le_refl, le_trans := @rat.le_trans, le_antisymm := @rat.le_antisymm, le_total := rat.le_total, decidable_eq := by apply_instance, decidable_le := assume a b, rat.decidable_nonneg (b - a) } /- Extra instances to short-circuit type class resolution -/ instance : has_lt ℚ := by apply_instance instance : distrib_lattice ℚ := by apply_instance instance : lattice ℚ := by apply_instance instance : semilattice_inf ℚ := by apply_instance instance : semilattice_sup ℚ := by apply_instance instance : has_inf ℚ := by apply_instance instance : has_sup ℚ := by apply_instance instance : partial_order ℚ := by apply_instance instance : preorder ℚ := by apply_instance protected lemma le_def' {p q : ℚ} : p ≤ q ↔ p.num * q.denom ≤ q.num * p.denom := begin rw [←(@num_denom q), ←(@num_denom p)], conv_rhs { simp only [num_denom] }, exact rat.le_def (by exact_mod_cast p.pos) (by exact_mod_cast q.pos) end protected lemma lt_def {p q : ℚ} : p < q ↔ p.num * q.denom < q.num * p.denom := begin rw [lt_iff_le_and_ne, rat.le_def'], suffices : p ≠ q ↔ p.num * q.denom ≠ q.num * p.denom, by { split; intro h, { exact lt_iff_le_and_ne.elim_right ⟨h.left, (this.elim_left h.right)⟩ }, { have tmp := lt_iff_le_and_ne.elim_left h, exact ⟨tmp.left, this.elim_right tmp.right⟩ }}, exact (not_iff_not.elim_right eq_iff_mul_eq_mul) end theorem nonneg_iff_zero_le {a} : rat.nonneg a ↔ 0 ≤ a := show rat.nonneg a ↔ rat.nonneg (a - 0), by simp theorem num_nonneg_iff_zero_le : ∀ {a : ℚ}, 0 ≤ a.num ↔ 0 ≤ a | ⟨n, d, h, c⟩ := @nonneg_iff_zero_le ⟨n, d, h, c⟩ protected theorem add_le_add_left {a b c : ℚ} : c + a ≤ c + b ↔ a ≤ b := by unfold has_le.le rat.le; rw add_sub_add_left_eq_sub protected theorem mul_nonneg {a b : ℚ} (ha : 0 ≤ a) (hb : 0 ≤ b) : 0 ≤ a * b := by rw ← nonneg_iff_zero_le at ha hb ⊢; exact rat.nonneg_mul ha hb instance : linear_ordered_field ℚ := { zero_le_one := dec_trivial, add_le_add_left := assume a b ab c, rat.add_le_add_left.2 ab, mul_pos := assume a b ha hb, lt_of_le_of_ne (rat.mul_nonneg (le_of_lt ha) (le_of_lt hb)) (mul_ne_zero (ne_of_lt ha).symm (ne_of_lt hb).symm).symm, ..rat.field, ..rat.linear_order, ..rat.semiring } /- Extra instances to short-circuit type class resolution -/ instance : linear_ordered_comm_ring ℚ := by apply_instance instance : linear_ordered_ring ℚ := by apply_instance instance : ordered_ring ℚ := by apply_instance instance : linear_ordered_semiring ℚ := by apply_instance instance : ordered_semiring ℚ := by apply_instance instance : linear_ordered_add_comm_group ℚ := by apply_instance instance : ordered_add_comm_group ℚ := by apply_instance instance : ordered_cancel_add_comm_monoid ℚ := by apply_instance instance : ordered_add_comm_monoid ℚ := by apply_instance attribute [irreducible] rat.le theorem num_pos_iff_pos {a : ℚ} : 0 < a.num ↔ 0 < a := lt_iff_lt_of_le_iff_le $ by simpa [(by cases a; refl : (-a).num = -a.num)] using @num_nonneg_iff_zero_le (-a) lemma div_lt_div_iff_mul_lt_mul {a b c d : ℤ} (b_pos : 0 < b) (d_pos : 0 < d) : (a : ℚ) / b < c / d ↔ a * d < c * b := begin simp only [lt_iff_le_not_le], apply and_congr, { simp [div_num_denom, (rat.le_def b_pos d_pos)] }, { apply not_iff_not_of_iff, simp [div_num_denom, (rat.le_def d_pos b_pos)] } end lemma lt_one_iff_num_lt_denom {q : ℚ} : q < 1 ↔ q.num < q.denom := begin cases decidable.em (0 < q) with q_pos q_nonpos, { simp [rat.lt_def] }, { replace q_nonpos : q ≤ 0, from not_lt.elim_left q_nonpos, have : q.num < q.denom, by { have : ¬0 < q.num ↔ ¬0 < q, from not_iff_not.elim_right num_pos_iff_pos, simp only [not_lt] at this, exact lt_of_le_of_lt (this.elim_right q_nonpos) (by exact_mod_cast q.pos) }, simp only [this, (lt_of_le_of_lt q_nonpos zero_lt_one)] } end theorem abs_def (q : ℚ) : abs q = q.num.nat_abs /. q.denom := begin have hz : (0:ℚ) = 0 /. 1 := rfl, cases le_total q 0 with hq hq, { rw [abs_of_nonpos hq], rw [←(@num_denom q), hz, rat.le_def (int.coe_nat_pos.2 q.pos) zero_lt_one, mul_one, zero_mul] at hq, rw [int.of_nat_nat_abs_of_nonpos hq, ← neg_def, num_denom] }, { rw [abs_of_nonneg hq], rw [←(@num_denom q), hz, rat.le_def zero_lt_one (int.coe_nat_pos.2 q.pos), mul_one, zero_mul] at hq, rw [int.nat_abs_of_nonneg hq, num_denom] } end end rat
b7a136197f818dfcf5cdc12803326c9eb1b88963
80162757f50b09d3cad5564907e4c9b00742e045
/combo.lean
8b37ba85a0ffadb19d99578c7a4cf0fbaeb2e291
[]
no_license
EdAyers/edlib
cc30d0a54fed347a85b6df6045f68e6b48bc71a3
78b8c5d91f023f939c102837d748868e2f3ed27d
refs/heads/master
1,586,459,758,216
1,571,322,179,000
1,571,322,179,000
160,538,917
2
0
null
null
null
null
UTF-8
Lean
false
false
4,835
lean
/- Combinatorics on lists. Copied from mathlib. -/ namespace list universes u v variables {α : Type u} {β : Type v} inductive perm : list α → list α → Prop |nil {}: perm [] [] |skip {x : α} {l₁ l₂ : list α} : perm l₁ l₂ → perm (x :: l₁) (x :: l₂) |swap {x y : α} {l : list α} : perm (y::x::l) (x::y::l) |trans {l₁ l₂ l₃ : list α} : perm l₁ l₂ → perm l₂ l₃ → perm l₁ l₃ open perm infix ~ := perm @[refl] protected theorem perm.refl : ∀ (l : list α), l ~ l |[] := perm.nil |(h::t) := skip $ perm.refl t @[symm] protected theorem perm.symm : ∀ {l₁ l₂ : list α}, l₁ ~ l₂ → l₂ ~ l₁ := λ l₁ l₂ p, perm.rec_on p (perm.nil) (λ x l₁ l₂ p q, skip q) (λ x y l, swap) (λ l₁ l₂ l₃ p₁₂ p₂₃ p₂₁ p₃₂, trans p₃₂ p₂₁) attribute [trans] perm.trans local infix ` ◾ ` :30 := perm.trans /- ...etc etc -/ end list namespace perm open nat inductive fin : ℕ → Type | zero {n} : fin (succ n) | succ {n} : fin n → fin (succ n) def fin_0_absurd {C : Type} : fin 0 → C := λ z, @fin.rec_on (λ n f, nat.rec_on n C (λ _ _, unit)) 0 z (λ x,⟨⟩) (λ x y g, ⟨⟩) def fin.comp : Π {n m : ℕ}, fin n → fin m → ordering |(succ n) (succ m) (fin.zero) (fin.zero) := ordering.eq |(succ n) (succ m) (fin.succ x) (fin.zero) := ordering.gt |(succ n) (succ m) (fin.zero) (fin.succ y) := ordering.lt |(succ n) (succ m) (fin.succ x) (fin.succ y) := fin.comp x y def fin.inj : Π {n : ℕ}, fin n → fin (succ n) |(succ n) (fin.zero) := fin.zero |(succ n) (fin.succ x) := fin.succ (fin.inj x) universes u inductive vec (α : Type u) : ℕ → Type u |zero {} : vec 0 |cons {n} : α → vec n → vec (succ n) def vec.popn {α : Type u} : Π {n:ℕ} (i:fin (succ n)), vec α (succ n) → α × vec α ( n) |n (fin.zero) (vec.cons h t) := ⟨h,t⟩ |(succ n) (fin.succ i) (vec.cons h t) := let ⟨a,t'⟩ := vec.popn i t in ⟨a, vec.cons h t'⟩ inductive perm : ℕ → ℕ → Type | first {n} : perm 0 n | pick {r n} : fin (succ n) → perm r n → perm (succ r) (succ n) infix ` ↪ `:40 := perm def perm.id : Π {r}, r ↪ r |0 := perm.first |(succ r) := perm.pick (fin.zero) (perm.id) def perm.empty_motive : ℕ → ℕ → Type |0 n := unit |(succ r) (succ n) := unit |_ _ := empty def perm.absurd : Π {r}, (succ r) ↪ 0 → empty := λ r π, @perm.rec_on (λ r n π, perm.empty_motive r n) (succ r) (0) π (λ n, ⟨⟩) (λ r n x y z, ⟨⟩) def perm.inj : Π {r n}, r ↪ n → r ↪ (succ n) |_ _ (perm.first) := perm.first |_ _ (perm.pick i π) := perm.pick (fin.inj i) (perm.inj π) def perm.inj_at : Π {r n}, r ↪ n → fin (succ n) → r ↪ (succ n) |_ _ (perm.first) _ := perm.first |_ _ (perm.pick i π) (fin.zero) := perm.pick (fin.succ i) (perm.inj_at π fin.zero) |_ _ (perm.pick (fin.zero) π) (fin.succ x) := perm.pick (fin.zero) (perm.inj_at π x) |_ _ (perm.pick (fin.succ y) π) (fin.succ x) := -- def perm.get : Π {r n}, r ↪ n → fin r → fin n -- --|r n (perm.first) z := -- |r n (perm.pick i π) (fin.zero) := i -- |r n (perm.pick i π) (fin.succ x) := -- let j := perm.get π x in -- match fin.comp i j with -- |ordering.lt := fin.in def perm.pop :Π {r n}, (succ r) ↪ (succ n) → fin (succ r) → fin (succ n) × r ↪ (succ n) |r n (perm.pick i π) (fin.zero) := ⟨i, perm.inj π⟩ |(succ r) (succ n) (perm.pick (fin.zero) π) (fin.succ x) := def perm.comp : Π {a b c}, a ↪ b → b ↪ c → a ↪ c |0 _ _ _ _ := perm.first |(succ a) 0 c π (perm.first) := empty.rec_on _ $ perm.absurd π |(succ a) (succ b) 0 π₁ π₂ := empty.rec_on _ $ perm.absurd π₂ |(succ a) (succ b) (succ c) (perm.pick i₁ π₁) (perm.pick i₂ π₂) := _ def action {α : Type*} : Π {r n : ℕ}, perm r n → vec α n → vec α r |_ _ (perm.first) v := vec.zero |_ _ (perm.pick i π) v := let ⟨a,t'⟩ := vec.popn i v in vec.cons a (action π t') end perm #check fin universes u structure iso (n m : ℕ) := (f : fin n → fin m) (g : fin m → fin n) (fg := f ∘ g = id) (gf := g ∘ f = id) def iso_inv {X} {Y} : iso X Y → iso Y X := λ ⟨f,g,fg,gf⟩, ⟨g,f,gf,fg⟩ infix ` ↭ `: 40 := iso instance {X} {Y} : has_coe_to_fun (X ↭ Y) := ⟨λ F, fin X → fin Y, λ F, F.f⟩ postfix `⁻¹ ` := iso_inv structure inj (r n : ℕ) : Type := (f : fin r → fin n) (is_inj : ∀ x y, f x = f y → x = y) infix ` ↪ `:40 := inj instance {n r} : has_coe_to_fun (r ↪ n) := ⟨λ F, fin r → fin n, λ F, F.f⟩ def permuatation (r n : ℕ) := inj r n def same_image {r} {n} : (r ↪ n) → (r ↪ n) → Prop := λ π₁ π₂, ∃ σ: r ↭ r, π₁ ∘ σ = π₂ /- something like; quotient inj by ordering? -/ def combination (r n : ℕ) := @quot (r ↪ n) same_image inductive permuatation_concrete (r n : ℕ) :=
81fb51d8c15fa5df3a9259be10f90ed93452bdba
4727251e0cd73359b15b664c3170e5d754078599
/src/linear_algebra/basis.lean
b0564cf382f04751baaade19e5e868d9b0ac2536
[ "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
49,160
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, Alexander Bentkamp -/ import algebra.big_operators.finsupp import algebra.big_operators.finprod import data.fintype.card import linear_algebra.finsupp import linear_algebra.linear_independent import linear_algebra.linear_pmap import linear_algebra.projection /-! # Bases This file defines bases in a module or vector space. It is inspired by Isabelle/HOL's linear algebra, and hence indirectly by HOL Light. ## Main definitions All definitions are given for families of vectors, i.e. `v : ι → M` where `M` is the module or vector space and `ι : Type*` is an arbitrary indexing type. * `basis ι R M` is the type of `ι`-indexed `R`-bases for a module `M`, represented by a linear equiv `M ≃ₗ[R] ι →₀ R`. * the basis vectors of a basis `b : basis ι R M` are available as `b i`, where `i : ι` * `basis.repr` is the isomorphism sending `x : M` to its coordinates `basis.repr x : ι →₀ R`. The converse, turning this isomorphism into a basis, is called `basis.of_repr`. * If `ι` is finite, there is a variant of `repr` called `basis.equiv_fun b : M ≃ₗ[R] ι → R` (saving you from having to work with `finsupp`). The converse, turning this isomorphism into a basis, is called `basis.of_equiv_fun`. * `basis.constr hv f` constructs a linear map `M₁ →ₗ[R] M₂` given the values `f : ι → M₂` at the basis elements `⇑b : ι → M₁`. * `basis.reindex` uses an equiv to map a basis to a different indexing set. * `basis.map` uses a linear equiv to map a basis to a different module. ## Main statements * `basis.mk`: a linear independent set of vectors spanning the whole module determines a basis * `basis.ext` states that two linear maps are equal if they coincide on a basis. Similar results are available for linear equivs (if they coincide on the basis vectors), elements (if their coordinates coincide) and the functions `b.repr` and `⇑b`. * `basis.of_vector_space` states that every vector space has a basis. ## Implementation notes We use families instead of sets because it allows us to say that two identical vectors are linearly dependent. For bases, this is useful as well because we can easily derive ordered bases by using an ordered index type `ι`. ## Tags basis, bases -/ noncomputable theory universe u open function set submodule open_locale classical big_operators variables {ι : Type*} {ι' : Type*} {R : Type*} {K : Type*} variables {M : Type*} {M' M'' : Type*} {V : Type u} {V' : Type*} section module variables [semiring R] variables [add_comm_monoid M] [module R M] [add_comm_monoid M'] [module R M'] section variables (ι) (R) (M) /-- A `basis ι R M` for a module `M` is the type of `ι`-indexed `R`-bases of `M`. The basis vectors are available as `coe_fn (b : basis ι R M) : ι → M`. To turn a linear independent family of vectors spanning `M` into a basis, use `basis.mk`. They are internally represented as linear equivs `M ≃ₗ[R] (ι →₀ R)`, available as `basis.repr`. -/ structure basis := of_repr :: (repr : M ≃ₗ[R] (ι →₀ R)) end namespace basis instance : inhabited (basis ι R (ι →₀ R)) := ⟨basis.of_repr (linear_equiv.refl _ _)⟩ variables (b b₁ : basis ι R M) (i : ι) (c : R) (x : M) section repr /-- `b i` is the `i`th basis vector. -/ instance : has_coe_to_fun (basis ι R M) (λ _, ι → M) := { coe := λ b i, b.repr.symm (finsupp.single i 1) } @[simp] lemma coe_of_repr (e : M ≃ₗ[R] (ι →₀ R)) : ⇑(of_repr e) = λ i, e.symm (finsupp.single i 1) := rfl protected lemma injective [nontrivial R] : injective b := b.repr.symm.injective.comp (λ _ _, (finsupp.single_left_inj (one_ne_zero : (1 : R) ≠ 0)).mp) lemma repr_symm_single_one : b.repr.symm (finsupp.single i 1) = b i := rfl lemma repr_symm_single : b.repr.symm (finsupp.single i c) = c • b i := calc b.repr.symm (finsupp.single i c) = b.repr.symm (c • finsupp.single i 1) : by rw [finsupp.smul_single', mul_one] ... = c • b i : by rw [linear_equiv.map_smul, repr_symm_single_one] @[simp] lemma repr_self : b.repr (b i) = finsupp.single i 1 := linear_equiv.apply_symm_apply _ _ lemma repr_self_apply (j) [decidable (i = j)] : b.repr (b i) j = if i = j then 1 else 0 := by rw [repr_self, finsupp.single_apply] @[simp] lemma repr_symm_apply (v) : b.repr.symm v = finsupp.total ι M R b v := calc b.repr.symm v = b.repr.symm (v.sum finsupp.single) : by simp ... = ∑ i in v.support, b.repr.symm (finsupp.single i (v i)) : by rw [finsupp.sum, linear_equiv.map_sum] ... = finsupp.total ι M R b v : by simp [repr_symm_single, finsupp.total_apply, finsupp.sum] @[simp] lemma coe_repr_symm : ↑b.repr.symm = finsupp.total ι M R b := linear_map.ext (λ v, b.repr_symm_apply v) @[simp] lemma repr_total (v) : b.repr (finsupp.total _ _ _ b v) = v := by { rw ← b.coe_repr_symm, exact b.repr.apply_symm_apply v } @[simp] lemma total_repr : finsupp.total _ _ _ b (b.repr x) = x := by { rw ← b.coe_repr_symm, exact b.repr.symm_apply_apply x } lemma repr_range : (b.repr : M →ₗ[R] (ι →₀ R)).range = finsupp.supported R R univ := by rw [linear_equiv.range, finsupp.supported_univ] lemma mem_span_repr_support {ι : Type*} (b : basis ι R M) (m : M) : m ∈ span R (b '' (b.repr m).support) := (finsupp.mem_span_image_iff_total _).2 ⟨b.repr m, (by simp [finsupp.mem_supported_support])⟩ end repr section coord /-- `b.coord i` is the linear function giving the `i`'th coordinate of a vector with respect to the basis `b`. `b.coord i` is an element of the dual space. In particular, for finite-dimensional spaces it is the `ι`th basis vector of the dual space. -/ @[simps] def coord : M →ₗ[R] R := (finsupp.lapply i) ∘ₗ ↑b.repr lemma forall_coord_eq_zero_iff {x : M} : (∀ i, b.coord i x = 0) ↔ x = 0 := iff.trans (by simp only [b.coord_apply, finsupp.ext_iff, finsupp.zero_apply]) b.repr.map_eq_zero_iff /-- The sum of the coordinates of an element `m : M` with respect to a basis. -/ noncomputable def sum_coords : M →ₗ[R] R := finsupp.lsum ℕ (λ i, linear_map.id) ∘ₗ (b.repr : M →ₗ[R] ι →₀ R) @[simp] lemma coe_sum_coords : (b.sum_coords : M → R) = λ m, (b.repr m).sum (λ i, id) := rfl lemma coe_sum_coords_eq_finsum : (b.sum_coords : M → R) = λ m, ∑ᶠ i, b.coord i m := begin ext m, simp only [basis.sum_coords, basis.coord, finsupp.lapply_apply, linear_map.id_coe, linear_equiv.coe_coe, function.comp_app, finsupp.coe_lsum, linear_map.coe_comp, finsum_eq_sum _ (b.repr m).finite_support, finsupp.sum, finset.finite_to_set_to_finset, id.def, finsupp.fun_support_eq], end @[simp] lemma coe_sum_coords_of_fintype [fintype ι] : (b.sum_coords : M → R) = ∑ i, b.coord i := begin ext m, simp only [sum_coords, finsupp.sum_fintype, linear_map.id_coe, linear_equiv.coe_coe, coord_apply, id.def, fintype.sum_apply, implies_true_iff, eq_self_iff_true, finsupp.coe_lsum, linear_map.coe_comp], end @[simp] lemma sum_coords_self_apply : b.sum_coords (b i) = 1 := by simp only [basis.sum_coords, linear_map.id_coe, linear_equiv.coe_coe, id.def, basis.repr_self, function.comp_app, finsupp.coe_lsum, linear_map.coe_comp, finsupp.sum_single_index] end coord section ext variables {R₁ : Type*} [semiring R₁] {σ : R →+* R₁} {σ' : R₁ →+* R} variables [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] variables {M₁ : Type*} [add_comm_monoid M₁] [module R₁ M₁] /-- Two linear maps are equal if they are equal on basis vectors. -/ theorem ext {f₁ f₂ : M →ₛₗ[σ] M₁} (h : ∀ i, f₁ (b i) = f₂ (b i)) : f₁ = f₂ := by { ext x, rw [← b.total_repr x, finsupp.total_apply, finsupp.sum], simp only [linear_map.map_sum, linear_map.map_smulₛₗ, h] } include σ' /-- Two linear equivs are equal if they are equal on basis vectors. -/ theorem ext' {f₁ f₂ : M ≃ₛₗ[σ] M₁} (h : ∀ i, f₁ (b i) = f₂ (b i)) : f₁ = f₂ := by { ext x, rw [← b.total_repr x, finsupp.total_apply, finsupp.sum], simp only [linear_equiv.map_sum, linear_equiv.map_smulₛₗ, h] } omit σ' /-- Two elements are equal if their coordinates are equal. -/ theorem ext_elem {x y : M} (h : ∀ i, b.repr x i = b.repr y i) : x = y := by { rw [← b.total_repr x, ← b.total_repr y], congr' 1, ext i, exact h i } lemma repr_eq_iff {b : basis ι R M} {f : M →ₗ[R] ι →₀ R} : ↑b.repr = f ↔ ∀ i, f (b i) = finsupp.single i 1 := ⟨λ h i, h ▸ b.repr_self i, λ h, b.ext (λ i, (b.repr_self i).trans (h i).symm)⟩ lemma repr_eq_iff' {b : basis ι R M} {f : M ≃ₗ[R] ι →₀ R} : b.repr = f ↔ ∀ i, f (b i) = finsupp.single i 1 := ⟨λ h i, h ▸ b.repr_self i, λ h, b.ext' (λ i, (b.repr_self i).trans (h i).symm)⟩ lemma apply_eq_iff {b : basis ι R M} {x : M} {i : ι} : b i = x ↔ b.repr x = finsupp.single i 1 := ⟨λ h, h ▸ b.repr_self i, λ h, b.repr.injective ((b.repr_self i).trans h.symm)⟩ /-- An unbundled version of `repr_eq_iff` -/ lemma repr_apply_eq (f : M → ι → R) (hadd : ∀ x y, f (x + y) = f x + f y) (hsmul : ∀ (c : R) (x : M), f (c • x) = c • f x) (f_eq : ∀ i, f (b i) = finsupp.single i 1) (x : M) (i : ι) : b.repr x i = f x i := begin let f_i : M →ₗ[R] R := { to_fun := λ x, f x i, map_add' := λ _ _, by rw [hadd, pi.add_apply], map_smul' := λ _ _, by { simp [hsmul, pi.smul_apply] } }, have : (finsupp.lapply i) ∘ₗ ↑b.repr = f_i, { refine b.ext (λ j, _), show b.repr (b j) i = f (b j) i, rw [b.repr_self, f_eq] }, calc b.repr x i = f_i x : by { rw ← this, refl } ... = f x i : rfl end /-- Two bases are equal if they assign the same coordinates. -/ lemma eq_of_repr_eq_repr {b₁ b₂ : basis ι R M} (h : ∀ x i, b₁.repr x i = b₂.repr x i) : b₁ = b₂ := have b₁.repr = b₂.repr, by { ext, apply h }, by { cases b₁, cases b₂, simpa } /-- Two bases are equal if their basis vectors are the same. -/ @[ext] lemma eq_of_apply_eq {b₁ b₂ : basis ι R M} (h : ∀ i, b₁ i = b₂ i) : b₁ = b₂ := suffices b₁.repr = b₂.repr, by { cases b₁, cases b₂, simpa }, repr_eq_iff'.mpr (λ i, by rw [h, b₂.repr_self]) end ext section map variables (f : M ≃ₗ[R] M') /-- Apply the linear equivalence `f` to the basis vectors. -/ @[simps] protected def map : basis ι R M' := of_repr (f.symm.trans b.repr) @[simp] lemma map_apply (i) : b.map f i = f (b i) := rfl end map section map_coeffs variables {R' : Type*} [semiring R'] [module R' M] (f : R ≃+* R') (h : ∀ c (x : M), f c • x = c • x) include f h b local attribute [instance] has_scalar.comp.is_scalar_tower /-- If `R` and `R'` are isomorphic rings that act identically on a module `M`, then a basis for `M` as `R`-module is also a basis for `M` as `R'`-module. See also `basis.algebra_map_coeffs` for the case where `f` is equal to `algebra_map`. -/ @[simps {simp_rhs := tt}] def map_coeffs : basis ι R' M := begin letI : module R' R := module.comp_hom R (↑f.symm : R' →+* R), haveI : is_scalar_tower R' R M := { smul_assoc := λ x y z, begin dsimp [(•)], rw [mul_smul, ←h, f.apply_symm_apply], end }, exact (of_repr $ (b.repr.restrict_scalars R').trans $ finsupp.map_range.linear_equiv (module.comp_hom.to_linear_equiv f.symm).symm ) end lemma map_coeffs_apply (i : ι) : b.map_coeffs f h i = b i := apply_eq_iff.mpr $ by simp [f.to_add_equiv_eq_coe] @[simp] lemma coe_map_coeffs : (b.map_coeffs f h : ι → M) = b := funext $ b.map_coeffs_apply f h end map_coeffs section reindex variables (b' : basis ι' R M') variables (e : ι ≃ ι') /-- `b.reindex (e : ι ≃ ι')` is a basis indexed by `ι'` -/ def reindex : basis ι' R M := basis.of_repr (b.repr.trans (finsupp.dom_lcongr e)) lemma reindex_apply (i' : ι') : b.reindex e i' = b (e.symm i') := show (b.repr.trans (finsupp.dom_lcongr e)).symm (finsupp.single i' 1) = b.repr.symm (finsupp.single (e.symm i') 1), by rw [linear_equiv.symm_trans_apply, finsupp.dom_lcongr_symm, finsupp.dom_lcongr_single] @[simp] lemma coe_reindex : (b.reindex e : ι' → M) = b ∘ e.symm := funext (b.reindex_apply e) @[simp] lemma coe_reindex_repr : ((b.reindex e).repr x : ι' → R) = b.repr x ∘ e.symm := funext $ λ i', show (finsupp.dom_lcongr e : _ ≃ₗ[R] _) (b.repr x) i' = _, by simp @[simp] lemma reindex_repr (i' : ι') : (b.reindex e).repr x i' = b.repr x (e.symm i') := by rw coe_reindex_repr @[simp] lemma reindex_refl : b.reindex (equiv.refl ι) = b := eq_of_apply_eq $ λ i, by simp /-- `simp` normal form version of `range_reindex` -/ @[simp] lemma range_reindex' : set.range (b ∘ e.symm) = set.range b := by rw [range_comp, equiv.range_eq_univ, set.image_univ] lemma range_reindex : set.range (b.reindex e) = set.range b := by rw [coe_reindex, range_reindex'] /-- `b.reindex_range` is a basis indexed by `range b`, the basis vectors themselves. -/ def reindex_range : basis (range b) R M := if h : nontrivial R then by letI := h; exact b.reindex (equiv.of_injective b (basis.injective b)) else by letI : subsingleton R := not_nontrivial_iff_subsingleton.mp h; exact basis.of_repr (module.subsingleton_equiv R M (range b)) lemma finsupp.single_apply_left {α β γ : Type*} [has_zero γ] {f : α → β} (hf : function.injective f) (x z : α) (y : γ) : finsupp.single (f x) y (f z) = finsupp.single x y z := by simp [finsupp.single_apply, hf.eq_iff] lemma reindex_range_self (i : ι) (h := set.mem_range_self i) : b.reindex_range ⟨b i, h⟩ = b i := begin by_cases htr : nontrivial R, { letI := htr, simp [htr, reindex_range, reindex_apply, equiv.apply_of_injective_symm b.injective, subtype.coe_mk] }, { letI : subsingleton R := not_nontrivial_iff_subsingleton.mp htr, letI := module.subsingleton R M, simp [reindex_range] } end lemma reindex_range_repr_self (i : ι) : b.reindex_range.repr (b i) = finsupp.single ⟨b i, mem_range_self i⟩ 1 := calc b.reindex_range.repr (b i) = b.reindex_range.repr (b.reindex_range ⟨b i, mem_range_self i⟩) : congr_arg _ (b.reindex_range_self _ _).symm ... = finsupp.single ⟨b i, mem_range_self i⟩ 1 : b.reindex_range.repr_self _ @[simp] lemma reindex_range_apply (x : range b) : b.reindex_range x = x := by { rcases x with ⟨bi, ⟨i, rfl⟩⟩, exact b.reindex_range_self i, } lemma reindex_range_repr' (x : M) {bi : M} {i : ι} (h : b i = bi) : b.reindex_range.repr x ⟨bi, ⟨i, h⟩⟩ = b.repr x i := begin nontriviality, subst h, refine (b.repr_apply_eq (λ x i, b.reindex_range.repr x ⟨b i, _⟩) _ _ _ x i).symm, { intros x y, ext i, simp only [pi.add_apply, linear_equiv.map_add, finsupp.coe_add] }, { intros c x, ext i, simp only [pi.smul_apply, linear_equiv.map_smul, finsupp.coe_smul] }, { intros i, ext j, simp only [reindex_range_repr_self], refine @finsupp.single_apply_left _ _ _ _ (λ i, (⟨b i, _⟩ : set.range b)) _ _ _ _, exact λ i j h, b.injective (subtype.mk.inj h) } end @[simp] lemma reindex_range_repr (x : M) (i : ι) (h := set.mem_range_self i) : b.reindex_range.repr x ⟨b i, h⟩ = b.repr x i := b.reindex_range_repr' _ rfl section fintype variables [fintype ι] /-- `b.reindex_finset_range` is a basis indexed by `finset.univ.image b`, the finite set of basis vectors themselves. -/ def reindex_finset_range : basis (finset.univ.image b) R M := b.reindex_range.reindex ((equiv.refl M).subtype_equiv (by simp)) lemma reindex_finset_range_self (i : ι) (h := finset.mem_image_of_mem b (finset.mem_univ i)) : b.reindex_finset_range ⟨b i, h⟩ = b i := by { rw [reindex_finset_range, reindex_apply, reindex_range_apply], refl } @[simp] lemma reindex_finset_range_apply (x : finset.univ.image b) : b.reindex_finset_range x = x := by { rcases x with ⟨bi, hbi⟩, rcases finset.mem_image.mp hbi with ⟨i, -, rfl⟩, exact b.reindex_finset_range_self i } lemma reindex_finset_range_repr_self (i : ι) : b.reindex_finset_range.repr (b i) = finsupp.single ⟨b i, finset.mem_image_of_mem b (finset.mem_univ i)⟩ 1 := begin ext ⟨bi, hbi⟩, rw [reindex_finset_range, reindex_repr, reindex_range_repr_self], convert finsupp.single_apply_left ((equiv.refl M).subtype_equiv _).symm.injective _ _ _, refl end @[simp] lemma reindex_finset_range_repr (x : M) (i : ι) (h := finset.mem_image_of_mem b (finset.mem_univ i)) : b.reindex_finset_range.repr x ⟨b i, h⟩ = b.repr x i := by simp [reindex_finset_range] end fintype end reindex protected lemma linear_independent : linear_independent R b := linear_independent_iff.mpr $ λ l hl, calc l = b.repr (finsupp.total _ _ _ b l) : (b.repr_total l).symm ... = 0 : by rw [hl, linear_equiv.map_zero] protected lemma ne_zero [nontrivial R] (i) : b i ≠ 0 := b.linear_independent.ne_zero i protected lemma mem_span (x : M) : x ∈ span R (range b) := by { rw [← b.total_repr x, finsupp.total_apply, finsupp.sum], exact submodule.sum_mem _ (λ i hi, submodule.smul_mem _ _ (submodule.subset_span ⟨i, rfl⟩)) } protected lemma span_eq : span R (range b) = ⊤ := eq_top_iff.mpr $ λ x _, b.mem_span x lemma index_nonempty (b : basis ι R M) [nontrivial M] : nonempty ι := begin obtain ⟨x, y, ne⟩ : ∃ (x y : M), x ≠ y := nontrivial.exists_pair_ne, obtain ⟨i, _⟩ := not_forall.mp (mt b.ext_elem ne), exact ⟨i⟩ end section constr variables (S : Type*) [semiring S] [module S M'] variables [smul_comm_class R S M'] /-- Construct a linear map given the value at the basis. This definition is parameterized over an extra `semiring S`, such that `smul_comm_class R S M'` holds. If `R` is commutative, you can set `S := R`; if `R` is not commutative, you can recover an `add_equiv` by setting `S := ℕ`. See library note [bundled maps over different rings]. -/ def constr : (ι → M') ≃ₗ[S] (M →ₗ[R] M') := { to_fun := λ f, (finsupp.total M' M' R id).comp $ (finsupp.lmap_domain R R f) ∘ₗ ↑b.repr, inv_fun := λ f i, f (b i), left_inv := λ f, by { ext, simp }, right_inv := λ f, by { refine b.ext (λ i, _), simp }, map_add' := λ f g, by { refine b.ext (λ i, _), simp }, map_smul' := λ c f, by { refine b.ext (λ i, _), simp } } theorem constr_def (f : ι → M') : b.constr S f = (finsupp.total M' M' R id) ∘ₗ ((finsupp.lmap_domain R R f) ∘ₗ ↑b.repr) := rfl theorem constr_apply (f : ι → M') (x : M) : b.constr S f x = (b.repr x).sum (λ b a, a • f b) := by { simp only [constr_def, linear_map.comp_apply, finsupp.lmap_domain_apply, finsupp.total_apply], rw finsupp.sum_map_domain_index; simp [add_smul] } @[simp] lemma constr_basis (f : ι → M') (i : ι) : (b.constr S f : M → M') (b i) = f i := by simp [basis.constr_apply, b.repr_self] lemma constr_eq {g : ι → M'} {f : M →ₗ[R] M'} (h : ∀i, g i = f (b i)) : b.constr S g = f := b.ext $ λ i, (b.constr_basis S g i).trans (h i) lemma constr_self (f : M →ₗ[R] M') : b.constr S (λ i, f (b i)) = f := b.constr_eq S $ λ x, rfl lemma constr_range [nonempty ι] {f : ι → M'} : (b.constr S f).range = span R (range f) := by rw [b.constr_def S f, linear_map.range_comp, linear_map.range_comp, linear_equiv.range, ← finsupp.supported_univ, finsupp.lmap_domain_supported, ←set.image_univ, ← finsupp.span_image_eq_map_total, set.image_id] @[simp] lemma constr_comp (f : M' →ₗ[R] M') (v : ι → M') : b.constr S (f ∘ v) = f.comp (b.constr S v) := b.ext (λ i, by simp only [basis.constr_basis, linear_map.comp_apply]) end constr section equiv variables (b' : basis ι' R M') (e : ι ≃ ι') variables [add_comm_monoid M''] [module R M''] /-- If `b` is a basis for `M` and `b'` a basis for `M'`, and the index types are equivalent, `b.equiv b' e` is a linear equivalence `M ≃ₗ[R] M'`, mapping `b i` to `b' (e i)`. -/ protected def equiv : M ≃ₗ[R] M' := b.repr.trans (b'.reindex e.symm).repr.symm @[simp] lemma equiv_apply : b.equiv b' e (b i) = b' (e i) := by simp [basis.equiv] @[simp] lemma equiv_refl : b.equiv b (equiv.refl ι) = linear_equiv.refl R M := b.ext' (λ i, by simp) @[simp] lemma equiv_symm : (b.equiv b' e).symm = b'.equiv b e.symm := b'.ext' $ λ i, (b.equiv b' e).injective (by simp) @[simp] lemma equiv_trans {ι'' : Type*} (b'' : basis ι'' R M'') (e : ι ≃ ι') (e' : ι' ≃ ι'') : (b.equiv b' e).trans (b'.equiv b'' e') = b.equiv b'' (e.trans e') := b.ext' (λ i, by simp) @[simp] lemma map_equiv (b : basis ι R M) (b' : basis ι' R M') (e : ι ≃ ι') : b.map (b.equiv b' e) = b'.reindex e.symm := by { ext i, simp } end equiv section prod variables (b' : basis ι' R M') /-- `basis.prod` maps a `ι`-indexed basis for `M` and a `ι'`-indexed basis for `M'` to a `ι ⊕ ι'`-index basis for `M × M'`. -/ protected def prod : basis (ι ⊕ ι') R (M × M') := of_repr ((b.repr.prod b'.repr).trans (finsupp.sum_finsupp_lequiv_prod_finsupp R).symm) @[simp] lemma prod_repr_inl (x) (i) : (b.prod b').repr x (sum.inl i) = b.repr x.1 i := rfl @[simp] lemma prod_repr_inr (x) (i) : (b.prod b').repr x (sum.inr i) = b'.repr x.2 i := rfl lemma prod_apply_inl_fst (i) : (b.prod b' (sum.inl i)).1 = b i := b.repr.injective $ by { ext j, simp only [basis.prod, basis.coe_of_repr, linear_equiv.symm_trans_apply, linear_equiv.prod_symm, linear_equiv.prod_apply, b.repr.apply_symm_apply, linear_equiv.symm_symm, repr_self, equiv.to_fun_as_coe, finsupp.fst_sum_finsupp_lequiv_prod_finsupp], apply finsupp.single_apply_left sum.inl_injective } lemma prod_apply_inr_fst (i) : (b.prod b' (sum.inr i)).1 = 0 := b.repr.injective $ by { ext i, simp only [basis.prod, basis.coe_of_repr, linear_equiv.symm_trans_apply, linear_equiv.prod_symm, linear_equiv.prod_apply, b.repr.apply_symm_apply, linear_equiv.symm_symm, repr_self, equiv.to_fun_as_coe, finsupp.fst_sum_finsupp_lequiv_prod_finsupp, linear_equiv.map_zero, finsupp.zero_apply], apply finsupp.single_eq_of_ne sum.inr_ne_inl } lemma prod_apply_inl_snd (i) : (b.prod b' (sum.inl i)).2 = 0 := b'.repr.injective $ by { ext j, simp only [basis.prod, basis.coe_of_repr, linear_equiv.symm_trans_apply, linear_equiv.prod_symm, linear_equiv.prod_apply, b'.repr.apply_symm_apply, linear_equiv.symm_symm, repr_self, equiv.to_fun_as_coe, finsupp.snd_sum_finsupp_lequiv_prod_finsupp, linear_equiv.map_zero, finsupp.zero_apply], apply finsupp.single_eq_of_ne sum.inl_ne_inr } lemma prod_apply_inr_snd (i) : (b.prod b' (sum.inr i)).2 = b' i := b'.repr.injective $ by { ext i, simp only [basis.prod, basis.coe_of_repr, linear_equiv.symm_trans_apply, linear_equiv.prod_symm, linear_equiv.prod_apply, b'.repr.apply_symm_apply, linear_equiv.symm_symm, repr_self, equiv.to_fun_as_coe, finsupp.snd_sum_finsupp_lequiv_prod_finsupp], apply finsupp.single_apply_left sum.inr_injective } @[simp] lemma prod_apply (i) : b.prod b' i = sum.elim (linear_map.inl R M M' ∘ b) (linear_map.inr R M M' ∘ b') i := by { ext; cases i; simp only [prod_apply_inl_fst, sum.elim_inl, linear_map.inl_apply, prod_apply_inr_fst, sum.elim_inr, linear_map.inr_apply, prod_apply_inl_snd, prod_apply_inr_snd, comp_app] } end prod section no_zero_smul_divisors -- Can't be an instance because the basis can't be inferred. protected lemma no_zero_smul_divisors [no_zero_divisors R] (b : basis ι R M) : no_zero_smul_divisors R M := ⟨λ c x hcx, or_iff_not_imp_right.mpr (λ hx, begin rw [← b.total_repr x, ← linear_map.map_smul] at hcx, have := linear_independent_iff.mp b.linear_independent (c • b.repr x) hcx, rw smul_eq_zero at this, exact this.resolve_right (λ hr, hx (b.repr.map_eq_zero_iff.mp hr)) end)⟩ protected lemma smul_eq_zero [no_zero_divisors R] (b : basis ι R M) {c : R} {x : M} : c • x = 0 ↔ c = 0 ∨ x = 0 := @smul_eq_zero _ _ _ _ _ b.no_zero_smul_divisors _ _ lemma _root_.eq_bot_of_rank_eq_zero [no_zero_divisors R] (b : basis ι R M) (N : submodule R M) (rank_eq : ∀ {m : ℕ} (v : fin m → N), linear_independent R (coe ∘ v : fin m → M) → m = 0) : N = ⊥ := begin rw submodule.eq_bot_iff, intros x hx, contrapose! rank_eq with x_ne, refine ⟨1, λ _, ⟨x, hx⟩, _, one_ne_zero⟩, rw fintype.linear_independent_iff, rintros g sum_eq i, cases i, simp only [function.const_apply, fin.default_eq_zero, submodule.coe_mk, finset.univ_unique, function.comp_const, finset.sum_singleton] at sum_eq, convert (b.smul_eq_zero.mp sum_eq).resolve_right x_ne end end no_zero_smul_divisors section singleton /-- `basis.singleton ι R` is the basis sending the unique element of `ι` to `1 : R`. -/ protected def singleton (ι R : Type*) [unique ι] [semiring R] : basis ι R R := of_repr { to_fun := λ x, finsupp.single default x, inv_fun := λ f, f default, left_inv := λ x, by simp, right_inv := λ f, finsupp.unique_ext (by simp), map_add' := λ x y, by simp, map_smul' := λ c x, by simp } @[simp] lemma singleton_apply (ι R : Type*) [unique ι] [semiring R] (i) : basis.singleton ι R i = 1 := apply_eq_iff.mpr (by simp [basis.singleton]) @[simp] lemma singleton_repr (ι R : Type*) [unique ι] [semiring R] (x i) : (basis.singleton ι R).repr x i = x := by simp [basis.singleton, unique.eq_default i] lemma basis_singleton_iff {R M : Type*} [ring R] [nontrivial R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] (ι : Type*) [unique ι] : nonempty (basis ι R M) ↔ ∃ x ≠ 0, ∀ y : M, ∃ r : R, r • x = y := begin fsplit, { rintro ⟨b⟩, refine ⟨b default, b.linear_independent.ne_zero _, _⟩, simpa [span_singleton_eq_top_iff, set.range_unique] using b.span_eq }, { rintro ⟨x, nz, w⟩, refine ⟨of_repr $ linear_equiv.symm { to_fun := λ f, f default • x, inv_fun := λ y, finsupp.single default (w y).some, left_inv := λ f, finsupp.unique_ext _, right_inv := λ y, _, map_add' := λ y z, _, map_smul' := λ c y, _ }⟩, { rw [finsupp.add_apply, add_smul] }, { rw [finsupp.smul_apply, smul_assoc], simp }, { refine smul_left_injective _ nz _, simp only [finsupp.single_eq_same], exact (w (f default • x)).some_spec }, { simp only [finsupp.single_eq_same], exact (w y).some_spec } } end end singleton section empty variables (M) /-- If `M` is a subsingleton and `ι` is empty, this is the unique `ι`-indexed basis for `M`. -/ protected def empty [subsingleton M] [is_empty ι] : basis ι R M := of_repr 0 instance empty_unique [subsingleton M] [is_empty ι] : unique (basis ι R M) := { default := basis.empty M, uniq := λ ⟨x⟩, congr_arg of_repr $ subsingleton.elim _ _ } end empty end basis section fintype open basis open fintype variables [fintype ι] (b : basis ι R M) /-- A module over `R` with a finite basis is linearly equivalent to functions from its basis to `R`. -/ def basis.equiv_fun : M ≃ₗ[R] (ι → R) := linear_equiv.trans b.repr ({ to_fun := coe_fn, map_add' := finsupp.coe_add, map_smul' := finsupp.coe_smul, ..finsupp.equiv_fun_on_fintype } : (ι →₀ R) ≃ₗ[R] (ι → R)) /-- A module over a finite ring that admits a finite basis is finite. -/ def module.fintype_of_fintype [fintype R] : fintype M := fintype.of_equiv _ b.equiv_fun.to_equiv.symm theorem module.card_fintype [fintype R] [fintype M] : card M = (card R) ^ (card ι) := calc card M = card (ι → R) : card_congr b.equiv_fun.to_equiv ... = card R ^ card ι : card_fun /-- Given a basis `v` indexed by `ι`, the canonical linear equivalence between `ι → R` and `M` maps a function `x : ι → R` to the linear combination `∑_i x i • v i`. -/ @[simp] lemma basis.equiv_fun_symm_apply (x : ι → R) : b.equiv_fun.symm x = ∑ i, x i • b i := by simp [basis.equiv_fun, finsupp.total_apply, finsupp.sum_fintype] @[simp] lemma basis.equiv_fun_apply (u : M) : b.equiv_fun u = b.repr u := rfl @[simp] lemma basis.map_equiv_fun (f : M ≃ₗ[R] M') : (b.map f).equiv_fun = f.symm.trans b.equiv_fun := rfl lemma basis.sum_equiv_fun (u : M) : ∑ i, b.equiv_fun u i • b i = u := begin conv_rhs { rw ← b.total_repr u }, simp [finsupp.total_apply, finsupp.sum_fintype, b.equiv_fun_apply] end lemma basis.sum_repr (u : M) : ∑ i, b.repr u i • b i = u := b.sum_equiv_fun u @[simp] lemma basis.equiv_fun_self (i j : ι) : b.equiv_fun (b i) j = if i = j then 1 else 0 := by { rw [b.equiv_fun_apply, b.repr_self_apply] } /-- Define a basis by mapping each vector `x : M` to its coordinates `e x : ι → R`, as long as `ι` is finite. -/ def basis.of_equiv_fun (e : M ≃ₗ[R] (ι → R)) : basis ι R M := basis.of_repr $ e.trans $ linear_equiv.symm $ finsupp.linear_equiv_fun_on_fintype R R ι @[simp] lemma basis.of_equiv_fun_repr_apply (e : M ≃ₗ[R] (ι → R)) (x : M) (i : ι) : (basis.of_equiv_fun e).repr x i = e x i := rfl @[simp] lemma basis.coe_of_equiv_fun (e : M ≃ₗ[R] (ι → R)) : (basis.of_equiv_fun e : ι → M) = λ i, e.symm (function.update 0 i 1) := funext $ λ i, e.injective $ funext $ λ j, by simp [basis.of_equiv_fun, ←finsupp.single_eq_pi_single, finsupp.single_eq_update] @[simp] lemma basis.of_equiv_fun_equiv_fun (v : basis ι R M) : basis.of_equiv_fun v.equiv_fun = v := begin ext j, simp only [basis.equiv_fun_symm_apply, basis.coe_of_equiv_fun], simp_rw [function.update_apply, ite_smul], simp only [finset.mem_univ, if_true, pi.zero_apply, one_smul, finset.sum_ite_eq', zero_smul], end variables (S : Type*) [semiring S] [module S M'] variables [smul_comm_class R S M'] @[simp] theorem basis.constr_apply_fintype (f : ι → M') (x : M) : (b.constr S f : M → M') x = ∑ i, (b.equiv_fun x i) • f i := by simp [b.constr_apply, b.equiv_fun_apply, finsupp.sum_fintype] end fintype end module section comm_semiring namespace basis variables [comm_semiring R] variables [add_comm_monoid M] [module R M] [add_comm_monoid M'] [module R M'] variables (b : basis ι R M) (b' : basis ι' R M') /-- If `b` is a basis for `M` and `b'` a basis for `M'`, and `f`, `g` form a bijection between the basis vectors, `b.equiv' b' f g hf hg hgf hfg` is a linear equivalence `M ≃ₗ[R] M'`, mapping `b i` to `f (b i)`. -/ def equiv' (f : M → M') (g : M' → M) (hf : ∀ i, f (b i) ∈ range b') (hg : ∀ i, g (b' i) ∈ range b) (hgf : ∀i, g (f (b i)) = b i) (hfg : ∀i, f (g (b' i)) = b' i) : M ≃ₗ[R] M' := { inv_fun := b'.constr R (g ∘ b'), left_inv := have (b'.constr R (g ∘ b')).comp (b.constr R (f ∘ b)) = linear_map.id, from (b.ext $ λ i, exists.elim (hf i) (λ i' hi', by rw [linear_map.comp_apply, b.constr_basis, function.comp_apply, ← hi', b'.constr_basis, function.comp_apply, hi', hgf, linear_map.id_apply])), λ x, congr_arg (λ (h : M →ₗ[R] M), h x) this, right_inv := have (b.constr R (f ∘ b)).comp (b'.constr R (g ∘ b')) = linear_map.id, from (b'.ext $ λ i, exists.elim (hg i) (λ i' hi', by rw [linear_map.comp_apply, b'.constr_basis, function.comp_apply, ← hi', b.constr_basis, function.comp_apply, hi', hfg, linear_map.id_apply])), λ x, congr_arg (λ (h : M' →ₗ[R] M'), h x) this, .. b.constr R (f ∘ b) } @[simp] lemma equiv'_apply (f : M → M') (g : M' → M) (hf hg hgf hfg) (i : ι) : b.equiv' b' f g hf hg hgf hfg (b i) = f (b i) := b.constr_basis R _ _ @[simp] lemma equiv'_symm_apply (f : M → M') (g : M' → M) (hf hg hgf hfg) (i : ι') : (b.equiv' b' f g hf hg hgf hfg).symm (b' i) = g (b' i) := b'.constr_basis R _ _ lemma sum_repr_mul_repr {ι'} [fintype ι'] (b' : basis ι' R M) (x : M) (i : ι) : ∑ (j : ι'), b.repr (b' j) i * b'.repr x j = b.repr x i := begin conv_rhs { rw [← b'.sum_repr x] }, simp_rw [linear_equiv.map_sum, linear_equiv.map_smul, finset.sum_apply'], refine finset.sum_congr rfl (λ j _, _), rw [finsupp.smul_apply, smul_eq_mul, mul_comm] end end basis end comm_semiring section module open linear_map variables {v : ι → M} variables [ring R] [add_comm_group M] [add_comm_group M'] [add_comm_group M''] variables [module R M] [module R M'] [module R M''] variables {c d : R} {x y : M} variables (b : basis ι R M) namespace basis /-- Any basis is a maximal linear independent set. -/ lemma maximal [nontrivial R] (b : basis ι R M) : b.linear_independent.maximal := λ w hi h, begin -- If `range w` is strictly bigger than `range b`, apply le_antisymm h, -- then choose some `x ∈ range w \ range b`, intros x p, by_contradiction q, -- and write it in terms of the basis. have e := b.total_repr x, -- This then expresses `x` as a linear combination -- of elements of `w` which are in the range of `b`, let u : ι ↪ w := ⟨λ i, ⟨b i, h ⟨i, rfl⟩⟩, λ i i' r, b.injective (by simpa only [subtype.mk_eq_mk] using r)⟩, have r : ∀ i, b i = u i := λ i, rfl, simp_rw [finsupp.total_apply, r] at e, change (b.repr x).sum (λ (i : ι) (a : R), (λ (x : w) (r : R), r • (x : M)) (u i) a) = ((⟨x, p⟩ : w) : M) at e, rw [←finsupp.sum_emb_domain, ←finsupp.total_apply] at e, -- Now we can contradict the linear independence of `hi` refine hi.total_ne_of_not_mem_support _ _ e, simp only [finset.mem_map, finsupp.support_emb_domain], rintro ⟨j, -, W⟩, simp only [embedding.coe_fn_mk, subtype.mk_eq_mk, ←r] at W, apply q ⟨j, W⟩, end section mk variables (hli : linear_independent R v) (hsp : span R (range v) = ⊤) /-- A linear independent family of vectors spanning the whole module is a basis. -/ protected noncomputable def mk : basis ι R M := basis.of_repr { inv_fun := finsupp.total _ _ _ v, left_inv := λ x, hli.total_repr ⟨x, _⟩, right_inv := λ x, hli.repr_eq rfl, .. hli.repr.comp (linear_map.id.cod_restrict _ (λ h, hsp.symm ▸ submodule.mem_top)) } @[simp] lemma mk_repr : (basis.mk hli hsp).repr x = hli.repr ⟨x, hsp.symm ▸ submodule.mem_top⟩ := rfl lemma mk_apply (i : ι) : basis.mk hli hsp i = v i := show finsupp.total _ _ _ v _ = v i, by simp @[simp] lemma coe_mk : ⇑(basis.mk hli hsp) = v := funext (mk_apply _ _) variables {hli hsp} /-- Given a basis, the `i`th element of the dual basis evaluates to 1 on the `i`th element of the basis. -/ lemma mk_coord_apply_eq (i : ι) : (basis.mk hli hsp).coord i (v i) = 1 := show hli.repr ⟨v i, submodule.subset_span (mem_range_self i)⟩ i = 1, by simp [hli.repr_eq_single i] /-- Given a basis, the `i`th element of the dual basis evaluates to 0 on the `j`th element of the basis if `j ≠ i`. -/ lemma mk_coord_apply_ne {i j : ι} (h : j ≠ i) : (basis.mk hli hsp).coord i (v j) = 0 := show hli.repr ⟨v j, submodule.subset_span (mem_range_self j)⟩ i = 0, by simp [hli.repr_eq_single j, h] /-- Given a basis, the `i`th element of the dual basis evaluates to the Kronecker delta on the `j`th element of the basis. -/ lemma mk_coord_apply {i j : ι} : (basis.mk hli hsp).coord i (v j) = if j = i then 1 else 0 := begin cases eq_or_ne j i, { simp only [h, if_true, eq_self_iff_true, mk_coord_apply_eq i], }, { simp only [h, if_false, mk_coord_apply_ne h], }, end end mk section span variables (hli : linear_independent R v) /-- A linear independent family of vectors is a basis for their span. -/ protected noncomputable def span : basis ι R (span R (range v)) := basis.mk (linear_independent_span hli) $ begin rw eq_top_iff, intros x _, have h₁ : (coe : span R (range v) → M) '' set.range (λ i, subtype.mk (v i) _) = range v, { rw ← set.range_comp, refl }, have h₂ : map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))) = span R (range v), { rw [← span_image, submodule.coe_subtype, h₁] }, have h₃ : (x : M) ∈ map (submodule.subtype _) (span R (set.range (λ i, subtype.mk (v i) _))), { rw h₂, apply subtype.mem x }, rcases mem_map.1 h₃ with ⟨y, hy₁, hy₂⟩, have h_x_eq_y : x = y, { rw [subtype.ext_iff, ← hy₂], simp }, rwa h_x_eq_y end end span lemma group_smul_span_eq_top {G : Type*} [group G] [distrib_mul_action G R] [distrib_mul_action G M] [is_scalar_tower G R M] {v : ι → M} (hv : submodule.span R (set.range v) = ⊤) {w : ι → G} : submodule.span R (set.range (w • v)) = ⊤ := begin rw eq_top_iff, intros j hj, rw ← hv at hj, rw submodule.mem_span at hj ⊢, refine λ p hp, hj p (λ u hu, _), obtain ⟨i, rfl⟩ := hu, have : ((w i)⁻¹ • 1 : R) • w i • v i ∈ p := p.smul_mem ((w i)⁻¹ • 1 : R) (hp ⟨i, rfl⟩), rwa [smul_one_smul, inv_smul_smul] at this, end /-- Given a basis `v` and a map `w` such that for all `i`, `w i` are elements of a group, `group_smul` provides the basis corresponding to `w • v`. -/ def group_smul {G : Type*} [group G] [distrib_mul_action G R] [distrib_mul_action G M] [is_scalar_tower G R M] [smul_comm_class G R M] (v : basis ι R M) (w : ι → G) : basis ι R M := @basis.mk ι R M (w • v) _ _ _ (v.linear_independent.group_smul w) (group_smul_span_eq_top v.span_eq) lemma group_smul_apply {G : Type*} [group G] [distrib_mul_action G R] [distrib_mul_action G M] [is_scalar_tower G R M] [smul_comm_class G R M] {v : basis ι R M} {w : ι → G} (i : ι) : v.group_smul w i = (w • v : ι → M) i := mk_apply (v.linear_independent.group_smul w) (group_smul_span_eq_top v.span_eq) i lemma units_smul_span_eq_top {v : ι → M} (hv : submodule.span R (set.range v) = ⊤) {w : ι → Rˣ} : submodule.span R (set.range (w • v)) = ⊤ := group_smul_span_eq_top hv /-- Given a basis `v` and a map `w` such that for all `i`, `w i` is a unit, `smul_of_is_unit` provides the basis corresponding to `w • v`. -/ def units_smul (v : basis ι R M) (w : ι → Rˣ) : basis ι R M := @basis.mk ι R M (w • v) _ _ _ (v.linear_independent.units_smul w) (units_smul_span_eq_top v.span_eq) lemma units_smul_apply {v : basis ι R M} {w : ι → Rˣ} (i : ι) : v.units_smul w i = w i • v i := mk_apply (v.linear_independent.units_smul w) (units_smul_span_eq_top v.span_eq) i /-- A version of `smul_of_units` that uses `is_unit`. -/ def is_unit_smul (v : basis ι R M) {w : ι → R} (hw : ∀ i, is_unit (w i)): basis ι R M := units_smul v (λ i, (hw i).unit) lemma is_unit_smul_apply {v : basis ι R M} {w : ι → R} (hw : ∀ i, is_unit (w i)) (i : ι) : v.is_unit_smul hw i = w i • v i := units_smul_apply i section fin /-- Let `b` be a basis for a submodule `N` of `M`. If `y : M` is linear independent of `N` and `y` and `N` together span the whole of `M`, then there is a basis for `M` whose basis vectors are given by `fin.cons y b`. -/ noncomputable def mk_fin_cons {n : ℕ} {N : submodule R M} (y : M) (b : basis (fin n) R N) (hli : ∀ (c : R) (x ∈ N), c • y + x = 0 → c = 0) (hsp : ∀ (z : M), ∃ (c : R), z + c • y ∈ N) : basis (fin (n + 1)) R M := have span_b : submodule.span R (set.range (N.subtype ∘ b)) = N, { rw [set.range_comp, submodule.span_image, b.span_eq, submodule.map_subtype_top] }, @basis.mk _ _ _ (fin.cons y (N.subtype ∘ b) : fin (n + 1) → M) _ _ _ ((b.linear_independent.map' N.subtype (submodule.ker_subtype _)) .fin_cons' _ _ $ by { rintros c ⟨x, hx⟩ hc, rw span_b at hx, exact hli c x hx hc }) (eq_top_iff.mpr (λ x _, by { rw [fin.range_cons, submodule.mem_span_insert', span_b], exact hsp x })) @[simp] lemma coe_mk_fin_cons {n : ℕ} {N : submodule R M} (y : M) (b : basis (fin n) R N) (hli : ∀ (c : R) (x ∈ N), c • y + x = 0 → c = 0) (hsp : ∀ (z : M), ∃ (c : R), z + c • y ∈ N) : (mk_fin_cons y b hli hsp : fin (n + 1) → M) = fin.cons y (coe ∘ b) := coe_mk _ _ /-- Let `b` be a basis for a submodule `N ≤ O`. If `y ∈ O` is linear independent of `N` and `y` and `N` together span the whole of `O`, then there is a basis for `O` whose basis vectors are given by `fin.cons y b`. -/ noncomputable def mk_fin_cons_of_le {n : ℕ} {N O : submodule R M} (y : M) (yO : y ∈ O) (b : basis (fin n) R N) (hNO : N ≤ O) (hli : ∀ (c : R) (x ∈ N), c • y + x = 0 → c = 0) (hsp : ∀ (z ∈ O), ∃ (c : R), z + c • y ∈ N) : basis (fin (n + 1)) R O := mk_fin_cons ⟨y, yO⟩ (b.map (submodule.comap_subtype_equiv_of_le hNO).symm) (λ c x hc hx, hli c x (submodule.mem_comap.mp hc) (congr_arg coe hx)) (λ z, hsp z z.2) @[simp] lemma coe_mk_fin_cons_of_le {n : ℕ} {N O : submodule R M} (y : M) (yO : y ∈ O) (b : basis (fin n) R N) (hNO : N ≤ O) (hli : ∀ (c : R) (x ∈ N), c • y + x = 0 → c = 0) (hsp : ∀ (z ∈ O), ∃ (c : R), z + c • y ∈ N) : (mk_fin_cons_of_le y yO b hNO hli hsp : fin (n + 1) → O) = fin.cons ⟨y, yO⟩ (submodule.of_le hNO ∘ b) := coe_mk_fin_cons _ _ _ _ end fin end basis end module section induction variables [ring R] [is_domain R] variables [add_comm_group M] [module R M] {b : ι → M} /-- If `N` is a submodule with finite rank, do induction on adjoining a linear independent element to a submodule. -/ def submodule.induction_on_rank_aux (b : basis ι R M) (P : submodule R M → Sort*) (ih : ∀ (N : submodule R M), (∀ (N' ≤ N) (x ∈ N), (∀ (c : R) (y ∈ N'), c • x + y = (0 : M) → c = 0) → P N') → P N) (n : ℕ) (N : submodule R M) (rank_le : ∀ {m : ℕ} (v : fin m → N), linear_independent R (coe ∘ v : fin m → M) → m ≤ n) : P N := begin haveI : decidable_eq M := classical.dec_eq M, have Pbot : P ⊥, { apply ih, intros N N_le x x_mem x_ortho, exfalso, simpa using x_ortho 1 0 N.zero_mem }, induction n with n rank_ih generalizing N, { suffices : N = ⊥, { rwa this }, apply eq_bot_of_rank_eq_zero b _ (λ m v hv, nat.le_zero_iff.mp (rank_le v hv)) }, apply ih, intros N' N'_le x x_mem x_ortho, apply rank_ih, intros m v hli, refine nat.succ_le_succ_iff.mp (rank_le (fin.cons ⟨x, x_mem⟩ (λ i, ⟨v i, N'_le (v i).2⟩)) _), convert hli.fin_cons' x _ _, { ext i, refine fin.cases _ _ i; simp }, { intros c y hcy, refine x_ortho c y (submodule.span_le.mpr _ y.2) hcy, rintros _ ⟨z, rfl⟩, exact (v z).2 } end end induction section division_ring variables [division_ring K] [add_comm_group V] [add_comm_group V'] [module K V] [module K V'] variables {v : ι → V} {s t : set V} {x y z : V} include K open submodule namespace basis section exists_basis /-- If `s` is a linear independent set of vectors, we can extend it to a basis. -/ noncomputable def extend (hs : linear_independent K (coe : s → V)) : basis _ K V := basis.mk (@linear_independent.restrict_of_comp_subtype _ _ _ id _ _ _ _ (hs.linear_independent_extend _)) (eq_top_iff.mpr $ set_like.coe_subset_coe.mp $ by simpa using hs.subset_span_extend (subset_univ s)) lemma extend_apply_self (hs : linear_independent K (coe : s → V)) (x : hs.extend _) : basis.extend hs x = x := basis.mk_apply _ _ _ @[simp] lemma coe_extend (hs : linear_independent K (coe : s → V)) : ⇑(basis.extend hs) = coe := funext (extend_apply_self hs) lemma range_extend (hs : linear_independent K (coe : s → V)) : range (basis.extend hs) = hs.extend (subset_univ _) := by rw [coe_extend, subtype.range_coe_subtype, set_of_mem_eq] /-- If `v` is a linear independent family of vectors, extend it to a basis indexed by a sum type. -/ noncomputable def sum_extend (hs : linear_independent K v) : basis (ι ⊕ _) K V := let s := set.range v, e : ι ≃ s := equiv.of_injective v hs.injective, b := hs.to_subtype_range.extend (subset_univ (set.range v)) in (basis.extend hs.to_subtype_range).reindex $ equiv.symm $ calc ι ⊕ (b \ s : set V) ≃ s ⊕ (b \ s : set V) : equiv.sum_congr e (equiv.refl _) ... ≃ b : equiv.set.sum_diff_subset (hs.to_subtype_range.subset_extend _) lemma subset_extend {s : set V} (hs : linear_independent K (coe : s → V)) : s ⊆ hs.extend (set.subset_univ _) := hs.subset_extend _ section variables (K V) /-- A set used to index `basis.of_vector_space`. -/ noncomputable def of_vector_space_index : set V := (linear_independent_empty K V).extend (subset_univ _) /-- Each vector space has a basis. -/ noncomputable def of_vector_space : basis (of_vector_space_index K V) K V := basis.extend (linear_independent_empty K V) lemma of_vector_space_apply_self (x : of_vector_space_index K V) : of_vector_space K V x = x := basis.mk_apply _ _ _ @[simp] lemma coe_of_vector_space : ⇑(of_vector_space K V) = coe := funext (λ x, of_vector_space_apply_self K V x) lemma of_vector_space_index.linear_independent : linear_independent K (coe : of_vector_space_index K V → V) := by { convert (of_vector_space K V).linear_independent, ext x, rw of_vector_space_apply_self } lemma range_of_vector_space : range (of_vector_space K V) = of_vector_space_index K V := range_extend _ lemma exists_basis : ∃ s : set V, nonempty (basis s K V) := ⟨of_vector_space_index K V, ⟨of_vector_space K V⟩⟩ end end exists_basis end basis open fintype variables (K V) theorem vector_space.card_fintype [fintype K] [fintype V] : ∃ n : ℕ, card V = (card K) ^ n := ⟨card (basis.of_vector_space_index K V), module.card_fintype (basis.of_vector_space K V)⟩ end division_ring section field variables [field K] [add_comm_group V] [add_comm_group V'] [module K V] [module K V'] variables {v : ι → V} {s t : set V} {x y z : V} lemma linear_map.exists_left_inverse_of_injective (f : V →ₗ[K] V') (hf_inj : f.ker = ⊥) : ∃g:V' →ₗ[K] V, g.comp f = linear_map.id := begin let B := basis.of_vector_space_index K V, let hB := basis.of_vector_space K V, have hB₀ : _ := hB.linear_independent.to_subtype_range, have : linear_independent K (λ x, x : f '' B → V'), { have h₁ : linear_independent K (λ (x : ↥(⇑f '' range (basis.of_vector_space _ _))), ↑x) := @linear_independent.image_subtype _ _ _ _ _ _ _ _ _ f hB₀ (show disjoint _ _, by simp [hf_inj]), rwa [basis.range_of_vector_space K V] at h₁ }, let C := this.extend (subset_univ _), have BC := this.subset_extend (subset_univ _), let hC := basis.extend this, haveI : inhabited V := ⟨0⟩, refine ⟨hC.constr K (C.restrict (inv_fun f)), hB.ext (λ b, _)⟩, rw image_subset_iff at BC, have fb_eq : f b = hC ⟨f b, BC b.2⟩, { change f b = basis.extend this _, rw [basis.extend_apply_self, subtype.coe_mk] }, dsimp [hB], rw [basis.of_vector_space_apply_self, fb_eq, hC.constr_basis], exact left_inverse_inv_fun (linear_map.ker_eq_bot.1 hf_inj) _ end lemma submodule.exists_is_compl (p : submodule K V) : ∃ q : submodule K V, is_compl p q := let ⟨f, hf⟩ := p.subtype.exists_left_inverse_of_injective p.ker_subtype in ⟨f.ker, linear_map.is_compl_of_proj $ linear_map.ext_iff.1 hf⟩ instance module.submodule.is_complemented : is_complemented (submodule K V) := ⟨submodule.exists_is_compl⟩ lemma linear_map.exists_right_inverse_of_surjective (f : V →ₗ[K] V') (hf_surj : f.range = ⊤) : ∃g:V' →ₗ[K] V, f.comp g = linear_map.id := begin let C := basis.of_vector_space_index K V', let hC := basis.of_vector_space K V', haveI : inhabited V := ⟨0⟩, use hC.constr K (C.restrict (inv_fun f)), refine hC.ext (λ c, _), rw [linear_map.comp_apply, hC.constr_basis], simp [right_inverse_inv_fun (linear_map.range_eq_top.1 hf_surj) c] end /-- Any linear map `f : p →ₗ[K] V'` defined on a subspace `p` can be extended to the whole space. -/ lemma linear_map.exists_extend {p : submodule K V} (f : p →ₗ[K] V') : ∃ g : V →ₗ[K] V', g.comp p.subtype = f := let ⟨g, hg⟩ := p.subtype.exists_left_inverse_of_injective p.ker_subtype in ⟨f.comp g, by rw [linear_map.comp_assoc, hg, f.comp_id]⟩ open submodule linear_map /-- If `p < ⊤` is a subspace of a vector space `V`, then there exists a nonzero linear map `f : V →ₗ[K] K` such that `p ≤ ker f`. -/ lemma submodule.exists_le_ker_of_lt_top (p : submodule K V) (hp : p < ⊤) : ∃ f ≠ (0 : V →ₗ[K] K), p ≤ ker f := begin rcases set_like.exists_of_lt hp with ⟨v, -, hpv⟩, clear hp, rcases (linear_pmap.sup_span_singleton ⟨p, 0⟩ v (1 : K) hpv).to_fun.exists_extend with ⟨f, hf⟩, refine ⟨f, _, _⟩, { rintro rfl, rw [linear_map.zero_comp] at hf, have := linear_pmap.sup_span_singleton_apply_mk ⟨p, 0⟩ v (1 : K) hpv 0 p.zero_mem 1, simpa using (linear_map.congr_fun hf _).trans this }, { refine λ x hx, mem_ker.2 _, have := linear_pmap.sup_span_singleton_apply_mk ⟨p, 0⟩ v (1 : K) hpv x hx 0, simpa using (linear_map.congr_fun hf _).trans this } end theorem quotient_prod_linear_equiv (p : submodule K V) : nonempty (((V ⧸ p) × p) ≃ₗ[K] V) := let ⟨q, hq⟩ := p.exists_is_compl in nonempty.intro $ ((quotient_equiv_of_is_compl p q hq).prod (linear_equiv.refl _ _)).trans (prod_equiv_of_is_compl q p hq.symm) end field
e75e7ca3d1ad50792008706659ee99b9afac43cc
2a70b774d16dbdf5a533432ee0ebab6838df0948
/_target/deps/mathlib/src/tactic/lint/type_classes.lean
d0c7b521723761320454ff7dcc711e691d7b5495
[ "Apache-2.0" ]
permissive
hjvromen/lewis
40b035973df7c77ebf927afab7878c76d05ff758
105b675f73630f028ad5d890897a51b3c1146fb0
refs/heads/master
1,677,944,636,343
1,676,555,301,000
1,676,555,301,000
327,553,599
0
0
null
null
null
null
UTF-8
Lean
false
false
17,088
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.basic /-! # Linters about type classes This file defines several linters checking the correct usage of type classes and the appropriate definition of instances: * `instance_priority` ensures that blanket instances have low priority * `has_inhabited_instances` checks that every type has an `inhabited` instance * `impossible_instance` checks that there are no instances which can never apply * `incorrect_type_class_argument` checks that only type classes are used in instance-implicit arguments * `dangerous_instance` checks for instances that generate subproblems with metavariables * `fails_quickly` checks that type class resolution finishes quickly * `has_coe_variable` checks that there is no instance of type `has_coe α t` * `inhabited_nonempty` checks whether `[inhabited α]` arguments could be generalized to `[nonempty α]` * `decidable_classical` checks propositions for `[decidable_... p]` hypotheses that are not used in the statement, and could thus be removed by using `classical` in the proof. * `linter.has_coe_to_fun` checks whether necessary `has_coe_to_fun` instances are declared -/ open tactic /-- Pretty prints a list of arguments of a declaration. Assumes `l` is a list of argument positions and binders (or any other element that can be pretty printed). `l` can be obtained e.g. by applying `list.indexes_values` to a list obtained by `get_pi_binders`. -/ meta def print_arguments {α} [has_to_tactic_format α] (l : list (ℕ × α)) : tactic string := do fs ← l.mmap (λ ⟨n, b⟩, (λ s, to_fmt "argument " ++ to_fmt (n+1) ++ ": " ++ s) <$> pp b), return $ fs.to_string_aux tt /-- checks whether an instance that always applies has priority ≥ 1000. -/ private meta def instance_priority (d : declaration) : tactic (option string) := do let nm := d.to_name, b ← is_instance nm, /- return `none` if `d` is not an instance -/ if ¬ b then return none else do (is_persistent, prio) ← has_attribute `instance nm, /- return `none` if `d` is has low priority -/ if prio < 1000 then return none else do let (fn, args) := d.type.pi_codomain.get_app_fn_args, cls ← get_decl fn.const_name, let (pi_args, _) := cls.type.pi_binders, guard (args.length = pi_args.length), /- List all the arguments of the class that block type-class inference from firing (if they are metavariables). These are all the arguments except instance-arguments and out-params. -/ let relevant_args := (args.zip pi_args).filter_map $ λ⟨e, ⟨_, info, tp⟩⟩, if info = binder_info.inst_implicit ∨ tp.get_app_fn.is_constant_of `out_param then none else some e, let always_applies := relevant_args.all expr.is_var ∧ relevant_args.nodup, if always_applies then return $ some "set priority below 1000" else return none /-- There are places where typeclass arguments are specified with implicit `{}` brackets instead of the usual `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type of one of the other arguments. When they can be inferred from these other arguments, it is faster to use this method than to use type class inference. For example, when writing lemmas about `(f : α →+* β)`, it is faster to specify the fact that `α` and `β` are `semiring`s as `{rα : semiring α} {rβ : semiring β}` rather than the usual `[semiring α] [semiring β]`. -/ library_note "implicit instance arguments" /-- Certain instances always apply during type-class resolution. For example, the instance `add_comm_group.to_add_group {α} [add_comm_group α] : add_group α` applies to all type-class resolution problems of the form `add_group _`, and type-class inference will then do an exhaustive search to find a commutative group. These instances take a long time to fail. Other instances will only apply if the goal has a certain shape. For example `int.add_group : add_group ℤ` or `add_group.prod {α β} [add_group α] [add_group β] : add_group (α × β)`. Usually these instances will fail quickly, and when they apply, they are almost the desired instance. For this reason, we want the instances of the second type (that only apply in specific cases) to always have higher priority than the instances of the first type (that always apply). See also #1561. Therefore, if we create an instance that always applies, we set the priority of these instances to 100 (or something similar, which is below the default value of 1000). -/ library_note "lower instance priority" /-- A linter object for checking instance priorities of instances that always apply. This is in the default linter set. -/ @[linter] meta def linter.instance_priority : linter := { test := instance_priority, no_errors_found := "All instance priorities are good", errors_found := "DANGEROUS INSTANCE PRIORITIES. The following instances always apply, and therefore should have a priority < 1000. If you don't know what priority to choose, use priority 100. See note [lower instance priority] for instructions to change the priority.", auto_decls := tt } /-- Reports declarations of types that do not have an associated `inhabited` instance. -/ private meta def has_inhabited_instance (d : declaration) : tactic (option string) := do tt ← pure d.is_trusted | pure none, ff ← has_attribute' `reducible d.to_name | pure none, ff ← has_attribute' `class d.to_name | pure none, (_, ty) ← open_pis d.type, ty ← whnf ty, if ty = `(Prop) then pure none else do `(Sort _) ← whnf ty | pure none, insts ← attribute.get_instances `instance, insts_tys ← insts.mmap $ λ i, expr.pi_codomain <$> declaration.type <$> get_decl i, let inhabited_insts := insts_tys.filter (λ i, i.app_fn.const_name = ``inhabited ∨ i.app_fn.const_name = `unique), let inhabited_tys := inhabited_insts.map (λ i, i.app_arg.get_app_fn.const_name), if d.to_name ∈ inhabited_tys then pure none else pure "inhabited instance missing" /-- A linter for missing `inhabited` instances. -/ @[linter] meta def linter.has_inhabited_instance : linter := { test := has_inhabited_instance, auto_decls := ff, no_errors_found := "No types have missing inhabited instances", errors_found := "TYPES ARE MISSING INHABITED INSTANCES", is_fast := ff } attribute [nolint has_inhabited_instance] pempty /-- Checks whether an instance can never be applied. -/ private meta def impossible_instance (d : declaration) : tactic (option string) := do tt ← is_instance d.to_name | return none, (binders, _) ← get_pi_binders_nondep d.type, let bad_arguments := binders.filter $ λ nb, nb.2.info ≠ binder_info.inst_implicit, _ :: _ ← return bad_arguments | return none, (λ s, some $ "Impossible to infer " ++ s) <$> print_arguments bad_arguments /-- A linter object for `impossible_instance`. -/ @[linter] meta def linter.impossible_instance : linter := { test := impossible_instance, auto_decls := tt, no_errors_found := "All instances are applicable", errors_found := "IMPOSSIBLE INSTANCES FOUND. These instances have an argument that cannot be found during type-class resolution, and therefore can never succeed. Either mark the arguments with square brackets (if it is a class), or don't make it an instance" } /-- Checks whether an instance can never be applied. -/ private meta def incorrect_type_class_argument (d : declaration) : tactic (option string) := do (binders, _) ← get_pi_binders d.type, let instance_arguments := binders.indexes_values $ λ b : binder, b.info = binder_info.inst_implicit, /- the head of the type should either unfold to a class, or be a local constant. A local constant is allowed, because that could be a class when applied to the proper arguments. -/ bad_arguments ← instance_arguments.mfilter (λ ⟨_, b⟩, do (_, head) ← open_pis b.type, if head.get_app_fn.is_local_constant then return ff else do bnot <$> is_class head), _ :: _ ← return bad_arguments | return none, (λ s, some $ "These are not classes. " ++ s) <$> print_arguments bad_arguments /-- A linter object for `incorrect_type_class_argument`. -/ @[linter] meta def linter.incorrect_type_class_argument : linter := { test := incorrect_type_class_argument, auto_decls := tt, no_errors_found := "All declarations have correct type-class arguments", errors_found := "INCORRECT TYPE-CLASS ARGUMENTS. Some declarations have non-classes between [square brackets]" } /-- Checks whether an instance is dangerous: it creates a new type-class problem with metavariable arguments. -/ private meta def dangerous_instance (d : declaration) : tactic (option string) := do tt ← is_instance d.to_name | return none, (local_constants, target) ← open_pis d.type, let instance_arguments := local_constants.indexes_values $ λ e : expr, e.local_binding_info = binder_info.inst_implicit, let bad_arguments := local_constants.indexes_values $ λ x, !target.has_local_constant x && (x.local_binding_info ≠ binder_info.inst_implicit) && instance_arguments.any (λ nb, nb.2.local_type.has_local_constant x), let bad_arguments : list (ℕ × binder) := bad_arguments.map $ λ ⟨n, e⟩, ⟨n, e.to_binder⟩, _ :: _ ← return bad_arguments | return none, (λ s, some $ "The following arguments become metavariables. " ++ s) <$> print_arguments bad_arguments /-- A linter object for `dangerous_instance`. -/ @[linter] meta def linter.dangerous_instance : linter := { test := dangerous_instance, no_errors_found := "No dangerous instances", errors_found := "DANGEROUS INSTANCES FOUND.\nThese instances are recursive, and create a new type-class problem which will have metavariables. Possible solution: remove the instance attribute or make it a local instance instead. Currently this linter does not check whether the metavariables only occur in arguments marked with `out_param`, in which case this linter gives a false positive.", auto_decls := tt } /-- Applies expression `e` to local constants, but lifts all the arguments that are `Sort`-valued to `Type`-valued sorts. -/ meta def apply_to_fresh_variables (e : expr) : tactic expr := do t ← infer_type e, (xs, b) ← open_pis t, xs.mmap' $ λ x, try $ do { u ← mk_meta_univ, tx ← infer_type x, ttx ← infer_type tx, unify ttx (expr.sort u.succ) }, return $ e.app_of_list xs /-- Tests whether type-class inference search for a class will end quickly when applied to variables. This tactic succeeds if `mk_instance` succeeds quickly or fails quickly with the error message that it cannot find an instance. It fails if the tactic takes too long, or if any other error message is raised. We make sure that we apply the tactic to variables living in `Type u` instead of `Sort u`, because many instances only apply in that special case, and we do want to catch those loops. -/ meta def fails_quickly (max_steps : ℕ) (d : declaration) : tactic (option string) := do e ← mk_const d.to_name, tt ← is_class e | return none, e' ← apply_to_fresh_variables e, sum.inr msg ← retrieve_or_report_error $ tactic.try_for max_steps $ succeeds_or_fails_with_msg (mk_instance e') $ λ s, "tactic.mk_instance failed to generate instance for".is_prefix_of s | return none, return $ some $ if msg = "try_for tactic failed, timeout" then "type-class inference timed out" else msg /-- A linter object for `fails_quickly`. If we want to increase the maximum number of steps type-class inference is allowed to take, we can increase the number `3000` in the definition. As of 5 Mar 2020 the longest trace (for `is_add_hom`) takes 2900-3000 "heartbeats". -/ @[linter] meta def linter.fails_quickly : linter := { test := fails_quickly 3000, auto_decls := tt, no_errors_found := "No type-class searches timed out", errors_found := "TYPE CLASS SEARCHES TIMED OUT. For the following classes, there is an instance that causes a loop, or an excessively long search.", is_fast := ff } /-- Tests whether there is no instance of type `has_coe α t` where `α` is a variable, or `has_coe t α` where `α` does not occur in `t`. See note [use has_coe_t]. -/ private meta def has_coe_variable (d : declaration) : tactic (option string) := do tt ← is_instance d.to_name | return none, `(has_coe %%a %%b) ← return d.type.pi_codomain | return none, if a.is_var then return $ some $ "illegal instance, first argument is variable" else if b.is_var ∧ ¬ b.occurs a then return $ some $ "illegal instance, second argument is variable not occurring in first argument" else return none /-- A linter object for `has_coe_variable`. -/ @[linter] meta def linter.has_coe_variable : linter := { test := has_coe_variable, auto_decls := tt, no_errors_found := "No invalid `has_coe` instances", errors_found := "INVALID `has_coe` INSTANCES. Make the following declarations instances of the class `has_coe_t` instead of `has_coe`." } /-- Checks whether a declaration is prop-valued and takes an `inhabited _` argument that is unused elsewhere in the type. In this case, that argument can be replaced with `nonempty _`. -/ private meta def inhabited_nonempty (d : declaration) : tactic (option string) := do tt ← is_prop d.type | return none, (binders, _) ← get_pi_binders_nondep d.type, let inhd_binders := binders.filter $ λ pr, pr.2.type.is_app_of `inhabited, if inhd_binders.length = 0 then return none else (λ s, some $ "The following `inhabited` instances should be `nonempty`. " ++ s) <$> print_arguments inhd_binders /-- A linter object for `inhabited_nonempty`. -/ @[linter] meta def linter.inhabited_nonempty : linter := { test := inhabited_nonempty, auto_decls := ff, no_errors_found := "No uses of `inhabited` arguments should be replaced with `nonempty`", errors_found := "USES OF `inhabited` SHOULD BE REPLACED WITH `nonempty`." } /-- Checks whether a declaration is `Prop`-valued and takes a `decidable* _` hypothesis that is unused elsewhere in the type. In this case, that hypothesis can be replaced with `classical` in the proof. Theorems in the `decidable` namespace are exempt from the check. -/ private meta def decidable_classical (d : declaration) : tactic (option string) := do tt ← is_prop d.type | return none, ff ← pure $ (`decidable).is_prefix_of d.to_name | return none, (binders, _) ← get_pi_binders_nondep d.type, let deceq_binders := binders.filter $ λ pr, pr.2.type.is_app_of `decidable_eq ∨ pr.2.type.is_app_of `decidable_pred ∨ pr.2.type.is_app_of `decidable_rel ∨ pr.2.type.is_app_of `decidable, if deceq_binders.length = 0 then return none else (λ s, some $ "The following `decidable` hypotheses should be replaced with `classical` in the proof. " ++ s) <$> print_arguments deceq_binders /-- A linter object for `decidable_classical`. -/ @[linter] meta def linter.decidable_classical : linter := { test := decidable_classical, auto_decls := ff, no_errors_found := "No uses of `decidable` arguments should be replaced with `classical`", errors_found := "USES OF `decidable` SHOULD BE REPLACED WITH `classical` IN THE PROOF." } /- The file `logic/basic.lean` emphasizes the differences between what holds under classical and non-classical logic. It makes little sense to make all these lemmas classical, so we add them to the list of lemmas which are not checked by the linter `decidable_classical`. -/ attribute [nolint decidable_classical] dec_em not.decidable_imp_symm private meta def has_coe_to_fun_linter (d : declaration) : tactic (option string) := retrieve $ do tt ← return d.is_trusted | pure none, mk_meta_var d.type >>= set_goals ∘ pure, args ← unfreezing intros, expr.sort _ ← target | pure none, let ty : expr := (expr.const d.to_name d.univ_levels).mk_app args, some coe_fn_inst ← try_core $ to_expr ``(_root_.has_coe_to_fun %%ty) >>= mk_instance | pure none, some trans_inst@(expr.app (expr.app _ trans_inst_1) trans_inst_2) ← try_core $ to_expr ``(@_root_.coe_fn_trans %%ty _ _ _) | pure none, tt ← succeeds $ unify trans_inst coe_fn_inst transparency.reducible | pure none, set_bool_option `pp.all true, trans_inst_1 ← pp trans_inst_1, trans_inst_2 ← pp trans_inst_2, pure $ format.to_string $ "`has_coe_to_fun` instance is definitionally equal to a transitive instance composed of: " ++ trans_inst_1.group.indent 2 ++ format.line ++ "and" ++ trans_inst_2.group.indent 2 /-- Linter that checks whether `has_coe_to_fun` instances comply with Note [function coercion]. -/ @[linter] meta def linter.has_coe_to_fun : linter := { test := has_coe_to_fun_linter, auto_decls := tt, no_errors_found := "has_coe_to_fun is used correctly", errors_found := "INVALID/MISSING `has_coe_to_fun` instances. You should add a `has_coe_to_fun` instance for the following types. See Note [function coercion]." }
06577b01368687f49b23a718d58f73a419e1615b
c777c32c8e484e195053731103c5e52af26a25d1
/src/algebra/ring/boolean_ring.lean
f93c74cd170fc19e90813629b6c6eda5f78f72ed
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
15,964
lean
/- Copyright (c) 2021 Bryan Gin-ge Chen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bryan Gin-ge Chen, Yaël Dillies -/ import algebra.punit_instances import tactic.abel import tactic.ring import order.hom.lattice /-! # Boolean rings A Boolean ring is a ring where multiplication is idempotent. They are equivalent to Boolean algebras. ## Main declarations * `boolean_ring`: a typeclass for rings where multiplication is idempotent. * `boolean_ring.to_boolean_algebra`: Turn a Boolean ring into a Boolean algebra. * `boolean_algebra.to_boolean_ring`: Turn a Boolean algebra into a Boolean ring. * `as_boolalg`: Type-synonym for the Boolean algebra associated to a Boolean ring. * `as_boolring`: Type-synonym for the Boolean ring associated to a Boolean algebra. ## Implementation notes We provide two ways of turning a Boolean algebra/ring into a Boolean ring/algebra: * Instances on the same type accessible in locales `boolean_algebra_of_boolean_ring` and `boolean_ring_of_boolean_algebra`. * Type-synonyms `as_boolalg` and `as_boolring`. At this point in time, it is not clear the first way is useful, but we keep it for educational purposes and because it is easier than dealing with `of_boolalg`/`to_boolalg`/`of_boolring`/`to_boolring` explicitly. ## Tags boolean ring, boolean algebra -/ variables {α β γ : Type*} /-- A Boolean ring is a ring where multiplication is idempotent. -/ class boolean_ring α extends ring α := (mul_self : ∀ a : α, a * a = a) section boolean_ring variables [boolean_ring α] (a b : α) instance : is_idempotent α (*) := ⟨boolean_ring.mul_self⟩ @[simp] lemma mul_self : a * a = a := boolean_ring.mul_self _ @[simp] lemma add_self : a + a = 0 := have a + a = a + a + (a + a) := calc a + a = (a+a) * (a+a) : by rw mul_self ... = a*a + a*a + (a*a + a*a) : by rw [add_mul, mul_add] ... = a + a + (a + a) : by rw mul_self, by rwa self_eq_add_left at this @[simp] lemma neg_eq : -a = a := calc -a = -a + 0 : by rw add_zero ... = -a + -a + a : by rw [←neg_add_self, add_assoc] ... = a : by rw [add_self, zero_add] lemma add_eq_zero' : a + b = 0 ↔ a = b := calc a + b = 0 ↔ a = -b : add_eq_zero_iff_eq_neg ... ↔ a = b : by rw neg_eq @[simp] lemma mul_add_mul : a*b + b*a = 0 := have a + b = a + b + (a*b + b*a) := calc a + b = (a + b) * (a + b) : by rw mul_self ... = a*a + a*b + (b*a + b*b) : by rw [add_mul, mul_add, mul_add] ... = a + a*b + (b*a + b) : by simp only [mul_self] ... = a + b + (a*b + b*a) : by abel, by rwa self_eq_add_right at this @[simp] lemma sub_eq_add : a - b = a + b := by rw [sub_eq_add_neg, add_right_inj, neg_eq] @[simp] lemma mul_one_add_self : a * (1 + a) = 0 := by rw [mul_add, mul_one, mul_self, add_self] @[priority 100] -- Note [lower instance priority] instance boolean_ring.to_comm_ring : comm_ring α := { mul_comm := λ a b, by rw [←add_eq_zero', mul_add_mul], .. (infer_instance : boolean_ring α) } end boolean_ring instance : boolean_ring punit := ⟨λ _, subsingleton.elim _ _⟩ /-! ### Turning a Boolean ring into a Boolean algebra -/ section ring_to_algebra /-- Type synonym to view a Boolean ring as a Boolean algebra. -/ def as_boolalg (α : Type*) := α /-- The "identity" equivalence between `as_boolalg α` and `α`. -/ def to_boolalg : α ≃ as_boolalg α := equiv.refl _ /-- The "identity" equivalence between `α` and `as_boolalg α`. -/ def of_boolalg : as_boolalg α ≃ α := equiv.refl _ @[simp] lemma to_boolalg_symm_eq : (@to_boolalg α).symm = of_boolalg := rfl @[simp] lemma of_boolalg_symm_eq : (@of_boolalg α).symm = to_boolalg := rfl @[simp] lemma to_boolalg_of_boolalg (a : as_boolalg α) : to_boolalg (of_boolalg a) = a := rfl @[simp] lemma of_boolalg_to_boolalg (a : α) : of_boolalg (to_boolalg a) = a := rfl @[simp] lemma to_boolalg_inj {a b : α} : to_boolalg a = to_boolalg b ↔ a = b := iff.rfl @[simp] lemma of_boolalg_inj {a b : as_boolalg α} : of_boolalg a = of_boolalg b ↔ a = b := iff.rfl instance [inhabited α] : inhabited (as_boolalg α) := ‹inhabited α› variables [boolean_ring α] [boolean_ring β] [boolean_ring γ] namespace boolean_ring /-- The join operation in a Boolean ring is `x + y + x * y`. -/ def has_sup : has_sup α := ⟨λ x y, x + y + x * y⟩ /-- The meet operation in a Boolean ring is `x * y`. -/ def has_inf : has_inf α := ⟨(*)⟩ -- Note [lower instance priority] localized "attribute [instance, priority 100] boolean_ring.has_sup" in boolean_algebra_of_boolean_ring localized "attribute [instance, priority 100] boolean_ring.has_inf" in boolean_algebra_of_boolean_ring lemma sup_comm (a b : α) : a ⊔ b = b ⊔ a := by { dsimp only [(⊔)], ring } lemma inf_comm (a b : α) : a ⊓ b = b ⊓ a := by { dsimp only [(⊓)], ring } lemma sup_assoc (a b c : α) : a ⊔ b ⊔ c = a ⊔ (b ⊔ c) := by { dsimp only [(⊔)], ring } lemma inf_assoc (a b c : α) : a ⊓ b ⊓ c = a ⊓ (b ⊓ c) := by { dsimp only [(⊓)], ring } lemma sup_inf_self (a b : α) : a ⊔ a ⊓ b = a := by { dsimp only [(⊔), (⊓)], assoc_rw [mul_self, add_self, add_zero] } lemma inf_sup_self (a b : α) : a ⊓ (a ⊔ b) = a := begin dsimp only [(⊔), (⊓)], rw [mul_add, mul_add, mul_self, ←mul_assoc, mul_self, add_assoc, add_self, add_zero] end lemma le_sup_inf_aux (a b c : α) : (a + b + a * b) * (a + c + a * c) = a + b * c + a * (b * c) := calc (a + b + a * b) * (a + c + a * c) = a * a + b * c + a * (b * c) + (a * b + (a * a) * b) + (a * c + (a * a) * c) + (a * b * c + (a * a) * b * c) : by ring ... = a + b * c + a * (b * c) : by simp only [mul_self, add_self, add_zero] lemma le_sup_inf (a b c : α) : (a ⊔ b) ⊓ (a ⊔ c) ⊔ (a ⊔ b ⊓ c) = a ⊔ b ⊓ c := by { dsimp only [(⊔), (⊓)], rw [le_sup_inf_aux, add_self, mul_self, zero_add] } /-- The Boolean algebra structure on a Boolean ring. The data is defined so that: * `a ⊔ b` unfolds to `a + b + a * b` * `a ⊓ b` unfolds to `a * b` * `a ≤ b` unfolds to `a + b + a * b = b` * `⊥` unfolds to `0` * `⊤` unfolds to `1` * `aᶜ` unfolds to `1 + a` * `a \ b` unfolds to `a * (1 + b)` -/ def to_boolean_algebra : boolean_algebra α := { le_sup_inf := le_sup_inf, top := 1, le_top := λ a, show a + 1 + a * 1 = 1, by assoc_rw [mul_one, add_comm, add_self, add_zero], bot := 0, bot_le := λ a, show 0 + a + 0 * a = a, by rw [zero_mul, zero_add, add_zero], compl := λ a, 1 + a, inf_compl_le_bot := λ a, show a*(1+a) + 0 + a*(1+a)*0 = 0, by norm_num [mul_add, mul_self, add_self], top_le_sup_compl := λ a, begin change 1 + (a + (1+a) + a*(1+a)) + 1*(a + (1+a) + a*(1+a)) = a + (1+a) + a*(1+a), norm_num [mul_add, mul_self], rw [←add_assoc, add_self], end, .. lattice.mk' sup_comm sup_assoc inf_comm inf_assoc sup_inf_self inf_sup_self } localized "attribute [instance, priority 100] boolean_ring.to_boolean_algebra" in boolean_algebra_of_boolean_ring end boolean_ring instance : boolean_algebra (as_boolalg α) := @boolean_ring.to_boolean_algebra α _ @[simp] lemma of_boolalg_top : of_boolalg (⊤ : as_boolalg α) = 1 := rfl @[simp] lemma of_boolalg_bot : of_boolalg (⊥ : as_boolalg α) = 0 := rfl @[simp] lemma of_boolalg_sup (a b : as_boolalg α) : of_boolalg (a ⊔ b) = of_boolalg a + of_boolalg b + of_boolalg a * of_boolalg b := rfl @[simp] lemma of_boolalg_inf (a b : as_boolalg α) : of_boolalg (a ⊓ b) = of_boolalg a * of_boolalg b := rfl @[simp] lemma of_boolalg_compl (a : as_boolalg α) : of_boolalg aᶜ = 1 + of_boolalg a := rfl @[simp] lemma of_boolalg_sdiff (a b : as_boolalg α) : of_boolalg (a \ b) = of_boolalg a * (1 + of_boolalg b) := rfl private lemma of_boolalg_symm_diff_aux (a b : α) : (a + b + a * b) * (1 + a * b) = a + b := calc (a + b + a * b) * (1 + a * b) = a + b + (a * b + (a * b) * (a * b)) + (a * (b * b) + (a * a) * b) : by ring ... = a + b : by simp only [mul_self, add_self, add_zero] @[simp] lemma of_boolalg_symm_diff (a b : as_boolalg α) : of_boolalg (a ∆ b) = of_boolalg a + of_boolalg b := by { rw symm_diff_eq_sup_sdiff_inf, exact of_boolalg_symm_diff_aux _ _ } @[simp] lemma of_boolalg_mul_of_boolalg_eq_left_iff {a b : as_boolalg α} : of_boolalg a * of_boolalg b = of_boolalg a ↔ a ≤ b := @inf_eq_left (as_boolalg α) _ _ _ @[simp] lemma to_boolalg_zero : to_boolalg (0 : α) = ⊥ := rfl @[simp] lemma to_boolalg_one : to_boolalg (1 : α) = ⊤ := rfl @[simp] lemma to_boolalg_mul (a b : α) : to_boolalg (a * b) = to_boolalg a ⊓ to_boolalg b := rfl -- `to_boolalg_add` simplifies the LHS but this lemma is eligible to `dsimp` @[simp, nolint simp_nf] lemma to_boolalg_add_add_mul (a b : α) : to_boolalg (a + b + a * b) = to_boolalg a ⊔ to_boolalg b := rfl @[simp] lemma to_boolalg_add (a b : α) : to_boolalg (a + b) = to_boolalg a ∆ to_boolalg b := (of_boolalg_symm_diff _ _).symm /-- Turn a ring homomorphism from Boolean rings `α` to `β` into a bounded lattice homomorphism from `α` to `β` considered as Boolean algebras. -/ @[simps] protected def ring_hom.as_boolalg (f : α →+* β) : bounded_lattice_hom (as_boolalg α) (as_boolalg β) := { to_fun := to_boolalg ∘ f ∘ of_boolalg, map_sup' := λ a b, begin dsimp, simp_rw [map_add f, map_mul f], refl, end, map_inf' := f.map_mul', map_top' := f.map_one', map_bot' := f.map_zero' } @[simp] lemma ring_hom.as_boolalg_id : (ring_hom.id α).as_boolalg = bounded_lattice_hom.id _ := rfl @[simp] lemma ring_hom.as_boolalg_comp (g : β →+* γ) (f : α →+* β) : (g.comp f).as_boolalg = g.as_boolalg.comp f.as_boolalg := rfl end ring_to_algebra /-! ### Turning a Boolean algebra into a Boolean ring -/ section algebra_to_ring /-- Type synonym to view a Boolean ring as a Boolean algebra. -/ def as_boolring (α : Type*) := α /-- The "identity" equivalence between `as_boolring α` and `α`. -/ def to_boolring : α ≃ as_boolring α := equiv.refl _ /-- The "identity" equivalence between `α` and `as_boolring α`. -/ def of_boolring : as_boolring α ≃ α := equiv.refl _ @[simp] lemma to_boolring_symm_eq : (@to_boolring α).symm = of_boolring := rfl @[simp] lemma of_boolring_symm_eq : (@of_boolring α).symm = to_boolring := rfl @[simp] lemma to_boolring_of_boolring (a : as_boolring α) : to_boolring (of_boolring a) = a := rfl @[simp] lemma of_boolring_to_boolring (a : α) : of_boolring (to_boolring a) = a := rfl @[simp] lemma to_boolring_inj {a b : α} : to_boolring a = to_boolring b ↔ a = b := iff.rfl @[simp] lemma of_boolring_inj {a b : as_boolring α} : of_boolring a = of_boolring b ↔ a = b := iff.rfl instance [inhabited α] : inhabited (as_boolring α) := ‹inhabited α› /-- Every generalized Boolean algebra has the structure of a non unital commutative ring with the following data: * `a + b` unfolds to `a ∆ b` (symmetric difference) * `a * b` unfolds to `a ⊓ b` * `-a` unfolds to `a` * `0` unfolds to `⊥` -/ @[reducible] -- See note [reducible non-instances] def generalized_boolean_algebra.to_non_unital_comm_ring [generalized_boolean_algebra α] : non_unital_comm_ring α := { add := (∆), add_assoc := symm_diff_assoc, zero := ⊥, zero_add := bot_symm_diff, add_zero := symm_diff_bot, zero_mul := λ _, bot_inf_eq, mul_zero := λ _, inf_bot_eq, neg := id, add_left_neg := symm_diff_self, add_comm := symm_diff_comm, mul := (⊓), mul_assoc := λ _ _ _, inf_assoc, mul_comm := λ _ _, inf_comm, left_distrib := inf_symm_diff_distrib_left, right_distrib := inf_symm_diff_distrib_right } instance [generalized_boolean_algebra α] : non_unital_comm_ring (as_boolring α) := @generalized_boolean_algebra.to_non_unital_comm_ring α _ variables [boolean_algebra α] [boolean_algebra β] [boolean_algebra γ] /-- Every Boolean algebra has the structure of a Boolean ring with the following data: * `a + b` unfolds to `a ∆ b` (symmetric difference) * `a * b` unfolds to `a ⊓ b` * `-a` unfolds to `a` * `0` unfolds to `⊥` * `1` unfolds to `⊤` -/ @[reducible] -- See note [reducible non-instances] def boolean_algebra.to_boolean_ring : boolean_ring α := { one := ⊤, one_mul := λ _, top_inf_eq, mul_one := λ _, inf_top_eq, mul_self := λ b, inf_idem, ..generalized_boolean_algebra.to_non_unital_comm_ring } localized "attribute [instance, priority 100] generalized_boolean_algebra.to_non_unital_comm_ring boolean_algebra.to_boolean_ring" in boolean_ring_of_boolean_algebra instance : boolean_ring (as_boolring α) := @boolean_algebra.to_boolean_ring α _ @[simp] lemma of_boolring_zero : of_boolring (0 : as_boolring α) = ⊥ := rfl @[simp] lemma of_boolring_one : of_boolring (1 : as_boolring α) = ⊤ := rfl -- `sub_eq_add` proves this lemma but it is eligible for `dsimp` @[simp, nolint simp_nf] lemma of_boolring_neg (a : as_boolring α) : of_boolring (-a) = of_boolring a := rfl @[simp] lemma of_boolring_add (a b : as_boolring α) : of_boolring (a + b) = of_boolring a ∆ of_boolring b := rfl -- `sub_eq_add` simplifies the LHS but this lemma is eligible for `dsimp` @[simp, nolint simp_nf] lemma of_boolring_sub (a b : as_boolring α) : of_boolring (a - b) = of_boolring a ∆ of_boolring b := rfl @[simp] lemma of_boolring_mul (a b : as_boolring α) : of_boolring (a * b) = of_boolring a ⊓ of_boolring b := rfl @[simp] lemma of_boolring_le_of_boolring_iff {a b : as_boolring α} : of_boolring a ≤ of_boolring b ↔ a * b = a := inf_eq_left.symm @[simp] lemma to_boolring_bot : to_boolring (⊥ : α) = 0 := rfl @[simp] lemma to_boolring_top : to_boolring (⊤ : α) = 1 := rfl @[simp] lemma to_boolring_inf (a b : α) : to_boolring (a ⊓ b) = to_boolring a * to_boolring b := rfl @[simp] lemma to_boolring_symm_diff (a b : α) : to_boolring (a ∆ b) = to_boolring a + to_boolring b := rfl /-- Turn a bounded lattice homomorphism from Boolean algebras `α` to `β` into a ring homomorphism from `α` to `β` considered as Boolean rings. -/ @[simps] protected def bounded_lattice_hom.as_boolring (f : bounded_lattice_hom α β) : as_boolring α →+* as_boolring β := { to_fun := to_boolring ∘ f ∘ of_boolring, map_zero' := f.map_bot', map_one' := f.map_top', map_add' := map_symm_diff' f, map_mul' := f.map_inf' } @[simp] lemma bounded_lattice_hom.as_boolring_id : (bounded_lattice_hom.id α).as_boolring = ring_hom.id _ := rfl @[simp] lemma bounded_lattice_hom.as_boolring_comp (g : bounded_lattice_hom β γ) (f : bounded_lattice_hom α β) : (g.comp f).as_boolring = g.as_boolring.comp f.as_boolring := rfl end algebra_to_ring /-! ### Equivalence between Boolean rings and Boolean algebras -/ /-- Order isomorphism between `α` considered as a Boolean ring considered as a Boolean algebra and `α`. -/ @[simps] def order_iso.as_boolalg_as_boolring (α : Type*) [boolean_algebra α] : as_boolalg (as_boolring α) ≃o α := ⟨of_boolalg.trans of_boolring, λ a b, of_boolring_le_of_boolring_iff.trans of_boolalg_mul_of_boolalg_eq_left_iff⟩ /-- Ring isomorphism between `α` considered as a Boolean algebra considered as a Boolean ring and `α`. -/ @[simps] def ring_equiv.as_boolring_as_boolalg (α : Type*) [boolean_ring α] : as_boolring (as_boolalg α) ≃+* α := { map_mul' := λ a b, rfl, map_add' := of_boolalg_symm_diff, ..of_boolring.trans of_boolalg } open bool instance : boolean_ring bool := { add := bxor, add_assoc := bxor_assoc, zero := ff, zero_add := ff_bxor, add_zero := bxor_ff, neg := id, sub := bxor, sub_eq_add_neg := λ _ _, rfl, add_left_neg := bxor_self, add_comm := bxor_comm, one := tt, mul := band, mul_assoc := band_assoc, one_mul := tt_band, mul_one := band_tt, left_distrib := band_bxor_distrib_left, right_distrib := band_bxor_distrib_right, mul_self := band_self }
33d6b1a8bbe69e95d1dcce9ebbd79bd7a9d9a84a
8cd4726d66eec7673bcc0325fed07d5ba5bf17c4
/practice-exam2.lean
57b6214a07fce78edcd9ad668c55b30780d6eaf8
[]
no_license
justinqcai/CS2102
8c5fddedffa6147fedd4b6ee7d5d39fc21f0ddab
d309f0db3f1df52eb77206ee1e8665a3b49d7a0c
refs/heads/master
1,590,108,991,894
1,557,610,044,000
1,557,610,044,000
186,064,169
0
0
null
null
null
null
UTF-8
Lean
false
false
9,660
lean
/- Conjunctions, disjunctions, implication, iff, negation -/ /- 1. Prove that 3 + 3 = 6 and 2 + 6 = 8 implies that 1 + 1 = 2. -/ -- answer: example : 3 + 3 = 6 ∧ 2 + 6 = 8 → 1 + 1 = 2 := begin assume ttts, exact rfl, end /- 2. Prove that 2 + 5 = 3 or 9 + 1 = 5 implies that 2 + 3 = 9. -/ -- answer: example : 2 + 5 = 0 ∨ 9 + 1 = 0 → 2 + 3 = 9 := begin assume tof, cases tof, have contra : 2 + 5 ≠ 0 := begin apply nat.no_confusion, end, contradiction, have contra : 9 + 1 ≠ 0 := begin apply nat.no_confusion, end, contradiction, end /- 3. Prove that ¬(A ∧ B ∧ C) ↔ (¬A ∨ ¬B ∨ ¬C). You may use the axiom of the excluded middle. -/ open classical --axiom em: ∀(P: Prop), P ∨ ¬P -- answer: example : ∀ (A B C : Prop), ¬(A ∧ B ∧ C) ↔ (¬A ∨ ¬B ∨ ¬C) := begin intros A B C, apply iff.intro, /- forward -/ assume nabc, cases em A with a na, cases em B with b nb, cases em C with c nc, have f : false := nabc (and.intro a (and.intro b c)), exact false.elim f, exact or.inr (or.inr nc), exact or.inr (or.inl nb), exact or.inl na, /- backward -/ assume nanbnc, assume abc, cases nanbnc with na nbnc, have f : false := na abc.1, exact f, cases nbnc with nb nc, have f : false := nb abc.2.1, exact f, have f : false := nc abc.2.2, exact f, end /- 4. Prove that ¬(A ∨ B ∨ C) ↔ (¬A ∧ ¬B ∧ ¬C). You may *NOT* use the axiom of the excluded middle. -/ example : ∀ (A B C : Prop), ¬(A ∨ B ∨ C) ↔ (¬A ∧ ¬B ∧ ¬C) := begin intros, split, assume nabc, split, assume a, exact nabc (or.inl a), split, assume b, exact nabc (or.inr (or.inl b)), assume c, exact nabc (or.inr (or.inr c)), assume nabc, assume abc, cases abc with a bc, exact (nabc.1 a), cases bc with b c, exact (nabc.2.1 b), exact (nabc.2.2 c), end -- answer: example : ∀ (A B C : Prop), ¬(A ∨ B ∨ C) ↔ (¬A ∧ ¬B ∧ ¬C) := begin intros A B C, apply iff.intro, /- forward -/ assume nabc, apply and.intro, assume a, have f : false := nabc (or.inl a), exact f, apply and.intro, assume b, have f : false := nabc (or.inr (or.inl b)), exact f, assume c, have f : false := nabc (or.inr (or.inr c)), exact f, /-backward-/ assume nanbnc, assume abc, cases abc with a bc, have na := nanbnc.1, exact false.elim (na a), cases bc with b c, have nb := nanbnc.2.1, exact false.elim (nb b), have nc := nanbnc.2.2, exact false.elim (nc c), end /- 5a. Prove that ¬(A ∨ ¬B) ↔ (¬A ∧ B). You may use the axiom of the excluded middle. -/ -- answer: example : ∀ (A B : Prop), ¬(A ∨ ¬B) ↔ (¬A ∧ B) := begin intros A B, apply iff.intro, /- forward-/ assume nanb, apply and.intro, assume a, have f : false := nanb (or.inl a), exact f, cases em B with b nb, exact b, have f : false := nanb (or.inr nb), exact false.elim f, /-backward-/ assume nab, assume anb, cases anb with a nb, have na := nab.1, exact false.elim (na a), have b := nab.2, exact false.elim (nb b), end /- 5b. Prove that ¬(A ∧ ¬B) ↔ (¬A ∨ B). You may use the axiom of the excluded middle. -/ -- answer: example : ∀ (A B : Prop), ¬(A ∧ ¬B) ↔ (¬A ∨ B) := begin intros A B, apply iff.intro, /-forward-/ assume nanb, cases em A with a na, cases em B with b nb, apply or.inr b, have f : false := nanb (and.intro a nb), exact false.elim f, apply or.inl na, /-backward-/ assume nab, assume anb, cases nab with na b, have a := anb.1, exact na a, have nb := anb.2, exact nb b, end /- 6. Prove that ¬P ∨ ¬Q ∨ R is true if and only if P → Q → R. You may use the axiom of the excluded middle. -/ -- answer: example : ∀ (P Q R : Prop ), (P → Q → R) ↔ (¬P ∨ ¬Q ∨ R) := begin intros P Q R, split, assume pqr, cases em P with p np, have QR := pqr p, cases em Q with q nq, have r := QR q, exact or.inr (or.inr r), exact or.inr (or.inl nq), exact or.inl np, assume npnqr, assume p, assume q, cases npnqr with np nqr, contradiction, cases nqr with nq r, contradiction, exact r, end /- 7. Prove that ¬¬¬P → ¬P. You may use the axiom of the excluded middle. -/ -- answer: example : ∀ P : Prop, ¬¬¬P → ¬P := begin intro P, assume nnnp, assume p, cases em ¬P with p np, contradiction, exact nnnp np, end /- Universal Quantifiers, Existential Quantifiers, and Satisfiability. -/ /- 8. Prove the following statements are satisfiable or prove that they are not. You may use the axiom of the excluded middle to prove cases where the statements are not satisfiable. -/ /- 8a. (P ∨ Q) ∧ (¬P ∨ Q) ∧ (P ∨ ¬Q) -/ example : ∃ P Q : Prop, (P ∨ Q) ∧ (¬P ∨ Q) ∧ (P ∨ ¬Q) := begin apply exists.intro true, apply exists.intro true, simp, end -- answer: example : ∃ P Q : Prop, (P ∨ Q) ∧ (¬P ∨ Q) ∧ (P ∨ ¬Q) := begin apply exists.intro true, apply exists.intro true, split, exact or.inl true.intro, split, exact or.inr true.intro, exact or.inl true.intro, end /- 8b. (P ∨ Q) ∧ (¬P ∨ Q) ∧ (P ∨ ¬Q) ∧ (¬P ∨ ¬Q) -/ -- answer: example : ¬∃ (P Q : Prop), (P ∨ Q) ∧ (¬P ∨ Q) ∧ (P ∨ ¬Q) ∧ (¬P ∨ ¬Q) := begin assume func, apply exists.elim func, assume P, assume Pf, apply exists.elim Pf, assume Q, assume Qf, cases em P with p np, cases em Q with q nq, have npnq := Qf.2.2.2, cases npnq with np nq, contradiction, contradiction, have npq := Qf.2.1, cases npq with np q, contradiction, contradiction, cases em Q with q nq, have pnq := Qf.2.2.1, cases pnq with p nq, contradiction, contradiction, have pq := Qf.1, cases pq with p q, contradiction, contradiction, end /- 8c. (P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (P ∨ ¬Q ∨ ¬R) ∧ (¬P ∨ ¬Q ∨ ¬R) -/ example : ∃ P Q R: Prop, (P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (P ∨ ¬Q ∨ ¬R) ∧ (¬P ∨ ¬Q ∨ ¬R) := begin apply exists.intro true, apply exists.intro true, apply exists.intro false, simp, end -- answer: example : ∃ (P Q R : Prop), (P ∨ Q ∨ R) ∧ (¬P ∨ Q ∨ R) ∧ (P ∨ ¬Q ∨ ¬R) ∧ (¬P ∨ ¬Q ∨ ¬R) := begin apply exists.intro true, apply exists.intro true, apply exists.intro false, split, exact or.inl true.intro, split, exact or.inr (or.inl true.intro), split, exact or.inl true.intro, exact or.inr (or.inr false.elim), end /- 9. Prove that exists a number such that it is both even and a multiple of 3. Use the supplied definitions of even and prime. -/ def isEven' (n: ℕ ) := ∃ m :ℕ, n = m*2 def isMult3' (n: ℕ ) := ∃ m: ℕ, n = m*3 example : ∃ (n: ℕ ), isEven' n ∧ isMult3' n := begin apply exists.intro 6, split, unfold isEven', apply exists.intro 3, trivial, unfold isMult3', apply exists.intro 2, trivial, end def isEven(n: ℕ) := (∃(m: ℕ), m * 2 = n) def isMult3(n: ℕ) := (∃(m: ℕ), m * 3 = n) -- answer example : ∃ (n: ℕ ), isEven n ∧ isMult3 n := begin have isEven6 : isEven 6 := ⟨3, rfl⟩, have isPrime6 : isMult3 6 := ⟨ 2, rfl ⟩ , have both := and.intro isEven6 isPrime6, exact ⟨ 6, both ⟩ , end /- 10a. Write the lemma that if there exists someone that you can fool all of the time, then there always exists someone you can fool. Use the supplied axioms, and make sure you use at least as many parentheses as needed. (It's okay to use more than you need.) 10b. Prove the above lemma. -/ axioms Person Time: Type axiom fool: Person → Time → Prop example : (∃ p: Person, ∀ t: Time, fool p t) → (∀ t : Time, ∃ p : Person, fool p t):= begin assume foolish, assume T, apply exists.elim foolish, assume P, assume func, have ifunc := func T, apply exists.intro P, assumption, end -- answers: example : (∃(p : Person), ∀ (t : Time), fool p t) → (∀ (t : Time), ∃ (p : Person), fool p t) := begin assume idiot, assume t, apply exists.elim idiot, assume p, assume tpfunc, have idiotfunc := tpfunc t, exact ⟨ p, idiotfunc ⟩ , end
8871146ce11e1c8696dd4d836cf7928f235f4827
e38d5e91d30731bef617cc9b6de7f79c34cdce9a
/src/core/kan/coe.lean
0faf5f2b21364fed2f0317c3db7654133770bce0
[ "Apache-2.0" ]
permissive
bbentzen/cubicalean
55e979c303fbf55a81ac46b1000c944b2498be7a
3b94cd2aefdfc2163c263bd3fc6f2086fef814b5
refs/heads/master
1,588,314,875,258
1,554,412,699,000
1,554,412,699,000
177,333,390
0
0
null
null
null
null
UTF-8
Lean
false
false
348
lean
/- Copyright (c) 2019 Bruno Bentzen. All rights reserved. Released under the Apache License 2.0 (see "License"); Author: Bruno Bentzen -/ import .basic universes u -- coercion for n-dimensional cubes namespace coe structure has_coe (A : I → Type) := (coe : Π i, A i → Π j, A j) (coeq : Π i a, coe i a i = a) end coe
a88cfb150e7955dc46bcbcc727f5c6ac05b237e4
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/lattice.lean
24a1453a0d1ae9b554b89c5b9a749a6257fdf977
[ "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,989
lean
/- Copyright (c) 2021 Rémy Degenne. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémy Degenne -/ import measure_theory.measure.ae_measurable /-! # Typeclasses for measurability of lattice operations > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file we define classes `has_measurable_sup` and `has_measurable_inf` and prove dot-style lemmas (`measurable.sup`, `ae_measurable.sup` etc). For binary operations we define two typeclasses: - `has_measurable_sup` says that both left and right sup are measurable; - `has_measurable_sup₂` 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_sup₂` etc require `α` to have a second countable topology. For instances relating, e.g., `has_continuous_sup` to `has_measurable_sup` see file `measure_theory.borel_space`. ## Tags measurable function, lattice operation -/ open measure_theory /-- We say that a type `has_measurable_sup` if `((⊔) c)` and `(⊔ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (⊔)` see `has_measurable_sup₂`. -/ class has_measurable_sup (M : Type*) [measurable_space M] [has_sup M] : Prop := (measurable_const_sup : ∀ c : M, measurable ((⊔) c)) (measurable_sup_const : ∀ c : M, measurable (⊔ c)) /-- We say that a type `has_measurable_sup₂` if `uncurry (⊔)` is a measurable functions. For a typeclass assuming measurability of `((⊔) c)` and `(⊔ c)` see `has_measurable_sup`. -/ class has_measurable_sup₂ (M : Type*) [measurable_space M] [has_sup M] : Prop := (measurable_sup : measurable (λ p : M × M, p.1 ⊔ p.2)) export has_measurable_sup₂ (measurable_sup) has_measurable_sup (measurable_const_sup measurable_sup_const) /-- We say that a type `has_measurable_inf` if `((⊓) c)` and `(⊓ c)` are measurable functions. For a typeclass assuming measurability of `uncurry (⊓)` see `has_measurable_inf₂`. -/ class has_measurable_inf (M : Type*) [measurable_space M] [has_inf M] : Prop := (measurable_const_inf : ∀ c : M, measurable ((⊓) c)) (measurable_inf_const : ∀ c : M, measurable (⊓ c)) /-- We say that a type `has_measurable_inf₂` if `uncurry (⊔)` is a measurable functions. For a typeclass assuming measurability of `((⊔) c)` and `(⊔ c)` see `has_measurable_inf`. -/ class has_measurable_inf₂ (M : Type*) [measurable_space M] [has_inf M] : Prop := (measurable_inf : measurable (λ p : M × M, p.1 ⊓ p.2)) export has_measurable_inf₂ (measurable_inf) has_measurable_inf (measurable_const_inf measurable_inf_const) variables {M : Type*} [measurable_space M] section order_dual @[priority 100] instance [has_inf M] [has_measurable_inf M] : has_measurable_sup Mᵒᵈ := ⟨@measurable_const_inf M _ _ _, @measurable_inf_const M _ _ _⟩ @[priority 100] instance [has_sup M] [has_measurable_sup M] : has_measurable_inf Mᵒᵈ := ⟨@measurable_const_sup M _ _ _, @measurable_sup_const M _ _ _⟩ @[priority 100] instance [has_inf M] [has_measurable_inf₂ M] : has_measurable_sup₂ Mᵒᵈ := ⟨@measurable_inf M _ _ _⟩ @[priority 100] instance [has_sup M] [has_measurable_sup₂ M] : has_measurable_inf₂ Mᵒᵈ := ⟨@measurable_sup M _ _ _⟩ end order_dual variables {α : Type*} {m : measurable_space α} {μ : measure α} {f g : α → M} include m section sup variables [has_sup M] section measurable_sup variables [has_measurable_sup M] @[measurability] lemma measurable.const_sup (hf : measurable f) (c : M) : measurable (λ x, c ⊔ f x) := (measurable_const_sup c).comp hf @[measurability] lemma ae_measurable.const_sup (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c ⊔ f x) μ := (has_measurable_sup.measurable_const_sup c).comp_ae_measurable hf @[measurability] lemma measurable.sup_const (hf : measurable f) (c : M) : measurable (λ x, f x ⊔ c) := (measurable_sup_const c).comp hf @[measurability] lemma ae_measurable.sup_const (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, f x ⊔ c) μ := (measurable_sup_const c).comp_ae_measurable hf end measurable_sup section measurable_sup₂ variables [has_measurable_sup₂ M] @[measurability] lemma measurable.sup' (hf : measurable f) (hg : measurable g) : measurable (f ⊔ g) := measurable_sup.comp (hf.prod_mk hg) @[measurability] lemma measurable.sup (hf : measurable f) (hg : measurable g) : measurable (λ a, f a ⊔ g a) := measurable_sup.comp (hf.prod_mk hg) @[measurability] lemma ae_measurable.sup' (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f ⊔ g) μ := measurable_sup.comp_ae_measurable (hf.prod_mk hg) @[measurability] lemma ae_measurable.sup (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a ⊔ g a) μ := measurable_sup.comp_ae_measurable (hf.prod_mk hg) omit m @[priority 100] instance has_measurable_sup₂.to_has_measurable_sup : has_measurable_sup M := ⟨λ c, measurable_const.sup measurable_id, λ c, measurable_id.sup measurable_const⟩ include m end measurable_sup₂ end sup section inf variables [has_inf M] section measurable_inf variables [has_measurable_inf M] @[measurability] lemma measurable.const_inf (hf : measurable f) (c : M) : measurable (λ x, c ⊓ f x) := (measurable_const_inf c).comp hf @[measurability] lemma ae_measurable.const_inf (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, c ⊓ f x) μ := (has_measurable_inf.measurable_const_inf c).comp_ae_measurable hf @[measurability] lemma measurable.inf_const (hf : measurable f) (c : M) : measurable (λ x, f x ⊓ c) := (measurable_inf_const c).comp hf @[measurability] lemma ae_measurable.inf_const (hf : ae_measurable f μ) (c : M) : ae_measurable (λ x, f x ⊓ c) μ := (measurable_inf_const c).comp_ae_measurable hf end measurable_inf section measurable_inf₂ variables [has_measurable_inf₂ M] @[measurability] lemma measurable.inf' (hf : measurable f) (hg : measurable g) : measurable (f ⊓ g) := measurable_inf.comp (hf.prod_mk hg) @[measurability] lemma measurable.inf (hf : measurable f) (hg : measurable g) : measurable (λ a, f a ⊓ g a) := measurable_inf.comp (hf.prod_mk hg) @[measurability] lemma ae_measurable.inf' (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (f ⊓ g) μ := measurable_inf.comp_ae_measurable (hf.prod_mk hg) @[measurability] lemma ae_measurable.inf (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ a, f a ⊓ g a) μ := measurable_inf.comp_ae_measurable (hf.prod_mk hg) omit m @[priority 100] instance has_measurable_inf₂.to_has_measurable_inf : has_measurable_inf M := ⟨λ c, measurable_const.inf measurable_id, λ c, measurable_id.inf measurable_const⟩ include m end measurable_inf₂ end inf section semilattice_sup open finset variables {δ : Type*} [measurable_space δ] [semilattice_sup α] [has_measurable_sup₂ α] @[measurability] lemma finset.measurable_sup' {ι : Type*} {s : finset ι} (hs : s.nonempty) {f : ι → δ → α} (hf : ∀ n ∈ s, measurable (f n)) : measurable (s.sup' hs f) := finset.sup'_induction hs _ (λ f hf g hg, hf.sup hg) (λ n hn, hf n hn) @[measurability] lemma finset.measurable_range_sup' {f : ℕ → δ → α} {n : ℕ} (hf : ∀ k ≤ n, measurable (f k)) : measurable ((range (n + 1)).sup' nonempty_range_succ f) := begin simp_rw ← nat.lt_succ_iff at hf, refine finset.measurable_sup' _ _, simpa [finset.mem_range], end @[measurability] lemma finset.measurable_range_sup'' {f : ℕ → δ → α} {n : ℕ} (hf : ∀ k ≤ n, measurable (f k)) : measurable (λ x, (range (n + 1)).sup' nonempty_range_succ (λ k, f k x)) := begin convert finset.measurable_range_sup' hf, ext x, simp, end end semilattice_sup
7cf9f1c8568c3a72f592f5e1bb8a4d940f874bbe
80746c6dba6a866de5431094bf9f8f841b043d77
/src/category_theory/examples/monoids.lean
cf1c2864b30806d6b5dae000eb03755ec4ab9521
[ "Apache-2.0" ]
permissive
leanprover-fork/mathlib-backup
8b5c95c535b148fca858f7e8db75a76252e32987
0eb9db6a1a8a605f0cf9e33873d0450f9f0ae9b0
refs/heads/master
1,585,156,056,139
1,548,864,430,000
1,548,864,438,000
143,964,213
0
0
Apache-2.0
1,550,795,966,000
1,533,705,322,000
Lean
UTF-8
Lean
false
false
1,739
lean
/- Copyright (c) 2018 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison Introduce Mon -- the category of monoids. Currently only the basic setup. -/ import category_theory.concrete_category import category_theory.fully_faithful import category_theory.adjunction import data.finsupp universes u v open category_theory namespace category_theory.examples /-- The category of monoids and monoid morphisms. -/ @[reducible] def Mon : Type (u+1) := bundled monoid instance (x : Mon) : monoid x := x.str instance concrete_is_monoid_hom : concrete_category @is_monoid_hom := ⟨by introsI α ia; apply_instance, by introsI α β γ ia ib ic f g hf hg; apply_instance⟩ instance Mon_hom_is_monoid_hom {R S : Mon} (f : R ⟶ S) : is_monoid_hom (f : R → S) := f.2 /-- The category of commutative monoids and monoid morphisms. -/ @[reducible] def CommMon : Type (u+1) := bundled comm_monoid instance (x : CommMon) : comm_monoid x := x.str @[reducible] def is_comm_monoid_hom {α β} [comm_monoid α] [comm_monoid β] (f : α → β) : Prop := is_monoid_hom f instance concrete_is_comm_monoid_hom : concrete_category @is_comm_monoid_hom := ⟨by introsI α ia; apply_instance, by introsI α β γ ia ib ic f g hf hg; apply_instance⟩ instance CommMon_hom_is_comm_monoid_hom {R S : CommMon} (f : R ⟶ S) : is_comm_monoid_hom (f : R → S) := f.2 namespace CommMon /-- The forgetful functor from commutative monoids to monoids. -/ def forget_to_Mon : CommMon ⥤ Mon := concrete_functor (by intros _ c; exact { ..c }) (by introsI _ _ _ _ f i; exact { ..i }) instance : faithful (forget_to_Mon) := {} end CommMon end category_theory.examples
8fc0f1abf4ecce3b4aacb0fb99c551ea2d92ecec
9dc8cecdf3c4634764a18254e94d43da07142918
/src/field_theory/splitting_field.lean
f4522aa6a7dcc13680783ab4589b6423973e2a20
[ "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
39,371
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import field_theory.intermediate_field import ring_theory.adjoin_root /-! # Splitting fields This file introduces the notion of a splitting field of a polynomial and provides an embedding from a splitting field to any field that splits the polynomial. A polynomial `f : polynomial K` splits over a field extension `L` of `K` if it is zero or all of its irreducible factors over `L` have degree `1`. A field extension of `K` of a polynomial `f : polynomial K` is called a splitting field if it is the smallest field extension of `K` such that `f` splits. ## Main definitions * `polynomial.splits i f`: A predicate on a homomorphism `i : K →+* L` from a commutative ring to a field and a polynomial `f` saying that `f.map i` is zero or all of its irreducible factors over `L` have degree `1`. * `polynomial.splitting_field f`: A fixed splitting field of the polynomial `f`. * `polynomial.is_splitting_field`: A predicate on a field to be a splitting field of a polynomial `f`. ## Main statements * `lift_of_splits`: If `K` and `L` are field extensions of a field `F` and for some finite subset `S` of `K`, the minimal polynomial of every `x ∈ K` splits as a polynomial with coefficients in `L`, then `algebra.adjoin F S` embeds into `L`. * `polynomial.is_splitting_field.lift`: An embedding of a splitting field of the polynomial `f` into another field such that `f` splits. * `polynomial.is_splitting_field.alg_equiv`: Every splitting field of a polynomial `f` is isomorphic to `splitting_field f` and thus, being a splitting field is unique up to isomorphism. -/ noncomputable theory open_locale classical big_operators polynomial universes u v w variables {F : Type u} {K : Type v} {L : Type w} namespace polynomial open polynomial section splits section comm_ring variables [comm_ring K] [field L] [field F] variables (i : K →+* L) /-- A polynomial `splits` iff it is zero or all of its irreducible factors have `degree` 1. -/ def splits (f : K[X]) : Prop := f.map i = 0 ∨ ∀ {g : L[X]}, irreducible g → g ∣ f.map i → degree g = 1 @[simp] lemma splits_zero : splits i (0 : K[X]) := or.inl (polynomial.map_zero i) lemma splits_of_map_eq_C {f : K[X]} {a : L} (h : f.map i = C a) : splits i f := if ha : a = 0 then or.inl (h.trans (ha.symm ▸ C_0)) else or.inr $ λ g hg ⟨p, hp⟩, absurd hg.1 $ not_not.2 $ is_unit_iff_degree_eq_zero.2 $ begin have := congr_arg degree hp, rw [h, degree_C ha, degree_mul, @eq_comm (with_bot ℕ) 0, nat.with_bot.add_eq_zero_iff] at this, exact this.1, end @[simp] lemma splits_C (a : K) : splits i (C a) := splits_of_map_eq_C i (map_C i) lemma splits_of_map_degree_eq_one {f : K[X]} (hf : degree (f.map i) = 1) : splits i f := or.inr $ λ g hg ⟨p, hp⟩, by have := congr_arg degree hp; simp [nat.with_bot.add_eq_one_iff, hf, @eq_comm (with_bot ℕ) 1, mt is_unit_iff_degree_eq_zero.2 hg.1] at this; clear _fun_match; tauto lemma splits_of_degree_le_one {f : K[X]} (hf : degree f ≤ 1) : splits i f := if hif : degree (f.map i) ≤ 0 then splits_of_map_eq_C i (degree_le_zero_iff.mp hif) else begin push_neg at hif, rw [← order.succ_le_iff, ← with_bot.coe_zero, with_bot.succ_coe, nat.succ_eq_succ] at hif, exact splits_of_map_degree_eq_one i (le_antisymm ((degree_map_le i _).trans hf) hif), end lemma splits_of_degree_eq_one {f : K[X]} (hf : degree f = 1) : splits i f := splits_of_degree_le_one i hf.le lemma splits_of_nat_degree_le_one {f : K[X]} (hf : nat_degree f ≤ 1) : splits i f := splits_of_degree_le_one i (degree_le_of_nat_degree_le hf) lemma splits_of_nat_degree_eq_one {f : K[X]} (hf : nat_degree f = 1) : splits i f := splits_of_nat_degree_le_one i (le_of_eq hf) lemma splits_mul {f g : K[X]} (hf : splits i f) (hg : splits i g) : splits i (f * g) := if h : (f * g).map i = 0 then or.inl h else or.inr $ λ p hp hpf, ((principal_ideal_ring.irreducible_iff_prime.1 hp).2.2 _ _ (show p ∣ map i f * map i g, by convert hpf; rw polynomial.map_mul)).elim (hf.resolve_left (λ hf, by simpa [hf] using h) hp) (hg.resolve_left (λ hg, by simpa [hg] using h) hp) lemma splits_of_splits_mul' {f g : K[X]} (hfg : (f * g).map i ≠ 0) (h : splits i (f * g)) : splits i f ∧ splits i g := ⟨or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw polynomial.map_mul; exact hg.trans (dvd_mul_right _ _)), or.inr $ λ g hgi hg, or.resolve_left h hfg hgi (by rw polynomial.map_mul; exact hg.trans (dvd_mul_left _ _))⟩ lemma splits_map_iff (j : L →+* F) {f : K[X]} : splits j (f.map i) ↔ splits (j.comp i) f := by simp [splits, polynomial.map_map] theorem splits_one : splits i 1 := splits_C i 1 theorem splits_of_is_unit [is_domain K] {u : K[X]} (hu : is_unit u) : u.splits i := (is_unit_iff.mp hu).some_spec.2 ▸ splits_C _ _ theorem splits_X_sub_C {x : K} : (X - C x).splits i := splits_of_degree_le_one _ $ degree_X_sub_C_le _ theorem splits_X : X.splits i := splits_of_degree_le_one _ degree_X_le theorem splits_prod {ι : Type u} {s : ι → K[X]} {t : finset ι} : (∀ j ∈ t, (s j).splits i) → (∏ x in t, s x).splits i := begin refine finset.induction_on t (λ _, splits_one i) (λ a t hat ih ht, _), rw finset.forall_mem_insert at ht, rw finset.prod_insert hat, exact splits_mul i ht.1 (ih ht.2) end lemma splits_pow {f : K[X]} (hf : f.splits i) (n : ℕ) : (f ^ n).splits i := begin rw [←finset.card_range n, ←finset.prod_const], exact splits_prod i (λ j hj, hf), end lemma splits_X_pow (n : ℕ) : (X ^ n).splits i := splits_pow i (splits_X i) n theorem splits_id_iff_splits {f : K[X]} : (f.map i).splits (ring_hom.id L) ↔ f.splits i := by rw [splits_map_iff, ring_hom.id_comp] lemma exists_root_of_splits' {f : K[X]} (hs : splits i f) (hf0 : degree (f.map i) ≠ 0) : ∃ x, eval₂ i x f = 0 := if hf0' : f.map i = 0 then by simp [eval₂_eq_eval_map, hf0'] else let ⟨g, hg⟩ := wf_dvd_monoid.exists_irreducible_factor (show ¬ is_unit (f.map i), from mt is_unit_iff_degree_eq_zero.1 hf0) hf0' in let ⟨x, hx⟩ := exists_root_of_degree_eq_one (hs.resolve_left hf0' hg.1 hg.2) in let ⟨i, hi⟩ := hg.2 in ⟨x, by rw [← eval_map, hi, eval_mul, show _ = _, from hx, zero_mul]⟩ lemma roots_ne_zero_of_splits' {f : K[X]} (hs : splits i f) (hf0 : nat_degree (f.map i) ≠ 0) : (f.map i).roots ≠ 0 := let ⟨x, hx⟩ := exists_root_of_splits' i hs (λ h, hf0 $ nat_degree_eq_of_degree_eq_some h) in λ h, by { rw ← eval_map at hx, cases h.subst ((mem_roots _).2 hx), exact ne_zero_of_nat_degree_gt (nat.pos_of_ne_zero hf0) } /-- Pick a root of a polynomial that splits. See `root_of_splits` for polynomials over a field which has simpler assumptions. -/ def root_of_splits' {f : K[X]} (hf : f.splits i) (hfd : (f.map i).degree ≠ 0) : L := classical.some $ exists_root_of_splits' i hf hfd theorem map_root_of_splits' {f : K[X]} (hf : f.splits i) (hfd) : f.eval₂ i (root_of_splits' i hf hfd) = 0 := classical.some_spec $ exists_root_of_splits' i hf hfd lemma nat_degree_eq_card_roots' {p : K[X]} {i : K →+* L} (hsplit : splits i p) : (p.map i).nat_degree = (p.map i).roots.card := begin by_cases hp : p.map i = 0, { rw [hp, nat_degree_zero, roots_zero, multiset.card_zero] }, obtain ⟨q, he, hd, hr⟩ := exists_prod_multiset_X_sub_C_mul (p.map i), rw [← splits_id_iff_splits, ← he] at hsplit, rw ← he at hp, have hq : q ≠ 0 := λ h, hp (by rw [h, mul_zero]), rw [← hd, add_right_eq_self], by_contra, have h' : (map (ring_hom.id L) q).nat_degree ≠ 0, { simp [h], }, have := roots_ne_zero_of_splits' (ring_hom.id L) (splits_of_splits_mul' _ _ hsplit).2 h', { rw map_id at this, exact this hr }, { rw [map_id], exact mul_ne_zero monic_prod_multiset_X_sub_C.ne_zero hq }, end lemma degree_eq_card_roots' {p : K[X]} {i : K →+* L} (p_ne_zero : p.map i ≠ 0) (hsplit : splits i p) : (p.map i).degree = (p.map i).roots.card := by rw [degree_eq_nat_degree p_ne_zero, nat_degree_eq_card_roots' hsplit] end comm_ring variables [field K] [field L] [field F] variables (i : K →+* L) /-- This lemma is for polynomials over a field. -/ lemma splits_iff (f : K[X]) : splits i f ↔ f = 0 ∨ ∀ {g : L[X]}, irreducible g → g ∣ f.map i → degree g = 1 := by rw [splits, map_eq_zero] /-- This lemma is for polynomials over a field. -/ lemma splits.def {i : K →+* L} {f : K[X]} (h : splits i f) : f = 0 ∨ ∀ {g : L[X]}, irreducible g → g ∣ f.map i → degree g = 1 := (splits_iff i f).mp h lemma splits_of_splits_mul {f g : K[X]} (hfg : f * g ≠ 0) (h : splits i (f * g)) : splits i f ∧ splits i g := splits_of_splits_mul' i (map_ne_zero hfg) h lemma splits_of_splits_of_dvd {f g : K[X]} (hf0 : f ≠ 0) (hf : splits i f) (hgf : g ∣ f) : splits i g := by { obtain ⟨f, rfl⟩ := hgf, exact (splits_of_splits_mul i hf0 hf).1 } lemma splits_of_splits_gcd_left {f g : K[X]} (hf0 : f ≠ 0) (hf : splits i f) : splits i (euclidean_domain.gcd f g) := polynomial.splits_of_splits_of_dvd i hf0 hf (euclidean_domain.gcd_dvd_left f g) lemma splits_of_splits_gcd_right {f g : K[X]} (hg0 : g ≠ 0) (hg : splits i g) : splits i (euclidean_domain.gcd f g) := polynomial.splits_of_splits_of_dvd i hg0 hg (euclidean_domain.gcd_dvd_right f g) theorem splits_mul_iff {f g : K[X]} (hf : f ≠ 0) (hg : g ≠ 0) : (f * g).splits i ↔ f.splits i ∧ g.splits i := ⟨splits_of_splits_mul i (mul_ne_zero hf hg), λ ⟨hfs, hgs⟩, splits_mul i hfs hgs⟩ theorem splits_prod_iff {ι : Type u} {s : ι → K[X]} {t : finset ι} : (∀ j ∈ t, s j ≠ 0) → ((∏ x in t, s x).splits i ↔ ∀ j ∈ t, (s j).splits i) := begin refine finset.induction_on t (λ _, ⟨λ _ _ h, h.elim, λ _, splits_one i⟩) (λ a t hat ih ht, _), rw finset.forall_mem_insert at ht ⊢, rw [finset.prod_insert hat, splits_mul_iff i ht.1 (finset.prod_ne_zero_iff.2 ht.2), ih ht.2] end lemma degree_eq_one_of_irreducible_of_splits {p : K[X]} (hp : irreducible p) (hp_splits : splits (ring_hom.id K) p) : p.degree = 1 := begin rcases hp_splits, { exfalso, simp * at *, }, { apply hp_splits hp, simp } end lemma exists_root_of_splits {f : K[X]} (hs : splits i f) (hf0 : degree f ≠ 0) : ∃ x, eval₂ i x f = 0 := exists_root_of_splits' i hs ((f.degree_map i).symm ▸ hf0) lemma roots_ne_zero_of_splits {f : K[X]} (hs : splits i f) (hf0 : nat_degree f ≠ 0) : (f.map i).roots ≠ 0 := roots_ne_zero_of_splits' i hs (ne_of_eq_of_ne (nat_degree_map i) hf0) /-- Pick a root of a polynomial that splits. This version is for polynomials over a field and has simpler assumptions. -/ def root_of_splits {f : K[X]} (hf : f.splits i) (hfd : f.degree ≠ 0) : L := root_of_splits' i hf ((f.degree_map i).symm ▸ hfd) /-- `root_of_splits'` is definitionally equal to `root_of_splits`. -/ lemma root_of_splits'_eq_root_of_splits {f : K[X]} (hf : f.splits i) (hfd) : root_of_splits' i hf hfd = root_of_splits i hf (f.degree_map i ▸ hfd) := rfl theorem map_root_of_splits {f : K[X]} (hf : f.splits i) (hfd) : f.eval₂ i (root_of_splits i hf hfd) = 0 := map_root_of_splits' i hf (ne_of_eq_of_ne (degree_map f i) hfd) lemma nat_degree_eq_card_roots {p : K[X]} {i : K →+* L} (hsplit : splits i p) : p.nat_degree = (p.map i).roots.card := (nat_degree_map i).symm.trans $ nat_degree_eq_card_roots' hsplit lemma degree_eq_card_roots {p : K[X]} {i : K →+* L} (p_ne_zero : p ≠ 0) (hsplit : splits i p) : p.degree = (p.map i).roots.card := by rw [degree_eq_nat_degree p_ne_zero, nat_degree_eq_card_roots hsplit] theorem roots_map {f : K[X]} (hf : f.splits $ ring_hom.id K) : (f.map i).roots = f.roots.map i := (roots_map_of_injective_card_eq_total_degree i.injective $ by { convert (nat_degree_eq_card_roots hf).symm, rw map_id }).symm lemma image_root_set [algebra F K] [algebra F L] {p : F[X]} (h : p.splits (algebra_map F K)) (f : K →ₐ[F] L) : f '' p.root_set K = p.root_set L := begin classical, rw [root_set, ←finset.coe_image, ←multiset.to_finset_map, ←f.coe_to_ring_hom, ←roots_map ↑f ((splits_id_iff_splits (algebra_map F K)).mpr h), map_map, f.comp_algebra_map, ←root_set], end lemma adjoin_root_set_eq_range [algebra F K] [algebra F L] {p : F[X]} (h : p.splits (algebra_map F K)) (f : K →ₐ[F] L) : algebra.adjoin F (p.root_set L) = f.range ↔ algebra.adjoin F (p.root_set K) = ⊤ := begin rw [←image_root_set h f, algebra.adjoin_image, ←algebra.map_top], exact (subalgebra.map_injective f.to_ring_hom.injective).eq_iff, end lemma eq_prod_roots_of_splits {p : K[X]} {i : K →+* L} (hsplit : splits i p) : p.map i = C (i p.leading_coeff) * ((p.map i).roots.map (λ a, X - C a)).prod := begin rw ← leading_coeff_map, symmetry, apply C_leading_coeff_mul_prod_multiset_X_sub_C, rw nat_degree_map, exact (nat_degree_eq_card_roots hsplit).symm, end lemma eq_prod_roots_of_splits_id {p : K[X]} (hsplit : splits (ring_hom.id K) p) : p = C p.leading_coeff * (p.roots.map (λ a, X - C a)).prod := by simpa using eq_prod_roots_of_splits hsplit lemma eq_prod_roots_of_monic_of_splits_id {p : K[X]} (m : monic p) (hsplit : splits (ring_hom.id K) p) : p = (p.roots.map (λ a, X - C a)).prod := begin convert eq_prod_roots_of_splits_id hsplit, simp [m], end lemma eq_X_sub_C_of_splits_of_single_root {x : K} {h : K[X]} (h_splits : splits i h) (h_roots : (h.map i).roots = {i x}) : h = C h.leading_coeff * (X - C x) := begin apply polynomial.map_injective _ i.injective, rw [eq_prod_roots_of_splits h_splits, h_roots], simp, end section UFD local attribute [instance, priority 10] principal_ideal_ring.to_unique_factorization_monoid local infix ` ~ᵤ ` : 50 := associated open unique_factorization_monoid associates lemma splits_of_exists_multiset {f : K[X]} {s : multiset L} (hs : f.map i = C (i f.leading_coeff) * (s.map (λ a : L, X - C a)).prod) : splits i f := if hf0 : f = 0 then hf0.symm ▸ splits_zero i else or.inr $ λ p hp hdp, begin rw irreducible_iff_prime at hp, rw [hs, ← multiset.prod_to_list] at hdp, obtain (hd|hd) := hp.2.2 _ _ hdp, { refine (hp.2.1 $ is_unit_of_dvd_unit hd _).elim, exact is_unit_C.2 ((leading_coeff_ne_zero.2 hf0).is_unit.map i) }, { obtain ⟨q, hq, hd⟩ := hp.dvd_prod_iff.1 hd, obtain ⟨a, ha, rfl⟩ := multiset.mem_map.1 (multiset.mem_to_list.1 hq), rw degree_eq_degree_of_associated ((hp.dvd_prime_iff_associated $ prime_X_sub_C a).1 hd), exact degree_X_sub_C a }, end lemma splits_of_splits_id {f : K[X]} : splits (ring_hom.id K) f → splits i f := unique_factorization_monoid.induction_on_prime f (λ _, splits_zero _) (λ _ hu _, splits_of_degree_le_one _ ((is_unit_iff_degree_eq_zero.1 hu).symm ▸ dec_trivial)) (λ a p ha0 hp ih hfi, splits_mul _ (splits_of_degree_eq_one _ ((splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).1.def.resolve_left hp.1 hp.irreducible (by rw map_id))) (ih (splits_of_splits_mul _ (mul_ne_zero hp.1 ha0) hfi).2)) end UFD lemma splits_iff_exists_multiset {f : K[X]} : splits i f ↔ ∃ (s : multiset L), f.map i = C (i f.leading_coeff) * (s.map (λ a : L, X - C a)).prod := ⟨λ hf, ⟨(f.map i).roots, eq_prod_roots_of_splits hf⟩, λ ⟨s, hs⟩, splits_of_exists_multiset i hs⟩ lemma splits_comp_of_splits (j : L →+* F) {f : K[X]} (h : splits i f) : splits (j.comp i) f := begin change i with ((ring_hom.id _).comp i) at h, rw [← splits_map_iff], rw [← splits_map_iff i] at h, exact splits_of_splits_id _ h end /-- A polynomial splits if and only if it has as many roots as its degree. -/ lemma splits_iff_card_roots {p : K[X]} : splits (ring_hom.id K) p ↔ p.roots.card = p.nat_degree := begin split, { intro H, rw [nat_degree_eq_card_roots H, map_id] }, { intro hroots, rw splits_iff_exists_multiset (ring_hom.id K), use p.roots, simp only [ring_hom.id_apply, map_id], exact (C_leading_coeff_mul_prod_multiset_X_sub_C hroots).symm }, end lemma aeval_root_derivative_of_splits [algebra K L] {P : K[X]} (hmo : P.monic) (hP : P.splits (algebra_map K L)) {r : L} (hr : r ∈ (P.map (algebra_map K L)).roots) : aeval r P.derivative = (((P.map $ algebra_map K L).roots.erase r).map (λ a, r - a)).prod := begin replace hmo := hmo.map (algebra_map K L), replace hP := (splits_id_iff_splits (algebra_map K L)).2 hP, rw [aeval_def, ← eval_map, ← derivative_map], nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP], rw [eval_multiset_prod_X_sub_C_derivative hr] end /-- If `P` is a monic polynomial that splits, then `coeff P 0` equals the product of the roots. -/ lemma prod_roots_eq_coeff_zero_of_monic_of_split {P : K[X]} (hmo : P.monic) (hP : P.splits (ring_hom.id K)) : coeff P 0 = (-1) ^ P.nat_degree * P.roots.prod := begin nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP], rw [coeff_zero_eq_eval_zero, eval_multiset_prod, multiset.map_map], simp_rw [function.comp_app, eval_sub, eval_X, zero_sub, eval_C], conv_lhs { congr, congr, funext, rw [neg_eq_neg_one_mul] }, rw [multiset.prod_map_mul, multiset.map_const, multiset.prod_repeat, multiset.map_id', splits_iff_card_roots.1 hP] end /-- If `P` is a monic polynomial that splits, then `P.next_coeff` equals the sum of the roots. -/ lemma sum_roots_eq_next_coeff_of_monic_of_split {P : K[X]} (hmo : P.monic) (hP : P.splits (ring_hom.id K)) : P.next_coeff = - P.roots.sum := begin nth_rewrite 0 [eq_prod_roots_of_monic_of_splits_id hmo hP], rw [monic.next_coeff_multiset_prod _ _ (λ a ha, _)], { simp_rw [next_coeff_X_sub_C, multiset.sum_map_neg'] }, { exact monic_X_sub_C a } end end splits end polynomial section embeddings variables (F) [field F] /-- If `p` is the minimal polynomial of `a` over `F` then `F[a] ≃ₐ[F] F[x]/(p)` -/ def alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly {R : Type*} [comm_ring R] [algebra F R] (x : R) : algebra.adjoin F ({x} : set R) ≃ₐ[F] adjoin_root (minpoly F x) := alg_equiv.symm $ alg_equiv.of_bijective (alg_hom.cod_restrict (adjoin_root.lift_hom _ x $ minpoly.aeval F x) _ (λ p, adjoin_root.induction_on _ p $ λ p, (algebra.adjoin_singleton_eq_range_aeval F x).symm ▸ (polynomial.aeval _).mem_range.mpr ⟨p, rfl⟩)) ⟨(alg_hom.injective_cod_restrict _ _ _).2 $ (injective_iff_map_eq_zero _).2 $ λ p, adjoin_root.induction_on _ p $ λ p hp, ideal.quotient.eq_zero_iff_mem.2 $ ideal.mem_span_singleton.2 $ minpoly.dvd F x hp, λ y, let ⟨p, hp⟩ := (set_like.ext_iff.1 (algebra.adjoin_singleton_eq_range_aeval F x) (y : R)).1 y.2 in ⟨adjoin_root.mk _ p, subtype.eq hp⟩⟩ open finset /-- If a `subalgebra` is finite_dimensional as a submodule then it is `finite_dimensional`. -/ lemma finite_dimensional.of_subalgebra_to_submodule {K V : Type*} [field K] [ring V] [algebra K V] {s : subalgebra K V} (h : finite_dimensional K s.to_submodule) : finite_dimensional K s := h /-- If `K` and `L` are field extensions of `F` and we have `s : finset K` such that the minimal polynomial of each `x ∈ s` splits in `L` then `algebra.adjoin F s` embeds in `L`. -/ theorem lift_of_splits {F K L : Type*} [field F] [field K] [field L] [algebra F K] [algebra F L] (s : finset K) : (∀ x ∈ s, is_integral F x ∧ polynomial.splits (algebra_map F L) (minpoly F x)) → nonempty (algebra.adjoin F (↑s : set K) →ₐ[F] L) := begin refine finset.induction_on s (λ H, _) (λ a s has ih H, _), { rw [coe_empty, algebra.adjoin_empty], exact ⟨(algebra.of_id F L).comp (algebra.bot_equiv F K)⟩ }, rw forall_mem_insert at H, rcases H with ⟨⟨H1, H2⟩, H3⟩, cases ih H3 with f, choose H3 H4 using H3, rw [coe_insert, set.insert_eq, set.union_comm, algebra.adjoin_union_eq_adjoin_adjoin], letI := (f : algebra.adjoin F (↑s : set K) →+* L).to_algebra, haveI : finite_dimensional F (algebra.adjoin F (↑s : set K)) := ( (submodule.fg_iff_finite_dimensional _).1 (fg_adjoin_of_finite s.finite_to_set H3)).of_subalgebra_to_submodule, letI := field_of_finite_dimensional F (algebra.adjoin F (↑s : set K)), have H5 : is_integral (algebra.adjoin F (↑s : set K)) a := is_integral_of_is_scalar_tower a H1, have H6 : (minpoly (algebra.adjoin F (↑s : set K)) a).splits (algebra_map (algebra.adjoin F (↑s : set K)) L), { refine polynomial.splits_of_splits_of_dvd _ (polynomial.map_ne_zero $ minpoly.ne_zero H1 : polynomial.map (algebra_map _ _) _ ≠ 0) ((polynomial.splits_map_iff _ _).2 _) (minpoly.dvd _ _ _), { rw ← is_scalar_tower.algebra_map_eq, exact H2 }, { rw [← is_scalar_tower.aeval_apply, minpoly.aeval] } }, obtain ⟨y, hy⟩ := polynomial.exists_root_of_splits _ H6 (ne_of_lt (minpoly.degree_pos H5)).symm, refine ⟨subalgebra.of_restrict_scalars _ _ _⟩, refine (adjoin_root.lift_hom (minpoly (algebra.adjoin F (↑s : set K)) a) y hy).comp _, exact alg_equiv.adjoin_singleton_equiv_adjoin_root_minpoly (algebra.adjoin F (↑s : set K)) a end end embeddings namespace polynomial variables [field K] [field L] [field F] open polynomial section splitting_field /-- Non-computably choose an irreducible factor from a polynomial. -/ def factor (f : K[X]) : K[X] := if H : ∃ g, irreducible g ∧ g ∣ f then classical.some H else X lemma irreducible_factor (f : K[X]) : irreducible (factor f) := begin rw factor, split_ifs with H, { exact (classical.some_spec H).1 }, { exact irreducible_X } end /-- See note [fact non-instances]. -/ lemma fact_irreducible_factor (f : K[X]) : fact (irreducible (factor f)) := ⟨irreducible_factor f⟩ local attribute [instance] fact_irreducible_factor theorem factor_dvd_of_not_is_unit {f : K[X]} (hf1 : ¬is_unit f) : factor f ∣ f := begin by_cases hf2 : f = 0, { rw hf2, exact dvd_zero _ }, rw [factor, dif_pos (wf_dvd_monoid.exists_irreducible_factor hf1 hf2)], exact (classical.some_spec $ wf_dvd_monoid.exists_irreducible_factor hf1 hf2).2 end theorem factor_dvd_of_degree_ne_zero {f : K[X]} (hf : f.degree ≠ 0) : factor f ∣ f := factor_dvd_of_not_is_unit (mt degree_eq_zero_of_is_unit hf) theorem factor_dvd_of_nat_degree_ne_zero {f : K[X]} (hf : f.nat_degree ≠ 0) : factor f ∣ f := factor_dvd_of_degree_ne_zero (mt nat_degree_eq_of_degree_eq_some hf) /-- Divide a polynomial f by X - C r where r is a root of f in a bigger field extension. -/ def remove_factor (f : K[X]) : polynomial (adjoin_root $ factor f) := map (adjoin_root.of f.factor) f /ₘ (X - C (adjoin_root.root f.factor)) theorem X_sub_C_mul_remove_factor (f : K[X]) (hf : f.nat_degree ≠ 0) : (X - C (adjoin_root.root f.factor)) * f.remove_factor = map (adjoin_root.of f.factor) f := let ⟨g, hg⟩ := factor_dvd_of_nat_degree_ne_zero hf in mul_div_by_monic_eq_iff_is_root.2 $ by rw [is_root.def, eval_map, hg, eval₂_mul, ← hg, adjoin_root.eval₂_root, zero_mul] theorem nat_degree_remove_factor (f : K[X]) : f.remove_factor.nat_degree = f.nat_degree - 1 := by rw [remove_factor, nat_degree_div_by_monic _ (monic_X_sub_C _), nat_degree_map, nat_degree_X_sub_C] theorem nat_degree_remove_factor' {f : K[X]} {n : ℕ} (hfn : f.nat_degree = n+1) : f.remove_factor.nat_degree = n := by rw [nat_degree_remove_factor, hfn, n.add_sub_cancel] /-- Auxiliary construction to a splitting field of a polynomial, which removes `n` (arbitrarily-chosen) factors. Uses recursion on the degree. For better definitional behaviour, structures including `splitting_field_aux` (such as instances) should be defined using this recursion in each field, rather than defining the whole tuple through recursion. -/ def splitting_field_aux (n : ℕ) : Π {K : Type u} [field K], by exactI Π (f : K[X]), Type u := nat.rec_on n (λ K _ _, K) $ λ n ih K _ f, by exactI ih f.remove_factor namespace splitting_field_aux theorem succ (n : ℕ) (f : K[X]) : splitting_field_aux (n+1) f = splitting_field_aux n f.remove_factor := rfl instance field (n : ℕ) : Π {K : Type u} [field K], by exactI Π {f : K[X]}, field (splitting_field_aux n f) := nat.rec_on n (λ K _ _, ‹field K›) $ λ n ih K _ f, ih instance inhabited {n : ℕ} {f : K[X]} : inhabited (splitting_field_aux n f) := ⟨37⟩ /- Note that the recursive nature of this definition and `splitting_field_aux.field` creates non-definitionally-equal diamonds in the `ℕ`- and `ℤ`- actions. ```lean example (n : ℕ) {K : Type u} [field K] {f : K[X]} (hfn : f.nat_degree = n) : (add_comm_monoid.nat_module : module ℕ (splitting_field_aux n f hfn)) = @algebra.to_module _ _ _ _ (splitting_field_aux.algebra n _ hfn) := rfl -- fails ``` It's not immediately clear whether this _can_ be fixed; the failure is much the same as the reason that the following fails: ```lean def cases_twice {α} (a₀ aₙ : α) : ℕ → α × α | 0 := (a₀, a₀) | (n + 1) := (aₙ, aₙ) example (x : ℕ) {α} (a₀ aₙ : α) : (cases_twice a₀ aₙ x).1 = (cases_twice a₀ aₙ x).2 := rfl -- fails ``` We don't really care at this point because this is an implementation detail (which is why this is not a docstring), but we do in `splitting_field.algebra'` below. -/ instance algebra (n : ℕ) : Π (R : Type*) {K : Type u} [comm_semiring R] [field K], by exactI Π [algebra R K] {f : K[X]}, algebra R (splitting_field_aux n f) := nat.rec_on n (λ R K _ _ _ _, by exactI ‹algebra R K›) $ λ n ih R K _ _ _ f, by exactI ih R instance is_scalar_tower (n : ℕ) : Π (R₁ R₂ : Type*) {K : Type u} [comm_semiring R₁] [comm_semiring R₂] [has_smul R₁ R₂] [field K], by exactI Π [algebra R₁ K] [algebra R₂ K], by exactI Π [is_scalar_tower R₁ R₂ K] {f : K[X]}, is_scalar_tower R₁ R₂ (splitting_field_aux n f) := nat.rec_on n (λ R₁ R₂ K _ _ _ _ _ _ _ _, by exactI ‹is_scalar_tower R₁ R₂ K›) $ λ n ih R₁ R₂ K _ _ _ _ _ _ _ f, by exactI ih R₁ R₂ instance algebra''' {n : ℕ} {f : K[X]} : algebra (adjoin_root f.factor) (splitting_field_aux n f.remove_factor) := splitting_field_aux.algebra n _ instance algebra' {n : ℕ} {f : K[X]} : algebra (adjoin_root f.factor) (splitting_field_aux n.succ f) := splitting_field_aux.algebra''' instance algebra'' {n : ℕ} {f : K[X]} : algebra K (splitting_field_aux n f.remove_factor) := splitting_field_aux.algebra n K instance scalar_tower' {n : ℕ} {f : K[X]} : is_scalar_tower K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor) := begin -- finding this instance ourselves makes things faster haveI : is_scalar_tower K (adjoin_root f.factor) (adjoin_root f.factor) := is_scalar_tower.right, exact splitting_field_aux.is_scalar_tower n K (adjoin_root f.factor), end instance scalar_tower {n : ℕ} {f : K[X]} : is_scalar_tower K (adjoin_root f.factor) (splitting_field_aux (n + 1) f) := splitting_field_aux.scalar_tower' theorem algebra_map_succ (n : ℕ) (f : K[X]) : by exact algebra_map K (splitting_field_aux (n+1) f) = (algebra_map (adjoin_root f.factor) (splitting_field_aux n f.remove_factor)).comp (adjoin_root.of f.factor) := is_scalar_tower.algebra_map_eq _ _ _ protected theorem splits (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : K[X]) (hfn : f.nat_degree = n), splits (algebra_map K $ splitting_field_aux n f) f := nat.rec_on n (λ K _ _ hf, by exactI splits_of_degree_le_one _ (le_trans degree_le_nat_degree $ hf.symm ▸ with_bot.coe_le_coe.2 zero_le_one)) $ λ n ih K _ f hf, by { resetI, rw [← splits_id_iff_splits, algebra_map_succ, ← map_map, splits_id_iff_splits, ← X_sub_C_mul_remove_factor f (λ h, by { rw h at hf, cases hf })], exact splits_mul _ (splits_X_sub_C _) (ih _ (nat_degree_remove_factor' hf)) } theorem exists_lift (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : K[X]) (hfn : f.nat_degree = n) {L : Type*} [field L], by exactI ∀ (j : K →+* L) (hf : splits j f), ∃ k : splitting_field_aux n f →+* L, k.comp (algebra_map _ _) = j := nat.rec_on n (λ K _ _ _ L _ j _, by exactI ⟨j, j.comp_id⟩) $ λ n ih K _ f hf L _ j hj, by exactI have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hf, cases hf }, have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl }, let ⟨r, hr⟩ := exists_root_of_splits _ (splits_of_splits_of_dvd j hfn0 hj (factor_dvd_of_nat_degree_ne_zero hndf)) (mt is_unit_iff_degree_eq_zero.2 f.irreducible_factor.1) in have hmf0 : map (adjoin_root.of f.factor) f ≠ 0, from map_ne_zero hfn0, have hsf : splits (adjoin_root.lift j r hr) f.remove_factor, by { rw ← X_sub_C_mul_remove_factor _ hndf at hmf0, refine (splits_of_splits_mul _ hmf0 _).2, rwa [X_sub_C_mul_remove_factor _ hndf, ← splits_id_iff_splits, map_map, adjoin_root.lift_comp_of, splits_id_iff_splits] }, let ⟨k, hk⟩ := ih f.remove_factor (nat_degree_remove_factor' hf) (adjoin_root.lift j r hr) hsf in ⟨k, by rw [algebra_map_succ, ← ring_hom.comp_assoc, hk, adjoin_root.lift_comp_of]⟩ theorem adjoin_roots (n : ℕ) : ∀ {K : Type u} [field K], by exactI ∀ (f : K[X]) (hfn : f.nat_degree = n), algebra.adjoin K (↑(f.map $ algebra_map K $ splitting_field_aux n f).roots.to_finset : set (splitting_field_aux n f)) = ⊤ := nat.rec_on n (λ K _ f hf, by exactI algebra.eq_top_iff.2 (λ x, subalgebra.range_le _ ⟨x, rfl⟩)) $ λ n ih K _ f hfn, by exactI have hndf : f.nat_degree ≠ 0, by { intro h, rw h at hfn, cases hfn }, have hfn0 : f ≠ 0, by { intro h, rw h at hndf, exact hndf rfl }, have hmf0 : map (algebra_map K (splitting_field_aux n.succ f)) f ≠ 0 := map_ne_zero hfn0, by { rw [algebra_map_succ, ← map_map, ← X_sub_C_mul_remove_factor _ hndf, polynomial.map_mul] at hmf0 ⊢, rw [roots_mul hmf0, polynomial.map_sub, map_X, map_C, roots_X_sub_C, multiset.to_finset_add, finset.coe_union, multiset.to_finset_singleton, finset.coe_singleton, algebra.adjoin_union_eq_adjoin_adjoin, ← set.image_singleton, algebra.adjoin_algebra_map K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor), adjoin_root.adjoin_root_eq_top, algebra.map_top, is_scalar_tower.adjoin_range_to_alg_hom K (adjoin_root f.factor) (splitting_field_aux n f.remove_factor), ih _ (nat_degree_remove_factor' hfn), subalgebra.restrict_scalars_top] } end splitting_field_aux /-- A splitting field of a polynomial. -/ def splitting_field (f : K[X]) := splitting_field_aux f.nat_degree f namespace splitting_field variables (f : K[X]) instance : field (splitting_field f) := splitting_field_aux.field _ instance inhabited : inhabited (splitting_field f) := ⟨37⟩ /-- This should be an instance globally, but it creates diamonds with the `ℕ`, `ℤ`, and `ℚ` algebras (via their `smul` and `to_fun` fields): ```lean example : (algebra_nat : algebra ℕ (splitting_field f)) = splitting_field.algebra' f := rfl -- fails example : (algebra_int _ : algebra ℤ (splitting_field f)) = splitting_field.algebra' f := rfl -- fails example [char_zero K] [char_zero (splitting_field f)] : (algebra_rat : algebra ℚ (splitting_field f)) = splitting_field.algebra' f := rfl -- fails ``` Until we resolve these diamonds, it's more convenient to only turn this instance on with `local attribute [instance]` in places where the benefit of having the instance outweighs the cost. In the meantime, the `splitting_field.algebra` instance below is immune to these particular diamonds since `K = ℕ` and `K = ℤ` are not possible due to the `field K` assumption. Diamonds in `algebra ℚ (splitting_field f)` instances are still possible via this instance unfortunately, but these are less common as they require suitable `char_zero` instances to be present. -/ instance algebra' {R} [comm_semiring R] [algebra R K] : algebra R (splitting_field f) := splitting_field_aux.algebra _ _ instance : algebra K (splitting_field f) := splitting_field_aux.algebra _ _ protected theorem splits : splits (algebra_map K (splitting_field f)) f := splitting_field_aux.splits _ _ rfl variables [algebra K L] (hb : splits (algebra_map K L) f) /-- Embeds the splitting field into any other field that splits the polynomial. -/ def lift : splitting_field f →ₐ[K] L := { commutes' := λ r, by { have := classical.some_spec (splitting_field_aux.exists_lift _ _ rfl _ hb), exact ring_hom.ext_iff.1 this r }, .. classical.some (splitting_field_aux.exists_lift _ _ _ _ hb) } theorem adjoin_roots : algebra.adjoin K (↑(f.map (algebra_map K $ splitting_field f)).roots.to_finset : set (splitting_field f)) = ⊤ := splitting_field_aux.adjoin_roots _ _ rfl theorem adjoin_root_set : algebra.adjoin K (f.root_set f.splitting_field) = ⊤ := adjoin_roots f end splitting_field variables (K L) [algebra K L] /-- Typeclass characterising splitting fields. -/ class is_splitting_field (f : K[X]) : Prop := (splits [] : splits (algebra_map K L) f) (adjoin_roots [] : algebra.adjoin K (↑(f.map (algebra_map K L)).roots.to_finset : set L) = ⊤) namespace is_splitting_field variables {K} instance splitting_field (f : K[X]) : is_splitting_field K (splitting_field f) f := ⟨splitting_field.splits f, splitting_field.adjoin_roots f⟩ section scalar_tower variables {K L F} [algebra F K] [algebra F L] [is_scalar_tower F K L] variables {K} instance map (f : F[X]) [is_splitting_field F L f] : is_splitting_field K L (f.map $ algebra_map F K) := ⟨by { rw [splits_map_iff, ← is_scalar_tower.algebra_map_eq], exact splits L f }, subalgebra.restrict_scalars_injective F $ by { rw [map_map, ← is_scalar_tower.algebra_map_eq, subalgebra.restrict_scalars_top, eq_top_iff, ← adjoin_roots L f, algebra.adjoin_le_iff], exact λ x hx, @algebra.subset_adjoin K _ _ _ _ _ _ hx }⟩ variables {K} (L) theorem splits_iff (f : K[X]) [is_splitting_field K L f] : polynomial.splits (ring_hom.id K) f ↔ (⊤ : subalgebra K L) = ⊥ := ⟨λ h, eq_bot_iff.2 $ adjoin_roots L f ▸ (roots_map (algebra_map K L) h).symm ▸ algebra.adjoin_le_iff.2 (λ y hy, let ⟨x, hxs, hxy⟩ := finset.mem_image.1 (by rwa multiset.to_finset_map at hy) in hxy ▸ set_like.mem_coe.2 $ subalgebra.algebra_map_mem _ _), λ h, @ring_equiv.to_ring_hom_refl K _ ▸ ring_equiv.self_trans_symm (ring_equiv.of_bijective _ $ algebra.bijective_algebra_map_iff.2 h) ▸ by { rw ring_equiv.to_ring_hom_trans, exact splits_comp_of_splits _ _ (splits L f) }⟩ theorem mul (f g : F[X]) (hf : f ≠ 0) (hg : g ≠ 0) [is_splitting_field F K f] [is_splitting_field K L (g.map $ algebra_map F K)] : is_splitting_field F L (f * g) := ⟨(is_scalar_tower.algebra_map_eq F K L).symm ▸ splits_mul _ (splits_comp_of_splits _ _ (splits K f)) ((splits_map_iff _ _).1 (splits L $ g.map $ algebra_map F K)), by rw [polynomial.map_mul, roots_mul (mul_ne_zero (map_ne_zero hf : f.map (algebra_map F L) ≠ 0) (map_ne_zero hg)), multiset.to_finset_add, finset.coe_union, algebra.adjoin_union_eq_adjoin_adjoin, is_scalar_tower.algebra_map_eq F K L, ← map_map, roots_map (algebra_map K L) ((splits_id_iff_splits $ algebra_map F K).2 $ splits K f), multiset.to_finset_map, finset.coe_image, algebra.adjoin_algebra_map, adjoin_roots, algebra.map_top, is_scalar_tower.adjoin_range_to_alg_hom, ← map_map, adjoin_roots, subalgebra.restrict_scalars_top]⟩ end scalar_tower /-- Splitting field of `f` embeds into any field that splits `f`. -/ def lift [algebra K F] (f : K[X]) [is_splitting_field K L f] (hf : polynomial.splits (algebra_map K F) f) : L →ₐ[K] F := if hf0 : f = 0 then (algebra.of_id K F).comp $ (algebra.bot_equiv K L : (⊥ : subalgebra K L) →ₐ[K] K).comp $ by { rw ← (splits_iff L f).1 (show f.splits (ring_hom.id K), from hf0.symm ▸ splits_zero _), exact algebra.to_top } else alg_hom.comp (by { rw ← adjoin_roots L f, exact classical.choice (lift_of_splits _ $ λ y hy, have aeval y f = 0, from (eval₂_eq_eval_map _).trans $ (mem_roots $ by exact map_ne_zero hf0).1 (multiset.mem_to_finset.mp hy), ⟨is_algebraic_iff_is_integral.1 ⟨f, hf0, this⟩, splits_of_splits_of_dvd _ hf0 hf $ minpoly.dvd _ _ this⟩) }) algebra.to_top theorem finite_dimensional (f : K[X]) [is_splitting_field K L f] : finite_dimensional K L := ⟨@algebra.top_to_submodule K L _ _ _ ▸ adjoin_roots L f ▸ fg_adjoin_of_finite (finset.finite_to_set _) (λ y hy, if hf : f = 0 then by { rw [hf, polynomial.map_zero, roots_zero] at hy, cases hy } else is_algebraic_iff_is_integral.1 ⟨f, hf, (eval₂_eq_eval_map _).trans $ (mem_roots $ by exact map_ne_zero hf).1 (multiset.mem_to_finset.mp hy)⟩)⟩ instance (f : K[X]) : _root_.finite_dimensional K f.splitting_field := finite_dimensional f.splitting_field f /-- Any splitting field is isomorphic to `splitting_field f`. -/ def alg_equiv (f : K[X]) [is_splitting_field K L f] : L ≃ₐ[K] splitting_field f := begin refine alg_equiv.of_bijective (lift L f $ splits (splitting_field f) f) ⟨ring_hom.injective (lift L f $ splits (splitting_field f) f).to_ring_hom, _⟩, haveI := finite_dimensional (splitting_field f) f, haveI := finite_dimensional L f, have : finite_dimensional.finrank K L = finite_dimensional.finrank K (splitting_field f) := le_antisymm (linear_map.finrank_le_finrank_of_injective (show function.injective (lift L f $ splits (splitting_field f) f).to_linear_map, from ring_hom.injective (lift L f $ splits (splitting_field f) f : L →+* f.splitting_field))) (linear_map.finrank_le_finrank_of_injective (show function.injective (lift (splitting_field f) f $ splits L f).to_linear_map, from ring_hom.injective (lift (splitting_field f) f $ splits L f : f.splitting_field →+* L))), change function.surjective (lift L f $ splits (splitting_field f) f).to_linear_map, refine (linear_map.injective_iff_surjective_of_finrank_eq_finrank this).1 _, exact ring_hom.injective (lift L f $ splits (splitting_field f) f : L →+* f.splitting_field) end lemma of_alg_equiv [algebra K F] (p : K[X]) (f : F ≃ₐ[K] L) [is_splitting_field K F p] : is_splitting_field K L p := begin split, { rw ← f.to_alg_hom.comp_algebra_map, exact splits_comp_of_splits _ _ (splits F p) }, { rw [←(algebra.range_top_iff_surjective f.to_alg_hom).mpr f.surjective, ←root_set, adjoin_root_set_eq_range (splits F p), root_set, adjoin_roots F p] }, end end is_splitting_field end splitting_field end polynomial namespace intermediate_field open polynomial variables [field K] [field L] [algebra K L] {p : polynomial K} lemma splits_of_splits {F : intermediate_field K L} (h : p.splits (algebra_map K L)) (hF : ∀ x ∈ p.root_set L, x ∈ F) : p.splits (algebra_map K F) := begin simp_rw [root_set, finset.mem_coe, multiset.mem_to_finset] at hF, rw splits_iff_exists_multiset, refine ⟨multiset.pmap subtype.mk _ hF, map_injective _ (algebra_map F L).injective _⟩, conv_lhs { rw [polynomial.map_map, ←is_scalar_tower.algebra_map_eq, eq_prod_roots_of_splits h, ←multiset.pmap_eq_map _ _ _ hF] }, simp_rw [polynomial.map_mul, polynomial.map_multiset_prod, multiset.map_pmap, polynomial.map_sub, map_C, map_X], refl, end end intermediate_field
85de35d44a99843c5a13f79c28b9a45eff7bded9
63abd62053d479eae5abf4951554e1064a4c45b4
/test/tactics.lean
09d52ccfcebe8bda7463d66738170332fbff26ac
[ "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
16,297
lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Scott Morrison -/ import tactic.interactive import tactic.finish import tactic.ext import tactic.lift import tactic.apply import tactic.reassoc_axiom import tactic.tfae import tactic.elide import tactic.ring_exp import tactic.clear import tactic.simp_rw example (m n p q : nat) (h : m + n = p) : true := begin have : m + n = q, { generalize_hyp h' : m + n = x at h, guard_hyp h' : m + n = x, guard_hyp h : x = p, guard_target m + n = q, admit }, have : m + n = q, { generalize_hyp h' : m + n = x at h ⊢, guard_hyp h' : m + n = x, guard_hyp h : x = p, guard_target x = q, admit }, trivial end example (α : Sort*) (L₁ L₂ L₃ : list α) (H : L₁ ++ L₂ = L₃) : true := begin have : L₁ ++ L₂ = L₂, { generalize_hyp h : L₁ ++ L₂ = L at H, induction L with hd tl ih, case list.nil { tactic.cleanup, change list.nil = L₃ at H, admit }, case list.cons { change list.cons hd tl = L₃ at H, admit } }, trivial end example (x y : ℕ) (p q : Prop) (h : x = y) (h' : p ↔ q) : true := begin symmetry' at h, guard_hyp' h : y = x, guard_hyp' h' : p ↔ q, symmetry' at *, guard_hyp' h : x = y, guard_hyp' h' : q ↔ p, trivial end section h_generalize variables {α β γ φ ψ : Type} (f : α → α → α → φ → γ) (x y : α) (a b : β) (z : φ) (h₀ : β = α) (h₁ : β = α) (h₂ : φ = β) (hx : x == a) (hy : y == b) (hz : z == a) include f x y z a b hx hy hz example : f x y x z = f (eq.rec_on h₀ a) (cast h₀ b) (eq.mpr h₁.symm a) (eq.mpr h₂ a) := begin guard_hyp_nums 16, h_generalize hp : a == p with hh, guard_hyp_nums 19, guard_hyp' hh : β = α, guard_target f x y x z = f p (cast h₀ b) p (eq.mpr h₂ a), h_generalize hq : _ == q, guard_hyp_nums 21, guard_target f x y x z = f p q p (eq.mpr h₂ a), h_generalize _ : _ == r, guard_hyp_nums 23, guard_target f x y x z = f p q p r, casesm* [_ == _, _ = _], refl end end h_generalize section h_generalize variables {α β γ φ ψ : Type} (f : list α → list α → γ) (x : list α) (a : list β) (z : φ) (h₀ : β = α) (h₁ : list β = list α) (hx : x == a) include f x z a hx h₀ h₁ example : true := begin have : f x x = f (eq.rec_on h₀ a) (cast h₁ a), { guard_hyp_nums 11, h_generalize : a == p with _, guard_hyp_nums 13, guard_hyp' h : β = α, guard_target f x x = f p (cast h₁ a), h_generalize! : a == q , guard_hyp_nums 13, guard_target ∀ q, f x x = f p q, casesm* [_ == _, _ = _], success_if_fail { refl }, admit }, trivial end end h_generalize section tfae example (p q r s : Prop) (h₀ : p ↔ q) (h₁ : q ↔ r) (h₂ : r ↔ s) : p ↔ s := begin scc, end example (p' p q r r' s s' : Prop) (h₀ : p' → p) (h₀ : p → q) (h₁ : q → r) (h₁ : r' → r) (h₂ : r ↔ s) (h₂ : s → p) (h₂ : s → s') : p ↔ s := begin scc, end example (p' p q r r' s s' : Prop) (h₀ : p' → p) (h₀ : p → q) (h₁ : q → r) (h₁ : r' → r) (h₂ : r ↔ s) (h₂ : s → p) (h₂ : s → s') : p ↔ s := begin scc', assumption end example : tfae [true, ∀ n : ℕ, 0 ≤ n * n, true, true] := begin tfae_have : 3 → 1, { intro h, constructor }, tfae_have : 2 → 3, { intro h, constructor }, tfae_have : 2 ← 1, { intros h n, apply nat.zero_le }, tfae_have : 4 ↔ 2, { tauto }, tfae_finish, end example : tfae [] := begin tfae_finish, end variables P Q R : Prop example (pq : P → Q) (qr : Q → R) (rp : R → P) : tfae [P, Q, R] := begin tfae_finish end example (pq : P ↔ Q) (qr : Q ↔ R) : tfae [P, Q, R] := begin tfae_finish -- the success or failure of this tactic is nondeterministic! end example (p : unit → Prop) : tfae [p (), p ()] := begin tfae_have : 1 ↔ 2, from iff.rfl, tfae_finish end end tfae section clear_aux_decl example (n m : ℕ) (h₁ : n = m) (h₂ : ∃ a : ℕ, a = n ∧ a = m) : 2 * m = 2 * n := let ⟨a, ha⟩ := h₂ in begin clear_aux_decl, -- subst will fail without this line subst h₁ end example (x y : ℕ) (h₁ : ∃ n : ℕ, n * 1 = 2) (h₂ : 1 + 1 = 2 → x * 1 = y) : x = y := let ⟨n, hn⟩ := h₁ in begin clear_aux_decl, -- finish produces an error without this line finish end end clear_aux_decl section swap example {α₁ α₂ α₃ : Type} : true := by {have : α₁, have : α₂, have : α₃, swap, swap, rotate, rotate, rotate, rotate 2, rotate 2, triv, recover} end swap section lift example (n m k x z u : ℤ) (hn : 0 < n) (hk : 0 ≤ k + n) (hu : 0 ≤ u) (h : k + n = 2 + x) (f : false) : k + n = m + x := begin lift n to ℕ using le_of_lt hn, guard_target (k + ↑n = m + x), guard_hyp hn : (0 : ℤ) < ↑n, lift m to ℕ, guard_target (k + ↑n = ↑m + x), tactic.swap, guard_target (0 ≤ m), tactic.swap, tactic.num_goals >>= λ n, guard (n = 2), lift (k + n) to ℕ using hk with l hl, guard_hyp l : ℕ, guard_hyp hl : ↑l = k + ↑n, guard_target (↑l = ↑m + x), tactic.success_if_fail (tactic.get_local `hk), lift x to ℕ with y hy, guard_hyp y : ℕ, guard_hyp hy : ↑y = x, guard_target (↑l = ↑m + x), lift z to ℕ with w, guard_hyp w : ℕ, tactic.success_if_fail (tactic.get_local `z), lift u to ℕ using hu with u rfl hu, guard_hyp hu : (0 : ℤ) ≤ ↑u, all_goals { exfalso, assumption }, end -- test lift of functions example (α : Type*) (f : α → ℤ) (hf : ∀ a, 0 ≤ f a) (hf' : ∀ a, f a < 1) (a : α) : 0 ≤ 2 * f a := begin lift f to α → ℕ using hf, guard_target ((0:ℤ) ≤ 2 * (λ i : α, (f i : ℤ)) a), guard_hyp hf' : ∀ a, ((λ i : α, (f i:ℤ)) a) < 1, exact int.coe_nat_nonneg _ end instance can_lift_unit : can_lift unit unit := ⟨id, λ x, true, λ x _, ⟨x, rfl⟩⟩ /- test whether new instances of `can_lift` are added as simp lemmas -/ run_cmd do l ← can_lift_attr.get_cache, guard (`can_lift_unit ∈ l) /- test error messages -/ example (n : ℤ) (hn : 0 < n) : true := begin success_if_fail_with_msg {lift n to ℕ using hn} "lift tactic failed. The type of\n hn\nis 0 < n\nbut it is expected to be\n 0 ≤ n", success_if_fail_with_msg {lift (n : option ℤ) to ℕ} "Failed to find a lift from option ℤ to ℕ. Provide an instance of\n can_lift (option ℤ) ℕ", trivial end example (n : ℤ) : ℕ := begin success_if_fail_with_msg {lift n to ℕ} "lift tactic failed. Tactic is only applicable when the target is a proposition.", exact 0 end end lift private meta def get_exception_message (t : lean.parser unit) : lean.parser string | s := match t s with | result.success a s' := result.success "No exception" s | result.exception none pos s' := result.success "Exception no msg" s | result.exception (some msg) pos s' := result.success (msg ()).to_string s end @[user_command] meta def test_parser1_fail_cmd (_ : interactive.parse (lean.parser.tk "test_parser1")) : lean.parser unit := do let msg := "oh, no!", let t : lean.parser unit := tactic.fail msg, s ← get_exception_message t, if s = msg then tactic.skip else interaction_monad.fail "Message was corrupted while being passed through `lean.parser.of_tactic`" . -- Due to `lean.parser.of_tactic'` priority, the following *should not* fail with -- a VM check error, and instead catch the error gracefully and just -- run and succeed silently. test_parser1 section category_theory open category_theory variables {C : Type} [category.{1} C] example (X Y Z W : C) (x : X ⟶ Y) (y : Y ⟶ Z) (z z' : Z ⟶ W) (w : X ⟶ Z) (h : x ≫ y = w) (h' : y ≫ z = y ≫ z') : x ≫ y ≫ z = w ≫ z' := begin rw [h',reassoc_of h], end end category_theory section is_eta_expansion /- test the is_eta_expansion tactic -/ open function tactic structure my_equiv (α : Sort*) (β : Sort*) := (to_fun : α → β) (inv_fun : β → α) (left_inv : left_inverse inv_fun to_fun) (right_inv : right_inverse inv_fun to_fun) infix ` my≃ `:25 := my_equiv protected def my_rfl {α} : α my≃ α := ⟨id, λ x, x, λ x, rfl, λ x, rfl⟩ def eta_expansion_test : ℕ × ℕ := ((1,0).1,(1,0).2) run_cmd do e ← get_env, x ← e.get `eta_expansion_test, let v := (x.value.get_app_args).drop 2, let nms := [`prod.fst, `prod.snd], guard $ expr.is_eta_expansion_test (nms.zip v) = some `((1, 0)) def eta_expansion_test2 : ℕ my≃ ℕ := ⟨my_rfl.to_fun, my_rfl.inv_fun, λ x, rfl, λ x, rfl⟩ run_cmd do e ← get_env, x ← e.get `eta_expansion_test2, let v := (x.value.get_app_args).drop 2, projs ← e.structure_fields_full `my_equiv, b ← expr.is_eta_expansion_aux x.value (projs.zip v), guard $ b = some `(@my_rfl ℕ) run_cmd do e ← get_env, x1 ← e.get `eta_expansion_test, x2 ← e.get `eta_expansion_test2, b1 ← expr.is_eta_expansion x1.value, b2 ← expr.is_eta_expansion x2.value, guard $ b1 = some `((1, 0)) ∧ b2 = some `(@my_rfl ℕ) structure my_str (n : ℕ) := (x y : ℕ) def dummy : my_str 3 := ⟨1, 1⟩ def wrong_param : my_str 2 := ⟨dummy.1, dummy.2⟩ def right_param : my_str 3 := ⟨dummy.1, dummy.2⟩ run_cmd do e ← get_env, x ← e.get `wrong_param, o ← x.value.is_eta_expansion, guard o.is_none, x ← e.get `right_param, o ← x.value.is_eta_expansion, guard $ o = some `(dummy) end is_eta_expansion section elide variables {x y z w : ℕ} variables (h : x + y + z ≤ w) (h' : x ≤ y + z + w) include h h' example : x + y + z ≤ w := begin elide 0 at h, elide 2 at h', guard_hyp h : @hidden _ (x + y + z ≤ w), guard_hyp h' : x ≤ @has_add.add (@hidden Type nat) (@hidden (has_add nat) nat.has_add) (@hidden ℕ (y + z)) (@hidden ℕ w), unelide at h, unelide at h', guard_hyp h' : x ≤ y + z + w, exact h, -- there was a universe problem in `elide`. `exact h` lets the kernel check -- the consistency of the universes end end elide section struct_eq @[ext] structure foo (α : Type*) := (x y : ℕ) (z : {z // z < x}) (k : α) (h : x < y) example {α : Type*} : Π (x y : foo α), x.x = y.x → x.y = y.y → x.z == y.z → x.k = y.k → x = y := foo.ext example {α : Type*} : Π (x y : foo α), x = y ↔ x.x = y.x ∧ x.y = y.y ∧ x.z == y.z ∧ x.k = y.k := foo.ext_iff example {α} (x y : foo α) (h : x = y) : y = x := begin ext, { guard_target' y.x = x.x, rw h }, { guard_target' y.y = x.y, rw h }, { guard_target' y.z == x.z, rw h }, { guard_target' y.k = x.k, rw h }, end end struct_eq section ring_exp example (a b : ℤ) (n : ℕ) : (a + b)^(n + 2) = (a^2 + 2 * a * b + b^2) * (a + b)^n := by ring_exp end ring_exp section clear' example (a : ℕ) (b : fin a) : unit := begin success_if_fail { clear a b }, -- fails since `b` depends on `a` success_if_fail { clear' a }, -- fails since `b` depends on `a` clear' a b, guard_hyp_nums 0, exact () end example (a : ℕ) : fin a → unit := begin success_if_fail { clear' a }, -- fails since the target depends on `a` success_if_fail { clear_dependent a }, -- ditto exact λ _, () end example (a : unit) : unit := begin -- Check we fail with an error (but don't segfault) if hypotheses are repeated. success_if_fail { clear' a a }, success_if_fail { clear_dependent a a }, exact () end example (a a a : unit) : unit := begin -- If there are multiple hypotheses with the same name, -- `clear'`/`clear_dependent` currently clears only the last. clear' a, clear_dependent a, guard_hyp_nums 1, exact () end end clear' section clear_dependent example (a : ℕ) (b : fin a) : unit := begin success_if_fail { clear' a }, -- fails since `b` depends on `a` clear_dependent a, guard_hyp_nums 0, exact () end end clear_dependent section simp_rw example {α β : Type} {f : α → β} {t : set β} : (∀ s, f '' s ⊆ t) = ∀ s : set α, ∀ x ∈ s, x ∈ f ⁻¹' t := by simp_rw [set.image_subset_iff, set.subset_def] end simp_rw section local_definitions /- Some tactics about local definitions. Testing revert_deps, revert_after, generalize', clear_value. -/ open tactic example {A : ℕ → Type} {n : ℕ} : let k := n + 3, l := k + n, f : A k → A k := id in ∀(x : A k) (y : A (n + k)) (z : A n) (h : k = n + n), unit := begin intros, guard_target unit, do { e ← get_local `k, e1 ← tactic.local_def_value e, e2 ← to_expr ```(n + 3), guard $ e1 = e2 }, do { e ← get_local `n, success_if_fail_with_msg (tactic.local_def_value e) "Variable n is not a local definition." }, do { success_if_fail_with_msg (tactic.local_def_value `(1 + 2)) "No such hypothesis 1 + 2." }, revert_deps k, tactic.intron 5, guard_target unit, revert_after n, tactic.intron 7, guard_target unit, do { e ← get_local `k, tactic.revert_reverse_dependencies_of_hyp e, l ← local_context, guard $ e ∈ l, intros }, exact unit.star end example {A : ℕ → Type} {n : ℕ} : let k := n + 3, l := k + n, f : A k → A (n+3) := id in ∀(x : A k) (y : A (n + k)) (z : A n) (h : k = n + n), unit := begin intros, success_if_fail_with_msg {generalize : n + k = x} "generalize tactic failed, failed to find expression in the target", generalize' : n + k = x, generalize' h : n + k = y, exact unit.star end example {A : ℕ → Type} {n : ℕ} : let k := n + 3, l := k + n, f : A k → A (n+3) := id in ∀(x : A k) (y : A (n + k)) (z : A n) (h : k = n + n), unit := begin intros, tactic.to_expr ```(n + n) >>= λ e, tactic.generalize' e `xxx, success_if_fail_with_msg {clear_value n} "Cannot clear the body of n. It is not a local definition.", success_if_fail_with_msg {clear_value k} "Cannot clear the body of k. The resulting goal is not type correct.", clear_value k f, get_local `k, -- test that `k` is not renamed. exact unit.star end example {A : ℕ → Type} {n : ℕ} : let k := n + 3, l := k + n, f : A k → A k := id in ∀(x : A k) (y : A (n + k)) (z : A n) (h : k = n + n), unit := begin intros, clear_value k f, exact unit.star end /-- test `clear_value` and the preservation of naming -/ example : ∀ x y : ℤ, let z := x + y in x = z - y → x = y - z → true := begin introv h h, guard_hyp x : ℤ, guard_hyp y : ℤ, guard_hyp z : ℤ := x + y, guard_hyp h : x = y - z, suffices : true, -- test the type of the second assumption named `h` { clear h, guard_hyp h : x = z - y, assumption }, do { to_expr ```(z) >>= is_local_def }, clear_value z, guard_hyp z : ℤ, success_if_fail { do { to_expr ```(z) >>= is_local_def } }, guard_hyp h : x = y - z, suffices : true, { clear h, guard_hyp h : x = z - y, assumption }, trivial end /- Test whether generalize' always uses the exact name stated by the user, even if that name already exists. -/ example (n : Type) (k : ℕ) : k = 5 → unit := begin generalize' : 5 = n, guard_target (k = n → unit), intro, constructor end /- Test that `generalize'` works correctly with argument `h`, when the expression occurs in the target -/ example (n : Type) (k : ℕ) : k = 5 → unit := begin generalize' h : 5 = n, guard_target (k = n → unit), intro, constructor end end local_definitions section set_attribute open tactic @[user_attribute] meta def my_user_attribute : user_attribute unit bool := { name := `my_attr, descr := "", parser := return ff } run_cmd do nm ← get_user_attribute_name `library_note, guard $ nm = `library_note_attr run_cmd do nm ← get_user_attribute_name `higher_order, guard $ nm = `tactic.higher_order_attr run_cmd do success_if_fail $ get_user_attribute_name `zxy.xzy run_cmd set_attribute `norm `prod.map tt run_cmd success_if_fail $ set_attribute `higher_order `prod.map tt run_cmd success_if_fail $ set_attribute `my_attr `prod.map run_cmd success_if_fail $ set_attribute `norm `xyz.zxy run_cmd success_if_fail $ set_attribute `zxy.xyz `prod.map end set_attribute
374e06e69f5e038abe0bc44f9076ba93bc2e6fcc
5fbbd711f9bfc21ee168f46a4be146603ece8835
/lean/natural_number_game/inequality/05.lean
8d5c90e64b7e0235aa3ffbf69e4aa9e5482060e8
[ "LicenseRef-scancode-warranty-disclaimer" ]
no_license
goedel-gang/maths
22596f71e3fde9c088e59931f128a3b5efb73a2c
a20a6f6a8ce800427afd595c598a5ad43da1408d
refs/heads/master
1,623,055,941,960
1,621,599,441,000
1,621,599,441,000
169,335,840
0
0
null
null
null
null
UTF-8
Lean
false
false
172
lean
theorem le_trans (a b c : mynat) (hab : a ≤ b) (hbc : b ≤ c) : a ≤ c := begin cases hab with n hn, cases hbc with m hm, use (m + n), rw [hm, hn], ring, end
f9cc18fe0e9354c9e62444d737809a078c4fde66
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/algebraic_geometry/presheafed_space.lean
0a4e7077b5e377fab1fa952cb9f01c5b0cd844a6
[ "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
7,586
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import topology.Top.presheaf /-! # Presheafed spaces Introduces the category of topological spaces equipped with a presheaf (taking values in an arbitrary target category `C`.) We further describe how to apply functors and natural transformations to the values of the presheaves. -/ universes v u open category_theory open Top open topological_space open opposite open category_theory.category category_theory.functor variables (C : Type u) [𝒞 : category.{v+1} C] include 𝒞 local attribute [tidy] tactic.op_induction' namespace algebraic_geometry /-- A `PresheafedSpace C` is a topological space equipped with a presheaf of `C`s. -/ structure PresheafedSpace := (to_Top : Top.{v}) (𝒪 : to_Top.presheaf C) variables {C} namespace PresheafedSpace instance coe_to_Top : has_coe (PresheafedSpace.{v} C) Top := { coe := λ X, X.to_Top } @[simp] lemma as_coe (X : PresheafedSpace.{v} C) : X.to_Top = (X : Top.{v}) := rfl @[simp] lemma mk_coe (to_Top) (𝒪) : (({ to_Top := to_Top, 𝒪 := 𝒪 } : PresheafedSpace.{v} C) : Top.{v}) = to_Top := rfl instance (X : PresheafedSpace.{v} C) : topological_space X := X.to_Top.str /-- A morphism between presheafed spaces `X` and `Y` consists of a continuous map `f` between the underlying topological spaces, and a (notice contravariant!) map from the presheaf on `Y` to the pushforward of the presheaf on `X` via `f`. -/ structure hom (X Y : PresheafedSpace.{v} C) := (f : (X : Top.{v}) ⟶ (Y : Top.{v})) (c : Y.𝒪 ⟶ f _* X.𝒪) @[extensionality] lemma ext {X Y : PresheafedSpace.{v} C} (α β : hom X Y) (w : α.f = β.f) (h : α.c ≫ (whisker_right (nat_trans.op (opens.map_iso _ _ w).inv) X.𝒪) = β.c) : α = β := begin cases α, cases β, dsimp [presheaf.pushforward] at *, tidy, -- TODO including `injections` would make tidy work earlier. end . def id (X : PresheafedSpace.{v} C) : hom X X := { f := 𝟙 (X : Top.{v}), c := ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _) } def comp (X Y Z : PresheafedSpace.{v} C) (α : hom X Y) (β : hom Y Z) : hom X Z := { f := α.f ≫ β.f, c := β.c ≫ (whisker_left (opens.map β.f).op α.c) } variables (C) section local attribute [simp] id comp presheaf.pushforward /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ /-- The category of PresheafedSpaces. Morphisms are pairs, a continuous map and a presheaf map from the presheaf on the target to the pushforward of the presheaf on the source. -/ instance category_of_PresheafedSpaces : category (PresheafedSpace.{v} C) := { hom := hom, id := id, comp := comp, id_comp' := λ X Y f, begin ext1, swap, { dsimp, simp only [id_comp] }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, map_id] }, end, comp_id' := λ X Y f, begin ext1, swap, { dsimp, simp only [comp_id] }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, id_comp, map_id] } end, assoc' := λ W X Y Z f g h, begin simp only [true_and, presheaf.pushforward, id, comp, whisker_left_twice, whisker_left_comp, heq_iff_eq, category.assoc], split; refl end } end variables {C} instance {X Y : PresheafedSpace.{v} C} : has_coe (X ⟶ Y) (X.to_Top ⟶ Y.to_Top) := { coe := λ α, α.f } @[simp] lemma hom_mk_coe {X Y : PresheafedSpace.{v} C} (f) (c) : (({ f := f, c := c } : X ⟶ Y) : (X : Top.{v}) ⟶ (Y : Top.{v})) = f := rfl @[simp] lemma f_as_coe {X Y : PresheafedSpace.{v} C} (α : X ⟶ Y) : α.f = (α : (X : Top.{v}) ⟶ (Y : Top.{v})) := rfl @[simp] lemma id_coe (X : PresheafedSpace.{v} C) : (((𝟙 X) : X ⟶ X) : (X : Top.{v}) ⟶ X) = 𝟙 (X : Top.{v}) := rfl @[simp] lemma comp_coe {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) : ((α ≫ β : X ⟶ Z) : (X : Top.{v}) ⟶ Z) = (α : (X : Top.{v}) ⟶ Y) ≫ (β : Y ⟶ Z) := rfl lemma id_c (X : PresheafedSpace.{v} C) : ((𝟙 X) : X ⟶ X).c = (((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _)) := rfl -- Implementation note: this harmless looking lemma causes deterministic timeouts, -- but happily we can survive without it. -- lemma comp_c {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) : -- (α ≫ β).c = (β.c ≫ (whisker_left (opens.map β.f).op α.c)) := rfl @[simp] lemma id_c_app (X : PresheafedSpace.{v} C) (U) : ((𝟙 X) : X ⟶ X).c.app U = eq_to_hom (by { op_induction U, cases U, refl }) := by { op_induction U, cases U, simp only [id_c], dsimp, simp, } @[simp] lemma comp_c_app {X Y Z : PresheafedSpace.{v} C} (α : X ⟶ Y) (β : Y ⟶ Z) (U) : (α ≫ β).c.app U = (β.c).app U ≫ (α.c).app (op ((opens.map (β.f)).obj (unop U))) := rfl /-- The forgetful functor from `PresheafedSpace` to `Top`. -/ def forget : PresheafedSpace.{v} C ⥤ Top := { obj := λ X, (X : Top.{v}), map := λ X Y f, f } end PresheafedSpace end algebraic_geometry open algebraic_geometry algebraic_geometry.PresheafedSpace variables {C} namespace category_theory variables {D : Type u} [𝒟 : category.{v+1} D] include 𝒟 local attribute [simp] presheaf.pushforward namespace functor /-- We can apply a functor `F : C ⥤ D` to the values of the presheaf in any `PresheafedSpace C`, giving a functor `PresheafedSpace C ⥤ PresheafedSpace D` -/ /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ def map_presheaf (F : C ⥤ D) : PresheafedSpace.{v} C ⥤ PresheafedSpace.{v} D := { obj := λ X, { to_Top := X.to_Top, 𝒪 := X.𝒪 ⋙ F }, map := λ X Y f, { f := f.f, c := whisker_right f.c F }, map_id' := λ X, begin ext1, swap, { refl }, { ext1, dsimp, simp only [presheaf.pushforward, eq_to_hom_map, map_id, comp_id, id_c_app], refl } end, map_comp' := λ X Y Z f g, begin ext1, swap, { refl, }, { ext, dsimp, simp only [comp_id, assoc, map_comp, map_id], }, end } @[simp] lemma map_presheaf_obj_X (F : C ⥤ D) (X : PresheafedSpace.{v} C) : ((F.map_presheaf.obj X) : Top.{v}) = (X : Top.{v}) := rfl @[simp] lemma map_presheaf_obj_𝒪 (F : C ⥤ D) (X : PresheafedSpace.{v} C) : (F.map_presheaf.obj X).𝒪 = X.𝒪 ⋙ F := rfl @[simp] lemma map_presheaf_map_f (F : C ⥤ D) {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) : ((F.map_presheaf.map f) : (X : Top.{v}) ⟶ (Y : Top.{v})) = f := rfl @[simp] lemma map_presheaf_map_c (F : C ⥤ D) {X Y : PresheafedSpace.{v} C} (f : X ⟶ Y) : (F.map_presheaf.map f).c = whisker_right f.c F := rfl end functor namespace nat_trans /- The proofs below can be done by `tidy`, but it is too slow, and we don't have a tactic caching mechanism. -/ def on_presheaf {F G : C ⥤ D} (α : F ⟶ G) : G.map_presheaf ⟶ F.map_presheaf := { app := λ X, { f := 𝟙 _, c := whisker_left X.𝒪 α ≫ ((functor.left_unitor _).inv) ≫ (whisker_right (nat_trans.op (opens.map_id _).hom) _) }, naturality' := λ X Y f, begin ext1, swap, { refl }, { ext1 U, op_induction, cases U, dsimp, simp only [comp_id, assoc, map_id, nat_trans.naturality] } end } -- TODO Assemble the last two constructions into a functor -- `(C ⥤ D) ⥤ (PresheafedSpace C ⥤ PresheafedSpace D)` end nat_trans end category_theory
ab885e9a4474bfab718908d75a44cfd3cfcc1f42
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/monoid_algebra/to_direct_sum.lean
4a42590b8590554d6bd2895ebf673b25c7b3d705
[ "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,930
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.direct_sum.algebra import algebra.monoid_algebra.basic import data.finsupp.to_dfinsupp /-! # Conversion between `add_monoid_algebra` and homogenous `direct_sum` > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This module provides conversions between `add_monoid_algebra` and `direct_sum`. The latter is essentially a dependent version of the former. Note that since `direct_sum.has_mul` combines indices additively, there is no equivalent to `monoid_algebra`. ## Main definitions * `add_monoid_algebra.to_direct_sum : add_monoid_algebra M ι → (⨁ i : ι, M)` * `direct_sum.to_add_monoid_algebra : (⨁ i : ι, M) → add_monoid_algebra M ι` * Bundled equiv versions of the above: * `add_monoid_algebra_equiv_direct_sum : add_monoid_algebra M ι ≃ (⨁ i : ι, M)` * `add_monoid_algebra_add_equiv_direct_sum : add_monoid_algebra M ι ≃+ (⨁ i : ι, M)` * `add_monoid_algebra_ring_equiv_direct_sum R : add_monoid_algebra M ι ≃+* (⨁ i : ι, M)` * `add_monoid_algebra_alg_equiv_direct_sum R : add_monoid_algebra A ι ≃ₐ[R] (⨁ i : ι, A)` ## Theorems The defining feature of these operations is that they map `finsupp.single` to `direct_sum.of` and vice versa: * `add_monoid_algebra.to_direct_sum_single` * `direct_sum.to_add_monoid_algebra_of` as well as preserving arithmetic operations. For the bundled equivalences, we provide lemmas that they reduce to `add_monoid_algebra.to_direct_sum`: * `add_monoid_algebra_add_equiv_direct_sum_apply` * `add_monoid_algebra_lequiv_direct_sum_apply` * `add_monoid_algebra_add_equiv_direct_sum_symm_apply` * `add_monoid_algebra_lequiv_direct_sum_symm_apply` ## Implementation notes This file largely just copies the API of `data/finsupp/to_dfinsupp`, and reuses the proofs. Recall that `add_monoid_algebra M ι` is defeq to `ι →₀ M` and `⨁ i : ι, M` is defeq to `Π₀ i : ι, M`. Note that there is no `add_monoid_algebra` equivalent to `finsupp.single`, so many statements still involve this definition. -/ variables {ι : Type*} {R : Type*} {M : Type*} {A : Type*} open_locale direct_sum /-! ### Basic definitions and lemmas -/ section defs /-- Interpret a `add_monoid_algebra` as a homogenous `direct_sum`. -/ def add_monoid_algebra.to_direct_sum [semiring M] (f : add_monoid_algebra M ι) : ⨁ i : ι, M := finsupp.to_dfinsupp f section variables [decidable_eq ι] [semiring M] @[simp] lemma add_monoid_algebra.to_direct_sum_single (i : ι) (m : M) : add_monoid_algebra.to_direct_sum (finsupp.single i m) = direct_sum.of _ i m := finsupp.to_dfinsupp_single i m variables [Π m : M, decidable (m ≠ 0)] /-- Interpret a homogenous `direct_sum` as a `add_monoid_algebra`. -/ def direct_sum.to_add_monoid_algebra (f : ⨁ i : ι, M) : add_monoid_algebra M ι := dfinsupp.to_finsupp f @[simp] lemma direct_sum.to_add_monoid_algebra_of (i : ι) (m : M) : (direct_sum.of _ i m : ⨁ i : ι, M).to_add_monoid_algebra = finsupp.single i m := dfinsupp.to_finsupp_single i m @[simp] lemma add_monoid_algebra.to_direct_sum_to_add_monoid_algebra (f : add_monoid_algebra M ι) : f.to_direct_sum.to_add_monoid_algebra = f := finsupp.to_dfinsupp_to_finsupp f @[simp] lemma direct_sum.to_add_monoid_algebra_to_direct_sum (f : ⨁ i : ι, M) : f.to_add_monoid_algebra.to_direct_sum = f := dfinsupp.to_finsupp_to_dfinsupp f end end defs /-! ### Lemmas about arithmetic operations -/ section lemmas namespace add_monoid_algebra @[simp] lemma to_direct_sum_zero [semiring M] : (0 : add_monoid_algebra M ι).to_direct_sum = 0 := finsupp.to_dfinsupp_zero @[simp] lemma to_direct_sum_add [semiring M] (f g : add_monoid_algebra M ι) : (f + g).to_direct_sum = f.to_direct_sum + g.to_direct_sum := finsupp.to_dfinsupp_add _ _ @[simp] lemma to_direct_sum_mul [decidable_eq ι] [add_monoid ι] [semiring M] (f g : add_monoid_algebra M ι) : (f * g).to_direct_sum = f.to_direct_sum * g.to_direct_sum := begin let to_hom : add_monoid_algebra M ι →+ (⨁ i : ι, M) := ⟨to_direct_sum, to_direct_sum_zero, to_direct_sum_add⟩, show to_hom (f * g) = to_hom f * to_hom g, revert f g, rw add_monoid_hom.map_mul_iff, ext xi xv yi yv : 4, dsimp only [add_monoid_hom.comp_apply, add_monoid_hom.compl₂_apply, add_monoid_hom.compr₂_apply, add_monoid_hom.mul_apply, add_equiv.coe_to_add_monoid_hom, finsupp.single_add_hom_apply], simp only [add_monoid_algebra.single_mul_single, to_hom, add_monoid_hom.coe_mk, add_monoid_algebra.to_direct_sum_single, direct_sum.of_mul_of, has_mul.ghas_mul_mul] end end add_monoid_algebra namespace direct_sum variables [decidable_eq ι] @[simp] lemma to_add_monoid_algebra_zero [semiring M] [Π m : M, decidable (m ≠ 0)] : to_add_monoid_algebra 0 = (0 : add_monoid_algebra M ι) := dfinsupp.to_finsupp_zero @[simp] lemma to_add_monoid_algebra_add [semiring M] [Π m : M, decidable (m ≠ 0)] (f g : ⨁ i : ι, M) : (f + g).to_add_monoid_algebra = to_add_monoid_algebra f + to_add_monoid_algebra g := dfinsupp.to_finsupp_add _ _ @[simp] lemma to_add_monoid_algebra_mul [add_monoid ι] [semiring M] [Π m : M, decidable (m ≠ 0)] (f g : ⨁ i : ι, M) : (f * g).to_add_monoid_algebra = to_add_monoid_algebra f * to_add_monoid_algebra g := begin apply_fun add_monoid_algebra.to_direct_sum, { simp }, { apply function.left_inverse.injective, apply add_monoid_algebra.to_direct_sum_to_add_monoid_algebra } end end direct_sum end lemmas /-! ### Bundled `equiv`s -/ section equivs /-- `add_monoid_algebra.to_direct_sum` and `direct_sum.to_add_monoid_algebra` together form an equiv. -/ @[simps {fully_applied := ff}] def add_monoid_algebra_equiv_direct_sum [decidable_eq ι] [semiring M] [Π m : M, decidable (m ≠ 0)] : add_monoid_algebra M ι ≃ (⨁ i : ι, M) := { to_fun := add_monoid_algebra.to_direct_sum, inv_fun := direct_sum.to_add_monoid_algebra, ..finsupp_equiv_dfinsupp } /-- The additive version of `add_monoid_algebra.to_add_monoid_algebra`. Note that this is `noncomputable` because `add_monoid_algebra.has_add` is noncomputable. -/ @[simps {fully_applied := ff}] def add_monoid_algebra_add_equiv_direct_sum [decidable_eq ι] [semiring M] [Π m : M, decidable (m ≠ 0)] : add_monoid_algebra M ι ≃+ (⨁ i : ι, M) := { to_fun := add_monoid_algebra.to_direct_sum, inv_fun := direct_sum.to_add_monoid_algebra, map_add' := add_monoid_algebra.to_direct_sum_add, .. add_monoid_algebra_equiv_direct_sum} /-- The ring version of `add_monoid_algebra.to_add_monoid_algebra`. Note that this is `noncomputable` because `add_monoid_algebra.has_add` is noncomputable. -/ @[simps {fully_applied := ff}] def add_monoid_algebra_ring_equiv_direct_sum [decidable_eq ι] [add_monoid ι] [semiring M] [Π m : M, decidable (m ≠ 0)] : add_monoid_algebra M ι ≃+* ⨁ i : ι, M := { to_fun := add_monoid_algebra.to_direct_sum, inv_fun := direct_sum.to_add_monoid_algebra, map_mul' := add_monoid_algebra.to_direct_sum_mul, ..(add_monoid_algebra_add_equiv_direct_sum : add_monoid_algebra M ι ≃+ ⨁ i : ι, M) } /-- The algebra version of `add_monoid_algebra.to_add_monoid_algebra`. Note that this is `noncomputable` because `add_monoid_algebra.has_add` is noncomputable. -/ @[simps {fully_applied := ff}] def add_monoid_algebra_alg_equiv_direct_sum [decidable_eq ι] [add_monoid ι] [comm_semiring R] [semiring A] [algebra R A] [Π m : A, decidable (m ≠ 0)] : add_monoid_algebra A ι ≃ₐ[R] ⨁ i : ι, A := { to_fun := add_monoid_algebra.to_direct_sum, inv_fun := direct_sum.to_add_monoid_algebra, commutes' := λ r, add_monoid_algebra.to_direct_sum_single _ _, ..(add_monoid_algebra_ring_equiv_direct_sum : add_monoid_algebra A ι ≃+* ⨁ i : ι, A) } end equivs
1d36a6c262cca0965055174c1e24c7ac866bcb2d
1446f520c1db37e157b631385707cc28a17a595e
/src/Init/Lean/LocalContext.lean
40099452dd1ff2fc7e83dd52fabda26c0e4ec244
[ "Apache-2.0" ]
permissive
bdbabiak/lean4
cab06b8a2606d99a168dd279efdd404edb4e825a
3f4d0d78b2ce3ef541cb643bbe21496bd6b057ac
refs/heads/master
1,615,045,275,530
1,583,793,696,000
1,583,793,696,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
11,292
lean
/- Copyright (c) 2019 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import Init.Data.PersistentArray.Basic import Init.Data.PersistentHashMap.Basic import Init.Lean.Expr import Init.Lean.Hygiene namespace Lean inductive LocalDecl | cdecl (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo) | ldecl (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (value : Expr) @[export lean_mk_local_decl] def mkLocalDeclEx (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo) : LocalDecl := LocalDecl.cdecl index fvarId userName type bi @[export lean_mk_let_decl] def mkLetDeclEx (index : Nat) (fvarId : FVarId) (userName : Name) (type : Expr) (val : Expr) : LocalDecl := LocalDecl.ldecl index fvarId userName type val @[export lean_local_decl_binder_info] def LocalDecl.binderInfoEx : LocalDecl → BinderInfo | LocalDecl.cdecl _ _ _ _ bi => bi | _ => BinderInfo.default namespace LocalDecl instance : Inhabited LocalDecl := ⟨ldecl (arbitrary _) (arbitrary _) (arbitrary _) (arbitrary _) (arbitrary _)⟩ def isLet : LocalDecl → Bool | cdecl _ _ _ _ _ => false | ldecl _ _ _ _ _ => true def index : LocalDecl → Nat | cdecl idx _ _ _ _ => idx | ldecl idx _ _ _ _ => idx def fvarId : LocalDecl → FVarId | cdecl _ id _ _ _ => id | ldecl _ id _ _ _ => id def userName : LocalDecl → Name | cdecl _ _ n _ _ => n | ldecl _ _ n _ _ => n def type : LocalDecl → Expr | cdecl _ _ _ t _ => t | ldecl _ _ _ t _ => t def binderInfo : LocalDecl → BinderInfo | cdecl _ _ _ _ bi => bi | ldecl _ _ _ _ _ => BinderInfo.default def value? : LocalDecl → Option Expr | cdecl _ _ _ _ _ => none | ldecl _ _ _ _ v => some v def value : LocalDecl → Expr | cdecl _ _ _ _ _ => panic! "let declaration expected" | ldecl _ _ _ _ v => v def updateUserName : LocalDecl → Name → LocalDecl | cdecl index id _ type bi, userName => cdecl index id userName type bi | ldecl index id _ type val, userName => ldecl index id userName type val def toExpr (decl : LocalDecl) : Expr := mkFVar decl.fvarId end LocalDecl structure LocalContext := (fvarIdToDecl : PersistentHashMap FVarId LocalDecl := {}) (decls : PersistentArray (Option LocalDecl) := {}) namespace LocalContext instance : Inhabited LocalContext := ⟨{}⟩ @[export lean_mk_empty_local_ctx] def mkEmpty : Unit → LocalContext := fun _ => {} def empty : LocalContext := {} @[export lean_local_ctx_is_empty] def isEmpty (lctx : LocalContext) : Bool := lctx.fvarIdToDecl.isEmpty /- Low level API for creating local declarations. It is used to implement actions in the monads `Elab` and `Tactic`. It should not be used directly since the argument `(name : Name)` is assumed to be "unique". -/ @[export lean_local_ctx_mk_local_decl] def mkLocalDecl (lctx : LocalContext) (fvarId : FVarId) (userName : Name) (type : Expr) (bi : BinderInfo := BinderInfo.default) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => let idx := decls.size; let decl := LocalDecl.cdecl idx fvarId userName type bi; { fvarIdToDecl := map.insert fvarId decl, decls := decls.push decl } @[export lean_local_ctx_mk_let_decl] def mkLetDecl (lctx : LocalContext) (fvarId : FVarId) (userName : Name) (type : Expr) (value : Expr) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => let idx := decls.size; let decl := LocalDecl.ldecl idx fvarId userName type value; { fvarIdToDecl := map.insert fvarId decl, decls := decls.push decl } /- Low level API -/ def addDecl (lctx : LocalContext) (newDecl : LocalDecl) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => { fvarIdToDecl := map.insert newDecl.fvarId newDecl, decls := decls.set newDecl.index newDecl } @[export lean_local_ctx_find] def find? (lctx : LocalContext) (fvarId : FVarId) : Option LocalDecl := lctx.fvarIdToDecl.find? fvarId def findFVar? (lctx : LocalContext) (e : Expr) : Option LocalDecl := lctx.find? e.fvarId! def get! (lctx : LocalContext) (fvarId : FVarId) : LocalDecl := match lctx.find? fvarId with | some d => d | none => panic! "unknown free variable" def getFVar! (lctx : LocalContext) (e : Expr) : LocalDecl := lctx.get! e.fvarId! def contains (lctx : LocalContext) (fvarId : FVarId) : Bool := lctx.fvarIdToDecl.contains fvarId def containsFVar (lctx : LocalContext) (e : Expr) : Bool := lctx.contains e.fvarId! private partial def popTailNoneAux : PArray (Option LocalDecl) → PArray (Option LocalDecl) | a => if a.size == 0 then a else match a.get! (a.size - 1) with | none => popTailNoneAux a.pop | some _ => a @[export lean_local_ctx_erase] def erase (lctx : LocalContext) (fvarId : FVarId) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => match map.find? fvarId with | none => lctx | some decl => { fvarIdToDecl := map.erase fvarId, decls := popTailNoneAux (decls.set decl.index none) } @[export lean_local_ctx_pop] def pop (lctx : LocalContext): LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => if decls.size == 0 then lctx else match decls.get! (decls.size - 1) with | none => lctx -- unreachable | some decl => { fvarIdToDecl := map.erase decl.fvarId, decls := popTailNoneAux decls.pop } @[export lean_local_ctx_find_from_user_name] def findFromUserName? (lctx : LocalContext) (userName : Name) : Option LocalDecl := lctx.decls.findSomeRev? (fun decl => match decl with | none => none | some decl => if decl.userName == userName then some decl else none) @[export lean_local_ctx_uses_user_name] def usesUserName (lctx : LocalContext) (userName : Name) : Bool := (lctx.findFromUserName? userName).isSome private partial def getUnusedNameAux (lctx : LocalContext) (suggestion : Name) : Nat → Name × Nat | i => let curr := suggestion.appendIndexAfter i; if lctx.usesUserName curr then getUnusedNameAux (i + 1) else (curr, i + 1) @[export lean_local_ctx_get_unused_name] def getUnusedName (lctx : LocalContext) (suggestion : Name) : Name := let suggestion := suggestion.eraseMacroScopes; if lctx.usesUserName suggestion then (getUnusedNameAux lctx suggestion 1).1 else suggestion @[export lean_local_ctx_last_decl] def lastDecl (lctx : LocalContext) : Option LocalDecl := lctx.decls.get! (lctx.decls.size - 1) @[export lean_local_ctx_rename_user_name] def renameUserName (lctx : LocalContext) (fromName : Name) (toName : Name) : LocalContext := match lctx with | { fvarIdToDecl := map, decls := decls } => match lctx.findFromUserName? fromName with | none => lctx | some decl => let decl := decl.updateUserName toName; { fvarIdToDecl := map.insert decl.fvarId decl, decls := decls.set decl.index decl } @[export lean_local_ctx_num_indices] def numIndices (lctx : LocalContext) : Nat := lctx.decls.size @[export lean_local_ctx_get] def getAt! (lctx : LocalContext) (i : Nat) : Option LocalDecl := lctx.decls.get! i section universes u v variables {m : Type u → Type v} [Monad m] variable {β : Type u} @[specialize] def foldlM (lctx : LocalContext) (f : β → LocalDecl → m β) (b : β) : m β := lctx.decls.foldlM (fun b decl => match decl with | none => pure b | some decl => f b decl) b @[specialize] def forM (lctx : LocalContext) (f : LocalDecl → m β) : m PUnit := lctx.decls.forM $ fun decl => match decl with | none => pure PUnit.unit | some decl => f decl *> pure PUnit.unit @[specialize] def findDeclM? (lctx : LocalContext) (f : LocalDecl → m (Option β)) : m (Option β) := lctx.decls.findSomeM? $ fun decl => match decl with | none => pure none | some decl => f decl @[specialize] def findDeclRevM? (lctx : LocalContext) (f : LocalDecl → m (Option β)) : m (Option β) := lctx.decls.findSomeRevM? $ fun decl => match decl with | none => pure none | some decl => f decl @[specialize] def foldlFromM (lctx : LocalContext) (f : β → LocalDecl → m β) (b : β) (decl : LocalDecl) : m β := lctx.decls.foldlFromM (fun b decl => match decl with | none => pure b | some decl => f b decl) b decl.index end @[inline] def foldl {β} (lctx : LocalContext) (f : β → LocalDecl → β) (b : β) : β := Id.run $ lctx.foldlM f b @[inline] def findDecl? {β} (lctx : LocalContext) (f : LocalDecl → Option β) : Option β := Id.run $ lctx.findDeclM? f @[inline] def findDeclRev? {β} (lctx : LocalContext) (f : LocalDecl → Option β) : Option β := Id.run $ lctx.findDeclRevM? f @[inline] def foldlFrom {β} (lctx : LocalContext) (f : β → LocalDecl → β) (b : β) (decl : LocalDecl) : β := Id.run $ lctx.foldlFromM f b decl partial def isSubPrefixOfAux (a₁ a₂ : PArray (Option LocalDecl)) (exceptFVars : Array Expr) : Nat → Nat → Bool | i, j => if i < a₁.size then match a₁.get! i with | none => isSubPrefixOfAux (i+1) j | some decl₁ => if exceptFVars.any $ fun fvar => fvar.fvarId! == decl₁.fvarId then isSubPrefixOfAux (i+1) j else if j < a₂.size then match a₂.get! j with | none => isSubPrefixOfAux i (j+1) | some decl₂ => if decl₁.fvarId == decl₂.fvarId then isSubPrefixOfAux (i+1) (j+1) else isSubPrefixOfAux i (j+1) else false else true /- Given `lctx₁ - exceptFVars` of the form `(x_1 : A_1) ... (x_n : A_n)`, then return true iff there is a local context `B_1* (x_1 : A_1) ... B_n* (x_n : A_n)` which is a prefix of `lctx₂` where `B_i`'s are (possibly empty) sequences of local declarations. -/ def isSubPrefixOf (lctx₁ lctx₂ : LocalContext) (exceptFVars : Array Expr := #[]) : Bool := isSubPrefixOfAux lctx₁.decls lctx₂.decls exceptFVars 0 0 @[inline] def mkBinding (isLambda : Bool) (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := let b := b.abstract xs; xs.size.foldRev (fun i b => let x := xs.get! i; match lctx.findFVar? x with | some (LocalDecl.cdecl _ _ n ty bi) => let ty := ty.abstractRange i xs; if isLambda then Lean.mkLambda n bi ty b else Lean.mkForall n bi ty b | some (LocalDecl.ldecl _ _ n ty val) => if b.hasLooseBVar 0 then let ty := ty.abstractRange i xs; let val := val.abstractRange i xs; mkLet n ty val b else b | none => panic! "unknown free variable") b def mkLambda (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := mkBinding true lctx xs b def mkForall (lctx : LocalContext) (xs : Array Expr) (b : Expr) : Expr := mkBinding false lctx xs b section universes u variables {m : Type → Type u} [Monad m] @[inline] def anyM (lctx : LocalContext) (p : LocalDecl → m Bool) : m Bool := lctx.decls.anyM $ fun d => match d with | some decl => p decl | none => pure false @[inline] def allM (lctx : LocalContext) (p : LocalDecl → m Bool) : m Bool := lctx.decls.allM $ fun d => match d with | some decl => p decl | none => pure true end @[inline] def any (lctx : LocalContext) (p : LocalDecl → Bool) : Bool := Id.run $ lctx.anyM p @[inline] def all (lctx : LocalContext) (p : LocalDecl → Bool) : Bool := Id.run $ lctx.allM p end LocalContext end Lean
2867ad11108fdd3ce81013dafe878ad941f72aa2
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/combinatorics/quiver/push.lean
9c3b5c09f217b8b21ef567ac719eb93a513a5d3b
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
2,536
lean
/- Copyright (c) 2022 Rémi Bottinelli. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Rémi Bottinelli -/ import combinatorics.quiver.basic /-! # Pushing a quiver structure along a map Given a map `σ : V → W` and a `quiver` instance on `V`, this files defines a `quiver` instance on `W` by associating to each arrow `v ⟶ v'` in `V` an arrow `σ v ⟶ σ v'` in `W`. -/ universes v v₁ v₂ u u₁ u₂ variables {V : Type*} [quiver V] {W : Type*} (σ : V → W) /-- The `quiver` instance obtained by pushing arrows of `V` along the map `σ : V → W` -/ @[nolint unused_arguments] def push (σ : V → W) := W instance [h : nonempty W] : nonempty (push σ) := h /-- The quiver structure obtained by pushing arrows of `V` along the map `σ : V → W` -/ @[nolint has_nonempty_instance] inductive push_quiver {V : Type u} [quiver.{v} V] {W : Type u₂} (σ : V → W) : W → W → Type (max u u₂ v) | arrow {X Y : V} (f : X ⟶ Y) : push_quiver (σ X) (σ Y) instance : quiver (push σ) := ⟨push_quiver σ⟩ namespace push /-- The prefunctor induced by pushing arrows via `σ` -/ def of : V ⥤q push σ := { obj := σ, map := λ X Y f, push_quiver.arrow f } @[simp] lemma of_obj : (of σ).obj = σ := rfl variables {W' : Type*} [quiver W'] (φ : V ⥤q W') (τ : W → W') (h : ∀ x, φ.obj x = τ (σ x) ) include φ h /-- Given a function `τ : W → W'` and a prefunctor `φ : V ⥤q W'`, one can extend `τ` to be a prefunctor `W ⥤q W'` if `τ` and `σ` factorize `φ` at the level of objects, where `W` is given the pushforward quiver structure `push σ`. -/ def lift : push σ ⥤q W' := { obj := τ, map := @push_quiver.rec V _ W σ (λ X Y f, τ X ⟶ τ Y) (λ X Y f, by { rw [←h X, ←h Y], exact φ.map f, }) } lemma lift_obj : (lift σ φ τ h).obj = τ := rfl lemma lift_comp : of σ ⋙q lift σ φ τ h = φ := begin fapply prefunctor.ext, { rintros, simp only [prefunctor.comp_obj], symmetry, exact h X, }, { rintros _ _ f, simp only [prefunctor.comp_map], apply eq_of_heq, iterate 2 { apply (cast_heq _ _).trans }, symmetry, iterate 2 { apply (eq_rec_heq _ _).trans }, refl, }, end lemma lift_unique (Φ : push σ ⥤q W') (Φ₀ : Φ.obj = τ) (Φcomp : of σ ⋙q Φ = φ) : Φ = lift σ φ τ h := begin dsimp only [of,lift], fapply prefunctor.ext, { rintros, simp_rw [←Φ₀], }, { rintros _ _ ⟨⟩, subst_vars, simp only [prefunctor.comp_map, cast_eq], refl, } end end push
85f68f6c0bc93997fa961cb26693f1a0b62392dd
4727251e0cd73359b15b664c3170e5d754078599
/src/ring_theory/witt_vector/init_tail.lean
1333a32ac7a30db08910e153ba46bb80b8a3619d
[ "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
7,063
lean
/- Copyright (c) 2020 Johan Commelin, Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin, Robert Y. Lewis -/ import ring_theory.witt_vector.basic import ring_theory.witt_vector.is_poly /-! # `init` and `tail` Given a Witt vector `x`, we are sometimes interested in its components before and after an index `n`. This file defines those operations, proves that `init` is polynomial, and shows how that polynomial interacts with `mv_polynomial.bind₁`. ## Main declarations * `witt_vector.init n x`: the first `n` coefficients of `x`, as a Witt vector. All coefficients at indices ≥ `n` are 0. * `witt_vector.tail n x`: the complementary part to `init`. All coefficients at indices < `n` are 0, otherwise they are the same as in `x`. * `witt_vector.coeff_add_of_disjoint`: if `x` and `y` are Witt vectors such that for every `n` the `n`-th coefficient of `x` or of `y` is `0`, then the coefficients of `x + y` are just `x.coeff n + y.coeff n`. ## References * [Hazewinkel, *Witt Vectors*][Haze09] * [Commelin and Lewis, *Formalizing the Ring of Witt Vectors*][CL21] -/ variables {p : ℕ} [hp : fact p.prime] (n : ℕ) {R : Type*} [comm_ring R] local notation `𝕎` := witt_vector p -- type as `\bbW` namespace tactic namespace interactive setup_tactic_parser /-- `init_ring` is an auxiliary tactic that discharges goals factoring `init` over ring operations. -/ meta def init_ring (assert : parse (tk "using" *> parser.pexpr)?) : tactic unit := do `[rw ext_iff, intros i, simp only [init, select, coeff_mk], split_ifs with hi; try {refl}], match assert with | none := skip | some e := do `[simp only [add_coeff, mul_coeff, neg_coeff, sub_coeff, nsmul_coeff, zsmul_coeff, pow_coeff], apply eval₂_hom_congr' (ring_hom.ext_int _ _) _ rfl, rintro ⟨b, k⟩ h -], tactic.replace `h ```(%%e p _ h), `[simp only [finset.mem_range, finset.mem_product, true_and, finset.mem_univ] at h, have hk : k < n, by linarith, fin_cases b; simp only [function.uncurry, matrix.cons_val_zero, matrix.head_cons, coeff_mk, matrix.cons_val_one, coeff_mk, hk, if_true]] end end interactive end tactic namespace witt_vector open mv_polynomial open_locale classical noncomputable theory section /-- `witt_vector.select P x`, for a predicate `P : ℕ → Prop` is the Witt vector whose `n`-th coefficient is `x.coeff n` if `P n` is true, and `0` otherwise. -/ def select (P : ℕ → Prop) (x : 𝕎 R) : 𝕎 R := mk p (λ n, if P n then x.coeff n else 0) section select variables (P : ℕ → Prop) /-- The polynomial that witnesses that `witt_vector.select` is a polynomial function. `select_poly n` is `X n` if `P n` holds, and `0` otherwise. -/ def select_poly (n : ℕ) : mv_polynomial ℕ ℤ := if P n then X n else 0 lemma coeff_select (x : 𝕎 R) (n : ℕ) : (select P x).coeff n = aeval x.coeff (select_poly P n) := begin dsimp [select, select_poly], split_ifs with hi, { rw aeval_X }, { rw alg_hom.map_zero } end @[is_poly] lemma select_is_poly (P : ℕ → Prop) : is_poly p (λ R _Rcr x, by exactI select P x) := begin use (select_poly P), rintro R _Rcr x, funext i, apply coeff_select end include hp lemma select_add_select_not : ∀ (x : 𝕎 R), select P x + select (λ i, ¬ P i) x = x := begin ghost_calc _, intro n, simp only [ring_hom.map_add], suffices : (bind₁ (select_poly P)) (witt_polynomial p ℤ n) + (bind₁ (select_poly (λ i, ¬P i))) (witt_polynomial p ℤ n) = witt_polynomial p ℤ n, { apply_fun (aeval x.coeff) at this, simpa only [alg_hom.map_add, aeval_bind₁, ← coeff_select] }, simp only [witt_polynomial_eq_sum_C_mul_X_pow, select_poly, alg_hom.map_sum, alg_hom.map_pow, alg_hom.map_mul, bind₁_X_right, bind₁_C_right, ← finset.sum_add_distrib, ← mul_add], apply finset.sum_congr rfl, refine λ m hm, mul_eq_mul_left_iff.mpr (or.inl _), rw [ite_pow, ite_pow, zero_pow (pow_pos hp.out.pos _)], by_cases Pm : P m, { rw [if_pos Pm, if_neg _, add_zero], exact not_not.mpr Pm }, { rwa [if_neg Pm, if_pos, zero_add] } end lemma coeff_add_of_disjoint (x y : 𝕎 R) (h : ∀ n, x.coeff n = 0 ∨ y.coeff n = 0) : (x + y).coeff n = x.coeff n + y.coeff n := begin let P : ℕ → Prop := λ n, y.coeff n = 0, haveI : decidable_pred P := classical.dec_pred P, set z := mk p (λ n, if P n then x.coeff n else y.coeff n) with hz, have hx : select P z = x, { ext1 n, rw [select, coeff_mk, coeff_mk], split_ifs with hn, { refl }, { rw (h n).resolve_right hn } }, have hy : select (λ i, ¬ P i) z = y, { ext1 n, rw [select, coeff_mk, coeff_mk], split_ifs with hn, { exact hn.symm }, { refl } }, calc (x + y).coeff n = z.coeff n : by rw [← hx, ← hy, select_add_select_not P z] ... = x.coeff n + y.coeff n : _, dsimp [z], split_ifs with hn, { dsimp [P] at hn, rw [hn, add_zero] }, { rw [(h n).resolve_right hn, zero_add] } end end select /-- `witt_vector.init n x` is the Witt vector of which the first `n` coefficients are those from `x` and all other coefficients are `0`. See `witt_vector.tail` for the complementary part. -/ def init (n : ℕ) : 𝕎 R → 𝕎 R := select (λ i, i < n) /-- `witt_vector.tail n x` is the Witt vector of which the first `n` coefficients are `0` and all other coefficients are those from `x`. See `witt_vector.init` for the complementary part. -/ def tail (n : ℕ) : 𝕎 R → 𝕎 R := select (λ i, n ≤ i) include hp @[simp] lemma init_add_tail (x : 𝕎 R) (n : ℕ) : init n x + tail n x = x := by simp only [init, tail, ← not_lt, select_add_select_not] end @[simp] lemma init_init (x : 𝕎 R) (n : ℕ) : init n (init n x) = init n x := by init_ring include hp lemma init_add (x y : 𝕎 R) (n : ℕ) : init n (x + y) = init n (init n x + init n y) := by init_ring using witt_add_vars lemma init_mul (x y : 𝕎 R) (n : ℕ) : init n (x * y) = init n (init n x * init n y) := by init_ring using witt_mul_vars lemma init_neg (x : 𝕎 R) (n : ℕ) : init n (-x) = init n (-init n x) := by init_ring using witt_neg_vars lemma init_sub (x y : 𝕎 R) (n : ℕ) : init n (x - y) = init n (init n x - init n y) := by init_ring using witt_sub_vars lemma init_nsmul (m : ℕ) (x : 𝕎 R) (n : ℕ) : init n (m • x) = init n (m • init n x) := by init_ring using (λ p [fact (nat.prime p)] n, by exactI witt_nsmul_vars p m n) lemma init_zsmul (m : ℤ) (x : 𝕎 R) (n : ℕ) : init n (m • x) = init n (m • init n x) := by init_ring using (λ p [fact (nat.prime p)] n, by exactI witt_zsmul_vars p m n) lemma init_pow (m : ℕ) (x : 𝕎 R) (n : ℕ) : init n (x ^ m) = init n (init n x ^ m) := by init_ring using (λ p [fact (nat.prime p)] n, by exactI witt_pow_vars p m n) section variables (p) omit hp /-- `witt_vector.init n x` is polynomial in the coefficients of `x`. -/ lemma init_is_poly (n : ℕ) : is_poly p (λ R _Rcr, by exactI init n) := select_is_poly (λ i, i < n) end end witt_vector
31839a322b5d969937c5cd7cffb4fa2266eba374
4efff1f47634ff19e2f786deadd394270a59ecd2
/src/ring_theory/integral_closure.lean
8b0751537ef7eeca606dfa1b62d8a42d148e0f24
[ "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
16,991
lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import ring_theory.algebra_tower import ring_theory.polynomial.scale_roots /-! # Integral closure of a subring. If A is an R-algebra then `a : A` is integral over R if it is a root of a monic polynomial with coefficients in R. Enough theory is developed to prove that integral elements form a sub-R-algebra of A. ## Main definitions Let `R` be a `comm_ring` and let `A` be an R-algebra. * `is_integral (x : A)` : `x` is integral over `R`, i.e., is a root of a monic polynomial with coefficients in `R`. * `integral_closure R A` : the integral closure of `R` in `A`, regarded as a sub-`R`-algebra of `A`. -/ universes u v w open_locale classical open_locale big_operators open polynomial submodule section variables (R : Type u) {A : Type v} {B : Type w} variables [comm_ring R] [comm_ring A] [comm_ring B] variables [algebra R A] [algebra R B] /-- An element `x` of an algebra `A` over a commutative ring `R` is said to be *integral*, if it is a root of some monic polynomial `p : polynomial R`. -/ def is_integral (x : A) : Prop := ∃ p : polynomial R, monic p ∧ aeval x p = 0 variables {R} theorem is_integral_algebra_map {x : R} : is_integral R (algebra_map R A x) := ⟨X - C x, monic_X_sub_C _, by rw [alg_hom.map_sub, aeval_def, aeval_def, eval₂_X, eval₂_C, sub_self]⟩ theorem is_integral_alg_hom (f : A →ₐ[R] B) {x : A} (hx : is_integral R x) : is_integral R (f x) := let ⟨p, hp, hpx⟩ := hx in ⟨p, hp, by rw [aeval_alg_hom_apply, hpx, f.map_zero]⟩ theorem is_integral_of_is_scalar_tower [algebra A B] [is_scalar_tower R A B] (x : B) (hx : is_integral R x) : is_integral A x := let ⟨p, hp, hpx⟩ := hx in ⟨p.map $ algebra_map R A, monic_map _ hp, by rw [← is_scalar_tower.aeval_apply, hpx]⟩ theorem is_integral_of_subring {x : A} (T : set R) [is_subring T] (hx : is_integral T x) : is_integral R x := is_integral_of_is_scalar_tower x hx theorem is_integral_iff_is_integral_closure_finite {r : A} : is_integral R r ↔ ∃ s : set R, s.finite ∧ is_integral (ring.closure s) r := begin split; intro hr, { rcases hr with ⟨p, hmp, hpr⟩, refine ⟨_, set.finite_mem_finset _, p.restriction, subtype.eq hmp, _⟩, erw [is_scalar_tower.aeval_apply _ R, map_restriction, hpr] }, rcases hr with ⟨s, hs, hsr⟩, exact is_integral_of_subring _ hsr end theorem fg_adjoin_singleton_of_integral (x : A) (hx : is_integral R x) : (algebra.adjoin R ({x} : set A) : submodule R A).fg := begin rcases hx with ⟨f, hfm, hfx⟩, existsi finset.image ((^) x) (finset.range (nat_degree f + 1)), apply le_antisymm, { rw span_le, intros s hs, rw finset.mem_coe at hs, rcases finset.mem_image.1 hs with ⟨k, hk, rfl⟩, clear hk, exact is_submonoid.pow_mem (algebra.subset_adjoin (set.mem_singleton _)) }, intros r hr, change r ∈ algebra.adjoin R ({x} : set A) at hr, rw algebra.adjoin_singleton_eq_range at hr, rcases hr with ⟨p, rfl⟩, rw ← mod_by_monic_add_div p hfm, rw [alg_hom.map_add, alg_hom.map_mul, hfx, zero_mul, add_zero], have : degree (p %ₘ f) ≤ degree f := degree_mod_by_monic_le p hfm, generalize_hyp : p %ₘ f = q at this ⊢, rw [← sum_C_mul_X_eq q, aeval_def, eval₂_sum, finsupp.sum], refine sum_mem _ (λ k hkq, _), rw [eval₂_mul, eval₂_C, eval₂_pow, eval₂_X, ← algebra.smul_def], refine smul_mem _ _ (subset_span _), rw finset.mem_coe, refine finset.mem_image.2 ⟨_, _, rfl⟩, rw [finset.mem_range, nat.lt_succ_iff], refine le_of_not_lt (λ hk, _), rw [degree_le_iff_coeff_zero] at this, rw [finsupp.mem_support_iff] at hkq, apply hkq, apply this, exact lt_of_le_of_lt degree_le_nat_degree (with_bot.coe_lt_coe.2 hk) end theorem fg_adjoin_of_finite {s : set A} (hfs : s.finite) (his : ∀ x ∈ s, is_integral R x) : (algebra.adjoin R s : submodule R A).fg := set.finite.induction_on hfs (λ _, ⟨{1}, submodule.ext $ λ x, by { erw [algebra.adjoin_empty, finset.coe_singleton, ← one_eq_span, one_eq_map_top, map_top, linear_map.mem_range, algebra.mem_bot], refl }⟩) (λ a s has hs ih his, by rw [← set.union_singleton, algebra.adjoin_union_coe_submodule]; exact fg_mul _ _ (ih $ λ i hi, his i $ set.mem_insert_of_mem a hi) (fg_adjoin_singleton_of_integral _ $ his a $ set.mem_insert a s)) his theorem is_integral_of_noetherian' (H : is_noetherian R A) (x : A) : is_integral R x := begin let leval : @linear_map R (polynomial R) A _ _ _ _ _ := (aeval x).to_linear_map, let D : ℕ → submodule R A := λ n, (degree_le R n).map leval, let M := well_founded.min (is_noetherian_iff_well_founded.1 H) (set.range D) ⟨_, ⟨0, rfl⟩⟩, have HM : M ∈ set.range D := well_founded.min_mem _ _ _, cases HM with N HN, have HM : ¬M < D (N+1) := well_founded.not_lt_min (is_noetherian_iff_well_founded.1 H) (set.range D) _ ⟨N+1, rfl⟩, rw ← HN at HM, have HN2 : D (N+1) ≤ D N := classical.by_contradiction (λ H, HM (lt_of_le_not_le (map_mono (degree_le_mono (with_bot.coe_le_coe.2 (nat.le_succ N)))) H)), have HN3 : leval (X^(N+1)) ∈ D N, { exact HN2 (mem_map_of_mem (mem_degree_le.2 (degree_X_pow_le _))) }, rcases HN3 with ⟨p, hdp, hpe⟩, refine ⟨X^(N+1) - p, monic_X_pow_sub (mem_degree_le.1 hdp), _⟩, show leval (X ^ (N + 1) - p) = 0, rw [linear_map.map_sub, hpe, sub_self] end theorem is_integral_of_noetherian (S : subalgebra R A) (H : is_noetherian R (S : submodule R A)) (x : A) (hx : x ∈ S) : is_integral R x := begin letI : algebra R S := S.algebra, letI : comm_ring S := S.comm_ring R A, suffices : is_integral R (⟨x, hx⟩ : S), { rcases this with ⟨p, hpm, hpx⟩, replace hpx := congr_arg subtype.val hpx, refine ⟨p, hpm, eq.trans _ hpx⟩, simp only [aeval_def, eval₂, finsupp.sum], rw ← p.support.sum_hom subtype.val, { refine finset.sum_congr rfl (λ n hn, _), change _ = _ * _, rw is_monoid_hom.map_pow coe, refl, split; intros; refl }, refine { map_add := _, map_zero := _ }; intros; refl }, refine is_integral_of_noetherian' H ⟨x, hx⟩ end theorem is_integral_of_mem_of_fg (S : subalgebra R A) (HS : (S : submodule R A).fg) (x : A) (hx : x ∈ S) : is_integral R x := begin cases HS with y hy, obtain ⟨lx, hlx1, hlx2⟩ : ∃ (l : A →₀ R) (H : l ∈ finsupp.supported R R ↑y), (finsupp.total A A R id) l = x, { rwa [←(@finsupp.mem_span_iff_total A A R _ _ _ id ↑y x), set.image_id ↑y, hy] }, have hyS : ∀ {p}, p ∈ y → p ∈ S := λ p hp, show p ∈ (S : submodule R A), by { rw ← hy, exact subset_span hp }, have : ∀ (jk : (↑(y.product y) : set (A × A))), jk.1.1 * jk.1.2 ∈ (S : submodule R A) := λ jk, S.mul_mem (hyS (finset.mem_product.1 jk.2).1) (hyS (finset.mem_product.1 jk.2).2), rw [← hy, ← set.image_id ↑y] at this, simp only [finsupp.mem_span_iff_total] at this, choose ly hly1 hly2, let S₀ : set R := ring.closure ↑(lx.frange ∪ finset.bind finset.univ (finsupp.frange ∘ ly)), refine is_integral_of_subring S₀ _, have : span S₀ (insert 1 ↑y : set A) * span S₀ (insert 1 ↑y : set A) ≤ span S₀ (insert 1 ↑y : set A), { rw span_mul_span, refine span_le.2 (λ z hz, _), rcases set.mem_mul.1 hz with ⟨p, q, rfl | hp, hq, rfl⟩, { rw one_mul, exact subset_span hq }, rcases hq with rfl | hq, { rw mul_one, exact subset_span (or.inr hp) }, erw ← hly2 ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, rw [finsupp.total_apply, finsupp.sum], refine (span S₀ (insert 1 ↑y : set A)).sum_mem (λ t ht, _), have : ly ⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩ t ∈ S₀ := ring.subset_closure (finset.mem_union_right _ $ finset.mem_bind.2 ⟨⟨(p, q), finset.mem_product.2 ⟨hp, hq⟩⟩, finset.mem_univ _, finsupp.mem_frange.2 ⟨finsupp.mem_support_iff.1 ht, _, rfl⟩⟩), change (⟨_, this⟩ : S₀) • t ∈ _, exact smul_mem _ _ (subset_span $ or.inr $ hly1 _ ht) }, haveI : is_subring (span S₀ (insert 1 ↑y : set A) : set A) := { one_mem := subset_span $ or.inl rfl, mul_mem := λ p q hp hq, this $ mul_mem_mul hp hq, zero_mem := (span S₀ (insert 1 ↑y : set A)).zero_mem, add_mem := λ _ _, (span S₀ (insert 1 ↑y : set A)).add_mem, neg_mem := λ _, (span S₀ (insert 1 ↑y : set A)).neg_mem }, have : span S₀ (insert 1 ↑y : set A) = algebra.adjoin S₀ (↑y : set A), { refine le_antisymm (span_le.2 $ set.insert_subset.2 ⟨(algebra.adjoin S₀ ↑y).one_mem, algebra.subset_adjoin⟩) (λ z hz, _), rw [subalgebra.mem_to_submodule, algebra.mem_adjoin_iff] at hz, rw ← submodule.mem_coe, refine ring.closure_subset (set.union_subset (set.range_subset_iff.2 $ λ t, _) (λ t ht, subset_span $ or.inr ht)) hz, rw algebra.algebra_map_eq_smul_one, exact smul_mem (span S₀ (insert 1 ↑y : set A)) _ (subset_span $ or.inl rfl) }, haveI : is_noetherian_ring ↥S₀ := is_noetherian_ring_closure _ (finset.finite_to_set _), refine is_integral_of_noetherian (algebra.adjoin S₀ ↑y) (is_noetherian_of_fg_of_noetherian _ ⟨insert 1 y, by rw [finset.coe_insert, this]⟩) _ _, rw [← hlx2, finsupp.total_apply, finsupp.sum], refine subalgebra.sum_mem _ (λ r hr, _), have : lx r ∈ S₀ := ring.subset_closure (finset.mem_union_left _ (finset.mem_image_of_mem _ hr)), change (⟨_, this⟩ : S₀) • r ∈ _, rw finsupp.mem_supported at hlx1, exact subalgebra.smul_mem _ (algebra.subset_adjoin $ hlx1 hr) _ end theorem is_integral_of_mem_closure {x y z : A} (hx : is_integral R x) (hy : is_integral R y) (hz : z ∈ ring.closure ({x, y} : set A)) : is_integral R z := begin have := fg_mul _ _ (fg_adjoin_singleton_of_integral x hx) (fg_adjoin_singleton_of_integral y hy), rw [← algebra.adjoin_union_coe_submodule, set.singleton_union] at this, exact is_integral_of_mem_of_fg (algebra.adjoin R {x, y}) this z (algebra.mem_adjoin_iff.2 $ ring.closure_mono (set.subset_union_right _ _) hz) end theorem is_integral_zero : is_integral R (0:A) := (algebra_map R A).map_zero ▸ is_integral_algebra_map theorem is_integral_one : is_integral R (1:A) := (algebra_map R A).map_one ▸ is_integral_algebra_map theorem is_integral_add {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x + y) := is_integral_of_mem_closure hx hy (is_add_submonoid.add_mem (ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl))) theorem is_integral_neg {x : A} (hx : is_integral R x) : is_integral R (-x) := is_integral_of_mem_closure hx hx (is_add_subgroup.neg_mem (ring.subset_closure (or.inl rfl))) theorem is_integral_sub {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x - y) := is_integral_add hx (is_integral_neg hy) theorem is_integral_mul {x y : A} (hx : is_integral R x) (hy : is_integral R y) : is_integral R (x * y) := is_integral_of_mem_closure hx hy (is_submonoid.mul_mem (ring.subset_closure (or.inl rfl)) (ring.subset_closure (or.inr rfl))) variables (R A) /-- The integral closure of R in an R-algebra A. -/ def integral_closure : subalgebra R A := { carrier := { carrier := { r | is_integral R r }, zero_mem' := is_integral_zero, one_mem' := is_integral_one, add_mem' := λ _ _, is_integral_add, mul_mem' := λ _ _, is_integral_mul }, algebra_map_mem' := λ x, is_integral_algebra_map } theorem mem_integral_closure_iff_mem_fg {r : A} : r ∈ integral_closure R A ↔ ∃ M : subalgebra R A, (M : submodule R A).fg ∧ r ∈ M := ⟨λ hr, ⟨algebra.adjoin R {r}, fg_adjoin_singleton_of_integral _ hr, algebra.subset_adjoin rfl⟩, λ ⟨M, Hf, hrM⟩, is_integral_of_mem_of_fg M Hf _ hrM⟩ variables {R} {A} lemma integral_closure.is_integral (x : integral_closure R A) : is_integral R x := let ⟨p, hpm, hpx⟩ := x.2 in ⟨p, hpm, subtype.eq $ by rwa [subtype.val_eq_coe, ← subalgebra.val_apply, aeval_alg_hom_apply] at hpx⟩ theorem is_integral_of_is_integral_mul_unit {x y : A} {r : R} (hr : algebra_map R A r * y = 1) (hx : is_integral R (x * y)) : is_integral R x := begin obtain ⟨p, ⟨p_monic, hp⟩⟩ := hx, refine ⟨scale_roots p r, ⟨(monic_scale_roots_iff r).2 p_monic, _⟩⟩, convert scale_roots_aeval_eq_zero hp, rw [mul_comm x y, ← mul_assoc, hr, one_mul], end end section algebra open algebra variables {R : Type*} {A : Type*} {B : Type*} variables [comm_ring R] [comm_ring A] [comm_ring B] variables [algebra A B] [algebra R B] lemma is_integral_trans_aux (x : B) {p : polynomial A} (pmonic : monic p) (hp : aeval x p = 0) : is_integral (adjoin R (↑(p.map $ algebra_map A B).frange : set B)) x := begin generalize hS : (↑(p.map $ algebra_map A B).frange : set B) = S, have coeffs_mem : ∀ i, (p.map $ algebra_map A B).coeff i ∈ adjoin R S, { intro i, by_cases hi : (p.map $ algebra_map A B).coeff i = 0, { rw hi, exact subalgebra.zero_mem _ }, rw ← hS, exact subset_adjoin (finsupp.mem_frange.2 ⟨hi, i, rfl⟩) }, obtain ⟨q, hq⟩ : ∃ q : polynomial (adjoin R S), q.map (algebra_map (adjoin R S) B) = (p.map $ algebra_map A B), { rw ← set.mem_range, exact (polynomial.mem_map_range _).2 (λ i, ⟨⟨_, coeffs_mem i⟩, rfl⟩) }, use q, split, { suffices h : (q.map (algebra_map (adjoin R S) B)).monic, { refine monic_of_injective _ h, exact subtype.val_injective }, { rw hq, exact monic_map _ pmonic } }, { convert hp using 1, replace hq := congr_arg (eval x) hq, convert hq using 1; symmetry; apply eval_map }, end variables [algebra R A] [is_scalar_tower R A B] /-- If A is an R-algebra all of whose elements are integral over R, and x is an element of an A-algebra that is integral over A, then x is integral over R.-/ lemma is_integral_trans (A_int : ∀ x : A, is_integral R x) (x : B) (hx : is_integral A x) : is_integral R x := begin rcases hx with ⟨p, pmonic, hp⟩, let S : set B := ↑(p.map $ algebra_map A B).frange, refine is_integral_of_mem_of_fg (adjoin R (S ∪ {x})) _ _ (subset_adjoin $ or.inr rfl), refine fg_trans (fg_adjoin_of_finite (finset.finite_to_set _) (λ x hx, _)) _, { rw [finset.mem_coe, finsupp.mem_frange] at hx, rcases hx with ⟨_, i, rfl⟩, show is_integral R ((p.map $ algebra_map A B).coeff i), rw coeff_map, convert is_integral_alg_hom (is_scalar_tower.to_alg_hom R A B) (A_int _) }, { apply fg_adjoin_singleton_of_integral, exact is_integral_trans_aux _ pmonic hp } end /-- If A is an R-algebra all of whose elements are integral over R, and B is an A-algebra all of whose elements are integral over A, then all elements of B are integral over R.-/ lemma algebra.is_integral_trans (A_int : ∀ x : A, is_integral R x)(B_int : ∀ x:B, is_integral A x) : ∀ x:B, is_integral R x := λ x, is_integral_trans A_int x (B_int x) lemma is_integral_of_surjective (h : function.surjective (algebra_map R A)) : ∀ x : A, is_integral R x := λ x, (h x).rec_on (λ y hy, (hy ▸ is_integral_algebra_map : is_integral R x)) /-- If `R → A → B` is an algebra tower with `A → B` injective, then if the entire tower is an integral extension so is `R → A` -/ lemma is_integral_tower_bot_of_is_integral (H : function.injective (algebra_map A B)) {x : A} (h : is_integral R (algebra_map A B x)) : is_integral R x := begin rcases h with ⟨p, ⟨hp, hp'⟩⟩, refine ⟨p, ⟨hp, _⟩⟩, rw [aeval_def, is_scalar_tower.algebra_map_eq R A B, ← eval₂_map, eval₂_hom, ← ring_hom.map_zero (algebra_map A B)] at hp', rw [aeval_def, eval₂_eq_eval_map], exact H hp', end /-- If `R → A → B` is an algebra tower, then if the entire tower is an integral extension so is `A → B` -/ lemma is_integral_tower_top_of_is_integral {x : B} (h : is_integral R x) : is_integral A x := begin rcases h with ⟨p, ⟨hp, hp'⟩⟩, refine ⟨p.map (algebra_map R A), ⟨monic_map (algebra_map R A) hp, _⟩⟩, rw [aeval_def, is_scalar_tower.algebra_map_eq R A B, ← eval₂_map] at hp', rw [aeval_def], exact hp', end end algebra theorem integral_closure_idem {R : Type*} {A : Type*} [comm_ring R] [comm_ring A] [algebra R A] : integral_closure (integral_closure R A : set A) A = ⊥ := eq_bot_iff.2 $ λ x hx, algebra.mem_bot.2 ⟨⟨x, is_integral_trans integral_closure.is_integral _ hx⟩, rfl⟩ section integral_domain variables {R S : Type*} [comm_ring R] [integral_domain S] [algebra R S] instance : integral_domain (integral_closure R S) := { exists_pair_ne := ⟨0, 1, mt subtype.ext_iff_val.mp zero_ne_one⟩, eq_zero_or_eq_zero_of_mul_eq_zero := λ ⟨a, ha⟩ ⟨b, hb⟩ h, or.imp subtype.ext_iff_val.mpr subtype.ext_iff_val.mpr (eq_zero_or_eq_zero_of_mul_eq_zero (subtype.ext_iff_val.mp h)), ..(integral_closure R S).comm_ring R S } end integral_domain
75d8310b2031c488bdb48eca4d41b26e146647fb
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/metric_space/baire.lean
56ce034d0a0bac54f9f5f1a627698ec82276cefb
[ "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
17,613
lean
/- Copyright (c) 2019 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Sébastien Gouëzel -/ import analysis.specific_limits.basic import order.filter.countable_Inter import topology.G_delta /-! # Baire theorem In a complete metric space, a countable intersection of dense open subsets is dense. The good concept underlying the theorem is that of a Gδ set, i.e., a countable intersection of open sets. Then Baire theorem can also be formulated as the fact that a countable intersection of dense Gδ sets is a dense Gδ set. We prove Baire theorem, giving several different formulations that can be handy. We also prove the important consequence that, if the space is covered by a countable union of closed sets, then the union of their interiors is dense. We also define the filter `residual α` generated by dense `Gδ` sets and prove that this filter has the countable intersection property. -/ noncomputable theory open_locale classical topological_space filter ennreal open filter encodable set topological_space variables {α : Type*} {β : Type*} {γ : Type*} {ι : Type*} section Baire_theorem open emetric ennreal /-- The property `baire_space α` means that the topological space `α` has the Baire property: any countable intersection of open dense subsets is dense. Formulated here when the source space is ℕ (and subsumed below by `dense_Inter_of_open` working with any encodable source space).-/ class baire_space (α : Type*) [topological_space α] : Prop := (baire_property : ∀ f : ℕ → set α, (∀ n, is_open (f n)) → (∀ n, dense (f n)) → dense (⋂n, f n)) /-- Baire theorems asserts that various topological spaces have the Baire property. Two versions of these theorems are given. The first states that complete pseudo_emetric spaces are Baire. -/ @[priority 100] instance baire_category_theorem_emetric_complete [pseudo_emetric_space α] [complete_space α] : baire_space α := begin refine ⟨λ f ho hd, _⟩, let B : ℕ → ℝ≥0∞ := λn, 1/2^n, have Bpos : ∀n, 0 < B n, { intro n, simp only [B, one_div, one_mul, ennreal.inv_pos], exact pow_ne_top two_ne_top }, /- Translate the density assumption into two functions `center` and `radius` associating to any n, x, δ, δpos a center and a positive radius such that `closed_ball center radius` is included both in `f n` and in `closed_ball x δ`. We can also require `radius ≤ (1/2)^(n+1)`, to ensure we get a Cauchy sequence later. -/ have : ∀n x δ, δ ≠ 0 → ∃y r, 0 < r ∧ r ≤ B (n+1) ∧ closed_ball y r ⊆ (closed_ball x δ) ∩ f n, { assume n x δ δpos, have : x ∈ closure (f n) := hd n x, rcases emetric.mem_closure_iff.1 this (δ/2) (ennreal.half_pos δpos) with ⟨y, ys, xy⟩, rw edist_comm at xy, obtain ⟨r, rpos, hr⟩ : ∃ r > 0, closed_ball y r ⊆ f n := nhds_basis_closed_eball.mem_iff.1 (is_open_iff_mem_nhds.1 (ho n) y ys), refine ⟨y, min (min (δ/2) r) (B (n+1)), _, _, λz hz, ⟨_, _⟩⟩, show 0 < min (min (δ / 2) r) (B (n+1)), from lt_min (lt_min (ennreal.half_pos δpos) rpos) (Bpos (n+1)), show min (min (δ / 2) r) (B (n+1)) ≤ B (n+1), from min_le_right _ _, show z ∈ closed_ball x δ, from calc edist z x ≤ edist z y + edist y x : edist_triangle _ _ _ ... ≤ (min (min (δ / 2) r) (B (n+1))) + (δ/2) : add_le_add hz (le_of_lt xy) ... ≤ δ/2 + δ/2 : add_le_add (le_trans (min_le_left _ _) (min_le_left _ _)) le_rfl ... = δ : ennreal.add_halves δ, show z ∈ f n, from hr (calc edist z y ≤ min (min (δ / 2) r) (B (n+1)) : hz ... ≤ r : le_trans (min_le_left _ _) (min_le_right _ _)) }, choose! center radius Hpos HB Hball using this, refine λ x, (mem_closure_iff_nhds_basis nhds_basis_closed_eball).2 (λ ε εpos, _), /- `ε` is positive. We have to find a point in the ball of radius `ε` around `x` belonging to all `f n`. For this, we construct inductively a sequence `F n = (c n, r n)` such that the closed ball `closed_ball (c n) (r n)` is included in the previous ball and in `f n`, and such that `r n` is small enough to ensure that `c n` is a Cauchy sequence. Then `c n` converges to a limit which belongs to all the `f n`. -/ let F : ℕ → (α × ℝ≥0∞) := λn, nat.rec_on n (prod.mk x (min ε (B 0))) (λn p, prod.mk (center n p.1 p.2) (radius n p.1 p.2)), let c : ℕ → α := λn, (F n).1, let r : ℕ → ℝ≥0∞ := λn, (F n).2, have rpos : ∀ n, 0 < r n, { assume n, induction n with n hn, exact lt_min εpos (Bpos 0), exact Hpos n (c n) (r n) hn.ne' }, have r0 : ∀ n, r n ≠ 0 := λ n, (rpos n).ne', have rB : ∀n, r n ≤ B n, { assume n, induction n with n hn, exact min_le_right _ _, exact HB n (c n) (r n) (r0 n) }, have incl : ∀n, closed_ball (c (n+1)) (r (n+1)) ⊆ (closed_ball (c n) (r n)) ∩ (f n) := λ n, Hball n (c n) (r n) (r0 n), have cdist : ∀n, edist (c n) (c (n+1)) ≤ B n, { assume n, rw edist_comm, have A : c (n+1) ∈ closed_ball (c (n+1)) (r (n+1)) := mem_closed_ball_self, have I := calc closed_ball (c (n+1)) (r (n+1)) ⊆ closed_ball (c n) (r n) : subset.trans (incl n) (inter_subset_left _ _) ... ⊆ closed_ball (c n) (B n) : closed_ball_subset_closed_ball (rB n), exact I A }, have : cauchy_seq c := cauchy_seq_of_edist_le_geometric_two _ one_ne_top cdist, -- as the sequence `c n` is Cauchy in a complete space, it converges to a limit `y`. rcases cauchy_seq_tendsto_of_complete this with ⟨y, ylim⟩, -- this point `y` will be the desired point. We will check that it belongs to all -- `f n` and to `ball x ε`. use y, simp only [exists_prop, set.mem_Inter], have I : ∀n, ∀m ≥ n, closed_ball (c m) (r m) ⊆ closed_ball (c n) (r n), { assume n, refine nat.le_induction _ (λm hnm h, _), { exact subset.refl _ }, { exact subset.trans (incl m) (subset.trans (inter_subset_left _ _) h) }}, have yball : ∀n, y ∈ closed_ball (c n) (r n), { assume n, refine is_closed_ball.mem_of_tendsto ylim _, refine (filter.eventually_ge_at_top n).mono (λ m hm, _), exact I n m hm mem_closed_ball_self }, split, show ∀n, y ∈ f n, { assume n, have : closed_ball (c (n+1)) (r (n+1)) ⊆ f n := subset.trans (incl n) (inter_subset_right _ _), exact this (yball (n+1)) }, show edist y x ≤ ε, from le_trans (yball 0) (min_le_left _ _), end /-- The second theorem states that locally compact spaces are Baire. -/ @[priority 100] instance baire_category_theorem_locally_compact [topological_space α] [t2_space α] [locally_compact_space α] : baire_space α := begin constructor, intros f ho hd, /- To prove that an intersection of open dense subsets is dense, prove that its intersection with any open neighbourhood `U` is dense. Define recursively a decreasing sequence `K` of compact neighbourhoods: start with some compact neighbourhood inside `U`, then at each step, take its interior, intersect with `f n`, then choose a compact neighbourhood inside the intersection.-/ apply dense_iff_inter_open.2, intros U U_open U_nonempty, rcases exists_positive_compacts_subset U_open U_nonempty with ⟨K₀, hK₀⟩, have : ∀ n (K : positive_compacts α), ∃ K' : positive_compacts α, ↑K' ⊆ f n ∩ interior K, { refine λ n K, exists_positive_compacts_subset ((ho n).inter is_open_interior) _, rw inter_comm, exact (hd n).inter_open_nonempty _ is_open_interior K.interior_nonempty }, choose K_next hK_next, let K : ℕ → positive_compacts α := λ n, nat.rec_on n K₀ K_next, /- This is a decreasing sequence of positive compacts contained in suitable open sets `f n`.-/ have hK_decreasing : ∀ (n : ℕ), ↑(K (n + 1)) ⊆ f n ∩ K n, from λ n, (hK_next n (K n)).trans $ inter_subset_inter_right _ interior_subset, /- Prove that ̀`⋂ n : ℕ, K n` is inside `U ∩ ⋂ n : ℕ, (f n)`. -/ have hK_subset : (⋂ n, K n : set α) ⊆ U ∩ (⋂ n, f n), { intros x hx, simp only [mem_inter_eq, mem_Inter] at hx ⊢, exact ⟨hK₀ $ hx 0, λ n, (hK_decreasing n (hx (n + 1))).1⟩ }, /- Prove that `⋂ n : ℕ, K n` is not empty, as an intersection of a decreasing sequence of nonempty compact subsets.-/ have hK_nonempty : (⋂ n, K n : set α).nonempty, from is_compact.nonempty_Inter_of_sequence_nonempty_compact_closed _ (λ n, (hK_decreasing n).trans (inter_subset_right _ _)) (λ n, (K n).nonempty) (K 0).compact (λ n, (K n).compact.is_closed), exact hK_nonempty.mono hK_subset end variables [topological_space α] [baire_space α] /-- Definition of a Baire space. -/ theorem dense_Inter_of_open_nat {f : ℕ → set α} (ho : ∀ n, is_open (f n)) (hd : ∀ n, dense (f n)) : dense (⋂ n, f n) := baire_space.baire_property f ho hd /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with ⋂₀. -/ theorem dense_sInter_of_open {S : set (set α)} (ho : ∀s∈S, is_open s) (hS : S.countable) (hd : ∀s∈S, dense s) : dense (⋂₀S) := begin cases S.eq_empty_or_nonempty with h h, { simp [h] }, { rcases hS.exists_eq_range h with ⟨f, hf⟩, have F : ∀n, f n ∈ S := λn, by rw hf; exact mem_range_self _, rw [hf, sInter_range], exact dense_Inter_of_open_nat (λn, ho _ (F n)) (λn, hd _ (F n)) } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_open {S : set β} {f : β → set α} (ho : ∀s∈S, is_open (f s)) (hS : S.countable) (hd : ∀s∈S, dense (f s)) : dense (⋂s∈S, f s) := begin rw ← sInter_image, apply dense_sInter_of_open, { rwa ball_image_iff }, { exact hS.image _ }, { rwa ball_image_iff } end /-- Baire theorem: a countable intersection of dense open sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_open [encodable β] {f : β → set α} (ho : ∀s, is_open (f s)) (hd : ∀s, dense (f s)) : dense (⋂s, f s) := begin rw ← sInter_range, apply dense_sInter_of_open, { rwa forall_range_iff }, { exact countable_range _ }, { rwa forall_range_iff } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with ⋂₀. -/ theorem dense_sInter_of_Gδ {S : set (set α)} (ho : ∀s∈S, is_Gδ s) (hS : S.countable) (hd : ∀s∈S, dense s) : dense (⋂₀S) := begin -- the result follows from the result for a countable intersection of dense open sets, -- by rewriting each set as a countable intersection of open sets, which are of course dense. choose T hTo hTc hsT using ho, have : ⋂₀ S = ⋂₀ (⋃ s ∈ S, T s ‹_›), -- := (sInter_bUnion (λs hs, (hT s hs).2.2)).symm, by simp only [sInter_Union, (hsT _ _).symm, ← sInter_eq_bInter], rw this, refine dense_sInter_of_open _ (hS.bUnion hTc) _; simp only [mem_Union]; rintro t ⟨s, hs, tTs⟩, show is_open t, from hTo s hs t tTs, show dense t, { intro x, have := hd s hs x, rw hsT s hs at this, exact closure_mono (sInter_subset_of_mem tTs) this } end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Inter_of_Gδ [encodable β] {f : β → set α} (ho : ∀s, is_Gδ (f s)) (hd : ∀s, dense (f s)) : dense (⋂s, f s) := begin rw ← sInter_range, exact dense_sInter_of_Gδ (forall_range_iff.2 ‹_›) (countable_range _) (forall_range_iff.2 ‹_›) end /-- Baire theorem: a countable intersection of dense Gδ sets is dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bInter_of_Gδ {S : set β} {f : Π x ∈ S, set α} (ho : ∀s∈S, is_Gδ (f s ‹_›)) (hS : S.countable) (hd : ∀s∈S, dense (f s ‹_›)) : dense (⋂s∈S, f s ‹_›) := begin rw bInter_eq_Inter, haveI := hS.to_encodable, exact dense_Inter_of_Gδ (λ s, ho s s.2) (λ s, hd s s.2) end /-- Baire theorem: the intersection of two dense Gδ sets is dense. -/ theorem dense.inter_of_Gδ {s t : set α} (hs : is_Gδ s) (ht : is_Gδ t) (hsc : dense s) (htc : dense t) : dense (s ∩ t) := begin rw [inter_eq_Inter], apply dense_Inter_of_Gδ; simp [bool.forall_bool, *] end /-- A property holds on a residual (comeagre) set if and only if it holds on some dense `Gδ` set. -/ lemma eventually_residual {p : α → Prop} : (∀ᶠ x in residual α, p x) ↔ ∃ (t : set α), is_Gδ t ∧ dense t ∧ ∀ x ∈ t, p x := calc (∀ᶠ x in residual α, p x) ↔ ∀ᶠ x in ⨅ (t : set α) (ht : is_Gδ t ∧ dense t), 𝓟 t, p x : by simp only [residual, infi_and] ... ↔ ∃ (t : set α) (ht : is_Gδ t ∧ dense t), ∀ᶠ x in 𝓟 t, p x : mem_binfi_of_directed (λ t₁ h₁ t₂ h₂, ⟨t₁ ∩ t₂, ⟨h₁.1.inter h₂.1, dense.inter_of_Gδ h₁.1 h₂.1 h₁.2 h₂.2⟩, by simp⟩) ⟨univ, is_Gδ_univ, dense_univ⟩ ... ↔ _ : by simp [and_assoc] /-- A set is residual (comeagre) if and only if it includes a dense `Gδ` set. -/ lemma mem_residual {s : set α} : s ∈ residual α ↔ ∃ t ⊆ s, is_Gδ t ∧ dense t := (@eventually_residual α _ _ (λ x, x ∈ s)).trans $ exists_congr $ λ t, by rw [exists_prop, and_comm (t ⊆ s), subset_def, and_assoc] lemma dense_of_mem_residual {s : set α} (hs : s ∈ residual α) : dense s := let ⟨t, hts, _, hd⟩ := mem_residual.1 hs in hd.mono hts instance : countable_Inter_filter (residual α) := ⟨begin intros S hSc hS, simp only [mem_residual] at *, choose T hTs hT using hS, refine ⟨⋂ s ∈ S, T s ‹_›, _, _, _⟩, { rw [sInter_eq_bInter], exact Inter₂_mono hTs }, { exact is_Gδ_bInter hSc (λ s hs, (hT s hs).1) }, { exact dense_bInter_of_Gδ (λ s hs, (hT s hs).1) hSc (λ s hs, (hT s hs).2) } end⟩ /-- If a countable family of closed sets cover a dense `Gδ` set, then the union of their interiors is dense. Formulated here with `⋃`. -/ lemma is_Gδ.dense_Union_interior_of_closed [encodable ι] {s : set α} (hs : is_Gδ s) (hd : dense s) {f : ι → set α} (hc : ∀ i, is_closed (f i)) (hU : s ⊆ ⋃ i, f i) : dense (⋃ i, interior (f i)) := begin let g := λ i, (frontier (f i))ᶜ, have hgo : ∀ i, is_open (g i), from λ i, is_closed_frontier.is_open_compl, have hgd : dense (⋂ i, g i), { refine dense_Inter_of_open hgo (λ i x, _), rw [closure_compl, interior_frontier (hc _)], exact id }, refine (hd.inter_of_Gδ hs (is_Gδ_Inter $ λ i, (hgo i).is_Gδ) hgd).mono _, rintro x ⟨hxs, hxg⟩, rw [mem_Inter] at hxg, rcases mem_Union.1 (hU hxs) with ⟨i, hi⟩, exact mem_Union.2 ⟨i, self_diff_frontier (f i) ▸ ⟨hi, hxg _⟩⟩, end /-- If a countable family of closed sets cover a dense `Gδ` set, then the union of their interiors is dense. Formulated here with a union over a countable set in any type. -/ lemma is_Gδ.dense_bUnion_interior_of_closed {t : set ι} {s : set α} (hs : is_Gδ s) (hd : dense s) (ht : t.countable) {f : ι → set α} (hc : ∀ i ∈ t, is_closed (f i)) (hU : s ⊆ ⋃ i ∈ t, f i) : dense (⋃ i ∈ t, interior (f i)) := begin haveI := ht.to_encodable, simp only [bUnion_eq_Union, set_coe.forall'] at *, exact hs.dense_Union_interior_of_closed hd hc hU end /-- If a countable family of closed sets cover a dense `Gδ` set, then the union of their interiors is dense. Formulated here with `⋃₀`. -/ lemma is_Gδ.dense_sUnion_interior_of_closed {T : set (set α)} {s : set α} (hs : is_Gδ s) (hd : dense s) (hc : T.countable) (hc' : ∀ t ∈ T, is_closed t) (hU : s ⊆ ⋃₀ T) : dense (⋃ t ∈ T, interior t) := hs.dense_bUnion_interior_of_closed hd hc hc' $ by rwa [← sUnion_eq_bUnion] /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is a countable set in any type. -/ theorem dense_bUnion_interior_of_closed {S : set β} {f : β → set α} (hc : ∀s∈S, is_closed (f s)) (hS : S.countable) (hU : (⋃s∈S, f s) = univ) : dense (⋃s∈S, interior (f s)) := is_Gδ_univ.dense_bUnion_interior_of_closed dense_univ hS hc hU.ge /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with `⋃₀`. -/ theorem dense_sUnion_interior_of_closed {S : set (set α)} (hc : ∀s∈S, is_closed s) (hS : S.countable) (hU : (⋃₀ S) = univ) : dense (⋃s∈S, interior s) := is_Gδ_univ.dense_sUnion_interior_of_closed dense_univ hS hc hU.ge /-- Baire theorem: if countably many closed sets cover the whole space, then their interiors are dense. Formulated here with an index set which is an encodable type. -/ theorem dense_Union_interior_of_closed [encodable β] {f : β → set α} (hc : ∀s, is_closed (f s)) (hU : (⋃s, f s) = univ) : dense (⋃s, interior (f s)) := is_Gδ_univ.dense_Union_interior_of_closed dense_univ hc hU.ge /-- One of the most useful consequences of Baire theorem: if a countable union of closed sets covers the space, then one of the sets has nonempty interior. -/ theorem nonempty_interior_of_Union_of_closed [nonempty α] [encodable β] {f : β → set α} (hc : ∀s, is_closed (f s)) (hU : (⋃s, f s) = univ) : ∃s, (interior $ f s).nonempty := by simpa using (dense_Union_interior_of_closed hc hU).nonempty end Baire_theorem
0881ea3c58c2d98e49317a6ad2db98db707a0ee3
26ac254ecb57ffcb886ff709cf018390161a9225
/src/algebra/category/Group/colimits.lean
a904e959e314b62ec3251e4c5e0a8b64954a8b67
[ "Apache-2.0" ]
permissive
eric-wieser/mathlib
42842584f584359bbe1fc8b88b3ff937c8acd72d
d0df6b81cd0920ad569158c06a3fd5abb9e63301
refs/heads/master
1,669,546,404,255
1,595,254,668,000
1,595,254,668,000
281,173,504
0
0
Apache-2.0
1,595,263,582,000
1,595,263,581,000
null
UTF-8
Lean
false
false
8,539
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import algebra.category.Group.basic import category_theory.limits.limits import category_theory.limits.concrete_category /-! # The category of additive commutative groups has all colimits. This file uses a "pre-automated" approach, just as for `Mon/colimits.lean`. It is a very uniform approach, that conceivably could be synthesised directly by a tactic that analyses the shape of `add_comm_group` and `monoid_hom`. TODO: In fact, in `AddCommGroup` there is a much nicer model of colimits as quotients of finitely supported functions, and we really should implement this as well (or instead). -/ universes u v open category_theory open category_theory.limits -- [ROBOT VOICE]: -- You should pretend for now that this file was automatically generated. -- It follows the same template as colimits in Mon. namespace AddCommGroup.colimits /-! We build the colimit of a diagram in `AddCommGroup` by constructing the free group on the disjoint union of all the abelian groups in the diagram, then taking the quotient by the abelian group laws within each abelian group, and the identifications given by the morphisms in the diagram. -/ variables {J : Type v} [small_category J] (F : J ⥤ AddCommGroup.{v}) /-- An inductive type representing all group expressions (without relations) on a collection of types indexed by the objects of `J`. -/ inductive prequotient -- There's always `of` | of : Π (j : J) (x : F.obj j), prequotient -- Then one generator for each operation | zero : prequotient | neg : prequotient → prequotient | add : prequotient → prequotient → prequotient instance : inhabited (prequotient F) := ⟨prequotient.zero⟩ open prequotient /-- The relation on `prequotient` saying when two expressions are equal because of the abelian group laws, or because one element is mapped to another by a morphism in the diagram. -/ inductive relation : prequotient F → prequotient F → Prop -- Make it an equivalence relation: | refl : Π (x), relation x x | symm : Π (x y) (h : relation x y), relation y x | trans : Π (x y z) (h : relation x y) (k : relation y z), relation x z -- There's always a `map` relation | map : Π (j j' : J) (f : j ⟶ j') (x : F.obj j), relation (of j' (F.map f x)) (of j x) -- Then one relation per operation, describing the interaction with `of` | zero : Π (j), relation (of j 0) zero | neg : Π (j) (x : F.obj j), relation (of j (-x)) (neg (of j x)) | add : Π (j) (x y : F.obj j), relation (of j (x + y)) (add (of j x) (of j y)) -- Then one relation per argument of each operation | neg_1 : Π (x x') (r : relation x x'), relation (neg x) (neg x') | add_1 : Π (x x' y) (r : relation x x'), relation (add x y) (add x' y) | add_2 : Π (x y y') (r : relation y y'), relation (add x y) (add x y') -- And one relation per axiom | zero_add : Π (x), relation (add zero x) x | add_zero : Π (x), relation (add x zero) x | add_left_neg : Π (x), relation (add (neg x) x) zero | add_comm : Π (x y), relation (add x y) (add y x) | add_assoc : Π (x y z), relation (add (add x y) z) (add x (add y z)) /-- The setoid corresponding to group expressions modulo abelian group relations and identifications. -/ def colimit_setoid : setoid (prequotient F) := { r := relation F, iseqv := ⟨relation.refl, relation.symm, relation.trans⟩ } attribute [instance] colimit_setoid /-- The underlying type of the colimit of a diagram in `AddCommGroup`. -/ @[derive inhabited] def colimit_type : Type v := quotient (colimit_setoid F) instance : add_comm_group (colimit_type F) := { zero := begin exact quot.mk _ zero end, neg := begin fapply @quot.lift, { intro x, exact quot.mk _ (neg x) }, { intros x x' r, apply quot.sound, exact relation.neg_1 _ _ r }, end, add := begin fapply @quot.lift _ _ ((colimit_type F) → (colimit_type F)), { intro x, fapply @quot.lift, { intro y, exact quot.mk _ (add x y) }, { intros y y' r, apply quot.sound, exact relation.add_2 _ _ _ r } }, { intros x x' r, funext y, induction y, dsimp, apply quot.sound, { exact relation.add_1 _ _ _ r }, { refl } }, end, zero_add := λ x, begin induction x, dsimp, apply quot.sound, apply relation.zero_add, refl, end, add_zero := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_zero, refl, end, add_left_neg := λ x, begin induction x, dsimp, apply quot.sound, apply relation.add_left_neg, refl, end, add_comm := λ x y, begin induction x, induction y, dsimp, apply quot.sound, apply relation.add_comm, refl, refl, end, add_assoc := λ x y z, begin induction x, induction y, induction z, dsimp, apply quot.sound, apply relation.add_assoc, refl, refl, refl, end, } @[simp] lemma quot_zero : quot.mk setoid.r zero = (0 : colimit_type F) := rfl @[simp] lemma quot_neg (x) : quot.mk setoid.r (neg x) = (-(quot.mk setoid.r x) : colimit_type F) := rfl @[simp] lemma quot_add (x y) : quot.mk setoid.r (add x y) = ((quot.mk setoid.r x) + (quot.mk setoid.r y) : colimit_type F) := rfl /-- The bundled abelian group giving the colimit of a diagram. -/ def colimit : AddCommGroup := AddCommGroup.of (colimit_type F) /-- The function from a given abelian group in the diagram to the colimit abelian group. -/ def cocone_fun (j : J) (x : F.obj j) : colimit_type F := quot.mk _ (of j x) /-- The group homomorphism from a given abelian group in the diagram to the colimit abelian group. -/ def cocone_morphism (j : J) : F.obj j ⟶ colimit F := { to_fun := cocone_fun F j, map_zero' := by apply quot.sound; apply relation.zero, map_add' := by intros; apply quot.sound; apply relation.add } @[simp] lemma cocone_naturality {j j' : J} (f : j ⟶ j') : F.map f ≫ (cocone_morphism F j') = cocone_morphism F j := begin ext, apply quot.sound, apply relation.map, end @[simp] lemma cocone_naturality_components (j j' : J) (f : j ⟶ j') (x : F.obj j): (cocone_morphism F j') (F.map f x) = (cocone_morphism F j) x := by { rw ←cocone_naturality F f, refl } /-- The cocone over the proposed colimit abelian group. -/ def colimit_cocone : cocone F := { X := colimit F, ι := { app := cocone_morphism F } }. /-- The function from the free abelian group on the diagram to the cone point of any other cocone. -/ @[simp] def desc_fun_lift (s : cocone F) : prequotient F → s.X | (of j x) := (s.ι.app j) x | zero := 0 | (neg x) := -(desc_fun_lift x) | (add x y) := desc_fun_lift x + desc_fun_lift y /-- The function from the colimit abelian group to the cone point of any other cocone. -/ def desc_fun (s : cocone F) : colimit_type F → s.X := begin fapply quot.lift, { exact desc_fun_lift F s }, { intros x y r, induction r; try { dsimp }, -- refl { refl }, -- symm { exact r_ih.symm }, -- trans { exact eq.trans r_ih_h r_ih_k }, -- map { simp, }, -- zero { simp, }, -- neg { simp, }, -- add { simp, }, -- neg_1 { rw r_ih, }, -- add_1 { rw r_ih, }, -- add_2 { rw r_ih, }, -- zero_add { rw zero_add, }, -- add_zero { rw add_zero, }, -- add_left_neg { rw add_left_neg, }, -- add_comm { rw add_comm, }, -- add_assoc { rw add_assoc, }, } end /-- The group homomorphism from the colimit abelian group to the cone point of any other cocone. -/ @[simps] def desc_morphism (s : cocone F) : colimit F ⟶ s.X := { to_fun := desc_fun F s, map_zero' := rfl, map_add' := λ x y, by { induction x; induction y; refl }, } /-- Evidence that the proposed colimit is the colimit. -/ def colimit_is_colimit : is_colimit (colimit_cocone F) := { desc := λ s, desc_morphism F s, uniq' := λ s m w, begin ext, induction x, induction x, { have w' := congr_fun (congr_arg (λ f : F.obj x_j ⟶ s.X, (f : F.obj x_j → s.X)) (w x_j)) x_x, erw w', refl, }, { simp *, }, { simp *, }, { simp *, }, refl end }. instance has_colimits_AddCommGroup : has_colimits AddCommGroup := { has_colimits_of_shape := λ J 𝒥, { has_colimit := λ F, by exactI { cocone := colimit_cocone F, is_colimit := colimit_is_colimit F } } } end AddCommGroup.colimits
ba6f3e49166354f809837065d9f6fd9d40d669ba
32c054a763e4aa96bcb6e8bc87775e0f403a1804
/src/spec/openprog.lean
a9cd90e56219f27d84d008430ac61169e5b5d0a4
[ "LicenseRef-scancode-generic-cla", "MIT" ]
permissive
Claudiusgonzo/AliveInLean
7fac3f82722c27acc5551260ea12a36519f6e24e
a21bfb90dee0c6c6e00a955b6de92c631198c5ba
refs/heads/master
1,635,381,727,801
1,555,783,536,000
1,555,783,536,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
21,953
lean
-- Copyright (c) Microsoft Corporation. All rights reserved. -- Licensed under the MIT license. import ..smtexpr import ..smtcompile import ..bitvector import .spec import .lemmas import .irstate import .freevar import .equiv import smt2.syntax import system.io import init.meta.tactic import init.meta.interactive namespace spec open irsem open freevar meta def solve_left := `[ unfold freevar.env.replace_sbv, split, refl] lemma foldr_and_replace: ∀ {α:Type} {f:α → poisonty_smt} p0 (l:list α) (η:freevar.env), η⟦list.foldr (λ itm (p:poisonty_smt), p & (f itm)) p0 l⟧ = list.foldr (λ itm (p:poisonty_smt), p & η⟦(f itm)⟧) (η⟦p0⟧) l := begin intros, induction l, { simp }, { simp, rw env.replace_sb_and, rw ← l_ih } end lemma bop_poison_replace: ∀ sz bopc (v1 v2:sbitvec sz) (η:freevar.env), η⟦bop_poison irsem_smt sz bopc v1 v2⟧ = bop_poison irsem_smt sz bopc (η⟦v1⟧) (η⟦v2⟧) := begin intros, cases bopc, any_goals { unfold bop_poison, delta bop_poison._match_1, delta id_rhs, simp, rw env.replace_b2p, refl }, any_goals { -- shl unfold bop_poison, delta bop_poison._match_1, delta id_rhs, simp, rw env.replace_b2p, try { unfold has_ult.ult, unfold has_comp.ult }, try { unfold has_sgt.sgt, unfold has_comp.sgt }, try { unfold has_ugt.ugt, unfold has_comp.ugt }, unfold freevar.env.replace_sb, congr, unfold uint_like.from_z, rw env.replace_sbv_of_int } end lemma bop_poison_flag_replace: ∀ sz bopc (v1 v2:sbitvec sz) (η:freevar.env) flag, η⟦bop_poison_flag irsem_smt sz bopc flag v1 v2⟧ = bop_poison_flag irsem_smt sz bopc flag (η⟦v1⟧) (η⟦v2⟧) := begin intros, cases flag, any_goals { -- nsw, nuw unfold bop_poison_flag, cases bopc; unfold bop_overflow_check; rw env.replace_sb_not; rw env.replace_b2p, { unfold has_overflow_check.add_chk, rw env.replace_sb_overflowchk_add }, { unfold has_overflow_check.sub_chk, rw env.replace_sb_overflowchk_sub }, any_goals { unfold has_overflow_check.mul_chk, rw env.replace_sb_overflowchk_mul }, { unfold has_overflow_check.shl_chk, rw env.replace_sb_overflowchk_shl } }, { -- exact have HZERO: η⟦uint_like.zero sz⟧ = uint_like.zero sz, { unfold uint_like.zero, unfold sbitvec.zero, rw env.replace_sbv_of_int }, unfold bop_poison_flag, cases bopc; unfold bop_exact_check; rw env.replace_sb_not; simp, any_goals { unfold bop_exact_check._match_1, rw env.replace_eq2p }, any_goals { unfold has_umod.umod, unfold uint_like.urem, rw env.replace_sbv_urem, rw HZERO }, any_goals { unfold has_mod.mod, unfold uint_like.srem, rw env.replace_sbv_srem, rw HZERO }, any_goals { unfold has_shl.shl, unfold uint_like.shl, rw env.replace_sbv_shl, try { unfold has_lshr.lshr, unfold uint_like.lshr, rw env.replace_sbv_lshr }, try { unfold has_ashr.ashr, unfold uint_like.ashr, rw env.replace_sbv_ashr }, unfold has_umod.umod, unfold uint_like.urem, rw env.replace_sbv_urem, congr; unfold uint_like.from_z; rw env.replace_sbv_of_int } } end lemma bop_replace: ∀ sz bopc flags η v1 p1 v2 p2 ubres vres pres (H:(ubres, vres, pres) = bop irsem_smt sz bopc flags v1 p1 v2 p2), bop irsem_smt sz bopc flags (η⟦v1⟧) (η⟦p1⟧) (η⟦v2⟧) (η⟦p2⟧) = (η⟦ubres⟧, η⟦vres⟧, η⟦pres⟧) := begin intros, unfold bop at *, simp at *, injection H with HUB H, injection H with HV HPOISON, congr, { rw HUB, unfold bop_ub, cases bopc, any_goals { simp, refl, done }, -- non-divison ops any_goals { -- udiv, urem simp, rw env.replace_sb_and, rw env.replace_sb_and, rw env.replace_eq2p', refl, unfold uint_like.zero, unfold sbitvec.zero, rw env.replace_sbv_of_int }, any_goals { simp, unfold_coes, unfold id, rw env.replace_sb_and, rw env.replace_sb_and, rw env.replace_sb_or, rw env.replace_sb_and, rw env.replace_eq2p', rw env.replace_eq2p', rw env.replace_eq2p', { unfold uint_like.allone, unfold sbitvec.uintmax, rw env.replace_sbv_of_int }, { unfold uint_like.signonly, unfold sbitvec.intmin, rw env.replace_sbv_of_int }, { unfold uint_like.zero, unfold sbitvec.zero, rw env.replace_sbv_of_int } } }, { rw HV, cases bopc, { unfold bop_val, unfold has_add.add, unfold uint_like.add, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_sub.sub, unfold uint_like.sub, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_mul.mul, unfold uint_like.mul, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_udiv.udiv, unfold uint_like.udiv, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_umod.umod, unfold uint_like.urem, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_div.div, unfold uint_like.sdiv, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_mod.mod, unfold uint_like.srem, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_and.and, unfold uint_like.and, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_or.or, unfold uint_like.or, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_xor.xor, unfold uint_like.xor, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_shl.shl, unfold uint_like.shl, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_lshr.lshr, unfold uint_like.lshr, unfold freevar.env.replace_sbv }, { unfold bop_val, unfold has_ashr.ashr, unfold uint_like.ashr, unfold freevar.env.replace_sbv } }, { rw HPOISON, unfold bop_poison_all, rw env.replace_sb_and, rw env.replace_sb_and, have AND:∀ (p1 p2 q1 q2:poisonty_smt), p1&p2 = q1&q2 ↔ p1 = q1 ∧ p2 = q2, { intros, split, { unfold has_and.and, unfold bool_like.and, intros H, injection H, split; assumption }, { intros H, cases H with H1 H2, rw [H1, H2] } }, rw AND, rw AND, split, { rw foldr_and_replace, rw bop_poison_replace, apply list.foldr_eq; try { refl }, { intros, rw AND, split, refl, rw bop_poison_flag_replace }, }, split ; refl } end lemma replace_to_ival: ∀ {sz:size} (v:intty_smt sz) (p:poisonty_smt) (η:freevar.env), η⟦to_ival irsem_smt (v, p)⟧ = to_ival irsem_smt (η⟦v⟧, η⟦p⟧) := begin intros, unfold to_ival, unfold freevar.env.replace_valty end lemma step_bop_replace: ∀ (ss:irstate_smt) bopc flags (vop1 vop2: valty_smt) vop1' vop2' (η:freevar.env) lhsn (HVOP1:vop1' = η⟦vop1⟧) (HVOP2:vop2' = η⟦vop2⟧), step_bop irsem_smt vop1' vop2' bopc flags (η⟦ss⟧) lhsn = η⟦step_bop irsem_smt vop1 vop2 bopc flags ss lhsn⟧' := begin intros, cases vop1 with sz1 v1 p1, cases vop2 with sz2 v2 p2, unfold freevar.env.replace_valty at *, rw [HVOP1, HVOP2], unfold step_bop, have HSZ: decidable(sz1 = sz2), apply_instance, cases HSZ, { -- sz1 ≠ sz2 rw dif_neg, rw dif_neg, assumption, assumption }, { -- sz1 = sz2 rw dif_pos, rw dif_pos, have HTY: sbitvec sz2 = irsem_smt.intty sz1, { rw HSZ, refl }, generalize HRES1: bop irsem_smt sz1 bopc flags (η⟦v1⟧) (η⟦p1⟧) (cast HTY (η⟦v2⟧)) (η⟦p2⟧) = res1, generalize HRES2: (bop irsem_smt sz1 bopc flags v1 p1 (cast HTY v2) p2) = res2, cases res1 with resub1 resvp1, cases resvp1 with resv1 resp1, cases res2 with resub2 resvp2, cases resvp2 with resv2 resp2, unfold step_bop._match_2, unfold apply, rw replace_updatereg, rw replace_updateub, rw ← env.replace_sbv_cast at HRES1, rw bop_replace at HRES1, injection HRES1, injection h_2 with h_2 h_3, congr, rw h_1, rw replace_to_ival, congr, rw h_2, rw h_3, rw HRES2, repeat { rw HSZ } } end lemma icmpop_replace: ∀ sz cond η v1 p1 v2 p2 vres pres (H:(vres, pres) = icmpop irsem_smt sz cond v1 p1 v2 p2), icmpop irsem_smt sz cond (η⟦v1⟧) (η⟦p1⟧) (η⟦v2⟧) (η⟦p2⟧) = (η⟦vres⟧, η⟦pres⟧) := begin intros, cases cond, all_goals { unfold icmpop at *, simp at *, unfold icmpop._match_1 at *, injection H, rw h_1, rw h_2, rw env.replace_sb_and, unfold_coes, rw env.replace_sbv_of_bool, unfold_ops }, { rw env.replace_sb_eqbv }, { rw env.replace_sb_nebv }, { rw env.replace_sb_ult }, { rw env.replace_sb_ule }, { rw env.replace_sb_ult }, { rw env.replace_sb_ule }, { rw env.replace_sb_slt }, { rw env.replace_sb_sle }, { rw env.replace_sb_slt }, { rw env.replace_sb_sle } end lemma step_icmpop_replace: ∀ (ss:irstate_smt) cond (vop1 vop2: valty_smt) vop1' vop2' (η:freevar.env) lhsn (HVOP1:vop1' = η⟦vop1⟧) (HVOP2:vop2' = η⟦vop2⟧), step_icmpop irsem_smt vop1' vop2' cond (η⟦ss⟧) lhsn = η⟦step_icmpop irsem_smt vop1 vop2 cond ss lhsn⟧' := begin intros, cases vop1 with sz1 v1 p1, cases vop2 with sz2 v2 p2, unfold freevar.env.replace_valty at *, rw [HVOP1, HVOP2], unfold step_icmpop, have HSZ: decidable(sz1 = sz2), apply_instance, cases HSZ, { -- sz1 ≠ sz2 rw dif_neg, rw dif_neg, assumption, assumption }, { -- sz1 = sz2 rw dif_pos, rw dif_pos, have HTY: sbitvec sz2 = irsem_smt.intty sz1, { rw HSZ, refl }, generalize HRES1: icmpop irsem_smt sz1 cond (η⟦v1⟧) (η⟦p1⟧) (cast HTY (η⟦v2⟧)) (η⟦p2⟧) = res1, generalize HRES2: (icmpop irsem_smt sz1 cond v1 p1 (cast HTY v2) p2) = res2, cases res1 with resv1 resp1, cases res2 with resv2 resp2, unfold apply, rw replace_updatereg, rw ← env.replace_sbv_cast at HRES1, rw icmpop_replace at HRES1, injection HRES1, congr, rw replace_to_ival, rw h_1, rw h_2, rw HRES2, repeat { rw HSZ } } end lemma selectop_replace: ∀ sz η vcond pcond v1 p1 v2 p2 vres pres (H:(vres, pres) = selectop irsem_smt vcond pcond sz v1 p1 v2 p2), selectop irsem_smt (η⟦vcond⟧) (η⟦pcond⟧) sz (η⟦v1⟧) (η⟦p1⟧) (η⟦v2⟧) (η⟦p2⟧) = (η⟦vres⟧, η⟦pres⟧) := begin intros, unfold selectop at *, simp at *, injection H, rw [h_1, h_2], unfold has_ite.ite, unfold has_eq.eq, unfold has_comp.eq, congr, { rw env.replace_sbv_ite, rw env.replace_sb_eqbv, congr }, { rw env.replace_sb_and, rw env.replace_sb_ite, rw env.replace_sb_eqbv, congr } end lemma step_selectop_replace: ∀ (ss:irstate_smt) (vcond vop1 vop2: valty_smt) vcond' vop1' vop2' (η:freevar.env) lhsn (HVCOND:vcond' = η⟦vcond⟧) (HVOP1:vop1' = η⟦vop1⟧) (HVOP2:vop2' = η⟦vop2⟧), step_selectop irsem_smt vcond' vop1' vop2' (η⟦ss⟧) lhsn = η⟦step_selectop irsem_smt vcond vop1 vop2 ss lhsn⟧' := begin intros, cases vcond with szcond vcond pcond, cases vop1 with sz1 v1 p1, cases vop2 with sz2 v2 p2, unfold freevar.env.replace_valty at *, rw [HVCOND, HVOP1, HVOP2], unfold step_selectop, have HSZ: decidable(szcond = size.one), apply_instance, cases HSZ, { -- szcond != size.one rw dif_neg, rw dif_neg, assumption, assumption }, { -- szcond = size.one rw dif_pos, rw dif_pos HSZ, have HSZ2: decidable(sz1 = sz2), apply_instance, cases HSZ2, { rw dif_neg, rw dif_neg, assumption, assumption }, { rw dif_pos, rw dif_pos, rw ← env.replace_sbv_cast, rw ← env.replace_sbv_cast, unfold apply, rw replace_updatereg, rw selectop_replace, { congr }, { refl }, any_goals { assumption }, rw HSZ2 } } end lemma castop_replace: ∀ fromsz code op1 op1p tosz η vres pres (H:(vres, pres) = castop irsem_smt fromsz code op1 op1p tosz), castop irsem_smt fromsz code (η⟦op1⟧) (η⟦op1p⟧) tosz = (η⟦vres⟧, η⟦pres⟧) := begin intros, cases code, any_goals { unfold castop at *, have H1: decidable (fromsz.val < tosz.val), apply_instance, have H2: decidable (fromsz.val = tosz.val), apply_instance, cases H1, { rw if_neg at *, any_goals { assumption }, cases H2, { rw if_neg at *, any_goals { assumption }, injection H with h_1 h_2, rw h_1, rw h_2, congr, unfold uint_like.trunc, rw env.replace_sbv_trunc }, { rw if_pos at *, any_goals { assumption }, injection H with h_1 h_2, rw h_1, rw h_2, congr, unfold uint_like.zero, unfold sbitvec.zero, rw env.replace_sbv_of_int } }, { rw if_pos at *, any_goals { assumption }, injection H with h_1 h_2, rw h_1, rw h_2, try { unfold uint_like.sext, rw env.replace_sbv_sext }, try { unfold uint_like.zext, rw env.replace_sbv_zext }, } } end lemma step_unaryop_replace: ∀ (ss:irstate_smt) (vop:valty_smt) (code:uopcode) (toisz:nat) vop' (η:freevar.env) lhsn (HVOP:vop' = η⟦vop⟧), step_unaryop irsem_smt vop' code toisz (η⟦ss⟧) lhsn = η⟦step_unaryop irsem_smt vop code toisz ss lhsn⟧' := begin intros, cases vop with sz v p, unfold freevar.env.replace_valty at *, rw HVOP, cases code, { unfold step_unaryop }, all_goals { unfold step_unaryop, have HSZ: decidable (toisz > 0), apply_instance, cases HSZ, { rw dif_neg, rw dif_neg, assumption, assumption }, { rw dif_pos, rw dif_pos, simp, unfold apply, rw replace_updatereg, rw castop_replace, { congr }, { simp }, { assumption } } } end lemma step_replace: ∀ (ss:irstate_smt) (i:instruction) (η:freevar.env), step irsem_smt (η⟦ss⟧) i = η⟦step irsem_smt ss i⟧' := begin intros, cases i, case instruction.binop : retty lhs bopc flags op1 op2 { cases lhs, unfold step, unfold has_bind.bind, rw get_value_replace, generalize HOP1: η⟦get_value irsem_smt ss op1 retty⟧' = vop1, generalize HOP2: η⟦get_value irsem_smt ss op2 retty⟧' = vop2, simp at HOP1, cases vop1, { -- none rw apply_none at HOP1, rw HOP1, unfold option.bind }, { have HOP1' := apply_some HOP1, cases HOP1' with vop1' HOP1', rw HOP1', rw get_value_replace, simp at HOP2, cases vop2, { -- none rw apply_none at HOP2, rw HOP2, unfold option.bind, }, { have HOP2' := apply_some HOP2, cases HOP2' with vop2' HOP2', rw HOP2', unfold option.bind, rw step_bop_replace, { rw HOP1' at HOP1, unfold apply at HOP1, injection HOP1, rw h_1 }, { rw HOP2' at HOP2 } } } }, case instruction.icmpop : opty lhs cond op1 op2 { cases lhs, unfold step, unfold has_bind.bind, rw get_value_replace, generalize HOP1: η⟦get_value irsem_smt ss op1 opty⟧' = vop1, generalize HOP2: η⟦get_value irsem_smt ss op2 opty⟧' = vop2, simp at HOP1, cases vop1, { -- none rw apply_none at HOP1, rw HOP1, unfold option.bind }, { have HOP1' := apply_some HOP1, cases HOP1' with vop1' HOP1', rw HOP1', rw get_value_replace, simp at HOP2, cases vop2, { -- none rw apply_none at HOP2, rw HOP2, unfold option.bind, }, { have HOP2' := apply_some HOP2, cases HOP2' with vop2' HOP2', rw HOP2', unfold option.bind, rw step_icmpop_replace, { rw HOP1' at HOP1, unfold apply at HOP1, injection HOP1, rw h_1 }, { rw HOP2' at HOP2 } } } }, case instruction.selectop : lhs condty cond opty op1 op2 { cases lhs, unfold step, unfold has_bind.bind, rw get_value_replace, generalize HOPCOND: η⟦get_value irsem_smt ss cond condty⟧' = vopcond, generalize HOP1: η⟦get_value irsem_smt ss op1 opty⟧' = vop1, generalize HOP2: η⟦get_value irsem_smt ss op2 opty⟧' = vop2, simp at *, cases vopcond, { rw apply_none at HOPCOND, rw HOPCOND, unfold option.bind }, { have HOPCOND' := apply_some HOPCOND, cases HOPCOND' with vopcond' HOPCOND', rw HOPCOND', unfold option.bind, rw get_value_replace, cases vop1, { rw apply_none at HOP1, rw HOP1, unfold option.bind }, { have HOP1' := apply_some HOP1, cases HOP1' with vop1' HOP1', rw HOP1', rw get_value_replace, unfold option.bind, cases vop2, { rw apply_none at HOP2, rw HOP2, unfold option.bind }, { have HOP2' := apply_some HOP2, cases HOP2' with vop2' HOP2', rw HOP2', unfold option.bind, rw step_selectop_replace, { rw HOPCOND' at HOPCOND, unfold apply at HOPCOND, injection HOPCOND, rw h_1 }, { rw HOP1' at HOP1 }, { rw HOP2' at HOP2 } } } } }, case instruction.unaryop : lhs ucode fromty op toty { cases lhs, cases toty, { unfold step, unfold has_bind.bind, rw get_value_replace, generalize HOP: η⟦get_value irsem_smt ss op fromty⟧' = vop, simp at *, cases vop, { unfold option.bind, rw apply_none at HOP, rw HOP, refl }, { have HOP' := apply_some HOP, cases HOP' with vop' HOP', rw HOP', unfold option.bind, rw step_unaryop_replace, { rw HOP' at HOP, unfold apply at HOP, injection HOP, rw h_1 } } }, { unfold step } } end theorem step_encode_both: ∀ ss se i oss ose η (HENC:encode ss se η) (HOSS': oss = step irsem_smt ss i) (HOSE': ose = step irsem_exec se i), none_or_some oss ose (λ ss' se', encode ss' se' η) := begin intros, have HSTEP : none_or_some (step irsem_smt (η⟦ss⟧) i) (step irsem_exec se i) (λ ss' se', irstate_equiv ss' se'), { apply step_both_prf, apply HENC, refl, refl }, have HOSS'': η⟦oss⟧' = step irsem_smt (η⟦ss⟧) i, { rw step_replace, rw HOSS' }, simp at HOSS'', rw ← HOSS'' at *, rw ← HOSE' at *, unfold encode at *, cases oss, { unfold none_or_some at *, cases HSTEP, { rw apply_none at HSTEP, left, assumption }, { cases HSTEP with _ HSTEP, cases HSTEP with _ HSTEP, cases HSTEP with HSTEP _, unfold apply at HSTEP, cases HSTEP } }, { unfold none_or_some at *, cases HSTEP, { left, cases HSTEP with HSTEP _, cases HSTEP }, { unfold apply at HSTEP, cases HSTEP with s HSTEP, cases HSTEP with e HSTEP, cases HSTEP with HSTEP_l HSTEP, cases HSTEP with HSTEP_r HSTEP_r2, injection HSTEP_l, rw ← h_1 at HSTEP_r2, right, apply exists.intro oss, apply exists.intro e, split, refl, split, assumption, assumption } } end theorem bigstep_both_prf: bigstep_both := begin unfold bigstep_both, intros, revert ss se oss' ose', cases p, induction p with i p', { -- empty instruction intros, unfold irsem.bigstep at HOSS' HOSE', simp at HOSS' HOSE', right, apply exists.intro ss, apply exists.intro se, split, assumption, split, assumption, assumption }, { -- a new instruction at the front intros, generalize HSS:irsem.step irsem_smt ss i = oss0, generalize HSE:irsem.step irsem_exec se i = ose0, have HENC0 : none_or_some oss0 ose0 (λ ss0 se0, encode ss0 se0 η), { apply step_encode_both, apply HENC, rw HSS, rw HSE }, cases oss0, { -- none, none have H: ose0 = none, { apply none_or_some_none, apply HENC0, refl }, rw H at *, rw bigstep_unroll_none_smt at HOSS', rw bigstep_unroll_none_exec at HOSE', rw HOSS', rw HOSE', apply none_or_some_none2; refl, rw HSE, rw HSS }, { -- some, some have H: (∃ se0', ose0 = some se0'), { apply none_or_some_some, apply HENC0, refl }, cases H with se0' H, rw H at *, unfold none_or_some at HENC0, cases HENC0, { cases HENC0, cases HENC0_left }, { cases HENC0 with s HENC0, cases HENC0 with e HENC0, cases HENC0 with _ HENC0, cases HENC0 with _ HENC0, apply p_ih, apply HENC0, rw ← bigstep_unroll_some_smt, apply HOSS', rw HSS, rw HENC0_left, rw ← bigstep_unroll_some_exec, apply HOSE', rw HSE, rw HENC0_left_1 } } } end lemma bigstep_replace: ∀ ss p (η:freevar.env), η⟦bigstep irsem_smt ss p⟧' = bigstep irsem_smt (η⟦ss⟧) p := begin intros, simp, cases p, revert ss, induction p with i p, { intros, refl }, { intros, generalize HSS: step irsem_smt ss i = ss', generalize HRSS: step irsem_smt (η⟦ss⟧) i = rss', cases ss'; cases rss', { rw bigstep_unroll_none_smt, rw bigstep_unroll_none_smt, rw HRSS, rw HSS }, any_goals { rw step_replace at HRSS, rw HSS at HRSS, cases HRSS, done }, { rw bigstep_unroll_some_smt, rw bigstep_unroll_some_smt, apply p_ih, have HTMP: η⟦step irsem_smt ss i⟧' = η⟦some ss'⟧', { rw HSS }, rw ← step_replace at HTMP, rw HRSS at HTMP, unfold apply at HTMP, injection HTMP, rw h_1 at HRSS, apply (eq.symm HRSS), rw HSS } } end end spec
bb73d61c8f0a71aac7a92466232036b425816d24
d534932ed7c1eba03b537c377a4f8961acd41e99
/examples/gethostname/lakefile.lean
c0ec5b5f68953a6fbf8c9479b6b27662169705a0
[ "Apache-2.0" ]
permissive
Adminixtrator/lean4-socket
d7e321d547df6545d0c085d310be8f2c41c44ddb
b313041f2e75f4ad8320ab66d7e2afafd2202318
refs/heads/main
1,692,582,696,753
1,633,439,398,000
1,633,439,523,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
135
lean
import Lake open Lake DSL package gethostname where dependencies := #[{ name := `socket src := Source.path "../../lake" }]
07103ec5eacb67ad1315aebda66a2301c3746b2d
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/logic/identities.lean
ed2f790a722687672178967afbf192c99c83c081
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
4,347
lean
/- Copyright (c) 2014 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura Useful logical identities. Since we are not using propositional extensionality, some of the calculations use the type class support provided by logic.instances. -/ import logic.connectives logic.quantifiers logic.cast open decidable theorem or.right_comm (a b c : Prop) : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := calc (a ∨ b) ∨ c ↔ a ∨ (b ∨ c) : or.assoc ... ↔ a ∨ (c ∨ b) : sorry -- by rewrite (@or.comm b c) ... ↔ (a ∨ c) ∨ b : iff.symm or.assoc theorem and.right_comm (a b c : Prop) : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := calc (a ∧ b) ∧ c ↔ a ∧ (b ∧ c) : and.assoc ... ↔ a ∧ (c ∧ b) : sorry -- by rewrite (@and.comm b c) ... ↔ (a ∧ c) ∧ b : iff.symm and.assoc theorem or_not_self_iff (a : Prop) [D : decidable a] : a ∨ ¬ a ↔ true := iff.intro (assume H, trivial) (assume H, em a) theorem not_or_self_iff (a : Prop) [D : decidable a] : ¬ a ∨ a ↔ true := iff.intro (λ H, trivial) (λ H, or.swap (em a)) theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume H, (and.right H) (and.left H)) (assume H, false.elim H) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := sorry -- iff.intro (λ H, and.elim H (by contradiction)) (λ H, false.elim H) theorem not_not_iff (a : Prop) [D : decidable a] : ¬¬a ↔ a := iff.intro by_contradiction not_not_intro theorem not_not_elim {a : Prop} [D : decidable a] : ¬¬a → a := by_contradiction theorem not_or_iff_not_and_not (a b : Prop) : ¬(a ∨ b) ↔ ¬a ∧ ¬b := or.imp_distrib theorem not_or_not_of_not_and {a b : Prop} [Da : decidable a] (H : ¬ (a ∧ b)) : ¬ a ∨ ¬ b := by_cases (λHa, or.inr (not.mto (and.intro Ha) H)) or.inl theorem not_or_not_of_not_and' {a b : Prop} [Db : decidable b] (H : ¬ (a ∧ b)) : ¬ a ∨ ¬ b := by_cases (λHb, or.inl (λHa, H (and.intro Ha Hb))) or.inr theorem not_and_iff_not_or_not (a b : Prop) [Da : decidable a] : ¬(a ∧ b) ↔ ¬a ∨ ¬b := iff.intro not_or_not_of_not_and (or.rec (not.mto and.left) (not.mto and.right)) theorem or_iff_not_and_not (a b : Prop) [Da : decidable a] [Db : decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := sorry -- by rewrite [-not_or_iff_not_and_not, not_not_iff] theorem and_iff_not_or_not (a b : Prop) [Da : decidable a] [Db : decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := sorry -- by rewrite [-not_and_iff_not_or_not, not_not_iff] theorem imp_iff_not_or (a b : Prop) [Da : decidable a] : (a → b) ↔ ¬a ∨ b := iff.intro (by_cases (λHa H, or.inr (H Ha)) (λHa H, or.inl Ha)) (or.rec not.elim imp.intro) theorem not_implies_iff_and_not (a b : Prop) [Da : decidable a] : ¬(a → b) ↔ a ∧ ¬b := calc ¬(a → b) ↔ ¬(¬a ∨ b) : sorry -- by rewrite (imp_iff_not_or a b) ... ↔ ¬¬a ∧ ¬b : not_or_iff_not_and_not _ _ ... ↔ a ∧ ¬b : sorry -- by rewrite (not_not_iff a) theorem and_not_of_not_implies {a b : Prop} [Da : decidable a] (H : ¬ (a → b)) : a ∧ ¬ b := iff.mp (not_implies_iff_and_not a b) H theorem not_implies_of_and_not {a b : Prop} [Da : decidable a] (H : a ∧ ¬ b) : ¬ (a → b) := iff.mpr (not_implies_iff_and_not a b) H theorem peirce (a b : Prop) [D : decidable a] : ((a → b) → a) → a := by_cases imp.intro (imp.syl imp.mp not.elim) theorem forall_not_of_not_exists {A : Type} {p : A → Prop} [D : ∀x, decidable (p x)] (H : ¬∃x, p x) : ∀x, ¬p x := take x, by_cases (assume Hp : p x, absurd (exists.intro x Hp) H) imp.id theorem forall_of_not_exists_not {A : Type} {p : A → Prop} [D : decidable_pred p] : ¬(∃ x, ¬p x) → ∀ x, p x := imp.syl (forall_imp_forall (λa, not_not_elim)) forall_not_of_not_exists theorem exists_not_of_not_forall {A : Type} {p : A → Prop} [D : ∀x, decidable (p x)] [D' : decidable (∃x, ¬p x)] (H : ¬∀x, p x) : ∃x, ¬p x := by_contradiction (λH1, absurd (λx, not_not_elim (forall_not_of_not_exists H1 x)) H) theorem exists_of_not_forall_not {A : Type} {p : A → Prop} [D : ∀x, decidable (p x)] [D' : decidable (∃x, p x)] (H : ¬∀x, ¬ p x) : ∃x, p x := by_contradiction (imp.syl H forall_not_of_not_exists)
89f44822e77737abd03cbda2485ace57e4497998
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/algebra/quaternion_basis.lean
b2cc69d8db73cd2648913303287215677861372d
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
6,031
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import algebra.quaternion import tactic.ring /-! # Basis on a quaternion-like algebra ## Main definitions * `quaternion_algebra.basis A c₁ c₂`: a basis for a subspace of an `R`-algebra `A` that has the same algebra structure as `ℍ[R,c₁,c₂]`. * `quaternion_algebra.basis.self R`: the canonical basis for `ℍ[R,c₁,c₂]`. * `quaternion_algebra.basis.comp_hom b f`: transform a basis `b` by an alg_hom `f`. * `quaternion_algebra.lift`: Define an `alg_hom` out of `ℍ[R,c₁,c₂]` by its action on the basis elements `i`, `j`, and `k`. In essence, this is a universal property. Analogous to `complex.lift`, but takes a bundled `quaternion_algebra.basis` instead of just a `subtype` as the amount of data / proves is non-negligeable. -/ open_locale quaternion namespace quaternion_algebra /-- A quaternion basis contains the information both sufficient and necessary to construct an `R`-algebra homomorphism from `ℍ[R,c₁,c₂]` to `A`; or equivalently, a surjective `R`-algebra homomorphism from `ℍ[R,c₁,c₂]` to an `R`-subalgebra of `A`. Note that for definitional convenience, `k` is provided as a field even though `i_mul_j` fully determines it. -/ structure basis {R : Type*} (A : Type*) [comm_ring R] [ring A] [algebra R A] (c₁ c₂ : R) := (i j k : A) (i_mul_i : i * i = c₁ • 1) (j_mul_j : j * j = c₂ • 1) (i_mul_j : i * j = k) (j_mul_i : j * i = -k) variables {R : Type*} {A B : Type*} [comm_ring R] [ring A] [ring B] [algebra R A] [algebra R B] variables {c₁ c₂ : R} namespace basis /-- Since `k` is redundant, it is not necessary to show `q₁.k = q₂.k` when showing `q₁ = q₂`. -/ @[ext] protected lemma ext ⦃q₁ q₂ : basis A c₁ c₂⦄ (hi : q₁.i = q₂.i) (hj : q₁.j = q₂.j) : q₁ = q₂ := begin cases q₁, cases q₂, congr', rw [←q₁_i_mul_j, ←q₂_i_mul_j], congr' end variables (R) /-- There is a natural quaternionic basis for the `quaternion_algebra`. -/ @[simps i j k] protected def self : basis ℍ[R,c₁,c₂] c₁ c₂ := { i := ⟨0, 1, 0, 0⟩, i_mul_i := by { ext; simp }, j := ⟨0, 0, 1, 0⟩, j_mul_j := by { ext; simp }, k := ⟨0, 0, 0, 1⟩, i_mul_j := by { ext; simp }, j_mul_i := by { ext; simp } } variables {R} instance : inhabited (basis ℍ[R,c₁,c₂] c₁ c₂) := ⟨basis.self R⟩ variables (q : basis A c₁ c₂) include q attribute [simp] i_mul_i j_mul_j i_mul_j j_mul_i @[simp] lemma i_mul_k : q.i * q.k = c₁ • q.j := by rw [←i_mul_j, ←mul_assoc, i_mul_i, smul_mul_assoc, one_mul] @[simp] lemma k_mul_i : q.k * q.i = -c₁ • q.j := by rw [←i_mul_j, mul_assoc, j_mul_i, mul_neg_eq_neg_mul_symm, i_mul_k, neg_smul] @[simp] lemma k_mul_j : q.k * q.j = c₂ • q.i := by rw [←i_mul_j, mul_assoc, j_mul_j, mul_smul_comm, mul_one] @[simp] lemma j_mul_k : q.j * q.k = -c₂ • q.i := by rw [←i_mul_j, ←mul_assoc, j_mul_i, neg_mul_eq_neg_mul_symm, k_mul_j, neg_smul] @[simp] lemma k_mul_k : q.k * q.k = -((c₁ * c₂) • 1) := by rw [←i_mul_j, mul_assoc, ←mul_assoc q.j _ _, j_mul_i, ←i_mul_j, ←mul_assoc, mul_neg_eq_neg_mul_symm, ←mul_assoc, i_mul_i, smul_mul_assoc, one_mul, neg_mul_eq_neg_mul_symm, smul_mul_assoc, j_mul_j, smul_smul] /-- Intermediate result used to define `quaternion_algebra.basis.lift_hom`. -/ def lift (x : ℍ[R,c₁,c₂]) : A := algebra_map R _ x.re + x.im_i • q.i + x.im_j • q.j + x.im_k • q.k lemma lift_zero : q.lift (0 : ℍ[R,c₁,c₂]) = 0 := by simp [lift] lemma lift_one : q.lift (1 : ℍ[R,c₁,c₂]) = 1 := by simp [lift] lemma lift_add (x y : ℍ[R,c₁,c₂]) : q.lift (x + y) = q.lift x + q.lift y := by { simp [lift, add_smul], abel } lemma lift_mul (x y : ℍ[R,c₁,c₂]) : q.lift (x * y) = q.lift x * q.lift y := begin simp only [lift, algebra.algebra_map_eq_smul_one], simp only [add_mul], simp only [add_mul, mul_add, smul_mul_assoc, mul_smul_comm, one_mul, mul_one, ←algebra.smul_def, smul_add, smul_smul], simp only [i_mul_i, j_mul_j, i_mul_j, j_mul_i, i_mul_k, k_mul_i, k_mul_j, j_mul_k, k_mul_k], simp only [smul_smul, smul_neg, sub_eq_add_neg, add_smul, ←add_assoc, mul_neg_eq_neg_mul_symm, neg_smul], simp only [mul_right_comm _ _ (c₁ * c₂), mul_comm _ (c₁ * c₂)], simp only [mul_comm _ c₁, mul_right_comm _ _ c₁], simp only [mul_comm _ c₂, mul_right_comm _ _ c₂], simp only [←mul_comm c₁ c₂, ←mul_assoc], simp [sub_eq_add_neg, add_smul, ←add_assoc], abel end lemma lift_smul (r : R) (x : ℍ[R,c₁,c₂]) : q.lift (r • x) = r • q.lift x := by simp [lift, mul_smul, ←algebra.smul_def] /-- A `quaternion_algebra.basis` implies an `alg_hom` from the quaternions. -/ @[simps] def lift_hom : ℍ[R,c₁,c₂] →ₐ[R] A := alg_hom.mk' { to_fun := q.lift, map_zero' := q.lift_zero, map_one' := q.lift_one, map_add' := q.lift_add, map_mul' := q.lift_mul } q.lift_smul /-- Transform a `quaternion_algebra.basis` through an `alg_hom`. -/ @[simps i j k] def comp_hom (F : A →ₐ[R] B) : basis B c₁ c₂ := { i := F q.i, i_mul_i := by rw [←F.map_mul, q.i_mul_i, F.map_smul, F.map_one], j := F q.j, j_mul_j := by rw [←F.map_mul, q.j_mul_j, F.map_smul, F.map_one], k := F q.k, i_mul_j := by rw [←F.map_mul, q.i_mul_j], j_mul_i := by rw [←F.map_mul, q.j_mul_i, F.map_neg], } end basis /-- A quaternionic basis on `A` is equivalent to a map from the quaternion algebra to `A`. -/ @[simps] def lift : basis A c₁ c₂ ≃ (ℍ[R,c₁,c₂] →ₐ[R] A) := { to_fun := basis.lift_hom, inv_fun := (basis.self R).comp_hom, left_inv := λ q, begin ext; simp [basis.lift], end, right_inv := λ F, begin ext, dsimp [basis.lift], rw ←F.commutes, simp only [←F.commutes, ←F.map_smul, ←F.map_add, mk_add_mk, smul_mk, smul_zero, algebra_map_eq], congr, simp, end } end quaternion_algebra
1b690f9113490faa12d4335a32948b475fd28389
96338d06deb5f54f351493a71d6ecf6c546089a2
/priv/Lean/Adjunction.lean
99f83a45fe7bdaf12a77a3e6e1adf186c78562ec
[]
no_license
silky/exe
5f9e4eea772d74852a1a2fac57d8d20588282d2b
e81690d6e16f2a83c105cce446011af6ae905b81
refs/heads/master
1,609,385,766,412
1,472,164,223,000
1,472,164,223,000
66,610,224
1
0
null
1,472,178,919,000
1,472,178,919,000
null
UTF-8
Lean
false
false
5,329
lean
/- Adjunction -/ import Setoid import Cat import Mor import Functor set_option pp.universes true set_option pp.metavar_args false namespace EXE namespace Adj abbreviation TriangleLProp {C D : CatType} (L : D⟶C) (R : C⟶D) (η : 𝟙 ⟹ (R ⊗ L)) (ε : (L ⊗ R) ⟹ 𝟙) : Prop := ∀ (X : D), ((ε /$$ (L $$ X)) ⊙C⊙ (L $$/ (η /$$ X))) ≡((L X) ⇒C⇒ (L X))≡ ① abbreviation TriangleRProp {C D : CatType} (L : D⟶C) (R : C⟶D) (η : 𝟙 ⟹ (R ⊗ L)) (ε : (L ⊗ R) ⟹ 𝟙) : Prop := ∀ (Y : C), ((R $$/ (ε /$$ Y)) ⊙D⊙ (η /$$ (R $$ Y))) ≡((R Y) ⇒D⇒ (R Y))≡ ① end Adj record AdjType {C : CatType} {D : CatType} (L : D⟶C) (R : C⟶D) : Type := (unit : 𝟙 ⟹ (R ⊗ L) ) (counit : (L ⊗ R) ⟹ 𝟙 ) (triangleL : Adj.TriangleLProp L R unit counit) (triangleR : Adj.TriangleRProp L R unit counit) infix ` ⊣ `:10 := AdjType record LeftAdj {C D : CatType} (Right : C ⟶ D) : Type := (Left : D ⟶ C) (adj : Left ⊣ Right) record RightAdj {C D : CatType} (Left : D ⟶ C) : Type := (Right : C ⟶ D) (adj : Left ⊣ Right) definition Lim (C D : CatType) := RightAdj (Cat.Delta C D) definition HaveAllLim (D : CatType) : Type := Π (C : CatType), Lim C D record CompleteCatType : Type := (C : CatType) (Lim : HaveAllLim C) definition Colim (C D : CatType) := LeftAdj (Cat.Delta C D) definition HaveAllColim (D : CatType) : Type := Π (C : CatType), Colim C D record CocompleteCatType : Type := (C : CatType) (Colim : HaveAllColim C) definition Lim.Apply {D : CatType} (lim : HaveAllLim D) {C : CatType} (F : C⟶D) : D := (RightAdj.Right (lim C)) $$ F definition Lim.Prj {C D : CatType} (lim : HaveAllLim D) (F : C⟶D) : (Cat.Delta C D (Lim.Apply lim F)) ⟹ F := (AdjType.counit (RightAdj.adj (lim C))) /$$ F definition Colim.Apply {D : CatType} (colim : HaveAllColim D) {C : CatType} (F : C⟶D) : D := (LeftAdj.Left (colim C)) $$ F definition Colim.Inj {C D : CatType} (colim : HaveAllColim D) (F : C⟶D) : F ⟹ (Cat.Delta C D (Colim.Apply colim F)) := (AdjType.unit (LeftAdj.adj (colim C))) /$$ F print AdjType print RightAdj print HaveAllLim namespace Adj namespace IsoOnLR definition LtoR {C D : CatType} {L : D⟶C} {R : C⟶D} (adj : AdjType L R) (X : D) (Y : C) : (L X ⇒C⇒ Y) ⥤ (X ⇒D⇒ R Y) := let ε := AdjType.counit adj, η := AdjType.unit adj in Setoid.MkHom ( λ (f : L X ⇒C⇒ Y), (R $$/ f) ⊙D⊙ (η /$$ X)) ( λ (f1 f2 : L X ⇒C⇒ Y), λ (eq : f1 ≡(L X ⇒C⇒ Y)≡ f2), (R $$// eq) /⊙D⊙ (η /$$ X)) definition RtoL {C D : CatType} {L : D⟶C} {R : C⟶D} (adj : AdjType L R) (X : D) (Y : C) : (X ⇒D⇒ R Y) ⥤ (L X ⇒C⇒ Y) := let ε := AdjType.counit adj, η := AdjType.unit adj in Setoid.MkHom ( λ (g : X ⇒D⇒ R Y), (ε /$$ Y) ⊙C⊙ (L $$/ g)) ( λ (g1 g2 : X ⇒D⇒ R Y), λ (eq : g1 ≡(X ⇒D⇒ R Y)≡ g2), (ε /$$ Y) ⊙C⊙/ (L $$// eq)) definition LeqL {C D : CatType} {L : D⟶C} {R : C⟶D} (adj : AdjType L R) (X : D) (Y : C) (f : L X ⇒C⇒ Y) : let ε := AdjType.counit adj, η := AdjType.unit adj in ((ε /$$ Y) ⊙C⊙ (L $$/ ((R $$/ f) ⊙D⊙ (η /$$ X)))) ≡(L X ⇒C⇒ Y)≡ f := let ε := AdjType.counit adj, η := AdjType.unit adj in begin refine (CatType.MulHE C (ε /$$ Y) (FunctorType.onMul L (R $$/ f) (η /$$ X))) ⊡(L X ⇒C⇒ Y)⊡ _, refine (CatType.AssocInv C (ε /$$ Y) (L $$/ (R $$/ f)) (L $$/ (η /$$ X))) ⊡(L X ⇒C⇒ Y)⊡ _, refine (CatType.MulEH C (ε /$$/ f) (L $$/ (η /$$ X))) ⊡(L X ⇒C⇒ Y)⊡ _, refine (CatType.Assoc C f (ε /$$ (L $$ X)) (L $$/ (η /$$ X))) ⊡(L X ⇒C⇒ Y)⊡ _, refine (CatType.MulHE C f (proof AdjType.triangleL adj X qed)) ⊡(L X ⇒C⇒ Y)⊡ _, refine (CatType.UnitR C f) end definition ReqR {C D : CatType} {L : D⟶C} {R : C⟶D} (adj : AdjType L R) (X : D) (Y : C) (g : X ⇒D⇒ R Y) : let ε := AdjType.counit adj, η := AdjType.unit adj in ((R $$/ ((ε /$$ Y) ⊙C⊙ (L $$/ g))) ⊙D⊙ (η /$$ X)) ≡(X ⇒D⇒ R Y)≡ g := let ε := AdjType.counit adj, η := AdjType.unit adj in begin refine (CatType.MulEH D (FunctorType.onMul R (ε /$$ Y) (L $$/ g)) (η /$$ X)) ⊡(X ⇒D⇒ R Y)⊡ _, refine (CatType.Assoc D (R $$/ (ε /$$ Y)) (R $$/ (L $$/ g)) (η /$$ X)) ⊡(X ⇒D⇒ R Y)⊡ _, refine (CatType.MulHE D (R $$/ (ε /$$ Y)) (η /$/$/ g)) ⊡(X ⇒D⇒ R Y)⊡ _, refine (CatType.AssocInv D (R $$/ (ε /$$ Y)) (η /$$ (R $$ Y)) g) ⊡(X ⇒D⇒ R Y)⊡ _, refine (CatType.MulEH D (AdjType.triangleR adj Y) g) ⊡(X ⇒D⇒ R Y)⊡ _, refine (CatType.UnitL D g) end end IsoOnLR definition IsoOnLR {C D : CatType} {L : D⟶C} {R : C⟶D} (adj : AdjType L R) (X : D) (Y : C) : (L X ⇒C⇒ Y) ⇔ (X ⇒D⇒ R Y) := CatType.MkIso (@IsoOnLR.LtoR C D L R adj X Y) (@IsoOnLR.RtoL C D L R adj X Y) (@IsoOnLR.LeqL C D L R adj X Y) (@IsoOnLR.ReqR C D L R adj X Y) end Adj end EXE
4edd5a6174fab62901ce11f7d3d2ef45bf9d4166
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/analysis/normed/field/unit_ball.lean
3b52ad13ab74aa23aaf22f0687e5f4785a9c9cec
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
7,424
lean
/- Copyright (c) 2022 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Heather Macbeth -/ import analysis.normed.field.basic import analysis.normed.group.ball_sphere /-! # Algebraic structures on unit balls and spheres In this file we define algebraic structures (`semigroup`, `comm_semigroup`, `monoid`, `comm_monoid`, `group`, `comm_group`) on `metric.ball (0 : 𝕜) 1`, `metric.closed_ball (0 : 𝕜) 1`, and `metric.sphere (0 : 𝕜) 1`. In each case we use the weakest possible typeclass assumption on `𝕜`, from `non_unital_semi_normed_ring` to `normed_field`. -/ open set metric variables {𝕜 : Type*} /-- Unit ball in a non unital semi normed ring as a bundled `subsemigroup`. -/ def subsemigroup.unit_ball (𝕜 : Type*) [non_unital_semi_normed_ring 𝕜] : subsemigroup 𝕜 := { carrier := ball (0 : 𝕜) 1, mul_mem' := λ x y hx hy, begin rw [mem_ball_zero_iff] at *, exact (norm_mul_le x y).trans_lt (mul_lt_one_of_nonneg_of_lt_one_left (norm_nonneg _) hx hy.le) end } instance [non_unital_semi_normed_ring 𝕜] : semigroup (ball (0 : 𝕜) 1) := mul_mem_class.to_semigroup (subsemigroup.unit_ball 𝕜) instance [non_unital_semi_normed_ring 𝕜] : has_continuous_mul (ball (0 : 𝕜) 1) := (subsemigroup.unit_ball 𝕜).has_continuous_mul instance [semi_normed_comm_ring 𝕜] : comm_semigroup (ball (0 : 𝕜) 1) := mul_mem_class.to_comm_semigroup (subsemigroup.unit_ball 𝕜) instance [non_unital_semi_normed_ring 𝕜] : has_distrib_neg (ball (0 : 𝕜) 1) := subtype.coe_injective.has_distrib_neg (coe : ball (0 : 𝕜) 1 → 𝕜) (λ _, rfl) (λ _ _, rfl) @[simp, norm_cast] lemma coe_mul_unit_ball [non_unital_semi_normed_ring 𝕜] (x y : ball (0 : 𝕜) 1) : ↑(x * y) = (x * y : 𝕜) := rfl /-- Closed unit ball in a non unital semi normed ring as a bundled `subsemigroup`. -/ def subsemigroup.unit_closed_ball (𝕜 : Type*) [non_unital_semi_normed_ring 𝕜] : subsemigroup 𝕜 := { carrier := closed_ball 0 1, mul_mem' := λ x y hx hy, begin rw [mem_closed_ball_zero_iff] at *, exact (norm_mul_le x y).trans (mul_le_one hx (norm_nonneg _) hy) end } instance [non_unital_semi_normed_ring 𝕜] : semigroup (closed_ball (0 : 𝕜) 1) := mul_mem_class.to_semigroup (subsemigroup.unit_closed_ball 𝕜) instance [non_unital_semi_normed_ring 𝕜] : has_distrib_neg (closed_ball (0 : 𝕜) 1) := subtype.coe_injective.has_distrib_neg (coe : closed_ball (0 : 𝕜) 1 → 𝕜) (λ _, rfl) (λ _ _, rfl) instance [non_unital_semi_normed_ring 𝕜] : has_continuous_mul (closed_ball (0 : 𝕜) 1) := (subsemigroup.unit_closed_ball 𝕜).has_continuous_mul @[simp, norm_cast] lemma coe_mul_unit_closed_ball [non_unital_semi_normed_ring 𝕜] (x y : closed_ball (0 : 𝕜) 1) : ↑(x * y) = (x * y : 𝕜) := rfl /-- Closed unit ball in a semi normed ring as a bundled `submonoid`. -/ def submonoid.unit_closed_ball (𝕜 : Type*) [semi_normed_ring 𝕜] [norm_one_class 𝕜] : submonoid 𝕜 := { carrier := closed_ball 0 1, one_mem' := mem_closed_ball_zero_iff.2 norm_one.le, .. subsemigroup.unit_closed_ball 𝕜 } instance [semi_normed_ring 𝕜] [norm_one_class 𝕜] : monoid (closed_ball (0 : 𝕜) 1) := submonoid_class.to_monoid (submonoid.unit_closed_ball 𝕜) instance [semi_normed_comm_ring 𝕜] [norm_one_class 𝕜] : comm_monoid (closed_ball (0 : 𝕜) 1) := submonoid_class.to_comm_monoid (submonoid.unit_closed_ball 𝕜) @[simp, norm_cast] lemma coe_one_unit_closed_ball [semi_normed_ring 𝕜] [norm_one_class 𝕜] : ((1 : closed_ball (0 : 𝕜) 1) : 𝕜) = 1 := rfl @[simp, norm_cast] lemma coe_pow_unit_closed_ball [semi_normed_ring 𝕜] [norm_one_class 𝕜] (x : closed_ball (0 : 𝕜) 1) (n : ℕ) : ↑(x ^ n) = (x ^ n : 𝕜) := rfl /-- Unit sphere in a normed division ring as a bundled `submonoid`. -/ def submonoid.unit_sphere (𝕜 : Type*) [normed_division_ring 𝕜] : submonoid 𝕜 := { carrier := sphere (0 : 𝕜) 1, mul_mem' := λ x y hx hy, by { rw [mem_sphere_zero_iff_norm] at *, simp * }, one_mem' := mem_sphere_zero_iff_norm.2 norm_one } instance [normed_division_ring 𝕜] : has_inv (sphere (0 : 𝕜) 1) := ⟨λ x, ⟨x⁻¹, mem_sphere_zero_iff_norm.2 $ by rw [norm_inv, mem_sphere_zero_iff_norm.1 x.coe_prop, inv_one]⟩⟩ @[simp, norm_cast] lemma coe_inv_unit_sphere [normed_division_ring 𝕜] (x : sphere (0 : 𝕜) 1) : ↑x⁻¹ = (x⁻¹ : 𝕜) := rfl instance [normed_division_ring 𝕜] : has_div (sphere (0 : 𝕜) 1) := ⟨λ x y, ⟨x / y, mem_sphere_zero_iff_norm.2 $ by rw [norm_div, mem_sphere_zero_iff_norm.1 x.coe_prop, mem_sphere_zero_iff_norm.1 y.coe_prop, div_one]⟩⟩ @[simp, norm_cast] lemma coe_div_unit_sphere [normed_division_ring 𝕜] (x y : sphere (0 : 𝕜) 1) : ↑(x / y) = (x / y : 𝕜) := rfl instance [normed_division_ring 𝕜] : has_pow (sphere (0 : 𝕜) 1) ℤ := ⟨λ x n, ⟨x ^ n, by rw [mem_sphere_zero_iff_norm, norm_zpow, mem_sphere_zero_iff_norm.1 x.coe_prop, one_zpow]⟩⟩ @[simp, norm_cast] lemma coe_zpow_unit_sphere [normed_division_ring 𝕜] (x : sphere (0 : 𝕜) 1) (n : ℤ) : ↑(x ^ n) = (x ^ n : 𝕜) := rfl instance [normed_division_ring 𝕜] : monoid (sphere (0 : 𝕜) 1) := submonoid_class.to_monoid (submonoid.unit_sphere 𝕜) @[simp, norm_cast] lemma coe_one_unit_sphere [normed_division_ring 𝕜] : ((1 : sphere (0 : 𝕜) 1) : 𝕜) = 1 := rfl @[simp, norm_cast] lemma coe_mul_unit_sphere [normed_division_ring 𝕜] (x y : sphere (0 : 𝕜) 1) : ↑(x * y) = (x * y : 𝕜) := rfl @[simp, norm_cast] lemma coe_pow_unit_sphere [normed_division_ring 𝕜] (x : sphere (0 : 𝕜) 1) (n : ℕ) : ↑(x ^ n) = (x ^ n : 𝕜) := rfl /-- Monoid homomorphism from the unit sphere to the group of units. -/ def unit_sphere_to_units (𝕜 : Type*) [normed_division_ring 𝕜] : sphere (0 : 𝕜) 1 →* units 𝕜 := units.lift_right (submonoid.unit_sphere 𝕜).subtype (λ x, units.mk0 x $ ne_zero_of_mem_unit_sphere _) (λ x, rfl) @[simp] lemma unit_sphere_to_units_apply_coe [normed_division_ring 𝕜] (x : sphere (0 : 𝕜) 1) : (unit_sphere_to_units 𝕜 x : 𝕜) = x := rfl lemma unit_sphere_to_units_injective [normed_division_ring 𝕜] : function.injective (unit_sphere_to_units 𝕜) := λ x y h, subtype.eq $ by convert congr_arg units.val h instance [normed_division_ring 𝕜] : group (sphere (0 : 𝕜) 1) := unit_sphere_to_units_injective.group (unit_sphere_to_units 𝕜) (units.ext rfl) (λ x y, units.ext rfl) (λ x, units.ext rfl) (λ x y, units.ext $ div_eq_mul_inv _ _) (λ x n, units.ext (units.coe_pow (unit_sphere_to_units 𝕜 x) n).symm) (λ x n, units.ext (units.coe_zpow (unit_sphere_to_units 𝕜 x) n).symm) instance [normed_division_ring 𝕜] : has_distrib_neg (sphere (0 : 𝕜) 1) := subtype.coe_injective.has_distrib_neg (coe : sphere (0 : 𝕜) 1 → 𝕜) (λ _, rfl) (λ _ _, rfl) instance [normed_division_ring 𝕜] : topological_group (sphere (0 : 𝕜) 1) := { to_has_continuous_mul := (submonoid.unit_sphere 𝕜).has_continuous_mul, continuous_inv := continuous_subtype_mk _ $ continuous_subtype_coe.inv₀ ne_zero_of_mem_unit_sphere } instance [normed_field 𝕜] : comm_group (sphere (0 : 𝕜) 1) := { .. metric.sphere.group, .. submonoid_class.to_comm_monoid (submonoid.unit_sphere 𝕜) }
906e63226fac5570e9d9f383a95eaf50f608092a
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/interactive/goal_info.lean
06692eba4574de4fb04c023d7f13edae26278f3e
[ "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
1,038
lean
constant addc {a b : nat} : a + b = b + a constant addassoc {a b c : nat} : (a + b) + c = a + (b + c) constant zadd (a : nat) : 0 + a = a open nat example : ∀ n m : ℕ, n + m = m + n := begin intros n m, induction m with m' ih, --^ "command": "info" { change n + 0 = 0 + n, simp [zadd, nat.add_zero, nat.zero_add] }, --^ "command": "info" { change succ (n + m') = succ m' + n, rw [succ_add, ih] --^ "command":"info" } end example : ∀ n m : ℕ, n + m = m + n := begin intros n m, induction m with m' ih, { change n + 0 = 0 + n, simp [zadd, nat.add_zero, nat.zero_add] }, --^ "command": "info" { change succ (n + m') = succ m' + n, rw [succ_add, ih] } end example : ∀ n m : ℕ, n + m = m + n := begin intros n m, induction m with m' ih, { change n + 0 = 0 + n, simp [zadd, nat.add_zero, nat.zero_add] }, --^ "command": "info" { change succ (n + m') = succ m' + n, rw [succ_add, ih] } end
b317105fd3b0f8f52983a8c8cf59cec5b397186d
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/tests/lean/run/909.lean
64c4220de2792f270b9d0e5b1368dded366c0d0f
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
910
lean
structure Date where val : Nat deriving Repr instance : LE Date := ⟨InvImage (Nat.le) Date.val⟩ instance bad (a b : Date) : Decidable (a <= b) := if h0 : (a.val <= b.val) then isTrue h0 else isFalse (fun hf => False.elim (h0 hf)) /- This implementation also fails: instance (a b : Date) : Decidable (a <= b) := inferInstanceAs (Decidable (a.val <= b.val)) -/ /- This implemenation evaluates successfully: instance (a b : Date) : Decidable (a <= b) := dite (a.val <= b.val) isTrue (fun nle => isFalse (fun hf => False.elim (nle hf))) -/ instance : ToString Date where toString d := s!"D{d.val}" structure DateRange where start_ : Date finish_ : Date h : start_ <= finish_ def r1 := (DateRange.mk (Date.mk 0) (Date.mk 10) (by decide)) -- If you change the start_ value here to `0`, it works. def r2 := (DateRange.mk (Date.mk 1) (Date.mk 10) (by decide)) #eval min r1.start_ r2.start_
eb32d3ce83a8a43ddb21b15fa5b06b4869bec687
d406927ab5617694ec9ea7001f101b7c9e3d9702
/test/norm_num.lean
7752d113a38aa1d356c5747ee3b919f61a71c653
[ "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
14,265
lean
/- Copyright (c) 2017 Simon Hudon All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Mario Carneiro -/ import tactic.norm_num /-! # Tests for `norm_num` extensions -/ constant real : Type notation `ℝ` := real @[instance] constant real.linear_ordered_ring : linear_ordered_field ℝ constant complex : Type notation `ℂ` := complex @[instance] constant complex.field : field ℂ @[instance] constant complex.char_zero : char_zero ℂ example : 374 + (32 - (2 * 8123) : ℤ) - 61 * 50 = 86 + 32 * 32 - 4 * 5000 ∧ 43 ≤ 74 + (33 : ℤ) := by norm_num example : ¬ (7-2)/(2*3) ≥ (1:ℝ) + 2/(3^2) := by norm_num example : (6:real) + 9 = 15 := by norm_num example : (2:real)/4 + 4 = 3*3/2 := by norm_num example : (((3:real)/4)-12)<6 := by norm_num example : (5:real) ≠ 8 := by norm_num example : (10:real) > 7 := by norm_num example : (2:real) * 2 + 3 = 7 := by norm_num example : (6:real) < 10 := by norm_num example : (7:real)/2 > 3 := by norm_num example : (4:real)⁻¹ < 1 := by norm_num example : ((1:real) / 2)⁻¹ = 2 := by norm_num example : 2 ^ 17 - 1 = 131071 := by {norm_num, tactic.try_for 200 (tactic.result >>= tactic.type_check)} example : (3 : real) ^ (-2 : ℤ) = 1/9 := by norm_num example : (3 : real) ^ (-2 : ℤ) = 1/9 := by norm_num1 example : (-3 : real) ^ (0 : ℤ) = 1 := by norm_num example : (-3 : real) ^ (-1 : ℤ) = -1/3 := by norm_num example : (-3 : real) ^ (2 : ℤ) = 9 := by norm_num example : (1:complex) ≠ 2 := by norm_num example : (1:complex) / 3 ≠ 2 / 7 := by norm_num example : (1:real) ≠ 2 := by norm_num example {α} [semiring α] [char_zero α] : (1:α) ≠ 2 := by norm_num example {α} [ring α] [char_zero α] : (-1:α) ≠ 2 := by norm_num example {α} [division_ring α] [char_zero α] : (-1:α) ≠ 2 := by norm_num example {α} [division_ring α] [char_zero α] : (1:α) / 3 ≠ 2 / 7 := by norm_num example {α} [division_ring α] [char_zero α] : (1:α) / 3 ≠ 0 := by norm_num example : (5 / 2:ℕ) = 2 := by norm_num example : (5 / -2:ℤ) < -1 := by norm_num example : (0 + 1) / 2 < 0 + 1 := by norm_num example : nat.succ (nat.succ (2 ^ 3)) = 10 := by norm_num example : 10 = (-1 : ℤ) % 11 := by norm_num example : (12321 - 2 : ℤ) = 12319 := by norm_num example : (63:ℚ) ≥ 5 := by norm_num example : nat.zero.succ.succ.succ.succ.succ.succ % 4 = 2 := by norm_num1 example (x : ℤ) (h : 1000 + 2000 < x) : 100 * 30 < x := by norm_num at *; try_for 100 {exact h} example : (1103 : ℤ) ≤ (2102 : ℤ) := by norm_num example : (110474 : ℤ) ≤ (210485 : ℤ) := by norm_num example : (11047462383473829263 : ℤ) ≤ (21048574677772382462 : ℤ) := by norm_num example : (210485742382937847263 : ℤ) ≤ (1104857462382937847262 : ℤ) := by norm_num example : (210485987642382937847263 : ℕ) ≤ (11048512347462382937847262 : ℕ) := by norm_num example : (210485987642382937847263 : ℚ) ≤ (11048512347462382937847262 : ℚ) := by norm_num example : (2 * 12868 + 25705) * 11621 ^ 2 ≤ 23235 ^ 2 * 12868 := by norm_num example (x : ℕ) : ℕ := begin let n : ℕ, {apply_normed (2^32 - 71)}, exact n end example (a : ℚ) (h : 3⁻¹ * a = a) : true := begin norm_num at h, guard_hyp h : 1 / 3 * a = a, trivial end example (h : (5 : ℤ) ∣ 2) : false := by norm_num at h example (h : false) : false := by norm_num at h example : true := by norm_num example : true ∧ true := by { split, norm_num, norm_num } example : 10 + 2 = 1 + 11 := by norm_num example : 10 - 1 = 9 := by norm_num example : 12 - 5 = 3 + 4 := by norm_num example : 5 - 20 = 0 := by norm_num example : 0 - 2 = 0 := by norm_num example : 4 - (5 - 10) = 2 + (3 - 1) := by norm_num example : 0 - 0 = 0 := by norm_num example : 100 - 100 = 0 := by norm_num example : 5 * (2 - 3) = 0 := by norm_num example : 10 - 5 * 5 + (7 - 3) * 6 = 27 - 3 := by norm_num def foo : ℕ := 1 @[norm_num] meta def eval_foo : expr → tactic (expr × expr) | `(foo) := pure (`(1:ℕ), `(eq.refl 1)) | _ := tactic.failed example : foo = 1 := by norm_num -- ordered field examples variable {α : Type} variable [linear_ordered_field α] example : (-1 :α) * 1 = -1 := by norm_num example : (-2 :α) * 1 = -2 := by norm_num example : (-2 :α) * -1 = 2 := by norm_num example : (-2 :α) * -2 = 4 := by norm_num example : (1 : α) * 0 = 0 := by norm_num example : ((1 : α) + 1) * 5 = 6 + 4 := by norm_num example : (1 : α) = 0 + 1 := by norm_num example : (1 : α) = 1 + 0 := by norm_num example : (2 : α) = 1 + 1 := by norm_num example : (2 : α) = 0 + 2 := by norm_num example : (3 : α) = 1 + 2 := by norm_num example : (3 : α) = 2 + 1 := by norm_num example : (4 : α) = 3 + 1 := by norm_num example : (4 : α) = 2 + 2 := by norm_num example : (5 : α) = 4 + 1 := by norm_num example : (5 : α) = 3 + 2 := by norm_num example : (5 : α) = 2 + 3 := by norm_num example : (6 : α) = 0 + 6 := by norm_num example : (6 : α) = 3 + 3 := by norm_num example : (6 : α) = 4 + 2 := by norm_num example : (6 : α) = 5 + 1 := by norm_num example : (7 : α) = 4 + 3 := by norm_num example : (7 : α) = 1 + 6 := by norm_num example : (7 : α) = 6 + 1 := by norm_num example : 33 = 5 + (28 : α) := by norm_num example : (12 : α) = 0 + (2 + 3) + 7 := by norm_num example : (105 : α) = 70 + (33 + 2) := by norm_num example : (45000000000 : α) = 23000000000 + 22000000000 := by norm_num example : (0 : α) - 3 = -3 := by norm_num example : (0 : α) - 2 = -2 := by norm_num example : (1 : α) - 3 = -2 := by norm_num example : (1 : α) - 1 = 0 := by norm_num example : (0 : α) - 3 = -3 := by norm_num example : (0 : α) - 3 = -3 := by norm_num example : (12 : α) - 4 - (5 + -2) = 5 := by norm_num example : (12 : α) - 4 - (5 + -2) - 20 = -15 := by norm_num example : (0 : α) * 0 = 0 := by norm_num example : (0 : α) * 1 = 0 := by norm_num example : (0 : α) * 2 = 0 := by norm_num example : (2 : α) * 0 = 0 := by norm_num example : (1 : α) * 0 = 0 := by norm_num example : (1 : α) * 1 = 1 := by norm_num example : (2 : α) * 1 = 2 := by norm_num example : (1 : α) * 2 = 2 := by norm_num example : (2 : α) * 2 = 4 := by norm_num example : (3 : α) * 2 = 6 := by norm_num example : (2 : α) * 3 = 6 := by norm_num example : (4 : α) * 1 = 4 := by norm_num example : (1 : α) * 4 = 4 := by norm_num example : (3 : α) * 3 = 9 := by norm_num example : (3 : α) * 4 = 12 := by norm_num example : (4 : α) * 4 = 16 := by norm_num example : (11 : α) * 2 = 22 := by norm_num example : (15 : α) * 6 = 90 := by norm_num example : (123456 : α) * 123456 = 15241383936 := by norm_num example : (4 : α) / 2 = 2 := by norm_num example : (4 : α) / 1 = 4 := by norm_num example : (4 : α) / 3 = 4 / 3 := by norm_num example : (50 : α) / 5 = 10 := by norm_num example : (1056 : α) / 1 = 1056 := by norm_num example : (6 : α) / 4 = 3/2 := by norm_num example : (0 : α) / 3 = 0 := by norm_num example : (3 : α) / 0 = 0 := by norm_num example : (9 * 9 * 9) * (12 : α) / 27 = 81 * (2 + 2) := by norm_num example : (-2 : α) * 4 / 3 = -8 / 3 := by norm_num example : - (-4 / 3) = 1 / (3 / (4 : α)) := by norm_num -- user command set_option trace.silence_norm_num_if_true true #norm_num 1 = 1 example : 1 = 1 := by norm_num #norm_num 2^4-1 ∣ 2^16-1 example : 2^4-1 ∣ 2^16-1 := by norm_num #norm_num (3 : real) ^ (-2 : ℤ) = 1/9 example : (3 : real) ^ (-2 : ℤ) = 1/9 := by norm_num section norm_num_cmd_variable variables (x y : ℕ) #norm_num bit0 x < bit0 (y + x) ↔ 0 < y example : bit0 x < bit0 (y + x) ↔ 0 < y := by norm_num #norm_num bit0 x < bit0 (y + (2^10%11 - 1) + x) ↔ 0 < y example : bit0 x < bit0 (y + (2^10%11 - 1) + x) ↔ 0 < y := by norm_num #norm_num bit0 x < bit0 (y + (2^10%11 - 1) + x) + 3*2-6 ↔ 0 < y example : bit0 x < bit0 (y + (2^10%11 - 1) + x) + 3*2-6 ↔ 0 < y := by norm_num end norm_num_cmd_variable -- auto gen tests example : ((25 * (1 / 1)) + (30 - 16)) = (39 : α) := by norm_num example : ((19 * (- 2 - 3)) / 6) = (-95/6 : α) := by norm_num example : - (3 * 28) = (-84 : α) := by norm_num example : - - (16 / ((11 / (- - (6 * 19) + 12)) * 21)) = (96/11 : α) := by norm_num example : (- (- 21 + 24) - - (- - (28 + (- 21 / - (16 / ((1 * 26) * ((0 * - 11) + 13))))) * 21)) = (79209/8 : α) := by norm_num example : (27 * (((16 + - (12 + 4)) + (22 - - 19)) - 23)) = (486 : α) := by norm_num example : - (13 * (- 30 / ((7 / 24) + - 7))) = (-9360/161 : α) := by norm_num example : - (0 + 20) = (-20 : α) := by norm_num example : (- 2 - (27 + (((2 / 14) - (7 + 21)) + (16 - - - 14)))) = (-22/7 : α) := by norm_num example : (25 + ((8 - 2) + 16)) = (47 : α) := by norm_num example : (- - 26 / 27) = (26/27 : α) := by norm_num example : ((((16 * (22 / 14)) - 18) / 11) + 30) = (2360/77 : α) := by norm_num example : (((- 28 * 28) / (29 - 24)) * 24) = (-18816/5 : α) := by norm_num example : ((- (18 - ((- - (10 + - 2) - - (23 / 5)) / 5)) - (21 * 22)) - (((20 / - ((((19 + 18) + 15) + 3) + - 22)) + 14) / 17)) = (-394571/825 : α) := by norm_num example : ((3 + 25) - - 4) = (32 : α) := by norm_num example : ((1 - 0) - 22) = (-21 : α) := by norm_num example : (((- (8 / 7) / 14) + 20) + 22) = (2054/49 : α) := by norm_num example : ((21 / 20) - 29) = (-559/20 : α) := by norm_num example : - - 20 = (20 : α) := by norm_num example : (24 - (- 9 / 4)) = (105/4 : α) := by norm_num example : (((7 / ((23 * 19) + (27 * 10))) - ((28 - - 15) * 24)) + (9 / - (10 * - 3))) = (-1042007/1010 : α) := by norm_num example : (26 - (- 29 + (12 / 25))) = (1363/25 : α) := by norm_num example : ((11 * 27) / (4 - 5)) = (-297 : α) := by norm_num example : (24 - (9 + 15)) = (0 : α) := by norm_num example : (- 9 - - 0) = (-9 : α) := by norm_num example : (- 10 / (30 + 10)) = (-1/4 : α) := by norm_num example : (22 - (6 * (28 * - 8))) = (1366 : α) := by norm_num example : ((- - 2 * (9 * - 3)) + (22 / 30)) = (-799/15 : α) := by norm_num example : - (26 / ((3 + 7) / - (27 * (12 / - 16)))) = (-1053/20 : α) := by norm_num example : ((- 29 / 1) + 28) = (-1 : α) := by norm_num example : ((21 * ((10 - (((17 + 28) - - 0) + 20)) + 26)) + ((17 + - 16) * 7)) = (-602 : α) := by norm_num example : (((- 5 - ((24 + - - 8) + 3)) + 20) + - 23) = (-43 : α) := by norm_num example : ((- ((14 - 15) * (14 + 8)) + ((- (18 - 27) - 0) + 12)) - 11) = (32 : α) := by norm_num example : (((15 / 17) * (26 / 27)) + 28) = (4414/153 : α) := by norm_num example : (14 - ((- 16 - 3) * - (20 * 19))) = (-7206 : α) := by norm_num example : (21 - - - (28 - (12 * 11))) = (125 : α) := by norm_num example : ((0 + (7 + (25 + 8))) * - (11 * 27)) = (-11880 : α) := by norm_num example : (19 * - 5) = (-95 : α) := by norm_num example : (29 * - 8) = (-232 : α) := by norm_num example : ((22 / 9) - 29) = (-239/9 : α) := by norm_num example : (3 + (19 / 12)) = (55/12 : α) := by norm_num example : - (13 + 30) = (-43 : α) := by norm_num example : - - - (((21 * - - ((- 25 - (- (30 - 5) / (- 5 - 5))) / (((6 + ((25 * - 13) + 22)) - 3) / 2))) / (- 3 / 10)) * (- 8 - 0)) = (-308/3 : α) := by norm_num example : - (2 * - (- 24 * 22)) = (-1056 : α) := by norm_num example : - - (((28 / - ((- 13 * - 5) / - (((7 - 30) / 16) + 6))) * 0) - 24) = (-24 : α) := by norm_num example : ((13 + 24) - (27 / (21 * 13))) = (3358/91 : α) := by norm_num example : ((3 / - 21) * 25) = (-25/7 : α) := by norm_num example : (17 - (29 - 18)) = (6 : α) := by norm_num example : ((28 / 20) * 15) = (21 : α) := by norm_num example : ((((26 * (- (23 - 13) - 3)) / 20) / (14 - (10 + 20))) / ((16 / 6) / (16 * - (3 / 28)))) = (-1521/2240 : α) := by norm_num example : (46 / (- ((- 17 * 28) - 77) + 87)) = (23/320 : α) := by norm_num example : (73 * - (67 - (74 * - - 11))) = (54531 : α) := by norm_num example : ((8 * (25 / 9)) + 59) = (731/9 : α) := by norm_num example : - ((59 + 85) * - 70) = (10080 : α) := by norm_num example : (66 + (70 * 58)) = (4126 : α) := by norm_num example : (- - 49 * 0) = (0 : α) := by norm_num example : ((- 78 - 69) * 9) = (-1323 : α) := by norm_num example : - - (7 - - (50 * 79)) = (3957 : α) := by norm_num example : - (85 * (((4 * 93) * 19) * - 31)) = (18624180 : α) := by norm_num example : (21 + (- 5 / ((74 * 85) / 45))) = (26373/1258 : α) := by norm_num example : (42 - ((27 + 64) + 26)) = (-75 : α) := by norm_num example : (- ((38 - - 17) + 86) - (74 + 58)) = (-273 : α) := by norm_num example : ((29 * - (75 + - 68)) + (- 41 / 28)) = (-5725/28 : α) := by norm_num example : (- - (40 - 11) - (68 * 86)) = (-5819 : α) := by norm_num example : (6 + ((65 - 14) + - 89)) = (-32 : α) := by norm_num example : (97 * - (29 * 35)) = (-98455 : α) := by norm_num example : - (66 / 33) = (-2 : α) := by norm_num example : - ((94 * 89) + (79 - (23 - (((- 1 / 55) + 95) * (28 - (54 / - - - 22)))))) = (-1369070/121 : α) := by norm_num example : (- 23 + 61) = (38 : α) := by norm_num example : - (93 / 69) = (-31/23 : α) := by norm_num example : (- - ((68 / (39 + (((45 * - (59 - (37 + 35))) / (53 - 75)) - - (100 + - (50 / (- 30 - 59)))))) - (69 - (23 * 30))) / (57 + 17)) = (137496481/16368578 : α) := by norm_num example : (- 19 * - - (75 * - - 41)) = (-58425 : α) := by norm_num example : ((3 / ((- 28 * 45) * (19 + ((- (- 88 - (- (- 1 + 90) + 8)) + 87) * 48)))) + 1) = (1903019/1903020 : α) := by norm_num example : ((- - (28 + 48) / 75) + ((- 59 - 14) - 0)) = (-5399/75 : α) := by norm_num example : (- ((- (((66 - 86) - 36) / 94) - 3) / - - (77 / (56 - - - 79))) + 87) = (312254/3619 : α) := by norm_num example : 2 ^ 13 - 1 = int.of_nat 8191 := by norm_num /-! Test the behaviour of removing one `norm_num` extension tactic. -/ section remove_extension -- turn off the `norm_num` extension which deals with `/`, `%`, `∣` local attribute [-norm_num] norm_num.eval_nat_int_ext example : (5 / 2:ℕ) = 2 := by success_if_fail { solve1 { norm_num } }; refl example : 10 = (-1 : ℤ) % 11 := by success_if_fail { solve1 { norm_num } }; refl example (h : (5 : ℤ) ∣ 2) : false := begin success_if_fail { norm_num at h }, have : (2:ℤ) ≠ 0 := by norm_num, exact this (int.mod_eq_zero_of_dvd h), end example : 2^4-1 ∣ 2^16-1 := begin success_if_fail { solve1 { norm_num } }, use 4369, norm_num, end end remove_extension
05fd11a3d1881ef22f6d1cd2d4c28472acd94fb7
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/lint/frontend.lean
16ee52d97973805aa632edadcd22ae3738a4803c
[]
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,202
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn, Robert Y. Lewis, Gabriel Ebner -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.tactic.lint.basic import Mathlib.PostPort universes l namespace Mathlib /-! # Linter frontend and commands This file defines the linter commands which 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) For a list of default / non-default linters, see the "Linting Commands" user command doc entry. 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 if all checks pass. A silent lint will fail if any test fails. You can append a `+` to any command (e.g. `#lint_mathlib+`) to run a verbose lint that reports the result of each linter, including the successes. 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. ## Tags sanity check, lint, cleanup, command, tactic -/ /-- Verbosity for the linter output. * `low`: only print failing checks, print nothing on success. * `medium`: only print failing checks, print confirmation on success. * `high`: print output of every check. -/ inductive lint_verbosity where | low : lint_verbosity | medium : lint_verbosity | high : lint_verbosity /-- `get_checks slow extra use_only` produces a list of linters. `extras` is a list of names that should resolve to declarations with type `linter`. If `use_only` is true, it only uses the linters in `extra`. Otherwise, it uses all linters in the environment tagged with `@[linter]`. If `slow` is false, it only uses the fast default tests. -/ /-- `lint_core all_decls non_auto_decls checks` applies the linters `checks` to the list of declarations. If `auto_decls` is false for a linter (default) the linter is applied to `non_auto_decls`. If `auto_decls` is true, then it is applied to `all_decls`. The resulting list has one element for each linter, containing the linter as well as a map from declaration name to warning. -/ /-- Sorts a map with declaration keys as names by line number. -/ /-- Formats a linter warning as `#print` command with comment. -/ /-- Formats a map of linter warnings using `print_warning`, sorted by line number. -/ /-- Formats a map of linter warnings grouped by filename with `-- filename` comments. The first `drop_fn_chars` characters are stripped from the filename. -/ /-- Formats the linter results as Lean code with comments and `#print` commands. -/ /-- The common denominator of `#lint[|mathlib|all]`. The different commands have different configurations for `l`, `group_by_filename` and `where_desc`. If `slow` is false, doesn't do the checks that take a lot of time. If `verbose` is false, it will suppress messages from passing checks. By setting `checks` you can customize which checks are performed. Returns a `name_set` containing the names of all declarations that fail any check in `check`, and a `format` object describing the failures. -/ /-- Return the message printed by `#lint` and a `name_set` containing all declarations that fail. -/ /-- Returns the declarations considered by the mathlib linter. -/ /-- Return the message printed by `#lint_mathlib` and a `name_set` containing all declarations that fail. -/ /-- Return the message printed by `#lint_all` and a `name_set` containing all declarations that fail. -/ /-- Parses an optional `only`, followed by a sequence of zero or more identifiers. Prepends `linter.` to each of these identifiers. -/ /-- Parses a "-" or "+", returning `lint_verbosity.low` or `lint_verbosity.high` respectively, or returns `none`. -/ /-- The common denominator of `lint_cmd`, `lint_mathlib_cmd`, `lint_all_cmd` -/ /-- The command `#lint` at the bottom of a file will warn you about some common mistakes in that file. Usage: `#lint`, `#lint linter_1 linter_2`, `#lint only linter_1 linter_2`. `#lint-` will suppress the output if all checks pass. `#lint+` will enable verbose output. Use the command `#list_linters` to see all available linters. -/ /-- The command `#lint_mathlib` checks all of mathlib for certain mistakes. Usage: `#lint_mathlib`, `#lint_mathlib linter_1 linter_2`, `#lint_mathlib only linter_1 linter_2`. `#lint_mathlib-` will suppress the output if all checks pass. `lint_mathlib+` will enable verbose output. Use the command `#list_linters` to see all available linters. -/ /-- The command `#lint_all` checks all imported files for certain mistakes. Usage: `#lint_all`, `#lint_all linter_1 linter_2`, `#lint_all only linter_1 linter_2`. `#lint_all-` will suppress the output if all checks pass. `lint_all+` will enable verbose output. Use the command `#list_linters` to see all available linters. -/ /-- The command `#list_linters` prints a list of all available linters. -/ /-- Invoking the hole command `lint` ("Find common mistakes in current file") will print text that indicates mistakes made in the file above the command. It is equivalent to copying and pasting the output of `#lint`. On large files, it may take some time before the output appears. -/
09b527a08706f8153bfceed9329590cfbe27e6ee
fe84e287c662151bb313504482b218a503b972f3
/src/exercises/misc_exercices_1.lean
872ef1a462aca83e1a54f0cfe7c2ebeeb1b41045
[]
no_license
NeilStrickland/lean_lib
91e163f514b829c42fe75636407138b5c75cba83
6a9563de93748ace509d9db4302db6cd77d8f92c
refs/heads/master
1,653,408,198,261
1,652,996,419,000
1,652,996,419,000
181,006,067
4
1
null
null
null
null
UTF-8
Lean
false
false
12,770
lean
import algebra.big_operators.basic import tactic.interactive tactic.ring tactic.fin_cases /- Exercise 1 Show that for every natural number n, there exists m with m > n Useful ingredients: nat.lt_succ_self; the tactics intro, use, apply -/ lemma exists_greater_a : ∀ (n : ℕ), ∃ (m : ℕ), m > n := begin intro n, use n + 1, apply nat.lt_succ_self, end lemma exists_greater_b (n : ℕ) : ∃ m, m > n := exists.intro (n + 1) n.lt_succ_self /- Exercise 2 Show that if we append a list to itself, then the length doubles. Useful ingredients: list.length_append, two_mul -/ lemma double_length_a {α : Type} (l : list α) : (l ++ l).length = 2 * l.length := begin rw[list.length_append l l],ring end lemma double_length_b {α : Type} (l : list α) : (l ++ l).length = 2 * l.length := (list.length_append l l).trans (two_mul l.length).symm /- Exercise 3 Show that 2 * (0 + 1 + ... + n) = n * (n + 1) Useful ingredients: finset.sum_range, finset.sum_range_succ; the tactics induction, rw and ring -/ lemma range_sum_a (n : ℕ) : 2 * ((finset.range (n + 1)).sum id) = n * (n + 1) := begin induction n with n ih, {refl}, {have e : nat.succ n = n + 1 := rfl, rw[finset.sum_range_succ id (n + 1),mul_add,ih,id,e], ring, } end lemma range_sum_b : ∀ n : ℕ, 2 * ((finset.range (n + 1)).sum id) = n * (n + 1) | 0 := rfl | (n + 1) := begin rw[finset.sum_range_succ id (n + 1),mul_add,range_sum_a n,id], ring, end /- Exercise 4 Show that every natural number can be written in the form 2 * m or 2 * m + 1. This one seems harder than it should be. Useful ingredients: the operations n / 2 and n % 2, the theorems nat.mod_add_div and nat.mod_lt, the fin_cases tactic. For a different approach: the functions nat.bodd and nat.div2 and the theorem nat.bodd_add_div2 -/ lemma even_odd_a : ∀ (n : ℕ), (∃ m, n = 2 * m) ∨ (∃ m, n = 2 * m + 1) | 0 := or.inl ⟨0,rfl⟩ | 1 := or.inr ⟨0,rfl⟩ | (n + 2) := begin rcases even_odd_a n with ⟨m,e⟩ | ⟨m,e⟩; rw[e], {left,use m + 1,ring}, {right,use m + 1,ring}, end lemma even_odd_b (n : ℕ) : (∃ m, n = 2 * m) ∨ (∃ m, n = 2 * m + 1) := begin let r := n % 2, let m := n / 2, have h : n = 2 * m + r := (nat.mod_add_div n 2).symm.trans (add_comm _ _), have r_lt_2 : r < 2 := nat.mod_lt n dec_trivial, let r0 : fin 2 := ⟨r,r_lt_2⟩, fin_cases r0, {left, use m, have hr : r = 0 := fin.veq_of_eq this, rw[hr, add_zero] at h, exact h, },{ right, use m, have hr : r = 1 := fin.veq_of_eq this, rw[hr] at h, exact h, }, end lemma even_odd_c_aux (n m : ℕ) (b : bool) (e : (cond b 1 0) + 2 * m = n) : (∃ m, n = 2 * m) ∨ (∃ m, n = 2 * m + 1) := begin cases b, {left ,exact ⟨m,(e.symm.trans (zero_add _))⟩,}, {right,exact ⟨m,e.symm.trans (add_comm _ _)⟩,}, end lemma even_odd_c (n : ℕ) : (∃ m, n = 2 * m) ∨ (∃ m, n = 2 * m + 1) := even_odd_c_aux n n.div2 (nat.bodd n) (nat.bodd_add_div2 n) /- Exercise 5 Given propositions p and q, show that p → q is equivalent to p → (p → q) Useful ingredients: The tactics split, intros and exact. Alternatively, you can use the tauto tactic, which will do all the work for you. -/ lemma ppq_a (p q : Prop) : (p → q) ↔ (p → p → q) := begin split, {intros hpq hp _,exact hpq hp,}, {intros hppq hp,exact hppq hp hp,} end lemma ppq_b (p q : Prop) : (p → q) ↔ (p → p → q) := ⟨λ hpq hp _,hpq hp,λ hppq hp,hppq hp hp⟩ lemma ppq_c (p q : Prop) : (p → q) ↔ (p → p → q) := by tauto /- Exercise 6 If G is a group where a^2 = 1 for all a, then G is commutative. Proof: for any a and b we have a^2 = 1 and b^2 = 1 and (ab)^2 = 1. Write the last of these as abab = 1, multiply on the left by a and on the right by b to get aababb = ab. Cancel aa = 1 and bb = 1 to get ba = ab. Useful ingredients: pow_two, calc, rw, mul_assoc, one_mul, mul_one -/ lemma elem_ab_a {G : Type*} [group G] (h : ∀ a : G, a ^ 2 = 1) : ∀ a b : G, a * b = b * a := begin intros a b, let ea : a * a = 1 := (pow_two a).symm.trans (h a), let eb : b * b = 1 := (pow_two b).symm.trans (h b), let eab : (a * b) * (a * b) = 1 := (pow_two (a * b)).symm.trans (h (a * b)), exact calc a * b = (a * 1) * b : by rw[mul_one] ... = (a * ((a * b) * (a * b))) * b : by rw[← eab] ... = ((a * (a * b)) * (a * b)) * b : by rw[← (mul_assoc a (a * b) (a * b))] ... = (b * (a * b)) * b : by rw[← (mul_assoc a a b),ea,one_mul b] ... = b * (a * (b * b)) : by rw[mul_assoc b (a * b) b,mul_assoc a b b] ... = b * a : by rw[eb,mul_one a] end /- Exercise 7 Encapsulate Exercise 6 as a typeclass instance -/ class elementary_two_group (G : Type*) extends (group G) := (square_one : ∀ a : G, a ^ 2 = 1) instance elementary_two_group_commutes (G : Type*) [e : elementary_two_group G] : comm_group G := { mul_comm := elem_ab_a e.square_one, ..e } /- Exercise 8 Define an inductive type of nonempty lists (which we will call pos_list). The basic structure will be similar to the definition of arbitrary lists, which can be done like this: inductive list (T : Type) | nil : list | cons : T → T → list However, instead of the constructor `nil`, which constructs the empty list, we should have a constructor called `const`, which constructs a list of length one. We can then give inductive definitions of functions that give the first and last element of a nonempty list, the length (normalised so that lists with one entry count as having length zero), the reverse and so on. -/ inductive pos_list (α : Type) : Type | const : α → pos_list | cons : α → pos_list → pos_list namespace pos_list variable {α : Type} def length : pos_list α → ℕ | (const a) := 0 | (cons a p) := p.length.succ def to_list : pos_list α → list α | (const a) := [a] | (cons a p) := list.cons a (to_list p) def head : pos_list α → α | (const a) := a | (cons a p) := a def foot : pos_list α → α | (const a) := a | (cons a p) := p.foot def append : pos_list α → pos_list α → pos_list α | (const a) q := cons a q | (cons a p) q := cons a (append p q) lemma head_append : ∀ (p q : pos_list α), head (append p q) = head p | (const a) q := rfl | (cons a p) q := rfl lemma foot_append : ∀ (p q : pos_list α), foot (append p q) = foot q | (const a) q := rfl | (cons a p) q := foot_append p q lemma append_assoc : ∀ (p q r : pos_list α), append (append p q) r = append p (append q r) | (const a) q r := rfl | (cons a p) q r := by {dsimp[append],rw[append_assoc p q r],} def reverse : pos_list α → pos_list α | (const a) := const a | (cons a p) := p.reverse.append (const a) lemma head_reverse : ∀ (p : pos_list α), p.reverse.head = p.foot | (const a) := rfl | (cons a p) := by {dsimp[reverse,foot],rw[head_append,head_reverse p],} lemma foot_reverse : ∀ (p : pos_list α), p.reverse.foot = p.head | (const a) := rfl | (cons a p) := by {dsimp[reverse,head],rw[foot_append],refl,} lemma reverse_append : ∀ (p q : pos_list α), reverse (append p q) = append (reverse q) (reverse p) | (const a) q := rfl | (cons a p) q := by {dsimp[append,reverse],rw[reverse_append p q,append_assoc],} lemma reverse_reverse : ∀ (p : pos_list α), p.reverse.reverse = p | (const a) := rfl | (cons a p) := begin dsimp[reverse],rw[reverse_append,reverse_reverse p],refl, end end pos_list /- Exercise 9 Set up some basic theory of graphs. In more detail, suppose we have a type V (to be considered as the vertex set). To make this a graph, we should have a predicate E so that E a b is true if there is an edge from a to b. We are considering simple undirected graphs with no loops, to this relation should be symmetric (ie E a b implies E b a) and irreflexive (so E a a is always false). Then define a type of paths in a graph. We can start by defining a predicate `is_path` on nonempty lists of vertices, which checks whether adjacent vertices in the list are linked by an edge. Using this, we can define paths as a subtype of nonempty lists. Now define a relation `are_connected` on V, which is satisfied by a pair of vertices if there is a path between them. Prove that this is an equivalence relation. -/ class graph (V : Type) : Type := (edge : V → V → Prop) (symm : ∀ {u v : V}, edge u v → edge v u) (asymm : ∀ v : V, ¬ edge v v) namespace graph variables {V : Type} [graph V] def is_path : pos_list V → Prop | (pos_list.const _) := true | (pos_list.cons v p) := (edge v p.head) ∧ is_path p lemma is_path_append (p q : pos_list V) : is_path (pos_list.append p q) ↔ is_path p ∧ edge p.foot q.head ∧ is_path q := begin induction p with a a p ih, {dsimp[is_path,pos_list.foot,pos_list.append,is_path], rw[true_and] }, {dsimp[pos_list.append,pos_list.foot,is_path], rw[pos_list.head_append,ih,and_assoc], } end lemma is_path_reverse : ∀ {p : pos_list V}, is_path p → is_path p.reverse | (pos_list.const a) _ := by {dsimp[pos_list.reverse,is_path],trivial} | (pos_list.cons a p) h := begin dsimp[pos_list.reverse,is_path], dsimp[is_path] at h, rw[is_path_append,pos_list.head,pos_list.foot_reverse,is_path,and_true], exact ⟨is_path_reverse h.right,symm h.left⟩, end variable (V) def path : Type := { p : pos_list V // is_path p } variable {V} def path_between (a b : V) : Type := { p : pos_list V // is_path p ∧ p.head = a ∧ p.foot = b } namespace path def head (p : path V) : V := p.val.head def foot (p : path V) : V := p.val.foot def length (p : path V) : ℕ := p.val.length def cons (v : V) (p : path V) (h : edge v p.head) : path V := ⟨pos_list.cons v p.val,⟨h,p.property⟩⟩ def const (v : V) : path_between v v := ⟨pos_list.const v,⟨trivial,rfl,rfl⟩⟩ def reverse {u v : V} : ∀ (p : path_between u v), path_between v u | ⟨p,⟨p_path,p_head,p_foot⟩⟩ := ⟨p.reverse,⟨is_path_reverse p_path, (pos_list.head_reverse p).trans p_foot, (pos_list.foot_reverse p).trans p_head⟩⟩ def splice {u v w : V} : ∀ (p : path_between u v) (q : path_between v w), path_between u w | ⟨p,⟨p_path,p_head,p_foot⟩⟩ ⟨pos_list.const x,⟨q_path,q_head,q_foot⟩⟩ := ⟨p,begin change x = v at q_head, change x = w at q_foot, replace p_foot := p_foot.trans (q_head.symm.trans q_foot), exact ⟨p_path,p_head,p_foot⟩ end⟩ | ⟨p,⟨p_path,p_head,p_foot⟩⟩ ⟨pos_list.cons x q,⟨q_path,q_head,q_foot⟩⟩ := ⟨pos_list.append p q, begin change x = v at q_head, change q.foot = w at q_foot, rw[pos_list.head_append,p_head,pos_list.foot_append,q_foot], split, {dsimp[is_path] at q_path,rw[is_path_append,p_foot,← q_head], exact ⟨p_path,q_path⟩}, {split;refl,} end ⟩ instance are_connected : setoid V := { r := λ a b, nonempty (path_between a b), iseqv := ⟨ λ a, nonempty.intro (const a), λ a b ⟨p⟩, nonempty.intro (reverse p), λ a b c ⟨p⟩ ⟨q⟩, nonempty.intro (splice p q) ⟩ } end path end graph /- Exercise 10 Construct an equivalence `ℕ ≃ E ⊕ O`, where `E` and `O` are the subtypes of even and odd natural numbers. Here `E ⊕ O` is alternative notation for `sum E O`, which is defined in the core lean library to be the disjoint union of E and O. Equivalences are defined in data.equiv: an equivalence from X to Y is a structure with four members: functions `to_fun : X → Y` and `inv_fun : Y → X`, and proofs that `inv_fun` is both left inverse and right inverse to `to_fun`. -/ def E := {n : ℕ // n.bodd = ff} def O := {n : ℕ // n.bodd = tt} def bool_elim {C : Sort*} {b : bool} : b ≠ ff → b ≠ tt → C := by {intros,cases b;contradiction} def f (n : ℕ) : E ⊕ O := begin by_cases n_even : n.bodd = ff, {exact sum.inl ⟨n,n_even⟩}, by_cases n_odd : n.bodd = tt, {exact sum.inr ⟨n,n_odd⟩}, exact bool_elim n_even n_odd end def g : E ⊕ O → ℕ | (sum.inl n) := n.val | (sum.inr n) := n.val lemma fg : ∀ x : E ⊕ O, f (g x) = x | (sum.inl ⟨n,n_even⟩) := by {dsimp[g,f],rw[dif_pos n_even]} | (sum.inr ⟨n,n_odd⟩) := begin have n_not_even : n.bodd ≠ ff := by {rw[n_odd],intro e,injection e,}, dsimp[g,f], rw[dif_neg n_not_even,dif_pos n_odd], end lemma gf (n : ℕ) : g (f n) = n := begin dsimp[f], by_cases n_even : n.bodd = ff, {rw[dif_pos n_even],refl}, by_cases n_odd : n.bodd = tt, {have n_not_even : n.bodd ≠ ff := by {rw[n_odd],intro e,injection e,}, rw[dif_neg n_not_even,dif_pos n_odd], refl, }, exact bool_elim n_even n_odd end def F : ℕ ≃ (E ⊕ O) := ⟨f,g,gf,fg⟩
589ec0211cfdc3095c2cc8d1df6d24bd386e3d2d
d1a52c3f208fa42c41df8278c3d280f075eb020c
/src/Lean/Data/Lsp/Workspace.lean
0324eb30ac63d45ff2309c5b67dab29818bca147
[ "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
486
lean
/- Copyright (c) 2020 Wojciech Nawrocki. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Wojciech Nawrocki -/ import Lean.Data.Lsp.Basic import Lean.Data.Json namespace Lean namespace Lsp open Json structure WorkspaceFolder where uri : DocumentUri name : String deriving ToJson, FromJson -- TODO(WN): -- WorkspaceFoldersServerCapabilities, -- DidChangeWorkspaceFoldersParams, -- WorkspaceFoldersChangeEvent end Lsp end Lean
22250ac652c5617a3f38207336691db507390f21
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/coe_issue3.lean
3030d7c5008ae50093b366a37e8fc620002543b4
[ "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
364
lean
import data.int data.real open algebra nat int rat real example : (1 : ℤ) * (1 : ℤ) = (1 : ℤ) := sorry example : (1 : ℤ) * 1 = 1 := sorry example : (1 : ℝ) * (1 : ℝ) = (1 : ℝ) := sorry example : (1 : ℝ) * 1 = 1 := sorry variable x : ℝ variable a : ℤ variable n : ℕ example : x + a + n + 1 = 1 := sorry example : x + 1 = 1 + x := sorry
fb1b911eca789ed2dc2b79396722fc55fea7d24d
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/algebra/regular/pow.lean
ee2d02d7819ad0654ad94b2e78c05b122ea859fd
[ "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,310
lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import algebra.hom.iterate import algebra.regular.basic /-! # Regular elements > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. ## Implementation details Group powers and other definitions import a lot of the algebra hierarchy. Lemmas about them are kept separate to be able to provide `is_regular` early in the algebra hierarchy. -/ variables {R : Type*} {a b : R} section monoid variable [monoid R] /-- Any power of a left-regular element is left-regular. -/ lemma is_left_regular.pow (n : ℕ) (rla : is_left_regular a) : is_left_regular (a ^ n) := by simp only [is_left_regular, ← mul_left_iterate, rla.iterate n] /-- Any power of a right-regular element is right-regular. -/ lemma is_right_regular.pow (n : ℕ) (rra : is_right_regular a) : is_right_regular (a ^ n) := by { rw [is_right_regular, ← mul_right_iterate], exact rra.iterate n } /-- Any power of a regular element is regular. -/ lemma is_regular.pow (n : ℕ) (ra : is_regular a) : is_regular (a ^ n) := ⟨is_left_regular.pow n ra.left, is_right_regular.pow n ra.right⟩ /-- An element `a` is left-regular if and only if a positive power of `a` is left-regular. -/ lemma is_left_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_left_regular (a ^ n) ↔ is_left_regular a := begin refine ⟨_, is_left_regular.pow n⟩, rw [← nat.succ_pred_eq_of_pos n0, pow_succ'], exact is_left_regular.of_mul, end /-- An element `a` is right-regular if and only if a positive power of `a` is right-regular. -/ lemma is_right_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_right_regular (a ^ n) ↔ is_right_regular a := begin refine ⟨_, is_right_regular.pow n⟩, rw [← nat.succ_pred_eq_of_pos n0, pow_succ], exact is_right_regular.of_mul, end /-- An element `a` is regular if and only if a positive power of `a` is regular. -/ lemma is_regular.pow_iff {n : ℕ} (n0 : 0 < n) : is_regular (a ^ n) ↔ is_regular a := ⟨λ h, ⟨(is_left_regular.pow_iff n0).mp h.left, (is_right_regular.pow_iff n0).mp h.right⟩, λ h, ⟨is_left_regular.pow n h.left, is_right_regular.pow n h.right⟩⟩ end monoid
280308e729f7634f842b03f5a608bd30c29c5b59
2eab05920d6eeb06665e1a6df77b3157354316ad
/src/logic/basic.lean
31c5949200c388b5f09a272c321c0448907a928d
[ "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
61,355
lean
/- Copyright (c) 2016 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ import tactic.doc_commands import tactic.reserved_notation /-! # Basic logic properties This file is one of the earliest imports in mathlib. ## Implementation notes Theorems that require decidability hypotheses are in the namespace "decidable". Classical versions are in the namespace "classical". In the presence of automation, this whole file may be unnecessary. On the other hand, maybe it is useful for writing automation. -/ open function local attribute [instance, priority 10] classical.prop_decidable section miscellany /- We add the `inline` attribute to optimize VM computation using these declarations. For example, `if p ∧ q then ... else ...` will not evaluate the decidability of `q` if `p` is false. -/ attribute [inline] and.decidable or.decidable decidable.false xor.decidable iff.decidable decidable.true implies.decidable not.decidable ne.decidable bool.decidable_eq decidable.to_bool attribute [simp] cast_eq cast_heq variables {α : Type*} {β : Type*} /-- An identity function with its main argument implicit. This will be printed as `hidden` even if it is applied to a large term, so it can be used for elision, as done in the `elide` and `unelide` tactics. -/ @[reducible] def hidden {α : Sort*} {a : α} := a /-- Ex falso, the nondependent eliminator for the `empty` type. -/ def empty.elim {C : Sort*} : empty → C. instance : subsingleton empty := ⟨λa, a.elim⟩ instance subsingleton.prod {α β : Type*} [subsingleton α] [subsingleton β] : subsingleton (α × β) := ⟨by { intros a b, cases a, cases b, congr, }⟩ instance : decidable_eq empty := λa, a.elim instance sort.inhabited : inhabited (Sort*) := ⟨punit⟩ instance sort.inhabited' : inhabited (default (Sort*)) := ⟨punit.star⟩ instance psum.inhabited_left {α β} [inhabited α] : inhabited (psum α β) := ⟨psum.inl (default _)⟩ instance psum.inhabited_right {α β} [inhabited β] : inhabited (psum α β) := ⟨psum.inr (default _)⟩ @[priority 10] instance decidable_eq_of_subsingleton {α} [subsingleton α] : decidable_eq α | a b := is_true (subsingleton.elim a b) @[simp] lemma eq_iff_true_of_subsingleton {α : Sort*} [subsingleton α] (x y : α) : x = y ↔ true := by cc /-- If all points are equal to a given point `x`, then `α` is a subsingleton. -/ lemma subsingleton_of_forall_eq {α : Sort*} (x : α) (h : ∀ y, y = x) : subsingleton α := ⟨λ a b, (h a).symm ▸ (h b).symm ▸ rfl⟩ lemma subsingleton_iff_forall_eq {α : Sort*} (x : α) : subsingleton α ↔ ∀ y, y = x := ⟨λ h y, @subsingleton.elim _ h y x, subsingleton_of_forall_eq x⟩ -- TODO[gh-6025]: make this an instance once safe to do so lemma subtype.subsingleton (α : Sort*) [subsingleton α] (p : α → Prop) : subsingleton (subtype p) := ⟨λ ⟨x,_⟩ ⟨y,_⟩, have x = y, from subsingleton.elim _ _, by { cases this, refl }⟩ /-- Add an instance to "undo" coercion transitivity into a chain of coercions, because most simp lemmas are stated with respect to simple coercions and will not match when part of a chain. -/ @[simp] theorem coe_coe {α β γ} [has_coe α β] [has_coe_t β γ] (a : α) : (a : γ) = (a : β) := rfl theorem coe_fn_coe_trans {α β γ δ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ δ] (x : α) : @coe_fn α _ _ x = @coe_fn β _ _ x := rfl /-- Non-dependent version of `coe_fn_coe_trans`, helps `rw` figure out the argument. -/ theorem coe_fn_coe_trans' {α β γ} {δ : out_param $ _} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_fun γ (λ _, δ)] (x : α) : @coe_fn α _ _ x = @coe_fn β _ _ x := rfl @[simp] theorem coe_fn_coe_base {α β γ} [has_coe α β] [has_coe_to_fun β γ] (x : α) : @coe_fn α _ _ x = @coe_fn β _ _ x := rfl /-- Non-dependent version of `coe_fn_coe_base`, helps `rw` figure out the argument. -/ theorem coe_fn_coe_base' {α β} {γ : out_param $ _} [has_coe α β] [has_coe_to_fun β (λ _, γ)] (x : α) : @coe_fn α _ _ x = @coe_fn β _ _ x := rfl theorem coe_sort_coe_trans {α β γ δ} [has_coe α β] [has_coe_t_aux β γ] [has_coe_to_sort γ δ] (x : α) : @coe_sort α _ _ x = @coe_sort β _ _ x := rfl /-- Many structures such as bundled morphisms coerce to functions so that you can transparently apply them to arguments. For example, if `e : α ≃ β` and `a : α` then you can write `e a` and this is elaborated as `⇑e a`. This type of coercion is implemented using the `has_coe_to_fun` type class. There is one important consideration: If a type coerces to another type which in turn coerces to a function, then it **must** implement `has_coe_to_fun` directly: ```lean structure sparkling_equiv (α β) extends α ≃ β -- if we add a `has_coe` instance, instance {α β} : has_coe (sparkling_equiv α β) (α ≃ β) := ⟨sparkling_equiv.to_equiv⟩ -- then a `has_coe_to_fun` instance **must** be added as well: instance {α β} : has_coe_to_fun (sparkling_equiv α β) := ⟨λ _, α → β, λ f, f.to_equiv.to_fun⟩ ``` (Rationale: if we do not declare the direct coercion, then `⇑e a` is not in simp-normal form. The lemma `coe_fn_coe_base` will unfold it to `⇑↑e a`. This often causes loops in the simplifier.) -/ library_note "function coercion" @[simp] theorem coe_sort_coe_base {α β γ} [has_coe α β] [has_coe_to_sort β γ] (x : α) : @coe_sort α _ _ x = @coe_sort β _ _ x := rfl /-- `pempty` is the universe-polymorphic analogue of `empty`. -/ @[derive decidable_eq] inductive {u} pempty : Sort u /-- Ex falso, the nondependent eliminator for the `pempty` type. -/ def pempty.elim {C : Sort*} : pempty → C. instance subsingleton_pempty : subsingleton pempty := ⟨λa, a.elim⟩ @[simp] lemma not_nonempty_pempty : ¬ nonempty pempty := assume ⟨h⟩, h.elim @[simp] theorem forall_pempty {P : pempty → Prop} : (∀ x : pempty, P x) ↔ true := ⟨λ h, trivial, λ h x, by cases x⟩ @[simp] theorem exists_pempty {P : pempty → Prop} : (∃ x : pempty, P x) ↔ false := ⟨λ h, by { cases h with w, cases w }, false.elim⟩ lemma congr_arg_heq {α} {β : α → Sort*} (f : ∀ a, β a) : ∀ {a₁ a₂ : α}, a₁ = a₂ → f a₁ == f a₂ | a _ rfl := heq.rfl lemma plift.down_inj {α : Sort*} : ∀ (a b : plift α), a.down = b.down → a = b | ⟨a⟩ ⟨b⟩ rfl := rfl -- missing [symm] attribute for ne in core. attribute [symm] ne.symm lemma ne_comm {α} {a b : α} : a ≠ b ↔ b ≠ a := ⟨ne.symm, ne.symm⟩ @[simp] lemma eq_iff_eq_cancel_left {b c : α} : (∀ {a}, a = b ↔ a = c) ↔ (b = c) := ⟨λ h, by rw [← h], λ h a, by rw h⟩ @[simp] lemma eq_iff_eq_cancel_right {a b : α} : (∀ {c}, a = c ↔ b = c) ↔ (a = b) := ⟨λ h, by rw h, λ h a, by rw h⟩ /-- Wrapper for adding elementary propositions to the type class systems. Warning: this can easily be abused. See the rest of this docstring for details. Certain propositions should not be treated as a class globally, but sometimes it is very convenient to be able to use the type class system in specific circumstances. For example, `zmod p` is a field if and only if `p` is a prime number. In order to be able to find this field instance automatically by type class search, we have to turn `p.prime` into an instance implicit assumption. On the other hand, making `nat.prime` a class would require a major refactoring of the library, and it is questionable whether making `nat.prime` a class is desirable at all. The compromise is to add the assumption `[fact p.prime]` to `zmod.field`. In particular, this class is not intended for turning the type class system into an automated theorem prover for first order logic. -/ class fact (p : Prop) : Prop := (out [] : p) lemma fact.elim {p : Prop} (h : fact p) : p := h.1 lemma fact_iff {p : Prop} : fact p ↔ p := ⟨λ h, h.1, λ h, ⟨h⟩⟩ end miscellany /-! ### Declarations about propositional connectives -/ theorem false_ne_true : false ≠ true | h := h.symm ▸ trivial section propositional variables {a b c d : Prop} /-! ### Declarations about `implies` -/ instance : is_refl Prop iff := ⟨iff.refl⟩ instance : is_trans Prop iff := ⟨λ _ _ _, iff.trans⟩ theorem iff_of_eq (e : a = b) : a ↔ b := e ▸ iff.rfl theorem iff_iff_eq : (a ↔ b) ↔ a = b := ⟨propext, iff_of_eq⟩ @[simp] lemma eq_iff_iff {p q : Prop} : (p = q) ↔ (p ↔ q) := iff_iff_eq.symm @[simp] theorem imp_self : (a → a) ↔ true := iff_true_intro id theorem imp_intro {α β : Prop} (h : α) : β → α := λ _, h theorem imp_false : (a → false) ↔ ¬ a := iff.rfl theorem imp_and_distrib {α} : (α → b ∧ c) ↔ (α → b) ∧ (α → c) := ⟨λ h, ⟨λ ha, (h ha).left, λ ha, (h ha).right⟩, λ h ha, ⟨h.left ha, h.right ha⟩⟩ @[simp] theorem and_imp : (a ∧ b → c) ↔ (a → b → c) := iff.intro (λ h ha hb, h ⟨ha, hb⟩) (λ h ⟨ha, hb⟩, h ha hb) theorem iff_def : (a ↔ b) ↔ (a → b) ∧ (b → a) := iff_iff_implies_and_implies _ _ theorem iff_def' : (a ↔ b) ↔ (b → a) ∧ (a → b) := iff_def.trans and.comm theorem imp_true_iff {α : Sort*} : (α → true) ↔ true := iff_true_intro $ λ_, trivial theorem imp_iff_right (ha : a) : (a → b) ↔ b := ⟨λf, f ha, imp_intro⟩ theorem decidable.imp_iff_right_iff [decidable a] : ((a → b) ↔ b) ↔ (a ∨ b) := ⟨λ H, (decidable.em a).imp_right $ λ ha', H.1 $ λ ha, (ha' ha).elim, λ H, H.elim imp_iff_right $ λ hb, ⟨λ hab, hb, λ _ _, hb⟩⟩ @[simp] theorem imp_iff_right_iff : ((a → b) ↔ b) ↔ (a ∨ b) := decidable.imp_iff_right_iff /-! ### Declarations about `not` -/ /-- Ex falso for negation. From `¬ a` and `a` anything follows. This is the same as `absurd` with the arguments flipped, but it is in the `not` namespace so that projection notation can be used. -/ def not.elim {α : Sort*} (H1 : ¬a) (H2 : a) : α := absurd H2 H1 @[reducible] theorem not.imp {a b : Prop} (H2 : ¬b) (H1 : a → b) : ¬a := mt H1 H2 theorem not_not_of_not_imp : ¬(a → b) → ¬¬a := mt not.elim theorem not_of_not_imp {a : Prop} : ¬(a → b) → ¬b := mt imp_intro theorem dec_em (p : Prop) [decidable p] : p ∨ ¬p := decidable.em p theorem dec_em' (p : Prop) [decidable p] : ¬p ∨ p := (dec_em p).swap theorem em (p : Prop) : p ∨ ¬p := classical.em _ theorem em' (p : Prop) : ¬p ∨ p := (em p).swap theorem or_not {p : Prop} : p ∨ ¬p := em _ section eq_or_ne variables {α : Sort*} (x y : α) theorem decidable.eq_or_ne [decidable (x = y)] : x = y ∨ x ≠ y := dec_em $ x = y theorem decidable.ne_or_eq [decidable (x = y)] : x ≠ y ∨ x = y := dec_em' $ x = y theorem eq_or_ne : x = y ∨ x ≠ y := em $ x = y theorem ne_or_eq : x ≠ y ∨ x = y := em' $ x = y end eq_or_ne theorem by_contradiction {p} : (¬p → false) → p := decidable.by_contradiction -- alias by_contradiction ← by_contra theorem by_contra {p} : (¬p → false) → p := decidable.by_contradiction /-- In most of mathlib, we use the law of excluded middle (LEM) and the axiom of choice (AC) freely. The `decidable` namespace contains versions of lemmas from the root namespace that explicitly attempt to avoid the axiom of choice, usually by adding decidability assumptions on the inputs. You can check if a lemma uses the axiom of choice by using `#print axioms foo` and seeing if `classical.choice` appears in the list. -/ library_note "decidable namespace" /-- As mathlib is primarily classical, if the type signature of a `def` or `lemma` does not require any `decidable` instances to state, it is preferable not to introduce any `decidable` instances that are needed in the proof as arguments, but rather to use the `classical` tactic as needed. In the other direction, when `decidable` instances do appear in the type signature, it is better to use explicitly introduced ones rather than allowing Lean to automatically infer classical ones, as these may cause instance mismatch errors later. -/ library_note "decidable arguments" -- See Note [decidable namespace] protected theorem decidable.not_not [decidable a] : ¬¬a ↔ a := iff.intro decidable.by_contradiction not_not_intro /-- The Double Negation Theorem: `¬ ¬ P` is equivalent to `P`. The left-to-right direction, double negation elimination (DNE), is classically true but not constructively. -/ @[simp] theorem not_not : ¬¬a ↔ a := decidable.not_not theorem of_not_not : ¬¬a → a := by_contra -- See Note [decidable namespace] protected theorem decidable.of_not_imp [decidable a] (h : ¬ (a → b)) : a := decidable.by_contradiction (not_not_of_not_imp h) theorem of_not_imp : ¬ (a → b) → a := decidable.of_not_imp -- See Note [decidable namespace] protected theorem decidable.not_imp_symm [decidable a] (h : ¬a → b) (hb : ¬b) : a := decidable.by_contradiction $ hb ∘ h theorem not.decidable_imp_symm [decidable a] : (¬a → b) → ¬b → a := decidable.not_imp_symm theorem not.imp_symm : (¬a → b) → ¬b → a := not.decidable_imp_symm -- See Note [decidable namespace] protected theorem decidable.not_imp_comm [decidable a] [decidable b] : (¬a → b) ↔ (¬b → a) := ⟨not.decidable_imp_symm, not.decidable_imp_symm⟩ theorem not_imp_comm : (¬a → b) ↔ (¬b → a) := decidable.not_imp_comm @[simp] theorem imp_not_self : (a → ¬a) ↔ ¬a := ⟨λ h ha, h ha ha, λ h _, h⟩ theorem decidable.not_imp_self [decidable a] : (¬a → a) ↔ a := by { have := @imp_not_self (¬a), rwa decidable.not_not at this } @[simp] theorem not_imp_self : (¬a → a) ↔ a := decidable.not_imp_self theorem imp.swap : (a → b → c) ↔ (b → a → c) := ⟨swap, swap⟩ theorem imp_not_comm : (a → ¬b) ↔ (b → ¬a) := imp.swap /-! ### Declarations about `xor` -/ @[simp] theorem xor_true : xor true = not := funext $ λ a, by simp [xor] @[simp] theorem xor_false : xor false = id := funext $ λ a, by simp [xor] theorem xor_comm (a b) : xor a b = xor b a := by simp [xor, and_comm, or_comm] instance : is_commutative Prop xor := ⟨xor_comm⟩ @[simp] theorem xor_self (a : Prop) : xor a a = false := by simp [xor] /-! ### Declarations about `and` -/ theorem and_congr_left (h : c → (a ↔ b)) : a ∧ c ↔ b ∧ c := and.comm.trans $ (and_congr_right h).trans and.comm theorem and_congr_left' (h : a ↔ b) : a ∧ c ↔ b ∧ c := and_congr h iff.rfl theorem and_congr_right' (h : b ↔ c) : a ∧ b ↔ a ∧ c := and_congr iff.rfl h theorem not_and_of_not_left (b : Prop) : ¬a → ¬(a ∧ b) := mt and.left theorem not_and_of_not_right (a : Prop) {b : Prop} : ¬b → ¬(a ∧ b) := mt and.right theorem and.imp_left (h : a → b) : a ∧ c → b ∧ c := and.imp h id theorem and.imp_right (h : a → b) : c ∧ a → c ∧ b := and.imp id h lemma and.right_comm : (a ∧ b) ∧ c ↔ (a ∧ c) ∧ b := by simp only [and.left_comm, and.comm] lemma and_and_and_comm (a b c d : Prop) : (a ∧ b) ∧ c ∧ d ↔ (a ∧ c) ∧ b ∧ d := by rw [←and_assoc, @and.right_comm a, and_assoc] lemma and.rotate : a ∧ b ∧ c ↔ b ∧ c ∧ a := by simp only [and.left_comm, and.comm] theorem and_not_self_iff (a : Prop) : a ∧ ¬ a ↔ false := iff.intro (assume h, (h.right) (h.left)) (assume h, h.elim) theorem not_and_self_iff (a : Prop) : ¬ a ∧ a ↔ false := iff.intro (assume ⟨hna, ha⟩, hna ha) false.elim theorem and_iff_left_of_imp {a b : Prop} (h : a → b) : (a ∧ b) ↔ a := iff.intro and.left (λ ha, ⟨ha, h ha⟩) theorem and_iff_right_of_imp {a b : Prop} (h : b → a) : (a ∧ b) ↔ b := iff.intro and.right (λ hb, ⟨h hb, hb⟩) @[simp] theorem and_iff_left_iff_imp {a b : Prop} : ((a ∧ b) ↔ a) ↔ (a → b) := ⟨λ h ha, (h.2 ha).2, and_iff_left_of_imp⟩ @[simp] theorem and_iff_right_iff_imp {a b : Prop} : ((a ∧ b) ↔ b) ↔ (b → a) := ⟨λ h ha, (h.2 ha).1, and_iff_right_of_imp⟩ @[simp] lemma iff_self_and {p q : Prop} : (p ↔ p ∧ q) ↔ (p → q) := by rw [@iff.comm p, and_iff_left_iff_imp] @[simp] lemma iff_and_self {p q : Prop} : (p ↔ q ∧ p) ↔ (p → q) := by rw [and_comm, iff_self_and] @[simp] lemma and.congr_right_iff : (a ∧ b ↔ a ∧ c) ↔ (a → (b ↔ c)) := ⟨λ h ha, by simp [ha] at h; exact h, and_congr_right⟩ @[simp] lemma and.congr_left_iff : (a ∧ c ↔ b ∧ c) ↔ c → (a ↔ b) := by simp only [and.comm, ← and.congr_right_iff] @[simp] lemma and_self_left : a ∧ a ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1, h.2.2⟩, λ h, ⟨h.1, h.1, h.2⟩⟩ @[simp] lemma and_self_right : (a ∧ b) ∧ b ↔ a ∧ b := ⟨λ h, ⟨h.1.1, h.2⟩, λ h, ⟨⟨h.1, h.2⟩, h.2⟩⟩ /-! ### Declarations about `or` -/ theorem or_congr_left (h : a ↔ b) : a ∨ c ↔ b ∨ c := or_congr h iff.rfl theorem or_congr_right (h : b ↔ c) : a ∨ b ↔ a ∨ c := or_congr iff.rfl h theorem or.right_comm : (a ∨ b) ∨ c ↔ (a ∨ c) ∨ b := by rw [or_assoc, or_assoc, or_comm b] theorem or_of_or_of_imp_of_imp (h₁ : a ∨ b) (h₂ : a → c) (h₃ : b → d) : c ∨ d := or.imp h₂ h₃ h₁ theorem or_of_or_of_imp_left (h₁ : a ∨ c) (h : a → b) : b ∨ c := or.imp_left h h₁ theorem or_of_or_of_imp_right (h₁ : c ∨ a) (h : a → b) : c ∨ b := or.imp_right h h₁ theorem or.elim3 (h : a ∨ b ∨ c) (ha : a → d) (hb : b → d) (hc : c → d) : d := or.elim h ha (assume h₂, or.elim h₂ hb hc) theorem or_imp_distrib : (a ∨ b → c) ↔ (a → c) ∧ (b → c) := ⟨assume h, ⟨assume ha, h (or.inl ha), assume hb, h (or.inr hb)⟩, assume ⟨ha, hb⟩, or.rec ha hb⟩ -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_left [decidable a] : a ∨ b ↔ (¬ a → b) := ⟨or.resolve_left, λ h, dite _ or.inl (or.inr ∘ h)⟩ theorem or_iff_not_imp_left : a ∨ b ↔ (¬ a → b) := decidable.or_iff_not_imp_left -- See Note [decidable namespace] protected theorem decidable.or_iff_not_imp_right [decidable b] : a ∨ b ↔ (¬ b → a) := or.comm.trans decidable.or_iff_not_imp_left theorem or_iff_not_imp_right : a ∨ b ↔ (¬ b → a) := decidable.or_iff_not_imp_right -- See Note [decidable namespace] protected theorem decidable.not_imp_not [decidable a] : (¬ a → ¬ b) ↔ (b → a) := ⟨assume h hb, decidable.by_contradiction $ assume na, h na hb, mt⟩ theorem not_imp_not : (¬ a → ¬ b) ↔ (b → a) := decidable.not_imp_not @[simp] theorem or_iff_left_iff_imp : (a ∨ b ↔ a) ↔ (b → a) := ⟨λ h hb, h.1 (or.inr hb), or_iff_left_of_imp⟩ @[simp] theorem or_iff_right_iff_imp : (a ∨ b ↔ b) ↔ (a → b) := by rw [or_comm, or_iff_left_iff_imp] /-! ### Declarations about distributivity -/ /-- `∧` distributes over `∨` (on the left). -/ theorem and_or_distrib_left : a ∧ (b ∨ c) ↔ (a ∧ b) ∨ (a ∧ c) := ⟨λ ⟨ha, hbc⟩, hbc.imp (and.intro ha) (and.intro ha), or.rec (and.imp_right or.inl) (and.imp_right or.inr)⟩ /-- `∧` distributes over `∨` (on the right). -/ theorem or_and_distrib_right : (a ∨ b) ∧ c ↔ (a ∧ c) ∨ (b ∧ c) := (and.comm.trans and_or_distrib_left).trans (or_congr and.comm and.comm) /-- `∨` distributes over `∧` (on the left). -/ theorem or_and_distrib_left : a ∨ (b ∧ c) ↔ (a ∨ b) ∧ (a ∨ c) := ⟨or.rec (λha, and.intro (or.inl ha) (or.inl ha)) (and.imp or.inr or.inr), and.rec $ or.rec (imp_intro ∘ or.inl) (or.imp_right ∘ and.intro)⟩ /-- `∨` distributes over `∧` (on the right). -/ theorem and_or_distrib_right : (a ∧ b) ∨ c ↔ (a ∨ c) ∧ (b ∨ c) := (or.comm.trans or_and_distrib_left).trans (and_congr or.comm or.comm) @[simp] lemma or_self_left : a ∨ a ∨ b ↔ a ∨ b := ⟨λ h, h.elim or.inl id, λ h, h.elim or.inl (or.inr ∘ or.inr)⟩ @[simp] lemma or_self_right : (a ∨ b) ∨ b ↔ a ∨ b := ⟨λ h, h.elim id or.inr, λ h, h.elim (or.inl ∘ or.inl) or.inr⟩ /-! Declarations about `iff` -/ theorem iff_of_true (ha : a) (hb : b) : a ↔ b := ⟨λ_, hb, λ _, ha⟩ theorem iff_of_false (ha : ¬a) (hb : ¬b) : a ↔ b := ⟨ha.elim, hb.elim⟩ theorem iff_true_left (ha : a) : (a ↔ b) ↔ b := ⟨λ h, h.1 ha, iff_of_true ha⟩ theorem iff_true_right (ha : a) : (b ↔ a) ↔ b := iff.comm.trans (iff_true_left ha) theorem iff_false_left (ha : ¬a) : (a ↔ b) ↔ ¬b := ⟨λ h, mt h.2 ha, iff_of_false ha⟩ theorem iff_false_right (ha : ¬a) : (b ↔ a) ↔ ¬b := iff.comm.trans (iff_false_left ha) @[simp] lemma iff_mpr_iff_true_intro {P : Prop} (h : P) : iff.mpr (iff_true_intro h) true.intro = h := rfl -- See Note [decidable namespace] protected theorem decidable.not_or_of_imp [decidable a] (h : a → b) : ¬ a ∨ b := if ha : a then or.inr (h ha) else or.inl ha theorem not_or_of_imp : (a → b) → ¬ a ∨ b := decidable.not_or_of_imp -- See Note [decidable namespace] protected theorem decidable.imp_iff_not_or [decidable a] : (a → b) ↔ (¬ a ∨ b) := ⟨decidable.not_or_of_imp, or.neg_resolve_left⟩ theorem imp_iff_not_or : (a → b) ↔ (¬ a ∨ b) := decidable.imp_iff_not_or -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib [decidable a] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by simp [decidable.imp_iff_not_or, or.comm, or.left_comm] theorem imp_or_distrib : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib -- See Note [decidable namespace] protected theorem decidable.imp_or_distrib' [decidable b] : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := by by_cases b; simp [h, or_iff_right_of_imp ((∘) false.elim)] theorem imp_or_distrib' : (a → b ∨ c) ↔ (a → b) ∨ (a → c) := decidable.imp_or_distrib' theorem not_imp_of_and_not : a ∧ ¬ b → ¬ (a → b) | ⟨ha, hb⟩ h := hb $ h ha -- See Note [decidable namespace] protected theorem decidable.not_imp [decidable a] : ¬(a → b) ↔ a ∧ ¬b := ⟨λ h, ⟨decidable.of_not_imp h, not_of_not_imp h⟩, not_imp_of_and_not⟩ theorem not_imp : ¬(a → b) ↔ a ∧ ¬b := decidable.not_imp -- for monotonicity lemma imp_imp_imp (h₀ : c → a) (h₁ : b → d) : (a → b) → (c → d) := assume (h₂ : a → b), h₁ ∘ h₂ ∘ h₀ -- See Note [decidable namespace] protected theorem decidable.peirce (a b : Prop) [decidable a] : ((a → b) → a) → a := if ha : a then λ h, ha else λ h, h ha.elim theorem peirce (a b : Prop) : ((a → b) → a) → a := decidable.peirce _ _ theorem peirce' {a : Prop} (H : ∀ b : Prop, (a → b) → a) : a := H _ id -- See Note [decidable namespace] protected theorem decidable.not_iff_not [decidable a] [decidable b] : (¬ a ↔ ¬ b) ↔ (a ↔ b) := by rw [@iff_def (¬ a), @iff_def' a]; exact and_congr decidable.not_imp_not decidable.not_imp_not theorem not_iff_not : (¬ a ↔ ¬ b) ↔ (a ↔ b) := decidable.not_iff_not -- See Note [decidable namespace] protected theorem decidable.not_iff_comm [decidable a] [decidable b] : (¬ a ↔ b) ↔ (¬ b ↔ a) := by rw [@iff_def (¬ a), @iff_def (¬ b)]; exact and_congr decidable.not_imp_comm imp_not_comm theorem not_iff_comm : (¬ a ↔ b) ↔ (¬ b ↔ a) := decidable.not_iff_comm -- See Note [decidable namespace] protected theorem decidable.not_iff : ∀ [decidable b], ¬ (a ↔ b) ↔ (¬ a ↔ b) := by intro h; cases h; simp only [h, iff_true, iff_false] theorem not_iff : ¬ (a ↔ b) ↔ (¬ a ↔ b) := decidable.not_iff -- See Note [decidable namespace] protected theorem decidable.iff_not_comm [decidable a] [decidable b] : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := by rw [@iff_def a, @iff_def b]; exact and_congr imp_not_comm decidable.not_imp_comm theorem iff_not_comm : (a ↔ ¬ b) ↔ (b ↔ ¬ a) := decidable.iff_not_comm -- See Note [decidable namespace] protected theorem decidable.iff_iff_and_or_not_and_not [decidable b] : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := by { split; intro h, { rw h; by_cases b; [left,right]; split; assumption }, { cases h with h h; cases h; split; intro; { contradiction <|> assumption } } } theorem iff_iff_and_or_not_and_not : (a ↔ b) ↔ (a ∧ b) ∨ (¬ a ∧ ¬ b) := decidable.iff_iff_and_or_not_and_not lemma decidable.iff_iff_not_or_and_or_not [decidable a] [decidable b] : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := begin rw [iff_iff_implies_and_implies a b], simp only [decidable.imp_iff_not_or, or.comm] end lemma iff_iff_not_or_and_or_not : (a ↔ b) ↔ ((¬a ∨ b) ∧ (a ∨ ¬b)) := decidable.iff_iff_not_or_and_or_not -- See Note [decidable namespace] protected theorem decidable.not_and_not_right [decidable b] : ¬(a ∧ ¬b) ↔ (a → b) := ⟨λ h ha, h.decidable_imp_symm $ and.intro ha, λ h ⟨ha, hb⟩, hb $ h ha⟩ theorem not_and_not_right : ¬(a ∧ ¬b) ↔ (a → b) := decidable.not_and_not_right /-- Transfer decidability of `a` to decidability of `b`, if the propositions are equivalent. **Important**: this function should be used instead of `rw` on `decidable b`, because the kernel will get stuck reducing the usage of `propext` otherwise, and `dec_trivial` will not work. -/ @[inline] def decidable_of_iff (a : Prop) (h : a ↔ b) [D : decidable a] : decidable b := decidable_of_decidable_of_iff D h /-- Transfer decidability of `b` to decidability of `a`, if the propositions are equivalent. This is the same as `decidable_of_iff` but the iff is flipped. -/ @[inline] def decidable_of_iff' (b : Prop) (h : a ↔ b) [D : decidable b] : decidable a := decidable_of_decidable_of_iff D h.symm /-- Prove that `a` is decidable by constructing a boolean `b` and a proof that `b ↔ a`. (This is sometimes taken as an alternate definition of decidability.) -/ def decidable_of_bool : ∀ (b : bool) (h : b ↔ a), decidable a | tt h := is_true (h.1 rfl) | ff h := is_false (mt h.2 bool.ff_ne_tt) /-! ### De Morgan's laws -/ theorem not_and_of_not_or_not (h : ¬ a ∨ ¬ b) : ¬ (a ∧ b) | ⟨ha, hb⟩ := or.elim h (absurd ha) (absurd hb) -- See Note [decidable namespace] protected theorem decidable.not_and_distrib [decidable a] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if ha : a then or.inr (λ hb, h ⟨ha, hb⟩) else or.inl ha, not_and_of_not_or_not⟩ -- See Note [decidable namespace] protected theorem decidable.not_and_distrib' [decidable b] : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := ⟨λ h, if hb : b then or.inl (λ ha, h ⟨ha, hb⟩) else or.inr hb, not_and_of_not_or_not⟩ /-- One of de Morgan's laws: the negation of a conjunction is logically equivalent to the disjunction of the negations. -/ theorem not_and_distrib : ¬ (a ∧ b) ↔ ¬a ∨ ¬b := decidable.not_and_distrib @[simp] theorem not_and : ¬ (a ∧ b) ↔ (a → ¬ b) := and_imp theorem not_and' : ¬ (a ∧ b) ↔ b → ¬a := not_and.trans imp_not_comm /-- One of de Morgan's laws: the negation of a disjunction is logically equivalent to the conjunction of the negations. -/ theorem not_or_distrib : ¬ (a ∨ b) ↔ ¬ a ∧ ¬ b := ⟨λ h, ⟨λ ha, h (or.inl ha), λ hb, h (or.inr hb)⟩, λ ⟨h₁, h₂⟩ h, or.elim h h₁ h₂⟩ -- See Note [decidable namespace] protected theorem decidable.or_iff_not_and_not [decidable a] [decidable b] : a ∨ b ↔ ¬ (¬a ∧ ¬b) := by rw [← not_or_distrib, decidable.not_not] theorem or_iff_not_and_not : a ∨ b ↔ ¬ (¬a ∧ ¬b) := decidable.or_iff_not_and_not -- See Note [decidable namespace] protected theorem decidable.and_iff_not_or_not [decidable a] [decidable b] : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := by rw [← decidable.not_and_distrib, decidable.not_not] theorem and_iff_not_or_not : a ∧ b ↔ ¬ (¬ a ∨ ¬ b) := decidable.and_iff_not_or_not end propositional /-! ### Declarations about equality -/ section equality variables {α : Sort*} {a b : α} @[simp] theorem heq_iff_eq : a == b ↔ a = b := ⟨eq_of_heq, heq_of_eq⟩ theorem proof_irrel_heq {p q : Prop} (hp : p) (hq : q) : hp == hq := have p = q, from propext ⟨λ _, hq, λ _, hp⟩, by subst q; refl theorem ne_of_mem_of_not_mem {α β} [has_mem α β] {s : β} {a b : α} (h : a ∈ s) : b ∉ s → a ≠ b := mt $ λ e, e ▸ h lemma ne_of_apply_ne {α β : Sort*} (f : α → β) {x y : α} (h : f x ≠ f y) : x ≠ y := λ (w : x = y), h (congr_arg f w) theorem eq_equivalence : equivalence (@eq α) := ⟨eq.refl, @eq.symm _, @eq.trans _⟩ /-- Transport through trivial families is the identity. -/ @[simp] lemma eq_rec_constant {α : Sort*} {a a' : α} {β : Sort*} (y : β) (h : a = a') : (@eq.rec α a (λ a, β) y a' h) = y := by { cases h, refl, } @[simp] lemma eq_mp_eq_cast {α β : Sort*} (h : α = β) : eq.mp h = cast h := rfl @[simp] lemma eq_mpr_eq_cast {α β : Sort*} (h : α = β) : eq.mpr h = cast h.symm := rfl @[simp] lemma cast_cast : ∀ {α β γ : Sort*} (ha : α = β) (hb : β = γ) (a : α), cast hb (cast ha a) = cast (ha.trans hb) a | _ _ _ rfl rfl a := rfl @[simp] lemma congr_refl_left {α β : Sort*} (f : α → β) {a b : α} (h : a = b) : congr (eq.refl f) h = congr_arg f h := rfl @[simp] lemma congr_refl_right {α β : Sort*} {f g : α → β} (h : f = g) (a : α) : congr h (eq.refl a) = congr_fun h a := rfl @[simp] lemma congr_arg_refl {α β : Sort*} (f : α → β) (a : α) : congr_arg f (eq.refl a) = eq.refl (f a) := rfl @[simp] lemma congr_fun_rfl {α β : Sort*} (f : α → β) (a : α) : congr_fun (eq.refl f) a = eq.refl (f a) := rfl @[simp] lemma congr_fun_congr_arg {α β γ : Sort*} (f : α → β → γ) {a a' : α} (p : a = a') (b : β) : congr_fun (congr_arg f p) b = congr_arg (λ a, f a b) p := rfl lemma heq_of_cast_eq : ∀ {α β : Sort*} {a : α} {a' : β} (e : α = β) (h₂ : cast e a = a'), a == a' | α ._ a a' rfl h := eq.rec_on h (heq.refl _) lemma cast_eq_iff_heq {α β : Sort*} {a : α} {a' : β} {e : α = β} : cast e a = a' ↔ a == a' := ⟨heq_of_cast_eq _, λ h, by cases h; refl⟩ lemma rec_heq_of_heq {β} {C : α → Sort*} {x : C a} {y : β} (eq : a = b) (h : x == y) : @eq.rec α a C x b eq == y := by subst eq; exact h protected lemma eq.congr {x₁ x₂ y₁ y₂ : α} (h₁ : x₁ = y₁) (h₂ : x₂ = y₂) : (x₁ = x₂) ↔ (y₁ = y₂) := by { subst h₁, subst h₂ } lemma eq.congr_left {x y z : α} (h : x = y) : x = z ↔ y = z := by rw [h] lemma eq.congr_right {x y z : α} (h : x = y) : z = x ↔ z = y := by rw [h] lemma congr_arg2 {α β γ : Type*} (f : α → β → γ) {x x' : α} {y y' : β} (hx : x = x') (hy : y = y') : f x y = f x' y' := by { subst hx, subst hy } end equality /-! ### Declarations about quantifiers -/ section quantifiers variables {α : Sort*} {β : Sort*} {p q : α → Prop} {b : Prop} lemma forall_imp (h : ∀ a, p a → q a) : (∀ a, p a) → ∀ a, q a := λ h' a, h a (h' a) lemma forall₂_congr {p q : α → β → Prop} (h : ∀ a b, p a b ↔ q a b) : (∀ a b, p a b) ↔ (∀ a b, q a b) := forall_congr (λ a, forall_congr (h a)) lemma forall₃_congr {γ : Sort*} {p q : α → β → γ → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∀ a b c, p a b c) ↔ (∀ a b c, q a b c) := forall_congr (λ a, forall₂_congr (h a)) lemma forall₄_congr {γ δ : Sort*} {p q : α → β → γ → δ → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∀ a b c d, p a b c d) ↔ (∀ a b c d, q a b c d) := forall_congr (λ a, forall₃_congr (h a)) lemma Exists.imp (h : ∀ a, (p a → q a)) (p : ∃ a, p a) : ∃ a, q a := exists_imp_exists h p lemma exists_imp_exists' {p : α → Prop} {q : β → Prop} (f : α → β) (hpq : ∀ a, p a → q (f a)) (hp : ∃ a, p a) : ∃ b, q b := exists.elim hp (λ a hp', ⟨_, hpq _ hp'⟩) lemma exists₂_congr {p q : α → β → Prop} (h : ∀ a b, p a b ↔ q a b) : (∃ a b, p a b) ↔ (∃ a b, q a b) := exists_congr (λ a, exists_congr (h a)) lemma exists₃_congr {γ : Sort*} {p q : α → β → γ → Prop} (h : ∀ a b c, p a b c ↔ q a b c) : (∃ a b c, p a b c) ↔ (∃ a b c, q a b c) := exists_congr (λ a, exists₂_congr (h a)) lemma exists₄_congr {γ δ : Sort*} {p q : α → β → γ → δ → Prop} (h : ∀ a b c d, p a b c d ↔ q a b c d) : (∃ a b c d, p a b c d) ↔ (∃ a b c d, q a b c d) := exists_congr (λ a, exists₃_congr (h a)) theorem forall_swap {p : α → β → Prop} : (∀ x y, p x y) ↔ ∀ y x, p x y := ⟨swap, swap⟩ theorem exists_swap {p : α → β → Prop} : (∃ x y, p x y) ↔ ∃ y x, p x y := ⟨λ ⟨x, y, h⟩, ⟨y, x, h⟩, λ ⟨y, x, h⟩, ⟨x, y, h⟩⟩ @[simp] theorem forall_exists_index {q : (∃ x, p x) → Prop} : (∀ h, q h) ↔ ∀ x (h : p x), q ⟨x, h⟩ := ⟨λ h x hpx, h ⟨x, hpx⟩, λ h ⟨x, hpx⟩, h x hpx⟩ theorem exists_imp_distrib : ((∃ x, p x) → b) ↔ ∀ x, p x → b := forall_exists_index /-- Extract an element from a existential statement, using `classical.some`. -/ -- This enables projection notation. @[reducible] noncomputable def Exists.some {p : α → Prop} (P : ∃ a, p a) : α := classical.some P /-- Show that an element extracted from `P : ∃ a, p a` using `P.some` satisfies `p`. -/ lemma Exists.some_spec {p : α → Prop} (P : ∃ a, p a) : p (P.some) := classical.some_spec P --theorem forall_not_of_not_exists (h : ¬ ∃ x, p x) : ∀ x, ¬ p x := --forall_imp_of_exists_imp h theorem not_exists_of_forall_not (h : ∀ x, ¬ p x) : ¬ ∃ x, p x := exists_imp_distrib.2 h @[simp] theorem not_exists : (¬ ∃ x, p x) ↔ ∀ x, ¬ p x := exists_imp_distrib theorem not_forall_of_exists_not : (∃ x, ¬ p x) → ¬ ∀ x, p x | ⟨x, hn⟩ h := hn (h x) -- See Note [decidable namespace] protected theorem decidable.not_forall {p : α → Prop} [decidable (∃ x, ¬ p x)] [∀ x, decidable (p x)] : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := ⟨not.decidable_imp_symm $ λ nx x, nx.decidable_imp_symm $ λ h, ⟨x, h⟩, not_forall_of_exists_not⟩ @[simp] theorem not_forall {p : α → Prop} : (¬ ∀ x, p x) ↔ ∃ x, ¬ p x := decidable.not_forall -- See Note [decidable namespace] protected theorem decidable.not_forall_not [decidable (∃ x, p x)] : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := (@decidable.not_iff_comm _ _ _ (decidable_of_iff (¬ ∃ x, p x) not_exists)).1 not_exists theorem not_forall_not : (¬ ∀ x, ¬ p x) ↔ ∃ x, p x := decidable.not_forall_not -- See Note [decidable namespace] protected theorem decidable.not_exists_not [∀ x, decidable (p x)] : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := by simp [decidable.not_not] @[simp] theorem not_exists_not : (¬ ∃ x, ¬ p x) ↔ ∀ x, p x := decidable.not_exists_not theorem forall_imp_iff_exists_imp [ha : nonempty α] : ((∀ x, p x) → b) ↔ ∃ x, p x → b := let ⟨a⟩ := ha in ⟨λ h, not_forall_not.1 $ λ h', classical.by_cases (λ hb : b, h' a $ λ _, hb) (λ hb, hb $ h $ λ x, (not_imp.1 (h' x)).1), λ ⟨x, hx⟩ h, hx (h x)⟩ -- TODO: duplicate of a lemma in core theorem forall_true_iff : (α → true) ↔ true := implies_true_iff α -- Unfortunately this causes simp to loop sometimes, so we -- add the 2 and 3 cases as simp lemmas instead theorem forall_true_iff' (h : ∀ a, p a ↔ true) : (∀ a, p a) ↔ true := iff_true_intro (λ _, of_iff_true (h _)) @[simp] theorem forall_2_true_iff {β : α → Sort*} : (∀ a, β a → true) ↔ true := forall_true_iff' $ λ _, forall_true_iff @[simp] theorem forall_3_true_iff {β : α → Sort*} {γ : Π a, β a → Sort*} : (∀ a (b : β a), γ a b → true) ↔ true := forall_true_iff' $ λ _, forall_2_true_iff lemma exists_unique.exists {α : Sort*} {p : α → Prop} (h : ∃! x, p x) : ∃ x, p x := exists.elim h (λ x hx, ⟨x, and.left hx⟩) @[simp] lemma exists_unique_iff_exists {α : Sort*} [subsingleton α] {p : α → Prop} : (∃! x, p x) ↔ ∃ x, p x := ⟨λ h, h.exists, Exists.imp $ λ x hx, ⟨hx, λ y _, subsingleton.elim y x⟩⟩ @[simp] theorem forall_const (α : Sort*) [i : nonempty α] : (α → b) ↔ b := ⟨i.elim, λ hb x, hb⟩ @[simp] theorem exists_const (α : Sort*) [i : nonempty α] : (∃ x : α, b) ↔ b := ⟨λ ⟨x, h⟩, h, i.elim exists.intro⟩ theorem exists_unique_const (α : Sort*) [i : nonempty α] [subsingleton α] : (∃! x : α, b) ↔ b := by simp theorem forall_and_distrib : (∀ x, p x ∧ q x) ↔ (∀ x, p x) ∧ (∀ x, q x) := ⟨λ h, ⟨λ x, (h x).left, λ x, (h x).right⟩, λ ⟨h₁, h₂⟩ x, ⟨h₁ x, h₂ x⟩⟩ theorem exists_or_distrib : (∃ x, p x ∨ q x) ↔ (∃ x, p x) ∨ (∃ x, q x) := ⟨λ ⟨x, hpq⟩, hpq.elim (λ hpx, or.inl ⟨x, hpx⟩) (λ hqx, or.inr ⟨x, hqx⟩), λ hepq, hepq.elim (λ ⟨x, hpx⟩, ⟨x, or.inl hpx⟩) (λ ⟨x, hqx⟩, ⟨x, or.inr hqx⟩)⟩ @[simp] theorem exists_and_distrib_left {q : Prop} {p : α → Prop} : (∃x, q ∧ p x) ↔ q ∧ (∃x, p x) := ⟨λ ⟨x, hq, hp⟩, ⟨hq, x, hp⟩, λ ⟨hq, x, hp⟩, ⟨x, hq, hp⟩⟩ @[simp] theorem exists_and_distrib_right {q : Prop} {p : α → Prop} : (∃x, p x ∧ q) ↔ (∃x, p x) ∧ q := by simp [and_comm] @[simp] theorem forall_eq {a' : α} : (∀a, a = a' → p a) ↔ p a' := ⟨λ h, h a' rfl, λ h a e, e.symm ▸ h⟩ @[simp] theorem forall_eq' {a' : α} : (∀a, a' = a → p a) ↔ p a' := by simp [@eq_comm _ a'] theorem and_forall_ne (a : α) : (p a ∧ ∀ b ≠ a, p b) ↔ ∀ b, p b := by simp only [← @forall_eq _ p a, ← forall_and_distrib, ← or_imp_distrib, classical.em, forall_const] -- this lemma is needed to simplify the output of `list.mem_cons_iff` @[simp] theorem forall_eq_or_imp {a' : α} : (∀ a, a = a' ∨ q a → p a) ↔ p a' ∧ ∀ a, q a → p a := by simp only [or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem exists_eq {a' : α} : ∃ a, a = a' := ⟨_, rfl⟩ @[simp] theorem exists_eq' {a' : α} : ∃ a, a' = a := ⟨_, rfl⟩ @[simp] theorem exists_eq_left {a' : α} : (∃ a, a = a' ∧ p a) ↔ p a' := ⟨λ ⟨a, e, h⟩, e ▸ h, λ h, ⟨_, rfl, h⟩⟩ @[simp] theorem exists_eq_right {a' : α} : (∃ a, p a ∧ a = a') ↔ p a' := (exists_congr $ by exact λ a, and.comm).trans exists_eq_left @[simp] theorem exists_eq_right_right {a' : α} : (∃ (a : α), p a ∧ b ∧ a = a') ↔ p a' ∧ b := ⟨λ ⟨_, hp, hq, rfl⟩, ⟨hp, hq⟩, λ ⟨hp, hq⟩, ⟨a', hp, hq, rfl⟩⟩ @[simp] theorem exists_eq_right_right' {a' : α} : (∃ (a : α), p a ∧ b ∧ a' = a) ↔ p a' ∧ b := ⟨λ ⟨_, hp, hq, rfl⟩, ⟨hp, hq⟩, λ ⟨hp, hq⟩, ⟨a', hp, hq, rfl⟩⟩ @[simp] theorem exists_apply_eq_apply {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a = f a' := ⟨a', rfl⟩ @[simp] theorem exists_apply_eq_apply' {α β : Type*} (f : α → β) (a' : α) : ∃ a, f a' = f a := ⟨a', rfl⟩ @[simp] theorem exists_exists_and_eq_and {f : α → β} {p : α → Prop} {q : β → Prop} : (∃ b, (∃ a, p a ∧ f a = b) ∧ q b) ↔ ∃ a, p a ∧ q (f a) := ⟨λ ⟨b, ⟨a, ha, hab⟩, hb⟩, ⟨a, ha, hab.symm ▸ hb⟩, λ ⟨a, hp, hq⟩, ⟨f a, ⟨a, hp, rfl⟩, hq⟩⟩ @[simp] theorem exists_exists_eq_and {f : α → β} {p : β → Prop} : (∃ b, (∃ a, f a = b) ∧ p b) ↔ ∃ a, p (f a) := ⟨λ ⟨b, ⟨a, ha⟩, hb⟩, ⟨a, ha.symm ▸ hb⟩, λ ⟨a, ha⟩, ⟨f a, ⟨a, rfl⟩, ha⟩⟩ @[simp] lemma exists_or_eq_left (y : α) (p : α → Prop) : ∃ (x : α), x = y ∨ p x := ⟨y, or.inl rfl⟩ @[simp] lemma exists_or_eq_right (y : α) (p : α → Prop) : ∃ (x : α), p x ∨ x = y := ⟨y, or.inr rfl⟩ @[simp] lemma exists_or_eq_left' (y : α) (p : α → Prop) : ∃ (x : α), y = x ∨ p x := ⟨y, or.inl rfl⟩ @[simp] lemma exists_or_eq_right' (y : α) (p : α → Prop) : ∃ (x : α), p x ∨ y = x := ⟨y, or.inr rfl⟩ @[simp] theorem forall_apply_eq_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, f a = b → p b) ↔ (∀ a, p (f a)) := ⟨λ h a, h a (f a) rfl, λ h a b hab, hab ▸ h a⟩ @[simp] theorem forall_apply_eq_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, f a = b → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_eq_apply_imp_iff {f : α → β} {p : β → Prop} : (∀ a, ∀ b, b = f a → p b) ↔ (∀ a, p (f a)) := by simp [@eq_comm _ _ (f _)] @[simp] theorem forall_eq_apply_imp_iff' {f : α → β} {p : β → Prop} : (∀ b, ∀ a, b = f a → p b) ↔ (∀ a, p (f a)) := by { rw forall_swap, simp } @[simp] theorem forall_apply_eq_imp_iff₂ {f : α → β} {p : α → Prop} {q : β → Prop} : (∀ b, ∀ a, p a → f a = b → q b) ↔ ∀ a, p a → q (f a) := ⟨λ h a ha, h (f a) a ha rfl, λ h b a ha hb, hb ▸ h a ha⟩ @[simp] theorem exists_eq_left' {a' : α} : (∃ a, a' = a ∧ p a) ↔ p a' := by simp [@eq_comm _ a'] @[simp] theorem exists_eq_right' {a' : α} : (∃ a, p a ∧ a' = a) ↔ p a' := by simp [@eq_comm _ a'] theorem exists_comm {p : α → β → Prop} : (∃ a b, p a b) ↔ ∃ b a, p a b := ⟨λ ⟨a, b, h⟩, ⟨b, a, h⟩, λ ⟨b, a, h⟩, ⟨a, b, h⟩⟩ theorem and.exists {p q : Prop} {f : p ∧ q → Prop} : (∃ h, f h) ↔ ∃ hp hq, f ⟨hp, hq⟩ := ⟨λ ⟨h, H⟩, ⟨h.1, h.2, H⟩, λ ⟨hp, hq, H⟩, ⟨⟨hp, hq⟩, H⟩⟩ theorem forall_or_of_or_forall (h : b ∨ ∀x, p x) (x) : b ∨ p x := h.imp_right $ λ h₂, h₂ x -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_left {q : Prop} {p : α → Prop} [decidable q] : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := ⟨λ h, if hq : q then or.inl hq else or.inr $ λ x, (h x).resolve_left hq, forall_or_of_or_forall⟩ theorem forall_or_distrib_left {q : Prop} {p : α → Prop} : (∀x, q ∨ p x) ↔ q ∨ (∀x, p x) := decidable.forall_or_distrib_left -- See Note [decidable namespace] protected theorem decidable.forall_or_distrib_right {q : Prop} {p : α → Prop} [decidable q] : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := by simp [or_comm, decidable.forall_or_distrib_left] theorem forall_or_distrib_right {q : Prop} {p : α → Prop} : (∀x, p x ∨ q) ↔ (∀x, p x) ∨ q := decidable.forall_or_distrib_right /-- A predicate holds everywhere on the image of a surjective functions iff it holds everywhere. -/ theorem forall_iff_forall_surj {α β : Type*} {f : α → β} (h : function.surjective f) {P : β → Prop} : (∀ a, P (f a)) ↔ ∀ b, P b := ⟨λ ha b, by cases h b with a hab; rw ←hab; exact ha a, λ hb a, hb $ f a⟩ @[simp] theorem exists_prop {p q : Prop} : (∃ h : p, q) ↔ p ∧ q := ⟨λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨h₁, h₂⟩⟩ theorem exists_unique_prop {p q : Prop} : (∃! h : p, q) ↔ p ∧ q := by simp @[simp] theorem exists_false : ¬ (∃a:α, false) := assume ⟨a, h⟩, h @[simp] lemma exists_unique_false : ¬ (∃! (a : α), false) := assume ⟨a, h, h'⟩, h theorem Exists.fst {p : b → Prop} : Exists p → b | ⟨h, _⟩ := h theorem Exists.snd {p : b → Prop} : ∀ h : Exists p, p h.fst | ⟨_, h⟩ := h theorem forall_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∀ h' : p, q h') ↔ q h := @forall_const (q h) p ⟨h⟩ theorem exists_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃ h' : p, q h') ↔ q h := @exists_const (q h) p ⟨h⟩ theorem exists_unique_prop_of_true {p : Prop} {q : p → Prop} (h : p) : (∃! h' : p, q h') ↔ q h := @exists_unique_const (q h) p ⟨h⟩ _ theorem forall_prop_of_false {p : Prop} {q : p → Prop} (hn : ¬ p) : (∀ h' : p, q h') ↔ true := iff_true_intro $ λ h, hn.elim h theorem exists_prop_of_false {p : Prop} {q : p → Prop} : ¬ p → ¬ (∃ h' : p, q h') := mt Exists.fst @[congr] lemma exists_prop_congr {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : Exists q ↔ ∃ h : p', q' (hp.2 h) := ⟨λ ⟨_, _⟩, ⟨hp.1 ‹_›, (hq _).1 ‹_›⟩, λ ⟨_, _⟩, ⟨_, (hq _).2 ‹_›⟩⟩ @[congr] lemma exists_prop_congr' {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : Exists q = ∃ h : p', q' (hp.2 h) := propext (exists_prop_congr hq _) @[simp] lemma exists_true_left (p : true → Prop) : (∃ x, p x) ↔ p true.intro := exists_prop_of_true _ @[simp] lemma exists_false_left (p : false → Prop) : ¬ ∃ x, p x := exists_prop_of_false not_false lemma exists_unique.unique {α : Sort*} {p : α → Prop} (h : ∃! x, p x) {y₁ y₂ : α} (py₁ : p y₁) (py₂ : p y₂) : y₁ = y₂ := unique_of_exists_unique h py₁ py₂ @[congr] lemma forall_prop_congr {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : (∀ h, q h) ↔ ∀ h : p', q' (hp.2 h) := ⟨λ h1 h2, (hq _).1 (h1 (hp.2 _)), λ h1 h2, (hq _).2 (h1 (hp.1 h2))⟩ @[congr] lemma forall_prop_congr' {p p' : Prop} {q q' : p → Prop} (hq : ∀ h, q h ↔ q' h) (hp : p ↔ p') : (∀ h, q h) = ∀ h : p', q' (hp.2 h) := propext (forall_prop_congr hq _) @[simp] lemma forall_true_left (p : true → Prop) : (∀ x, p x) ↔ p true.intro := forall_prop_of_true _ @[simp] lemma forall_false_left (p : false → Prop) : (∀ x, p x) ↔ true := forall_prop_of_false not_false lemma exists_unique.elim2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π x (h : p x), Prop} {b : Prop} (h₂ : ∃! x (h : p x), q x h) (h₁ : ∀ x (h : p x), q x h → (∀ y (hy : p y), q y hy → y = x) → b) : b := begin simp only [exists_unique_iff_exists] at h₂, apply h₂.elim, exact λ x ⟨hxp, hxq⟩ H, h₁ x hxp hxq (λ y hyp hyq, H y ⟨hyp, hyq⟩) end lemma exists_unique.intro2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (h : p x), Prop} (w : α) (hp : p w) (hq : q w hp) (H : ∀ y (hy : p y), q y hy → y = w) : ∃! x (hx : p x), q x hx := begin simp only [exists_unique_iff_exists], exact exists_unique.intro w ⟨hp, hq⟩ (λ y ⟨hyp, hyq⟩, H y hyp hyq) end lemma exists_unique.exists2 {α : Sort*} {p : α → Sort*} {q : Π (x : α) (h : p x), Prop} (h : ∃! x (hx : p x), q x hx) : ∃ x (hx : p x), q x hx := h.exists.imp (λ x hx, hx.exists) lemma exists_unique.unique2 {α : Sort*} {p : α → Sort*} [∀ x, subsingleton (p x)] {q : Π (x : α) (hx : p x), Prop} (h : ∃! x (hx : p x), q x hx) {y₁ y₂ : α} (hpy₁ : p y₁) (hqy₁ : q y₁ hpy₁) (hpy₂ : p y₂) (hqy₂ : q y₂ hpy₂) : y₁ = y₂ := begin simp only [exists_unique_iff_exists] at h, exact h.unique ⟨hpy₁, hqy₁⟩ ⟨hpy₂, hqy₂⟩ end end quantifiers /-! ### Classical lemmas -/ namespace classical variables {α : Sort*} {p : α → Prop} theorem cases {p : Prop → Prop} (h1 : p true) (h2 : p false) : ∀a, p a := assume a, cases_on a h1 h2 /- use shortened names to avoid conflict when classical namespace is open. -/ noncomputable lemma dec (p : Prop) : decidable p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_pred (p : α → Prop) : decidable_pred p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_rel (p : α → α → Prop) : decidable_rel p := -- see Note [classical lemma] by apply_instance noncomputable lemma dec_eq (α : Sort*) : decidable_eq α := -- see Note [classical lemma] by apply_instance /-- We make decidability results that depends on `classical.choice` noncomputable lemmas. * We have to mark them as noncomputable, because otherwise Lean will try to generate bytecode for them, and fail because it depends on `classical.choice`. * We make them lemmas, and not definitions, because otherwise later definitions will raise \"failed to generate bytecode\" errors when writing something like `letI := classical.dec_eq _`. Cf. <https://leanprover-community.github.io/archive/stream/113488-general/topic/noncomputable.20theorem.html> -/ library_note "classical lemma" /-- Construct a function from a default value `H0`, and a function to use if there exists a value satisfying the predicate. -/ @[elab_as_eliminator] noncomputable def {u} exists_cases {C : Sort u} (H0 : C) (H : ∀ a, p a → C) : C := if h : ∃ a, p a then H (classical.some h) (classical.some_spec h) else H0 lemma some_spec2 {α : Sort*} {p : α → Prop} {h : ∃a, p a} (q : α → Prop) (hpq : ∀a, p a → q a) : q (some h) := hpq _ $ some_spec _ /-- A version of classical.indefinite_description which is definitionally equal to a pair -/ noncomputable def subtype_of_exists {α : Type*} {P : α → Prop} (h : ∃ x, P x) : {x // P x} := ⟨classical.some h, classical.some_spec h⟩ /-- A version of `by_contradiction` that uses types instead of propositions. -/ protected noncomputable def by_contradiction' {α : Sort*} (H : ¬ (α → false)) : α := classical.choice $ peirce _ false $ λ h, (H $ λ a, h ⟨a⟩).elim /-- `classical.by_contradiction'` is equivalent to lean's axiom `classical.choice`. -/ def choice_of_by_contradiction' {α : Sort*} (contra : ¬ (α → false) → α) : nonempty α → α := λ H, contra H.elim end classical /-- This function has the same type as `exists.rec_on`, and can be used to case on an equality, but `exists.rec_on` can only eliminate into Prop, while this version eliminates into any universe using the axiom of choice. -/ @[elab_as_eliminator] noncomputable def {u} exists.classical_rec_on {α} {p : α → Prop} (h : ∃ a, p a) {C : Sort u} (H : ∀ a, p a → C) : C := H (classical.some h) (classical.some_spec h) /-! ### Declarations about bounded quantifiers -/ section bounded_quantifiers variables {α : Sort*} {r p q : α → Prop} {P Q : ∀ x, p x → Prop} {b : Prop} theorem bex_def : (∃ x (h : p x), q x) ↔ ∃ x, p x ∧ q x := ⟨λ ⟨x, px, qx⟩, ⟨x, px, qx⟩, λ ⟨x, px, qx⟩, ⟨x, px, qx⟩⟩ theorem bex.elim {b : Prop} : (∃ x h, P x h) → (∀ a h, P a h → b) → b | ⟨a, h₁, h₂⟩ h' := h' a h₁ h₂ theorem bex.intro (a : α) (h₁ : p a) (h₂ : P a h₁) : ∃ x (h : p x), P x h := ⟨a, h₁, h₂⟩ theorem ball_congr (H : ∀ x h, P x h ↔ Q x h) : (∀ x h, P x h) ↔ (∀ x h, Q x h) := forall_congr $ λ x, forall_congr (H x) theorem bex_congr (H : ∀ x h, P x h ↔ Q x h) : (∃ x h, P x h) ↔ (∃ x h, Q x h) := exists_congr $ λ x, exists_congr (H x) theorem bex_eq_left {a : α} : (∃ x (_ : x = a), p x) ↔ p a := by simp only [exists_prop, exists_eq_left] theorem ball.imp_right (H : ∀ x h, (P x h → Q x h)) (h₁ : ∀ x h, P x h) (x h) : Q x h := H _ _ $ h₁ _ _ theorem bex.imp_right (H : ∀ x h, (P x h → Q x h)) : (∃ x h, P x h) → ∃ x h, Q x h | ⟨x, h, h'⟩ := ⟨_, _, H _ _ h'⟩ theorem ball.imp_left (H : ∀ x, p x → q x) (h₁ : ∀ x, q x → r x) (x) (h : p x) : r x := h₁ _ $ H _ h theorem bex.imp_left (H : ∀ x, p x → q x) : (∃ x (_ : p x), r x) → ∃ x (_ : q x), r x | ⟨x, hp, hr⟩ := ⟨x, H _ hp, hr⟩ theorem ball_of_forall (h : ∀ x, p x) (x) : p x := h x theorem forall_of_ball (H : ∀ x, p x) (h : ∀ x, p x → q x) (x) : q x := h x $ H x theorem bex_of_exists (H : ∀ x, p x) : (∃ x, q x) → ∃ x (_ : p x), q x | ⟨x, hq⟩ := ⟨x, H x, hq⟩ theorem exists_of_bex : (∃ x (_ : p x), q x) → ∃ x, q x | ⟨x, _, hq⟩ := ⟨x, hq⟩ @[simp] theorem bex_imp_distrib : ((∃ x h, P x h) → b) ↔ (∀ x h, P x h → b) := by simp theorem not_bex : (¬ ∃ x h, P x h) ↔ ∀ x h, ¬ P x h := bex_imp_distrib theorem not_ball_of_bex_not : (∃ x h, ¬ P x h) → ¬ ∀ x h, P x h | ⟨x, h, hp⟩ al := hp $ al x h -- See Note [decidable namespace] protected theorem decidable.not_ball [decidable (∃ x h, ¬ P x h)] [∀ x h, decidable (P x h)] : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := ⟨not.decidable_imp_symm $ λ nx x h, nx.decidable_imp_symm $ λ h', ⟨x, h, h'⟩, not_ball_of_bex_not⟩ theorem not_ball : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := decidable.not_ball theorem ball_true_iff (p : α → Prop) : (∀ x, p x → true) ↔ true := iff_true_intro (λ h hrx, trivial) theorem ball_and_distrib : (∀ x h, P x h ∧ Q x h) ↔ (∀ x h, P x h) ∧ (∀ x h, Q x h) := iff.trans (forall_congr $ λ x, forall_and_distrib) forall_and_distrib theorem bex_or_distrib : (∃ x h, P x h ∨ Q x h) ↔ (∃ x h, P x h) ∨ (∃ x h, Q x h) := iff.trans (exists_congr $ λ x, exists_or_distrib) exists_or_distrib theorem ball_or_left_distrib : (∀ x, p x ∨ q x → r x) ↔ (∀ x, p x → r x) ∧ (∀ x, q x → r x) := iff.trans (forall_congr $ λ x, or_imp_distrib) forall_and_distrib theorem bex_or_left_distrib : (∃ x (_ : p x ∨ q x), r x) ↔ (∃ x (_ : p x), r x) ∨ (∃ x (_ : q x), r x) := by simp only [exists_prop]; exact iff.trans (exists_congr $ λ x, or_and_distrib_right) exists_or_distrib end bounded_quantifiers namespace classical local attribute [instance] prop_decidable theorem not_ball {α : Sort*} {p : α → Prop} {P : Π (x : α), p x → Prop} : (¬ ∀ x h, P x h) ↔ (∃ x h, ¬ P x h) := _root_.not_ball end classical lemma ite_eq_iff {α} {p : Prop} [decidable p] {a b c : α} : (if p then a else b) = c ↔ p ∧ a = c ∨ ¬p ∧ b = c := by by_cases p; simp * @[simp] lemma ite_eq_left_iff {α} {p : Prop} [decidable p] {a b : α} : (if p then a else b) = a ↔ (¬p → b = a) := by by_cases p; simp * @[simp] lemma ite_eq_right_iff {α} {p : Prop} [decidable p] {a b : α} : (if p then a else b) = b ↔ (p → a = b) := by by_cases p; simp * lemma ite_eq_or_eq {α} {p : Prop} [decidable p] (a b : α) : ite p a b = a ∨ ite p a b = b := decidable.by_cases (λ h, or.inl (if_pos h)) (λ h, or.inr (if_neg h)) /-! ### Declarations about `nonempty` -/ section nonempty variables {α β : Type*} {γ : α → Type*} attribute [simp] nonempty_of_inhabited @[priority 20] instance has_zero.nonempty [has_zero α] : nonempty α := ⟨0⟩ @[priority 20] instance has_one.nonempty [has_one α] : nonempty α := ⟨1⟩ lemma exists_true_iff_nonempty {α : Sort*} : (∃a:α, true) ↔ nonempty α := iff.intro (λ⟨a, _⟩, ⟨a⟩) (λ⟨a⟩, ⟨a, trivial⟩) @[simp] lemma nonempty_Prop {p : Prop} : nonempty p ↔ p := iff.intro (assume ⟨h⟩, h) (assume h, ⟨h⟩) lemma not_nonempty_iff_imp_false {α : Sort*} : ¬ nonempty α ↔ α → false := ⟨λ h a, h ⟨a⟩, λ h ⟨a⟩, h a⟩ @[simp] lemma nonempty_sigma : nonempty (Σa:α, γ a) ↔ (∃a:α, nonempty (γ a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_subtype {α} {p : α → Prop} : nonempty (subtype p) ↔ (∃a:α, p a) := iff.intro (assume ⟨⟨a, h⟩⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a, h⟩⟩) @[simp] lemma nonempty_prod : nonempty (α × β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_pprod {α β} : nonempty (pprod α β) ↔ (nonempty α ∧ nonempty β) := iff.intro (assume ⟨⟨a, b⟩⟩, ⟨⟨a⟩, ⟨b⟩⟩) (assume ⟨⟨a⟩, ⟨b⟩⟩, ⟨⟨a, b⟩⟩) @[simp] lemma nonempty_sum : nonempty (α ⊕ β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with sum.inl a := or.inl ⟨a⟩ | sum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨sum.inl a⟩ | or.inr ⟨b⟩ := ⟨sum.inr b⟩ end) @[simp] lemma nonempty_psum {α β} : nonempty (psum α β) ↔ (nonempty α ∨ nonempty β) := iff.intro (assume ⟨h⟩, match h with psum.inl a := or.inl ⟨a⟩ | psum.inr b := or.inr ⟨b⟩ end) (assume h, match h with or.inl ⟨a⟩ := ⟨psum.inl a⟩ | or.inr ⟨b⟩ := ⟨psum.inr b⟩ end) @[simp] lemma nonempty_psigma {α} {β : α → Sort*} : nonempty (psigma β) ↔ (∃a:α, nonempty (β a)) := iff.intro (assume ⟨⟨a, c⟩⟩, ⟨a, ⟨c⟩⟩) (assume ⟨a, ⟨c⟩⟩, ⟨⟨a, c⟩⟩) @[simp] lemma nonempty_empty : ¬ nonempty empty := assume ⟨h⟩, h.elim @[simp] lemma nonempty_ulift : nonempty (ulift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty_plift {α} : nonempty (plift α) ↔ nonempty α := iff.intro (assume ⟨⟨a⟩⟩, ⟨a⟩) (assume ⟨a⟩, ⟨⟨a⟩⟩) @[simp] lemma nonempty.forall {α} {p : nonempty α → Prop} : (∀h:nonempty α, p h) ↔ (∀a, p ⟨a⟩) := iff.intro (assume h a, h _) (assume h ⟨a⟩, h _) @[simp] lemma nonempty.exists {α} {p : nonempty α → Prop} : (∃h:nonempty α, p h) ↔ (∃a, p ⟨a⟩) := iff.intro (assume ⟨⟨a⟩, h⟩, ⟨a, h⟩) (assume ⟨a, h⟩, ⟨⟨a⟩, h⟩) lemma classical.nonempty_pi {α} {β : α → Sort*} : nonempty (Πa:α, β a) ↔ (∀a:α, nonempty (β a)) := iff.intro (assume ⟨f⟩ a, ⟨f a⟩) (assume f, ⟨assume a, classical.choice $ f a⟩) /-- Using `classical.choice`, lifts a (`Prop`-valued) `nonempty` instance to a (`Type`-valued) `inhabited` instance. `classical.inhabited_of_nonempty` already exists, in `core/init/classical.lean`, but the assumption is not a type class argument, which makes it unsuitable for some applications. -/ noncomputable def classical.inhabited_of_nonempty' {α} [h : nonempty α] : inhabited α := ⟨classical.choice h⟩ /-- Using `classical.choice`, extracts a term from a `nonempty` type. -/ @[reducible] protected noncomputable def nonempty.some {α} (h : nonempty α) : α := classical.choice h /-- Using `classical.choice`, extracts a term from a `nonempty` type. -/ @[reducible] protected noncomputable def classical.arbitrary (α) [h : nonempty α] : α := classical.choice h /-- Given `f : α → β`, if `α` is nonempty then `β` is also nonempty. `nonempty` cannot be a `functor`, because `functor` is restricted to `Type`. -/ lemma nonempty.map {α β} (f : α → β) : nonempty α → nonempty β | ⟨h⟩ := ⟨f h⟩ protected lemma nonempty.map2 {α β γ : Sort*} (f : α → β → γ) : nonempty α → nonempty β → nonempty γ | ⟨x⟩ ⟨y⟩ := ⟨f x y⟩ protected lemma nonempty.congr {α β} (f : α → β) (g : β → α) : nonempty α ↔ nonempty β := ⟨nonempty.map f, nonempty.map g⟩ lemma nonempty.elim_to_inhabited {α : Sort*} [h : nonempty α] {p : Prop} (f : inhabited α → p) : p := h.elim $ f ∘ inhabited.mk instance {α β} [h : nonempty α] [h2 : nonempty β] : nonempty (α × β) := h.elim $ λ g, h2.elim $ λ g2, ⟨⟨g, g2⟩⟩ end nonempty lemma subsingleton_of_not_nonempty {α : Sort*} (h : ¬ nonempty α) : subsingleton α := ⟨λ x, false.elim $ not_nonempty_iff_imp_false.mp h x⟩ section ite /-- A `dite` whose results do not actually depend on the condition may be reduced to an `ite`. -/ @[simp] lemma dite_eq_ite (P : Prop) [decidable P] {α : Sort*} (x y : α) : dite P (λ h, x) (λ h, y) = ite P x y := rfl /-- A function applied to a `dite` is a `dite` of that function applied to each of the branches. -/ lemma apply_dite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x : P → α) (y : ¬P → α) : f (dite P x y) = dite P (λ h, f (x h)) (λ h, f (y h)) := by { by_cases h : P; simp [h] } /-- A function applied to a `ite` is a `ite` of that function applied to each of the branches. -/ lemma apply_ite {α β : Sort*} (f : α → β) (P : Prop) [decidable P] (x y : α) : f (ite P x y) = ite P (f x) (f y) := apply_dite f P (λ _, x) (λ _, y) /-- A two-argument function applied to two `dite`s is a `dite` of that two-argument function applied to each of the branches. -/ lemma apply_dite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a : P → α) (b : ¬P → α) (c : P → β) (d : ¬P → β) : f (dite P a b) (dite P c d) = dite P (λ h, f (a h) (c h)) (λ h, f (b h) (d h)) := by { by_cases h : P; simp [h] } /-- A two-argument function applied to two `ite`s is a `ite` of that two-argument function applied to each of the branches. -/ lemma apply_ite2 {α β γ : Sort*} (f : α → β → γ) (P : Prop) [decidable P] (a b : α) (c d : β) : f (ite P a b) (ite P c d) = ite P (f a c) (f b d) := apply_dite2 f P (λ _, a) (λ _, b) (λ _, c) (λ _, d) /-- A 'dite' producing a `Pi` type `Π a, β a`, applied to a value `x : α` is a `dite` that applies either branch to `x`. -/ lemma dite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P] (f : P → Π a, β a) (g : ¬ P → Π a, β a) (x : α) : (dite P f g) x = dite P (λ h, f h x) (λ h, g h x) := by { by_cases h : P; simp [h] } /-- A 'ite' producing a `Pi` type `Π a, β a`, applied to a value `x : α` is a `ite` that applies either branch to `x` -/ lemma ite_apply {α : Sort*} {β : α → Sort*} (P : Prop) [decidable P] (f g : Π a, β a) (x : α) : (ite P f g) x = ite P (f x) (g x) := dite_apply P (λ _, f) (λ _, g) x /-- Negation of the condition `P : Prop` in a `dite` is the same as swapping the branches. -/ @[simp] lemma dite_not {α : Sort*} (P : Prop) [decidable P] (x : ¬ P → α) (y : ¬¬ P → α) : dite (¬ P) x y = dite P (λ h, y (not_not_intro h)) x := by { by_cases h : P; simp [h] } /-- Negation of the condition `P : Prop` in a `ite` is the same as swapping the branches. -/ @[simp] lemma ite_not {α : Sort*} (P : Prop) [decidable P] (x y : α) : ite (¬ P) x y = ite P y x := dite_not P (λ _, x) (λ _, y) lemma ite_and {α} {p q : Prop} [decidable p] [decidable q] {x y : α} : ite (p ∧ q) x y = ite p (ite q x y) y := by { by_cases hp : p; by_cases hq : q; simp [hp, hq] } end ite
573c106fb7db890fa1a10df7a0459aaac7cada55
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/linear_algebra/free_module/finite/rank.lean
009206e8e2331c4d79191ba157dd6882ff5abcce
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,972
lean
/- Copyright (c) 2021 Riccardo Brasca. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import linear_algebra.free_module.rank import linear_algebra.free_module.finite.basic /-! # Rank of finite free modules This is a basic API for the rank of finite free modules. -/ --TODO: `linear_algebra/finite_dimensional` should import this file, and a lot of results should --be moved here. universes u v w variables (R : Type u) (M : Type v) (N : Type w) open_locale tensor_product direct_sum big_operators cardinal open cardinal finite_dimensional fintype namespace module.free section ring variables [ring R] [strong_rank_condition R] variables [add_comm_group M] [module R M] [module.free R M] [module.finite R M] variables [add_comm_group N] [module R N] [module.free R N] [module.finite R N] /-- The rank of a finite and free module is finite. -/ lemma rank_lt_omega : module.rank R M < ω := begin letI := nontrivial_of_invariant_basis_number R, rw [← (choose_basis R M).mk_eq_dim'', lt_omega_iff_fintype], exact nonempty.intro infer_instance end /-- If `M` is finite and free, `finrank M = rank M`. -/ @[simp] lemma finrank_eq_rank : ↑(finrank R M) = module.rank R M := by { rw [finrank, cast_to_nat_of_lt_omega (rank_lt_omega R M)] } /-- The finrank of a free module `M` over `R` is the cardinality of `choose_basis_index R M`. -/ lemma finrank_eq_card_choose_basis_index : finrank R M = @card (choose_basis_index R M) (@choose_basis_index.fintype R M _ _ _ _ (nontrivial_of_invariant_basis_number R) _) := begin letI := nontrivial_of_invariant_basis_number R, simp [finrank, rank_eq_card_choose_basis_index] end /-- The finrank of `(ι →₀ R)` is `fintype.card ι`. -/ @[simp] lemma finrank_finsupp {ι : Type v} [fintype ι] : finrank R (ι →₀ R) = card ι := by { rw [finrank, rank_finsupp, ← mk_to_nat_eq_card, to_nat_lift] } /-- The finrank of `(ι → R)` is `fintype.card ι`. -/ lemma finrank_pi {ι : Type v} [fintype ι] : finrank R (ι → R) = card ι := by simp [finrank] /-- The finrank of the direct sum is the sum of the finranks. -/ @[simp] lemma finrank_direct_sum {ι : Type v} [fintype ι] (M : ι → Type w) [Π (i : ι), add_comm_group (M i)] [Π (i : ι), module R (M i)] [Π (i : ι), module.free R (M i)] [Π (i : ι), module.finite R (M i)] : finrank R (⨁ i, M i) = ∑ i, finrank R (M i) := begin letI := nontrivial_of_invariant_basis_number R, simp only [finrank, λ i, rank_eq_card_choose_basis_index R (M i), rank_direct_sum, ← mk_sigma, mk_to_nat_eq_card, card_sigma], end /-- The finrank of `M × N` is `(finrank R M) + (finrank R N)`. -/ @[simp] lemma finrank_prod : finrank R (M × N) = (finrank R M) + (finrank R N) := by { simp [finrank, rank_lt_omega R M, rank_lt_omega R N] } /-- The finrank of a finite product is the sum of the finranks. -/ --TODO: this should follow from `linear_equiv.finrank_eq`, that is over a field. lemma finrank_pi_fintype {ι : Type v} [fintype ι] {M : ι → Type w} [Π (i : ι), add_comm_group (M i)] [Π (i : ι), module R (M i)] [Π (i : ι), module.free R (M i)] [Π (i : ι), module.finite R (M i)] : finrank R (Π i, M i) = ∑ i, finrank R (M i) := begin letI := nontrivial_of_invariant_basis_number R, simp only [finrank, λ i, rank_eq_card_choose_basis_index R (M i), rank_pi_fintype, ← mk_sigma, mk_to_nat_eq_card, card_sigma], end /-- If `n` and `m` are `fintype`, the finrank of `n × m` matrices is `(fintype.card n) * (fintype.card m)`. -/ lemma finrank_matrix (n : Type v) [fintype n] (m : Type w) [fintype m] : finrank R (matrix n m R) = (card n) * (card m) := by { simp [finrank] } end ring section comm_ring variables [comm_ring R] [strong_rank_condition R] variables [add_comm_group M] [module R M] [module.free R M] [module.finite R M] variables [add_comm_group N] [module R N] [module.free R N] [module.finite R N] /-- The finrank of `M →ₗ[R] N` is `(finrank R M) * (finrank R N)`. -/ --TODO: this should follow from `linear_equiv.finrank_eq`, that is over a field. lemma finrank_linear_hom : finrank R (M →ₗ[R] N) = (finrank R M) * (finrank R N) := begin classical, letI := nontrivial_of_invariant_basis_number R, have h := (linear_map.to_matrix (choose_basis R M) (choose_basis R N)), let b := (matrix.std_basis _ _ _).map h.symm, rw [finrank, dim_eq_card_basis b, ← fintype_card, mk_to_nat_eq_card, finrank, finrank, rank_eq_card_choose_basis_index, rank_eq_card_choose_basis_index, mk_to_nat_eq_card, mk_to_nat_eq_card, card_prod, mul_comm] end /-- The finrank of `M ⊗[R] N` is `(finrank R M) * (finrank R N)`. -/ @[simp] lemma finrank_tensor_product (M : Type v) (N : Type w) [add_comm_group M] [module R M] [module.free R M] [add_comm_group N] [module R N] [module.free R N] : finrank R (M ⊗[R] N) = (finrank R M) * (finrank R N) := by { simp [finrank] } end comm_ring end module.free
19ea3070a2317837e3b355623759fdf62ab7fe14
947fa6c38e48771ae886239b4edce6db6e18d0fb
/src/linear_algebra/finite_dimensional.lean
c62ab32f3d842c8992a835f2b25f2b6c4ab01a55
[ "Apache-2.0" ]
permissive
ramonfmir/mathlib
c5dc8b33155473fab97c38bd3aa6723dc289beaa
14c52e990c17f5a00c0cc9e09847af16fabbed25
refs/heads/master
1,661,979,343,526
1,660,830,384,000
1,660,830,384,000
182,072,989
0
0
null
1,555,585,876,000
1,555,585,876,000
null
UTF-8
Lean
false
false
75,704
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 algebra.algebra.subalgebra.basic import field_theory.finiteness /-! # Finite dimensional vector spaces Definition and basic properties of finite dimensional vector spaces, of their dimensions, and of linear maps on such spaces. ## Main definitions Assume `V` is a vector space over a field `K`. There are (at least) three equivalent definitions of finite-dimensionality of `V`: - it admits a finite basis. - it is finitely generated. - it is noetherian, i.e., every subspace is finitely generated. We introduce a typeclass `finite_dimensional K V` capturing this property. For ease of transfer of proof, it is defined using the second point of view, i.e., as `finite`. However, we prove that all these points of view are equivalent, with the following lemmas (in the namespace `finite_dimensional`): - `fintype_basis_index` states that a finite-dimensional vector space has a finite basis - `finite_dimensional.fin_basis` and `finite_dimensional.fin_basis_of_finrank_eq` are bases for finite dimensional vector spaces, where the index type is `fin` - `of_fintype_basis` states that the existence of a basis indexed by a finite type implies finite-dimensionality - `of_finset_basis` states that the existence of a basis indexed by a `finset` implies finite-dimensionality - `of_finite_basis` states that the existence of a basis indexed by a finite set implies finite-dimensionality - `is_noetherian.iff_fg` states that the space is finite-dimensional if and only if it is noetherian Also defined is `finrank`, the dimension of a finite dimensional space, returning a `nat`, as opposed to `module.rank`, which returns a `cardinal`. When the space has infinite dimension, its `finrank` is by convention set to `0`. Preservation of finite-dimensionality and formulas for the dimension are given for - submodules - quotients (for the dimension of a quotient, see `finrank_quotient_add_finrank`) - linear equivs, in `linear_equiv.finite_dimensional` and `linear_equiv.finrank_eq` - image under a linear map (the rank-nullity formula is in `finrank_range_add_finrank_ker`) Basic properties of linear maps of a finite-dimensional vector space are given. Notably, the equivalence of injectivity and surjectivity is proved in `linear_map.injective_iff_surjective`, and the equivalence between left-inverse and right-inverse in `linear_map.mul_eq_one_comm` and `linear_map.comp_eq_id_comm`. ## Implementation notes Most results are deduced from the corresponding results for the general dimension (as a cardinal), in `dimension.lean`. Not all results have been ported yet. Much of this file could be generalised away from fields or division rings. You should not assume that there has been any effort to state lemmas as generally as possible. One of the characterizations of finite-dimensionality is in terms of finite generation. This property is currently defined only for submodules, so we express it through the fact that the maximal submodule (which, as a set, coincides with the whole space) is finitely generated. This is not very convenient to use, although there are some helper functions. However, this becomes very convenient when speaking of submodules which are finite-dimensional, as this notion coincides with the fact that the submodule is finitely generated (as a submodule of the whole space). This equivalence is proved in `submodule.fg_iff_finite_dimensional`. -/ universes u v v' w open_locale classical cardinal open cardinal submodule module function /-- `finite_dimensional` vector spaces are defined to be finite modules. Use `finite_dimensional.of_fintype_basis` to prove finite dimension from another definition. -/ @[reducible] def finite_dimensional (K V : Type*) [division_ring K] [add_comm_group V] [module K V] := module.finite K V variables {K : Type u} {V : Type v} namespace finite_dimensional open is_noetherian section division_ring variables [division_ring K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] /-- If the codomain of an injective linear map is finite dimensional, the domain must be as well. -/ lemma of_injective (f : V →ₗ[K] V₂) (w : function.injective f) [finite_dimensional K V₂] : finite_dimensional K V := have is_noetherian K V₂ := is_noetherian.iff_fg.mpr ‹_›, by exactI module.finite.of_injective f w /-- If the domain of a surjective linear map is finite dimensional, the codomain must be as well. -/ lemma of_surjective (f : V →ₗ[K] V₂) (w : function.surjective f) [finite_dimensional K V] : finite_dimensional K V₂ := module.finite.of_surjective f w variables (K V) instance finite_dimensional_pi {ι} [fintype ι] : finite_dimensional K (ι → K) := iff_fg.1 is_noetherian_pi instance finite_dimensional_pi' {ι} [fintype ι] (M : ι → Type*) [∀ i, add_comm_group (M i)] [∀ i, module K (M i)] [I : ∀ i, finite_dimensional K (M i)] : finite_dimensional K (Π i, M i) := begin haveI : ∀ i : ι, is_noetherian K (M i) := λ i, iff_fg.2 (I i), exact iff_fg.1 is_noetherian_pi end /-- A finite dimensional vector space over a finite field is finite -/ noncomputable def fintype_of_fintype [fintype K] [finite_dimensional K V] : fintype V := module.fintype_of_fintype (@finset_basis K V _ _ _ (iff_fg.2 infer_instance)) variables {K V} /-- If a vector space has a finite basis, then it is finite-dimensional. -/ lemma of_fintype_basis {ι : Type w} [fintype ι] (h : basis ι K V) : finite_dimensional K V := ⟨⟨finset.univ.image h, by { convert h.span_eq, simp } ⟩⟩ /-- If a vector space is `finite_dimensional`, all bases are indexed by a finite type -/ noncomputable def fintype_basis_index {ι : Type*} [finite_dimensional K V] (b : basis ι K V) : fintype ι := begin letI : is_noetherian K V := is_noetherian.iff_fg.2 infer_instance, exact is_noetherian.fintype_basis_index b, end /-- If a vector space is `finite_dimensional`, `basis.of_vector_space` is indexed by a finite type.-/ noncomputable instance [finite_dimensional K V] : fintype (basis.of_vector_space_index K V) := begin letI : is_noetherian K V := is_noetherian.iff_fg.2 infer_instance, apply_instance end /-- If a vector space has a basis indexed by elements of a finite set, then it is finite-dimensional. -/ lemma of_finite_basis {ι : Type w} {s : set ι} (h : basis s K V) (hs : set.finite s) : finite_dimensional K V := by haveI := hs.fintype; exact of_fintype_basis h /-- If a vector space has a finite basis, then it is finite-dimensional, finset style. -/ lemma of_finset_basis {ι : Type w} {s : finset ι} (h : basis s K V) : finite_dimensional K V := of_finite_basis h s.finite_to_set /-- A subspace of a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_submodule [finite_dimensional K V] (S : submodule K V) : finite_dimensional K S := begin letI : is_noetherian K V := iff_fg.2 _, exact iff_fg.1 (is_noetherian.iff_dim_lt_aleph_0.2 (lt_of_le_of_lt (dim_submodule_le _) (dim_lt_aleph_0 K V))), apply_instance, end /-- A quotient of a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_quotient [finite_dimensional K V] (S : submodule K V) : finite_dimensional K (V ⧸ S) := module.finite.of_surjective (submodule.mkq S) $ surjective_quot_mk _ /-- The rank of a module as a natural number. Defined by convention to be `0` if the space has infinite rank. For a vector space `V` over a field `K`, this is the same as the finite dimension of `V` over `K`. -/ noncomputable def finrank (R V : Type*) [semiring R] [add_comm_group V] [module R V] : ℕ := (module.rank R V).to_nat /-- In a finite-dimensional space, its dimension (seen as a cardinal) coincides with its `finrank`. -/ lemma finrank_eq_dim (K : Type u) (V : Type v) [division_ring K] [add_comm_group V] [module K V] [finite_dimensional K V] : (finrank K V : cardinal.{v}) = module.rank K V := begin letI : is_noetherian K V := iff_fg.2 infer_instance, rw [finrank, cast_to_nat_of_lt_aleph_0 (dim_lt_aleph_0 K V)] end lemma finrank_eq_of_dim_eq {n : ℕ} (h : module.rank K V = ↑ n) : finrank K V = n := begin apply_fun to_nat at h, rw to_nat_cast at h, exact_mod_cast h, end lemma finrank_of_infinite_dimensional {K V : Type*} [division_ring K] [add_comm_group V] [module K V] (h : ¬finite_dimensional K V) : finrank K V = 0 := dif_neg $ mt is_noetherian.iff_dim_lt_aleph_0.2 $ (not_iff_not.2 iff_fg).2 h lemma finite_dimensional_of_finrank {K V : Type*} [division_ring K] [add_comm_group V] [module K V] (h : 0 < finrank K V) : finite_dimensional K V := by { contrapose h, simp [finrank_of_infinite_dimensional h] } lemma finite_dimensional_of_finrank_eq_succ {K V : Type*} [field K] [add_comm_group V] [module K V] {n : ℕ} (hn : finrank K V = n.succ) : finite_dimensional K V := finite_dimensional_of_finrank $ by rw hn; exact n.succ_pos /-- We can infer `finite_dimensional K V` in the presence of `[fact (finrank K V = n + 1)]`. Declare this as a local instance where needed. -/ lemma fact_finite_dimensional_of_finrank_eq_succ {K V : Type*} [field K] [add_comm_group V] [module K V] (n : ℕ) [fact (finrank K V = n + 1)] : finite_dimensional K V := finite_dimensional_of_finrank $ by convert nat.succ_pos n; apply fact.out lemma finite_dimensional_iff_of_rank_eq_nsmul {K V W : Type*} [field K] [add_comm_group V] [add_comm_group W] [module K V] [module K W] {n : ℕ} (hn : n ≠ 0) (hVW : module.rank K V = n • module.rank K W) : finite_dimensional K V ↔ finite_dimensional K W := by simp only [finite_dimensional, ← is_noetherian.iff_fg, is_noetherian.iff_dim_lt_aleph_0, hVW, cardinal.nsmul_lt_aleph_0_iff_of_ne_zero hn] /-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the basis. -/ lemma finrank_eq_card_basis {ι : Type w} [fintype ι] (h : basis ι K V) : finrank K V = fintype.card ι := begin haveI : finite_dimensional K V := of_fintype_basis h, have := dim_eq_card_basis h, rw ← finrank_eq_dim at this, exact_mod_cast this end /-- If a vector space is finite-dimensional, then the cardinality of any basis is equal to its `finrank`. -/ lemma finrank_eq_card_basis' [finite_dimensional K V] {ι : Type w} (h : basis ι K V) : (finrank K V : cardinal.{w}) = #ι := begin haveI : is_noetherian K V := iff_fg.2 infer_instance, haveI : fintype ι := fintype_basis_index h, rw [cardinal.mk_fintype, finrank_eq_card_basis h] end /-- If a vector space has a finite basis, then its dimension is equal to the cardinality of the basis. This lemma uses a `finset` instead of indexed types. -/ lemma finrank_eq_card_finset_basis {ι : Type w} {b : finset ι} (h : basis.{w} b K V) : finrank K V = finset.card b := by rw [finrank_eq_card_basis h, fintype.card_coe] variables (K V) /-- A finite dimensional vector space has a basis indexed by `fin (finrank K V)`. -/ noncomputable def fin_basis [finite_dimensional K V] : basis (fin (finrank K V)) K V := have h : fintype.card (@finset_basis_index K V _ _ _ (iff_fg.2 infer_instance)) = finrank K V, from (finrank_eq_card_basis (@finset_basis K V _ _ _ (iff_fg.2 infer_instance))).symm, (@finset_basis K V _ _ _ (iff_fg.2 infer_instance)).reindex (fintype.equiv_fin_of_card_eq h) /-- An `n`-dimensional vector space has a basis indexed by `fin n`. -/ noncomputable def fin_basis_of_finrank_eq [finite_dimensional K V] {n : ℕ} (hn : finrank K V = n) : basis (fin n) K V := (fin_basis K V).reindex (fin.cast hn).to_equiv variables {K V} /-- A module with dimension 1 has a basis with one element. -/ noncomputable def basis_unique (ι : Type*) [unique ι] (h : finrank K V = 1) : basis ι K V := begin haveI := finite_dimensional_of_finrank (_root_.zero_lt_one.trans_le h.symm.le), exact (fin_basis_of_finrank_eq K V h).reindex (equiv.equiv_of_unique _ _) end @[simp] lemma basis_unique.repr_eq_zero_iff {ι : Type*} [unique ι] {h : finrank K V = 1} {v : V} {i : ι} : (basis_unique ι h).repr v i = 0 ↔ v = 0 := ⟨λ hv, (basis_unique ι h).repr.map_eq_zero_iff.mp (finsupp.ext $ λ j, subsingleton.elim i j ▸ hv), λ hv, by rw [hv, linear_equiv.map_zero, finsupp.zero_apply]⟩ lemma cardinal_mk_le_finrank_of_linear_independent [finite_dimensional K V] {ι : Type w} {b : ι → V} (h : linear_independent K b) : #ι ≤ finrank K V := begin rw ← lift_le.{_ (max v w)}, simpa [← finrank_eq_dim K V] using cardinal_lift_le_dim_of_linear_independent.{_ _ _ (max v w)} h end lemma fintype_card_le_finrank_of_linear_independent [finite_dimensional K V] {ι : Type*} [fintype ι] {b : ι → V} (h : linear_independent K b) : fintype.card ι ≤ finrank K V := by simpa using cardinal_mk_le_finrank_of_linear_independent h lemma finset_card_le_finrank_of_linear_independent [finite_dimensional K V] {b : finset V} (h : linear_independent K (λ x, x : b → V)) : b.card ≤ finrank K V := begin rw ←fintype.card_coe, exact fintype_card_le_finrank_of_linear_independent h, end lemma lt_aleph_0_of_linear_independent {ι : Type w} [finite_dimensional K V] {v : ι → V} (h : linear_independent K v) : #ι < ℵ₀ := begin apply cardinal.lift_lt.1, apply lt_of_le_of_lt, apply cardinal_lift_le_dim_of_linear_independent h, rw [←finrank_eq_dim, cardinal.lift_aleph_0, cardinal.lift_nat_cast], apply cardinal.nat_lt_aleph_0, end lemma _root_.linear_independent.finite {K : Type*} {V : Type*} [division_ring K] [add_comm_group V] [module K V] [finite_dimensional K V] {b : set V} (h : linear_independent K (λ (x:b), (x:V))) : b.finite := cardinal.lt_aleph_0_iff_set_finite.mp (finite_dimensional.lt_aleph_0_of_linear_independent h) lemma not_linear_independent_of_infinite {ι : Type w} [inf : infinite ι] [finite_dimensional K V] (v : ι → V) : ¬ linear_independent K v := begin intro h_lin_indep, have : ¬ ℵ₀ ≤ #ι := not_le.mpr (lt_aleph_0_of_linear_independent h_lin_indep), have : ℵ₀ ≤ #ι := infinite_iff.mp inf, contradiction end /-- A finite dimensional space has positive `finrank` iff it has a nonzero element. -/ lemma finrank_pos_iff_exists_ne_zero [finite_dimensional K V] : 0 < finrank K V ↔ ∃ x : V, x ≠ 0 := iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_pos_iff_exists_ne_zero K V _ _ _ _ _) /-- A finite dimensional space has positive `finrank` iff it is nontrivial. -/ lemma finrank_pos_iff [finite_dimensional K V] : 0 < finrank K V ↔ nontrivial V := iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_pos_iff_nontrivial K V _ _ _ _ _) /-- A finite dimensional space is nontrivial if it has positive `finrank`. -/ lemma nontrivial_of_finrank_pos (h : 0 < finrank K V) : nontrivial V := begin haveI : finite_dimensional K V := finite_dimensional_of_finrank h, rwa finrank_pos_iff at h end /-- A finite dimensional space is nontrivial if it has `finrank` equal to the successor of a natural number. -/ lemma nontrivial_of_finrank_eq_succ {n : ℕ} (hn : finrank K V = n.succ) : nontrivial V := nontrivial_of_finrank_pos (by rw hn; exact n.succ_pos) /-- A nontrivial finite dimensional space has positive `finrank`. -/ lemma finrank_pos [finite_dimensional K V] [h : nontrivial V] : 0 < finrank K V := finrank_pos_iff.mpr h /-- A finite dimensional space has zero `finrank` iff it is a subsingleton. This is the `finrank` version of `dim_zero_iff`. -/ lemma finrank_zero_iff [finite_dimensional K V] : finrank K V = 0 ↔ subsingleton V := iff.trans (by { rw ← finrank_eq_dim, norm_cast }) (@dim_zero_iff K V _ _ _ _ _) /-- A finite dimensional space that is a subsingleton has zero `finrank`. -/ lemma finrank_zero_of_subsingleton [h : subsingleton V] : finrank K V = 0 := finrank_zero_iff.2 h lemma basis.subset_extend {s : set V} (hs : linear_independent K (coe : s → V)) : s ⊆ hs.extend (set.subset_univ _) := hs.subset_extend _ /-- If a submodule has maximal dimension in a finite dimensional space, then it is equal to the whole space. -/ lemma eq_top_of_finrank_eq [finite_dimensional K V] {S : submodule K V} (h : finrank K S = finrank K V) : S = ⊤ := begin haveI : is_noetherian K V := iff_fg.2 infer_instance, set bS := basis.of_vector_space K S with bS_eq, have : linear_independent K (coe : (coe '' basis.of_vector_space_index K S : set V) → V), from @linear_independent.image_subtype _ _ _ _ _ _ _ _ _ (submodule.subtype S) (by simpa using bS.linear_independent) (by simp), set b := basis.extend this with b_eq, letI : fintype (this.extend _) := (finite_of_linear_independent (by simpa using b.linear_independent)).fintype, letI : fintype (coe '' basis.of_vector_space_index K S) := (finite_of_linear_independent this).fintype, letI : fintype (basis.of_vector_space_index K S) := (finite_of_linear_independent (by simpa using bS.linear_independent)).fintype, have : coe '' (basis.of_vector_space_index K S) = this.extend (set.subset_univ _), from set.eq_of_subset_of_card_le (this.subset_extend _) (by rw [set.card_image_of_injective _ subtype.coe_injective, ← finrank_eq_card_basis bS, ← finrank_eq_card_basis b, h]; apply_instance), rw [← b.span_eq, b_eq, basis.coe_extend, subtype.range_coe, ← this, ← submodule.coe_subtype, span_image], have := bS.span_eq, rw [bS_eq, basis.coe_of_vector_space, subtype.range_coe] at this, rw [this, map_top (submodule.subtype S), range_subtype], end variable (K) /-- A division_ring is one-dimensional as a vector space over itself. -/ @[simp] lemma finrank_self : finrank K K = 1 := begin have := dim_self K, rw [←finrank_eq_dim] at this, exact_mod_cast this end instance finite_dimensional_self : finite_dimensional K K := by apply_instance /-- The vector space of functions on a fintype ι has finrank equal to the cardinality of ι. -/ @[simp] lemma finrank_fintype_fun_eq_card {ι : Type v} [fintype ι] : finrank K (ι → K) = fintype.card ι := begin have : module.rank K (ι → K) = fintype.card ι := dim_fun', rwa [← finrank_eq_dim, nat_cast_inj] at this, end /-- The vector space of functions on `fin n` has finrank equal to `n`. -/ @[simp] lemma finrank_fin_fun {n : ℕ} : finrank K (fin n → K) = n := by simp /-- The submodule generated by a finite set is finite-dimensional. -/ theorem span_of_finite {A : set V} (hA : set.finite A) : finite_dimensional K (submodule.span K A) := iff_fg.1 $ is_noetherian_span_of_finite K hA /-- The submodule generated by a single element is finite-dimensional. -/ instance span_singleton (x : V) : finite_dimensional K (K ∙ x) := span_of_finite K $ set.finite_singleton _ /-- The submodule generated by a finset is finite-dimensional. -/ instance span_finset (s : finset V) : finite_dimensional K (span K (s : set V)) := span_of_finite K $ s.finite_to_set /-- Pushforwards of finite-dimensional submodules are finite-dimensional. -/ instance (f : V →ₗ[K] V₂) (p : submodule K V) [h : finite_dimensional K p] : finite_dimensional K (p.map f) := begin unfreezingI { rw [finite_dimensional, ← iff_fg, is_noetherian.iff_dim_lt_aleph_0] at h ⊢ }, rw [← cardinal.lift_lt.{v' v}], rw [← cardinal.lift_lt.{v v'}] at h, rw [cardinal.lift_aleph_0] at h ⊢, exact (lift_dim_map_le f p).trans_lt h end /-- Pushforwards of finite-dimensional submodules have a smaller finrank. -/ lemma finrank_map_le (f : V →ₗ[K] V₂) (p : submodule K V) [finite_dimensional K p] : finrank K (p.map f) ≤ finrank K p := by simpa [← finrank_eq_dim] using lift_dim_map_le f p variable {K} lemma _root_.complete_lattice.independent.subtype_ne_bot_le_finrank_aux [finite_dimensional K V] {ι : Type w} {p : ι → submodule K V} (hp : complete_lattice.independent p) : #{i // p i ≠ ⊥} ≤ (finrank K V : cardinal.{w}) := begin suffices : cardinal.lift.{v} (#{i // p i ≠ ⊥}) ≤ cardinal.lift.{v} (finrank K V : cardinal.{w}), { rwa cardinal.lift_le at this }, calc cardinal.lift.{v} (# {i // p i ≠ ⊥}) ≤ cardinal.lift.{w} (module.rank K V) : hp.subtype_ne_bot_le_rank ... = cardinal.lift.{w} (finrank K V : cardinal.{v}) : by rw finrank_eq_dim ... = cardinal.lift.{v} (finrank K V : cardinal.{w}) : by simp end /-- If `p` is an independent family of subspaces of a finite-dimensional space `V`, then the number of nontrivial subspaces in the family `p` is finite. -/ noncomputable def _root_.complete_lattice.independent.fintype_ne_bot_of_finite_dimensional [finite_dimensional K V] {ι : Type w} {p : ι → submodule K V} (hp : complete_lattice.independent p) : fintype {i : ι // p i ≠ ⊥} := begin suffices : #{i // p i ≠ ⊥} < (ℵ₀ : cardinal.{w}), { rw cardinal.lt_aleph_0_iff_fintype at this, exact this.some }, refine lt_of_le_of_lt hp.subtype_ne_bot_le_finrank_aux _, simp [cardinal.nat_lt_aleph_0], end /-- If `p` is an independent family of subspaces of a finite-dimensional space `V`, then the number of nontrivial subspaces in the family `p` is bounded above by the dimension of `V`. Note that the `fintype` hypothesis required here can be provided by `complete_lattice.independent.fintype_ne_bot_of_finite_dimensional`. -/ lemma _root_.complete_lattice.independent.subtype_ne_bot_le_finrank [finite_dimensional K V] {ι : Type w} {p : ι → submodule K V} (hp : complete_lattice.independent p) [fintype {i // p i ≠ ⊥}] : fintype.card {i // p i ≠ ⊥} ≤ finrank K V := by simpa using hp.subtype_ne_bot_le_finrank_aux section open_locale big_operators open finset /-- If a finset has cardinality larger than the dimension of the space, then there is a nontrivial linear relation amongst its elements. -/ lemma exists_nontrivial_relation_of_dim_lt_card [finite_dimensional K V] {t : finset V} (h : finrank K V < t.card) : ∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∃ x ∈ t, f x ≠ 0 := begin have := mt finset_card_le_finrank_of_linear_independent (by { simpa using h }), rw not_linear_independent_iff at this, obtain ⟨s, g, sum, z, zm, nonzero⟩ := this, -- Now we have to extend `g` to all of `t`, then to all of `V`. let f : V → K := λ x, if h : x ∈ t then if (⟨x, h⟩ : t) ∈ s then g ⟨x, h⟩ else 0 else 0, -- and finally clean up the mess caused by the extension. refine ⟨f, _, _⟩, { dsimp [f], rw ← sum, fapply sum_bij_ne_zero (λ v hvt _, (⟨v, hvt⟩ : {v // v ∈ t})), { intros v hvt H, dsimp, rw [dif_pos hvt] at H, contrapose! H, rw [if_neg H, zero_smul], }, { intros _ _ _ _ _ _, exact subtype.mk.inj, }, { intros b hbs hb, use b, simpa only [hbs, exists_prop, dif_pos, finset.mk_coe, and_true, if_true, finset.coe_mem, eq_self_iff_true, exists_prop_of_true, ne.def] using hb, }, { intros a h₁, dsimp, rw [dif_pos h₁], intro h₂, rw [if_pos], contrapose! h₂, rw [if_neg h₂, zero_smul], }, }, { refine ⟨z, z.2, _⟩, dsimp only [f], erw [dif_pos z.2, if_pos]; rwa [subtype.coe_eta] }, end /-- If a finset has cardinality larger than `finrank + 1`, then there is a nontrivial linear relation amongst its elements, such that the coefficients of the relation sum to zero. -/ lemma exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card [finite_dimensional K V] {t : finset V} (h : finrank K V + 1 < t.card) : ∃ f : V → K, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, f x ≠ 0 := begin -- Pick an element x₀ ∈ t, have card_pos : 0 < t.card := lt_trans (nat.succ_pos _) h, obtain ⟨x₀, m⟩ := (finset.card_pos.1 card_pos).bex, -- and apply the previous lemma to the {xᵢ - x₀} let shift : V ↪ V := ⟨λ x, x - x₀, sub_left_injective⟩, let t' := (t.erase x₀).map shift, have h' : finrank K V < t'.card, { simp only [t', card_map, finset.card_erase_of_mem m], exact nat.lt_pred_iff.mpr h, }, -- to obtain a function `g`. obtain ⟨g, gsum, x₁, x₁_mem, nz⟩ := exists_nontrivial_relation_of_dim_lt_card h', -- Then obtain `f` by translating back by `x₀`, -- and setting the value of `f` at `x₀` to ensure `∑ e in t, f e = 0`. let f : V → K := λ z, if z = x₀ then - ∑ z in (t.erase x₀), g (z - x₀) else g (z - x₀), refine ⟨f, _ ,_ ,_⟩, -- After this, it's a matter of verifiying the properties, -- based on the corresponding properties for `g`. { show ∑ (e : V) in t, f e • e = 0, -- We prove this by splitting off the `x₀` term of the sum, -- which is itself a sum over `t.erase x₀`, -- combining the two sums, and -- observing that after reindexing we have exactly -- ∑ (x : V) in t', g x • x = 0. simp only [f], conv_lhs { apply_congr, skip, rw [ite_smul], }, rw [finset.sum_ite], conv { congr, congr, apply_congr, simp [filter_eq', m], }, conv { congr, congr, skip, apply_congr, simp [filter_ne'], }, rw [sum_singleton, neg_smul, add_comm, ←sub_eq_add_neg, sum_smul, ←sum_sub_distrib], simp only [←smul_sub], -- At the end we have to reindex the sum, so we use `change` to -- express the summand using `shift`. change (∑ (x : V) in t.erase x₀, (λ e, g e • e) (shift x)) = 0, rw ←sum_map _ shift, exact gsum, }, { show ∑ (e : V) in t, f e = 0, -- Again we split off the `x₀` term, -- observing that it exactly cancels the other terms. rw [← insert_erase m, sum_insert (not_mem_erase x₀ t)], dsimp [f], rw [if_pos rfl], conv_lhs { congr, skip, apply_congr, skip, rw if_neg (show x ≠ x₀, from (mem_erase.mp H).1), }, exact neg_add_self _, }, { show ∃ (x : V) (H : x ∈ t), f x ≠ 0, -- We can use x₁ + x₀. refine ⟨x₁ + x₀, _, _⟩, { rw finset.mem_map at x₁_mem, rcases x₁_mem with ⟨x₁, x₁_mem, rfl⟩, rw mem_erase at x₁_mem, simp only [x₁_mem, sub_add_cancel, function.embedding.coe_fn_mk], }, { dsimp only [f], rwa [if_neg, add_sub_cancel], rw [add_left_eq_self], rintro rfl, simpa only [sub_eq_zero, exists_prop, finset.mem_map, embedding.coe_fn_mk, eq_self_iff_true, mem_erase, not_true, exists_eq_right, ne.def, false_and] using x₁_mem, } }, end section variables {L : Type*} [linear_ordered_field L] variables {W : Type v} [add_comm_group W] [module L W] /-- A slight strengthening of `exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card` available when working over an ordered field: we can ensure a positive coefficient, not just a nonzero coefficient. -/ lemma exists_relation_sum_zero_pos_coefficient_of_dim_succ_lt_card [finite_dimensional L W] {t : finset W} (h : finrank L W + 1 < t.card) : ∃ f : W → L, ∑ e in t, f e • e = 0 ∧ ∑ e in t, f e = 0 ∧ ∃ x ∈ t, 0 < f x := begin obtain ⟨f, sum, total, nonzero⟩ := exists_nontrivial_relation_sum_zero_of_dim_succ_lt_card h, exact ⟨f, sum, total, exists_pos_of_sum_zero_of_exists_nonzero f total nonzero⟩, end end end /-- In a vector space with dimension 1, each set {v} is a basis for `v ≠ 0`. -/ @[simps] noncomputable def basis_singleton (ι : Type*) [unique ι] (h : finrank K V = 1) (v : V) (hv : v ≠ 0) : basis ι K V := let b := basis_unique ι h in let h : b.repr v default ≠ 0 := mt basis_unique.repr_eq_zero_iff.mp hv in basis.of_repr { to_fun := λ w, finsupp.single default (b.repr w default / b.repr v default), inv_fun := λ f, f default • v, map_add' := by simp [add_div], map_smul' := by simp [mul_div], left_inv := λ w, begin apply_fun b.repr using b.repr.to_equiv.injective, apply_fun equiv.finsupp_unique, simp only [linear_equiv.map_smulₛₗ, finsupp.coe_smul, finsupp.single_eq_same, ring_hom.id_apply, smul_eq_mul, pi.smul_apply, equiv.finsupp_unique_apply], exact div_mul_cancel _ h, end , right_inv := λ f, begin ext, simp only [linear_equiv.map_smulₛₗ, finsupp.coe_smul, finsupp.single_eq_same, ring_hom.id_apply, smul_eq_mul, pi.smul_apply], exact mul_div_cancel _ h, end, } @[simp] lemma basis_singleton_apply (ι : Type*) [unique ι] (h : finrank K V = 1) (v : V) (hv : v ≠ 0) (i : ι) : basis_singleton ι h v hv i = v := by { cases unique.uniq ‹unique ι› i, simp [basis_singleton], } @[simp] lemma range_basis_singleton (ι : Type*) [unique ι] (h : finrank K V = 1) (v : V) (hv : v ≠ 0) : set.range (basis_singleton ι h v hv) = {v} := by rw [set.range_unique, basis_singleton_apply] end division_ring end finite_dimensional variables {K V} section zero_dim variables [division_ring K] [add_comm_group V] [module K V] open finite_dimensional lemma finite_dimensional_of_dim_eq_zero (h : module.rank K V = 0) : finite_dimensional K V := begin dsimp [finite_dimensional], rw [← is_noetherian.iff_fg, is_noetherian.iff_dim_lt_aleph_0, h], exact cardinal.aleph_0_pos end lemma finite_dimensional_of_dim_eq_one (h : module.rank K V = 1) : finite_dimensional K V := begin dsimp [finite_dimensional], rw [← is_noetherian.iff_fg, is_noetherian.iff_dim_lt_aleph_0, h], exact one_lt_aleph_0 end lemma finrank_eq_zero_of_dim_eq_zero [finite_dimensional K V] (h : module.rank K V = 0) : finrank K V = 0 := begin convert finrank_eq_dim K V, rw h, norm_cast end lemma finrank_eq_zero_of_basis_imp_not_finite (h : ∀ s : set V, basis.{v} (s : set V) K V → ¬ s.finite) : finrank K V = 0 := dif_neg (λ dim_lt, h _ (basis.of_vector_space K V) ((basis.of_vector_space K V).finite_index_of_dim_lt_aleph_0 dim_lt)) lemma finrank_eq_zero_of_basis_imp_false (h : ∀ s : finset V, basis.{v} (s : set V) K V → false) : finrank K V = 0 := finrank_eq_zero_of_basis_imp_not_finite (λ s b hs, h hs.to_finset (by { convert b, simp })) lemma finrank_eq_zero_of_not_exists_basis (h : ¬ (∃ s : finset V, nonempty (basis (s : set V) K V))) : finrank K V = 0 := finrank_eq_zero_of_basis_imp_false (λ s b, h ⟨s, ⟨b⟩⟩) lemma finrank_eq_zero_of_not_exists_basis_finite (h : ¬ ∃ (s : set V) (b : basis.{v} (s : set V) K V), s.finite) : finrank K V = 0 := finrank_eq_zero_of_basis_imp_not_finite (λ s b hs, h ⟨s, b, hs⟩) lemma finrank_eq_zero_of_not_exists_basis_finset (h : ¬ ∃ (s : finset V), nonempty (basis s K V)) : finrank K V = 0 := finrank_eq_zero_of_basis_imp_false (λ s b, h ⟨s, ⟨b⟩⟩) variables (K V) instance finite_dimensional_bot : finite_dimensional K (⊥ : submodule K V) := finite_dimensional_of_dim_eq_zero $ by simp @[simp] lemma finrank_bot : finrank K (⊥ : submodule K V) = 0 := begin convert finrank_eq_dim K (⊥ : submodule K V), rw dim_bot, norm_cast end variables {K V} lemma bot_eq_top_of_dim_eq_zero (h : module.rank K V = 0) : (⊥ : submodule K V) = ⊤ := begin haveI := finite_dimensional_of_dim_eq_zero h, apply eq_top_of_finrank_eq, rw [finrank_bot, finrank_eq_zero_of_dim_eq_zero h] end @[simp] theorem dim_eq_zero {S : submodule K V} : module.rank K S = 0 ↔ S = ⊥ := ⟨λ h, (submodule.eq_bot_iff _).2 $ λ x hx, congr_arg subtype.val $ ((submodule.eq_bot_iff _).1 $ eq.symm $ bot_eq_top_of_dim_eq_zero h) ⟨x, hx⟩ submodule.mem_top, λ h, by rw [h, dim_bot]⟩ @[simp] theorem finrank_eq_zero {S : submodule K V} [finite_dimensional K S] : finrank K S = 0 ↔ S = ⊥ := by rw [← dim_eq_zero, ← finrank_eq_dim, ← @nat.cast_zero cardinal, cardinal.nat_cast_inj] end zero_dim namespace submodule open is_noetherian finite_dimensional section division_ring variables [division_ring K] [add_comm_group V] [module K V] /-- A submodule is finitely generated if and only if it is finite-dimensional -/ theorem fg_iff_finite_dimensional (s : submodule K V) : s.fg ↔ finite_dimensional K s := ⟨λ h, module.finite_def.2 $ (fg_top s).2 h, λ h, (fg_top s).1 $ module.finite_def.1 h⟩ /-- A submodule contained in a finite-dimensional submodule is finite-dimensional. -/ lemma finite_dimensional_of_le {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (h : S₁ ≤ S₂) : finite_dimensional K S₁ := begin haveI : is_noetherian K S₂ := iff_fg.2 infer_instance, exact iff_fg.1 (is_noetherian.iff_dim_lt_aleph_0.2 (lt_of_le_of_lt (dim_le_of_submodule _ _ h) (dim_lt_aleph_0 K S₂))), end /-- The inf of two submodules, the first finite-dimensional, is finite-dimensional. -/ instance finite_dimensional_inf_left (S₁ S₂ : submodule K V) [finite_dimensional K S₁] : finite_dimensional K (S₁ ⊓ S₂ : submodule K V) := finite_dimensional_of_le inf_le_left /-- The inf of two submodules, the second finite-dimensional, is finite-dimensional. -/ instance finite_dimensional_inf_right (S₁ S₂ : submodule K V) [finite_dimensional K S₂] : finite_dimensional K (S₁ ⊓ S₂ : submodule K V) := finite_dimensional_of_le inf_le_right /-- The sup of two finite-dimensional submodules is finite-dimensional. -/ instance finite_dimensional_sup (S₁ S₂ : submodule K V) [h₁ : finite_dimensional K S₁] [h₂ : finite_dimensional K S₂] : finite_dimensional K (S₁ ⊔ S₂ : submodule K V) := begin unfold finite_dimensional at *, rw [finite_def] at *, exact (fg_top _).2 (((fg_top S₁).1 h₁).sup ((fg_top S₂).1 h₂)), end /-- The submodule generated by a finite supremum of finite dimensional submodules is finite-dimensional. Note that strictly this only needs `∀ i ∈ s, finite_dimensional K (S i)`, but that doesn't work well with typeclass search. -/ instance finite_dimensional_finset_sup {ι : Type*} (s : finset ι) (S : ι → submodule K V) [Π i, finite_dimensional K (S i)] : finite_dimensional K (s.sup S : submodule K V) := begin refine @finset.sup_induction _ _ _ _ s S (λ i, finite_dimensional K ↥i) (finite_dimensional_bot K V) _ (λ i hi, by apply_instance), { introsI S₁ hS₁ S₂ hS₂, exact submodule.finite_dimensional_sup S₁ S₂ }, end /-- The submodule generated by a supremum of finite dimensional submodules, indexed by a finite type is finite-dimensional. -/ instance finite_dimensional_supr {ι : Type*} [fintype ι] (S : ι → submodule K V) [Π i, finite_dimensional K (S i)] : finite_dimensional K ↥(⨆ i, S i) := begin rw ←finset.sup_univ_eq_supr, exact submodule.finite_dimensional_finset_sup _ _, end /-- The submodule generated by a supremum indexed by a proposition is finite-dimensional if the submodule is. -/ instance finite_dimensional_supr_prop {P : Prop} (S : P → submodule K V) [Π h, finite_dimensional K (S h)] : finite_dimensional K ↥(⨆ h, S h) := begin by_cases hp : P, { rw supr_pos hp, apply_instance }, { rw supr_neg hp, apply_instance }, end /-- The dimension of a submodule is bounded by the dimension of the ambient space. -/ lemma finrank_le [finite_dimensional K V] (s : submodule K V) : finrank K s ≤ finrank K V := by simpa only [cardinal.nat_cast_le, ←finrank_eq_dim] using s.subtype.dim_le_of_injective (injective_subtype s) /-- The dimension of a quotient is bounded by the dimension of the ambient space. -/ lemma finrank_quotient_le [finite_dimensional K V] (s : submodule K V) : finrank K (V ⧸ s) ≤ finrank K V := by simpa only [cardinal.nat_cast_le, ←finrank_eq_dim] using (mkq s).dim_le_of_surjective (surjective_quot_mk _) end division_ring section field variables [field K] [add_comm_group V] [module K V] /-- In a finite-dimensional vector space, the dimensions of a submodule and of the corresponding quotient add up to the dimension of the space. -/ theorem finrank_quotient_add_finrank [finite_dimensional K V] (s : submodule K V) : finrank K (V ⧸ s) + finrank K s = finrank K V := begin have := dim_quotient_add_dim s, rw [← finrank_eq_dim, ← finrank_eq_dim, ← finrank_eq_dim] at this, exact_mod_cast this end /-- The dimension of a strict submodule is strictly bounded by the dimension of the ambient space. -/ lemma finrank_lt [finite_dimensional K V] {s : submodule K V} (h : s < ⊤) : finrank K s < finrank K V := begin rw [← s.finrank_quotient_add_finrank, add_comm], exact nat.lt_add_of_zero_lt_left _ _ (finrank_pos_iff.mpr (quotient.nontrivial_of_lt_top _ h)) end /-- The sum of the dimensions of s + t and s ∩ t is the sum of the dimensions of s and t -/ theorem dim_sup_add_dim_inf_eq (s t : submodule K V) [finite_dimensional K s] [finite_dimensional K t] : finrank K ↥(s ⊔ t) + finrank K ↥(s ⊓ t) = finrank K ↥s + finrank K ↥t := begin have key : module.rank K ↥(s ⊔ t) + module.rank K ↥(s ⊓ t) = module.rank K s + module.rank K t := dim_sup_add_dim_inf_eq s t, repeat { rw ←finrank_eq_dim at key }, norm_cast at key, exact key end lemma eq_top_of_disjoint [finite_dimensional K V] (s t : submodule K V) (hdim : finrank K s + finrank K t = finrank K V) (hdisjoint : disjoint s t) : s ⊔ t = ⊤ := begin have h_finrank_inf : finrank K ↥(s ⊓ t) = 0, { rw [disjoint, le_bot_iff] at hdisjoint, rw [hdisjoint, finrank_bot] }, apply eq_top_of_finrank_eq, rw ←hdim, convert s.dim_sup_add_dim_inf_eq t, rw h_finrank_inf, refl, end end field end submodule namespace linear_equiv open finite_dimensional variables [division_ring K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] /-- Finite dimensionality is preserved under linear equivalence. -/ protected theorem finite_dimensional (f : V ≃ₗ[K] V₂) [finite_dimensional K V] : finite_dimensional K V₂ := module.finite.equiv f variables {R M M₂ : Type*} [ring R] [add_comm_group M] [add_comm_group M₂] variables [module R M] [module R M₂] /-- The dimension of a finite dimensional space is preserved under linear equivalence. -/ theorem finrank_eq (f : M ≃ₗ[R] M₂) : finrank R M = finrank R M₂ := by { unfold finrank, rw [← cardinal.to_nat_lift, f.lift_dim_eq, cardinal.to_nat_lift] } /-- Pushforwards of finite-dimensional submodules along a `linear_equiv` have the same finrank. -/ lemma finrank_map_eq (f : M ≃ₗ[R] M₂) (p : submodule R M) : finrank R (p.map (f : M →ₗ[R] M₂)) = finrank R p := (f.submodule_map p).finrank_eq.symm end linear_equiv section variables [division_ring K] [add_comm_group V] [module K V] instance finite_dimensional_finsupp {ι : Type*} [fintype ι] [h : finite_dimensional K V] : finite_dimensional K (ι →₀ V) := begin letI : is_noetherian K V := is_noetherian.iff_fg.2 infer_instance, exact (finsupp.linear_equiv_fun_on_fintype K V ι).symm.finite_dimensional end end namespace finite_dimensional section division_ring variables [division_ring K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] /-- Two finite-dimensional vector spaces are isomorphic if they have the same (finite) dimension. -/ theorem nonempty_linear_equiv_of_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂] (cond : finrank K V = finrank K V₂) : nonempty (V ≃ₗ[K] V₂) := nonempty_linear_equiv_of_lift_dim_eq $ by simp only [← finrank_eq_dim, cond, lift_nat_cast] /-- Two finite-dimensional vector spaces are isomorphic if and only if they have the same (finite) dimension. -/ theorem nonempty_linear_equiv_iff_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂] : nonempty (V ≃ₗ[K] V₂) ↔ finrank K V = finrank K V₂ := ⟨λ ⟨h⟩, h.finrank_eq, λ h, nonempty_linear_equiv_of_finrank_eq h⟩ variables (V V₂) /-- Two finite-dimensional vector spaces are isomorphic if they have the same (finite) dimension. -/ noncomputable def linear_equiv.of_finrank_eq [finite_dimensional K V] [finite_dimensional K V₂] (cond : finrank K V = finrank K V₂) : V ≃ₗ[K] V₂ := classical.choice $ nonempty_linear_equiv_of_finrank_eq cond variables {V} lemma eq_of_le_of_finrank_le {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂) (hd : finrank K S₂ ≤ finrank K S₁) : S₁ = S₂ := begin rw ←linear_equiv.finrank_eq (submodule.comap_subtype_equiv_of_le hle) at hd, exact le_antisymm hle (submodule.comap_subtype_eq_top.1 (eq_top_of_finrank_eq (le_antisymm (comap (submodule.subtype S₂) S₁).finrank_le hd))), end /-- If a submodule is less than or equal to a finite-dimensional submodule with the same dimension, they are equal. -/ lemma eq_of_le_of_finrank_eq {S₁ S₂ : submodule K V} [finite_dimensional K S₂] (hle : S₁ ≤ S₂) (hd : finrank K S₁ = finrank K S₂) : S₁ = S₂ := eq_of_le_of_finrank_le hle hd.ge @[simp] lemma finrank_map_subtype_eq (p : submodule K V) (q : submodule K p) : finite_dimensional.finrank K (q.map p.subtype) = finite_dimensional.finrank K q := (submodule.equiv_subtype_map p q).symm.finrank_eq end division_ring section field variables [field K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] variables [finite_dimensional K V] [finite_dimensional K V₂] /-- Given isomorphic subspaces `p q` of vector spaces `V` and `V₁` respectively, `p.quotient` is isomorphic to `q.quotient`. -/ noncomputable def linear_equiv.quot_equiv_of_equiv {p : subspace K V} {q : subspace K V₂} (f₁ : p ≃ₗ[K] q) (f₂ : V ≃ₗ[K] V₂) : (V ⧸ p) ≃ₗ[K] (V₂ ⧸ q) := linear_equiv.of_finrank_eq _ _ begin rw [← @add_right_cancel_iff _ _ (finrank K p), submodule.finrank_quotient_add_finrank, linear_equiv.finrank_eq f₁, submodule.finrank_quotient_add_finrank, linear_equiv.finrank_eq f₂], end /-- Given the subspaces `p q`, if `p.quotient ≃ₗ[K] q`, then `q.quotient ≃ₗ[K] p` -/ noncomputable def linear_equiv.quot_equiv_of_quot_equiv {p q : subspace K V} (f : (V ⧸ p) ≃ₗ[K] q) : (V ⧸ q) ≃ₗ[K] p := linear_equiv.of_finrank_eq _ _ begin rw [← @add_right_cancel_iff _ _ (finrank K q), submodule.finrank_quotient_add_finrank, ← linear_equiv.finrank_eq f, add_comm, submodule.finrank_quotient_add_finrank] end end field end finite_dimensional namespace linear_map open finite_dimensional section division_ring variables [division_ring K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] /-- On a finite-dimensional space, an injective linear map is surjective. -/ lemma surjective_of_injective [finite_dimensional K V] {f : V →ₗ[K] V} (hinj : injective f) : surjective f := begin have h := dim_eq_of_injective _ hinj, rw [← finrank_eq_dim, ← finrank_eq_dim, nat_cast_inj] at h, exact range_eq_top.1 (eq_top_of_finrank_eq h.symm) end /-- The image under an onto linear map of a finite-dimensional space is also finite-dimensional. -/ lemma finite_dimensional_of_surjective [h : finite_dimensional K V] (f : V →ₗ[K] V₂) (hf : f.range = ⊤) : finite_dimensional K V₂ := module.finite.of_surjective f $ range_eq_top.1 hf /-- The range of a linear map defined on a finite-dimensional space is also finite-dimensional. -/ instance finite_dimensional_range [h : finite_dimensional K V] (f : V →ₗ[K] V₂) : finite_dimensional K f.range := f.quot_ker_equiv_range.finite_dimensional /-- The dimensions of the domain and range of an injective linear map are equal. -/ lemma finrank_range_of_inj {f : V →ₗ[K] V₂} (hf : function.injective f) : finrank K f.range = finrank K V := by rw (linear_equiv.of_injective f hf).finrank_eq end division_ring section field variables [field K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] /-- On a finite-dimensional space, a linear map is injective if and only if it is surjective. -/ lemma injective_iff_surjective [finite_dimensional K V] {f : V →ₗ[K] V} : injective f ↔ surjective f := ⟨surjective_of_injective, λ hsurj, let ⟨g, hg⟩ := f.exists_right_inverse_of_surjective (range_eq_top.2 hsurj) in have function.right_inverse g f, from linear_map.ext_iff.1 hg, (left_inverse_of_surjective_of_right_inverse (surjective_of_injective this.injective) this).injective⟩ lemma ker_eq_bot_iff_range_eq_top [finite_dimensional K V] {f : V →ₗ[K] V} : f.ker = ⊥ ↔ f.range = ⊤ := by rw [range_eq_top, ker_eq_bot, injective_iff_surjective] /-- In a finite-dimensional space, if linear maps are inverse to each other on one side then they are also inverse to each other on the other side. -/ lemma mul_eq_one_of_mul_eq_one [finite_dimensional K V] {f g : V →ₗ[K] V} (hfg : f * g = 1) : g * f = 1 := have ginj : injective g, from has_left_inverse.injective ⟨f, (λ x, show (f * g) x = (1 : V →ₗ[K] V) x, by rw hfg; refl)⟩, let ⟨i, hi⟩ := g.exists_right_inverse_of_surjective (range_eq_top.2 (injective_iff_surjective.1 ginj)) in have f * (g * i) = f * 1, from congr_arg _ hi, by rw [← mul_assoc, hfg, one_mul, mul_one] at this; rwa ← this /-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if they are inverse to each other on the other side. -/ lemma mul_eq_one_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f * g = 1 ↔ g * f = 1 := ⟨mul_eq_one_of_mul_eq_one, mul_eq_one_of_mul_eq_one⟩ /-- In a finite-dimensional space, linear maps are inverse to each other on one side if and only if they are inverse to each other on the other side. -/ lemma comp_eq_id_comm [finite_dimensional K V] {f g : V →ₗ[K] V} : f.comp g = id ↔ g.comp f = id := mul_eq_one_comm /-- rank-nullity theorem : the dimensions of the kernel and the range of a linear map add up to the dimension of the source space. -/ theorem finrank_range_add_finrank_ker [finite_dimensional K V] (f : V →ₗ[K] V₂) : finrank K f.range + finrank K f.ker = finrank K V := by { rw [← f.quot_ker_equiv_range.finrank_eq], exact submodule.finrank_quotient_add_finrank _ } end field end linear_map namespace linear_equiv open finite_dimensional variables [field K] [add_comm_group V] [module K V] variables [finite_dimensional K V] /-- The linear equivalence corresponging to an injective endomorphism. -/ noncomputable def of_injective_endo (f : V →ₗ[K] V) (h_inj : injective f) : V ≃ₗ[K] V := linear_equiv.of_bijective f h_inj $ linear_map.injective_iff_surjective.mp h_inj @[simp] lemma coe_of_injective_endo (f : V →ₗ[K] V) (h_inj : injective f) : ⇑(of_injective_endo f h_inj) = f := rfl @[simp] lemma of_injective_endo_right_inv (f : V →ₗ[K] V) (h_inj : injective f) : f * (of_injective_endo f h_inj).symm = 1 := linear_map.ext $ (of_injective_endo f h_inj).apply_symm_apply @[simp] lemma of_injective_endo_left_inv (f : V →ₗ[K] V) (h_inj : injective f) : ((of_injective_endo f h_inj).symm : V →ₗ[K] V) * f = 1 := linear_map.ext $ (of_injective_endo f h_inj).symm_apply_apply end linear_equiv namespace linear_map variables [field K] [add_comm_group V] [module K V] lemma is_unit_iff_ker_eq_bot [finite_dimensional K V] (f : V →ₗ[K] V): is_unit f ↔ f.ker = ⊥ := begin split, { rintro ⟨u, rfl⟩, exact linear_map.ker_eq_bot_of_inverse u.inv_mul }, { intro h_inj, rw ker_eq_bot at h_inj, exact ⟨⟨f, (linear_equiv.of_injective_endo f h_inj).symm.to_linear_map, linear_equiv.of_injective_endo_right_inv f h_inj, linear_equiv.of_injective_endo_left_inv f h_inj⟩, rfl⟩ } end lemma is_unit_iff_range_eq_top [finite_dimensional K V] (f : V →ₗ[K] V): is_unit f ↔ f.range = ⊤ := by rw [is_unit_iff_ker_eq_bot, ker_eq_bot_iff_range_eq_top] end linear_map open module finite_dimensional section variables [division_ring K] [add_comm_group V] [module K V] section top @[simp] theorem finrank_top : finrank K (⊤ : submodule K V) = finrank K V := by { unfold finrank, simp [dim_top] } end top lemma finrank_zero_iff_forall_zero [finite_dimensional K V] : finrank K V = 0 ↔ ∀ x : V, x = 0 := finrank_zero_iff.trans (subsingleton_iff_forall_eq 0) /-- If `ι` is an empty type and `V` is zero-dimensional, there is a unique `ι`-indexed basis. -/ noncomputable def basis_of_finrank_zero [finite_dimensional K V] {ι : Type*} [is_empty ι] (hV : finrank K V = 0) : basis ι K V := begin haveI : subsingleton V := finrank_zero_iff.1 hV, exact basis.empty _ end end namespace linear_map variables [field K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] theorem injective_iff_surjective_of_finrank_eq_finrank [finite_dimensional K V] [finite_dimensional K V₂] (H : finrank K V = finrank K V₂) {f : V →ₗ[K] V₂} : function.injective f ↔ function.surjective f := begin have := finrank_range_add_finrank_ker f, rw [← ker_eq_bot, ← range_eq_top], refine ⟨λ h, _, λ h, _⟩, { rw [h, finrank_bot, add_zero, H] at this, exact eq_top_of_finrank_eq this }, { rw [h, finrank_top, H] at this, exact finrank_eq_zero.1 (add_right_injective _ this) } end lemma ker_eq_bot_iff_range_eq_top_of_finrank_eq_finrank [finite_dimensional K V] [finite_dimensional K V₂] (H : finrank K V = finrank K V₂) {f : V →ₗ[K] V₂} : f.ker = ⊥ ↔ f.range = ⊤ := by rw [range_eq_top, ker_eq_bot, injective_iff_surjective_of_finrank_eq_finrank H] theorem finrank_le_finrank_of_injective [finite_dimensional K V] [finite_dimensional K V₂] {f : V →ₗ[K] V₂} (hf : function.injective f) : finrank K V ≤ finrank K V₂ := calc finrank K V = finrank K f.range + finrank K f.ker : (finrank_range_add_finrank_ker f).symm ... = finrank K f.range : by rw [ker_eq_bot.2 hf, finrank_bot, add_zero] ... ≤ finrank K V₂ : submodule.finrank_le _ /-- Given a linear map `f` between two vector spaces with the same dimension, if `ker f = ⊥` then `linear_equiv_of_injective` is the induced isomorphism between the two vector spaces. -/ noncomputable def linear_equiv_of_injective [finite_dimensional K V] [finite_dimensional K V₂] (f : V →ₗ[K] V₂) (hf : injective f) (hdim : finrank K V = finrank K V₂) : V ≃ₗ[K] V₂ := linear_equiv.of_bijective f hf $ (linear_map.injective_iff_surjective_of_finrank_eq_finrank hdim).mp hf @[simp] lemma linear_equiv_of_injective_apply [finite_dimensional K V] [finite_dimensional K V₂] {f : V →ₗ[K] V₂} (hf : injective f) (hdim : finrank K V = finrank K V₂) (x : V) : f.linear_equiv_of_injective hf hdim x = f x := rfl end linear_map section /-- A domain that is module-finite as an algebra over a field is a division ring. -/ noncomputable def division_ring_of_finite_dimensional (F K : Type*) [field F] [ring K] [is_domain K] [algebra F K] [finite_dimensional F K] : division_ring K := { inv := λ x, if H : x = 0 then 0 else classical.some $ (show function.surjective (linear_map.mul_left F x), from linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' H).1) 1, mul_inv_cancel := λ x hx, show x * dite _ _ _ = _, by { rw dif_neg hx, exact classical.some_spec ((show function.surjective (linear_map.mul_left F x), from linear_map.injective_iff_surjective.1 $ λ _ _, (mul_right_inj' hx).1) 1) }, inv_zero := dif_pos rfl, .. ‹is_domain K›, .. ‹ring K› } /-- An integral domain that is module-finite as an algebra over a field is a field. -/ noncomputable def field_of_finite_dimensional (F K : Type*) [field F] [comm_ring K] [is_domain K] [algebra F K] [finite_dimensional F K] : field K := { .. division_ring_of_finite_dimensional F K, .. ‹comm_ring K› } end namespace submodule section division_ring variables [division_ring K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] lemma lt_of_le_of_finrank_lt_finrank {s t : submodule K V} (le : s ≤ t) (lt : finrank K s < finrank K t) : s < t := lt_of_le_of_ne le (λ h, ne_of_lt lt (by rw h)) lemma lt_top_of_finrank_lt_finrank {s : submodule K V} (lt : finrank K s < finrank K V) : s < ⊤ := begin rw ← @finrank_top K V at lt, exact lt_of_le_of_finrank_lt_finrank le_top lt end lemma finrank_mono [finite_dimensional K V] : monotone (λ (s : submodule K V), finrank K s) := λ s t hst, calc finrank K s = finrank K (comap t.subtype s) : linear_equiv.finrank_eq (comap_subtype_equiv_of_le hst).symm ... ≤ finrank K t : submodule.finrank_le _ end division_ring section field variables [field K] [add_comm_group V] [module K V] {V₂ : Type v'} [add_comm_group V₂] [module K V₂] lemma finrank_lt_finrank_of_lt [finite_dimensional K V] {s t : submodule K V} (hst : s < t) : finrank K s < finrank K t := begin rw linear_equiv.finrank_eq (comap_subtype_equiv_of_le (le_of_lt hst)).symm, refine finrank_lt (lt_of_le_of_ne le_top _), intro h_eq_top, rw comap_subtype_eq_top at h_eq_top, apply not_le_of_lt hst h_eq_top, end lemma finrank_add_eq_of_is_compl [finite_dimensional K V] {U W : submodule K V} (h : is_compl U W) : finrank K U + finrank K W = finrank K V := begin rw [← submodule.dim_sup_add_dim_inf_eq, top_le_iff.1 h.2, le_bot_iff.1 h.1, finrank_bot, add_zero], exact finrank_top end end field end submodule section span open submodule section division_ring variables [division_ring K] [add_comm_group V] [module K V] variable (K) /-- The rank of a set of vectors as a natural number. -/ protected noncomputable def set.finrank (s : set V) : ℕ := finrank K (span K s) variable {K} lemma finrank_span_le_card (s : set V) [fintype s] : finrank K (span K s) ≤ s.to_finset.card := begin haveI := span_of_finite K s.to_finite, have : module.rank K (span K s) ≤ #s := dim_span_le s, rw [←finrank_eq_dim, cardinal.mk_fintype, ←set.to_finset_card] at this, exact_mod_cast this, end lemma finrank_span_finset_le_card (s : finset V) : (s : set V).finrank K ≤ s.card := calc (s : set V).finrank K ≤ (s : set V).to_finset.card : finrank_span_le_card s ... = s.card : by simp lemma finrank_range_le_card {ι : Type*} [fintype ι] {b : ι → V} : (set.range b).finrank K ≤ fintype.card ι := (finrank_span_le_card _).trans $ by { rw set.to_finset_range, exact finset.card_image_le } lemma finrank_span_eq_card {ι : Type*} [fintype ι] {b : ι → V} (hb : linear_independent K b) : finrank K (span K (set.range b)) = fintype.card ι := begin haveI : finite_dimensional K (span K (set.range b)) := span_of_finite K (set.finite_range b), have : module.rank K (span K (set.range b)) = #(set.range b) := dim_span hb, rwa [←finrank_eq_dim, ←lift_inj, mk_range_eq_of_injective hb.injective, cardinal.mk_fintype, lift_nat_cast, lift_nat_cast, nat_cast_inj] at this, end lemma finrank_span_set_eq_card (s : set V) [fintype s] (hs : linear_independent K (coe : s → V)) : finrank K (span K s) = s.to_finset.card := begin haveI := span_of_finite K s.to_finite, have : module.rank K (span K s) = #s := dim_span_set hs, rw [←finrank_eq_dim, cardinal.mk_fintype, ←set.to_finset_card] at this, exact_mod_cast this, end lemma finrank_span_finset_eq_card (s : finset V) (hs : linear_independent K (coe : s → V)) : finrank K (span K (s : set V)) = s.card := begin convert finrank_span_set_eq_card ↑s hs, ext, simp, end lemma span_lt_of_subset_of_card_lt_finrank {s : set V} [fintype s] {t : submodule K V} (subset : s ⊆ t) (card_lt : s.to_finset.card < finrank K t) : span K s < t := lt_of_le_of_finrank_lt_finrank (span_le.mpr subset) (lt_of_le_of_lt (finrank_span_le_card _) card_lt) lemma span_lt_top_of_card_lt_finrank {s : set V} [fintype s] (card_lt : s.to_finset.card < finrank K V) : span K s < ⊤ := lt_top_of_finrank_lt_finrank (lt_of_le_of_lt (finrank_span_le_card _) card_lt) lemma finrank_span_singleton {v : V} (hv : v ≠ 0) : finrank K (K ∙ v) = 1 := begin apply le_antisymm, { exact finrank_span_le_card ({v} : set V) }, { rw [nat.succ_le_iff, finrank_pos_iff], use [⟨v, mem_span_singleton_self v⟩, 0], simp [hv] } end end division_ring section field variables [field K] [add_comm_group V] [module K V] lemma set.finrank_mono [finite_dimensional K V] {s t : set V} (h : s ⊆ t) : s.finrank K ≤ t.finrank K := finrank_mono (span_mono h) end field end span section basis section division_ring variables [division_ring K] [add_comm_group V] [module K V] lemma linear_independent_of_top_le_span_of_card_eq_finrank {ι : Type*} [fintype ι] {b : ι → V} (spans : ⊤ ≤ span K (set.range b)) (card_eq : fintype.card ι = finrank K V) : linear_independent K b := linear_independent_iff'.mpr $ λ s g dependent i i_mem_s, begin by_contra gx_ne_zero, -- We'll derive a contradiction by showing `b '' (univ \ {i})` of cardinality `n - 1` -- spans a vector space of dimension `n`. refine not_le_of_gt (span_lt_top_of_card_lt_finrank (show (b '' (set.univ \ {i})).to_finset.card < finrank K V, from _)) _, { calc (b '' (set.univ \ {i})).to_finset.card = ((set.univ \ {i}).to_finset.image b).card : by rw [set.to_finset_card, fintype.card_of_finset] ... ≤ (set.univ \ {i}).to_finset.card : finset.card_image_le ... = (finset.univ.erase i).card : congr_arg finset.card (finset.ext (by simp [and_comm])) ... < finset.univ.card : finset.card_erase_lt_of_mem (finset.mem_univ i) ... = finrank K V : card_eq }, -- We already have that `b '' univ` spans the whole space, -- so we only need to show that the span of `b '' (univ \ {i})` contains each `b j`. refine spans.trans (span_le.mpr _), rintros _ ⟨j, rfl, rfl⟩, -- The case that `j ≠ i` is easy because `b j ∈ b '' (univ \ {i})`. by_cases j_eq : j = i, swap, { refine subset_span ⟨j, (set.mem_diff _).mpr ⟨set.mem_univ _, _⟩, rfl⟩, exact mt set.mem_singleton_iff.mp j_eq }, -- To show `b i ∈ span (b '' (univ \ {i}))`, we use that it's a weighted sum -- of the other `b j`s. rw [j_eq, set_like.mem_coe, show b i = -((g i)⁻¹ • (s.erase i).sum (λ j, g j • b j)), from _], { refine neg_mem (smul_mem _ _ (sum_mem (λ k hk, _))), obtain ⟨k_ne_i, k_mem⟩ := finset.mem_erase.mp hk, refine smul_mem _ _ (subset_span ⟨k, _, rfl⟩), simpa using k_mem }, -- To show `b i` is a weighted sum of the other `b j`s, we'll rewrite this sum -- to have the form of the assumption `dependent`. apply eq_neg_of_add_eq_zero_left, calc b i + (g i)⁻¹ • (s.erase i).sum (λ j, g j • b j) = (g i)⁻¹ • (g i • b i + (s.erase i).sum (λ j, g j • b j)) : by rw [smul_add, ←mul_smul, inv_mul_cancel gx_ne_zero, one_smul] ... = (g i)⁻¹ • 0 : congr_arg _ _ ... = 0 : smul_zero _, -- And then it's just a bit of manipulation with finite sums. rwa [← finset.insert_erase i_mem_s, finset.sum_insert (finset.not_mem_erase _ _)] at dependent end /-- A finite family of vectors is linearly independent if and only if its cardinality equals the dimension of its span. -/ lemma linear_independent_iff_card_eq_finrank_span {ι : Type*} [fintype ι] {b : ι → V} : linear_independent K b ↔ fintype.card ι = (set.range b).finrank K := begin split, { intro h, exact (finrank_span_eq_card h).symm }, { intro hc, let f := (submodule.subtype (span K (set.range b))), let b' : ι → span K (set.range b) := λ i, ⟨b i, mem_span.2 (λ p hp, hp (set.mem_range_self _))⟩, have hs : ⊤ ≤ span K (set.range b'), { intro x, have h : span K (f '' (set.range b')) = map f (span K (set.range b')) := span_image f, have hf : f '' (set.range b') = set.range b, { ext x, simp [set.mem_image, set.mem_range] }, rw hf at h, have hx : (x : V) ∈ span K (set.range b) := x.property, conv at hx { congr, skip, rw h }, simpa [mem_map] using hx }, have hi : f.ker = ⊥ := ker_subtype _, convert (linear_independent_of_top_le_span_of_card_eq_finrank hs hc).map' _ hi } end lemma linear_independent_iff_card_le_finrank_span {ι : Type*} [fintype ι] {b : ι → V} : linear_independent K b ↔ fintype.card ι ≤ (set.range b).finrank K := by rw [linear_independent_iff_card_eq_finrank_span, finrank_range_le_card.le_iff_eq] /-- A family of `finrank K V` vectors forms a basis if they span the whole space. -/ noncomputable def basis_of_top_le_span_of_card_eq_finrank {ι : Type*} [fintype ι] (b : ι → V) (le_span : ⊤ ≤ span K (set.range b)) (card_eq : fintype.card ι = finrank K V) : basis ι K V := basis.mk (linear_independent_of_top_le_span_of_card_eq_finrank le_span card_eq) le_span @[simp] lemma coe_basis_of_top_le_span_of_card_eq_finrank {ι : Type*} [fintype ι] (b : ι → V) (le_span : ⊤ ≤ span K (set.range b)) (card_eq : fintype.card ι = finrank K V) : ⇑(basis_of_top_le_span_of_card_eq_finrank b le_span card_eq) = b := basis.coe_mk _ _ /-- A finset of `finrank K V` vectors forms a basis if they span the whole space. -/ @[simps] noncomputable def finset_basis_of_top_le_span_of_card_eq_finrank {s : finset V} (le_span : ⊤ ≤ span K (s : set V)) (card_eq : s.card = finrank K V) : basis (s : set V) K V := basis_of_top_le_span_of_card_eq_finrank (coe : (s : set V) → V) ((@subtype.range_coe_subtype _ (λ x, x ∈ s)).symm ▸ le_span) (trans (fintype.card_coe _) card_eq) /-- A set of `finrank K V` vectors forms a basis if they span the whole space. -/ @[simps] noncomputable def set_basis_of_top_le_span_of_card_eq_finrank {s : set V} [fintype s] (le_span : ⊤ ≤ span K s) (card_eq : s.to_finset.card = finrank K V) : basis s K V := basis_of_top_le_span_of_card_eq_finrank (coe : s → V) ((@subtype.range_coe_subtype _ s).symm ▸ le_span) (trans s.to_finset_card.symm card_eq) end division_ring section field variables [field K] [add_comm_group V] [module K V] lemma span_eq_top_of_linear_independent_of_card_eq_finrank {ι : Type*} [hι : nonempty ι] [fintype ι] {b : ι → V} (lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) : span K (set.range b) = ⊤ := begin by_cases fin : (finite_dimensional K V), { haveI := fin, by_contra ne_top, have lt_top : span K (set.range b) < ⊤ := lt_of_le_of_ne le_top ne_top, exact ne_of_lt (submodule.finrank_lt lt_top) (trans (finrank_span_eq_card lin_ind) card_eq) }, { exfalso, apply ne_of_lt (fintype.card_pos_iff.mpr hι), symmetry, replace fin := (not_iff_not.2 is_noetherian.iff_fg).2 fin, calc fintype.card ι = finrank K V : card_eq ... = 0 : dif_neg (mt is_noetherian.iff_dim_lt_aleph_0.mpr fin) } end /-- A linear independent family of `finrank K V` vectors forms a basis. -/ @[simps] noncomputable def basis_of_linear_independent_of_card_eq_finrank {ι : Type*} [nonempty ι] [fintype ι] {b : ι → V} (lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) : basis ι K V := basis.mk lin_ind $ (span_eq_top_of_linear_independent_of_card_eq_finrank lin_ind card_eq).ge @[simp] lemma coe_basis_of_linear_independent_of_card_eq_finrank {ι : Type*} [nonempty ι] [fintype ι] {b : ι → V} (lin_ind : linear_independent K b) (card_eq : fintype.card ι = finrank K V) : ⇑(basis_of_linear_independent_of_card_eq_finrank lin_ind card_eq) = b := basis.coe_mk _ _ /-- A linear independent finset of `finrank K V` vectors forms a basis. -/ @[simps] noncomputable def finset_basis_of_linear_independent_of_card_eq_finrank {s : finset V} (hs : s.nonempty) (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.card = finrank K V) : basis s K V := @basis_of_linear_independent_of_card_eq_finrank _ _ _ _ _ _ ⟨(⟨hs.some, hs.some_spec⟩ : s)⟩ _ _ lin_ind (trans (fintype.card_coe _) card_eq) @[simp] lemma coe_finset_basis_of_linear_independent_of_card_eq_finrank {s : finset V} (hs : s.nonempty) (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.card = finrank K V) : ⇑(finset_basis_of_linear_independent_of_card_eq_finrank hs lin_ind card_eq) = coe := basis.coe_mk _ _ /-- A linear independent set of `finrank K V` vectors forms a basis. -/ @[simps] noncomputable def set_basis_of_linear_independent_of_card_eq_finrank {s : set V} [nonempty s] [fintype s] (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.to_finset.card = finrank K V) : basis s K V := basis_of_linear_independent_of_card_eq_finrank lin_ind (trans s.to_finset_card.symm card_eq) @[simp] lemma coe_set_basis_of_linear_independent_of_card_eq_finrank {s : set V} [nonempty s] [fintype s] (lin_ind : linear_independent K (coe : s → V)) (card_eq : s.to_finset.card = finrank K V) : ⇑(set_basis_of_linear_independent_of_card_eq_finrank lin_ind card_eq) = coe := basis.coe_mk _ _ end field end basis /-! We now give characterisations of `finrank K V = 1` and `finrank K V ≤ 1`. -/ section finrank_eq_one variables [division_ring K] [add_comm_group V] [module K V] /-- If there is a nonzero vector and every other vector is a multiple of it, then the module has dimension one. -/ lemma finrank_eq_one (v : V) (n : v ≠ 0) (h : ∀ w : V, ∃ c : K, c • v = w) : finrank K V = 1 := begin obtain ⟨b⟩ := (basis.basis_singleton_iff punit).mpr ⟨v, n, h⟩, rw [finrank_eq_card_basis b, fintype.card_punit] end /-- If every vector is a multiple of some `v : V`, then `V` has dimension at most one. -/ lemma finrank_le_one (v : V) (h : ∀ w : V, ∃ c : K, c • v = w) : finrank K V ≤ 1 := begin rcases eq_or_ne v 0 with rfl | hn, { haveI := subsingleton_of_forall_eq (0 : V) (λ w, by { obtain ⟨c, rfl⟩ := h w, simp }), rw finrank_zero_of_subsingleton, exact zero_le_one }, { exact (finrank_eq_one v hn h).le } end /-- A vector space with a nonzero vector `v` has dimension 1 iff `v` spans. -/ lemma finrank_eq_one_iff_of_nonzero (v : V) (nz : v ≠ 0) : finrank K V = 1 ↔ span K ({v} : set V) = ⊤ := ⟨λ h, by simpa using (basis_singleton punit h v nz).span_eq, λ s, finrank_eq_card_basis (basis.mk (linear_independent_singleton nz) (by { convert s, simp }))⟩ /-- A module with a nonzero vector `v` has dimension 1 iff every vector is a multiple of `v`. -/ lemma finrank_eq_one_iff_of_nonzero' (v : V) (nz : v ≠ 0) : finrank K V = 1 ↔ ∀ w : V, ∃ c : K, c • v = w := begin rw finrank_eq_one_iff_of_nonzero v nz, apply span_singleton_eq_top_iff, end /-- A module has dimension 1 iff there is some `v : V` so `{v}` is a basis. -/ lemma finrank_eq_one_iff (ι : Type*) [unique ι] : finrank K V = 1 ↔ nonempty (basis ι K V) := begin fsplit, { intro h, haveI := finite_dimensional_of_finrank (_root_.zero_lt_one.trans_le h.symm.le), exact ⟨basis_unique ι h⟩ }, { rintro ⟨b⟩, simpa using finrank_eq_card_basis b } end /-- A module has dimension 1 iff there is some nonzero `v : V` so every vector is a multiple of `v`. -/ lemma finrank_eq_one_iff' : finrank K V = 1 ↔ ∃ (v : V) (n : v ≠ 0), ∀ w : V, ∃ c : K, c • v = w := begin convert finrank_eq_one_iff punit, simp only [exists_prop, eq_iff_iff, ne.def], convert (basis.basis_singleton_iff punit).symm, funext v, simp, apply_instance, apply_instance, -- Not sure why this aren't found automatically. end /-- A finite dimensional module has dimension at most 1 iff there is some `v : V` so every vector is a multiple of `v`. -/ lemma finrank_le_one_iff [finite_dimensional K V] : finrank K V ≤ 1 ↔ ∃ (v : V), ∀ w : V, ∃ c : K, c • v = w := begin fsplit, { intro h, by_cases h' : finrank K V = 0, { use 0, intro w, use 0, haveI := finrank_zero_iff.mp h', apply subsingleton.elim, }, { replace h' := zero_lt_iff.mpr h', have : finrank K V = 1, { linarith }, obtain ⟨v, -, p⟩ := finrank_eq_one_iff'.mp this, use ⟨v, p⟩, }, }, { rintro ⟨v, p⟩, exact finrank_le_one v p, } end lemma submodule.finrank_le_one_iff_is_principal (W : submodule K V) [finite_dimensional K W] : finite_dimensional.finrank K W ≤ 1 ↔ W.is_principal := by rw [← W.rank_le_one_iff_is_principal, ← finite_dimensional.finrank_eq_dim, ← cardinal.nat_cast_le, nat.cast_one] lemma module.finrank_le_one_iff_top_is_principal [finite_dimensional K V] : finite_dimensional.finrank K V ≤ 1 ↔ (⊤ : submodule K V).is_principal := by rw [← module.rank_le_one_iff_top_is_principal, ← finite_dimensional.finrank_eq_dim, ← cardinal.nat_cast_le, nat.cast_one] -- We use the `linear_map.compatible_smul` typeclass here, to encompass two situations: -- * `A = K` -- * `[field K] [algebra K A] [is_scalar_tower K A V] [is_scalar_tower K A W]` lemma surjective_of_nonzero_of_finrank_eq_one {K : Type*} [division_ring K] {A : Type*} [semiring A] [module K V] [module A V] {W : Type*} [add_comm_group W] [module K W] [module A W] [linear_map.compatible_smul V W K A] (h : finrank K W = 1) {f : V →ₗ[A] W} (w : f ≠ 0) : surjective f := begin change surjective (f.restrict_scalars K), obtain ⟨v, n⟩ := fun_like.ne_iff.mp w, intro z, obtain ⟨c, rfl⟩ := (finrank_eq_one_iff_of_nonzero' (f v) n).mp h z, exact ⟨c • v, by simp⟩, end end finrank_eq_one section subalgebra_dim open module variables {F E : Type*} [field F] [field E] [algebra F E] lemma subalgebra.dim_eq_one_of_eq_bot {S : subalgebra F E} (h : S = ⊥) : module.rank F S = 1 := begin rw [← S.to_submodule_equiv.dim_eq, h, (linear_equiv.of_eq (⊥ : subalgebra F E).to_submodule _ algebra.to_submodule_bot).dim_eq, dim_span_set], exacts [mk_singleton _, linear_independent_singleton one_ne_zero] end @[simp] lemma subalgebra.dim_bot : module.rank F (⊥ : subalgebra F E) = 1 := subalgebra.dim_eq_one_of_eq_bot rfl lemma subalgebra_top_dim_eq_submodule_top_dim : module.rank F (⊤ : subalgebra F E) = module.rank F (⊤ : submodule F E) := by { rw ← algebra.top_to_submodule, refl } lemma subalgebra_top_finrank_eq_submodule_top_finrank : finrank F (⊤ : subalgebra F E) = finrank F (⊤ : submodule F E) := by { rw ← algebra.top_to_submodule, refl } lemma subalgebra.dim_top : module.rank F (⊤ : subalgebra F E) = module.rank F E := by { rw subalgebra_top_dim_eq_submodule_top_dim, exact dim_top F E } instance subalgebra.finite_dimensional_bot : finite_dimensional F (⊥ : subalgebra F E) := finite_dimensional_of_dim_eq_one subalgebra.dim_bot @[simp] lemma subalgebra.finrank_bot : finrank F (⊥ : subalgebra F E) = 1 := begin have : module.rank F (⊥ : subalgebra F E) = 1 := subalgebra.dim_bot, rw ← finrank_eq_dim at this, norm_cast at *, simp *, end lemma subalgebra.finrank_eq_one_of_eq_bot {S : subalgebra F E} (h : S = ⊥) : finrank F S = 1 := by { rw h, exact subalgebra.finrank_bot } lemma subalgebra.eq_bot_of_finrank_one {S : subalgebra F E} (h : finrank F S = 1) : S = ⊥ := begin rw eq_bot_iff, let b : set S := {1}, have : fintype b := unique.fintype, have b_lin_ind : linear_independent F (coe : b → S) := linear_independent_singleton one_ne_zero, have b_card : fintype.card b = 1 := fintype.card_of_subsingleton _, let hb := set_basis_of_linear_independent_of_card_eq_finrank b_lin_ind (by simp only [*, set.to_finset_card]), have b_spans := hb.span_eq, intros x hx, rw [algebra.mem_bot], have x_in_span_b : (⟨x, hx⟩ : S) ∈ submodule.span F b, { rw [coe_set_basis_of_linear_independent_of_card_eq_finrank, subtype.range_coe] at b_spans, rw b_spans, exact submodule.mem_top, }, obtain ⟨a, ha⟩ := submodule.mem_span_singleton.mp x_in_span_b, replace ha : a • 1 = x := by injections with ha, exact ⟨a, by rw [← ha, algebra.smul_def, mul_one]⟩, end lemma subalgebra.eq_bot_of_dim_one {S : subalgebra F E} (h : module.rank F S = 1) : S = ⊥ := begin haveI : finite_dimensional F S := finite_dimensional_of_dim_eq_one h, rw ← finrank_eq_dim at h, norm_cast at h, exact subalgebra.eq_bot_of_finrank_one h, end @[simp] lemma subalgebra.bot_eq_top_of_dim_eq_one (h : module.rank F E = 1) : (⊥ : subalgebra F E) = ⊤ := begin rw [← dim_top, ← subalgebra_top_dim_eq_submodule_top_dim] at h, exact eq.symm (subalgebra.eq_bot_of_dim_one h), end @[simp] lemma subalgebra.bot_eq_top_of_finrank_eq_one (h : finrank F E = 1) : (⊥ : subalgebra F E) = ⊤ := begin rw [← finrank_top, ← subalgebra_top_finrank_eq_submodule_top_finrank] at h, exact eq.symm (subalgebra.eq_bot_of_finrank_one h), end @[simp] theorem subalgebra.dim_eq_one_iff {S : subalgebra F E} : module.rank F S = 1 ↔ S = ⊥ := ⟨subalgebra.eq_bot_of_dim_one, subalgebra.dim_eq_one_of_eq_bot⟩ @[simp] theorem subalgebra.finrank_eq_one_iff {S : subalgebra F E} : finrank F S = 1 ↔ S = ⊥ := ⟨subalgebra.eq_bot_of_finrank_one, subalgebra.finrank_eq_one_of_eq_bot⟩ end subalgebra_dim namespace module namespace End variables [field K] [add_comm_group V] [module K V] lemma exists_ker_pow_eq_ker_pow_succ [finite_dimensional K V] (f : End K V) : ∃ (k : ℕ), k ≤ finrank K V ∧ (f ^ k).ker = (f ^ k.succ).ker := begin classical, by_contradiction h_contra, simp_rw [not_exists, not_and] at h_contra, have h_le_ker_pow : ∀ (n : ℕ), n ≤ (finrank K V).succ → n ≤ finrank K (f ^ n).ker, { intros n hn, induction n with n ih, { exact zero_le (finrank _ _) }, { have h_ker_lt_ker : (f ^ n).ker < (f ^ n.succ).ker, { refine lt_of_le_of_ne _ (h_contra n (nat.le_of_succ_le_succ hn)), rw pow_succ, apply linear_map.ker_le_ker_comp }, have h_finrank_lt_finrank : finrank K (f ^ n).ker < finrank K (f ^ n.succ).ker, { apply submodule.finrank_lt_finrank_of_lt h_ker_lt_ker }, calc n.succ ≤ (finrank K ↥(linear_map.ker (f ^ n))).succ : nat.succ_le_succ (ih (nat.le_of_succ_le hn)) ... ≤ finrank K ↥(linear_map.ker (f ^ n.succ)) : nat.succ_le_of_lt h_finrank_lt_finrank } }, have h_le_finrank_V : ∀ n, finrank K (f ^ n).ker ≤ finrank K V := λ n, submodule.finrank_le _, have h_any_n_lt: ∀ n, n ≤ (finrank K V).succ → n ≤ finrank K V := λ n hn, (h_le_ker_pow n hn).trans (h_le_finrank_V n), show false, from nat.not_succ_le_self _ (h_any_n_lt (finrank K V).succ (finrank K V).succ.le_refl), end lemma ker_pow_constant {f : End K V} {k : ℕ} (h : (f ^ k).ker = (f ^ k.succ).ker) : ∀ m, (f ^ k).ker = (f ^ (k + m)).ker | 0 := by simp | (m + 1) := begin apply le_antisymm, { rw [add_comm, pow_add], apply linear_map.ker_le_ker_comp }, { rw [ker_pow_constant m, add_comm m 1, ←add_assoc, pow_add, pow_add f k m], change linear_map.ker ((f ^ (k + 1)).comp (f ^ m)) ≤ linear_map.ker ((f ^ k).comp (f ^ m)), rw [linear_map.ker_comp, linear_map.ker_comp, h, nat.add_one], exact le_rfl, } end lemma ker_pow_eq_ker_pow_finrank_of_le [finite_dimensional K V] {f : End K V} {m : ℕ} (hm : finrank K V ≤ m) : (f ^ m).ker = (f ^ finrank K V).ker := begin obtain ⟨k, h_k_le, hk⟩ : ∃ k, k ≤ finrank K V ∧ linear_map.ker (f ^ k) = linear_map.ker (f ^ k.succ) := exists_ker_pow_eq_ker_pow_succ f, calc (f ^ m).ker = (f ^ (k + (m - k))).ker : by rw add_tsub_cancel_of_le (h_k_le.trans hm) ... = (f ^ k).ker : by rw ker_pow_constant hk _ ... = (f ^ (k + (finrank K V - k))).ker : ker_pow_constant hk (finrank K V - k) ... = (f ^ finrank K V).ker : by rw add_tsub_cancel_of_le h_k_le end lemma ker_pow_le_ker_pow_finrank [finite_dimensional K V] (f : End K V) (m : ℕ) : (f ^ m).ker ≤ (f ^ finrank K V).ker := begin by_cases h_cases: m < finrank K V, { rw [←add_tsub_cancel_of_le (nat.le_of_lt h_cases), add_comm, pow_add], apply linear_map.ker_le_ker_comp }, { rw [ker_pow_eq_ker_pow_finrank_of_le (le_of_not_lt h_cases)], exact le_rfl } end end End end module
0e8f56cfd38fef13a2191d0e7addffa323d26122
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch6/ex0301.lean
d92c12727f850cb7453f5968cf84cd72cb8a5951
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
81
lean
namespace foo def bar : ℕ := 1 end foo open foo #check bar #check foo.bar
12db88e2c6ad426ac50161576e6266bd31bc223d
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/ring_theory/discrete_valuation_ring.lean
b97fe3a6ba47f0545dcd049da977251409d5553f
[ "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
17,482
lean
/- Copyright (c) 2020 Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard -/ import ring_theory.principal_ideal_domain import ring_theory.ideal.local_ring import ring_theory.multiplicity import ring_theory.valuation.basic import linear_algebra.adic_completion /-! # Discrete valuation rings This file defines discrete valuation rings (DVRs) and develops a basic interface for them. ## Important definitions There are various definitions of a DVR in the literature; we define a DVR to be a local PID which is not a field (the first definition in Wikipedia) and prove that this is equivalent to being a PID with a unique non-zero prime ideal (the definition in Serre's book "Local Fields"). Let R be an integral domain, assumed to be a principal ideal ring and a local ring. * `discrete_valuation_ring R` : a predicate expressing that R is a DVR ### Definitions * `add_val R : add_valuation R part_enat` : the additive valuation on a DVR. ## Implementation notes It's a theorem that an element of a DVR is a uniformizer if and only if it's irreducible. We do not hence define `uniformizer` at all, because we can use `irreducible` instead. ## Tags discrete valuation ring -/ open_locale classical universe u open ideal local_ring /-- An integral domain is a *discrete valuation ring* (DVR) if it's a local PID which is not a field. -/ class discrete_valuation_ring (R : Type u) [comm_ring R] [is_domain R] extends is_principal_ideal_ring R, local_ring R : Prop := (not_a_field' : maximal_ideal R ≠ ⊥) namespace discrete_valuation_ring variables (R : Type u) [comm_ring R] [is_domain R] [discrete_valuation_ring R] lemma not_a_field : maximal_ideal R ≠ ⊥ := not_a_field' /-- A discrete valuation ring `R` is not a field. -/ lemma not_is_field : ¬ is_field R := ring.not_is_field_iff_exists_prime.mpr ⟨_, not_a_field R, is_maximal.is_prime' (maximal_ideal R)⟩ variable {R} open principal_ideal_ring theorem irreducible_of_span_eq_maximal_ideal {R : Type*} [comm_ring R] [local_ring R] [is_domain R] (ϖ : R) (hϖ : ϖ ≠ 0) (h : maximal_ideal R = ideal.span {ϖ}) : irreducible ϖ := begin have h2 : ¬(is_unit ϖ) := show ϖ ∈ maximal_ideal R, from h.symm ▸ submodule.mem_span_singleton_self ϖ, refine ⟨h2, _⟩, intros a b hab, by_contra' h, obtain ⟨ha : a ∈ maximal_ideal R, hb : b ∈ maximal_ideal R⟩ := h, rw [h, mem_span_singleton'] at ha hb, rcases ha with ⟨a, rfl⟩, rcases hb with ⟨b, rfl⟩, rw (show a * ϖ * (b * ϖ) = ϖ * (ϖ * (a * b)), by ring) at hab, apply hϖ, apply eq_zero_of_mul_eq_self_right _ hab.symm, exact λ hh, h2 (is_unit_of_dvd_one ϖ ⟨_, hh.symm⟩) end /-- An element of a DVR is irreducible iff it is a uniformizer, that is, generates the maximal ideal of R -/ theorem irreducible_iff_uniformizer (ϖ : R) : irreducible ϖ ↔ maximal_ideal R = ideal.span {ϖ} := ⟨λ hϖ, (eq_maximal_ideal (is_maximal_of_irreducible hϖ)).symm, λ h, irreducible_of_span_eq_maximal_ideal ϖ (λ e, not_a_field R $ by rwa [h, span_singleton_eq_bot]) h⟩ lemma _root_.irreducible.maximal_ideal_eq {ϖ : R} (h : irreducible ϖ) : maximal_ideal R = ideal.span {ϖ} := (irreducible_iff_uniformizer _).mp h variable (R) /-- Uniformisers exist in a DVR -/ theorem exists_irreducible : ∃ ϖ : R, irreducible ϖ := by {simp_rw [irreducible_iff_uniformizer], exact (is_principal_ideal_ring.principal $ maximal_ideal R).principal} /-- Uniformisers exist in a DVR -/ theorem exists_prime : ∃ ϖ : R, prime ϖ := (exists_irreducible R).imp (λ _, principal_ideal_ring.irreducible_iff_prime.1) /-- an integral domain is a DVR iff it's a PID with a unique non-zero prime ideal -/ theorem iff_pid_with_one_nonzero_prime (R : Type u) [comm_ring R] [is_domain R] : discrete_valuation_ring R ↔ is_principal_ideal_ring R ∧ ∃! P : ideal R, P ≠ ⊥ ∧ is_prime P := begin split, { intro RDVR, rcases id RDVR with ⟨Rlocal⟩, split, assumption, resetI, use local_ring.maximal_ideal R, split, split, { assumption }, { apply_instance } , { rintro Q ⟨hQ1, hQ2⟩, obtain ⟨q, rfl⟩ := (is_principal_ideal_ring.principal Q).1, have hq : q ≠ 0, { rintro rfl, apply hQ1, simp }, erw span_singleton_prime hq at hQ2, replace hQ2 := hQ2.irreducible, rw irreducible_iff_uniformizer at hQ2, exact hQ2.symm } }, { rintro ⟨RPID, Punique⟩, haveI : local_ring R := local_ring.of_unique_nonzero_prime Punique, refine {not_a_field' := _}, rcases Punique with ⟨P, ⟨hP1, hP2⟩, hP3⟩, have hPM : P ≤ maximal_ideal R := le_maximal_ideal (hP2.1), intro h, rw [h, le_bot_iff] at hPM, exact hP1 hPM } end lemma associated_of_irreducible {a b : R} (ha : irreducible a) (hb : irreducible b) : associated a b := begin rw irreducible_iff_uniformizer at ha hb, rw [←span_singleton_eq_span_singleton, ←ha, hb], end end discrete_valuation_ring namespace discrete_valuation_ring variable (R : Type*) /-- Alternative characterisation of discrete valuation rings. -/ def has_unit_mul_pow_irreducible_factorization [comm_ring R] : Prop := ∃ p : R, irreducible p ∧ ∀ {x : R}, x ≠ 0 → ∃ (n : ℕ), associated (p ^ n) x namespace has_unit_mul_pow_irreducible_factorization variables {R} [comm_ring R] (hR : has_unit_mul_pow_irreducible_factorization R) include hR lemma unique_irreducible ⦃p q : R⦄ (hp : irreducible p) (hq : irreducible q) : associated p q := begin rcases hR with ⟨ϖ, hϖ, hR⟩, suffices : ∀ {p : R} (hp : irreducible p), associated p ϖ, { apply associated.trans (this hp) (this hq).symm, }, clear hp hq p q, intros p hp, obtain ⟨n, hn⟩ := hR hp.ne_zero, have : irreducible (ϖ ^ n) := hn.symm.irreducible hp, rcases lt_trichotomy n 1 with (H|rfl|H), { obtain rfl : n = 0, { clear hn this, revert H n, exact dec_trivial }, simpa only [not_irreducible_one, pow_zero] using this, }, { simpa only [pow_one] using hn.symm, }, { obtain ⟨n, rfl⟩ : ∃ k, n = 1 + k + 1 := nat.exists_eq_add_of_lt H, rw pow_succ at this, rcases this.is_unit_or_is_unit rfl with H0|H0, { exact (hϖ.not_unit H0).elim, }, { rw [add_comm, pow_succ] at H0, exact (hϖ.not_unit (is_unit_of_mul_is_unit_left H0)).elim } } end variables [is_domain R] /-- An integral domain in which there is an irreducible element `p` such that every nonzero element is associated to a power of `p` is a unique factorization domain. See `discrete_valuation_ring.of_has_unit_mul_pow_irreducible_factorization`. -/ theorem to_unique_factorization_monoid : unique_factorization_monoid R := let p := classical.some hR in let spec := classical.some_spec hR in unique_factorization_monoid.of_exists_prime_factors $ λ x hx, begin use multiset.repeat p (classical.some (spec.2 hx)), split, { intros q hq, have hpq := multiset.eq_of_mem_repeat hq, rw hpq, refine ⟨spec.1.ne_zero, spec.1.not_unit, _⟩, intros a b h, by_cases ha : a = 0, { rw ha, simp only [true_or, dvd_zero], }, obtain ⟨m, u, rfl⟩ := spec.2 ha, rw [mul_assoc, mul_left_comm, is_unit.dvd_mul_left _ _ _ (units.is_unit _)] at h, rw is_unit.dvd_mul_right (units.is_unit _), by_cases hm : m = 0, { simp only [hm, one_mul, pow_zero] at h ⊢, right, exact h }, left, obtain ⟨m, rfl⟩ := nat.exists_eq_succ_of_ne_zero hm, rw pow_succ, apply dvd_mul_of_dvd_left dvd_rfl _ }, { rw [multiset.prod_repeat], exact (classical.some_spec (spec.2 hx)), } end omit hR lemma of_ufd_of_unique_irreducible [unique_factorization_monoid R] (h₁ : ∃ p : R, irreducible p) (h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) : has_unit_mul_pow_irreducible_factorization R := begin obtain ⟨p, hp⟩ := h₁, refine ⟨p, hp, _⟩, intros x hx, cases wf_dvd_monoid.exists_factors x hx with fx hfx, refine ⟨fx.card, _⟩, have H := hfx.2, rw ← associates.mk_eq_mk_iff_associated at H ⊢, rw [← H, ← associates.prod_mk, associates.mk_pow, ← multiset.prod_repeat], congr' 1, symmetry, rw multiset.eq_repeat, simp only [true_and, and_imp, multiset.card_map, eq_self_iff_true, multiset.mem_map, exists_imp_distrib], rintros _ q hq rfl, rw associates.mk_eq_mk_iff_associated, apply h₂ (hfx.1 _ hq) hp, end end has_unit_mul_pow_irreducible_factorization lemma aux_pid_of_ufd_of_unique_irreducible (R : Type u) [comm_ring R] [is_domain R] [unique_factorization_monoid R] (h₁ : ∃ p : R, irreducible p) (h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) : is_principal_ideal_ring R := begin constructor, intro I, by_cases I0 : I = ⊥, { rw I0, use 0, simp only [set.singleton_zero, submodule.span_zero], }, obtain ⟨x, hxI, hx0⟩ : ∃ x ∈ I, x ≠ (0:R) := I.ne_bot_iff.mp I0, obtain ⟨p, hp, H⟩ := has_unit_mul_pow_irreducible_factorization.of_ufd_of_unique_irreducible h₁ h₂, have ex : ∃ n : ℕ, p ^ n ∈ I, { obtain ⟨n, u, rfl⟩ := H hx0, refine ⟨n, _⟩, simpa only [units.mul_inv_cancel_right] using I.mul_mem_right ↑u⁻¹ hxI, }, constructor, use p ^ (nat.find ex), show I = ideal.span _, apply le_antisymm, { intros r hr, by_cases hr0 : r = 0, { simp only [hr0, submodule.zero_mem], }, obtain ⟨n, u, rfl⟩ := H hr0, simp only [mem_span_singleton, units.is_unit, is_unit.dvd_mul_right], apply pow_dvd_pow, apply nat.find_min', simpa only [units.mul_inv_cancel_right] using I.mul_mem_right ↑u⁻¹ hr, }, { erw submodule.span_singleton_le_iff_mem, exact nat.find_spec ex, }, end /-- A unique factorization domain with at least one irreducible element in which all irreducible elements are associated is a discrete valuation ring. -/ lemma of_ufd_of_unique_irreducible {R : Type u} [comm_ring R] [is_domain R] [unique_factorization_monoid R] (h₁ : ∃ p : R, irreducible p) (h₂ : ∀ ⦃p q : R⦄, irreducible p → irreducible q → associated p q) : discrete_valuation_ring R := begin rw iff_pid_with_one_nonzero_prime, haveI PID : is_principal_ideal_ring R := aux_pid_of_ufd_of_unique_irreducible R h₁ h₂, obtain ⟨p, hp⟩ := h₁, refine ⟨PID, ⟨ideal.span {p}, ⟨_, _⟩, _⟩⟩, { rw submodule.ne_bot_iff, refine ⟨p, ideal.mem_span_singleton.mpr (dvd_refl p), hp.ne_zero⟩, }, { rwa [ideal.span_singleton_prime hp.ne_zero, ← unique_factorization_monoid.irreducible_iff_prime], }, { intro I, rw ← submodule.is_principal.span_singleton_generator I, rintro ⟨I0, hI⟩, apply span_singleton_eq_span_singleton.mpr, apply h₂ _ hp, erw [ne.def, span_singleton_eq_bot] at I0, rwa [unique_factorization_monoid.irreducible_iff_prime, ← ideal.span_singleton_prime I0], apply_instance, }, end /-- An integral domain in which there is an irreducible element `p` such that every nonzero element is associated to a power of `p` is a discrete valuation ring. -/ lemma of_has_unit_mul_pow_irreducible_factorization {R : Type u} [comm_ring R] [is_domain R] (hR : has_unit_mul_pow_irreducible_factorization R) : discrete_valuation_ring R := begin letI : unique_factorization_monoid R := hR.to_unique_factorization_monoid, apply of_ufd_of_unique_irreducible _ hR.unique_irreducible, unfreezingI { obtain ⟨p, hp, H⟩ := hR, exact ⟨p, hp⟩, }, end section variables [comm_ring R] [is_domain R] [discrete_valuation_ring R] variable {R} lemma associated_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) : ∃ (n : ℕ), associated x (ϖ ^ n) := begin have : wf_dvd_monoid R := is_noetherian_ring.wf_dvd_monoid, cases wf_dvd_monoid.exists_factors x hx with fx hfx, unfreezingI { use fx.card }, have H := hfx.2, rw ← associates.mk_eq_mk_iff_associated at H ⊢, rw [← H, ← associates.prod_mk, associates.mk_pow, ← multiset.prod_repeat], congr' 1, rw multiset.eq_repeat, simp only [true_and, and_imp, multiset.card_map, eq_self_iff_true, multiset.mem_map, exists_imp_distrib], rintros _ _ _ rfl, rw associates.mk_eq_mk_iff_associated, refine associated_of_irreducible _ _ hirr, apply hfx.1, assumption end lemma eq_unit_mul_pow_irreducible {x : R} (hx : x ≠ 0) {ϖ : R} (hirr : irreducible ϖ) : ∃ (n : ℕ) (u : Rˣ), x = u * ϖ ^ n := begin obtain ⟨n, hn⟩ := associated_pow_irreducible hx hirr, obtain ⟨u, rfl⟩ := hn.symm, use [n, u], apply mul_comm, end open submodule.is_principal lemma ideal_eq_span_pow_irreducible {s : ideal R} (hs : s ≠ ⊥) {ϖ : R} (hirr : irreducible ϖ) : ∃ n : ℕ, s = ideal.span {ϖ ^ n} := begin have gen_ne_zero : generator s ≠ 0, { rw [ne.def, ← eq_bot_iff_generator_eq_zero], assumption }, rcases associated_pow_irreducible gen_ne_zero hirr with ⟨n, u, hnu⟩, use n, have : span _ = _ := span_singleton_generator s, rw [← this, ← hnu, span_singleton_eq_span_singleton], use u end lemma unit_mul_pow_congr_pow {p q : R} (hp : irreducible p) (hq : irreducible q) (u v : Rˣ) (m n : ℕ) (h : ↑u * p ^ m = v * q ^ n) : m = n := begin have key : associated (multiset.repeat p m).prod (multiset.repeat q n).prod, { rw [multiset.prod_repeat, multiset.prod_repeat, associated], refine ⟨u * v⁻¹, _⟩, simp only [units.coe_mul], rw [mul_left_comm, ← mul_assoc, h, mul_right_comm, units.mul_inv, one_mul], }, have := multiset.card_eq_card_of_rel (unique_factorization_monoid.factors_unique _ _ key), { simpa only [multiset.card_repeat] }, all_goals { intros x hx, unfreezingI { obtain rfl := multiset.eq_of_mem_repeat hx, assumption } }, end lemma unit_mul_pow_congr_unit {ϖ : R} (hirr : irreducible ϖ) (u v : Rˣ) (m n : ℕ) (h : ↑u * ϖ ^ m = v * ϖ ^ n) : u = v := begin obtain rfl : m = n := unit_mul_pow_congr_pow hirr hirr u v m n h, rw ← sub_eq_zero at h, rw [← sub_mul, mul_eq_zero] at h, cases h, { rw sub_eq_zero at h, exact_mod_cast h }, { apply (hirr.ne_zero (pow_eq_zero h)).elim, } end /-! ## The additive valuation on a DVR -/ open multiplicity /-- The `part_enat`-valued additive valuation on a DVR -/ noncomputable def add_val (R : Type u) [comm_ring R] [is_domain R] [discrete_valuation_ring R] : add_valuation R part_enat := add_valuation (classical.some_spec (exists_prime R)) lemma add_val_def (r : R) (u : Rˣ) {ϖ : R} (hϖ : irreducible ϖ) (n : ℕ) (hr : r = u * ϖ ^ n) : add_val R r = n := by rw [add_val, add_valuation_apply, hr, eq_of_associated_left (associated_of_irreducible R hϖ (classical.some_spec (exists_prime R)).irreducible), eq_of_associated_right (associated.symm ⟨u, mul_comm _ _⟩), multiplicity_pow_self_of_prime (principal_ideal_ring.irreducible_iff_prime.1 hϖ)] lemma add_val_def' (u : Rˣ) {ϖ : R} (hϖ : irreducible ϖ) (n : ℕ) : add_val R ((u : R) * ϖ ^ n) = n := add_val_def _ u hϖ n rfl @[simp] lemma add_val_zero : add_val R 0 = ⊤ := (add_val R).map_zero @[simp] lemma add_val_one : add_val R 1 = 0 := (add_val R).map_one @[simp] lemma add_val_uniformizer {ϖ : R} (hϖ : irreducible ϖ) : add_val R ϖ = 1 := by simpa only [one_mul, eq_self_iff_true, units.coe_one, pow_one, forall_true_left, nat.cast_one] using add_val_def ϖ 1 hϖ 1 @[simp] lemma add_val_mul {a b : R} : add_val R (a * b) = add_val R a + add_val R b := (add_val R).map_mul _ _ lemma add_val_pow (a : R) (n : ℕ) : add_val R (a ^ n) = n • add_val R a := (add_val R).map_pow _ _ lemma _root_.irreducible.add_val_pow {ϖ : R} (h : irreducible ϖ) (n : ℕ) : add_val R (ϖ ^ n) = n := by rw [add_val_pow, add_val_uniformizer h, nsmul_one] lemma add_val_eq_top_iff {a : R} : add_val R a = ⊤ ↔ a = 0 := begin have hi := (classical.some_spec (exists_prime R)).irreducible, split, { contrapose, intro h, obtain ⟨n, ha⟩ := associated_pow_irreducible h hi, obtain ⟨u, rfl⟩ := ha.symm, rw [mul_comm, add_val_def' u hi n], exact part_enat.coe_ne_top _ }, { rintro rfl, exact add_val_zero } end lemma add_val_le_iff_dvd {a b : R} : add_val R a ≤ add_val R b ↔ a ∣ b := begin have hp := classical.some_spec (exists_prime R), split; intro h, { by_cases ha0 : a = 0, { rw [ha0, add_val_zero, top_le_iff, add_val_eq_top_iff] at h, rw h, apply dvd_zero }, obtain ⟨n, ha⟩ := associated_pow_irreducible ha0 hp.irreducible, rw [add_val, add_valuation_apply, add_valuation_apply, multiplicity_le_multiplicity_iff] at h, exact ha.dvd.trans (h n ha.symm.dvd), }, { rw [add_val, add_valuation_apply, add_valuation_apply], exact multiplicity_le_multiplicity_of_dvd_right h } end lemma add_val_add {a b : R} : min (add_val R a) (add_val R b) ≤ add_val R (a + b) := (add_val R).map_add _ _ end instance (R : Type*) [comm_ring R] [is_domain R] [discrete_valuation_ring R] : is_Hausdorff (maximal_ideal R) R := { haus' := λ x hx, begin obtain ⟨ϖ, hϖ⟩ := exists_irreducible R, simp only [← ideal.one_eq_top, smul_eq_mul, mul_one, smodeq.zero, hϖ.maximal_ideal_eq, ideal.span_singleton_pow, ideal.mem_span_singleton, ← add_val_le_iff_dvd, hϖ.add_val_pow] at hx, rwa [← add_val_eq_top_iff, part_enat.eq_top_iff_forall_le], end } end discrete_valuation_ring
8a632fa21f95142b8ab627726d88e53be28ac269
130c49f47783503e462c16b2eff31933442be6ff
/stage0/src/Init/Notation.lean
5855919d4029519959941df4b73b8cf88dbef6ce
[ "Apache-2.0" ]
permissive
Hazel-Brown/lean4
8aa5860e282435ffc30dcdfccd34006c59d1d39c
79e6732fc6bbf5af831b76f310f9c488d44e7a16
refs/heads/master
1,689,218,208,951
1,629,736,869,000
1,629,736,896,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
18,785
lean
/- Copyright (c) 2020 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Notation for operators defined at Prelude.lean -/ prelude import Init.Prelude -- DSL for specifying parser precedences and priorities namespace Lean.Parser.Syntax syntax:65 (name := addPrec) prec " + " prec:66 : prec syntax:65 (name := subPrec) prec " - " prec:66 : prec syntax:65 (name := addPrio) prio " + " prio:66 : prio syntax:65 (name := subPrio) prio " - " prio:66 : prio end Lean.Parser.Syntax macro "max" : prec => `(1024) -- maximum precedence used in term parsers, in particular for terms in function position (`ident`, `paren`, ...) macro "arg" : prec => `(1023) -- precedence used for application arguments (`do`, `by`, ...) macro "lead" : prec => `(1022) -- precedence used for terms not supposed to be used as arguments (`let`, `have`, ...) macro "(" p:prec ")" : prec => p macro "min" : prec => `(10) -- minimum precedence used in term parsers macro "min1" : prec => `(11) -- `(min+1) we can only `min+1` after `Meta.lean` /- `max:prec` as a term. It is equivalent to `eval_prec max` for `eval_prec` defined at `Meta.lean`. We use `max_prec` to workaround bootstrapping issues. -/ macro "max_prec" : term => `(1024) macro "default" : prio => `(1000) macro "low" : prio => `(100) macro "mid" : prio => `(1000) macro "high" : prio => `(10000) macro "(" p:prio ")" : prio => p -- Basic notation for defining parsers -- NOTE: precedence must be at least `arg` to be used in `macro` without parentheses syntax:arg stx:max "+" : stx syntax:arg stx:max "*" : stx syntax:arg stx:max "?" : stx syntax:2 stx:2 " <|> " stx:1 : stx macro_rules | `(stx| $p +) => `(stx| many1($p)) | `(stx| $p *) => `(stx| many($p)) | `(stx| $p ?) => `(stx| optional($p)) | `(stx| $p₁ <|> $p₂) => `(stx| orelse($p₁, $p₂)) /- Comma-separated sequence. -/ macro:arg x:stx:max ",*" : stx => `(stx| sepBy($x, ",", ", ")) macro:arg x:stx:max ",+" : stx => `(stx| sepBy1($x, ",", ", ")) /- Comma-separated sequence with optional trailing comma. -/ macro:arg x:stx:max ",*,?" : stx => `(stx| sepBy($x, ",", ", ", allowTrailingSep)) macro:arg x:stx:max ",+,?" : stx => `(stx| sepBy1($x, ",", ", ", allowTrailingSep)) macro:arg "!" x:stx:max : stx => `(stx| notFollowedBy($x)) syntax (name := rawNatLit) "nat_lit " num : term infixr:90 " ∘ " => Function.comp infixr:35 " × " => Prod infixl:55 " ||| " => HOr.hOr infixl:58 " ^^^ " => HXor.hXor infixl:60 " &&& " => HAnd.hAnd infixl:65 " + " => HAdd.hAdd infixl:65 " - " => HSub.hSub infixl:70 " * " => HMul.hMul infixl:70 " / " => HDiv.hDiv infixl:70 " % " => HMod.hMod infixl:75 " <<< " => HShiftLeft.hShiftLeft infixl:75 " >>> " => HShiftRight.hShiftRight infixr:80 " ^ " => HPow.hPow infixl:65 " ++ " => HAppend.hAppend prefix:100 "-" => Neg.neg prefix:100 "~~~" => Complement.complement /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binop%` elaboration helper for binary operators. It addresses issue #382. -/ macro_rules | `($x ||| $y) => `(binop% HOr.hOr $x $y) macro_rules | `($x ^^^ $y) => `(binop% HXor.hXor $x $y) macro_rules | `($x &&& $y) => `(binop% HAnd.hAnd $x $y) macro_rules | `($x + $y) => `(binop% HAdd.hAdd $x $y) macro_rules | `($x - $y) => `(binop% HSub.hSub $x $y) macro_rules | `($x * $y) => `(binop% HMul.hMul $x $y) macro_rules | `($x / $y) => `(binop% HDiv.hDiv $x $y) macro_rules | `($x ++ $y) => `(binop% HAppend.hAppend $x $y) -- declare ASCII alternatives first so that the latter Unicode unexpander wins infix:50 " <= " => LE.le infix:50 " ≤ " => LE.le infix:50 " < " => LT.lt infix:50 " >= " => GE.ge infix:50 " ≥ " => GE.ge infix:50 " > " => GT.gt infix:50 " = " => Eq infix:50 " == " => BEq.beq infix:50 " ~= " => HEq infix:50 " ≅ " => HEq /- Remark: the infix commands above ensure a delaborator is generated for each relations. We redefine the macros below to be able to use the auxiliary `binrel%` elaboration helper for binary relations. It has better support for applying coercions. For example, suppose we have `binrel% Eq n i` where `n : Nat` and `i : Int`. The default elaborator fails because we don't have a coercion from `Int` to `Nat`, but `binrel%` succeeds because it also tries a coercion from `Nat` to `Int` even when the nat occurs before the int. -/ macro_rules | `($x <= $y) => `(binrel% LE.le $x $y) macro_rules | `($x ≤ $y) => `(binrel% LE.le $x $y) macro_rules | `($x < $y) => `(binrel% LT.lt $x $y) macro_rules | `($x > $y) => `(binrel% GT.gt $x $y) macro_rules | `($x >= $y) => `(binrel% GE.ge $x $y) macro_rules | `($x ≥ $y) => `(binrel% GE.ge $x $y) macro_rules | `($x = $y) => `(binrel% Eq $x $y) macro_rules | `($x == $y) => `(binrel% BEq.beq $x $y) infixr:35 " /\\ " => And infixr:35 " ∧ " => And infixr:30 " \\/ " => Or infixr:30 " ∨ " => Or notation:max "¬" p:40 => Not p infixl:35 " && " => and infixl:30 " || " => or notation:max "!" b:40 => not b infixr:67 " :: " => List.cons infixr:20 " <|> " => HOrElse.hOrElse infixr:60 " >> " => HAndThen.hAndThen infixl:55 " >>= " => Bind.bind infixl:60 " <*> " => Seq.seq infixl:60 " <* " => SeqLeft.seqLeft infixr:60 " *> " => SeqRight.seqRight infixr:100 " <$> " => Functor.map macro_rules | `($x <|> $y) => `(binop% HOrElse.hOrElse $x $y) macro_rules | `($x >> $y) => `(binop% HAndThen.hAndThen $x $y) syntax (name := termDepIfThenElse) ppGroup(ppDedent("if " ident " : " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term macro_rules | `(if $h:ident : $c then $t:term else $e:term) => ``(dite $c (fun $h:ident => $t) (fun $h:ident => $e)) syntax (name := termIfThenElse) ppGroup(ppDedent("if " term " then" ppSpace term ppDedent(ppSpace "else") ppSpace term)) : term macro_rules | `(if $c then $t:term else $e:term) => ``(ite $c $t $e) macro "if " "let " pat:term " := " d:term " then " t:term " else " e:term : term => `(match $d:term with | $pat:term => $t | _ => $e) syntax:min term "<|" term:min : term macro_rules | `($f $args* <| $a) => let args := args.push a; `($f $args*) | `($f <| $a) => `($f $a) syntax:min term "|>" term:min1 : term macro_rules | `($a |> $f $args*) => let args := args.push a; `($f $args*) | `($a |> $f) => `($f $a) -- Haskell-like pipe <| -- Note that we have a whitespace after `$` to avoid an ambiguity with the antiquotations. syntax:min term atomic("$" ws) term:min : term macro_rules | `($f $args* $ $a) => let args := args.push a; `($f $args*) | `($f $ $a) => `($f $a) syntax "{ " ident (" : " term)? " // " term " }" : term macro_rules | `({ $x : $type // $p }) => ``(Subtype (fun ($x:ident : $type) => $p)) | `({ $x // $p }) => ``(Subtype (fun ($x:ident : _) => $p)) /- `without_expected_type t` instructs Lean to elaborate `t` without an expected type. Recall that terms such as `match ... with ...` and `⟨...⟩` will postpone elaboration until expected type is known. So, `without_expected_type` is not effective in this case. -/ macro "without_expected_type " x:term : term => `(let aux := $x; aux) syntax "[" term,* "]" : term syntax "%[" term,* "|" term "]" : term -- auxiliary notation for creating big list literals namespace Lean macro_rules | `([ $elems,* ]) => do let rec expandListLit (i : Nat) (skip : Bool) (result : Syntax) : MacroM Syntax := do match i, skip with | 0, _ => pure result | i+1, true => expandListLit i false result | i+1, false => expandListLit i true (← ``(List.cons $(elems.elemsAndSeps[i]) $result)) if elems.elemsAndSeps.size < 64 then expandListLit elems.elemsAndSeps.size false (← ``(List.nil)) else `(%[ $elems,* | List.nil ]) notation:50 e:51 " matches " p:51 => match e with | p => true | _ => false namespace Parser.Tactic /-- Introduce one or more hypotheses, optionally naming and/or pattern-matching them. For each hypothesis to be introduced, the remaining main goal's target type must be a `let` or function type. * `intro` by itself introduces one anonymous hypothesis, which can be accessed by e.g. `assumption`. * `intro x y` introduces two hypotheses and names them. Individual hypotheses can be anonymized via `_`, or matched against a pattern: ```lean -- ... ⊢ α × β → ... intro (a, b) -- ..., a : α, b : β ⊢ ... ``` * Alternatively, `intro` can be combined with pattern matching much like `fun`: ```lean intro | n + 1, 0 => tac | ... ``` -/ syntax (name := intro) "intro " notFollowedBy("|") (colGt term:max)* : tactic /-- `intros x...` behaves like `intro x...`, but then keeps introducing (anonymous) hypotheses until goal is not of a function type. -/ syntax (name := intros) "intros " (colGt (ident <|> "_"))* : tactic /-- `rename t => x` renames the most recent hypothesis whose type matches `t` (which may contain placeholders) to `x`, or fails if no such hypothesis could be found. -/ syntax (name := rename) "rename " term " => " ident : tactic /-- `revert x...` is the inverse of `intro x...`: it moves the given hypotheses into the main goal's target type. -/ syntax (name := revert) "revert " (colGt ident)+ : tactic /-- `clear x...` removes the given hypotheses, or fails if there are remaining references to a hypothesis. -/ syntax (name := clear) "clear " (colGt ident)+ : tactic /-- `subst x...` substitutes each `x` with `e` in the goal if there is a hypothesis of type `x = e` or `e = x`. If `x` is itself a hypothesis of type `y = e` or `e = y`, `y` is substituted instead. -/ syntax (name := subst) "subst " (colGt ident)+ : tactic /-- `assumption` tries to solve the main goal using a hypothesis of compatible type, or else fails. Note also the `‹t›` term notation, which is a shorthand for `show t by assumption`. -/ syntax (name := assumption) "assumption" : tactic /-- `contradiction` closes the main goal if its hypotheses are "trivially contradictory". ```lean example (h : False) : p := by contradiction -- inductive type/family with no applicable constructors example (h : none = some true) : p := by contradiction -- injectivity of constructors example (h : 2 + 2 = 3) : p := by contradiction -- decidable false proposition example (h : p) (h' : ¬ p) : q := by contradiction example (x : Nat) (h : x ≠ x) : p := by contradiction ``` -/ syntax (name := contradiction) "contradiction" : tactic /-- `apply e` tries to match the current goal against the conclusion of `e`'s type. If it succeeds, then the tactic returns as many subgoals as the number of premises that have not been fixed by type inference or type class resolution. Non-dependent premises are added before dependent ones. The `apply` tactic uses higher-order pattern matching, type class resolution, and first-order unification with dependent types. -/ syntax (name := apply) "apply " term : tactic /-- `exact e` closes the main goal if its target type matches that of `e`. -/ syntax (name := exact) "exact " term : tactic /-- `refine e` behaves like `exact e`, except that named (`?x`) or unnamed (`?_`) holes in `e` that are not solved by unification with the main goal's target type are converted into new goals, using the hole's name, if any, as the goal case name. -/ syntax (name := refine) "refine " term : tactic /-- `refine' e` behaves like `refine e`, except that unsolved placeholders (`_`) and implicit parameters are also converted into new goals. -/ syntax (name := refine') "refine' " term : tactic /-- If the main goal's target type is an inductive type, `constructor` solves it with the first matching constructor, or else fails. -/ syntax (name := constructor) "constructor" : tactic /-- `case tag => tac` focuses on the goal with case name `tag` and solves it using `tac`, or else fails. `case tag x₁ ... xₙ => tac` additionally renames the `n` most recent hypotheses with inaccessible names to the given names. -/ syntax (name := case) "case " ident (ident <|> "_")* " => " tacticSeq : tactic /-- `allGoals tac` runs `tac` on each goal, concatenating the resulting goals, if any. -/ syntax (name := allGoals) "allGoals " tacticSeq : tactic /-- `focus tac` focuses on the main goal, suppressing all other goals, and runs `tac` on it. Usually `· tac`, which enforces that the goal is closed by `tac`, should be preferred. -/ syntax (name := focus) "focus " tacticSeq : tactic /-- `skip` does nothing. -/ syntax (name := skip) "skip" : tactic /-- `done` succeeds iff there are no remaining goals. -/ syntax (name := done) "done" : tactic syntax (name := traceState) "traceState" : tactic syntax (name := failIfSuccess) "failIfSuccess " tacticSeq : tactic /-- `generalize [h :] e = x` replaces all occurrences of the term `e` in the main goal with a fresh hypothesis `x`. If `h` is given, `h : e = x` is introduced as well. -/ syntax (name := generalize) "generalize " atomic(ident " : ")? term:51 " = " ident : tactic syntax (name := paren) "(" tacticSeq ")" : tactic syntax (name := withReducible) "withReducible " tacticSeq : tactic syntax (name := withReducibleAndInstances) "withReducibleAndInstances " tacticSeq : tactic /-- `first | tac | ...` runs each `tac` until one succeeds, or else fails. -/ syntax (name := first) "first " withPosition((group(colGe "|" tacticSeq))+) : tactic syntax (name := rotateLeft) "rotateLeft" (num)? : tactic syntax (name := rotateRight) "rotateRight" (num)? : tactic /-- `try tac` runs `tac` and succeeds even if `tac` failed. -/ macro "try " t:tacticSeq : tactic => `(first | $t | skip) /-- `tac <;> tac'` runs `tac` on the main goal and `tac'` on each produced goal, concatenating all goals produced by `tac'`. -/ macro:1 x:tactic " <;> " y:tactic:0 : tactic => `(tactic| focus ($x:tactic; allGoals $y:tactic)) /-- `· tac` focuses on the main goal and tries to solve it using `tac`, or else fails. -/ macro dot:("·" <|> ".") ts:tacticSeq : tactic => `(tactic| {%$dot ($ts:tacticSeq) }) /-- `rfl` is a shorthand for `exact rfl`. -/ macro "rfl" : tactic => `(exact rfl) /-- `admit` is a shorthand for `exact sorry`. -/ macro "admit" : tactic => `(exact sorry) macro "inferInstance" : tactic => `(exact inferInstance) syntax locationWildcard := "*" syntax locationHyp := (colGt ident)+ ("⊢" <|> "|-")? -- TODO: delete syntax locationTargets := (colGt ident)+ ("⊢" <|> "|-")? syntax location := withPosition("at " locationWildcard <|> locationHyp) syntax (name := change) "change " term (location)? : tactic syntax (name := changeWith) "change " term " with " term (location)? : tactic syntax rwRule := ("←" <|> "<-")? term syntax rwRuleSeq := "[" rwRule,+,? "]" syntax (name := rewriteSeq) "rewrite " rwRuleSeq (location)? : tactic syntax (name := erewriteSeq) "erewrite " rwRuleSeq (location)? : tactic syntax (name := rwSeq) "rw " rwRuleSeq (location)? : tactic syntax (name := erwSeq) "erw " rwRuleSeq (location)? : tactic def rwWithRfl (kind : SyntaxNodeKind) (atom : String) (stx : Syntax) : MacroM Syntax := do -- We show the `rfl` state on `]` let seq := stx[1] let rbrak := seq[2] -- Replace `]` token with one without position information in the expanded tactic let seq := seq.setArg 2 (mkAtom "]") let tac := stx.setKind kind |>.setArg 0 (mkAtomFrom stx atom) |>.setArg 1 seq `(tactic| $tac; try (withReducible rfl%$rbrak)) @[macro rwSeq] def expandRwSeq : Macro := rwWithRfl ``Lean.Parser.Tactic.rewriteSeq "rewrite" @[macro erwSeq] def expandERwSeq : Macro := rwWithRfl ``Lean.Parser.Tactic.erewriteSeq "erewrite" syntax (name := injection) "injection " term (" with " (colGt (ident <|> "_"))+)? : tactic syntax simpPre := "↓" syntax simpPost := "↑" syntax simpLemma := (simpPre <|> simpPost)? ("←" <|> "<-")? term syntax simpErase := "-" ident syntax simpStar := "*" syntax (name := simp) "simp " ("(" &"config" " := " term ")")? (&"only ")? ("[" (simpStar <|> simpErase <|> simpLemma),* "]")? (location)? : tactic syntax (name := simpAll) "simp_all " ("(" &"config" " := " term ")")? (&"only ")? ("[" (simpErase <|> simpLemma),* "]")? : tactic -- Auxiliary macro for lifting have/suffices/let/... -- It makes sure the "continuation" `?_` is the main goal after refining macro "refineLift " e:term : tactic => `(focus (refine noImplicitLambda% $e; rotateRight)) macro "have " d:haveDecl : tactic => `(refineLift have $d:haveDecl; ?_) /- We use a priority > default, to avoid ambiguity with previous `have` notation -/ macro (priority := high) "have" x:ident " := " p:term : tactic => `(have $x:ident : _ := $p) macro "suffices " d:sufficesDecl : tactic => `(refineLift suffices $d:sufficesDecl; ?_) macro "let " d:letDecl : tactic => `(refineLift let $d:letDecl; ?_) macro "show " e:term : tactic => `(refineLift show $e:term from ?_) syntax (name := letrec) withPosition(atomic(group("let " &"rec ")) letRecDecls) : tactic macro_rules | `(tactic| let rec $d:letRecDecls) => `(tactic| refineLift let rec $d:letRecDecls; ?_) -- Similar to `refineLift`, but using `refine'` macro "refineLift' " e:term : tactic => `(focus (refine' noImplicitLambda% $e; rotateRight)) macro "have' " d:haveDecl : tactic => `(refineLift' have $d:haveDecl; ?_) macro (priority := high) "have'" x:ident " := " p:term : tactic => `(have' $x:ident : _ := $p) macro "let' " d:letDecl : tactic => `(refineLift' let $d:letDecl; ?_) syntax inductionAlt := "| " (group("@"? ident) <|> "_") (ident <|> "_")* " => " (hole <|> syntheticHole <|> tacticSeq) syntax inductionAlts := "with " (tactic)? withPosition( (colGe inductionAlt)+) syntax (name := induction) "induction " term,+ (" using " ident)? ("generalizing " ident+)? (inductionAlts)? : tactic syntax casesTarget := atomic(ident " : ")? term syntax (name := cases) "cases " casesTarget,+ (" using " ident)? (inductionAlts)? : tactic syntax (name := existsIntro) "exists " term : tactic syntax "repeat " tacticSeq : tactic macro_rules | `(tactic| repeat $seq) => `(tactic| first | ($seq); repeat $seq | skip) syntax "trivial" : tactic macro_rules | `(tactic| trivial) => `(tactic| assumption) macro_rules | `(tactic| trivial) => `(tactic| rfl) macro_rules | `(tactic| trivial) => `(tactic| contradiction) macro_rules | `(tactic| trivial) => `(tactic| apply True.intro) macro_rules | `(tactic| trivial) => `(tactic| apply And.intro <;> trivial) macro "unhygienic " t:tacticSeq : tactic => `(set_option tactic.hygienic false in $t:tacticSeq) end Tactic namespace Attr -- simp attribute syntax syntax (name := simp) "simp" (Tactic.simpPre <|> Tactic.simpPost)? (prio)? : attr end Attr end Parser end Lean macro "‹" type:term "›" : term => `((by assumption : $type))
8369db621d0c99cf941d31b2053ea998ae93623a
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/ftree.lean
6f9e887dd898d7968a61b3bfe12c9f68e345ab12
[ "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
785
lean
inductive list (T : Type) : Type := nil {} : list T | cons : T → list T → list T namespace explicit inductive ftree.{l₁ l₂} (A : Type.{l₁}) (B : Type.{l₂}) : Type.{max 1 l₁ l₂} := leafa : A → ftree A B | leafb : B → ftree A B | node : (A → ftree A B) → (B → ftree A B) → ftree A B end explicit namespace implicit inductive ftree (A : Type) (B : Type) : Type := leafa : ftree A B | node : (A → B → ftree A B) → (B → ftree A B) → ftree A B set_option pp.universes true check ftree end implicit namespace implicit2 inductive ftree (A : Type) (B : Type) : Type := leafa : A → ftree A B | leafb : B → ftree A B | node : (list A → ftree A B) → (B → ftree A B) → ftree A B set_option pp.universes true check ftree end implicit2
af1b147b9cb3e633ceec9ff391c7dd0bcc273bb8
bbecf0f1968d1fba4124103e4f6b55251d08e9c4
/src/number_theory/bernoulli_polynomials.lean
3aceaa931375028212bb80c9667a79af7cc8c61b
[ "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
7,104
lean
/- Copyright (c) 2021 Ashvni Narayanan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ashvni Narayanan -/ import data.nat.choose.cast import number_theory.bernoulli /-! # Bernoulli polynomials The Bernoulli polynomials (defined here : https://en.wikipedia.org/wiki/Bernoulli_polynomials) are an important tool obtained from Bernoulli numbers. ## Mathematical overview The $n$-th Bernoulli polynomial is defined as $$ B_n(X) = ∑_{k = 0}^n {n \choose k} (-1)^k * B_k * X^{n - k} $$ where $B_k$ is the $k$-th Bernoulli number. The Bernoulli polynomials are generating functions, $$ t * e^{tX} / (e^t - 1) = ∑_{n = 0}^{\infty} B_n(X) * \frac{t^n}{n!} $$ ## Implementation detail Bernoulli polynomials are defined using `bernoulli`, the Bernoulli numbers. ## Main theorems - `sum_bernoulli_poly`: The sum of the $k^\mathrm{th}$ Bernoulli polynomial with binomial coefficients up to n is `(n + 1) * X^n`. - `exp_bernoulli_poly`: The Bernoulli polynomials act as generating functions for the exponential. ## TODO - `bernoulli_poly_eval_one_neg` : $$ B_n(1 - x) = (-1)^n*B_n(x) $$ - ``bernoulli_poly_eval_one` : Follows as a consequence of `bernoulli_poly_eval_one_neg`. -/ noncomputable theory open_locale big_operators open_locale nat open nat finset /-- The Bernoulli polynomials are defined in terms of the negative Bernoulli numbers. -/ def bernoulli_poly (n : ℕ) : polynomial ℚ := ∑ i in range (n + 1), polynomial.monomial (n - i) ((bernoulli i) * (choose n i)) lemma bernoulli_poly_def (n : ℕ) : bernoulli_poly n = ∑ i in range (n + 1), polynomial.monomial i ((bernoulli (n - i)) * (choose n i)) := begin rw [←sum_range_reflect, add_succ_sub_one, add_zero, bernoulli_poly], apply sum_congr rfl, rintros x hx, rw mem_range_succ_iff at hx, rw [choose_symm hx, nat.sub_sub_self hx], end namespace bernoulli_poly /- ### examples -/ section examples @[simp] lemma bernoulli_poly_zero : bernoulli_poly 0 = 1 := by simp [bernoulli_poly] @[simp] lemma bernoulli_poly_eval_zero (n : ℕ) : (bernoulli_poly n).eval 0 = bernoulli n := begin rw [bernoulli_poly, polynomial.eval_finset_sum, sum_range_succ], have : ∑ (x : ℕ) in range n, bernoulli x * (n.choose x) * 0 ^ (n - x) = 0, { apply sum_eq_zero (λ x hx, _), have h : 0 < n - x := nat.sub_pos_of_lt (mem_range.1 hx), simp [h] }, simp [this], end @[simp] lemma bernoulli_poly_eval_one (n : ℕ) : (bernoulli_poly n).eval 1 = bernoulli' n := begin simp only [bernoulli_poly, polynomial.eval_finset_sum], simp only [←succ_eq_add_one, sum_range_succ, mul_one, cast_one, choose_self, (bernoulli _).mul_comm, sum_bernoulli, one_pow, mul_one, polynomial.eval_C, polynomial.eval_monomial], by_cases h : n = 1, { norm_num [h], }, { simp [h], exact bernoulli_eq_bernoulli'_of_ne_one h, } end end examples @[simp] theorem sum_bernoulli_poly (n : ℕ) : ∑ k in range (n + 1), ((n + 1).choose k : ℚ) • bernoulli_poly k = polynomial.monomial n (n + 1 : ℚ) := begin simp_rw [bernoulli_poly_def, finset.smul_sum, finset.range_eq_Ico, ←finset.sum_Ico_Ico_comm, finset.sum_Ico_eq_sum_range], simp only [cast_succ, nat.add_sub_cancel_left, nat.sub_zero, zero_add, linear_map.map_add], simp_rw [polynomial.smul_monomial, mul_comm (bernoulli _) _, smul_eq_mul, ←mul_assoc], conv_lhs { apply_congr, skip, conv { apply_congr, skip, rw [← nat.cast_mul, choose_mul ((le_sub_iff_left $ mem_range_le H).1 $ mem_range_le H_1) (le.intro rfl), nat.cast_mul, add_comm x x_1, nat.add_sub_cancel, mul_assoc, mul_comm, ←smul_eq_mul, ←polynomial.smul_monomial] }, rw [←sum_smul], }, rw [sum_range_succ_comm], simp only [add_right_eq_self, cast_succ, mul_one, cast_one, cast_add, nat.add_sub_cancel_left, choose_succ_self_right, one_smul, bernoulli_zero, sum_singleton, zero_add, linear_map.map_add, range_one], apply sum_eq_zero (λ x hx, _), have f : ∀ x ∈ range n, ¬ n + 1 - x = 1, { rintros x H, rw [mem_range] at H, rw [eq_comm], exact ne_of_lt (nat.lt_of_lt_of_le one_lt_two (le_sub_of_add_le_left' (succ_le_succ H))) }, rw [sum_bernoulli], have g : (ite (n + 1 - x = 1) (1 : ℚ) 0) = 0, { simp only [ite_eq_right_iff, one_ne_zero], intro h₁, exact (f x hx) h₁, }, rw [g, zero_smul], end open power_series open polynomial (aeval) variables {A : Type*} [comm_ring A] [algebra ℚ A] -- TODO: define exponential generating functions, and use them here -- This name should probably be updated afterwards /-- The theorem that `∑ Bₙ(t)X^n/n!)(e^X-1)=Xe^{tX}` -/ theorem exp_bernoulli_poly' (t : A) : mk (λ n, aeval t ((1 / n! : ℚ) • bernoulli_poly n)) * (exp A - 1) = X * rescale t (exp A) := begin -- check equality of power series by checking coefficients of X^n ext n, -- n = 0 case solved by `simp` cases n, { simp }, -- n ≥ 1, the coefficients is a sum to n+2, so use `sum_range_succ` to write as -- last term plus sum to n+1 rw [coeff_succ_X_mul, coeff_rescale, coeff_exp, coeff_mul, nat.sum_antidiagonal_eq_sum_range_succ_mk, sum_range_succ], -- last term is zero so kill with `add_zero` simp only [ring_hom.map_sub, nat.sub_self, constant_coeff_one, constant_coeff_exp, coeff_zero_eq_constant_coeff, mul_zero, sub_self, add_zero], -- Let's multiply both sides by (n+1)! (OK because it's a unit) set u : units ℚ := ⟨(n+1)!, (n+1)!⁻¹, mul_inv_cancel (by exact_mod_cast factorial_ne_zero (n+1)), inv_mul_cancel (by exact_mod_cast factorial_ne_zero (n+1))⟩ with hu, rw ←units.mul_right_inj (units.map (algebra_map ℚ A).to_monoid_hom u), -- now tidy up unit mess and generally do trivial rearrangements -- to make RHS (n+1)*t^n rw [units.coe_map, mul_left_comm, ring_hom.to_monoid_hom_eq_coe, ring_hom.coe_monoid_hom, ←ring_hom.map_mul, hu, units.coe_mk], change _ = t^n * algebra_map ℚ A (((n+1)*n! : ℕ)*(1/n!)), rw [cast_mul, mul_assoc, mul_one_div_cancel (show (n! : ℚ) ≠ 0, from cast_ne_zero.2 (factorial_ne_zero n)), mul_one, mul_comm (t^n), ← polynomial.aeval_monomial, cast_add, cast_one], -- But this is the RHS of `sum_bernoulli_poly` rw [← sum_bernoulli_poly, finset.mul_sum, alg_hom.map_sum], -- and now we have to prove a sum is a sum, but all the terms are equal. apply finset.sum_congr rfl, -- The rest is just trivialities, hampered by the fact that we're coercing -- factorials and binomial coefficients between ℕ and ℚ and A. intros i hi, -- deal with coefficients of e^X-1 simp only [nat.cast_choose ℚ (mem_range_le hi), coeff_mk, if_neg (mem_range_sub_ne_zero hi), one_div, alg_hom.map_smul, coeff_one, units.coe_mk, coeff_exp, sub_zero, linear_map.map_sub, algebra.smul_mul_assoc, algebra.smul_def, mul_right_comm _ ((aeval t) _), ←mul_assoc, ← ring_hom.map_mul, succ_eq_add_one], -- finally cancel the Bernoulli polynomial and the algebra_map congr', apply congr_arg, rw [mul_assoc, div_eq_mul_inv, ← mul_inv₀], end end bernoulli_poly
1f3850f97d3ad382b90a57cd19dd1103f38e991f
1abd1ed12aa68b375cdef28959f39531c6e95b84
/src/category_theory/limits/preserves/shapes/terminal.lean
de72dd03ed691c5d24dc4b1dc0675ab9ddf59a51
[ "Apache-2.0" ]
permissive
jumpy4/mathlib
d3829e75173012833e9f15ac16e481e17596de0f
af36f1a35f279f0e5b3c2a77647c6bf2cfd51a13
refs/heads/master
1,693,508,842,818
1,636,203,271,000
1,636,203,271,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
4,172
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import category_theory.limits.preserves.limits import category_theory.limits.shapes.terminal /-! # Preserving terminal object Constructions to relate the notions of preserving terminal objects and reflecting terminal objects to concrete objects. In particular, we show that `terminal_comparison G` is an isomorphism iff `G` preserves terminal objects. -/ universes v u₁ u₂ noncomputable theory open category_theory category_theory.category category_theory.limits variables {C : Type u₁} [category.{v} C] variables {D : Type u₂} [category.{v} D] variables (G : C ⥤ D) namespace category_theory.limits variables (X : C) /-- The map of an empty cone is a limit iff the mapped object is terminal. -/ def is_limit_map_cone_empty_cone_equiv : is_limit (G.map_cone (as_empty_cone X)) ≃ is_terminal (G.obj X) := (is_limit.postcompose_hom_equiv (functor.empty_ext _ _) _).symm.trans (is_limit.equiv_iso_limit (cones.ext (iso.refl _) (by tidy))) /-- The property of preserving terminal objects expressed in terms of `is_terminal`. -/ def is_terminal_obj_of_is_terminal [preserves_limit (functor.empty C) G] (l : is_terminal X) : is_terminal (G.obj X) := is_limit_map_cone_empty_cone_equiv G X (preserves_limit.preserves l) /-- The property of reflecting terminal objects expressed in terms of `is_terminal`. -/ def is_terminal_of_is_terminal_obj [reflects_limit (functor.empty C) G] (l : is_terminal (G.obj X)) : is_terminal X := reflects_limit.reflects ((is_limit_map_cone_empty_cone_equiv G X).symm l) variables [has_terminal C] /-- If `G` preserves the terminal object and `C` has a terminal object, then the image of the terminal object is terminal. -/ def is_limit_of_has_terminal_of_preserves_limit [preserves_limit (functor.empty C) G] : is_terminal (G.obj (⊤_ C)) := is_terminal_obj_of_is_terminal G (⊤_ C) terminal_is_terminal /-- If `C` has a terminal object and `G` preserves terminal objects, then `D` has a terminal object also. Note this property is somewhat unique to (co)limits of the empty diagram: for general `J`, if `C` has limits of shape `J` and `G` preserves them, then `D` does not necessarily have limits of shape `J`. -/ lemma has_terminal_of_has_terminal_of_preserves_limit [preserves_limit (functor.empty C) G] : has_terminal D := ⟨λ F, begin haveI := has_limit.mk ⟨_, is_limit_of_has_terminal_of_preserves_limit G⟩, apply has_limit_of_iso F.unique_from_empty.symm, end⟩ variable [has_terminal D] /-- If the terminal comparison map for `G` is an isomorphism, then `G` preserves terminal objects. -/ def preserves_terminal.of_iso_comparison [i : is_iso (terminal_comparison G)] : preserves_limit (functor.empty C) G := begin apply preserves_limit_of_preserves_limit_cone terminal_is_terminal, apply (is_limit_map_cone_empty_cone_equiv _ _).symm _, apply is_limit.of_point_iso (limit.is_limit (functor.empty D)), apply i, end /-- If there is any isomorphism `G.obj ⊤ ⟶ ⊤`, then `G` preserves terminal objects. -/ def preserves_terminal_of_is_iso (f : G.obj (⊤_ C) ⟶ ⊤_ D) [i : is_iso f] : preserves_limit (functor.empty C) G := begin rw subsingleton.elim f (terminal_comparison G) at i, exactI preserves_terminal.of_iso_comparison G, end /-- If there is any isomorphism `G.obj ⊤ ≅ ⊤`, then `G` preserves terminal objects. -/ def preserves_terminal_of_iso (f : G.obj (⊤_ C) ≅ ⊤_ D) : preserves_limit (functor.empty C) G := preserves_terminal_of_is_iso G f.hom variables [preserves_limit (functor.empty C) G] /-- If `G` preserves terminal objects, then the terminal comparison map for `G` an isomorphism. -/ def preserves_terminal.iso : G.obj (⊤_ C) ≅ ⊤_ D := (is_limit_of_has_terminal_of_preserves_limit G).cone_point_unique_up_to_iso (limit.is_limit _) @[simp] lemma preserves_terminal.iso_hom : (preserves_terminal.iso G).hom = terminal_comparison G := rfl instance : is_iso (terminal_comparison G) := begin rw ← preserves_terminal.iso_hom, apply_instance, end end category_theory.limits
28c02ed1628bb6fea8c40385e92bdffb9c33b631
29cc89d6158dd3b90acbdbcab4d2c7eb9a7dbf0f
/Project/test.lean
dc9faa1dc851ceb4afbac5122fbb1dc20d0f301a
[]
no_license
KjellZijlemaker/Logical_Verification_VU
ced0ba95316a30e3c94ba8eebd58ea004fa6f53b
4578b93bf1615466996157bb333c84122b201d99
refs/heads/master
1,585,966,086,108
1,549,187,704,000
1,549,187,704,000
155,690,284
0
0
null
null
null
null
UTF-8
Lean
false
false
4,955
lean
def concat {α : Type} : list (list α) → list α | [] := [] | (xs :: xss) := xs ++ concat xss def map {α β : Type} (f : α → β) : list α → list β | [] := [] | (x :: xs) := f x :: map xs def reverse {α: Type}: list α → list α | [] := [] | (x::xs) := reverse xs ++ [x] lemma concat_snoc {α : Type} : ∀(xs : list α) (xss : list (list α)), concat (xss ++ [xs]) = concat xss ++ xs:=sorry lemma reverse_append {α : Type} : ∀xs ys : list α, reverse (xs ++ ys) = reverse xs ++ reverse ys := sorry lemma reverse_concat {α : Type} : ∀xss : list (list α), reverse (concat xss) = concat (reverse (map reverse xss)) | [] := by refl | (x :: xs) := begin simp[concat], simp[map], simp[reverse], simp[reverse_append], simp[concat_snoc], rw[reverse_concat xs] end inductive sorted: list ℕ → Prop | nil : sorted [] | single {n: ℕ} : sorted [n] | largertwo {x y: ℕ} {xs: list ℕ} (xy: x <= y) (yxs: sorted (y :: xs)) : sorted (x :: y :: xs) -- intro xss, -- induction xss, -- refl, -- simp[map], -- simp[concat], -- rw[<-xss_ih], example : sorted [] := sorted.nil example : sorted [2] := sorted.single example : sorted [3, 5] := begin apply sorted.largertwo, trivial, sorted.single end example : sorted [7, 9, 9, 11] := begin apply repeat{sorted.largertwo}, trivial, end inductive program (σ : Type) : Type | skip {} : program | assign : (σ → σ) → program | seq : program → program → program | ite : (σ → Prop) → program → program → program | while : (σ → Prop) → program → program def program_equiv (p1 p2 : program σ) : Prop := ∀s t, (p1, s) =⇒ t ↔ (p2, s) =⇒ t inductive finite {α: Type}: set α → Prop | empty: finite ∅ | single {a: α}: finite(insert a ∅) | both_fin {A B: set α} (fin_A: finite A) (fin_B: finite B): finite (A ∪ B) lemma finite.union {α : Type} : ∀A B : set α, finite A → finite B → finite (A ∪ B) := begin intros A B, intros finA finB, apply finite.both_fin, assumption, assumption end instance fin_set.rel (α : Type) : setoid (list α) := { r := λxs ys, ∀x, x ∈ xs ↔ x ∈ ys, iseqv := r x x } lemma isreflective {α: Type} (xs: list α): xs = xs := sorry namespace v1 def concat {α: Type}: list (list α) → list α | [] := [] | (xs :: xss) := xs ++ concat xss def reverse {α: Type}: list α → list α | [] := [] | (x :: xs) := reverse xs ++ [x] #reduce reverse [2,3,4] def map {α β: Type} (f: α → β): list α → list β | [] := [] | (x :: xs) := f x :: map xs lemma map_append {α: Type}: ∀xs ys : list α, map f (xs ++ ys) = (map xs) ++ (map ys) #reduce map 2 [2,3,4] lemma map_concat {α β : Type} (f : α → β) : ∀xss : list (list α), map f (concat xss) = concat (map (map f) xss) | [] := by refl | (x :: xss) := begin simp[concat], simp[map], simp[concat], rw[<-map_concat xss], induction xss, simp[concat], simp[map], simp[map_concat], simp[map], simp[concat], end lemma concat_snoc {α : Type} : ∀(xs : list α) (xss : list (list α)), concat (xss ++ [xs]) = concat xss ++ xs := sorry lemma reverse_append {α : Type} : ∀xs ys : list α, reverse (xs ++ ys) = reverse xs ++ reverse ys := sorry lemma reverse_concat {α : Type} : ∀xss : list (list α), reverse (concat xss) = concat (reverse (map reverse xss)) | [] := by refl | (x :: xss) := begin simp[concat], simp[map], simp[reverse], simp[concat_snoc],simp[reverse_append], rw[reverse_concat xss], simp end inductive sorted: list ℕ → Prop | empty : sorted [] | any_list {a: ℕ}: sorted [a] | greater_two {xs: list ℕ} {x y: ℕ} (xsmallery: x <= y) (sxy: sorted (y :: xs)) : sorted (x :: y :: xs) example : sorted [] := sorted.empty example : sorted [2] := sorted.any_list example : sorted [3, 5] := begin apply sorted.greater_two, apply trivial, apply sorted.any_list end example : sorted [7, 9, 9, 11] := begin apply sorted.greater_two, apply trivial, apply sorted.greater_two...end example : ¬ sorted [17, 13] := begin assume s : sorted [17,13],begin have 17 <= 13 := match s with sorted.greater_two xy yxs := xy end end -- inductive finite {α: Type} : set α → Prop -- | empty : finite ∅ -- | finite_set {a: α} {A: set α} (finA : finite A) : finite ({a} ∪ A) inductive finite {α: Type} : set α → Prop | empty : finite ∅ | singleton {a: α} : finite (insert a ∅) | ABFinite {finiteA finiteB: set α} (afinite: finite finiteA) (bfinite: finite finiteB) : finite (finiteA ∪ finiteB) lemma finite.union {α : Type} : ∀A B : set α, finite A → finite B → finite (A ∪ B) := begin intros A B, intros finiteA finiteB, apply finite.ABFinite, repeat{assumption} end lemma refleciver {α: Type} (xs: list α): xs = xs := by refl lemma symmetric {α: Type} (xs ys: list α): xs → ys = ys → xs := sorry lemma transitive {α: Type} (xs ys zs: list α): xs → ys ∧ ys → zs = xs → zs := sorry def fin_empty {α : Type} (a: α) : fin_set α := [[a]] end v1
f65afbe815dc456c5e0b5c014058994ec9f5411e
5e42295de7f5bcdf224b94603a8ec29b17c2d367
/sum_form.lean
abb652f2349e893c322e322ce4baf5bdfbcc9620
[]
no_license
pnmadelaine/lean_polya
9369e0d87dce773f91383bb58ac6fde0a00a1a40
1c62b0b3fa71044b0225ce28030627d251b08ebc
refs/heads/master
1,590,161,172,243
1,515,010,019,000
1,515,010,019,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
9,781
lean
import .datatypes .blackboard -- TODO: maybe more of this can move to datatypes namespace polya section sfcd_to_ineq -- assumes lhs < rhs as exprs. cl*lhs + cr*rhs R 0 ==> ineq_data private meta def mk_ineq_data_of_lhs_rhs (lhs rhs : expr) (cl cr : ℚ) (c : comp) {s} (pf : sum_form_proof s) : Σ l r, ineq_data l r := let c' := if cl > 0 then c else c.reverse, iq := ineq.of_comp_and_slope (c') (slope.some (-cr/cl)) in ⟨lhs, rhs, ⟨iq, ineq_proof.of_sum_form_proof lhs rhs iq pf⟩⟩ meta def sum_form_comp_data.to_ineq_data : sum_form_comp_data → option (Σ lhs rhs, ineq_data lhs rhs) | ⟨⟨sf, spec_comp.eq⟩, prf, _⟩ := none | ⟨sfc, prf, _⟩ := match sfc.sf.get_nonzero_factors with | [(rhs, cr), (lhs, cl)] := if rhs.lt lhs then mk_ineq_data_of_lhs_rhs lhs rhs cl cr (sfc.c.to_comp) prf else mk_ineq_data_of_lhs_rhs rhs lhs cr cl (sfc.c.to_comp) prf | _ := none end meta def sum_form_comp_data.to_eq_data : sum_form_comp_data → option (Σ lhs rhs, eq_data lhs rhs) | ⟨⟨sf, spec_comp.eq⟩, prf, _⟩ := match sf.get_nonzero_factors with | [(rhs, cr), (lhs, cl)] := some ⟨lhs, rhs, ⟨(-cr/cl), eq_proof.of_sum_form_proof _ _ _ prf⟩⟩ | _ := none end | _ := none meta def sum_form_comp_data.to_sign_data : sum_form_comp_data → option Σ e, sign_data e | ⟨sfc, prf, _⟩ := match sfc.sf.get_nonzero_factors with | [(e, n)] := let c := if n < 0 then sfc.c.to_gen_comp.reverse else sfc.c.to_gen_comp in some ⟨e, ⟨c, sign_proof.of_sum_form_proof _ _ prf⟩⟩ --sign_proof.adhoc _ _ (prf.reconstruct >>= tactic.trace, tactic.fail "sum_form_comp_data.to_sign_data not implemented")⟩⟩ | _ := none end end sfcd_to_ineq -- assumes the coeff of pvt in both is nonzero. Does not enforce elim_list preservation meta def sum_form_comp_data.elim_expr_aux : sum_form_comp_data → sum_form_comp_data → expr → option sum_form_comp_data | ⟨⟨sf1, c1⟩, prf1, elim_list1⟩ ⟨⟨sf2, c2⟩, prf2, elim_list2⟩ pvt := let cf1 := sf1.get_coeff pvt, cf2 := sf2.get_coeff pvt, fac := (-cf1/cf2) in if (fac > 0) then some ⟨_, sum_form_proof.of_add_factor_same_comp fac prf1 prf2, (rb_set.union elim_list1 elim_list2).insert pvt⟩ else if hce : c1 = spec_comp.eq then some ⟨_, sum_form_proof.of_add_eq_factor_op_comp fac prf2 (by rw ←hce; apply prf1), (rb_set.union elim_list1 elim_list2).insert pvt⟩ else if hce' : c2 = spec_comp.eq then some ⟨_, sum_form_proof.of_add_eq_factor_op_comp fac prf1 (by rw ←hce'; apply prf2), (rb_set.union elim_list1 elim_list2).insert pvt⟩ else none meta def sum_form_comp_data.elim_expr (sfcd1 sfcd2 : sum_form_comp_data) (pvt : expr) : option sum_form_comp_data := if sfcd1.get_coeff pvt = 0 then some ⟨sfcd1.sfc, sfcd1.prf, sfcd1.elim_list.insert pvt⟩ else if sfcd2.get_coeff pvt = 0 then none else sum_form_comp_data.elim_expr_aux sfcd1 sfcd2 pvt meta def sum_form_comp_data.elim_into (sfcd1 sfcd2 : sum_form_comp_data) (pvt : expr) (rv : rb_set sum_form_comp_data) : rb_set sum_form_comp_data := match sfcd1.elim_expr sfcd2 pvt with | none := rv | some sfcd := rv.insert sfcd end /-- Uses sfcd to eliminate the e from all comparisons in cmps, and adds the new comparisons to rv -/ meta def elim_expr_from_comp_data (sfcd : sum_form_comp_data) (cmps : rb_set sum_form_comp_data) (e : expr) (rv : rb_set sum_form_comp_data) : rb_set sum_form_comp_data := cmps.fold rv (λ c rv', sfcd.elim_into c e rv') /-- Eliminates the expression e from all comparisons in cmps in all possible ways -/ meta def elim_expr_from_comp_data_set (cmps : rb_set sum_form_comp_data) (e : expr) : rb_set sum_form_comp_data := cmps.fold mk_rb_set (λ c rv, elim_expr_from_comp_data c cmps e rv) /- /-- Performs all possible eliminations with sfcd on cmps. Returns a set of all new comps, NOT including the old ones. -/ meta def new_exprs_from_comp_data_set (sfcd : sum_form_comp_data) (cmps : rb_set sum_form_comp_data) : rb_set sum_form_comp_data := sfcd.vars.foldr (λ e rv, elim_expr_from_comp_data sfcd cmps e rv) mk_rb_set -/ meta def elim_expr_from_comp_data_list (cmps : list sum_form_comp_data) (e : expr) : list sum_form_comp_data := (elim_expr_from_comp_data_set (rb_set.of_list cmps) e).to_list private meta def check_elim_lists_aux (sfcd1 sfcd2 : sum_form_comp_data) : bool := sfcd1.vars.all (λ e, bnot (sfcd2.elim_list.contains e)) private meta def check_elim_lists (sfcd1 sfcd2 : sum_form_comp_data) : bool := check_elim_lists_aux sfcd1 sfcd2 && check_elim_lists_aux sfcd2 sfcd1 meta def sum_form_comp_data.needs_elim_against (sfcd1 sfcd2 : sum_form_comp_data) (e : expr) : bool := (check_elim_lists sfcd1 sfcd2)-- && --(((sfcd1.vars.append sfcd2.vars).filter (λ e' : expr, e'.lt e)).length ≤ 2) -- change back to e' lt e /-- Uses sfcd to eliminate the e from all comparisons in cmps, and adds the new comparisons to rv -/ meta def elim_expr_from_comp_data_filtered (sfcd : sum_form_comp_data) (cmps : rb_set sum_form_comp_data) (e : expr) (rv : rb_set sum_form_comp_data) : rb_set sum_form_comp_data := cmps.fold rv (λ c rv', if sfcd.needs_elim_against c e then sfcd.elim_into c e rv' else rv') /-- Performs all possible eliminations with sfcd on cmps. Returns a set of all new comps, NOT including the old ones. -/ meta def new_exprs_from_comp_data_set (sfcd : sum_form_comp_data) (cmps : rb_set sum_form_comp_data) : rb_set sum_form_comp_data := sfcd.vars.foldr (λ e rv, elim_expr_from_comp_data_filtered sfcd cmps e rv) mk_rb_set meta def elim_list_into_set : rb_set sum_form_comp_data → list sum_form_comp_data → rb_set sum_form_comp_data | cmps [] := cmps | cmps (sfcd::new_cmps) := let sfcdn := sfcd.normalize in if cmps.contains (/-trace_val-/ (new_cmps.length, sfcdn)).2 then elim_list_into_set cmps new_cmps else let new_gen := (new_exprs_from_comp_data_set sfcdn cmps).keys in elim_list_into_set (cmps.insert sfcdn) (new_cmps.append new_gen) meta def elim_list_set (cmps : list sum_form_comp_data) (start : rb_set sum_form_comp_data := mk_rb_set) : rb_set sum_form_comp_data := elim_list_into_set start (trace_val cmps) meta def elim_list (cmps : list sum_form_comp_data) (start : rb_set sum_form_comp_data := mk_rb_set) : list sum_form_comp_data := (elim_list_into_set start cmps).to_list meta def vars_of_sfcd_set (cmps : rb_set sum_form_comp_data) : rb_set expr := cmps.fold mk_rb_set (λ sfcd s, s.insert_list sfcd.vars) def list.first {α} (P : α → bool) : list α → option α | [] := none | (h::t) := if P h then some h else list.first t meta def find_contrad_sfcd_in_sfcd_set (cmps : rb_set sum_form_comp_data) : option sum_form_comp_data := let elimd := (vars_of_sfcd_set cmps).fold cmps (λ s cmps', elim_expr_from_comp_data_set cmps' s) in list.first sum_form_comp_data.is_contr elimd.keys -- dot notation doesn't work here?? meta def find_contrad_sfcd_in_sfcd_list (cmps : list sum_form_comp_data) : option sum_form_comp_data := find_contrad_sfcd_in_sfcd_set (rb_set.of_list cmps) meta def find_contrad_in_sfcd_set (cmps : rb_set sum_form_comp_data) : option contrad := do sfcd ← find_contrad_sfcd_in_sfcd_set cmps, return $ contrad.sum_form sfcd.prf meta def find_contrad_in_sfcd_list (cmps : list sum_form_comp_data) : option contrad := find_contrad_in_sfcd_set (rb_set.of_list cmps) meta def is_inconsistent (cmps : rb_set sum_form_comp_data) : bool := (find_contrad_sfcd_in_sfcd_set cmps).is_some meta def is_inconsistent_list (cmps : list sum_form_comp_data) : bool := is_inconsistent $ rb_set.of_list cmps section bb_process meta def mk_eqs_of_expr_sum_form_pair : expr × sum_form → sum_form_comp_data | (e, sf) := let sf' := sf - (sum_form.of_expr e) in ⟨⟨sf', spec_comp.eq⟩, sum_form_proof.of_expr_def e sf', mk_rb_set⟩ private meta def mk_sfcd_list : polya_state (list sum_form_comp_data) := do il ← get_ineq_list, el ← get_eq_list, sl ← get_sign_list, dfs ← get_add_defs, let il' := il.map (λ ⟨lhs, rhs, id⟩, sum_form_comp_data.of_ineq_data id) in let el' := el.map (λ ⟨_, _, ed⟩, sum_form_comp_data.of_eq_data ed) in let sl' := sl.map (λ ⟨_, sd⟩, sum_form_comp_data.of_sign_data sd) in let dfs' := dfs.map mk_eqs_of_expr_sum_form_pair in return $ (((il'.append el').append sl').append dfs').qsort (λ a b, if has_ordering.cmp a b = ordering.lt then tt else ff) private meta def mk_eq_list (cmps : list sum_form_comp_data) : list Σ lhs rhs, eq_data lhs rhs := let il := cmps.map (λ sfcd, sfcd.to_eq_data) in reduce_option_list il private meta def mk_ineq_list (cmps : list sum_form_comp_data) : list Σ lhs rhs, ineq_data lhs rhs := let il := cmps.map (λ sfcd, sfcd.to_ineq_data) in reduce_option_list il private meta def mk_sign_list (cmps : list sum_form_comp_data) : list Σ e, sign_data e := let il := cmps.map (λ sfcd, sfcd.to_sign_data) in reduce_option_list il meta def sum_form.add_new_ineqs (start : rb_set sum_form_comp_data := mk_rb_set) : polya_state (rb_set sum_form_comp_data) := do is_contr ← contr_found, if (/-trace_val-/ ("is_contr", is_contr)).2 then return start else do sfcds ← mk_sfcd_list, let elim_set := elim_list_set sfcds start, let elims := elim_set.to_list, let ineqs := mk_ineq_list elims, let eqs := mk_eq_list elims, let signs := (/-trace_val-/ ("found signs:", mk_sign_list elims)).2, ineqs.mmap' (λ s, add_ineq s.2.2), eqs.mmap' (λ s, add_eq s.2.2), signs.mmap' (λ s, add_sign s.2), return elim_set end bb_process end polya
53ac9ae5475e48f3350fa1c6572cf5007ef956d9
1136b4d61007050cc632ede270de45a662f8dba4
/library/init/category/functor.lean
dfcdaae0cb2e58f23595958a28b29d22e8598569
[ "Apache-2.0" ]
permissive
zk744750315/lean
7fe895f16cc0ef1869238a01cae903bbd623b4a9
c17e5b913b2db687ab38f53285326b9dbb2b1b6e
refs/heads/master
1,618,208,425,413
1,521,520,544,000
1,521,520,936,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
1,204
lean
/- Copyright (c) Luke Nelson and Jared Roesch. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Luke Nelson, Jared Roesch, Sebastian Ullrich, Leonardo de Moura -/ prelude import init.core init.function init.meta.name open function universes u v section set_option auto_param.check_exists false class has_map (f : Type u → Type v) : Type (max (u+1) v) := (map : Π {α β : Type u}, (α → β) → f α → f β) (map_const : Π {α β : Type u}, α → f β → f α := λ α β, map ∘ const β) infixr ` <$> `:100 := has_map.map infixr ` <$ `:100 := has_map.map_const @[reducible] def has_map.map_const_rev {f : Type u → Type v} [has_map f] {α β : Type u} : f β → α → f α := λ a b, b <$ a infixr ` $> `:100 := has_map.map_const_rev class functor (f : Type u → Type v) extends has_map f : Type (max (u+1) v) := (map_const_eq : ∀ {α β : Type u}, @map_const α β = map ∘ const β . control_laws_tac) -- `functor` is indeed a categorical functor (id_map : Π {α : Type u} (x : f α), id <$> x = x) (map_comp : Π {α β γ : Type u} (g : α → β) (h : β → γ) (x : f α), (h ∘ g) <$> x = h <$> g <$> x) end
2548c26ed244137629e30a9a2f179b3364b8a533
957a80ea22c5abb4f4670b250d55534d9db99108
/library/init/meta/converter/interactive.lean
fa9e665cd64c5add00f0f6e81f52b6d9a84f90e4
[ "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
5,841
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura Converter monad for building simplifiers. -/ prelude import init.meta.interactive init.meta.converter.conv namespace conv meta def save_info (p : pos) : conv unit := do s ← tactic.read, tactic.save_info_thunk p (λ _, s.to_format tt) meta def step {α : Type} (c : conv α) : conv unit := c >> return () meta def istep {α : Type} (line0 col0 line col : nat) (c : conv α) : conv unit := tactic.istep line0 col0 line col c meta def execute (c : conv unit) : tactic unit := c meta def solve1 (c : conv unit) : conv unit := tactic.solve1 $ c >> tactic.repeat tactic.reflexivity namespace interactive open lean open lean.parser open interactive open interactive.types meta def itactic : Type := conv unit meta def skip : conv unit := conv.skip meta def whnf : conv unit := conv.whnf meta def dsimp (no_dflt : parse only_flag) (es : parse tactic.simp_arg_list) (attr_names : parse with_ident_list) (cfg : tactic.dsimp_config := {}) : conv unit := do (s, u) ← tactic.mk_simp_set no_dflt attr_names es, conv.dsimp (some s) u cfg meta def trace_lhs : conv unit := lhs >>= tactic.trace meta def change (p : parse texpr) : conv unit := tactic.i_to_expr p >>= conv.change meta def congr : conv unit := conv.congr meta def funext : conv unit := conv.funext private meta def is_relation : conv unit := (lhs >>= tactic.relation_lhs_rhs >> return ()) <|> tactic.fail "current expression is not a relation" meta def to_lhs : conv unit := is_relation >> congr >> tactic.swap >> skip meta def to_rhs : conv unit := is_relation >> congr >> skip meta def done : conv unit := tactic.done meta def find (p : parse parser.pexpr) (c : itactic) : conv unit := do (r, lhs, _) ← tactic.target_lhs_rhs, pat ← tactic.pexpr_to_pattern p, s ← simp_lemmas.mk_default, -- to be able to use congruence lemmas @[congr] (found, new_lhs, pr) ← tactic.ext_simplify_core ff {zeta := ff, beta := ff, single_pass := tt, eta := ff, proj := ff, fail_if_unchanged := ff, memoize := ff} s (λ u, return u) (λ found s r p e, do guard (not found), matched ← (tactic.match_pattern_core reducible pat e >> return tt) <|> return ff, guard matched, ⟨new_e, pr⟩ ← c.convert e r, return (tt, new_e, pr, ff)) (λ a s r p e, tactic.failed) r lhs, when (not found) $ tactic.fail "find converter failed, pattern was not found", update_lhs new_lhs pr meta def for (p : parse parser.pexpr) (occs : parse (list_of small_nat)) (c : itactic) : conv unit := do (r, lhs, _) ← tactic.target_lhs_rhs, pat ← tactic.pexpr_to_pattern p, s ← simp_lemmas.mk_default, -- to be able to use congruence lemmas @[congr] (found, new_lhs, pr) ← tactic.ext_simplify_core 1 {zeta := ff, beta := ff, single_pass := tt, eta := ff, proj := ff, fail_if_unchanged := ff, memoize := ff} s (λ u, return u) (λ i s r p e, do matched ← (tactic.match_pattern_core reducible pat e >> return tt) <|> return ff, guard matched, if i ∈ occs then do ⟨new_e, pr⟩ ← c.convert e r, return (i+1, new_e, some pr, tt) else return (i+1, e, none, tt)) (λ a s r p e, tactic.failed) r lhs, update_lhs new_lhs pr meta def simp (no_dflt : parse only_flag) (hs : parse tactic.simp_arg_list) (attr_names : parse with_ident_list) (cfg : tactic.simp_config_ext := {}) : conv unit := do (s, u) ← tactic.mk_simp_set no_dflt attr_names hs, (r, lhs, rhs) ← tactic.target_lhs_rhs, (new_lhs, pr) ← tactic.simplify s u lhs cfg.to_simp_config r cfg.discharger, update_lhs new_lhs pr, return () meta def guard_lhs (p : parse texpr) : tactic unit := do t ← lhs, tactic.interactive.guard_expr_eq t p section rw open tactic.interactive (rw_rules rw_rule get_rule_eqn_lemmas to_expr') open tactic (rewrite_cfg) private meta def rw_lhs (h : expr) (cfg : rewrite_cfg) : conv unit := do l ← conv.lhs, (new_lhs, prf, _) ← tactic.rewrite h l cfg, update_lhs new_lhs prf private meta def rw_core (rs : list rw_rule) (cfg : rewrite_cfg) : conv unit := rs.mmap' $ λ r, do save_info r.pos, eq_lemmas ← get_rule_eqn_lemmas r, orelse' (do h ← to_expr' r.rule, rw_lhs h {cfg with symm := r.symm}) (eq_lemmas.mfirst $ λ n, do e ← tactic.mk_const n, rw_lhs e {cfg with symm := r.symm}) (eq_lemmas.empty) meta def rewrite (q : parse rw_rules) (cfg : rewrite_cfg := {}) : conv unit := rw_core q.rules cfg meta def rw (q : parse rw_rules) (cfg : rewrite_cfg := {}) : conv unit := rw_core q.rules cfg end rw end interactive end conv namespace tactic namespace interactive open lean open lean.parser open interactive open interactive.types open tactic local postfix `?`:9001 := optional private meta def conv_at (h_name : name) (c : conv unit) : tactic unit := do h ← get_local h_name, h_type ← infer_type h, (new_h_type, pr) ← c.convert h_type, replace_hyp h new_h_type pr, return () private meta def conv_target (c : conv unit) : tactic unit := do t ← target, (new_t, pr) ← c.convert t, replace_target new_t pr, try tactic.triv, try (tactic.reflexivity reducible) meta def conv (loc : parse (tk "at" *> ident)?) (p : parse (tk "in" *> parser.pexpr)?) (c : conv.interactive.itactic) : tactic unit := do let c := match p with | some p := _root_.conv.interactive.find p c | none := c end, match loc with | some h := conv_at h c | none := conv_target c end end interactive end tactic
de44b5aadbc047979d033cecb374b16d568865c4
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/data/seq/wseq.lean
8bcce722ab441bdea6fd814bfca41cc2c5f451e0
[ "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
55,371
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import data.seq.seq import data.dlist universes u v w /- coinductive wseq (α : Type u) : Type u | nil : wseq α | cons : α → wseq α → wseq α | think : wseq α → wseq α -/ /-- Weak sequences. While the `seq` structure allows for lists which may not be finite, a weak sequence also allows the computation of each element to involve an indeterminate amount of computation, including possibly an infinite loop. This is represented as a regular `seq` interspersed with `none` elements to indicate that computation is ongoing. This model is appropriate for Haskell style lazy lists, and is closed under most interesting computation patterns on infinite lists, but conversely it is difficult to extract elements from it. -/ def wseq (α) := seq (option α) namespace wseq variables {α : Type u} {β : Type v} {γ : Type w} /-- Turn a sequence into a weak sequence -/ def of_seq : seq α → wseq α := (<$>) some /-- Turn a list into a weak sequence -/ def of_list (l : list α) : wseq α := of_seq l /-- Turn a stream into a weak sequence -/ def of_stream (l : stream α) : wseq α := of_seq l instance coe_seq : has_coe (seq α) (wseq α) := ⟨of_seq⟩ instance coe_list : has_coe (list α) (wseq α) := ⟨of_list⟩ instance coe_stream : has_coe (stream α) (wseq α) := ⟨of_stream⟩ /-- The empty weak sequence -/ def nil : wseq α := seq.nil instance : inhabited (wseq α) := ⟨nil⟩ /-- Prepend an element to a weak sequence -/ def cons (a : α) : wseq α → wseq α := seq.cons (some a) /-- Compute for one tick, without producing any elements -/ def think : wseq α → wseq α := seq.cons none /-- Destruct a weak sequence, to (eventually possibly) produce either `none` for `nil` or `some (a, s)` if an element is produced. -/ def destruct : wseq α → computation (option (α × wseq α)) := computation.corec (λs, match seq.destruct s with | none := sum.inl none | some (none, s') := sum.inr s' | some (some a, s') := sum.inl (some (a, s')) end) def cases_on {C : wseq α → Sort v} (s : wseq α) (h1 : C nil) (h2 : ∀ x s, C (cons x s)) (h3 : ∀ s, C (think s)) : C s := seq.cases_on s h1 (λ o, option.cases_on o h3 h2) protected def mem (a : α) (s : wseq α) := seq.mem (some a) s instance : has_mem α (wseq α) := ⟨wseq.mem⟩ theorem not_mem_nil (a : α) : a ∉ @nil α := seq.not_mem_nil a /-- Get the head of a weak sequence. This involves a possibly infinite computation. -/ def head (s : wseq α) : computation (option α) := computation.map ((<$>) prod.fst) (destruct s) /-- Encode a computation yielding a weak sequence into additional `think` constructors in a weak sequence -/ def flatten : computation (wseq α) → wseq α := seq.corec (λc, match computation.destruct c with | sum.inl s := seq.omap return (seq.destruct s) | sum.inr c' := some (none, c') end) /-- Get the tail of a weak sequence. This doesn't need a `computation` wrapper, unlike `head`, because `flatten` allows us to hide this in the construction of the weak sequence itself. -/ def tail (s : wseq α) : wseq α := flatten $ (λo, option.rec_on o nil prod.snd) <$> destruct s /-- drop the first `n` elements from `s`. -/ def drop (s : wseq α) : ℕ → wseq α | 0 := s | (n+1) := tail (drop n) attribute [simp] drop /-- Get the nth element of `s`. -/ def nth (s : wseq α) (n : ℕ) : computation (option α) := head (drop s n) /-- Convert `s` to a list (if it is finite and completes in finite time). -/ def to_list (s : wseq α) : computation (list α) := @computation.corec (list α) (list α × wseq α) (λ⟨l, s⟩, match seq.destruct s with | none := sum.inl l.reverse | some (none, s') := sum.inr (l, s') | some (some a, s') := sum.inr (a::l, s') end) ([], s) /-- Get the length of `s` (if it is finite and completes in finite time). -/ def length (s : wseq α) : computation ℕ := @computation.corec ℕ (ℕ × wseq α) (λ⟨n, s⟩, match seq.destruct s with | none := sum.inl n | some (none, s') := sum.inr (n, s') | some (some a, s') := sum.inr (n+1, s') end) (0, s) /-- A weak sequence is finite if `to_list s` terminates. Equivalently, it is a finite number of `think` and `cons` applied to `nil`. -/ class is_finite (s : wseq α) : Prop := (out : (to_list s).terminates) instance to_list_terminates (s : wseq α) [h : is_finite s] : (to_list s).terminates := h.out /-- Get the list corresponding to a finite weak sequence. -/ def get (s : wseq α) [is_finite s] : list α := (to_list s).get /-- A weak sequence is *productive* if it never stalls forever - there are always a finite number of `think`s between `cons` constructors. The sequence itself is allowed to be infinite though. -/ class productive (s : wseq α) : Prop := (nth_terminates : ∀ n, (nth s n).terminates) theorem productive_iff (s : wseq α) : productive s ↔ ∀ n, (nth s n).terminates := ⟨λ h, h.1, λ h, ⟨h⟩⟩ instance nth_terminates (s : wseq α) [h : productive s] : ∀ n, (nth s n).terminates := h.nth_terminates instance head_terminates (s : wseq α) [productive s] : (head s).terminates := s.nth_terminates 0 /-- Replace the `n`th element of `s` with `a`. -/ def update_nth (s : wseq α) (n : ℕ) (a : α) : wseq α := @seq.corec (option α) (ℕ × wseq α) (λ⟨n, s⟩, match seq.destruct s, n with | none, n := none | some (none, s'), n := some (none, n, s') | some (some a', s'), 0 := some (some a', 0, s') | some (some a', s'), 1 := some (some a, 0, s') | some (some a', s'), (n+2) := some (some a', n+1, s') end) (n+1, s) /-- Remove the `n`th element of `s`. -/ def remove_nth (s : wseq α) (n : ℕ) : wseq α := @seq.corec (option α) (ℕ × wseq α) (λ⟨n, s⟩, match seq.destruct s, n with | none, n := none | some (none, s'), n := some (none, n, s') | some (some a', s'), 0 := some (some a', 0, s') | some (some a', s'), 1 := some (none, 0, s') | some (some a', s'), (n+2) := some (some a', n+1, s') end) (n+1, s) /-- Map the elements of `s` over `f`, removing any values that yield `none`. -/ def filter_map (f : α → option β) : wseq α → wseq β := seq.corec (λs, match seq.destruct s with | none := none | some (none, s') := some (none, s') | some (some a, s') := some (f a, s') end) /-- Select the elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] : wseq α → wseq α := filter_map (λa, if p a then some a else none) -- example of infinite list manipulations /-- Get the first element of `s` satisfying `p`. -/ def find (p : α → Prop) [decidable_pred p] (s : wseq α) : computation (option α) := head $ filter p s /-- Zip a function over two weak sequences -/ def zip_with (f : α → β → γ) (s1 : wseq α) (s2 : wseq β) : wseq γ := @seq.corec (option γ) (wseq α × wseq β) (λ⟨s1, s2⟩, match seq.destruct s1, seq.destruct s2 with | some (none, s1'), some (none, s2') := some (none, s1', s2') | some (some a1, s1'), some (none, s2') := some (none, s1, s2') | some (none, s1'), some (some a2, s2') := some (none, s1', s2) | some (some a1, s1'), some (some a2, s2') := some (some (f a1 a2), s1', s2') | _, _ := none end) (s1, s2) /-- Zip two weak sequences into a single sequence of pairs -/ def zip : wseq α → wseq β → wseq (α × β) := zip_with prod.mk /-- Get the list of indexes of elements of `s` satisfying `p` -/ def find_indexes (p : α → Prop) [decidable_pred p] (s : wseq α) : wseq ℕ := (zip s (stream.nats : wseq ℕ)).filter_map (λ ⟨a, n⟩, if p a then some n else none) /-- Get the index of the first element of `s` satisfying `p` -/ def find_index (p : α → Prop) [decidable_pred p] (s : wseq α) : computation ℕ := (λ o, option.get_or_else o 0) <$> head (find_indexes p s) /-- Get the index of the first occurrence of `a` in `s` -/ def index_of [decidable_eq α] (a : α) : wseq α → computation ℕ := find_index (eq a) /-- Get the indexes of occurrences of `a` in `s` -/ def indexes_of [decidable_eq α] (a : α) : wseq α → wseq ℕ := find_indexes (eq a) /-- `union s1 s2` is a weak sequence which interleaves `s1` and `s2` in some order (nondeterministically). -/ def union (s1 s2 : wseq α) : wseq α := @seq.corec (option α) (wseq α × wseq α) (λ⟨s1, s2⟩, match seq.destruct s1, seq.destruct s2 with | none, none := none | some (a1, s1'), none := some (a1, s1', nil) | none, some (a2, s2') := some (a2, nil, s2') | some (none, s1'), some (none, s2') := some (none, s1', s2') | some (some a1, s1'), some (none, s2') := some (some a1, s1', s2') | some (none, s1'), some (some a2, s2') := some (some a2, s1', s2') | some (some a1, s1'), some (some a2, s2') := some (some a1, cons a2 s1', s2') end) (s1, s2) /-- Returns `tt` if `s` is `nil` and `ff` if `s` has an element -/ def is_empty (s : wseq α) : computation bool := computation.map option.is_none $ head s /-- Calculate one step of computation -/ def compute (s : wseq α) : wseq α := match seq.destruct s with | some (none, s') := s' | _ := s end /-- Get the first `n` elements of a weak sequence -/ def take (s : wseq α) (n : ℕ) : wseq α := @seq.corec (option α) (ℕ × wseq α) (λ⟨n, s⟩, match n, seq.destruct s with | 0, _ := none | m+1, none := none | m+1, some (none, s') := some (none, m+1, s') | m+1, some (some a, s') := some (some a, m, s') end) (n, s) /-- Split the sequence at position `n` into a finite initial segment and the weak sequence tail -/ def split_at (s : wseq α) (n : ℕ) : computation (list α × wseq α) := @computation.corec (list α × wseq α) (ℕ × list α × wseq α) (λ⟨n, l, s⟩, match n, seq.destruct s with | 0, _ := sum.inl (l.reverse, s) | m+1, none := sum.inl (l.reverse, s) | m+1, some (none, s') := sum.inr (n, l, s') | m+1, some (some a, s') := sum.inr (m, a::l, s') end) (n, [], s) /-- Returns `tt` if any element of `s` satisfies `p` -/ def any (s : wseq α) (p : α → bool) : computation bool := computation.corec (λs : wseq α, match seq.destruct s with | none := sum.inl ff | some (none, s') := sum.inr s' | some (some a, s') := if p a then sum.inl tt else sum.inr s' end) s /-- Returns `tt` if every element of `s` satisfies `p` -/ def all (s : wseq α) (p : α → bool) : computation bool := computation.corec (λs : wseq α, match seq.destruct s with | none := sum.inl tt | some (none, s') := sum.inr s' | some (some a, s') := if p a then sum.inr s' else sum.inl ff end) s /-- Apply a function to the elements of the sequence to produce a sequence of partial results. (There is no `scanr` because this would require working from the end of the sequence, which may not exist.) -/ def scanl (f : α → β → α) (a : α) (s : wseq β) : wseq α := cons a $ @seq.corec (option α) (α × wseq β) (λ⟨a, s⟩, match seq.destruct s with | none := none | some (none, s') := some (none, a, s') | some (some b, s') := let a' := f a b in some (some a', a', s') end) (a, s) /-- Get the weak sequence of initial segments of the input sequence -/ def inits (s : wseq α) : wseq (list α) := cons [] $ @seq.corec (option (list α)) (dlist α × wseq α) (λ ⟨l, s⟩, match seq.destruct s with | none := none | some (none, s') := some (none, l, s') | some (some a, s') := let l' := l.concat a in some (some l'.to_list, l', s') end) (dlist.empty, s) /-- Like take, but does not wait for a result. Calculates `n` steps of computation and returns the sequence computed so far -/ def collect (s : wseq α) (n : ℕ) : list α := (seq.take n s).filter_map id /-- Append two weak sequences. As with `seq.append`, this may not use the second sequence if the first one takes forever to compute -/ def append : wseq α → wseq α → wseq α := seq.append /-- Map a function over a weak sequence -/ def map (f : α → β) : wseq α → wseq β := seq.map (option.map f) /-- Flatten a sequence of weak sequences. (Note that this allows empty sequences, unlike `seq.join`.) -/ def join (S : wseq (wseq α)) : wseq α := seq.join ((λo : option (wseq α), match o with | none := seq1.ret none | some s := (none, s) end) <$> S) /-- Monadic bind operator for weak sequences -/ def bind (s : wseq α) (f : α → wseq β) : wseq β := join (map f s) @[simp] def lift_rel_o (R : α → β → Prop) (C : wseq α → wseq β → Prop) : option (α × wseq α) → option (β × wseq β) → Prop | none none := true | (some (a, s)) (some (b, t)) := R a b ∧ C s t | _ _ := false theorem lift_rel_o.imp {R S : α → β → Prop} {C D : wseq α → wseq β → Prop} (H1 : ∀ a b, R a b → S a b) (H2 : ∀ s t, C s t → D s t) : ∀ {o p}, lift_rel_o R C o p → lift_rel_o S D o p | none none h := trivial | (some (a, s)) (some (b, t)) h := and.imp (H1 _ _) (H2 _ _) h | none (some _) h := false.elim h | (some (_, _)) none h := false.elim h theorem lift_rel_o.imp_right (R : α → β → Prop) {C D : wseq α → wseq β → Prop} (H : ∀ s t, C s t → D s t) {o p} : lift_rel_o R C o p → lift_rel_o R D o p := lift_rel_o.imp (λ _ _, id) H @[simp] def bisim_o (R : wseq α → wseq α → Prop) : option (α × wseq α) → option (α × wseq α) → Prop := lift_rel_o (=) R theorem bisim_o.imp {R S : wseq α → wseq α → Prop} (H : ∀ s t, R s t → S s t) {o p} : bisim_o R o p → bisim_o S o p := lift_rel_o.imp_right _ H /-- Two weak sequences are `lift_rel R` related if they are either both empty, or they are both nonempty and the heads are `R` related and the tails are `lift_rel R` related. (This is a coinductive definition.) -/ def lift_rel (R : α → β → Prop) (s : wseq α) (t : wseq β) : Prop := ∃ C : wseq α → wseq β → Prop, C s t ∧ ∀ {s t}, C s t → computation.lift_rel (lift_rel_o R C) (destruct s) (destruct t) /-- If two sequences are equivalent, then they have the same values and the same computational behavior (i.e. if one loops forever then so does the other), although they may differ in the number of `think`s needed to arrive at the answer. -/ def equiv : wseq α → wseq α → Prop := lift_rel (=) theorem lift_rel_destruct {R : α → β → Prop} {s : wseq α} {t : wseq β} : lift_rel R s t → computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct s) (destruct t) | ⟨R, h1, h2⟩ := by refine computation.lift_rel.imp _ _ _ (h2 h1); apply lift_rel_o.imp_right; exact λ s' t' h', ⟨R, h', @h2⟩ theorem lift_rel_destruct_iff {R : α → β → Prop} {s : wseq α} {t : wseq β} : lift_rel R s t ↔ computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct s) (destruct t) := ⟨lift_rel_destruct, λ h, ⟨λ s t, lift_rel R s t ∨ computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct s) (destruct t), or.inr h, λ s t h, begin have h : computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct s) (destruct t), { cases h with h h, exact lift_rel_destruct h, assumption }, apply computation.lift_rel.imp _ _ _ h, intros a b, apply lift_rel_o.imp_right, intros s t, apply or.inl end⟩⟩ infix ~ := equiv theorem destruct_congr {s t : wseq α} : s ~ t → computation.lift_rel (bisim_o (~)) (destruct s) (destruct t) := lift_rel_destruct theorem destruct_congr_iff {s t : wseq α} : s ~ t ↔ computation.lift_rel (bisim_o (~)) (destruct s) (destruct t) := lift_rel_destruct_iff theorem lift_rel.refl (R : α → α → Prop) (H : reflexive R) : reflexive (lift_rel R) := λ s, begin refine ⟨(=), rfl, λ s t (h : s = t), _⟩, rw ←h, apply computation.lift_rel.refl, intro a, cases a with a, simp, cases a; simp, apply H end theorem lift_rel_o.swap (R : α → β → Prop) (C) : function.swap (lift_rel_o R C) = lift_rel_o (function.swap R) (function.swap C) := by funext x y; cases x with x; [skip, cases x]; { cases y with y; [skip, cases y]; refl } theorem lift_rel.swap_lem {R : α → β → Prop} {s1 s2} (h : lift_rel R s1 s2) : lift_rel (function.swap R) s2 s1 := begin refine ⟨function.swap (lift_rel R), h, λ s t (h : lift_rel R t s), _⟩, rw [←lift_rel_o.swap, computation.lift_rel.swap], apply lift_rel_destruct h end theorem lift_rel.swap (R : α → β → Prop) : function.swap (lift_rel R) = lift_rel (function.swap R) := funext $ λ x, funext $ λ y, propext ⟨lift_rel.swap_lem, lift_rel.swap_lem⟩ theorem lift_rel.symm (R : α → α → Prop) (H : symmetric R) : symmetric (lift_rel R) := λ s1 s2 (h : function.swap (lift_rel R) s2 s1), by rwa [lift_rel.swap, show function.swap R = R, from funext $ λ a, funext $ λ b, propext $ by constructor; apply H] at h theorem lift_rel.trans (R : α → α → Prop) (H : transitive R) : transitive (lift_rel R) := λ s t u h1 h2, begin refine ⟨λ s u, ∃ t, lift_rel R s t ∧ lift_rel R t u, ⟨t, h1, h2⟩, λ s u h, _⟩, rcases h with ⟨t, h1, h2⟩, have h1 := lift_rel_destruct h1, have h2 := lift_rel_destruct h2, refine computation.lift_rel_def.2 ⟨(computation.terminates_of_lift_rel h1).trans (computation.terminates_of_lift_rel h2), λ a c ha hc, _⟩, rcases h1.left ha with ⟨b, hb, t1⟩, have t2 := computation.rel_of_lift_rel h2 hb hc, cases a with a; cases c with c, { trivial }, { cases b, {cases t2}, {cases t1} }, { cases a, cases b with b, {cases t1}, {cases b, cases t2} }, { cases a with a s, cases b with b, {cases t1}, cases b with b t, cases c with c u, cases t1 with ab st, cases t2 with bc tu, exact ⟨H ab bc, t, st, tu⟩ } end theorem lift_rel.equiv (R : α → α → Prop) : equivalence R → equivalence (lift_rel R) | ⟨refl, symm, trans⟩ := ⟨lift_rel.refl R refl, lift_rel.symm R symm, lift_rel.trans R trans⟩ @[refl] theorem equiv.refl : ∀ (s : wseq α), s ~ s := lift_rel.refl (=) eq.refl @[symm] theorem equiv.symm : ∀ {s t : wseq α}, s ~ t → t ~ s := lift_rel.symm (=) (@eq.symm _) @[trans] theorem equiv.trans : ∀ {s t u : wseq α}, s ~ t → t ~ u → s ~ u := lift_rel.trans (=) (@eq.trans _) theorem equiv.equivalence : equivalence (@equiv α) := ⟨@equiv.refl _, @equiv.symm _, @equiv.trans _⟩ open computation local notation `return` := computation.return @[simp] theorem destruct_nil : destruct (nil : wseq α) = return none := computation.destruct_eq_ret rfl @[simp] theorem destruct_cons (a : α) (s) : destruct (cons a s) = return (some (a, s)) := computation.destruct_eq_ret $ by simp [destruct, cons, computation.rmap] @[simp] theorem destruct_think (s : wseq α) : destruct (think s) = (destruct s).think := computation.destruct_eq_think $ by simp [destruct, think, computation.rmap] @[simp] theorem seq_destruct_nil : seq.destruct (nil : wseq α) = none := seq.destruct_nil @[simp] theorem seq_destruct_cons (a : α) (s) : seq.destruct (cons a s) = some (some a, s) := seq.destruct_cons _ _ @[simp] theorem seq_destruct_think (s : wseq α) : seq.destruct (think s) = some (none, s) := seq.destruct_cons _ _ @[simp] theorem head_nil : head (nil : wseq α) = return none := by simp [head]; refl @[simp] theorem head_cons (a : α) (s) : head (cons a s) = return (some a) := by simp [head]; refl @[simp] theorem head_think (s : wseq α) : head (think s) = (head s).think := by simp [head]; refl @[simp] theorem flatten_ret (s : wseq α) : flatten (return s) = s := begin refine seq.eq_of_bisim (λs1 s2, flatten (return s2) = s1) _ rfl, intros s' s h, rw ←h, simp [flatten], cases seq.destruct s, { simp }, { cases val with o s', simp } end @[simp] theorem flatten_think (c : computation (wseq α)) : flatten c.think = think (flatten c) := seq.destruct_eq_cons $ by simp [flatten, think] @[simp] theorem destruct_flatten (c : computation (wseq α)) : destruct (flatten c) = c >>= destruct := begin refine computation.eq_of_bisim (λc1 c2, c1 = c2 ∨ ∃ c, c1 = destruct (flatten c) ∧ c2 = computation.bind c destruct) _ (or.inr ⟨c, rfl, rfl⟩), intros c1 c2 h, exact match c1, c2, h with | _, _, (or.inl $ eq.refl c) := by cases c.destruct; simp | _, _, (or.inr ⟨c, rfl, rfl⟩) := begin apply c.cases_on (λa, _) (λc', _); repeat {simp}, { cases (destruct a).destruct; simp }, { exact or.inr ⟨c', rfl, rfl⟩ } end end end theorem head_terminates_iff (s : wseq α) : terminates (head s) ↔ terminates (destruct s) := terminates_map_iff _ (destruct s) @[simp] theorem tail_nil : tail (nil : wseq α) = nil := by simp [tail] @[simp] theorem tail_cons (a : α) (s) : tail (cons a s) = s := by simp [tail] @[simp] theorem tail_think (s : wseq α) : tail (think s) = (tail s).think := by simp [tail] @[simp] theorem dropn_nil (n) : drop (nil : wseq α) n = nil := by induction n; simp [*, drop] @[simp] theorem dropn_cons (a : α) (s) (n) : drop (cons a s) (n+1) = drop s n := by induction n; simp [*, drop] @[simp] theorem dropn_think (s : wseq α) (n) : drop (think s) n = (drop s n).think := by induction n; simp [*, drop] theorem dropn_add (s : wseq α) (m) : ∀ n, drop s (m + n) = drop (drop s m) n | 0 := rfl | (n+1) := congr_arg tail (dropn_add n) theorem dropn_tail (s : wseq α) (n) : drop (tail s) n = drop s (n + 1) := by rw add_comm; symmetry; apply dropn_add theorem nth_add (s : wseq α) (m n) : nth s (m + n) = nth (drop s m) n := congr_arg head (dropn_add _ _ _) theorem nth_tail (s : wseq α) (n) : nth (tail s) n = nth s (n + 1) := congr_arg head (dropn_tail _ _) @[simp] theorem join_nil : join nil = (nil : wseq α) := seq.join_nil @[simp] theorem join_think (S : wseq (wseq α)) : join (think S) = think (join S) := by { simp [think, join], unfold functor.map, simp [join, seq1.ret] } @[simp] theorem join_cons (s : wseq α) (S) : join (cons s S) = think (append s (join S)) := by { simp [think, join], unfold functor.map, simp [join, cons, append] } @[simp] theorem nil_append (s : wseq α) : append nil s = s := seq.nil_append _ @[simp] theorem cons_append (a : α) (s t) : append (cons a s) t = cons a (append s t) := seq.cons_append _ _ _ @[simp] theorem think_append (s t : wseq α) : append (think s) t = think (append s t) := seq.cons_append _ _ _ @[simp] theorem append_nil (s : wseq α) : append s nil = s := seq.append_nil _ @[simp] theorem append_assoc (s t u : wseq α) : append (append s t) u = append s (append t u) := seq.append_assoc _ _ _ @[simp] def tail.aux : option (α × wseq α) → computation (option (α × wseq α)) | none := return none | (some (a, s)) := destruct s theorem destruct_tail (s : wseq α) : destruct (tail s) = destruct s >>= tail.aux := begin simp [tail], rw [← bind_pure_comp_eq_map, is_lawful_monad.bind_assoc], apply congr_arg, ext1 (_|⟨a, s⟩); apply (@pure_bind computation _ _ _ _ _ _).trans _; simp end @[simp] def drop.aux : ℕ → option (α × wseq α) → computation (option (α × wseq α)) | 0 := return | (n+1) := λ a, tail.aux a >>= drop.aux n theorem drop.aux_none : ∀ n, @drop.aux α n none = return none | 0 := rfl | (n+1) := show computation.bind (return none) (drop.aux n) = return none, by rw [ret_bind, drop.aux_none] theorem destruct_dropn : ∀ (s : wseq α) n, destruct (drop s n) = destruct s >>= drop.aux n | s 0 := (bind_ret' _).symm | s (n+1) := by rw [← dropn_tail, destruct_dropn _ n, destruct_tail, is_lawful_monad.bind_assoc]; refl theorem head_terminates_of_head_tail_terminates (s : wseq α) [T : terminates (head (tail s))] : terminates (head s) := (head_terminates_iff _).2 $ begin rcases (head_terminates_iff _).1 T with ⟨⟨a, h⟩⟩, simp [tail] at h, rcases exists_of_mem_bind h with ⟨s', h1, h2⟩, unfold functor.map at h1, exact let ⟨t, h3, h4⟩ := exists_of_mem_map h1 in terminates_of_mem h3 end theorem destruct_some_of_destruct_tail_some {s : wseq α} {a} (h : some a ∈ destruct (tail s)) : ∃ a', some a' ∈ destruct s := begin unfold tail functor.map at h, simp at h, rcases exists_of_mem_bind h with ⟨t, tm, td⟩, clear h, rcases exists_of_mem_map tm with ⟨t', ht', ht2⟩, clear tm, cases t' with t'; rw ←ht2 at td; simp at td, { have := mem_unique td (ret_mem _), contradiction }, { exact ⟨_, ht'⟩ } end theorem head_some_of_head_tail_some {s : wseq α} {a} (h : some a ∈ head (tail s)) : ∃ a', some a' ∈ head s := begin unfold head at h, rcases exists_of_mem_map h with ⟨o, md, e⟩, clear h, cases o with o; injection e with h', clear e h', cases destruct_some_of_destruct_tail_some md with a am, exact ⟨_, mem_map ((<$>) (@prod.fst α (wseq α))) am⟩ end theorem head_some_of_nth_some {s : wseq α} {a n} (h : some a ∈ nth s n) : ∃ a', some a' ∈ head s := begin revert a, induction n with n IH; intros, exacts [⟨_, h⟩, let ⟨a', h'⟩ := head_some_of_head_tail_some h in IH h'] end instance productive_tail (s : wseq α) [productive s] : productive (tail s) := ⟨λ n, by rw [nth_tail]; apply_instance⟩ instance productive_dropn (s : wseq α) [productive s] (n) : productive (drop s n) := ⟨λ m, by rw [←nth_add]; apply_instance⟩ /-- Given a productive weak sequence, we can collapse all the `think`s to produce a sequence. -/ def to_seq (s : wseq α) [productive s] : seq α := ⟨λ n, (nth s n).get, λn h, begin cases e : computation.get (nth s (n + 1)), {assumption}, have := mem_of_get_eq _ e, simp [nth] at this h, cases head_some_of_head_tail_some this with a' h', have := mem_unique h' (@mem_of_get_eq _ _ _ _ h), contradiction end⟩ theorem nth_terminates_le {s : wseq α} {m n} (h : m ≤ n) : terminates (nth s n) → terminates (nth s m) := by induction h with m' h IH; [exact id, exact λ T, IH (@head_terminates_of_head_tail_terminates _ _ T)] theorem head_terminates_of_nth_terminates {s : wseq α} {n} : terminates (nth s n) → terminates (head s) := nth_terminates_le (nat.zero_le n) theorem destruct_terminates_of_nth_terminates {s : wseq α} {n} (T : terminates (nth s n)) : terminates (destruct s) := (head_terminates_iff _).1 $ head_terminates_of_nth_terminates T theorem mem_rec_on {C : wseq α → Prop} {a s} (M : a ∈ s) (h1 : ∀ b s', (a = b ∨ C s') → C (cons b s')) (h2 : ∀ s, C s → C (think s)) : C s := begin apply seq.mem_rec_on M, intros o s' h, cases o with b, { apply h2, cases h, {contradiction}, {assumption} }, { apply h1, apply or.imp_left _ h, intro h, injection h } end @[simp] theorem mem_think (s : wseq α) (a) : a ∈ think s ↔ a ∈ s := begin cases s with f al, change some (some a) ∈ some none :: f ↔ some (some a) ∈ f, constructor; intro h, { apply (stream.eq_or_mem_of_mem_cons h).resolve_left, intro, injections }, { apply stream.mem_cons_of_mem _ h } end theorem eq_or_mem_iff_mem {s : wseq α} {a a' s'} : some (a', s') ∈ destruct s → (a ∈ s ↔ a = a' ∨ a ∈ s') := begin generalize e : destruct s = c, intro h, revert s, apply computation.mem_rec_on h _ (λ c IH, _); intro s; apply s.cases_on _ (λ x s, _) (λ s, _); intros m; have := congr_arg computation.destruct m; simp at this; cases this with i1 i2, { rw [i1, i2], cases s' with f al, unfold cons has_mem.mem wseq.mem seq.mem seq.cons, simp, have h_a_eq_a' : a = a' ↔ some (some a) = some (some a'), {simp}, rw [h_a_eq_a'], refine ⟨stream.eq_or_mem_of_mem_cons, λo, _⟩, { cases o with e m, { rw e, apply stream.mem_cons }, { exact stream.mem_cons_of_mem _ m } } }, { simp, exact IH this } end @[simp] theorem mem_cons_iff (s : wseq α) (b) {a} : a ∈ cons b s ↔ a = b ∨ a ∈ s := eq_or_mem_iff_mem $ by simp [ret_mem] theorem mem_cons_of_mem {s : wseq α} (b) {a} (h : a ∈ s) : a ∈ cons b s := (mem_cons_iff _ _).2 (or.inr h) theorem mem_cons (s : wseq α) (a) : a ∈ cons a s := (mem_cons_iff _ _).2 (or.inl rfl) theorem mem_of_mem_tail {s : wseq α} {a} : a ∈ tail s → a ∈ s := begin intro h, have := h, cases h with n e, revert s, simp [stream.nth], induction n with n IH; intro s; apply s.cases_on _ (λx s, _) (λ s, _); repeat{simp}; intros m e; injections, { exact or.inr m }, { exact or.inr m }, { apply IH m, rw e, cases tail s, refl } end theorem mem_of_mem_dropn {s : wseq α} {a} : ∀ {n}, a ∈ drop s n → a ∈ s | 0 h := h | (n+1) h := @mem_of_mem_dropn n (mem_of_mem_tail h) theorem nth_mem {s : wseq α} {a n} : some a ∈ nth s n → a ∈ s := begin revert s, induction n with n IH; intros s h, { rcases exists_of_mem_map h with ⟨o, h1, h2⟩, cases o with o; injection h2 with h', cases o with a' s', exact (eq_or_mem_iff_mem h1).2 (or.inl h'.symm) }, { have := @IH (tail s), rw nth_tail at this, exact mem_of_mem_tail (this h) } end theorem exists_nth_of_mem {s : wseq α} {a} (h : a ∈ s) : ∃ n, some a ∈ nth s n := begin apply mem_rec_on h, { intros a' s' h, cases h with h h, { existsi 0, simp [nth], rw h, apply ret_mem }, { cases h with n h, existsi n+1, simp [nth], exact h } }, { intros s' h, cases h with n h, existsi n, simp [nth], apply think_mem h } end theorem exists_dropn_of_mem {s : wseq α} {a} (h : a ∈ s) : ∃ n s', some (a, s') ∈ destruct (drop s n) := let ⟨n, h⟩ := exists_nth_of_mem h in ⟨n, begin rcases (head_terminates_iff _).1 ⟨⟨_, h⟩⟩ with ⟨⟨o, om⟩⟩, have := mem_unique (mem_map _ om) h, cases o with o; injection this with i, cases o with a' s', dsimp at i, rw i at om, exact ⟨_, om⟩ end⟩ theorem lift_rel_dropn_destruct {R : α → β → Prop} {s t} (H : lift_rel R s t) : ∀ n, computation.lift_rel (lift_rel_o R (lift_rel R)) (destruct (drop s n)) (destruct (drop t n)) | 0 := lift_rel_destruct H | (n+1) := begin simp [destruct_tail], apply lift_rel_bind, apply lift_rel_dropn_destruct n, exact λ a b o, match a, b, o with | none, none, _ := by simp | some (a, s), some (b, t), ⟨h1, h2⟩ := by simp [tail.aux]; apply lift_rel_destruct h2 end end theorem exists_of_lift_rel_left {R : α → β → Prop} {s t} (H : lift_rel R s t) {a} (h : a ∈ s) : ∃ {b}, b ∈ t ∧ R a b := let ⟨n, h⟩ := exists_nth_of_mem h, ⟨some (._, s'), sd, rfl⟩ := exists_of_mem_map h, ⟨some (b, t'), td, ⟨ab, _⟩⟩ := (lift_rel_dropn_destruct H n).left sd in ⟨b, nth_mem (mem_map ((<$>) prod.fst.{v v}) td), ab⟩ theorem exists_of_lift_rel_right {R : α → β → Prop} {s t} (H : lift_rel R s t) {b} (h : b ∈ t) : ∃ {a}, a ∈ s ∧ R a b := by rw ←lift_rel.swap at H; exact exists_of_lift_rel_left H h theorem head_terminates_of_mem {s : wseq α} {a} (h : a ∈ s) : terminates (head s) := let ⟨n, h⟩ := exists_nth_of_mem h in head_terminates_of_nth_terminates ⟨⟨_, h⟩⟩ theorem of_mem_append {s₁ s₂ : wseq α} {a : α} : a ∈ append s₁ s₂ → a ∈ s₁ ∨ a ∈ s₂ := seq.of_mem_append theorem mem_append_left {s₁ s₂ : wseq α} {a : α} : a ∈ s₁ → a ∈ append s₁ s₂ := seq.mem_append_left theorem exists_of_mem_map {f} {b : β} : ∀ {s : wseq α}, b ∈ map f s → ∃ a, a ∈ s ∧ f a = b | ⟨g, al⟩ h := let ⟨o, om, oe⟩ := seq.exists_of_mem_map h in by cases o with a; injection oe with h'; exact ⟨a, om, h'⟩ @[simp] theorem lift_rel_nil (R : α → β → Prop) : lift_rel R nil nil := by rw [lift_rel_destruct_iff]; simp @[simp] theorem lift_rel_cons (R : α → β → Prop) (a b s t) : lift_rel R (cons a s) (cons b t) ↔ R a b ∧ lift_rel R s t := by rw [lift_rel_destruct_iff]; simp @[simp] theorem lift_rel_think_left (R : α → β → Prop) (s t) : lift_rel R (think s) t ↔ lift_rel R s t := by rw [lift_rel_destruct_iff, lift_rel_destruct_iff]; simp @[simp] theorem lift_rel_think_right (R : α → β → Prop) (s t) : lift_rel R s (think t) ↔ lift_rel R s t := by rw [lift_rel_destruct_iff, lift_rel_destruct_iff]; simp theorem cons_congr {s t : wseq α} (a : α) (h : s ~ t) : cons a s ~ cons a t := by unfold equiv; simp; exact h theorem think_equiv (s : wseq α) : think s ~ s := by unfold equiv; simp; apply equiv.refl theorem think_congr {s t : wseq α} (a : α) (h : s ~ t) : think s ~ think t := by unfold equiv; simp; exact h theorem head_congr : ∀ {s t : wseq α}, s ~ t → head s ~ head t := suffices ∀ {s t : wseq α}, s ~ t → ∀ {o}, o ∈ head s → o ∈ head t, from λ s t h o, ⟨this h, this h.symm⟩, begin intros s t h o ho, rcases @computation.exists_of_mem_map _ _ _ _ (destruct s) ho with ⟨ds, dsm, dse⟩, rw ←dse, cases destruct_congr h with l r, rcases l dsm with ⟨dt, dtm, dst⟩, cases ds with a; cases dt with b, { apply mem_map _ dtm }, { cases b, cases dst }, { cases a, cases dst }, { cases a with a s', cases b with b t', rw dst.left, exact @mem_map _ _ (@functor.map _ _ (α × wseq α) _ prod.fst) _ (destruct t) dtm } end theorem flatten_equiv {c : computation (wseq α)} {s} (h : s ∈ c) : flatten c ~ s := begin apply computation.mem_rec_on h, { simp }, { intro s', apply equiv.trans, simp [think_equiv] } end theorem lift_rel_flatten {R : α → β → Prop} {c1 : computation (wseq α)} {c2 : computation (wseq β)} (h : c1.lift_rel (lift_rel R) c2) : lift_rel R (flatten c1) (flatten c2) := let S := λ s t, ∃ c1 c2, s = flatten c1 ∧ t = flatten c2 ∧ computation.lift_rel (lift_rel R) c1 c2 in ⟨S, ⟨c1, c2, rfl, rfl, h⟩, λ s t h, match s, t, h with ._, ._, ⟨c1, c2, rfl, rfl, h⟩ := begin simp, apply lift_rel_bind _ _ h, intros a b ab, apply computation.lift_rel.imp _ _ _ (lift_rel_destruct ab), intros a b, apply lift_rel_o.imp_right, intros s t h, refine ⟨return s, return t, _, _, _⟩; simp [h] end end⟩ theorem flatten_congr {c1 c2 : computation (wseq α)} : computation.lift_rel equiv c1 c2 → flatten c1 ~ flatten c2 := lift_rel_flatten theorem tail_congr {s t : wseq α} (h : s ~ t) : tail s ~ tail t := begin apply flatten_congr, unfold functor.map, rw [←bind_ret, ←bind_ret], apply lift_rel_bind _ _ (destruct_congr h), intros a b h, simp, cases a with a; cases b with b, { trivial }, { cases h }, { cases a, cases h }, { cases a with a s', cases b with b t', exact h.right } end theorem dropn_congr {s t : wseq α} (h : s ~ t) (n) : drop s n ~ drop t n := by induction n; simp [*, tail_congr] theorem nth_congr {s t : wseq α} (h : s ~ t) (n) : nth s n ~ nth t n := head_congr (dropn_congr h _) theorem mem_congr {s t : wseq α} (h : s ~ t) (a) : a ∈ s ↔ a ∈ t := suffices ∀ {s t : wseq α}, s ~ t → a ∈ s → a ∈ t, from ⟨this h, this h.symm⟩, λ s t h as, let ⟨n, hn⟩ := exists_nth_of_mem as in nth_mem ((nth_congr h _ _).1 hn) theorem productive_congr {s t : wseq α} (h : s ~ t) : productive s ↔ productive t := by simp only [productive_iff]; exact forall_congr (λ n, terminates_congr $ nth_congr h _) theorem equiv.ext {s t : wseq α} (h : ∀ n, nth s n ~ nth t n) : s ~ t := ⟨λ s t, ∀ n, nth s n ~ nth t n, h, λs t h, begin refine lift_rel_def.2 ⟨_, _⟩, { rw [←head_terminates_iff, ←head_terminates_iff], exact terminates_congr (h 0) }, { intros a b ma mb, cases a with a; cases b with b, { trivial }, { injection mem_unique (mem_map _ ma) ((h 0 _).2 (mem_map _ mb)) }, { injection mem_unique (mem_map _ ma) ((h 0 _).2 (mem_map _ mb)) }, { cases a with a s', cases b with b t', injection mem_unique (mem_map _ ma) ((h 0 _).2 (mem_map _ mb)) with ab, refine ⟨ab, λ n, _⟩, refine (nth_congr (flatten_equiv (mem_map _ ma)) n).symm.trans ((_ : nth (tail s) n ~ nth (tail t) n).trans (nth_congr (flatten_equiv (mem_map _ mb)) n)), rw [nth_tail, nth_tail], apply h } } end⟩ theorem length_eq_map (s : wseq α) : length s = computation.map list.length (to_list s) := begin refine eq_of_bisim (λ c1 c2, ∃ (l : list α) (s : wseq α), c1 = corec length._match_2 (l.length, s) ∧ c2 = computation.map list.length (corec to_list._match_2 (l, s))) _ ⟨[], s, rfl, rfl⟩, intros s1 s2 h, rcases h with ⟨l, s, h⟩, rw [h.left, h.right], apply s.cases_on _ (λ a s, _) (λ s, _); repeat {simp [to_list, nil, cons, think, length]}, { refine ⟨a::l, s, _, _⟩; simp }, { refine ⟨l, s, _, _⟩; simp } end @[simp] theorem of_list_nil : of_list [] = (nil : wseq α) := rfl @[simp] theorem of_list_cons (a : α) (l) : of_list (a :: l) = cons a (of_list l) := show seq.map some (seq.of_list (a :: l)) = seq.cons (some a) (seq.map some (seq.of_list l)), by simp @[simp] theorem to_list'_nil (l : list α) : corec to_list._match_2 (l, nil) = return l.reverse := destruct_eq_ret rfl @[simp] theorem to_list'_cons (l : list α) (s : wseq α) (a : α) : corec to_list._match_2 (l, cons a s) = (corec to_list._match_2 (a::l, s)).think := destruct_eq_think $ by simp [to_list, cons] @[simp] theorem to_list'_think (l : list α) (s : wseq α) : corec to_list._match_2 (l, think s) = (corec to_list._match_2 (l, s)).think := destruct_eq_think $ by simp [to_list, think] theorem to_list'_map (l : list α) (s : wseq α) : corec to_list._match_2 (l, s) = ((++) l.reverse) <$> to_list s := begin refine eq_of_bisim (λ c1 c2, ∃ (l' : list α) (s : wseq α), c1 = corec to_list._match_2 (l' ++ l, s) ∧ c2 = computation.map ((++) l.reverse) (corec to_list._match_2 (l', s))) _ ⟨[], s, rfl, rfl⟩, intros s1 s2 h, rcases h with ⟨l', s, h⟩, rw [h.left, h.right], apply s.cases_on _ (λ a s, _) (λ s, _); repeat {simp [to_list, nil, cons, think, length]}, { refine ⟨a::l', s, _, _⟩; simp }, { refine ⟨l', s, _, _⟩; simp } end @[simp] theorem to_list_cons (a : α) (s) : to_list (cons a s) = (list.cons a <$> to_list s).think := destruct_eq_think $ by unfold to_list; simp; rw to_list'_map; simp; refl @[simp] theorem to_list_nil : to_list (nil : wseq α) = return [] := destruct_eq_ret rfl theorem to_list_of_list (l : list α) : l ∈ to_list (of_list l) := by induction l with a l IH; simp [ret_mem]; exact think_mem (mem_map _ IH) @[simp] theorem destruct_of_seq (s : seq α) : destruct (of_seq s) = return (s.head.map $ λ a, (a, of_seq s.tail)) := destruct_eq_ret $ begin simp [of_seq, head, destruct, seq.destruct, seq.head], rw [show seq.nth (some <$> s) 0 = some <$> seq.nth s 0, by apply seq.map_nth], cases seq.nth s 0 with a, { refl }, unfold functor.map, simp [destruct] end @[simp] theorem head_of_seq (s : seq α) : head (of_seq s) = return s.head := by simp [head]; cases seq.head s; refl @[simp] theorem tail_of_seq (s : seq α) : tail (of_seq s) = of_seq s.tail := begin simp [tail], apply s.cases_on _ (λ x s, _); simp [of_seq], {refl}, rw [seq.head_cons, seq.tail_cons], refl end @[simp] theorem dropn_of_seq (s : seq α) : ∀ n, drop (of_seq s) n = of_seq (s.drop n) | 0 := rfl | (n+1) := by dsimp [drop]; rw [dropn_of_seq, tail_of_seq] theorem nth_of_seq (s : seq α) (n) : nth (of_seq s) n = return (seq.nth s n) := by dsimp [nth]; rw [dropn_of_seq, head_of_seq, seq.head_dropn] instance productive_of_seq (s : seq α) : productive (of_seq s) := ⟨λ n, by rw nth_of_seq; apply_instance⟩ theorem to_seq_of_seq (s : seq α) : to_seq (of_seq s) = s := begin apply subtype.eq, funext n, dsimp [to_seq], apply get_eq_of_mem, rw nth_of_seq, apply ret_mem end /-- The monadic `return a` is a singleton list containing `a`. -/ def ret (a : α) : wseq α := of_list [a] @[simp] theorem map_nil (f : α → β) : map f nil = nil := rfl @[simp] theorem map_cons (f : α → β) (a s) : map f (cons a s) = cons (f a) (map f s) := seq.map_cons _ _ _ @[simp] theorem map_think (f : α → β) (s) : map f (think s) = think (map f s) := seq.map_cons _ _ _ @[simp] theorem map_id (s : wseq α) : map id s = s := by simp [map] @[simp] theorem map_ret (f : α → β) (a) : map f (ret a) = ret (f a) := by simp [ret] @[simp] theorem map_append (f : α → β) (s t) : map f (append s t) = append (map f s) (map f t) := seq.map_append _ _ _ theorem map_comp (f : α → β) (g : β → γ) (s : wseq α) : map (g ∘ f) s = map g (map f s) := begin dsimp [map], rw ←seq.map_comp, apply congr_fun, apply congr_arg, ext ⟨⟩; refl end theorem mem_map (f : α → β) {a : α} {s : wseq α} : a ∈ s → f a ∈ map f s := seq.mem_map (option.map f) -- The converse is not true without additional assumptions theorem exists_of_mem_join {a : α} : ∀ {S : wseq (wseq α)}, a ∈ join S → ∃ s, s ∈ S ∧ a ∈ s := suffices ∀ ss : wseq α, a ∈ ss → ∀ s S, append s (join S) = ss → a ∈ append s (join S) → a ∈ s ∨ ∃ s, s ∈ S ∧ a ∈ s, from λ S h, (this _ h nil S (by simp) (by simp [h])).resolve_left (not_mem_nil _), begin intros ss h, apply mem_rec_on h (λ b ss o, _) (λ ss IH, _); intros s S, { refine s.cases_on (S.cases_on _ (λ s S, _) (λ S, _)) (λ b' s, _) (λ s, _); intros ej m; simp at ej; have := congr_arg seq.destruct ej; simp at this; try {cases this}; try {contradiction}, substs b' ss, simp at m ⊢, cases o with e IH, { simp [e] }, cases m with e m, { simp [e] }, exact or.imp_left or.inr (IH _ _ rfl m) }, { refine s.cases_on (S.cases_on _ (λ s S, _) (λ S, _)) (λ b' s, _) (λ s, _); intros ej m; simp at ej; have := congr_arg seq.destruct ej; simp at this; try { try {have := this.1}, contradiction }; subst ss, { apply or.inr, simp at m ⊢, cases IH s S rfl m with as ex, { exact ⟨s, or.inl rfl, as⟩ }, { rcases ex with ⟨s', sS, as⟩, exact ⟨s', or.inr sS, as⟩ } }, { apply or.inr, simp at m, rcases (IH nil S (by simp) (by simp [m])).resolve_left (not_mem_nil _) with ⟨s, sS, as⟩, exact ⟨s, by simp [sS], as⟩ }, { simp at m IH ⊢, apply IH _ _ rfl m } } end theorem exists_of_mem_bind {s : wseq α} {f : α → wseq β} {b} (h : b ∈ bind s f) : ∃ a ∈ s, b ∈ f a := let ⟨t, tm, bt⟩ := exists_of_mem_join h, ⟨a, as, e⟩ := exists_of_mem_map tm in ⟨a, as, by rwa e⟩ theorem destruct_map (f : α → β) (s : wseq α) : destruct (map f s) = computation.map (option.map (prod.map f (map f))) (destruct s) := begin apply eq_of_bisim (λ c1 c2, ∃ s, c1 = destruct (map f s) ∧ c2 = computation.map (option.map (prod.map f (map f))) (destruct s)), { intros c1 c2 h, cases h with s h, rw [h.left, h.right], apply s.cases_on _ (λ a s, _) (λ s, _); simp, exact ⟨s, rfl, rfl⟩ }, { exact ⟨s, rfl, rfl⟩ } end theorem lift_rel_map {δ} (R : α → β → Prop) (S : γ → δ → Prop) {s1 : wseq α} {s2 : wseq β} {f1 : α → γ} {f2 : β → δ} (h1 : lift_rel R s1 s2) (h2 : ∀ {a b}, R a b → S (f1 a) (f2 b)) : lift_rel S (map f1 s1) (map f2 s2) := ⟨λ s1 s2, ∃ s t, s1 = map f1 s ∧ s2 = map f2 t ∧ lift_rel R s t, ⟨s1, s2, rfl, rfl, h1⟩, λ s1 s2 h, match s1, s2, h with ._, ._, ⟨s, t, rfl, rfl, h⟩ := begin simp [destruct_map], apply computation.lift_rel_map _ _ (lift_rel_destruct h), intros o p h, cases o with a; cases p with b; simp, { cases b; cases h }, { cases a; cases h }, { cases a with a s; cases b with b t, cases h with r h, exact ⟨h2 r, s, rfl, t, rfl, h⟩ } end end⟩ theorem map_congr (f : α → β) {s t : wseq α} (h : s ~ t) : map f s ~ map f t := lift_rel_map _ _ h (λ _ _, congr_arg _) @[simp] def destruct_append.aux (t : wseq α) : option (α × wseq α) → computation (option (α × wseq α)) | none := destruct t | (some (a, s)) := return (some (a, append s t)) theorem destruct_append (s t : wseq α) : destruct (append s t) = (destruct s).bind (destruct_append.aux t) := begin apply eq_of_bisim (λ c1 c2, ∃ s t, c1 = destruct (append s t) ∧ c2 = (destruct s).bind (destruct_append.aux t)) _ ⟨s, t, rfl, rfl⟩, intros c1 c2 h, rcases h with ⟨s, t, h⟩, rw [h.left, h.right], apply s.cases_on _ (λ a s, _) (λ s, _); simp, { apply t.cases_on _ (λ b t, _) (λ t, _); simp, { refine ⟨nil, t, _, _⟩; simp } }, { exact ⟨s, t, rfl, rfl⟩ } end @[simp] def destruct_join.aux : option (wseq α × wseq (wseq α)) → computation (option (α × wseq α)) | none := return none | (some (s, S)) := (destruct (append s (join S))).think theorem destruct_join (S : wseq (wseq α)) : destruct (join S) = (destruct S).bind destruct_join.aux := begin apply eq_of_bisim (λ c1 c2, c1 = c2 ∨ ∃ S, c1 = destruct (join S) ∧ c2 = (destruct S).bind destruct_join.aux) _ (or.inr ⟨S, rfl, rfl⟩), intros c1 c2 h, exact match c1, c2, h with | _, _, (or.inl $ eq.refl c) := by cases c.destruct; simp | _, _, or.inr ⟨S, rfl, rfl⟩ := begin apply S.cases_on _ (λ s S, _) (λ S, _); simp, { refine or.inr ⟨S, rfl, rfl⟩ } end end end theorem lift_rel_append (R : α → β → Prop) {s1 s2 : wseq α} {t1 t2 : wseq β} (h1 : lift_rel R s1 t1) (h2 : lift_rel R s2 t2) : lift_rel R (append s1 s2) (append t1 t2) := ⟨λ s t, lift_rel R s t ∨ ∃ s1 t1, s = append s1 s2 ∧ t = append t1 t2 ∧ lift_rel R s1 t1, or.inr ⟨s1, t1, rfl, rfl, h1⟩, λ s t h, match s, t, h with | s, t, or.inl h := begin apply computation.lift_rel.imp _ _ _ (lift_rel_destruct h), intros a b, apply lift_rel_o.imp_right, intros s t, apply or.inl end | ._, ._, or.inr ⟨s1, t1, rfl, rfl, h⟩ := begin simp [destruct_append], apply computation.lift_rel_bind _ _ (lift_rel_destruct h), intros o p h, cases o with a; cases p with b, { simp, apply computation.lift_rel.imp _ _ _ (lift_rel_destruct h2), intros a b, apply lift_rel_o.imp_right, intros s t, apply or.inl }, { cases b; cases h }, { cases a; cases h }, { cases a with a s; cases b with b t, cases h with r h, simp, exact ⟨r, or.inr ⟨s, rfl, t, rfl, h⟩⟩ } end end⟩ theorem lift_rel_join.lem (R : α → β → Prop) {S T} {U : wseq α → wseq β → Prop} (ST : lift_rel (lift_rel R) S T) (HU : ∀ s1 s2, (∃ s t S T, s1 = append s (join S) ∧ s2 = append t (join T) ∧ lift_rel R s t ∧ lift_rel (lift_rel R) S T) → U s1 s2) {a} (ma : a ∈ destruct (join S)) : ∃ {b}, b ∈ destruct (join T) ∧ lift_rel_o R U a b := begin cases exists_results_of_mem ma with n h, clear ma, revert a S T, apply nat.strong_induction_on n _, intros n IH a S T ST ra, simp [destruct_join] at ra, exact let ⟨o, m, k, rs1, rs2, en⟩ := of_results_bind ra, ⟨p, mT, rop⟩ := computation.exists_of_lift_rel_left (lift_rel_destruct ST) rs1.mem in by exact match o, p, rop, rs1, rs2, mT with | none, none, _, rs1, rs2, mT := by simp only [destruct_join]; exact ⟨none, mem_bind mT (ret_mem _), by rw eq_of_ret_mem rs2.mem; trivial⟩ | some (s, S'), some (t, T'), ⟨st, ST'⟩, rs1, rs2, mT := by simp [destruct_append] at rs2; exact let ⟨k1, rs3, ek⟩ := of_results_think rs2, ⟨o', m1, n1, rs4, rs5, ek1⟩ := of_results_bind rs3, ⟨p', mt, rop'⟩ := computation.exists_of_lift_rel_left (lift_rel_destruct st) rs4.mem in by exact match o', p', rop', rs4, rs5, mt with | none, none, _, rs4, rs5', mt := have n1 < n, begin rw [en, ek, ek1], apply lt_of_lt_of_le _ (nat.le_add_right _ _), apply nat.lt_succ_of_le (nat.le_add_right _ _) end, let ⟨ob, mb, rob⟩ := IH _ this ST' rs5' in by refine ⟨ob, _, rob⟩; { simp [destruct_join], apply mem_bind mT, simp [destruct_append], apply think_mem, apply mem_bind mt, exact mb } | some (a, s'), some (b, t'), ⟨ab, st'⟩, rs4, rs5, mt := begin simp at rs5, refine ⟨some (b, append t' (join T')), _, _⟩, { simp [destruct_join], apply mem_bind mT, simp [destruct_append], apply think_mem, apply mem_bind mt, apply ret_mem }, rw eq_of_ret_mem rs5.mem, exact ⟨ab, HU _ _ ⟨s', t', S', T', rfl, rfl, st', ST'⟩⟩ end end end end theorem lift_rel_join (R : α → β → Prop) {S : wseq (wseq α)} {T : wseq (wseq β)} (h : lift_rel (lift_rel R) S T) : lift_rel R (join S) (join T) := ⟨λ s1 s2, ∃ s t S T, s1 = append s (join S) ∧ s2 = append t (join T) ∧ lift_rel R s t ∧ lift_rel (lift_rel R) S T, ⟨nil, nil, S, T, by simp, by simp, by simp, h⟩, λs1 s2 ⟨s, t, S, T, h1, h2, st, ST⟩, begin clear _fun_match _x, rw [h1, h2], rw [destruct_append, destruct_append], apply computation.lift_rel_bind _ _ (lift_rel_destruct st), exact λ o p h, match o, p, h with | some (a, s), some (b, t), ⟨h1, h2⟩ := by simp; exact ⟨h1, s, t, S, rfl, T, rfl, h2, ST⟩ | none, none, _ := begin dsimp [destruct_append.aux, computation.lift_rel], constructor, { intro, apply lift_rel_join.lem _ ST (λ _ _, id) }, { intros b mb, rw [←lift_rel_o.swap], apply lift_rel_join.lem (function.swap R), { rw [←lift_rel.swap R, ←lift_rel.swap], apply ST }, { rw [←lift_rel.swap R, ←lift_rel.swap (lift_rel R)], exact λ s1 s2 ⟨s, t, S, T, h1, h2, st, ST⟩, ⟨t, s, T, S, h2, h1, st, ST⟩ }, { exact mb } } end end end⟩ theorem join_congr {S T : wseq (wseq α)} (h : lift_rel equiv S T) : join S ~ join T := lift_rel_join _ h theorem lift_rel_bind {δ} (R : α → β → Prop) (S : γ → δ → Prop) {s1 : wseq α} {s2 : wseq β} {f1 : α → wseq γ} {f2 : β → wseq δ} (h1 : lift_rel R s1 s2) (h2 : ∀ {a b}, R a b → lift_rel S (f1 a) (f2 b)) : lift_rel S (bind s1 f1) (bind s2 f2) := lift_rel_join _ (lift_rel_map _ _ h1 @h2) theorem bind_congr {s1 s2 : wseq α} {f1 f2 : α → wseq β} (h1 : s1 ~ s2) (h2 : ∀ a, f1 a ~ f2 a) : bind s1 f1 ~ bind s2 f2 := lift_rel_bind _ _ h1 (λ a b h, by rw h; apply h2) @[simp] theorem join_ret (s : wseq α) : join (ret s) ~ s := by simp [ret]; apply think_equiv @[simp] theorem join_map_ret (s : wseq α) : join (map ret s) ~ s := begin refine ⟨λ s1 s2, join (map ret s2) = s1, rfl, _⟩, intros s' s h, rw ←h, apply lift_rel_rec (λ c1 c2, ∃ s, c1 = destruct (join (map ret s)) ∧ c2 = destruct s), { exact λ c1 c2 h, match c1, c2, h with | ._, ._, ⟨s, rfl, rfl⟩ := begin clear h _match, have : ∀ s, ∃ s' : wseq α, (map ret s).join.destruct = (map ret s').join.destruct ∧ destruct s = s'.destruct, from λ s, ⟨s, rfl, rfl⟩, apply s.cases_on _ (λ a s, _) (λ s, _); simp [ret, ret_mem, this, option.exists] end end }, { exact ⟨s, rfl, rfl⟩ } end @[simp] theorem join_append (S T : wseq (wseq α)) : join (append S T) ~ append (join S) (join T) := begin refine ⟨λ s1 s2, ∃ s S T, s1 = append s (join (append S T)) ∧ s2 = append s (append (join S) (join T)), ⟨nil, S, T, by simp, by simp⟩, _⟩, intros s1 s2 h, apply lift_rel_rec (λ c1 c2, ∃ (s : wseq α) S T, c1 = destruct (append s (join (append S T))) ∧ c2 = destruct (append s (append (join S) (join T)))) _ _ _ (let ⟨s, S, T, h1, h2⟩ := h in ⟨s, S, T, congr_arg destruct h1, congr_arg destruct h2⟩), intros c1 c2 h, exact match c1, c2, h with ._, ._, ⟨s, S, T, rfl, rfl⟩ := begin clear _match h h, apply wseq.cases_on s _ (λ a s, _) (λ s, _); simp, { apply wseq.cases_on S _ (λ s S, _) (λ S, _); simp, { apply wseq.cases_on T _ (λ s T, _) (λ T, _); simp, { refine ⟨s, nil, T, _, _⟩; simp }, { refine ⟨nil, nil, T, _, _⟩; simp } }, { exact ⟨s, S, T, rfl, rfl⟩ }, { refine ⟨nil, S, T, _, _⟩; simp } }, { exact ⟨s, S, T, rfl, rfl⟩ }, { exact ⟨s, S, T, rfl, rfl⟩ } end end end @[simp] theorem bind_ret (f : α → β) (s) : bind s (ret ∘ f) ~ map f s := begin dsimp [bind], change (λx, ret (f x)) with (ret ∘ f), rw [map_comp], apply join_map_ret end @[simp] theorem ret_bind (a : α) (f : α → wseq β) : bind (ret a) f ~ f a := by simp [bind] @[simp] theorem map_join (f : α → β) (S) : map f (join S) = join (map (map f) S) := begin apply seq.eq_of_bisim (λs1 s2, ∃ s S, s1 = append s (map f (join S)) ∧ s2 = append s (join (map (map f) S))), { intros s1 s2 h, exact match s1, s2, h with ._, ._, ⟨s, S, rfl, rfl⟩ := begin apply wseq.cases_on s _ (λ a s, _) (λ s, _); simp, { apply wseq.cases_on S _ (λ s S, _) (λ S, _); simp, { exact ⟨map f s, S, rfl, rfl⟩ }, { refine ⟨nil, S, _, _⟩; simp } }, { exact ⟨_, _, rfl, rfl⟩ }, { exact ⟨_, _, rfl, rfl⟩ } end end }, { refine ⟨nil, S, _, _⟩; simp } end @[simp] theorem join_join (SS : wseq (wseq (wseq α))) : join (join SS) ~ join (map join SS) := begin refine ⟨λ s1 s2, ∃ s S SS, s1 = append s (join (append S (join SS))) ∧ s2 = append s (append (join S) (join (map join SS))), ⟨nil, nil, SS, by simp, by simp⟩, _⟩, intros s1 s2 h, apply lift_rel_rec (λ c1 c2, ∃ s S SS, c1 = destruct (append s (join (append S (join SS)))) ∧ c2 = destruct (append s (append (join S) (join (map join SS))))) _ (destruct s1) (destruct s2) (let ⟨s, S, SS, h1, h2⟩ := h in ⟨s, S, SS, by simp [h1], by simp [h2]⟩), intros c1 c2 h, exact match c1, c2, h with ._, ._, ⟨s, S, SS, rfl, rfl⟩ := begin clear _match h h, apply wseq.cases_on s _ (λ a s, _) (λ s, _); simp, { apply wseq.cases_on S _ (λ s S, _) (λ S, _); simp, { apply wseq.cases_on SS _ (λ S SS, _) (λ SS, _); simp, { refine ⟨nil, S, SS, _, _⟩; simp }, { refine ⟨nil, nil, SS, _, _⟩; simp } }, { exact ⟨s, S, SS, rfl, rfl⟩ }, { refine ⟨nil, S, SS, _, _⟩; simp } }, { exact ⟨s, S, SS, rfl, rfl⟩ }, { exact ⟨s, S, SS, rfl, rfl⟩ } end end end @[simp] theorem bind_assoc (s : wseq α) (f : α → wseq β) (g : β → wseq γ) : bind (bind s f) g ~ bind s (λ (x : α), bind (f x) g) := begin simp [bind], rw [← map_comp f (map g), map_comp (map g ∘ f) join], apply join_join end instance : monad wseq := { map := @map, pure := @ret, bind := @bind } /- Unfortunately, wseq is not a lawful monad, because it does not satisfy the monad laws exactly, only up to sequence equivalence. Furthermore, even quotienting by the equivalence is not sufficient, because the join operation involves lists of quotient elements, with a lifted equivalence relation, and pure quotients cannot handle this type of construction. instance : is_lawful_monad wseq := { id_map := @map_id, bind_pure_comp_eq_map := @bind_ret, pure_bind := @ret_bind, bind_assoc := @bind_assoc } -/ end wseq
617bbb5a8770e17b57d3f18b250dc0b6a9d1cc7a
4727251e0cd73359b15b664c3170e5d754078599
/src/category_theory/preadditive/projective.lean
ff2a5cbe534a87062f3d6d1df3fde9d3cf3687c2
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
6,499
lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel, Scott Morrison -/ import algebra.homology.exact import category_theory.types import category_theory.limits.shapes.biproducts /-! # Projective objects and categories with enough projectives An object `P` is called projective if every morphism out of `P` factors through every epimorphism. A category `C` has enough projectives if every object admits an epimorphism from some projective object. `projective.over X` picks an arbitrary such projective object, and `projective.π X : projective.over X ⟶ X` is the corresponding epimorphism. Given a morphism `f : X ⟶ Y`, `projective.left f` is a projective object over `kernel f`, and `projective.d f : projective.left f ⟶ X` is the morphism `π (kernel f) ≫ kernel.ι f`. -/ noncomputable theory open category_theory open category_theory.limits universes v u namespace category_theory variables {C : Type u} [category.{v} C] /-- An object `P` is called projective if every morphism out of `P` factors through every epimorphism. -/ class projective (P : C) : Prop := (factors : ∀ {E X : C} (f : P ⟶ X) (e : E ⟶ X) [epi e], ∃ f', f' ≫ e = f) section /-- A projective presentation of an object `X` consists of an epimorphism `f : P ⟶ X` from some projective object `P`. -/ @[nolint has_inhabited_instance] structure projective_presentation (X : C) := (P : C) (projective : projective P . tactic.apply_instance) (f : P ⟶ X) (epi : epi f . tactic.apply_instance) variables (C) /-- A category "has enough projectives" if for every object `X` there is a projective object `P` and an epimorphism `P ↠ X`. -/ class enough_projectives : Prop := (presentation : ∀ (X : C), nonempty (projective_presentation X)) end namespace projective /-- An arbitrarily chosen factorisation of a morphism out of a projective object through an epimorphism. -/ def factor_thru {P X E : C} [projective P] (f : P ⟶ X) (e : E ⟶ X) [epi e] : P ⟶ E := (projective.factors f e).some @[simp] lemma factor_thru_comp {P X E : C} [projective P] (f : P ⟶ X) (e : E ⟶ X) [epi e] : factor_thru f e ≫ e = f := (projective.factors f e).some_spec section open_locale zero_object instance zero_projective [has_zero_object C] [has_zero_morphisms C] : projective (0 : C) := { factors := λ E X f e epi, by { use 0, ext, }} end lemma of_iso {P Q : C} (i : P ≅ Q) (hP : projective P) : projective Q := begin fsplit, introsI E X f e e_epi, obtain ⟨f', hf'⟩ := projective.factors (i.hom ≫ f) e, exact ⟨i.inv ≫ f', by simp [hf']⟩ end lemma iso_iff {P Q : C} (i : P ≅ Q) : projective P ↔ projective Q := ⟨of_iso i, of_iso i.symm⟩ /-- The axiom of choice says that every type is a projective object in `Type`. -/ instance (X : Type u) : projective X := { factors := λ E X' f e epi, ⟨λ x, ((epi_iff_surjective _).mp epi (f x)).some, by { ext x, exact ((epi_iff_surjective _).mp epi (f x)).some_spec, }⟩ } instance Type.enough_projectives : enough_projectives (Type u) := { presentation := λ X, ⟨{ P := X, f := 𝟙 X, }⟩, } instance {P Q : C} [has_binary_coproduct P Q] [projective P] [projective Q] : projective (P ⨿ Q) := { factors := λ E X' f e epi, by exactI ⟨coprod.desc (factor_thru (coprod.inl ≫ f) e) (factor_thru (coprod.inr ≫ f) e), by tidy⟩, } instance {β : Type v} (g : β → C) [has_coproduct g] [∀ b, projective (g b)] : projective (∐ g) := { factors := λ E X' f e epi, by exactI ⟨sigma.desc (λ b, factor_thru (sigma.ι g b ≫ f) e), by tidy⟩, } instance {P Q : C} [has_zero_morphisms C] [has_binary_biproduct P Q] [projective P] [projective Q] : projective (P ⊞ Q) := { factors := λ E X' f e epi, by exactI ⟨biprod.desc (factor_thru (biprod.inl ≫ f) e) (factor_thru (biprod.inr ≫ f) e), by tidy⟩, } instance {β : Type v} [decidable_eq β] (g : β → C) [has_zero_morphisms C] [has_biproduct g] [∀ b, projective (g b)] : projective (⨁ g) := { factors := λ E X' f e epi, by exactI ⟨biproduct.desc (λ b, factor_thru (biproduct.ι g b ≫ f) e), by tidy⟩, } section enough_projectives variables [enough_projectives C] /-- `projective.over X` provides an arbitrarily chosen projective object equipped with an epimorphism `projective.π : projective.over X ⟶ X`. -/ def over (X : C) : C := (enough_projectives.presentation X).some.P instance projective_over (X : C) : projective (over X) := (enough_projectives.presentation X).some.projective /-- The epimorphism `projective.π : projective.over X ⟶ X` from the arbitrarily chosen projective object over `X`. -/ def π (X : C) : over X ⟶ X := (enough_projectives.presentation X).some.f instance π_epi (X : C) : epi (π X) := (enough_projectives.presentation X).some.epi section variables [has_zero_morphisms C] {X Y : C} (f : X ⟶ Y) [has_kernel f] /-- When `C` has enough projectives, the object `projective.syzygies f` is an arbitrarily chosen projective object over `kernel f`. -/ @[derive projective] def syzygies : C := over (kernel f) /-- When `C` has enough projectives, `projective.d f : projective.syzygies f ⟶ X` is the composition `π (kernel f) ≫ kernel.ι f`. (When `C` is abelian, we have `exact (projective.d f) f`.) -/ abbreviation d : syzygies f ⟶ X := π (kernel f) ≫ kernel.ι f end end enough_projectives end projective open projective section variables [has_zero_morphisms C] [has_equalizers C] [has_images C] /-- Given a projective object `P` mapping via `h` into the middle object `R` of a pair of exact morphisms `f : Q ⟶ R` and `g : R ⟶ S`, such that `h ≫ g = 0`, there is a lift of `h` to `Q`. -/ def exact.lift {P Q R S : C} [projective P] (h : P ⟶ R) (f : Q ⟶ R) (g : R ⟶ S) (hfg : exact f g) (w : h ≫ g = 0) : P ⟶ Q := factor_thru (factor_thru (factor_thru_kernel_subobject g h w) (image_to_kernel f g hfg.w)) (factor_thru_image_subobject f) @[simp] lemma exact.lift_comp {P Q R S : C} [projective P] (h : P ⟶ R) (f : Q ⟶ R) (g : R ⟶ S) (hfg : exact f g) (w : h ≫ g = 0) : exact.lift h f g hfg w ≫ f = h := begin simp [exact.lift], conv_lhs { congr, skip, rw ← image_subobject_arrow_comp f, }, rw [←category.assoc, factor_thru_comp, ←image_to_kernel_arrow, ←category.assoc, category_theory.projective.factor_thru_comp, factor_thru_kernel_subobject_comp_arrow], end end end category_theory
ea126321e7a885cc59312a57883d8cff9c8cebb0
e481c7ff8466483a1cc4f8f9b020202609da362a
/mathlibtools/decls.lean
eccd074556e39ef3928cbc9751606dcd3318a5c6
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib-tools
61b2000d48624c03ff3718ef9492d8dbb6f91b1f
23f16313a659674570ed5bc25f339be027a0c5b4
refs/heads/master
1,693,438,940,126
1,693,335,201,000
1,693,335,201,000
186,169,342
33
43
Apache-2.0
1,693,335,554,000
1,557,599,877,000
Python
UTF-8
Lean
false
false
1,453
lean
import data.list.sort meta.expr system.io open tactic declaration environment io io.fs (put_str_ln close) -- The next instance is there to prevent PyYAML trying to be too smart meta def my_name_to_string : has_to_string name := ⟨λ n, "\"" ++ to_string n ++ "\""⟩ local attribute [instance] my_name_to_string meta def pos_line (p : option pos) : string := match p with | some x := to_string x.line | _ := "" end meta def file_name (p : option string) : string := match p with | some x := x | _ := "" end meta def print_item_crawl (env : environment) (h : handle) (decl : declaration) : io unit := let name := decl.to_name in do put_str_ln h ((to_string name) ++ ":"), put_str_ln h (" File: " ++ file_name (env.decl_olean name)), put_str_ln h (" Line: " ++ pos_line (env.decl_pos name)) /-- itersplit l n will cut a list l into 2^n pieces (not preserving order) -/ meta def itersplit {α} : list α → ℕ → list (list α) | l 0 := [l] | l 1 := let (l1, l2) := l.split in [l1, l2] | l (k+2) := let (l1, l2) := l.split in itersplit l1 (k+1) ++ itersplit l2 (k+1) meta def main : io unit := do curr_env ← run_tactic get_env, h ← mk_file_handle "decls.yaml" mode.write, let decls := curr_env.fold [] list.cons, let filtered_decls := decls.filter (λ x, not (to_name x).is_internal), let pieces := itersplit filtered_decls 6, pieces.mmap' (λ l, l.mmap' (print_item_crawl curr_env h)), close h
698e1469a4557194dc25f3f86345b9c6bc5760e2
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/simp_univ_poly.lean
52e0cea0ac19d3c444d1427f4dfce61711706fc3
[ "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
765
lean
universes u v def equinumerous (α : Type u) (β : Type v) := ∃ f : α → β, function.bijective f local infix ` ≈ ` := equinumerous @[refl] lemma refl {α} : α ≈ α := sorry @[trans] lemma trans {α β γ} : α ≈ β → β ≈ γ → α ≈ γ := sorry @[congr] lemma equinumerous.congr_eqn {α α' β β'} : α ≈ α' → β ≈ β' → (α ≈ β) = (α' ≈ β') := sorry @[congr] lemma congr_sum {α α' β β'} : α ≈ α' → β ≈ β' → (α ⊕ β) ≈ (α' ⊕ β') := sorry @[simp] lemma eqn_ulift {α} : ulift α ≈ α := sorry @[simp] lemma sum_empty {α} : (α ⊕ empty) ≈ α := sorry -- rewriting `ulift empty` ==> `empty` changes the universe level example {α : Type u} : (α ⊕ ulift empty) ≈ α := by simp
eb3773ac18ac544deb01e61b2a59a4eee40834d0
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/linear_algebra/matrix/mv_polynomial.lean
64a648d3bd5de3400ec704128193ab0a23f3cf86
[ "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,882
lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import linear_algebra.matrix.determinant import data.mv_polynomial.basic import data.mv_polynomial.comm_ring /-! # Matrices of multivariate polynomials > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. In this file, we prove results about matrices over an mv_polynomial ring. In particular, we provide `matrix.mv_polynomial_X` which associates every entry of a matrix with a unique variable. ## Tags matrix determinant, multivariate polynomial -/ variables {m n R S : Type*} namespace matrix variables (m n R) /-- The matrix with variable `X (i,j)` at location `(i,j)`. -/ noncomputable def mv_polynomial_X [comm_semiring R] : matrix m n (mv_polynomial (m × n) R) := of $ λ i j, mv_polynomial.X (i, j) -- TODO: set as an equation lemma for `mv_polynomial_X`, see mathlib4#3024 @[simp] lemma mv_polynomial_X_apply [comm_semiring R] (i j) : mv_polynomial_X m n R i j = mv_polynomial.X (i, j) := rfl variables {m n R S} /-- Any matrix `A` can be expressed as the evaluation of `matrix.mv_polynomial_X`. This is of particular use when `mv_polynomial (m × n) R` is an integral domain but `S` is not, as if the `mv_polynomial.eval₂` can be pulled to the outside of a goal, it can be solved in under cancellative assumptions. -/ lemma mv_polynomial_X_map_eval₂ [comm_semiring R] [comm_semiring S] (f : R →+* S) (A : matrix m n S) : (mv_polynomial_X m n R).map (mv_polynomial.eval₂ f $ λ p : m × n, A p.1 p.2) = A := ext $ λ i j, mv_polynomial.eval₂_X _ (λ p : m × n, A p.1 p.2) (i, j) /-- A variant of `matrix.mv_polynomial_X_map_eval₂` with a bundled `ring_hom` on the LHS. -/ lemma mv_polynomial_X_map_matrix_eval [fintype m] [decidable_eq m] [comm_semiring R] (A : matrix m m R) : (mv_polynomial.eval $ λ p : m × m, A p.1 p.2).map_matrix (mv_polynomial_X m m R) = A := mv_polynomial_X_map_eval₂ _ A variables (R) /-- A variant of `matrix.mv_polynomial_X_map_eval₂` with a bundled `alg_hom` on the LHS. -/ lemma mv_polynomial_X_map_matrix_aeval [fintype m] [decidable_eq m] [comm_semiring R] [comm_semiring S] [algebra R S] (A : matrix m m S) : (mv_polynomial.aeval $ λ p : m × m, A p.1 p.2).map_matrix (mv_polynomial_X m m R) = A := mv_polynomial_X_map_eval₂ _ A variables (m R) /-- In a nontrivial ring, `matrix.mv_polynomial_X m m R` has non-zero determinant. -/ lemma det_mv_polynomial_X_ne_zero [decidable_eq m] [fintype m] [comm_ring R] [nontrivial R] : det (mv_polynomial_X m m R) ≠ 0 := begin intro h_det, have := congr_arg matrix.det (mv_polynomial_X_map_matrix_eval (1 : matrix m m R)), rw [det_one, ←ring_hom.map_det, h_det, ring_hom.map_zero] at this, exact zero_ne_one this, end end matrix