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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
27560fa4ad10cd0d5363b07a4c1015fcb5cc7be4 | ed27983dd289b3bcad416f0b1927105d6ef19db8 | /src/inClassNotes/propositions/predicates_equality.lean | dbfe9c20c84354a765a1c13047b7ae79b2bfb2d3 | [] | no_license | liuxin-James/complogic-s21 | 0d55b76dbe25024473d31d98b5b83655c365f811 | 13e03e0114626643b44015c654151fb651603486 | refs/heads/master | 1,681,109,264,463 | 1,618,848,261,000 | 1,618,848,261,000 | 337,599,491 | 0 | 0 | null | 1,613,141,619,000 | 1,612,925,555,000 | null | UTF-8 | Lean | false | false | 5,784 | lean |
import inClassNotes.propositions.propositions_as_types_the_idea
/-
It's now an easy jump to using type
families indexed by two types to represent
predicates with two arguments. For example,
here's an equality predicate, representing
an equality relation, on terms of type day.
-/
open day
inductive eq_day : day → day → Prop
| eq_day_mo : eq_day mo mo
| eq_day_tu : eq_day tu tu
| eq_day_we : eq_day we we
| eq_day_th : eq_day th th
| eq_day_fr : eq_day fr fr
| eq_day_sa : eq_day sa sa
| eq_day_su : eq_day su su
/-
eq_day = { (mo,mo), (tu,tu), ..., (su,su) }
-/
open eq_day
#check eq_day mo mo -- inhabited thus true
#check eq_day mo tu -- not inhabited, not true
#check eq_day fr sa -- not inhabited, not true
lemma mos_equal : eq_day mo mo := eq_day_mo
lemma mo_tu_equal : eq_day mo tu := _ -- stuck
/-
We use predicates specify sets and relations.
Unary predicates specify sets. Binary and n-ary
predicates more generally specify relations.
For example, always_rains_on : day → Prop
*with its particular proof constructors*
represents the set { mo }, as mo is the only
proposition in the family of propositions
for which there's a proof, i.e., that is
inhabited.
Similarly, our eq_day : day → day → Prop
type family / binary predicate specifies
the 7-element binary relation (a set of
pairs), { (mo,mo), ..., (su,su)}, in the
sense these are all of and the only values
for which eq_day yields a proposition for
which there's a proof.
-/
/-
We can even use our home-grown equality
predicate (a binary relation) on days to
re-write our (always_rains_on d) predicate
using eq_day so that we can finish our
proof above.
-/
inductive always_rains_on' : day → Prop
| mo_rainy' : ∀ (d : day), (eq_day d mo) → always_rains_on' d
open always_rains_on'
lemma bad_tuesdays' : always_rains_on' tu := mo_rainy' tu _ -- stuck
lemma bad_fridays' : always_rains_on' fr := mo_rainy' fr _ -- stuck
lemma bad_mondays' : always_rains_on' mo := mo_rainy' mo eq_day_mo
/-
As an aside you can of course use tactic mode.
-/
lemma bad_mondays'' : always_rains_on' mo := /- term -/
begin
apply mo_rainy' _ _,
exact eq_day_mo,
end
/-
We're now completely set up to look at
a first crucial application of these
ideas: the definition of an equality
predicate in Lean. We just saw how we
can represent an equality relation on
days as a two-place predicate.
We generalize our approach by defining
a polymorphic type family, eq. The eq
type family takes on type parameter, α,
and two *values* of that type, a1 and
a2, (just as our eq_day predicate takes
two values of type day). The resulting
proposition is (eq a b), for which Lean
gives us infix syntactic sugar, a = b.
The type parameter is implicit.
That defines our family of propositions.
They include propositions such as follow.
-/
#check eq 1 1 -- a1=1
#check eq 0 1 -- 0 = 1
#check eq tt tt -- tt = tt
#check eq tt ff -- tt = ff
#check eq "hi" "there" -- "hi" = "there"
#check eq mo tu -- mo = tu
/-
The following terms are not propositions
in Lean, for they do not even type check.
-/
#check eq 1 tt
#check eq "hi" 2
#check eq tt "Lean!"
/-
The final trick is that there is just
one constructor for eq, called "refl"
(generally written eq.refl) that takes
*one* argument, a : α, and constructs
a value of type (eq a a), aka a=a, which
we take to be a proof of equality. The
upshot is that you can generate a proof that
any value of any type is equal to itself
and there is no way to create a proof of
any other equality.
-/
#check @eq
/-
Here's Lean's exact definition of the
eq relation as a type family with some
members having proofs of equalty, and
other members not. The real key is to
define the constructors/axioms so that
you get exacty the relation you want!
-/
namespace eql
universe u
inductive eq {α : Sort u} (a : α) : α → Prop
| refl [] : eq a -- skip notation for now
-- You can see exactly what's going on here
#check @eq
#check eq 1 1
#check 1 = 1 -- same thing
#check @eq.refl
#check eq.refl 1
end eql
example : 1 = 1 := eq.refl 1
example : 1 = 2 := _
/-
Moreover, Lean reduces arguments to calls
so you can use eq.refl to construct proofs
of propositions such as 1 + 1 = 2.
-/
example : 1 + 1 = 2 := eq.refl 2
example : "Hello, " ++ "Lean!" = "Hello, Lean!" := eq.refl "Hello, Lean!"
/-
By the way, "example", is like def, lemma
or theorem, but you don't bind a name to
a term. It's useful for show that a type
is inhabited by giving an example of a
value of that type. It's particularly
useful when you don't care which value
of a type is produced, since you're not
go to do anything else with it anyway,
given that it's unnamed.
-/
/-
A note on convenient shortcuts. The rfl
function uses inference pretty heavily.
-/
example : 1 + 1 = 4 - 2 := rfl
/-
Here's the definition of rfl from Lean's library.
def rfl {α : Sort u} {a : α} : a = a := eq.refl a
Lean has to be able to infer both the (α : Type)
and (a : α) value for this definition to work.
-/
/-
We've thus specified the binary relation, equality,
the equality predicate, on terms of any type. What
are its properties and related functions?
-/
/-
Properties of equality
-- reflexive
-- symmetric
-- transitive
An equivalence relation
-/
#check @eq.refl
#check @eq.symm
#check @eq.trans
#check @eq.rec
inductive ev : ℕ → Prop
| ev_0 : ev 0
| ev_ss : ∀ (n : ℕ), ev n → ev (n+2)
open ev
lemma zero_even : ev 0 := ev_0
lemma two_even : ev 2 := ev_ss 0 ev_0
lemma four_even : ev 4 := ev_ss 2 two_even
lemma four_even' : ev 4 :=
ev_ss -- emphasizes recursive structure of proof term
2
(ev_ss
0
ev_0
)
lemma five_hundred_even : ev 500 :=
begin
repeat { apply ev_ss },
_
end |
65c5b1b6bb9993c45359364eb5dc8f1892070146 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/geometry/manifold/algebra/structures.lean | b8fad067971af9abae3e673d4f9d18a33fe22219 | [
"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,631 | lean | /-
Copyright © 2020 Nicolò Cavalleri. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Nicolò Cavalleri
-/
import geometry.manifold.algebra.lie_group
/-!
# Smooth structures
In this file we define smooth structures that build on Lie groups. We prefer using the term smooth
instead of Lie mainly because Lie ring has currently another use in mathematics.
-/
open_locale manifold
section smooth_ring
variables {𝕜 : Type*} [nontrivially_normed_field 𝕜]
{H : Type*} [topological_space H]
{E : Type*} [normed_add_comm_group E] [normed_space 𝕜 E]
set_option default_priority 100 -- see Note [default priority]
/-- A smooth (semi)ring is a (semi)ring `R` where addition and multiplication are smooth.
If `R` is a ring, then negation is automatically smooth, as it is multiplication with `-1`. -/
-- See note [Design choices about smooth algebraic structures]
class smooth_ring (I : model_with_corners 𝕜 E H)
(R : Type*) [semiring R] [topological_space R] [charted_space H R]
extends has_smooth_add I R : Prop :=
(smooth_mul : smooth (I.prod I) I (λ p : R×R, p.1 * p.2))
instance smooth_ring.to_has_smooth_mul (I : model_with_corners 𝕜 E H)
(R : Type*) [semiring R] [topological_space R] [charted_space H R] [h : smooth_ring I R] :
has_smooth_mul I R := { ..h }
instance smooth_ring.to_lie_add_group (I : model_with_corners 𝕜 E H)
(R : Type*) [ring R] [topological_space R] [charted_space H R] [smooth_ring I R] :
lie_add_group I R :=
{ compatible := λ e e', has_groupoid.compatible (cont_diff_groupoid ⊤ I),
smooth_add := smooth_add I,
smooth_neg := by simpa only [neg_one_mul] using @smooth_mul_left 𝕜 _ H _ E _ _ I R _ _ _ _ (-1) }
end smooth_ring
instance field_smooth_ring {𝕜 : Type*} [nontrivially_normed_field 𝕜] :
smooth_ring 𝓘(𝕜) 𝕜 :=
{ smooth_mul :=
begin
rw smooth_iff,
refine ⟨continuous_mul, λ x y, _⟩,
simp only [prod.mk.eta] with mfld_simps,
rw cont_diff_on_univ,
exact cont_diff_mul,
end,
..normed_space_lie_add_group }
variables {𝕜 R E H : Type*} [topological_space R] [topological_space H]
[nontrivially_normed_field 𝕜] [normed_add_comm_group E] [normed_space 𝕜 E]
[charted_space H R] (I : model_with_corners 𝕜 E H)
/-- A smooth (semi)ring is a topological (semi)ring. This is not an instance for technical reasons,
see note [Design choices about smooth algebraic structures]. -/
lemma topological_semiring_of_smooth [semiring R] [smooth_ring I R] :
topological_semiring R :=
{ .. has_continuous_mul_of_smooth I, .. has_continuous_add_of_smooth I }
|
5f2aef4ca7c3ce781dbeb0e8b7fc378c98cadd4a | 4efff1f47634ff19e2f786deadd394270a59ecd2 | /src/topology/continuous_on.lean | 83026c795bbbbaa4e48a5fab1825f9878a6e8e7d | [
"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 | 33,739 | lean | /-
Copyright (c) 2019 Reid Barton. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel
-/
import topology.constructions
/-!
# Neighborhoods and continuity relative to a subset
This file defines relative versions
* `nhds_within` of `nhds`
* `continuous_on` of `continuous`
* `continuous_within_at` of `continuous_at`
and proves their basic properties, including the relationships between
these restricted notions and the corresponding notions for the subtype
equipped with the subspace topology.
## Notation
* `𝓝 x`: the filter of neighborhoods of a point `x`;
* `𝓟 s`: the principal filter of a set `s`;
* `𝓝[s] x`: the filter `nhds_within x s` of neighborhoods of a point `x` within a set `s`.
-/
open set filter
open_locale topological_space filter
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
variables [topological_space α]
/-- The "neighborhood within" filter. Elements of `𝓝[s] a` are sets containing the
intersection of `s` and a neighborhood of `a`. -/
def nhds_within (a : α) (s : set α) : filter α := 𝓝 a ⊓ 𝓟 s
localized "notation `𝓝[` s `] ` x:100 := nhds_within x s" in topological_space
@[simp] lemma nhds_bind_nhds_within {a : α} {s : set α} :
(𝓝 a).bind (λ x, 𝓝[s] x) = 𝓝[s] a :=
bind_inf_principal.trans $ congr_arg2 _ nhds_bind_nhds rfl
@[simp] lemma eventually_nhds_nhds_within {a : α} {s : set α} {p : α → Prop} :
(∀ᶠ y in 𝓝 a, ∀ᶠ x in 𝓝[s] y, p x) ↔ ∀ᶠ x in 𝓝[s] a, p x :=
filter.ext_iff.1 nhds_bind_nhds_within {x | p x}
lemma eventually_nhds_within_iff {a : α} {s : set α} {p : α → Prop} :
(∀ᶠ x in 𝓝[s] a, p x) ↔ ∀ᶠ x in 𝓝 a, x ∈ s → p x :=
eventually_inf_principal
@[simp] lemma eventually_nhds_within_nhds_within {a : α} {s : set α} {p : α → Prop} :
(∀ᶠ y in 𝓝[s] a, ∀ᶠ x in 𝓝[s] y, p x) ↔ ∀ᶠ x in 𝓝[s] a, p x :=
begin
refine ⟨λ h, _, λ h, (eventually_nhds_nhds_within.2 h).filter_mono inf_le_left⟩,
simp only [eventually_nhds_within_iff] at h ⊢,
exact h.mono (λ x hx hxs, (hx hxs).self_of_nhds hxs)
end
theorem nhds_within_eq (a : α) (s : set α) :
𝓝[s] a = ⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, 𝓟 (t ∩ s) :=
((nhds_basis_opens a).inf_principal s).eq_binfi
theorem nhds_within_univ (a : α) : 𝓝[set.univ] a = 𝓝 a :=
by rw [nhds_within, principal_univ, inf_top_eq]
lemma nhds_within_has_basis {p : β → Prop} {s : β → set α} {a : α} (h : (𝓝 a).has_basis p s)
(t : set α) :
(𝓝[t] a).has_basis p (λ i, s i ∩ t) :=
h.inf_principal t
lemma nhds_within_basis_open (a : α) (t : set α) :
(𝓝[t] a).has_basis (λ u, a ∈ u ∧ is_open u) (λ u, u ∩ t) :=
nhds_within_has_basis (nhds_basis_opens a) t
theorem mem_nhds_within {t : set α} {a : α} {s : set α} :
t ∈ 𝓝[s] a ↔ ∃ u, is_open u ∧ a ∈ u ∧ u ∩ s ⊆ t :=
by simpa only [exists_prop, and_assoc, and_comm] using (nhds_within_basis_open a s).mem_iff
lemma mem_nhds_within_iff_exists_mem_nhds_inter {t : set α} {a : α} {s : set α} :
t ∈ 𝓝[s] a ↔ ∃ u ∈ 𝓝 a, u ∩ s ⊆ t :=
(nhds_within_has_basis (𝓝 a).basis_sets s).mem_iff
lemma nhds_of_nhds_within_of_nhds
{s t : set α} {a : α} (h1 : s ∈ 𝓝 a) (h2 : t ∈ 𝓝[s] a) : (t ∈ 𝓝 a) :=
begin
rcases mem_nhds_within_iff_exists_mem_nhds_inter.mp h2 with ⟨_, Hw, hw⟩,
exact (nhds a).sets_of_superset ((nhds a).inter_sets Hw h1) hw,
end
lemma mem_nhds_within_of_mem_nhds {s t : set α} {a : α} (h : s ∈ 𝓝 a) :
s ∈ 𝓝[t] a :=
mem_inf_sets_of_left h
theorem self_mem_nhds_within {a : α} {s : set α} : s ∈ 𝓝[s] a :=
mem_inf_sets_of_right (mem_principal_self s)
theorem inter_mem_nhds_within (s : set α) {t : set α} {a : α} (h : t ∈ 𝓝 a) :
s ∩ t ∈ 𝓝[s] a :=
inter_mem_sets (mem_inf_sets_of_right (mem_principal_self s)) (mem_inf_sets_of_left h)
theorem nhds_within_mono (a : α) {s t : set α} (h : s ⊆ t) : 𝓝[s] a ≤ 𝓝[t] a :=
inf_le_inf_left _ (principal_mono.mpr h)
lemma pure_le_nhds_within {a : α} {s : set α} (ha : a ∈ s) : pure a ≤ 𝓝[s] a :=
le_inf (pure_le_nhds a) (le_principal_iff.2 ha)
lemma mem_of_mem_nhds_within {a : α} {s t : set α} (ha : a ∈ s) (ht : t ∈ 𝓝[s] a) :
a ∈ t :=
pure_le_nhds_within ha ht
lemma filter.eventually.self_of_nhds_within {p : α → Prop} {s : set α} {x : α}
(h : ∀ᶠ y in 𝓝[s] x, p y) (hx : x ∈ s) : p x :=
mem_of_mem_nhds_within hx h
lemma tendsto_const_nhds_within {l : filter β} {s : set α} {a : α} (ha : a ∈ s) :
tendsto (λ x : β, a) l (𝓝[s] a) :=
tendsto_const_pure.mono_right $ pure_le_nhds_within ha
theorem nhds_within_restrict'' {a : α} (s : set α) {t : set α} (h : t ∈ 𝓝[s] a) :
𝓝[s] a = 𝓝[s ∩ t] a :=
le_antisymm
(le_inf inf_le_left (le_principal_iff.mpr (inter_mem_sets self_mem_nhds_within h)))
(inf_le_inf_left _ (principal_mono.mpr (set.inter_subset_left _ _)))
theorem nhds_within_restrict' {a : α} (s : set α) {t : set α} (h : t ∈ 𝓝 a) :
𝓝[s] a = 𝓝[s ∩ t] a :=
nhds_within_restrict'' s $ mem_inf_sets_of_left h
theorem nhds_within_restrict {a : α} (s : set α) {t : set α} (h₀ : a ∈ t) (h₁ : is_open t) :
𝓝[s] a = 𝓝[s ∩ t] a :=
nhds_within_restrict' s (mem_nhds_sets h₁ h₀)
theorem nhds_within_le_of_mem {a : α} {s t : set α} (h : s ∈ 𝓝[t] a) :
𝓝[t] a ≤ 𝓝[s] a :=
begin
rcases mem_nhds_within.1 h with ⟨u, u_open, au, uts⟩,
have : 𝓝[t] a = 𝓝[t ∩ u] a := nhds_within_restrict _ au u_open,
rw [this, inter_comm],
exact nhds_within_mono _ uts
end
theorem nhds_within_eq_nhds_within {a : α} {s t u : set α}
(h₀ : a ∈ s) (h₁ : is_open s) (h₂ : t ∩ s = u ∩ s) :
𝓝[t] a = 𝓝[u] a :=
by rw [nhds_within_restrict t h₀ h₁, nhds_within_restrict u h₀ h₁, h₂]
theorem nhds_within_eq_of_open {a : α} {s : set α} (h₀ : a ∈ s) (h₁ : is_open s) :
𝓝[s] a = 𝓝 a :=
inf_eq_left.2 $ le_principal_iff.2 $ mem_nhds_sets h₁ h₀
@[simp] theorem nhds_within_empty (a : α) : 𝓝[∅] a = ⊥ :=
by rw [nhds_within, principal_empty, inf_bot_eq]
theorem nhds_within_union (a : α) (s t : set α) :
𝓝[s ∪ t] a = 𝓝[s] a ⊔ 𝓝[t] a :=
by { delta nhds_within, rw [←inf_sup_left, sup_principal] }
theorem nhds_within_inter (a : α) (s t : set α) :
𝓝[s ∩ t] a = 𝓝[s] a ⊓ 𝓝[t] a :=
by { delta nhds_within, rw [inf_left_comm, inf_assoc, inf_principal, ←inf_assoc, inf_idem] }
theorem nhds_within_inter' (a : α) (s t : set α) :
𝓝[s ∩ t] a = (𝓝[s] a) ⊓ 𝓟 t :=
by { delta nhds_within, rw [←inf_principal, inf_assoc] }
lemma mem_nhds_within_insert {a : α} {s t : set α} (h : t ∈ 𝓝[s] a) :
insert a t ∈ 𝓝[insert a s] a :=
begin
rcases mem_nhds_within.1 h with ⟨o, o_open, ao, ho⟩,
apply mem_nhds_within.2 ⟨o, o_open, ao, _⟩,
assume y,
simp only [and_imp, mem_inter_eq, mem_insert_iff],
rintro yo (rfl | ys),
{ simp },
{ simp [ho ⟨yo, ys⟩] }
end
lemma nhds_within_prod_eq {α : Type*} [topological_space α] {β : Type*} [topological_space β]
(a : α) (b : β) (s : set α) (t : set β) :
𝓝[s.prod t] (a, b) = 𝓝[s] a ×ᶠ 𝓝[t] b :=
by { delta nhds_within, rw [nhds_prod_eq, ←filter.prod_inf_prod, filter.prod_principal_principal] }
lemma nhds_within_prod {α : Type*} [topological_space α] {β : Type*} [topological_space β]
{s u : set α} {t v : set β} {a : α} {b : β}
(hu : u ∈ 𝓝[s] a) (hv : v ∈ 𝓝[t] b) :
(u.prod v) ∈ 𝓝[s.prod t] (a, b) :=
by { rw nhds_within_prod_eq, exact prod_mem_prod hu hv, }
theorem tendsto_if_nhds_within {f g : α → β} {p : α → Prop} [decidable_pred p]
{a : α} {s : set α} {l : filter β}
(h₀ : tendsto f (𝓝[s ∩ p] a) l)
(h₁ : tendsto g (𝓝[s ∩ {x | ¬ p x}] a) l) :
tendsto (λ x, if p x then f x else g x) (𝓝[s] a) l :=
by apply tendsto_if; rw [←nhds_within_inter']; assumption
lemma map_nhds_within (f : α → β) (a : α) (s : set α) :
map f (𝓝[s] a) =
⨅ t ∈ {t : set α | a ∈ t ∧ is_open t}, 𝓟 (set.image f (t ∩ s)) :=
((nhds_within_basis_open a s).map f).eq_binfi
theorem tendsto_nhds_within_mono_left {f : α → β} {a : α}
{s t : set α} {l : filter β} (hst : s ⊆ t) (h : tendsto f (𝓝[t] a) l) :
tendsto f (𝓝[s] a) l :=
h.mono_left $ nhds_within_mono a hst
theorem tendsto_nhds_within_mono_right {f : β → α} {l : filter β}
{a : α} {s t : set α} (hst : s ⊆ t) (h : tendsto f l (𝓝[s] a)) :
tendsto f l (𝓝[t] a) :=
h.mono_right (nhds_within_mono a hst)
theorem tendsto_nhds_within_of_tendsto_nhds {f : α → β} {a : α}
{s : set α} {l : filter β} (h : tendsto f (𝓝 a) l) :
tendsto f (𝓝[s] a) l :=
h.mono_left inf_le_left
theorem principal_subtype {α : Type*} (s : set α) (t : set {x // x ∈ s}) :
𝓟 t = comap coe (𝓟 ((coe : s → α) '' t)) :=
by rw [comap_principal, set.preimage_image_eq _ subtype.coe_injective]
lemma mem_closure_iff_nhds_within_ne_bot {s : set α} {x : α} :
x ∈ closure s ↔ ne_bot (𝓝[s] x) :=
mem_closure_iff_cluster_pt
lemma nhds_within_ne_bot_of_mem {s : set α} {x : α} (hx : x ∈ s) :
ne_bot (𝓝[s] x) :=
mem_closure_iff_nhds_within_ne_bot.1 $ subset_closure hx
lemma is_closed.mem_of_nhds_within_ne_bot {s : set α} (hs : is_closed s)
{x : α} (hx : ne_bot $ 𝓝[s] x) : x ∈ s :=
by simpa only [hs.closure_eq] using mem_closure_iff_nhds_within_ne_bot.2 hx
lemma eventually_eq_nhds_within_iff {f g : α → β} {s : set α} {a : α} :
(f =ᶠ[𝓝[s] a] g) ↔ ∀ᶠ x in 𝓝 a, x ∈ s → f x = g x :=
mem_inf_principal
lemma eventually_eq_nhds_within_of_eq_on {f g : α → β} {s : set α} {a : α} (h : eq_on f g s) :
f =ᶠ[𝓝[s] a] g :=
mem_inf_sets_of_right h
lemma set.eq_on.eventually_eq_nhds_within {f g : α → β} {s : set α} {a : α} (h : eq_on f g s) :
f =ᶠ[𝓝[s] a] g :=
eventually_eq_nhds_within_of_eq_on h
lemma tendsto_nhds_within_congr {f g : α → β} {s : set α} {a : α} {l : filter β}
(hfg : ∀ x ∈ s, f x = g x) (hf : tendsto f (𝓝[s] a) l) : tendsto g (𝓝[s] a) l :=
(tendsto_congr' $ eventually_eq_nhds_within_of_eq_on hfg).1 hf
lemma eventually_nhds_with_of_forall {s : set α} {a : α} {p : α → Prop} (h : ∀ x ∈ s, p x) :
∀ᶠ x in 𝓝[s] a, p x :=
mem_inf_sets_of_right h
lemma tendsto_nhds_within_of_tendsto_nhds_of_eventually_within {β : Type*} {a : α} {l : filter β}
{s : set α} (f : β → α) (h1 : tendsto f l (nhds a)) (h2 : ∀ᶠ x in l, f x ∈ s) :
tendsto f l (𝓝[s] a) :=
tendsto_inf.2 ⟨h1, tendsto_principal.2 h2⟩
lemma filter.eventually_eq.eq_of_nhds_within {s : set α} {f g : α → β} {a : α}
(h : f =ᶠ[𝓝[s] a] g) (hmem : a ∈ s) : f a = g a :=
h.self_of_nhds_within hmem
lemma eventually_nhds_within_of_eventually_nhds {α : Type*} [topological_space α]
{s : set α} {a : α} {p : α → Prop} (h : ∀ᶠ x in 𝓝 a, p x) :
∀ᶠ x in 𝓝[s] a, p x :=
mem_nhds_within_of_mem_nhds h
/-
nhds_within and subtypes
-/
theorem mem_nhds_within_subtype {s : set α} {a : {x // x ∈ s}} {t u : set {x // x ∈ s}} :
t ∈ 𝓝[u] a ↔ t ∈ comap (coe : s → α) (𝓝[coe '' u] a) :=
by rw [nhds_within, nhds_subtype, principal_subtype, ←comap_inf, ←nhds_within]
theorem nhds_within_subtype (s : set α) (a : {x // x ∈ s}) (t : set {x // x ∈ s}) :
𝓝[t] a = comap (coe : s → α) (𝓝[coe '' t] a) :=
filter.ext $ λ u, mem_nhds_within_subtype
theorem nhds_within_eq_map_subtype_coe {s : set α} {a : α} (h : a ∈ s) :
𝓝[s] a = map (coe : s → α) (𝓝 ⟨a, h⟩) :=
have h₀ : s ∈ 𝓝[s] a,
by { rw [mem_nhds_within], existsi set.univ, simp [set.diff_eq] },
have h₁ : ∀ y ∈ s, ∃ x : s, ↑x = y,
from λ y h, ⟨⟨y, h⟩, rfl⟩,
begin
conv_rhs { rw [← nhds_within_univ, nhds_within_subtype, subtype.coe_image_univ] },
exact (map_comap_of_surjective' h₀ h₁).symm,
end
theorem tendsto_nhds_within_iff_subtype {s : set α} {a : α} (h : a ∈ s) (f : α → β) (l : filter β) :
tendsto f (𝓝[s] a) l ↔ tendsto (s.restrict f) (𝓝 ⟨a, h⟩) l :=
by simp only [tendsto, nhds_within_eq_map_subtype_coe h, filter.map_map, restrict]
variables [topological_space β] [topological_space γ] [topological_space δ]
/-- A function between topological spaces is continuous at a point `x₀` within a subset `s`
if `f x` tends to `f x₀` when `x` tends to `x₀` while staying within `s`. -/
def continuous_within_at (f : α → β) (s : set α) (x : α) : Prop :=
tendsto f (𝓝[s] x) (𝓝 (f x))
/-- If a function is continuous within `s` at `x`, then it tends to `f x` within `s` by definition.
We register this fact for use with the dot notation, especially to use `tendsto.comp` as
`continuous_within_at.comp` will have a different meaning. -/
lemma continuous_within_at.tendsto {f : α → β} {s : set α} {x : α} (h : continuous_within_at f s x) :
tendsto f (𝓝[s] x) (𝓝 (f x)) := h
/-- A function between topological spaces is continuous on a subset `s`
when it's continuous at every point of `s` within `s`. -/
def continuous_on (f : α → β) (s : set α) : Prop := ∀ x ∈ s, continuous_within_at f s x
lemma continuous_on.continuous_within_at {f : α → β} {s : set α} {x : α} (hf : continuous_on f s)
(hx : x ∈ s) : continuous_within_at f s x :=
hf x hx
theorem continuous_within_at_univ (f : α → β) (x : α) :
continuous_within_at f set.univ x ↔ continuous_at f x :=
by rw [continuous_at, continuous_within_at, nhds_within_univ]
theorem continuous_within_at_iff_continuous_at_restrict (f : α → β) {x : α} {s : set α} (h : x ∈ s) :
continuous_within_at f s x ↔ continuous_at (s.restrict f) ⟨x, h⟩ :=
tendsto_nhds_within_iff_subtype h f _
theorem continuous_within_at.tendsto_nhds_within_image {f : α → β} {x : α} {s : set α}
(h : continuous_within_at f s x) :
tendsto f (𝓝[s] x) (𝓝[f '' s] (f x)) :=
tendsto_inf.2 ⟨h, tendsto_principal.2 $
mem_inf_sets_of_right $ mem_principal_sets.2 $
λ x, mem_image_of_mem _⟩
lemma continuous_within_at.prod_map {f : α → γ} {g : β → δ} {s : set α} {t : set β}
{x : α} {y : β}
(hf : continuous_within_at f s x) (hg : continuous_within_at g t y) :
continuous_within_at (prod.map f g) (s.prod t) (x, y) :=
begin
unfold continuous_within_at at *,
rw [nhds_within_prod_eq, prod.map, nhds_prod_eq],
exact hf.prod_map hg,
end
theorem continuous_on_iff {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ x ∈ s, ∀ t : set β, is_open t → f x ∈ t → ∃ u, is_open u ∧ x ∈ u ∧
u ∩ s ⊆ f ⁻¹' t :=
by simp only [continuous_on, continuous_within_at, tendsto_nhds, mem_nhds_within]
theorem continuous_on_iff_continuous_restrict {f : α → β} {s : set α} :
continuous_on f s ↔ continuous (s.restrict f) :=
begin
rw [continuous_on, continuous_iff_continuous_at], split,
{ rintros h ⟨x, xs⟩,
exact (continuous_within_at_iff_continuous_at_restrict f xs).mp (h x xs) },
intros h x xs,
exact (continuous_within_at_iff_continuous_at_restrict f xs).mpr (h ⟨x, xs⟩)
end
theorem continuous_on_iff' {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ t : set β, is_open t → ∃ u, is_open u ∧ f ⁻¹' t ∩ s = u ∩ s :=
have ∀ t, is_open (s.restrict f ⁻¹' t) ↔ ∃ (u : set α), is_open u ∧ f ⁻¹' t ∩ s = u ∩ s,
begin
intro t,
rw [is_open_induced_iff, set.restrict_eq, set.preimage_comp],
simp only [subtype.preimage_coe_eq_preimage_coe_iff],
split; { rintros ⟨u, ou, useq⟩, exact ⟨u, ou, useq.symm⟩ }
end,
by rw [continuous_on_iff_continuous_restrict, continuous]; simp only [this]
theorem continuous_on_iff_is_closed {f : α → β} {s : set α} :
continuous_on f s ↔ ∀ t : set β, is_closed t → ∃ u, is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s :=
have ∀ t, is_closed (s.restrict f ⁻¹' t) ↔ ∃ (u : set α), is_closed u ∧ f ⁻¹' t ∩ s = u ∩ s,
begin
intro t,
rw [is_closed_induced_iff, set.restrict_eq, set.preimage_comp],
simp only [subtype.preimage_coe_eq_preimage_coe_iff]
end,
by rw [continuous_on_iff_continuous_restrict, continuous_iff_is_closed]; simp only [this]
lemma continuous_on.prod_map {f : α → γ} {g : β → δ} {s : set α} {t : set β}
(hf : continuous_on f s) (hg : continuous_on g t) :
continuous_on (prod.map f g) (s.prod t) :=
λ ⟨x, y⟩ ⟨hx, hy⟩, continuous_within_at.prod_map (hf x hx) (hg y hy)
lemma continuous_on_empty (f : α → β) : continuous_on f ∅ :=
λ x, false.elim
theorem nhds_within_le_comap {x : α} {s : set α} {f : α → β} (ctsf : continuous_within_at f s x) :
𝓝[s] x ≤ comap f (𝓝[f '' s] (f x)) :=
map_le_iff_le_comap.1 ctsf.tendsto_nhds_within_image
theorem continuous_within_at_iff_ptendsto_res (f : α → β) {x : α} {s : set α} :
continuous_within_at f s x ↔ ptendsto (pfun.res f s) (𝓝 x) (𝓝 (f x)) :=
tendsto_iff_ptendsto _ _ _ _
lemma continuous_iff_continuous_on_univ {f : α → β} : continuous f ↔ continuous_on f univ :=
by simp [continuous_iff_continuous_at, continuous_on, continuous_at, continuous_within_at,
nhds_within_univ]
lemma continuous_within_at.mono {f : α → β} {s t : set α} {x : α} (h : continuous_within_at f t x)
(hs : s ⊆ t) : continuous_within_at f s x :=
h.mono_left (nhds_within_mono x hs)
lemma continuous_within_at_inter' {f : α → β} {s t : set α} {x : α} (h : t ∈ 𝓝[s] x) :
continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x :=
by simp [continuous_within_at, nhds_within_restrict'' s h]
lemma continuous_within_at_inter {f : α → β} {s t : set α} {x : α} (h : t ∈ 𝓝 x) :
continuous_within_at f (s ∩ t) x ↔ continuous_within_at f s x :=
by simp [continuous_within_at, nhds_within_restrict' s h]
lemma continuous_within_at.union {f : α → β} {s t : set α} {x : α}
(hs : continuous_within_at f s x) (ht : continuous_within_at f t x) :
continuous_within_at f (s ∪ t) x :=
by simp only [continuous_within_at, nhds_within_union, tendsto, map_sup, sup_le_iff.2 ⟨hs, ht⟩]
lemma continuous_within_at.mem_closure_image {f : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (hx : x ∈ closure s) : f x ∈ closure (f '' s) :=
by haveI := (mem_closure_iff_nhds_within_ne_bot.1 hx);
exact (mem_closure_of_tendsto h $
mem_sets_of_superset self_mem_nhds_within (subset_preimage_image f s))
lemma continuous_within_at.mem_closure {f : α → β} {s : set α} {x : α} {A : set β}
(h : continuous_within_at f s x) (hx : x ∈ closure s) (hA : s ⊆ f⁻¹' A) : f x ∈ closure A :=
closure_mono (image_subset_iff.2 hA) (h.mem_closure_image hx)
lemma continuous_within_at.image_closure {f : α → β} {s : set α}
(hf : ∀ x ∈ closure s, continuous_within_at f s x) :
f '' (closure s) ⊆ closure (f '' s) :=
begin
rintros _ ⟨x, hx, rfl⟩,
exact (hf x hx).mem_closure_image hx
end
theorem is_open_map.continuous_on_image_of_left_inv_on {f : α → β} {s : set α}
(h : is_open_map (s.restrict f)) {finv : β → α} (hleft : left_inv_on finv f s) :
continuous_on finv (f '' s) :=
begin
rintros _ ⟨x, xs, rfl⟩ t ht,
rw [hleft xs] at ht,
replace h := h.nhds_le ⟨x, xs⟩,
apply mem_nhds_within_of_mem_nhds,
apply h,
erw [map_compose.symm, function.comp, mem_map, ← nhds_within_eq_map_subtype_coe],
apply mem_sets_of_superset (inter_mem_nhds_within _ ht),
assume y hy,
rw [mem_set_of_eq, mem_preimage, hleft hy.1],
exact hy.2
end
theorem is_open_map.continuous_on_range_of_left_inverse {f : α → β} (hf : is_open_map f)
{finv : β → α} (hleft : function.left_inverse finv f) :
continuous_on finv (range f) :=
begin
rw [← image_univ],
exact (hf.restrict is_open_univ).continuous_on_image_of_left_inv_on (λ x _, hleft x)
end
lemma continuous_on.congr_mono {f g : α → β} {s s₁ : set α} (h : continuous_on f s)
(h' : eq_on g f s₁) (h₁ : s₁ ⊆ s) : continuous_on g s₁ :=
begin
assume x hx,
unfold continuous_within_at,
have A := (h x (h₁ hx)).mono h₁,
unfold continuous_within_at at A,
rw ← h' hx at A,
exact A.congr' h'.eventually_eq_nhds_within.symm
end
lemma continuous_on.congr {f g : α → β} {s : set α} (h : continuous_on f s) (h' : eq_on g f s) :
continuous_on g s :=
h.congr_mono h' (subset.refl _)
lemma continuous_on_congr {f g : α → β} {s : set α} (h' : eq_on g f s) :
continuous_on g s ↔ continuous_on f s :=
⟨λ h, continuous_on.congr h h'.symm, λ h, h.congr h'⟩
lemma continuous_at.continuous_within_at {f : α → β} {s : set α} {x : α} (h : continuous_at f x) :
continuous_within_at f s x :=
continuous_within_at.mono ((continuous_within_at_univ f x).2 h) (subset_univ _)
lemma continuous_within_at.continuous_at {f : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (hs : s ∈ 𝓝 x) : continuous_at f x :=
begin
have : s = univ ∩ s, by rw univ_inter,
rwa [this, continuous_within_at_inter hs, continuous_within_at_univ] at h
end
lemma continuous_on.continuous_at {f : α → β} {s : set α} {x : α}
(h : continuous_on f s) (hx : s ∈ 𝓝 x) : continuous_at f x :=
(h x (mem_of_nhds hx)).continuous_at hx
lemma continuous_within_at.comp {g : β → γ} {f : α → β} {s : set α} {t : set β} {x : α}
(hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) (h : s ⊆ f ⁻¹' t) :
continuous_within_at (g ∘ f) s x :=
begin
have : tendsto f (𝓟 s) (𝓟 t),
by { rw tendsto_principal_principal, exact λx hx, h hx },
have : tendsto f (𝓝[s] x) (𝓟 t) :=
this.mono_left inf_le_right,
have : tendsto f (𝓝[s] x) (𝓝[t] (f x)) :=
tendsto_inf.2 ⟨hf, this⟩,
exact tendsto.comp hg this
end
lemma continuous_within_at.comp' {g : β → γ} {f : α → β} {s : set α} {t : set β} {x : α}
(hg : continuous_within_at g t (f x)) (hf : continuous_within_at f s x) :
continuous_within_at (g ∘ f) (s ∩ f⁻¹' t) x :=
hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _)
lemma continuous_on.comp {g : β → γ} {f : α → β} {s : set α} {t : set β}
(hg : continuous_on g t) (hf : continuous_on f s) (h : s ⊆ f ⁻¹' t) :
continuous_on (g ∘ f) s :=
λx hx, continuous_within_at.comp (hg _ (h hx)) (hf x hx) h
lemma continuous_on.mono {f : α → β} {s t : set α} (hf : continuous_on f s) (h : t ⊆ s) :
continuous_on f t :=
λx hx, (hf x (h hx)).mono_left (nhds_within_mono _ h)
lemma continuous_on.comp' {g : β → γ} {f : α → β} {s : set α} {t : set β}
(hg : continuous_on g t) (hf : continuous_on f s) :
continuous_on (g ∘ f) (s ∩ f⁻¹' t) :=
hg.comp (hf.mono (inter_subset_left _ _)) (inter_subset_right _ _)
lemma continuous.continuous_on {f : α → β} {s : set α} (h : continuous f) :
continuous_on f s :=
begin
rw continuous_iff_continuous_on_univ at h,
exact h.mono (subset_univ _)
end
lemma continuous.continuous_within_at {f : α → β} {s : set α} {x : α} (h : continuous f) :
continuous_within_at f s x :=
h.continuous_at.continuous_within_at
lemma continuous.comp_continuous_on {g : β → γ} {f : α → β} {s : set α}
(hg : continuous g) (hf : continuous_on f s) :
continuous_on (g ∘ f) s :=
hg.continuous_on.comp hf subset_preimage_univ
lemma continuous_on.comp_continuous {g : β → γ} {f : α → β} {s : set β}
(hg : continuous_on g s) (hf : continuous f) (hfg : range f ⊆ s) : continuous (g ∘ f) :=
begin
rw continuous_iff_continuous_on_univ at *,
apply hg.comp hf,
rwa [← image_subset_iff, image_univ]
end
lemma continuous_within_at.preimage_mem_nhds_within {f : α → β} {x : α} {s : set α} {t : set β}
(h : continuous_within_at f s x) (ht : t ∈ 𝓝 (f x)) : f ⁻¹' t ∈ 𝓝[s] x :=
h ht
lemma continuous_within_at.preimage_mem_nhds_within' {f : α → β} {x : α} {s : set α} {t : set β}
(h : continuous_within_at f s x) (ht : t ∈ 𝓝[f '' s] (f x)) :
f ⁻¹' t ∈ 𝓝[s] x :=
begin
rw mem_nhds_within at ht,
rcases ht with ⟨u, u_open, fxu, hu⟩,
have : f ⁻¹' u ∩ s ∈ 𝓝[s] x :=
filter.inter_mem_sets (h (mem_nhds_sets u_open fxu)) self_mem_nhds_within,
apply mem_sets_of_superset this,
calc f ⁻¹' u ∩ s
⊆ f ⁻¹' u ∩ f ⁻¹' (f '' s) : inter_subset_inter_right _ (subset_preimage_image f s)
... = f ⁻¹' (u ∩ f '' s) : rfl
... ⊆ f ⁻¹' t : preimage_mono hu
end
lemma continuous_within_at.congr_of_eventually_eq {f f₁ : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) :
continuous_within_at f₁ s x :=
by rwa [continuous_within_at, filter.tendsto, hx, filter.map_congr h₁]
lemma continuous_within_at.congr {f f₁ : α → β} {s : set α} {x : α}
(h : continuous_within_at f s x) (h₁ : ∀y∈s, f₁ y = f y) (hx : f₁ x = f x) :
continuous_within_at f₁ s x :=
h.congr_of_eventually_eq (mem_sets_of_superset self_mem_nhds_within h₁) hx
lemma continuous_within_at.congr_mono {f g : α → β} {s s₁ : set α} {x : α}
(h : continuous_within_at f s x) (h' : eq_on g f s₁) (h₁ : s₁ ⊆ s) (hx : g x = f x):
continuous_within_at g s₁ x :=
(h.mono h₁).congr h' hx
lemma continuous_on_const {s : set α} {c : β} : continuous_on (λx, c) s :=
continuous_const.continuous_on
lemma continuous_within_at_const {b : β} {s : set α} {x : α} :
continuous_within_at (λ _:α, b) s x :=
continuous_const.continuous_within_at
lemma continuous_on_id {s : set α} : continuous_on id s :=
continuous_id.continuous_on
lemma continuous_within_at_id {s : set α} {x : α} : continuous_within_at id s x :=
continuous_id.continuous_within_at
lemma continuous_on_open_iff {f : α → β} {s : set α} (hs : is_open s) :
continuous_on f s ↔ (∀t, is_open t → is_open (s ∩ f⁻¹' t)) :=
begin
rw continuous_on_iff',
split,
{ assume h t ht,
rcases h t ht with ⟨u, u_open, hu⟩,
rw [inter_comm, hu],
apply is_open_inter u_open hs },
{ assume h t ht,
refine ⟨s ∩ f ⁻¹' t, h t ht, _⟩,
rw [@inter_comm _ s (f ⁻¹' t), inter_assoc, inter_self] }
end
lemma continuous_on.preimage_open_of_open {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_open s) (ht : is_open t) : is_open (s ∩ f⁻¹' t) :=
(continuous_on_open_iff hs).1 hf t ht
lemma continuous_on.preimage_closed_of_closed {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_closed s) (ht : is_closed t) : is_closed (s ∩ f⁻¹' t) :=
begin
rcases continuous_on_iff_is_closed.1 hf t ht with ⟨u, hu⟩,
rw [inter_comm, hu.2],
apply is_closed_inter hu.1 hs
end
lemma continuous_on.preimage_interior_subset_interior_preimage {f : α → β} {s : set α} {t : set β}
(hf : continuous_on f s) (hs : is_open s) : s ∩ f⁻¹' (interior t) ⊆ s ∩ interior (f⁻¹' t) :=
calc s ∩ f ⁻¹' (interior t) ⊆ interior (s ∩ f ⁻¹' t) :
interior_maximal (inter_subset_inter (subset.refl _) (preimage_mono interior_subset))
(hf.preimage_open_of_open hs is_open_interior)
... = s ∩ interior (f ⁻¹' t) : by rw [interior_inter, hs.interior_eq]
lemma continuous_on_of_locally_continuous_on {f : α → β} {s : set α}
(h : ∀x∈s, ∃t, is_open t ∧ x ∈ t ∧ continuous_on f (s ∩ t)) : continuous_on f s :=
begin
assume x xs,
rcases h x xs with ⟨t, open_t, xt, ct⟩,
have := ct x ⟨xs, xt⟩,
rwa [continuous_within_at, ← nhds_within_restrict _ xt open_t] at this
end
lemma continuous_on_open_of_generate_from {β : Type*} {s : set α} {T : set (set β)} {f : α → β}
(hs : is_open s) (h : ∀t ∈ T, is_open (s ∩ f⁻¹' t)) :
@continuous_on α β _ (topological_space.generate_from T) f s :=
begin
rw continuous_on_open_iff,
assume t ht,
induction ht with u hu u v Tu Tv hu hv U hU hU',
{ exact h u hu },
{ simp only [preimage_univ, inter_univ], exact hs },
{ have : s ∩ f ⁻¹' (u ∩ v) = (s ∩ f ⁻¹' u) ∩ (s ∩ f ⁻¹' v),
by { ext x, simp, split, finish, finish },
rw this,
exact is_open_inter hu hv },
{ rw [preimage_sUnion, inter_bUnion],
exact is_open_bUnion hU' },
{ exact hs }
end
lemma continuous_within_at.prod {f : α → β} {g : α → γ} {s : set α} {x : α}
(hf : continuous_within_at f s x) (hg : continuous_within_at g s x) :
continuous_within_at (λx, (f x, g x)) s x :=
hf.prod_mk_nhds hg
lemma continuous_on.prod {f : α → β} {g : α → γ} {s : set α}
(hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, (f x, g x)) s :=
λx hx, continuous_within_at.prod (hf x hx) (hg x hx)
lemma inducing.continuous_on_iff {f : α → β} {g : β → γ} (hg : inducing g) {s : set α} :
continuous_on f s ↔ continuous_on (g ∘ f) s :=
begin
simp only [continuous_on_iff_continuous_restrict, restrict_eq],
conv_rhs { rw [function.comp.assoc, ← (inducing.continuous_iff hg)] },
end
lemma embedding.continuous_on_iff {f : α → β} {g : β → γ} (hg : embedding g) {s : set α} :
continuous_on f s ↔ continuous_on (g ∘ f) s :=
inducing.continuous_on_iff hg.1
lemma continuous_within_at_of_not_mem_closure {f : α → β} {s : set α} {x : α} :
x ∉ closure s → continuous_within_at f s x :=
begin
intros hx,
rw [mem_closure_iff_nhds_within_ne_bot, ne_bot, not_not] at hx,
rw [continuous_within_at, hx],
exact tendsto_bot,
end
lemma continuous_on_if' {s : set α} {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)}
(hpf : ∀ a ∈ s ∩ frontier {a | p a},
tendsto f (nhds_within a $ s ∩ {a | p a}) (𝓝 $ if p a then f a else g a))
(hpg : ∀ a ∈ s ∩ frontier {a | p a},
tendsto g (nhds_within a $ s ∩ {a | ¬p a}) (𝓝 $ if p a then f a else g a))
(hf : continuous_on f $ s ∩ {a | p a}) (hg : continuous_on g $ s ∩ {a | ¬p a}) :
continuous_on (λ a, if p a then f a else g a) s :=
begin
set A := {a | p a},
set B := {a | ¬p a},
rw [← (inter_univ s), ← union_compl_self A],
intros x hx,
by_cases hx' : x ∈ frontier A,
{ have hx'' : x ∈ s ∩ frontier A, from ⟨hx.1, hx'⟩,
rw inter_union_distrib_left,
apply continuous_within_at.union,
{ apply tendsto_nhds_within_congr,
{ exact λ y ⟨hys, hyA⟩, (piecewise_eq_of_mem _ _ _ hyA).symm },
{ apply_assumption,
exact hx'' } },
{ apply tendsto_nhds_within_congr,
{ exact λ y ⟨hys, hyA⟩, (piecewise_eq_of_not_mem _ _ _ hyA).symm },
{ apply_assumption,
exact hx'' } } },
{ rw inter_union_distrib_left at ⊢ hx,
cases hx,
{ apply continuous_within_at.union,
{ exact (hf x hx).congr
(λ y hy, piecewise_eq_of_mem _ _ _ hy.2)
(piecewise_eq_of_mem _ _ _ hx.2), },
{ rw ← frontier_compl at hx',
have : x ∉ closure Aᶜ,
from λ h, hx' ⟨h, (λ (h' : x ∈ interior Aᶜ), interior_subset h' hx.2)⟩,
exact continuous_within_at_of_not_mem_closure
(λ h, this (closure_inter_subset_inter_closure _ _ h).2) } },
{ apply continuous_within_at.union,
{ have : x ∉ closure A,
from (λ h, hx' ⟨h, (λ (h' : x ∈ interior A), hx.2 (interior_subset h'))⟩),
exact continuous_within_at_of_not_mem_closure
(λ h, this (closure_inter_subset_inter_closure _ _ h).2) },
{ exact (hg x hx).congr
(λ y hy, piecewise_eq_of_not_mem _ _ _ hy.2)
(piecewise_eq_of_not_mem _ _ _ hx.2) } } }
end
lemma continuous_on_if {α β : Type*} [topological_space α] [topological_space β] {p : α → Prop}
{h : ∀a, decidable (p a)} {s : set α} {f g : α → β}
(hp : ∀ a ∈ s ∩ frontier {a | p a}, f a = g a) (hf : continuous_on f $ s ∩ closure {a | p a})
(hg : continuous_on g $ s ∩ closure {a | ¬ p a}) :
continuous_on (λa, if p a then f a else g a) s :=
begin
apply continuous_on_if',
{ rintros a ha,
simp only [← hp a ha, if_t_t],
apply tendsto_nhds_within_mono_left (inter_subset_inter_right s subset_closure),
exact (hf a ⟨ha.1, ha.2.1⟩).tendsto },
{ rintros a ha,
simp only [hp a ha, if_t_t],
apply tendsto_nhds_within_mono_left (inter_subset_inter_right s subset_closure),
rcases ha with ⟨has, ⟨_, ha⟩⟩,
rw [← mem_compl_iff, ← closure_compl] at ha,
apply (hg a ⟨has, ha⟩).tendsto, },
{ exact hf.mono (inter_subset_inter_right s subset_closure) },
{ exact hg.mono (inter_subset_inter_right s subset_closure) }
end
lemma continuous_if' {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)}
(hpf : ∀ a ∈ frontier {x | p x},
tendsto f (nhds_within a {x | p x}) (𝓝 $ ite (p a) (f a) (g a)))
(hpg : ∀ a ∈ frontier {x | p x},
tendsto g (nhds_within a {x | ¬p x}) (𝓝 $ ite (p a) (f a) (g a)))
(hf : continuous_on f {x | p x}) (hg : continuous_on g {x | ¬p x}) :
continuous (λ a, ite (p a) (f a) (g a)) :=
begin
rw continuous_iff_continuous_on_univ,
apply continuous_on_if'; simp; assumption
end
lemma continuous_on_fst {s : set (α × β)} : continuous_on prod.fst s :=
continuous_fst.continuous_on
lemma continuous_within_at_fst {s : set (α × β)} {p : α × β} :
continuous_within_at prod.fst s p :=
continuous_fst.continuous_within_at
lemma continuous_on_snd {s : set (α × β)} : continuous_on prod.snd s :=
continuous_snd.continuous_on
lemma continuous_within_at_snd {s : set (α × β)} {p : α × β} :
continuous_within_at prod.snd s p :=
continuous_snd.continuous_within_at
|
7f4f256428d783e01a6c27b72cc12a29105ccd83 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /tests/lean/run/toFromJson.lean | 58740d510e54f399622531ca9af12956f9a2632a | [
"Apache-2.0",
"LLVM-exception",
"NCSA",
"LGPL-3.0-only",
"LicenseRef-scancode-inner-net-2.0",
"BSD-3-Clause",
"LGPL-2.0-or-later",
"Spencer-94",
"LGPL-2.1-or-later",
"HPND",
"LicenseRef-scancode-pcre",
"ISC",
"LGPL-2.1-only",
"LicenseRef-scancode-other-permissive",
"SunPro",
"CMU-Mach"... | permissive | leanprover/lean4 | 4bdf9790294964627eb9be79f5e8f6157780b4cc | f1f9dc0f2f531af3312398999d8b8303fa5f096b | refs/heads/master | 1,693,360,665,786 | 1,693,350,868,000 | 1,693,350,868,000 | 129,571,436 | 2,827 | 311 | Apache-2.0 | 1,694,716,156,000 | 1,523,760,560,000 | Lean | UTF-8 | Lean | false | false | 3,637 | lean | import Lean
open Lean
declare_syntax_cat json
syntax str : json
syntax num : json
syntax "{" (ident ": " json),* "}" : json
syntax "[" json,* "]" : json
syntax "json " json : term
/- declare a micro json parser, so we can write our tests in a more legible way. -/
open Json in macro_rules
| `(json $s:str) => pure s
| `(json $n:num) => pure n
| `(json { $[$ns : $js],* }) => do
let mut fields := #[]
for n in ns, j in js do
fields := fields.push (← `(($(quote n.getId.getString!), json $j)))
`(mkObj [$fields,*])
| `(json [ $[$js],* ]) => do
let mut fields := #[]
for j in js do
fields := fields.push (← `(json $j))
`(Json.arr #[$fields,*])
def checkToJson [ToJson α] (obj : α) (rhs : Json) : MetaM Unit :=
let lhs := (obj |> toJson).pretty
if lhs == rhs.pretty then
pure ()
else
throwError "{lhs} ≟ {rhs}"
def checkRoundTrip [Repr α] [BEq α] [ToJson α] [FromJson α] (obj : α) : MetaM Unit := do
let roundTripped := obj |> toJson |> fromJson?
if let Except.ok roundTripped := roundTripped then
if obj == roundTripped then
pure ()
else
throwError "{repr obj} ≟ {repr roundTripped}"
else
throwError "couldn't parse: {repr obj} ≟ {obj |> toJson}"
-- set_option trace.Meta.debug true in
structure Foo where
x : Nat
y : String
deriving ToJson, FromJson, Repr, BEq
#eval checkToJson { x := 1, y := "bla" : Foo} (json { y : "bla", x : 1 })
#eval checkRoundTrip { x := 1, y := "bla" : Foo }
-- set_option trace.Elab.command true
structure WInfo where
a : Nat
b : Nat
deriving ToJson, FromJson, Repr, BEq
-- set_option trace.Elab.command true
inductive E
| W : WInfo → E
| WAlt (a b : Nat)
| X : Nat → Nat → E
| Y : Nat → E
| YAlt (a : Nat)
| Z
deriving ToJson, FromJson, Repr, BEq
#eval checkToJson (E.W { a := 2, b := 3}) (json { W : { a : 2, b : 3 } })
#eval checkRoundTrip (E.W { a := 2, b := 3 })
#eval checkToJson (E.WAlt 2 3) (json { WAlt : { a : 2, b : 3 } })
#eval checkRoundTrip (E.WAlt 2 3)
#eval checkToJson (E.X 2 3) (json { X : [2, 3] })
#eval checkRoundTrip (E.X 2 3)
#eval checkToJson (E.Y 4) (json { Y : 4 })
#eval checkRoundTrip (E.Y 4)
#eval checkToJson (E.YAlt 5) (json { YAlt : { a : 5 } })
#eval checkRoundTrip (E.YAlt 5)
#eval checkToJson E.Z (json "Z")
#eval checkRoundTrip E.Z
inductive ERec
| mk : Nat → ERec
| W : ERec → ERec
deriving ToJson, FromJson, Repr, BEq
#eval checkToJson (ERec.W (ERec.mk 6)) (json { W : { mk : 6 }})
#eval checkRoundTrip (ERec.mk 7)
#eval checkRoundTrip (ERec.W (ERec.mk 8))
inductive ENest
| mk : Nat → ENest
| W : (Array ENest) → ENest
deriving ToJson, FromJson, Repr, BEq
#eval checkToJson (ENest.W #[ENest.mk 9]) (json { W : [{ mk : 9 }]})
#eval checkRoundTrip (ENest.mk 10)
#eval checkRoundTrip (ENest.W #[ENest.mk 11])
inductive EParam (α : Type)
| mk : α → EParam α
deriving ToJson, FromJson, Repr, BEq
#eval checkToJson (EParam.mk 12) (json { mk : 12 })
#eval checkToJson (EParam.mk "abcd") (json { mk : "abcd" })
#eval checkRoundTrip (EParam.mk 13)
#eval checkRoundTrip (EParam.mk "efgh")
structure Minus where
«i-love-lisp» : Nat
deriving ToJson, FromJson, Repr, DecidableEq
#eval checkRoundTrip { «i-love-lisp» := 1 : Minus }
#eval checkToJson { «i-love-lisp» := 1 : Minus } (json { «i-love-lisp»: 1 })
structure MinusAsk where
«branches-ignore?» : Option (Array String)
deriving ToJson, FromJson, Repr, DecidableEq
#eval checkRoundTrip { «branches-ignore?» := #["master"] : MinusAsk }
#eval checkToJson { «branches-ignore?» := #["master"] : MinusAsk }
(json { «branches-ignore» : ["master"] })
|
c07733028814edab339223d52f97246025ec7c68 | cbcb0199842f03e7606d4e43666573fc15dd07a5 | /src/topology/constructions.lean | 9538ae59fca96d05a83e8fc82affa7523ec4cbf0 | [
"Apache-2.0"
] | permissive | truonghoangle/mathlib | a6a7c14b3767ec71156239d8ea97f6921fe79627 | 673bae584febcd830c2c9256eb7e7a81e27ed303 | refs/heads/master | 1,590,347,998,944 | 1,559,728,860,000 | 1,559,728,860,000 | 187,431,971 | 0 | 0 | null | 1,558,238,525,000 | 1,558,238,525,000 | null | UTF-8 | Lean | false | false | 48,965 | 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
Constructions of new topological spaces from old ones: product, sum, subtype, quotient, list, vector
-/
import topology.maps topology.subset_properties topology.separation topology.bases
noncomputable theory
open set filter lattice
local attribute [instance] classical.prop_decidable
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
section prod
open topological_space
variables [topological_space α] [topological_space β] [topological_space γ]
lemma continuous_fst : continuous (@prod.fst α β) :=
continuous_sup_dom_left continuous_induced_dom
lemma continuous_snd : continuous (@prod.snd α β) :=
continuous_sup_dom_right continuous_induced_dom
lemma continuous.prod_mk {f : γ → α} {g : γ → β}
(hf : continuous f) (hg : continuous g) : continuous (λx, prod.mk (f x) (g x)) :=
continuous_sup_rng (continuous_induced_rng hf) (continuous_induced_rng hg)
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 : β} : nhds (a, b) = filter.prod (nhds a) (nhds b) :=
by rw [filter.prod, prod.topological_space, nhds_sup, nhds_induced_eq_comap, nhds_induced_eq_comap]
instance [topological_space α] [discrete_topology α] [topological_space β] [discrete_topology β] :
discrete_topology (α × β) :=
⟨eq_of_nhds_eq_nhds $ assume ⟨a, b⟩,
by rw [nhds_prod_eq, nhds_discrete α, nhds_discrete β, nhds_top, filter.prod_pure_pure]⟩
lemma prod_mem_nhds_sets {s : set α} {t : set β} {a : α} {b : β}
(ha : s ∈ nhds a) (hb : t ∈ nhds b) : set.prod s t ∈ nhds (a, b) :=
by rw [nhds_prod_eq]; exact prod_mem_prod ha hb
lemma nhds_swap (a : α) (b : β) : nhds (a, b) = (nhds (b, a)).map prod.swap :=
by rw [nhds_prod_eq, filter.prod_comm, nhds_prod_eq]; refl
lemma tendsto_prod_mk_nhds {γ} {a : α} {b : β} {f : filter γ} {ma : γ → α} {mb : γ → β}
(ha : tendsto ma f (nhds a)) (hb : tendsto mb f (nhds b)) :
tendsto (λc, (ma c, mb c)) f (nhds (a, b)) :=
by rw [nhds_prod_eq]; exact filter.tendsto.prod_mk ha hb
lemma continuous_within_at.prod {f : α → β} {g : α → γ} {s : set α} {x : α}
(hf : continuous_within_at f s x) (hg : continuous_within_at g s x) :
continuous_within_at (λx, (f x, g x)) s x :=
tendsto_prod_mk_nhds hf hg
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 :=
tendsto_prod_mk_nhds hf hg
lemma continuous_on.prod {f : α → β} {g : α → γ} {s : set α}
(hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λx, (f x, g x)) s :=
λx hx, continuous_within_at.prod (hf x hx) (hg x hx)
lemma prod_generate_from_generate_from_eq {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
(sup_le
(induced_le_iff_le_coinduced.mpr $ generate_from_le $ 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⟩)
(induced_le_iff_le_coinduced.mpr $ generate_from_le $ 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⟩))
(generate_from_le $ 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))
lemma prod_eq_generate_from [tα : topological_space α] [tβ : topological_space β] :
prod.topological_space =
generate_from {g | ∃(s:set α) (t:set β), is_open s ∧ is_open t ∧ g = set.prod s t} :=
le_antisymm
(sup_le
(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⟩))
(generate_from_le $ assume g ⟨s, t, hs, ht, g_eq⟩, g_eq.symm ▸ is_open_prod hs 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
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 (nhds a) (nhds b) ⊓ principal (set.prod s t) =
filter.prod (nhds a ⊓ principal s) (nhds b ⊓ principal t),
by rw [←prod_inf_prod, prod_principal_principal],
by simp [closure_eq_nhds, nhds_prod_eq, this]; exact prod_neq_bot
lemma mem_closure2 [topological_space α] [topological_space β] [topological_space γ]
{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 [topological_space α] [topological_space β] {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]
protected lemma is_open_map.prod
[topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
{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 f).1 hf a) ((is_open_map_iff_nhds_le g).1 hg b)
end
section tube_lemma
def nhds_contain_boxes (s : set α) (t : set β) : Prop :=
∀ (n : set (α × β)) (hn : is_open n) (hp : set.prod s t ⊆ n),
∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n
lemma nhds_contain_boxes.symm {s : set α} {t : set β} :
nhds_contain_boxes s t → nhds_contain_boxes t s :=
assume H n hn hp,
let ⟨u, v, uo, vo, su, tv, p⟩ :=
H (prod.swap ⁻¹' n)
(continuous_swap n hn)
(by rwa [←image_subset_iff, prod.swap, image_swap_prod]) in
⟨v, u, vo, uo, tv, su,
by rwa [←image_subset_iff, prod.swap, image_swap_prod] at p⟩
lemma nhds_contain_boxes.comm {s : set α} {t : set β} :
nhds_contain_boxes s t ↔ nhds_contain_boxes t s :=
iff.intro nhds_contain_boxes.symm nhds_contain_boxes.symm
lemma nhds_contain_boxes_of_singleton {x : α} {y : β} :
nhds_contain_boxes ({x} : set α) ({y} : set β) :=
assume n hn hp,
let ⟨u, v, uo, vo, xu, yv, hp'⟩ :=
is_open_prod_iff.mp hn x y (hp $ by simp) in
⟨u, v, uo, vo, by simpa, by simpa, hp'⟩
lemma nhds_contain_boxes_of_compact {s : set α} (hs : compact s) (t : set β)
(H : ∀ x ∈ s, nhds_contain_boxes ({x} : set α) t) : nhds_contain_boxes s t :=
assume n hn hp,
have ∀x : subtype s, ∃uv : set α × set β,
is_open uv.1 ∧ is_open uv.2 ∧ {↑x} ⊆ uv.1 ∧ t ⊆ uv.2 ∧ set.prod uv.1 uv.2 ⊆ n,
from assume ⟨x, hx⟩,
have set.prod {x} t ⊆ n, from
subset.trans (prod_mono (by simpa) (subset.refl _)) hp,
let ⟨ux,vx,H1⟩ := H x hx n hn this in ⟨⟨ux,vx⟩,H1⟩,
let ⟨uvs, h⟩ := classical.axiom_of_choice this in
have us_cover : s ⊆ ⋃i, (uvs i).1, from
assume x hx, set.subset_Union _ ⟨x,hx⟩ (by simpa using (h ⟨x,hx⟩).2.2.1),
let ⟨s0, _, s0_fin, s0_cover⟩ :=
compact_elim_finite_subcover_image hs (λi _, (h i).1) $
by rw bUnion_univ; exact us_cover in
let u := ⋃(i ∈ s0), (uvs i).1 in
let v := ⋂(i ∈ s0), (uvs i).2 in
have is_open u, from is_open_bUnion (λi _, (h i).1),
have is_open v, from is_open_bInter s0_fin (λi _, (h i).2.1),
have t ⊆ v, from subset_bInter (λi _, (h i).2.2.2.1),
have set.prod u v ⊆ n, from assume ⟨x',y'⟩ ⟨hx',hy'⟩,
have ∃i ∈ s0, x' ∈ (uvs i).1, by simpa using hx',
let ⟨i,is0,hi⟩ := this in
(h i).2.2.2.2 ⟨hi, (bInter_subset_of_mem is0 : v ⊆ (uvs i).2) hy'⟩,
⟨u, v, ‹is_open u›, ‹is_open v›, s0_cover, ‹t ⊆ v›, ‹set.prod u v ⊆ n›⟩
lemma generalized_tube_lemma {s : set α} (hs : compact s) {t : set β} (ht : compact t)
{n : set (α × β)} (hn : is_open n) (hp : set.prod s t ⊆ n) :
∃ (u : set α) (v : set β), is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ set.prod u v ⊆ n :=
have _, from
nhds_contain_boxes_of_compact hs t $ assume x _, nhds_contain_boxes.symm $
nhds_contain_boxes_of_compact ht {x} $ assume y _, nhds_contain_boxes_of_singleton,
this n hn hp
end tube_lemma
lemma is_closed_diagonal [topological_space α] [t2_space α] : is_closed {p:α×α | p.1 = p.2} :=
is_closed_iff_nhds.mpr $ assume ⟨a₁, a₂⟩ h, eq_of_nhds_neq_bot $ assume : nhds a₁ ⊓ nhds a₂ = ⊥, h $
let ⟨t₁, ht₁, t₂, ht₂, (h' : t₁ ∩ t₂ ⊆ ∅)⟩ :=
by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this in
begin
change t₁ ∈ nhds a₁ at ht₁,
change t₂ ∈ nhds a₂ at ht₂,
rw [nhds_prod_eq, ←empty_in_sets_eq_bot],
apply filter.sets_of_superset,
apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)),
exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩,
show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩
end
lemma is_closed_eq [topological_space α] [t2_space α] [topological_space β] {f g : β → α}
(hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} :=
continuous_iff_is_closed.mp (hf.prod_mk hg) _ is_closed_diagonal
lemma diagonal_eq_range_diagonal_map : {p:α×α | p.1 = p.2} = range (λx, (x,x)) :=
ext $ assume p, iff.intro
(assume h, ⟨p.1, prod.ext_iff.2 ⟨rfl, h⟩⟩)
(assume ⟨x, hx⟩, show p.1 = p.2, by rw ←hx)
lemma prod_subset_compl_diagonal_iff_disjoint {s t : set α} :
set.prod s t ⊆ - {p:α×α | p.1 = p.2} ↔ s ∩ t = ∅ :=
by rw [eq_empty_iff_forall_not_mem, subset_compl_comm,
diagonal_eq_range_diagonal_map, range_subset_iff]; simp
lemma compact_compact_separated [t2_space α] {s t : set α}
(hs : compact s) (ht : compact t) (hst : s ∩ t = ∅) :
∃u v : set α, is_open u ∧ is_open v ∧ s ⊆ u ∧ t ⊆ v ∧ u ∩ v = ∅ :=
by simp only [prod_subset_compl_diagonal_iff_disjoint.symm] at ⊢ hst;
exact generalized_tube_lemma hs ht is_closed_diagonal hst
lemma closed_of_compact [t2_space α] (s : set α) (hs : compact s) : is_closed s :=
is_open_compl_iff.mpr $ is_open_iff_forall_mem_open.mpr $ assume x hx,
let ⟨u, v, uo, vo, su, xv, uv⟩ :=
compact_compact_separated hs (compact_singleton : compact {x})
(by rwa [inter_comm, ←subset_compl_iff_disjoint, singleton_subset_iff]) in
have v ⊆ -s, from
subset_compl_comm.mp (subset.trans su (subset_compl_iff_disjoint.mpr uv)),
⟨v, this, vo, by simpa using xv⟩
lemma locally_compact_of_compact_nhds [topological_space α] [t2_space α]
(h : ∀ x : α, ∃ s, s ∈ nhds x ∧ compact s) :
locally_compact_space α :=
⟨assume x n hn,
let ⟨u, un, uo, xu⟩ := mem_nhds_sets_iff.mp hn in
let ⟨k, kx, kc⟩ := h x in
-- K is compact but not necessarily contained in N.
-- K \ U is again compact and doesn't contain x, so
-- we may find open sets V, W separating x from K \ U.
-- Then K \ W is a compact neighborhood of x contained in U.
let ⟨v, w, vo, wo, xv, kuw, vw⟩ :=
compact_compact_separated compact_singleton (compact_diff kc uo)
(by rw [singleton_inter_eq_empty]; exact λ h, h.2 xu) in
have wn : -w ∈ nhds x, from
mem_nhds_sets_iff.mpr
⟨v, subset_compl_iff_disjoint.mpr vw, vo, singleton_subset_iff.mp xv⟩,
⟨k - w,
filter.inter_mem_sets kx wn,
subset.trans (diff_subset_comm.mp kuw) un,
compact_diff kc wo⟩⟩
instance locally_compact_of_compact [topological_space α] [t2_space α] [compact_space α] :
locally_compact_space α :=
locally_compact_of_compact_nhds (assume x, ⟨univ, mem_nhds_sets is_open_univ trivial, compact_univ⟩)
-- We can't make this an instance because it could cause an instance loop.
lemma normal_of_compact_t2 [topological_space α] [compact_space α] [t2_space α] : normal_space α :=
begin
refine ⟨assume s t hs ht st, _⟩,
simp only [disjoint_iff],
exact compact_compact_separated (compact_of_closed hs) (compact_of_closed ht) st.eq_bot
end
/- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/
instance [second_countable_topology α] [second_countable_topology β] :
second_countable_topology (α × β) :=
⟨let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := is_open_generated_countable_inter α in
let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := is_open_generated_countable_inter β in
⟨{g | ∃u∈a, ∃v∈b, g = set.prod u v},
have {g | ∃u∈a, ∃v∈b, g = set.prod u v} = (⋃u∈a, ⋃v∈b, {set.prod u v}),
by apply set.ext; simp,
by rw [this]; exact (countable_bUnion ha₁ $ assume u hu, countable_bUnion hb₁ $ by simp),
by rw [ha₅, hb₅, prod_generate_from_generate_from_eq ha₄ hb₄]⟩⟩
lemma compact_prod (s : set α) (t : set β) (ha : compact s) (hb : compact t) : compact (set.prod s t) :=
begin
rw compact_iff_ultrafilter_le_nhds at ha hb ⊢,
intros f hf hfs,
rw le_principal_iff at hfs,
rcases ha (map prod.fst f) (ultrafilter_map hf)
(le_principal_iff.2 (mem_map_sets_iff.2
⟨_, hfs, image_subset_iff.2 (λ s h, h.1)⟩)) with ⟨a, sa, ha⟩,
rcases hb (map prod.snd f) (ultrafilter_map hf)
(le_principal_iff.2 (mem_map_sets_iff.2
⟨_, hfs, image_subset_iff.2 (λ s h, h.2)⟩)) with ⟨b, tb, hb⟩,
rw map_le_iff_le_comap at ha hb,
refine ⟨⟨a, b⟩, ⟨sa, tb⟩, _⟩,
rw nhds_prod_eq, exact le_inf ha hb
end
instance [compact_space α] [compact_space β] : compact_space (α × β) :=
⟨begin
have A : compact (set.prod (univ : set α) (univ : set β)) :=
compact_prod univ univ compact_univ compact_univ,
have : set.prod (univ : set α) (univ : set β) = (univ : set (α × β)) := by simp,
rwa this at A,
end⟩
end prod
section sum
variables [topological_space α] [topological_space β] [topological_space γ]
lemma continuous_inl : continuous (@sum.inl α β) :=
continuous_inf_rng_left continuous_coinduced_rng
lemma continuous_inr : continuous (@sum.inr α β) :=
continuous_inf_rng_right continuous_coinduced_rng
lemma continuous_sum_rec {f : α → γ} {g : β → γ}
(hf : continuous f) (hg : continuous g) : @continuous (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) :=
continuous_inf_dom hf hg
lemma embedding_inl : embedding (@sum.inl α β) :=
⟨λ _ _, sum.inl.inj_iff.mp,
begin
unfold sum.topological_space,
apply le_antisymm,
{ 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⟩ },
{ rw induced_le_iff_le_coinduced, exact lattice.inf_le_left }
end⟩
lemma embedding_inr : embedding (@sum.inr α β) :=
⟨λ _ _, sum.inr.inj_iff.mp,
begin
unfold sum.topological_space,
apply le_antisymm,
{ 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⟩ },
{ rw induced_le_iff_le_coinduced, exact lattice.inf_le_right }
end⟩
instance [topological_space α] [topological_space β] [compact_space α] [compact_space β] :
compact_space (α ⊕ β) :=
⟨begin
have A : compact (@sum.inl α β '' univ) := compact_image compact_univ continuous_inl,
have B : compact (@sum.inr α β '' univ) := compact_image compact_univ continuous_inr,
have C := compact_union_of_compact A B,
have : (@sum.inl α β '' univ) ∪ (@sum.inr α β '' univ) = univ := by ext; cases x; simp,
rwa this at C,
end⟩
end sum
section subtype
variables [topological_space α] [topological_space β] [topological_space γ] {p : α → Prop}
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
lemma embedding_subtype_val : embedding (@subtype.val α p) :=
⟨subtype.val_injective, rfl⟩
lemma continuous_subtype_val : continuous (@subtype.val α p) :=
continuous_induced_dom
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 [topological_space α] {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} ∈ nhds a) :
map (@subtype.val α p) (nhds ⟨a, ha⟩) = nhds a :=
map_nhds_induced_eq (by simp [subtype.val_image, h])
lemma nhds_subtype_eq_comap {a : α} {h : p a} :
nhds (⟨a, h⟩ : subtype p) = comap subtype.val (nhds a) :=
nhds_induced_eq_comap
lemma tendsto_subtype_rng [topological_space α] {p : α → Prop} {b : filter β} {f : β → subtype p} :
∀{a:subtype p}, tendsto f b (nhds a) ↔ tendsto (λx, subtype.val (f x)) b (nhds 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} ∈ nhds 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} ∈ nhds x)⟩ := c_cover x in
let x' : subtype (c i) := ⟨x, mem_of_nhds c_sets⟩ in
calc map f (nhds x) = map f (map subtype.val (nhds x')) :
congr_arg (map f) (map_nhds_subtype_val_eq _ $ c_sets).symm
... = map (λx:subtype (c i), f x.val) (nhds x') : rfl
... ≤ nhds (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
lemma compact_iff_compact_image_of_embedding {s : set α} {f : α → β} (hf : embedding f) :
compact s ↔ compact (f '' s) :=
iff.intro (assume h, compact_image h hf.continuous) $ assume h, begin
rw compact_iff_ultrafilter_le_nhds at ⊢ h,
intros u hu us',
let u' : filter β := map f u,
have : u' ≤ principal (f '' s), begin
rw [map_le_iff_le_comap, comap_principal], convert us',
exact preimage_image_eq _ hf.1
end,
rcases h u' (ultrafilter_map hu) this with ⟨_, ⟨a, ha, ⟨⟩⟩, _⟩,
refine ⟨a, ha, _⟩,
rwa [hf.2, nhds_induced_eq_comap, ←map_le_iff_le_comap]
end
lemma compact_iff_compact_in_subtype {s : set {a // p a}} :
compact s ↔ compact (subtype.val '' s) :=
compact_iff_compact_image_of_embedding embedding_subtype_val
lemma compact_iff_compact_univ {s : set α} : compact s ↔ compact (univ : set (subtype s)) :=
by rw [compact_iff_compact_in_subtype, image_univ, subtype.val_range]; refl
lemma compact_iff_compact_space {s : set α} : compact s ↔ compact_space s :=
compact_iff_compact_univ.trans ⟨λ h, ⟨h⟩, @compact_space.compact_univ _ _⟩
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
instance quot.compact_space {r : α → α → Prop} [topological_space α] [compact_space α] :
compact_space (quot r) :=
⟨begin
have : quot.mk r '' univ = univ,
by rw [image_univ, range_iff_surjective]; exact quot.exists_rep,
rw ←this,
exact compact_image compact_univ continuous_quot_mk
end⟩
instance quotient.compact_space {s : setoid α} [topological_space α] [compact_space α] :
compact_space (quotient s) :=
quot.compact_space
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_supr_rng $ assume i, continuous_induced_rng $ h i
lemma continuous_apply [∀i, topological_space (π i)] (i : ι) :
continuous (λp:Πi, π i, p i) :=
continuous_supr_dom continuous_induced_dom
lemma nhds_pi [t : ∀i, topological_space (π i)] {a : Πi, π i} :
nhds a = (⨅i, comap (λx, x i) (nhds (a i))) :=
calc nhds a = (⨅i, @nhds _ (@topological_space.induced _ _ (λx:Πi, π i, x i) (t i)) a) : nhds_supr
... = (⨅i, comap (λx, x i) (nhds (a i))) : by simp [nhds_induced_eq_comap]
/-- Tychonoff's theorem -/
lemma compact_pi_infinite [∀i, topological_space (π i)] {s : Πi:ι, set (π i)} :
(∀i, compact (s i)) → compact {x : Πi:ι, π i | ∀i, x i ∈ s i} :=
begin
simp [compact_iff_ultrafilter_le_nhds, nhds_pi],
exact assume h f hf hfs,
let p : Πi:ι, filter (π i) := λi, map (λx:Πi:ι, π i, x i) f in
have ∀i:ι, ∃a, a∈s i ∧ p i ≤ nhds a,
from assume i, h i (p i) (ultrafilter_map hf) $
show (λx:Πi:ι, π i, x i) ⁻¹' s i ∈ f.sets,
from mem_sets_of_superset hfs $ assume x (hx : ∀i, x i ∈ s i), hx i,
let ⟨a, ha⟩ := classical.axiom_of_choice this in
⟨a, assume i, (ha i).left, assume i, map_le_iff_le_comap.mp $ (ha i).right⟩
end
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
(supr_le $ 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]⟩)
(generate_from_le $ assume g ⟨s, i, hi, eq⟩, eq.symm ▸ is_open_set_pi (finset.finite_to_set _) hi)
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_le _) (generate_from_mono _),
{ 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 generate_from_le _ _ (hi a ha),
exact assume s hs, generate_open.basic _ ⟨function.update (λa, univ) a s, {a}, by simp [hs]⟩ },
exact assume s ⟨t, i, ht, eq⟩, ⟨t, i, assume a ha, generate_open.basic _ (ht a ha), eq⟩
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_le _) (generate_from_mono _),
{ 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] } },
exact assume s ⟨t, ht, eq⟩, ⟨t, finset.univ, by simp [ht, eq]⟩
end
instance second_countable_topology_fintype
[fintype ι] [t : ∀a, topological_space (π a)] [sc : ∀a, second_countable_topology (π a)] :
second_countable_topology (∀a, π a) :=
have ∀i, ∃b : set (set (π i)), countable b ∧ ∅ ∉ b ∧ is_topological_basis b, from
assume a, @is_open_generated_countable_inter (π a) _ (sc a),
let ⟨g, hg⟩ := classical.axiom_of_choice this in
have t = (λa, generate_from (g a)), from funext $ assume a, (hg a).2.2.2.2,
begin
constructor,
refine ⟨pi univ '' pi univ g, countable_image _ _, _⟩,
{ suffices : countable {f : Πa, set (π a) | ∀a, f a ∈ g a}, { simpa [pi] },
exact countable_pi (assume i, (hg i).1), },
rw [this, pi_generate_from_eq_fintype],
{ congr' 1, ext f, simp [pi, eq_comm] },
exact assume a, (hg a).2.2.2.1
end
instance pi.compact [∀i:ι, topological_space (π i)] [∀i:ι, compact_space (π i)] : compact_space (Πi, π i) :=
⟨begin
have A : compact {x : Πi:ι, π i | ∀i, x i ∈ (univ : set (π i))} :=
compact_pi_infinite (λi, compact_univ),
have : {x : Πi:ι, π i | ∀i, x i ∈ (univ : set (π i))} = univ := by ext; simp,
rwa this at A,
end⟩
end pi
section sigma
variables {ι : Type*} {σ : ι → Type*} [Π i, topological_space (σ i)]
open lattice
lemma continuous_sigma_mk {i : ι} : continuous (@sigma.mk ι σ i) :=
continuous_infi_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_infi_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 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_infi_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)
/-- 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).1), _⟩,
refine le_antisymm _
(continuous_iff_induced_le.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).2.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
namespace list
variables [topological_space α] [topological_space β]
lemma tendsto_cons' {a : α} {l : list α} :
tendsto (λp:α×list α, list.cons p.1 p.2) ((nhds a).prod (nhds l)) (nhds (a :: l)) :=
by rw [nhds_cons, tendsto, map_prod]; exact le_refl _
lemma tendsto_cons {f : α → β} {g : α → list β}
{a : _root_.filter α} {b : β} {l : list β} (hf : tendsto f a (nhds b)) (hg : tendsto g a (nhds l)):
tendsto (λa, list.cons (f a) (g a)) a (nhds (b :: l)) :=
(tendsto.prod_mk hf hg).comp tendsto_cons'
lemma tendsto_cons_iff [topological_space β]
{f : list α → β} {b : _root_.filter β} {a : α} {l : list α} :
tendsto f (nhds (a :: l)) b ↔ tendsto (λp:α×list α, f (p.1 :: p.2)) ((nhds a).prod (nhds l)) b :=
have nhds (a :: l) = ((nhds a).prod (nhds l)).map (λp:α×list α, (p.1 :: p.2)),
begin
simp only
[nhds_cons, filter.prod_eq, (filter.map_def _ _).symm, (filter.seq_eq_filter_seq _ _).symm],
simp [-filter.seq_eq_filter_seq, -filter.map_def, (∘)] with functor_norm,
end,
by rw [this, filter.tendsto_map'_iff]
lemma tendsto_nhds [topological_space β]
{f : list α → β} {r : list α → _root_.filter β}
(h_nil : tendsto f (pure []) (r []))
(h_cons : ∀l a, tendsto f (nhds l) (r l) → tendsto (λp:α×list α, f (p.1 :: p.2)) ((nhds a).prod (nhds l)) (r (a::l))) :
∀l, tendsto f (nhds l) (r l)
| [] := by rwa [nhds_nil]
| (a::l) := by rw [tendsto_cons_iff]; exact h_cons l a (tendsto_nhds l)
lemma continuous_at_length [topological_space α] :
∀(l : list α), continuous_at list.length l :=
begin
simp only [continuous_at, nhds_discrete],
refine tendsto_nhds _ _,
{ exact tendsto_pure_pure _ _ },
{ assume l a ih,
dsimp only [list.length],
refine tendsto.comp _ (tendsto_pure_pure (λx, x + 1) _),
refine tendsto.comp tendsto_snd ih }
end
lemma tendsto_insert_nth' {a : α} : ∀{n : ℕ} {l : list α},
tendsto (λp:α×list α, insert_nth n p.1 p.2) ((nhds a).prod (nhds l)) (nhds (insert_nth n a l))
| 0 l := tendsto_cons'
| (n+1) [] :=
suffices tendsto (λa, []) (nhds a) (nhds ([] : list α)),
by simpa [nhds_nil, tendsto, map_prod, -filter.pure_def, (∘), insert_nth],
tendsto_const_nhds
| (n+1) (a'::l) :=
have (nhds a).prod (nhds (a' :: l)) =
((nhds a).prod ((nhds a').prod (nhds l))).map (λp:α×α×list α, (p.1, p.2.1 :: p.2.2)),
begin
simp only
[nhds_cons, filter.prod_eq, (filter.map_def _ _).symm, (filter.seq_eq_filter_seq _ _).symm],
simp [-filter.seq_eq_filter_seq, -filter.map_def, (∘)] with functor_norm
end,
begin
rw [this, tendsto_map'_iff],
exact tendsto_cons
(tendsto_snd.comp tendsto_fst)
((tendsto.prod_mk tendsto_fst (tendsto_snd.comp tendsto_snd)).comp (@tendsto_insert_nth' n l))
end
lemma tendsto_insert_nth {n : ℕ} {a : α} {l : list α} {f : β → α} {g : β → list α}
{b : _root_.filter β} (hf : tendsto f b (nhds a)) (hg : tendsto g b (nhds l)) :
tendsto (λb:β, insert_nth n (f b) (g b)) b (nhds (insert_nth n a l)) :=
(tendsto.prod_mk hf hg).comp tendsto_insert_nth'
lemma continuous_insert_nth {n : ℕ} : continuous (λp:α×list α, insert_nth n p.1 p.2) :=
continuous_iff_continuous_at.mpr $
assume ⟨a, l⟩, by rw [continuous_at, nhds_prod_eq]; exact tendsto_insert_nth'
lemma tendsto_remove_nth : ∀{n : ℕ} {l : list α},
tendsto (λl, remove_nth l n) (nhds l) (nhds (remove_nth l n))
| _ [] := by rw [nhds_nil]; exact tendsto_pure_nhds _ _
| 0 (a::l) := by rw [tendsto_cons_iff]; exact tendsto_snd
| (n+1) (a::l) :=
begin
rw [tendsto_cons_iff],
dsimp [remove_nth],
exact tendsto_cons tendsto_fst (tendsto_snd.comp (@tendsto_remove_nth n l))
end
lemma continuous_remove_nth {n : ℕ} : continuous (λl : list α, remove_nth l n) :=
continuous_iff_continuous_at.mpr $ assume a, tendsto_remove_nth
end list
namespace vector
open list filter
instance (n : ℕ) [topological_space α] : topological_space (vector α n) :=
by unfold vector; apply_instance
lemma cons_val {n : ℕ} {a : α} : ∀{v : vector α n}, (a :: v).val = a :: v.val
| ⟨l, hl⟩ := rfl
lemma tendsto_cons [topological_space α] {n : ℕ} {a : α} {l : vector α n}:
tendsto (λp:α×vector α n, vector.cons p.1 p.2) ((nhds a).prod (nhds l)) (nhds (a :: l)) :=
by
simp [tendsto_subtype_rng, cons_val];
exact tendsto_cons tendsto_fst (tendsto.comp tendsto_snd continuous_at_subtype_val)
lemma tendsto_insert_nth
[topological_space α] {n : ℕ} {i : fin (n+1)} {a:α} :
∀{l:vector α n}, tendsto (λp:α×vector α n, insert_nth p.1 i p.2)
((nhds a).prod (nhds l)) (nhds (insert_nth a i l))
| ⟨l, hl⟩ :=
begin
rw [insert_nth, tendsto_subtype_rng],
simp [insert_nth_val],
exact list.tendsto_insert_nth tendsto_fst (tendsto.comp tendsto_snd continuous_at_subtype_val)
end
lemma continuous_insert_nth' [topological_space α] {n : ℕ} {i : fin (n+1)} :
continuous (λp:α×vector α n, insert_nth p.1 i p.2) :=
continuous_iff_continuous_at.mpr $ assume ⟨a, l⟩,
by rw [continuous_at, nhds_prod_eq]; exact tendsto_insert_nth
lemma continuous_insert_nth [topological_space α] [topological_space β] {n : ℕ} {i : fin (n+1)}
{f : β → α} {g : β → vector α n} (hf : continuous f) (hg : continuous g) :
continuous (λb, insert_nth (f b) i (g b)) :=
continuous_insert_nth'.comp (continuous.prod_mk hf hg)
lemma continuous_at_remove_nth [topological_space α] {n : ℕ} {i : fin (n+1)} :
∀{l:vector α (n+1)}, continuous_at (remove_nth i) l
| ⟨l, hl⟩ :=
-- ∀{l:vector α (n+1)}, tendsto (remove_nth i) (nhds l) (nhds (remove_nth i l))
--| ⟨l, hl⟩ :=
begin
rw [continuous_at, remove_nth, tendsto_subtype_rng],
simp [remove_nth_val],
exact continuous_at_subtype_val.comp list.tendsto_remove_nth
end
lemma continuous_remove_nth [topological_space α] {n : ℕ} {i : fin (n+1)} :
continuous (remove_nth i : vector α (n+1) → vector α n) :=
continuous_iff_continuous_at.mpr $ assume ⟨a, l⟩, continuous_at_remove_nth
end vector
namespace dense_embedding
variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
/-- The product of two dense embeddings is a dense embedding -/
protected def prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁) (de₂ : dense_embedding e₂) :
dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) :=
{ dense_embedding .
dense :=
have closure (range (λ(p : α × γ), (e₁ p.1, e₂ p.2))) =
set.prod (closure (range e₁)) (closure (range e₂)),
by rw [←closure_prod_eq, prod_range_range_eq],
assume ⟨b, d⟩, begin rw [this], simp, constructor, apply de₁.dense, apply de₂.dense end,
inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩,
by simp; exact assume h₁ h₂, ⟨de₁.inj h₁, de₂.inj h₂⟩,
induced := assume ⟨a, b⟩,
by rw [nhds_prod_eq, nhds_prod_eq, ←prod_comap_comap_eq, de₁.induced, de₂.induced] }
def subtype_emb (p : α → Prop) {e : α → β} (de : dense_embedding e) (x : {x // p x}) :
{x // x ∈ closure (e '' {x | p x})} :=
⟨e x.1, subset_closure $ mem_image_of_mem e x.2⟩
protected def subtype (p : α → Prop) {e : α → β} (de : dense_embedding e) :
dense_embedding (de.subtype_emb p) :=
{ dense_embedding .
dense := assume ⟨x, hx⟩, closure_subtype.mpr $
have (λ (x : {x // p x}), e (x.val)) = e ∘ subtype.val, from rfl,
begin
rw ← image_univ,
simp [(image_comp _ _ _).symm, (∘), subtype_emb, -image_univ],
rw [this, image_comp, subtype.val_image],
simp,
assumption
end,
inj := assume ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq $ de.inj $ @@congr_arg subtype.val h,
induced := assume ⟨x, hx⟩,
by simp [subtype_emb, nhds_subtype_eq_comap, comap_comap_comp, (∘), (de.induced x).symm] }
end dense_embedding
lemma is_closed_property [topological_space α] [topological_space β] {e : α → β} {p : β → Prop}
(he : closure (range e) = univ) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) :
∀b, p b :=
have univ ⊆ {b | p b},
from calc univ = closure (range e) : he.symm
... ⊆ closure {b | p b} : closure_mono $ range_subset_iff.mpr h
... = _ : closure_eq_of_is_closed hp,
assume b, this trivial
lemma is_closed_property2 [topological_space α] [topological_space β] {e : α → β} {p : β → β → Prop}
(he : dense_embedding e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) :
∀b₁ b₂, p b₁ b₂ :=
have ∀q:β×β, p q.1 q.2,
from is_closed_property (he.prod he).closure_range hp $ assume a, h _ _,
assume b₁ b₂, this ⟨b₁, b₂⟩
lemma is_closed_property3 [topological_space α] [topological_space β] {e : α → β} {p : β → β → β → Prop}
(he : dense_embedding e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2}) (h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) :
∀b₁ b₂ b₃, p b₁ b₂ b₃ :=
have ∀q:β×β×β, p q.1 q.2.1 q.2.2,
from is_closed_property (he.prod $ he.prod he).closure_range hp $ assume ⟨a₁, a₂, a₃⟩, h _ _ _,
assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩
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₂
/-- α and β are homeomorph, also called topological isomoph -/
structure homeomorph (α : Type*) (β : Type*) [topological_space α] [topological_space β]
extends α ≃ β :=
(continuous_to_fun : continuous to_fun)
(continuous_inv_fun : continuous inv_fun)
infix ` ≃ₜ `:50 := homeomorph
namespace homeomorph
variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ]
instance : has_coe_to_fun (α ≃ₜ β) := ⟨λ_, α → β, λe, e.to_equiv⟩
lemma coe_eq_to_equiv (h : α ≃ₜ β) (a : α) : h a = h.to_equiv a := rfl
protected def refl (α : Type*) [topological_space α] : α ≃ₜ α :=
{ continuous_to_fun := continuous_id, continuous_inv_fun := continuous_id, .. equiv.refl α }
protected def trans (h₁ : α ≃ₜ β) (h₂ : β ≃ₜ γ) : α ≃ₜ γ :=
{ continuous_to_fun := h₂.continuous_to_fun.comp h₁.continuous_to_fun,
continuous_inv_fun := h₁.continuous_inv_fun.comp h₂.continuous_inv_fun,
.. equiv.trans h₁.to_equiv h₂.to_equiv }
protected def symm (h : α ≃ₜ β) : β ≃ₜ α :=
{ continuous_to_fun := h.continuous_inv_fun,
continuous_inv_fun := h.continuous_to_fun,
.. h.to_equiv.symm }
protected def continuous (h : α ≃ₜ β) : continuous h := h.continuous_to_fun
lemma symm_comp_self (h : α ≃ₜ β) : ⇑h.symm ∘ ⇑h = id :=
funext $ assume a, h.to_equiv.left_inv a
lemma self_comp_symm (h : α ≃ₜ β) : ⇑h ∘ ⇑h.symm = id :=
funext $ assume a, h.to_equiv.right_inv a
lemma range_coe (h : α ≃ₜ β) : range h = univ :=
eq_univ_of_forall $ assume b, ⟨h.symm b, congr_fun h.self_comp_symm b⟩
lemma image_symm (h : α ≃ₜ β) : image h.symm = preimage h :=
image_eq_preimage_of_inverse h.symm.to_equiv.left_inv h.symm.to_equiv.right_inv
lemma preimage_symm (h : α ≃ₜ β) : preimage h.symm = image h :=
(image_eq_preimage_of_inverse h.to_equiv.left_inv h.to_equiv.right_inv).symm
lemma induced_eq
{α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] (h : α ≃ₜ β) :
tβ.induced h = tα :=
le_antisymm
(induced_le_iff_le_coinduced.2 h.continuous)
(calc tα = (tα.induced h.symm).induced h : by rw [induced_compose, symm_comp_self, induced_id]
... ≤ tβ.induced h : induced_mono $ (induced_le_iff_le_coinduced.2 h.symm.continuous))
lemma coinduced_eq
{α : Type*} {β : Type*} [tα : topological_space α] [tβ : topological_space β] (h : α ≃ₜ β) :
tα.coinduced h = tβ :=
le_antisymm
(calc tα.coinduced h ≤ (tβ.coinduced h.symm).coinduced h : coinduced_mono h.symm.continuous
... = tβ : by rw [coinduced_compose, self_comp_symm, coinduced_id])
h.continuous
lemma compact_image {s : set α} (h : α ≃ₜ β) : compact (h '' s) ↔ compact s :=
⟨λ hs, by have := compact_image hs h.symm.continuous;
rwa [← image_comp, symm_comp_self, image_id] at this,
λ hs, compact_image hs h.continuous⟩
lemma compact_preimage {s : set β} (h : α ≃ₜ β) : compact (h ⁻¹' s) ↔ compact s :=
by rw ← image_symm; exact h.symm.compact_image
protected lemma embedding (h : α ≃ₜ β) : embedding h :=
⟨h.to_equiv.injective, h.induced_eq.symm⟩
protected lemma dense_embedding (h : α ≃ₜ β) : dense_embedding h :=
{ dense := assume a, by rw [h.range_coe, closure_univ]; trivial,
inj := h.to_equiv.injective,
induced := assume a, by rw [← nhds_induced_eq_comap, h.induced_eq] }
protected lemma is_open_map (h : α ≃ₜ β) : is_open_map h :=
begin
assume s,
rw ← h.preimage_symm,
exact h.symm.continuous s
end
protected lemma quotient_map (h : α ≃ₜ β) : quotient_map h :=
⟨h.to_equiv.surjective, h.coinduced_eq.symm⟩
def prod_congr (h₁ : α ≃ₜ β) (h₂ : γ ≃ₜ δ) : (α × γ) ≃ₜ (β × δ) :=
{ continuous_to_fun :=
continuous.prod_mk (h₁.continuous.comp continuous_fst) (h₂.continuous.comp continuous_snd),
continuous_inv_fun :=
continuous.prod_mk (h₁.symm.continuous.comp continuous_fst) (h₂.symm.continuous.comp continuous_snd),
.. h₁.to_equiv.prod_congr h₂.to_equiv }
section
variables (α β γ)
def prod_comm : (α × β) ≃ₜ (β × α) :=
{ continuous_to_fun := continuous.prod_mk continuous_snd continuous_fst,
continuous_inv_fun := continuous.prod_mk continuous_snd continuous_fst,
.. equiv.prod_comm α β }
def prod_assoc : ((α × β) × γ) ≃ₜ (α × (β × γ)) :=
{ continuous_to_fun :=
continuous.prod_mk (continuous_fst.comp continuous_fst)
(continuous.prod_mk (continuous_snd.comp continuous_fst) continuous_snd),
continuous_inv_fun := continuous.prod_mk
(continuous.prod_mk continuous_fst (continuous_fst.comp continuous_snd))
(continuous_snd.comp continuous_snd),
.. equiv.prod_assoc α β γ }
end
end homeomorph
|
40fdb852aa24d0ac76fd3bc36c5b27c9e9119276 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/linear_algebra/affine_space/affine_map.lean | 3714083f07b4f428aed6a898b9831b15f07126da | [
"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 | 25,613 | lean | /-
Copyright (c) 2020 Joseph Myers. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Joseph Myers
-/
import data.set.pointwise.interval
import linear_algebra.affine_space.basic
import linear_algebra.bilinear_map
import linear_algebra.pi
import linear_algebra.prod
/-!
# Affine maps
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file defines affine maps.
## Main definitions
* `affine_map` is the type of affine maps between two affine spaces with the same ring `k`. Various
basic examples of affine maps are defined, including `const`, `id`, `line_map` and `homothety`.
## Notations
* `P1 →ᵃ[k] P2` is a notation for `affine_map k P1 P2`;
* `affine_space V P`: a localized notation for `add_torsor V P` defined in
`linear_algebra.affine_space.basic`.
## Implementation notes
`out_param` is used in the definition of `[add_torsor V P]` to make `V` an implicit argument
(deduced from `P`) in most cases; `include V` is needed in many cases for `V`, and type classes
using it, to be added as implicit arguments to individual lemmas. As for modules, `k` is an
explicit argument rather than implied by `P` or `V`.
This file only provides purely algebraic definitions and results. Those depending on analysis or
topology are defined elsewhere; see `analysis.normed_space.add_torsor` and
`topology.algebra.affine`.
## References
* https://en.wikipedia.org/wiki/Affine_space
* https://en.wikipedia.org/wiki/Principal_homogeneous_space
-/
open_locale affine
/-- An `affine_map k P1 P2` (notation: `P1 →ᵃ[k] P2`) is a map from `P1` to `P2` that
induces a corresponding linear map from `V1` to `V2`. -/
structure affine_map (k : Type*) {V1 : Type*} (P1 : Type*) {V2 : Type*} (P2 : Type*)
[ring k]
[add_comm_group V1] [module k V1] [affine_space V1 P1]
[add_comm_group V2] [module k V2] [affine_space V2 P2] :=
(to_fun : P1 → P2)
(linear : V1 →ₗ[k] V2)
(map_vadd' : ∀ (p : P1) (v : V1), to_fun (v +ᵥ p) = linear v +ᵥ to_fun p)
notation P1 ` →ᵃ[`:25 k:25 `] `:0 P2:0 := affine_map k P1 P2
instance affine_map.fun_like (k : Type*) {V1 : Type*} (P1 : Type*) {V2 : Type*} (P2 : Type*)
[ring k]
[add_comm_group V1] [module k V1] [affine_space V1 P1]
[add_comm_group V2] [module k V2] [affine_space V2 P2]:
fun_like (P1 →ᵃ[k] P2) P1 (λ _, P2) :=
{ coe := affine_map.to_fun,
coe_injective' := λ ⟨f, f_linear, f_add⟩ ⟨g, g_linear, g_add⟩ (h : f = g), begin
cases (add_torsor.nonempty : nonempty P1) with p,
congr' with v,
apply vadd_right_cancel (f p),
erw [← f_add, h, ← g_add]
end }
instance affine_map.has_coe_to_fun (k : Type*) {V1 : Type*} (P1 : Type*) {V2 : Type*} (P2 : Type*)
[ring k]
[add_comm_group V1] [module k V1] [affine_space V1 P1]
[add_comm_group V2] [module k V2] [affine_space V2 P2] :
has_coe_to_fun (P1 →ᵃ[k] P2) (λ _, P1 → P2) := fun_like.has_coe_to_fun
namespace linear_map
variables {k : Type*} {V₁ : Type*} {V₂ : Type*} [ring k] [add_comm_group V₁] [module k V₁]
[add_comm_group V₂] [module k V₂] (f : V₁ →ₗ[k] V₂)
/-- Reinterpret a linear map as an affine map. -/
def to_affine_map : V₁ →ᵃ[k] V₂ :=
{ to_fun := f,
linear := f,
map_vadd' := λ p v, f.map_add v p }
@[simp] lemma coe_to_affine_map : ⇑f.to_affine_map = f := rfl
@[simp] lemma to_affine_map_linear : f.to_affine_map.linear = f := rfl
end linear_map
namespace affine_map
variables {k : Type*} {V1 : Type*} {P1 : Type*} {V2 : Type*} {P2 : Type*}
{V3 : Type*} {P3 : Type*} {V4 : Type*} {P4 : Type*} [ring k]
[add_comm_group V1] [module k V1] [affine_space V1 P1]
[add_comm_group V2] [module k V2] [affine_space V2 P2]
[add_comm_group V3] [module k V3] [affine_space V3 P3]
[add_comm_group V4] [module k V4] [affine_space V4 P4]
include V1 V2
/-- Constructing an affine map and coercing back to a function
produces the same map. -/
@[simp] lemma coe_mk (f : P1 → P2) (linear add) :
((mk f linear add : P1 →ᵃ[k] P2) : P1 → P2) = f := rfl
/-- `to_fun` is the same as the result of coercing to a function. -/
@[simp] lemma to_fun_eq_coe (f : P1 →ᵃ[k] P2) : f.to_fun = ⇑f := rfl
/-- An affine map on the result of adding a vector to a point produces
the same result as the linear map applied to that vector, added to the
affine map applied to that point. -/
@[simp] lemma map_vadd (f : P1 →ᵃ[k] P2) (p : P1) (v : V1) :
f (v +ᵥ p) = f.linear v +ᵥ f p := f.map_vadd' p v
/-- The linear map on the result of subtracting two points is the
result of subtracting the result of the affine map on those two
points. -/
@[simp] lemma linear_map_vsub (f : P1 →ᵃ[k] P2) (p1 p2 : P1) :
f.linear (p1 -ᵥ p2) = f p1 -ᵥ f p2 :=
by conv_rhs { rw [←vsub_vadd p1 p2, map_vadd, vadd_vsub] }
/-- Two affine maps are equal if they coerce to the same function. -/
@[ext] lemma ext {f g : P1 →ᵃ[k] P2} (h : ∀ p, f p = g p) : f = g :=
fun_like.ext _ _ h
lemma ext_iff {f g : P1 →ᵃ[k] P2} : f = g ↔ ∀ p, f p = g p := ⟨λ h p, h ▸ rfl, ext⟩
lemma coe_fn_injective : @function.injective (P1 →ᵃ[k] P2) (P1 → P2) coe_fn :=
fun_like.coe_injective
protected lemma congr_arg (f : P1 →ᵃ[k] P2) {x y : P1} (h : x = y) : f x = f y :=
congr_arg _ h
protected lemma congr_fun {f g : P1 →ᵃ[k] P2} (h : f = g) (x : P1) : f x = g x :=
h ▸ rfl
variables (k P1)
/-- Constant function as an `affine_map`. -/
def const (p : P2) : P1 →ᵃ[k] P2 :=
{ to_fun := function.const P1 p,
linear := 0,
map_vadd' := λ p v, by simp }
@[simp] lemma coe_const (p : P2) : ⇑(const k P1 p) = function.const P1 p := rfl
@[simp] lemma const_linear (p : P2) : (const k P1 p).linear = 0 := rfl
variables {k P1}
lemma linear_eq_zero_iff_exists_const (f : P1 →ᵃ[k] P2) :
f.linear = 0 ↔ ∃ q, f = const k P1 q :=
begin
refine ⟨λ h, _, λ h, _⟩,
{ use f (classical.arbitrary P1),
ext,
rw [coe_const, function.const_apply, ← @vsub_eq_zero_iff_eq V2, ← f.linear_map_vsub, h,
linear_map.zero_apply], },
{ rcases h with ⟨q, rfl⟩,
exact const_linear k P1 q, },
end
instance nonempty : nonempty (P1 →ᵃ[k] P2) :=
(add_torsor.nonempty : nonempty P2).elim $ λ p, ⟨const k P1 p⟩
/-- Construct an affine map by verifying the relation between the map and its linear part at one
base point. Namely, this function takes a map `f : P₁ → P₂`, a linear map `f' : V₁ →ₗ[k] V₂`, and
a point `p` such that for any other point `p'` we have `f p' = f' (p' -ᵥ p) +ᵥ f p`. -/
def mk' (f : P1 → P2) (f' : V1 →ₗ[k] V2) (p : P1) (h : ∀ p' : P1, f p' = f' (p' -ᵥ p) +ᵥ f p) :
P1 →ᵃ[k] P2 :=
{ to_fun := f,
linear := f',
map_vadd' := λ p' v, by rw [h, h p', vadd_vsub_assoc, f'.map_add, vadd_vadd] }
@[simp] lemma coe_mk' (f : P1 → P2) (f' : V1 →ₗ[k] V2) (p h) : ⇑(mk' f f' p h) = f := rfl
@[simp] lemma mk'_linear (f : P1 → P2) (f' : V1 →ₗ[k] V2) (p h) : (mk' f f' p h).linear = f' := rfl
section has_smul
variables {R : Type*} [monoid R] [distrib_mul_action R V2] [smul_comm_class k R V2]
/-- The space of affine maps to a module inherits an `R`-action from the action on its codomain. -/
instance : mul_action R (P1 →ᵃ[k] V2) :=
{ smul := λ c f, ⟨c • f, c • f.linear, λ p v, by simp [smul_add]⟩,
one_smul := λ f, ext $ λ p, one_smul _ _,
mul_smul := λ c₁ c₂ f, ext $ λ p, mul_smul _ _ _ }
@[simp, norm_cast] lemma coe_smul (c : R) (f : P1 →ᵃ[k] V2) : ⇑(c • f) = c • f := rfl
@[simp] lemma smul_linear (t : R) (f : P1 →ᵃ[k] V2) : (t • f).linear = t • f.linear := rfl
instance [distrib_mul_action Rᵐᵒᵖ V2] [is_central_scalar R V2] :
is_central_scalar R (P1 →ᵃ[k] V2) :=
{ op_smul_eq_smul := λ r x, ext $ λ _, op_smul_eq_smul _ _ }
end has_smul
instance : has_zero (P1 →ᵃ[k] V2) := { zero := ⟨0, 0, λ p v, (zero_vadd _ _).symm⟩ }
instance : has_add (P1 →ᵃ[k] V2) :=
{ add := λ f g, ⟨f + g, f.linear + g.linear, λ p v, by simp [add_add_add_comm]⟩ }
instance : has_sub (P1 →ᵃ[k] V2) :=
{ sub := λ f g, ⟨f - g, f.linear - g.linear, λ p v, by simp [sub_add_sub_comm]⟩ }
instance : has_neg (P1 →ᵃ[k] V2) := { neg := λ f, ⟨-f, -f.linear, λ p v, by simp [add_comm]⟩ }
@[simp, norm_cast] lemma coe_zero : ⇑(0 : P1 →ᵃ[k] V2) = 0 := rfl
@[simp, norm_cast] lemma coe_add (f g : P1 →ᵃ[k] V2) : ⇑(f + g) = f + g := rfl
@[simp, norm_cast] lemma coe_neg (f : P1 →ᵃ[k] V2) : ⇑(-f) = -f := rfl
@[simp, norm_cast] lemma coe_sub (f g : P1 →ᵃ[k] V2) : ⇑(f - g) = f - g := rfl
@[simp] lemma zero_linear : (0 : P1 →ᵃ[k] V2).linear = 0 := rfl
@[simp] lemma add_linear (f g : P1 →ᵃ[k] V2) : (f + g).linear = f.linear + g.linear := rfl
@[simp] lemma sub_linear (f g : P1 →ᵃ[k] V2) : (f - g).linear = f.linear - g.linear := rfl
@[simp] lemma neg_linear (f : P1 →ᵃ[k] V2) : (-f).linear = -f.linear := rfl
/-- The set of affine maps to a vector space is an additive commutative group. -/
instance : add_comm_group (P1 →ᵃ[k] V2) :=
coe_fn_injective.add_comm_group _
coe_zero coe_add coe_neg coe_sub (λ _ _, coe_smul _ _) (λ _ _, coe_smul _ _)
/-- The space of affine maps from `P1` to `P2` is an affine space over the space of affine maps
from `P1` to the vector space `V2` corresponding to `P2`. -/
instance : affine_space (P1 →ᵃ[k] V2) (P1 →ᵃ[k] P2) :=
{ vadd := λ f g, ⟨λ p, f p +ᵥ g p, f.linear + g.linear, λ p v,
by simp [vadd_vadd, add_right_comm]⟩,
zero_vadd := λ f, ext $ λ p, zero_vadd _ (f p),
add_vadd := λ f₁ f₂ f₃, ext $ λ p, add_vadd (f₁ p) (f₂ p) (f₃ p),
vsub := λ f g, ⟨λ p, f p -ᵥ g p, f.linear - g.linear, λ p v,
by simp [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub, sub_add_eq_add_sub]⟩,
vsub_vadd' := λ f g, ext $ λ p, vsub_vadd (f p) (g p),
vadd_vsub' := λ f g, ext $ λ p, vadd_vsub (f p) (g p) }
@[simp] lemma vadd_apply (f : P1 →ᵃ[k] V2) (g : P1 →ᵃ[k] P2) (p : P1) :
(f +ᵥ g) p = f p +ᵥ g p :=
rfl
@[simp] lemma vsub_apply (f g : P1 →ᵃ[k] P2) (p : P1) :
(f -ᵥ g : P1 →ᵃ[k] V2) p = f p -ᵥ g p :=
rfl
/-- `prod.fst` as an `affine_map`. -/
def fst : (P1 × P2) →ᵃ[k] P1 :=
{ to_fun := prod.fst,
linear := linear_map.fst k V1 V2,
map_vadd' := λ _ _, rfl }
@[simp] lemma coe_fst : ⇑(fst : (P1 × P2) →ᵃ[k] P1) = prod.fst := rfl
@[simp] lemma fst_linear : (fst : (P1 × P2) →ᵃ[k] P1).linear = linear_map.fst k V1 V2 := rfl
/-- `prod.snd` as an `affine_map`. -/
def snd : (P1 × P2) →ᵃ[k] P2 :=
{ to_fun := prod.snd,
linear := linear_map.snd k V1 V2,
map_vadd' := λ _ _, rfl }
@[simp] lemma coe_snd : ⇑(snd : (P1 × P2) →ᵃ[k] P2) = prod.snd := rfl
@[simp] lemma snd_linear : (snd : (P1 × P2) →ᵃ[k] P2).linear = linear_map.snd k V1 V2 := rfl
variables (k P1)
omit V2
/-- Identity map as an affine map. -/
def id : P1 →ᵃ[k] P1 :=
{ to_fun := id,
linear := linear_map.id,
map_vadd' := λ p v, rfl }
/-- The identity affine map acts as the identity. -/
@[simp] lemma coe_id : ⇑(id k P1) = _root_.id := rfl
@[simp] lemma id_linear : (id k P1).linear = linear_map.id := rfl
variable {P1}
/-- The identity affine map acts as the identity. -/
lemma id_apply (p : P1) : id k P1 p = p := rfl
variables {k P1}
instance : inhabited (P1 →ᵃ[k] P1) := ⟨id k P1⟩
include V2 V3
/-- Composition of affine maps. -/
def comp (f : P2 →ᵃ[k] P3) (g : P1 →ᵃ[k] P2) : P1 →ᵃ[k] P3 :=
{ to_fun := f ∘ g,
linear := f.linear.comp g.linear,
map_vadd' := begin
intros p v,
rw [function.comp_app, g.map_vadd, f.map_vadd],
refl
end }
/-- Composition of affine maps acts as applying the two functions. -/
@[simp] lemma coe_comp (f : P2 →ᵃ[k] P3) (g : P1 →ᵃ[k] P2) :
⇑(f.comp g) = f ∘ g := rfl
/-- Composition of affine maps acts as applying the two functions. -/
lemma comp_apply (f : P2 →ᵃ[k] P3) (g : P1 →ᵃ[k] P2) (p : P1) :
f.comp g p = f (g p) := rfl
omit V3
@[simp] lemma comp_id (f : P1 →ᵃ[k] P2) : f.comp (id k P1) = f := ext $ λ p, rfl
@[simp] lemma id_comp (f : P1 →ᵃ[k] P2) : (id k P2).comp f = f := ext $ λ p, rfl
include V3 V4
lemma comp_assoc (f₃₄ : P3 →ᵃ[k] P4) (f₂₃ : P2 →ᵃ[k] P3) (f₁₂ : P1 →ᵃ[k] P2) :
(f₃₄.comp f₂₃).comp f₁₂ = f₃₄.comp (f₂₃.comp f₁₂) :=
rfl
omit V2 V3 V4
instance : monoid (P1 →ᵃ[k] P1) :=
{ one := id k P1,
mul := comp,
one_mul := id_comp,
mul_one := comp_id,
mul_assoc := comp_assoc }
@[simp] lemma coe_mul (f g : P1 →ᵃ[k] P1) : ⇑(f * g) = f ∘ g := rfl
@[simp] lemma coe_one : ⇑(1 : P1 →ᵃ[k] P1) = _root_.id := rfl
/-- `affine_map.linear` on endomorphisms is a `monoid_hom`. -/
@[simps] def linear_hom : (P1 →ᵃ[k] P1) →* (V1 →ₗ[k] V1) :=
{ to_fun := linear,
map_one' := rfl,
map_mul' := λ _ _, rfl }
include V2
@[simp] lemma linear_injective_iff (f : P1 →ᵃ[k] P2) :
function.injective f.linear ↔ function.injective f :=
begin
obtain ⟨p⟩ := (infer_instance : nonempty P1),
have h : ⇑f.linear = (equiv.vadd_const (f p)).symm ∘ f ∘ (equiv.vadd_const p),
{ ext v, simp [f.map_vadd, vadd_vsub_assoc], },
rw [h, equiv.comp_injective, equiv.injective_comp],
end
@[simp] lemma linear_surjective_iff (f : P1 →ᵃ[k] P2) :
function.surjective f.linear ↔ function.surjective f :=
begin
obtain ⟨p⟩ := (infer_instance : nonempty P1),
have h : ⇑f.linear = (equiv.vadd_const (f p)).symm ∘ f ∘ (equiv.vadd_const p),
{ ext v, simp [f.map_vadd, vadd_vsub_assoc], },
rw [h, equiv.comp_surjective, equiv.surjective_comp],
end
@[simp] lemma linear_bijective_iff (f : P1 →ᵃ[k] P2) :
function.bijective f.linear ↔ function.bijective f :=
and_congr f.linear_injective_iff f.linear_surjective_iff
lemma image_vsub_image {s t : set P1} (f : P1 →ᵃ[k] P2) :
(f '' s) -ᵥ (f '' t) = f.linear '' (s -ᵥ t) :=
begin
ext v,
simp only [set.mem_vsub, set.mem_image, exists_exists_and_eq_and, exists_and_distrib_left,
← f.linear_map_vsub],
split,
{ rintros ⟨x, hx, y, hy, hv⟩,
exact ⟨x -ᵥ y, ⟨x, hx, y, hy, rfl⟩, hv⟩, },
{ rintros ⟨-, ⟨x, hx, y, hy, rfl⟩, rfl⟩,
exact ⟨x, hx, y, hy, rfl⟩, },
end
omit V2
/-! ### Definition of `affine_map.line_map` and lemmas about it -/
/-- The affine map from `k` to `P1` sending `0` to `p₀` and `1` to `p₁`. -/
def line_map (p₀ p₁ : P1) : k →ᵃ[k] P1 :=
((linear_map.id : k →ₗ[k] k).smul_right (p₁ -ᵥ p₀)).to_affine_map +ᵥ const k k p₀
lemma coe_line_map (p₀ p₁ : P1) : (line_map p₀ p₁ : k → P1) = λ c, c • (p₁ -ᵥ p₀) +ᵥ p₀ := rfl
lemma line_map_apply (p₀ p₁ : P1) (c : k) : line_map p₀ p₁ c = c • (p₁ -ᵥ p₀) +ᵥ p₀ := rfl
lemma line_map_apply_module' (p₀ p₁ : V1) (c : k) : line_map p₀ p₁ c = c • (p₁ - p₀) + p₀ := rfl
lemma line_map_apply_module (p₀ p₁ : V1) (c : k) : line_map p₀ p₁ c = (1 - c) • p₀ + c • p₁ :=
by simp [line_map_apply_module', smul_sub, sub_smul]; abel
omit V1
lemma line_map_apply_ring' (a b c : k) : line_map a b c = c * (b - a) + a :=
rfl
lemma line_map_apply_ring (a b c : k) : line_map a b c = (1 - c) * a + c * b :=
line_map_apply_module a b c
include V1
lemma line_map_vadd_apply (p : P1) (v : V1) (c : k) :
line_map p (v +ᵥ p) c = c • v +ᵥ p :=
by rw [line_map_apply, vadd_vsub]
@[simp] lemma line_map_linear (p₀ p₁ : P1) :
(line_map p₀ p₁ : k →ᵃ[k] P1).linear = linear_map.id.smul_right (p₁ -ᵥ p₀) :=
add_zero _
lemma line_map_same_apply (p : P1) (c : k) : line_map p p c = p := by simp [line_map_apply]
@[simp] lemma line_map_same (p : P1) : line_map p p = const k k p :=
ext $ line_map_same_apply p
@[simp] lemma line_map_apply_zero (p₀ p₁ : P1) : line_map p₀ p₁ (0:k) = p₀ :=
by simp [line_map_apply]
@[simp] lemma line_map_apply_one (p₀ p₁ : P1) : line_map p₀ p₁ (1:k) = p₁ :=
by simp [line_map_apply]
@[simp] lemma line_map_eq_line_map_iff [no_zero_smul_divisors k V1] {p₀ p₁ : P1} {c₁ c₂ : k} :
line_map p₀ p₁ c₁ = line_map p₀ p₁ c₂ ↔ p₀ = p₁ ∨ c₁ = c₂ :=
by rw [line_map_apply, line_map_apply, ←@vsub_eq_zero_iff_eq V1, vadd_vsub_vadd_cancel_right,
←sub_smul, smul_eq_zero, sub_eq_zero, vsub_eq_zero_iff_eq, or_comm, eq_comm]
@[simp] lemma line_map_eq_left_iff [no_zero_smul_divisors k V1] {p₀ p₁ : P1} {c : k} :
line_map p₀ p₁ c = p₀ ↔ p₀ = p₁ ∨ c = 0 :=
by rw [←@line_map_eq_line_map_iff k V1, line_map_apply_zero]
@[simp] lemma line_map_eq_right_iff [no_zero_smul_divisors k V1] {p₀ p₁ : P1} {c : k} :
line_map p₀ p₁ c = p₁ ↔ p₀ = p₁ ∨ c = 1 :=
by rw [←@line_map_eq_line_map_iff k V1, line_map_apply_one]
variables (k)
lemma line_map_injective [no_zero_smul_divisors k V1] {p₀ p₁ : P1} (h : p₀ ≠ p₁) :
function.injective (line_map p₀ p₁ : k → P1) :=
λ c₁ c₂ hc, (line_map_eq_line_map_iff.mp hc).resolve_left h
variables {k}
include V2
@[simp] lemma apply_line_map (f : P1 →ᵃ[k] P2) (p₀ p₁ : P1) (c : k) :
f (line_map p₀ p₁ c) = line_map (f p₀) (f p₁) c :=
by simp [line_map_apply]
@[simp] lemma comp_line_map (f : P1 →ᵃ[k] P2) (p₀ p₁ : P1) :
f.comp (line_map p₀ p₁) = line_map (f p₀) (f p₁) :=
ext $ f.apply_line_map p₀ p₁
@[simp] lemma fst_line_map (p₀ p₁ : P1 × P2) (c : k) :
(line_map p₀ p₁ c).1 = line_map p₀.1 p₁.1 c :=
fst.apply_line_map p₀ p₁ c
@[simp] lemma snd_line_map (p₀ p₁ : P1 × P2) (c : k) :
(line_map p₀ p₁ c).2 = line_map p₀.2 p₁.2 c :=
snd.apply_line_map p₀ p₁ c
omit V2
lemma line_map_symm (p₀ p₁ : P1) :
line_map p₀ p₁ = (line_map p₁ p₀).comp (line_map (1:k) (0:k)) :=
by { rw [comp_line_map], simp }
lemma line_map_apply_one_sub (p₀ p₁ : P1) (c : k) :
line_map p₀ p₁ (1 - c) = line_map p₁ p₀ c :=
by { rw [line_map_symm p₀, comp_apply], congr, simp [line_map_apply] }
@[simp] lemma line_map_vsub_left (p₀ p₁ : P1) (c : k) :
line_map p₀ p₁ c -ᵥ p₀ = c • (p₁ -ᵥ p₀) :=
vadd_vsub _ _
@[simp] lemma left_vsub_line_map (p₀ p₁ : P1) (c : k) :
p₀ -ᵥ line_map p₀ p₁ c = c • (p₀ -ᵥ p₁) :=
by rw [← neg_vsub_eq_vsub_rev, line_map_vsub_left, ← smul_neg, neg_vsub_eq_vsub_rev]
@[simp] lemma line_map_vsub_right (p₀ p₁ : P1) (c : k) :
line_map p₀ p₁ c -ᵥ p₁ = (1 - c) • (p₀ -ᵥ p₁) :=
by rw [← line_map_apply_one_sub, line_map_vsub_left]
@[simp] lemma right_vsub_line_map (p₀ p₁ : P1) (c : k) :
p₁ -ᵥ line_map p₀ p₁ c = (1 - c) • (p₁ -ᵥ p₀) :=
by rw [← line_map_apply_one_sub, left_vsub_line_map]
lemma line_map_vadd_line_map (v₁ v₂ : V1) (p₁ p₂ : P1) (c : k) :
line_map v₁ v₂ c +ᵥ line_map p₁ p₂ c = line_map (v₁ +ᵥ p₁) (v₂ +ᵥ p₂) c :=
((fst : V1 × P1 →ᵃ[k] V1) +ᵥ snd).apply_line_map (v₁, p₁) (v₂, p₂) c
lemma line_map_vsub_line_map (p₁ p₂ p₃ p₄ : P1) (c : k) :
line_map p₁ p₂ c -ᵥ line_map p₃ p₄ c = line_map (p₁ -ᵥ p₃) (p₂ -ᵥ p₄) c :=
-- Why Lean fails to find this instance without a hint?
by letI : affine_space (V1 × V1) (P1 × P1) := prod.add_torsor; exact
((fst : P1 × P1 →ᵃ[k] P1) -ᵥ (snd : P1 × P1 →ᵃ[k] P1)).apply_line_map (_, _) (_, _) c
/-- Decomposition of an affine map in the special case when the point space and vector space
are the same. -/
lemma decomp (f : V1 →ᵃ[k] V2) : (f : V1 → V2) = f.linear + (λ z, f 0) :=
begin
ext x,
calc
f x = f.linear x +ᵥ f 0 : by simp [← f.map_vadd]
... = (f.linear.to_fun + λ (z : V1), f 0) x : by simp
end
/-- Decomposition of an affine map in the special case when the point space and vector space
are the same. -/
lemma decomp' (f : V1 →ᵃ[k] V2) : (f.linear : V1 → V2) = f - (λ z, f 0) :=
by rw decomp ; simp only [linear_map.map_zero, pi.add_apply, add_sub_cancel, zero_add]
omit V1
lemma image_uIcc {k : Type*} [linear_ordered_field k] (f : k →ᵃ[k] k)
(a b : k) :
f '' set.uIcc a b = set.uIcc (f a) (f b) :=
begin
have : ⇑f = (λ x, x + f 0) ∘ λ x, x * (f 1 - f 0),
{ ext x,
change f x = x • (f 1 -ᵥ f 0) +ᵥ f 0,
rw [← f.linear_map_vsub, ← f.linear.map_smul, ← f.map_vadd],
simp only [vsub_eq_sub, add_zero, mul_one, vadd_eq_add, sub_zero, smul_eq_mul] },
rw [this, set.image_comp],
simp only [set.image_add_const_uIcc, set.image_mul_const_uIcc]
end
section
variables {ι : Type*} {V : Π i : ι, Type*} {P : Π i : ι, Type*} [Π i, add_comm_group (V i)]
[Π i, module k (V i)] [Π i, add_torsor (V i) (P i)]
include V
/-- Evaluation at a point as an affine map. -/
def proj (i : ι) : (Π i : ι, P i) →ᵃ[k] P i :=
{ to_fun := λ f, f i,
linear := @linear_map.proj k ι _ V _ _ i,
map_vadd' := λ p v, rfl }
@[simp] lemma proj_apply (i : ι) (f : Π i, P i) : @proj k _ ι V P _ _ _ i f = f i := rfl
@[simp] lemma proj_linear (i : ι) :
(@proj k _ ι V P _ _ _ i).linear = @linear_map.proj k ι _ V _ _ i := rfl
lemma pi_line_map_apply (f g : Π i, P i) (c : k) (i : ι) :
line_map f g c i = line_map (f i) (g i) c :=
(proj i : (Π i, P i) →ᵃ[k] P i).apply_line_map f g c
end
end affine_map
namespace affine_map
variables {R k V1 P1 V2 : Type*}
section ring
variables [ring k] [add_comm_group V1] [affine_space V1 P1] [add_comm_group V2]
variables [module k V1] [module k V2]
include V1
section distrib_mul_action
variables [monoid R] [distrib_mul_action R V2] [smul_comm_class k R V2]
/-- The space of affine maps to a module inherits an `R`-action from the action on its codomain. -/
instance : distrib_mul_action R (P1 →ᵃ[k] V2) :=
{ smul_add := λ c f g, ext $ λ p, smul_add _ _ _,
smul_zero := λ c, ext $ λ p, smul_zero _ }
end distrib_mul_action
section module
variables [semiring R] [module R V2] [smul_comm_class k R V2]
/-- The space of affine maps taking values in an `R`-module is an `R`-module. -/
instance : module R (P1 →ᵃ[k] V2) :=
{ smul := (•),
add_smul := λ c₁ c₂ f, ext $ λ p, add_smul _ _ _,
zero_smul := λ f, ext $ λ p, zero_smul _ _,
.. affine_map.distrib_mul_action }
variables (R)
/-- The space of affine maps between two modules is linearly equivalent to the product of the
domain with the space of linear maps, by taking the value of the affine map at `(0 : V1)` and the
linear part.
See note [bundled maps over different rings]-/
@[simps] def to_const_prod_linear_map : (V1 →ᵃ[k] V2) ≃ₗ[R] V2 × (V1 →ₗ[k] V2) :=
{ to_fun := λ f, ⟨f 0, f.linear⟩,
inv_fun := λ p, p.2.to_affine_map + const k V1 p.1,
left_inv := λ f, by { ext, rw f.decomp, simp, },
right_inv := by { rintros ⟨v, f⟩, ext; simp, },
map_add' := by simp,
map_smul' := by simp, }
end module
end ring
section comm_ring
variables [comm_ring k] [add_comm_group V1] [affine_space V1 P1] [add_comm_group V2]
variables [module k V1] [module k V2]
include V1
/-- `homothety c r` is the homothety (also known as dilation) about `c` with scale factor `r`. -/
def homothety (c : P1) (r : k) : P1 →ᵃ[k] P1 :=
r • (id k P1 -ᵥ const k P1 c) +ᵥ const k P1 c
lemma homothety_def (c : P1) (r : k) :
homothety c r = r • (id k P1 -ᵥ const k P1 c) +ᵥ const k P1 c :=
rfl
lemma homothety_apply (c : P1) (r : k) (p : P1) : homothety c r p = r • (p -ᵥ c : V1) +ᵥ c := rfl
lemma homothety_eq_line_map (c : P1) (r : k) (p : P1) : homothety c r p = line_map c p r := rfl
@[simp] lemma homothety_one (c : P1) : homothety c (1:k) = id k P1 :=
by { ext p, simp [homothety_apply] }
@[simp] lemma homothety_apply_same (c : P1) (r : k) : homothety c r c = c := line_map_same_apply c r
lemma homothety_mul_apply (c : P1) (r₁ r₂ : k) (p : P1) :
homothety c (r₁ * r₂) p = homothety c r₁ (homothety c r₂ p) :=
by simp [homothety_apply, mul_smul]
lemma homothety_mul (c : P1) (r₁ r₂ : k) :
homothety c (r₁ * r₂) = (homothety c r₁).comp (homothety c r₂) :=
ext $ homothety_mul_apply c r₁ r₂
@[simp] lemma homothety_zero (c : P1) : homothety c (0:k) = const k P1 c :=
by { ext p, simp [homothety_apply] }
@[simp] lemma homothety_add (c : P1) (r₁ r₂ : k) :
homothety c (r₁ + r₂) = r₁ • (id k P1 -ᵥ const k P1 c) +ᵥ homothety c r₂ :=
by simp only [homothety_def, add_smul, vadd_vadd]
/-- `homothety` as a multiplicative monoid homomorphism. -/
def homothety_hom (c : P1) : k →* P1 →ᵃ[k] P1 :=
⟨homothety c, homothety_one c, homothety_mul c⟩
@[simp] lemma coe_homothety_hom (c : P1) : ⇑(homothety_hom c : k →* _) = homothety c := rfl
/-- `homothety` as an affine map. -/
def homothety_affine (c : P1) : k →ᵃ[k] (P1 →ᵃ[k] P1) :=
⟨homothety c, (linear_map.lsmul k _).flip (id k P1 -ᵥ const k P1 c),
function.swap (homothety_add c)⟩
@[simp] lemma coe_homothety_affine (c : P1) :
⇑(homothety_affine c : k →ᵃ[k] _) = homothety c :=
rfl
end comm_ring
end affine_map
section
variables {𝕜 E F : Type*} [ring 𝕜] [add_comm_group E] [add_comm_group F] [module 𝕜 E] [module 𝕜 F]
/-- Applying an affine map to an affine combination of two points yields an affine combination of
the images. -/
lemma convex.combo_affine_apply {x y : E} {a b : 𝕜} {f : E →ᵃ[𝕜] F} (h : a + b = 1) :
f (a • x + b • y) = a • f x + b • f y :=
by { simp only [convex.combo_eq_smul_sub_add h, ←vsub_eq_sub], exact f.apply_line_map _ _ _ }
end
|
4e54ae7e31856e4ac8a4aaf470cdd579de1eac4e | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /library/algebra/category/constructions.lean | 38ada9afe16b1acff8ccc7ce5622c5542fa0c8ca | [
"Apache-2.0"
] | permissive | respu/lean | 6582d19a2f2838a28ecd2b3c6f81c32d07b5341d | 8c76419c60b63d0d9f7bc04ebb0b99812d0ec654 | refs/heads/master | 1,610,882,451,231 | 1,427,747,084,000 | 1,427,747,429,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,958 | lean | -- Copyright (c) 2014 Floris van Doorn. All rights reserved.
-- Released under Apache 2.0 license as described in the file LICENSE.
-- Author: Floris van Doorn
-- This file contains basic constructions on categories, including common categories
import .natural_transformation
import data.unit data.sigma data.prod data.empty data.bool
open eq eq.ops prod
namespace category
namespace opposite
section
definition opposite [reducible] {ob : Type} (C : category ob) : category ob :=
mk (λa b, hom b a)
(λ a b c f g, g ∘ f)
(λ a, id)
(λ a b c d f g h, symm !assoc)
(λ a b f, !id_right)
(λ a b f, !id_left)
definition Opposite [reducible] (C : Category) : Category := Mk (opposite C)
--direct construction:
-- MK C
-- (λa b, hom b a)
-- (λ a b c f g, g ∘ f)
-- (λ a, id)
-- (λ a b c d f g h, symm !assoc)
-- (λ a b f, !id_right)
-- (λ a b f, !id_left)
infixr `∘op`:60 := @compose _ (opposite _) _ _ _
variables {C : Category} {a b c : C}
theorem compose_op {f : hom a b} {g : hom b c} : f ∘op g = g ∘ f := rfl
theorem op_op' {ob : Type} (C : category ob) : opposite (opposite C) = C :=
category.rec (λ hom comp id assoc idl idr, refl (mk _ _ _ _ _ _)) C
theorem op_op : Opposite (Opposite C) = C :=
(@congr_arg _ _ _ _ (Category.mk C) (op_op' C)) ⬝ !Category.equal
end
end opposite
definition type_category [reducible] : category Type :=
mk (λa b, a → b)
(λ a b c, function.compose)
(λ a, function.id)
(λ a b c d h g f, symm (function.compose.assoc h g f))
(λ a b f, function.compose.left_id f)
(λ a b f, function.compose.right_id f)
definition Type_category [reducible] : Category := Mk type_category
section
open decidable unit empty
variables {A : Type} [H : decidable_eq A]
include H
definition set_hom [reducible] (a b : A) := decidable.rec_on (H a b) (λh, unit) (λh, empty)
theorem set_hom_subsingleton [instance] (a b : A) : subsingleton (set_hom a b) := _
definition set_compose [reducible] {a b c : A} (g : set_hom b c) (f : set_hom a b) : set_hom a c :=
decidable.rec_on
(H b c)
(λ Hbc g, decidable.rec_on
(H a b)
(λ Hab f, rec_on_true (trans Hab Hbc) ⋆)
(λh f, empty.rec _ f) f)
(λh (g : empty), empty.rec _ g) g
omit H
definition discrete_category (A : Type) [H : decidable_eq A] : category A :=
mk (λa b, set_hom a b)
(λ a b c g f, set_compose g f)
(λ a, decidable.rec_on_true rfl ⋆)
(λ a b c d h g f, @subsingleton.elim (set_hom a d) _ _ _)
(λ a b f, @subsingleton.elim (set_hom a b) _ _ _)
(λ a b f, @subsingleton.elim (set_hom a b) _ _ _)
local attribute discrete_category [reducible]
definition Discrete_category (A : Type) [H : decidable_eq A] := Mk (discrete_category A)
context
local attribute discrete_category [instance]
include H
theorem discrete_category.endomorphism {a b : A} (f : a ⟶ b) : a = b :=
decidable.rec_on (H a b) (λh f, h) (λh f, empty.rec _ f) f
theorem discrete_category.discrete {a b : A} (f : a ⟶ b)
: eq.rec_on (discrete_category.endomorphism f) f = (ID b) :=
@subsingleton.elim _ !set_hom_subsingleton _ _
definition discrete_category.rec_on {P : Πa b, a ⟶ b → Type} {a b : A} (f : a ⟶ b)
(H : ∀a, P a a id) : P a b f :=
cast (dcongr_arg3 P rfl (discrete_category.endomorphism f)⁻¹
(@subsingleton.elim _ !set_hom_subsingleton _ _))⁻¹ (H a)
end
end
section
open unit bool
definition category_one := discrete_category unit
definition Category_one := Mk category_one
definition category_two := discrete_category bool
definition Category_two := Mk category_two
end
namespace product
section
open prod
definition prod_category [reducible] {obC obD : Type} (C : category obC) (D : category obD)
: category (obC × obD) :=
mk (λa b, hom (pr1 a) (pr1 b) × hom (pr2 a) (pr2 b))
(λ a b c g f, (pr1 g ∘ pr1 f , pr2 g ∘ pr2 f) )
(λ a, (id,id))
(λ a b c d h g f, pair_eq !assoc !assoc )
(λ a b f, prod.equal !id_left !id_left )
(λ a b f, prod.equal !id_right !id_right)
definition Prod_category [reducible] (C D : Category) : Category := Mk (prod_category C D)
end
end product
namespace ops
notation `type`:max := Type_category
notation 1 := Category_one
notation 2 := Category_two
postfix `ᵒᵖ`:max := opposite.Opposite
infixr `×c`:30 := product.Prod_category
attribute type_category [instance]
attribute category_one [instance]
attribute category_two [instance]
attribute product.prod_category [instance]
end ops
open ops
namespace opposite
section
open functor
definition opposite_functor [reducible] {C D : Category} (F : C ⇒ D) : Cᵒᵖ ⇒ Dᵒᵖ :=
@functor.mk (Cᵒᵖ) (Dᵒᵖ)
(λ a, F a)
(λ a b f, F f)
(λ a, respect_id F a)
(λ a b c g f, respect_comp F f g)
end
end opposite
namespace product
section
open ops functor
definition prod_functor [reducible] {C C' D D' : Category} (F : C ⇒ D) (G : C' ⇒ D')
: C ×c C' ⇒ D ×c D' :=
functor.mk (λ a, pair (F (pr1 a)) (G (pr2 a)))
(λ a b f, pair (F (pr1 f)) (G (pr2 f)))
(λ a, pair_eq !respect_id !respect_id)
(λ a b c g f, pair_eq !respect_comp !respect_comp)
end
end product
namespace ops
infixr `×f`:30 := product.prod_functor
infixr `ᵒᵖᶠ`:max := opposite.opposite_functor
end ops
section functor_category
variables (C D : Category)
definition functor_category : category (functor C D) :=
mk (λa b, natural_transformation a b)
(λ a b c g f, natural_transformation.compose g f)
(λ a, natural_transformation.id)
(λ a b c d h g f, !natural_transformation.assoc)
(λ a b f, !natural_transformation.id_left)
(λ a b f, !natural_transformation.id_right)
end functor_category
namespace slice
open sigma function
variables {ob : Type} {C : category ob} {c : ob}
protected definition slice_obs (C : category ob) (c : ob) := Σ(b : ob), hom b c
variables {a b : slice_obs C c}
protected definition to_ob (a : slice_obs C c) : ob := sigma.pr1 a
protected definition to_ob_def (a : slice_obs C c) : to_ob a = sigma.pr1 a := rfl
protected definition ob_hom (a : slice_obs C c) : hom (to_ob a) c := sigma.pr2 a
-- protected theorem slice_obs_equal (H₁ : to_ob a = to_ob b)
-- (H₂ : eq.drec_on H₁ (ob_hom a) = ob_hom b) : a = b :=
-- sigma.equal H₁ H₂
protected definition slice_hom (a b : slice_obs C c) : Type :=
Σ(g : hom (to_ob a) (to_ob b)), ob_hom b ∘ g = ob_hom a
protected definition hom_hom (f : slice_hom a b) : hom (to_ob a) (to_ob b) := sigma.pr1 f
protected definition commute (f : slice_hom a b) : ob_hom b ∘ (hom_hom f) = ob_hom a := sigma.pr2 f
-- protected theorem slice_hom_equal (f g : slice_hom a b) (H : hom_hom f = hom_hom g) : f = g :=
-- sigma.equal H !proof_irrel
definition slice_category (C : category ob) (c : ob) : category (slice_obs C c) :=
mk (λa b, slice_hom a b)
(λ a b c g f, sigma.mk (hom_hom g ∘ hom_hom f)
(show ob_hom c ∘ (hom_hom g ∘ hom_hom f) = ob_hom a,
proof
calc
ob_hom c ∘ (hom_hom g ∘ hom_hom f) = (ob_hom c ∘ hom_hom g) ∘ hom_hom f : !assoc
... = ob_hom b ∘ hom_hom f : {commute g}
... = ob_hom a : {commute f}
qed))
(λ a, sigma.mk id !id_right)
(λ a b c d h g f, dpair_eq !assoc !proof_irrel)
(λ a b f, sigma.equal !id_left !proof_irrel)
(λ a b f, sigma.equal !id_right !proof_irrel)
-- We use !proof_irrel instead of rfl, to give the unifier an easier time
-- definition slice_category {ob : Type} (C : category ob) (c : ob) : category (Σ(b : ob), hom b c)
-- :=
-- mk (λa b, Σ(g : hom (dpr1 a) (dpr1 b)), dpr2 b ∘ g = dpr2 a)
-- (λ a b c g f, dpair (dpr1 g ∘ dpr1 f)
-- (show dpr2 c ∘ (dpr1 g ∘ dpr1 f) = dpr2 a,
-- proof
-- calc
-- dpr2 c ∘ (dpr1 g ∘ dpr1 f) = (dpr2 c ∘ dpr1 g) ∘ dpr1 f : !assoc
-- ... = dpr2 b ∘ dpr1 f : {dpr2 g}
-- ... = dpr2 a : {dpr2 f}
-- qed))
-- (λ a, dpair id !id_right)
-- (λ a b c d h g f, dpair_eq !assoc !proof_irrel)
-- (λ a b f, sigma.equal !id_left !proof_irrel)
-- (λ a b f, sigma.equal !id_right !proof_irrel)
-- We use !proof_irrel instead of rfl, to give the unifier an easier time
definition Slice_category [reducible] (C : Category) (c : C) := Mk (slice_category C c)
open category.ops
attribute slice_category [instance]
variables {D : Category}
definition forgetful (x : D) : (Slice_category D x) ⇒ D :=
functor.mk (λ a, to_ob a)
(λ a b f, hom_hom f)
(λ a, rfl)
(λ a b c g f, rfl)
definition postcomposition_functor {x y : D} (h : x ⟶ y)
: Slice_category D x ⇒ Slice_category D y :=
functor.mk
(λ a, sigma.mk (to_ob a) (h ∘ ob_hom a))
(λ a b f,
⟨hom_hom f,
calc
(h ∘ ob_hom b) ∘ hom_hom f = h ∘ (ob_hom b ∘ hom_hom f) : by rewrite assoc
... = h ∘ ob_hom a : by rewrite commute⟩)
(λ a, rfl)
(λ a b c g f, dpair_eq rfl !proof_irrel)
-- -- in the following comment I tried to have (A = B) in the type of a == b, but that doesn't solve the problems
-- definition heq2 {A B : Type} (H : A = B) (a : A) (b : B) := a == b
-- definition heq2.intro {A B : Type} {a : A} {b : B} (H : a == b) : heq2 (heq.type_eq H) a b := H
-- definition heq2.elim {A B : Type} {a : A} {b : B} (H : A = B) (H2 : heq2 H a b) : a == b := H2
-- definition heq2.proof_irrel {A B : Prop} (a : A) (b : B) (H : A = B) : heq2 H a b :=
-- hproof_irrel H a b
-- theorem functor.mk_eq2 {C D : Category} {obF obG : C → D} {homF homG idF idG compF compG}
-- (Hob : ∀x, obF x = obG x)
-- (Hmor : ∀(a b : C) (f : a ⟶ b), heq2 (congr_arg (λ x, x a ⟶ x b) (funext Hob)) (homF a b f) (homG a b f))
-- : functor.mk obF homF idF compF = functor.mk obG homG idG compG :=
-- hddcongr_arg4 functor.mk
-- (funext Hob)
-- (hfunext (λ a, hfunext (λ b, hfunext (λ f, !Hmor))))
-- !proof_irrel
-- !proof_irrel
-- set_option pp.implicit true
-- set_option pp.coercions true
-- definition slice_functor : D ⇒ Category_of_categories :=
-- functor.mk (λ a, Category.mk (slice_obs D a) (slice_category D a))
-- (λ a b f, postcomposition_functor f)
-- (λ a, functor.mk_heq
-- (λx, sigma.equal rfl !id_left)
-- (λb c f, sigma.hequal sorry !heq.refl (hproof_irrel sorry _ _)))
-- (λ a b c g f, functor.mk_heq
-- (λx, sigma.equal (sorry ⬝ refl (dpr1 x)) sorry)
-- (λb c f, sorry))
--the error message generated here is really confusing: the type of the above refl should be
-- "@dpr1 D (λ (a_1 : D), a_1 ⟶ a) x = @dpr1 D (λ (a_1 : D), a_1 ⟶ c) x", but the second dpr1 is not even well-typed
end slice
-- section coslice
-- open sigma
-- definition coslice {ob : Type} (C : category ob) (c : ob) : category (Σ(b : ob), hom c b) :=
-- mk (λa b, Σ(g : hom (dpr1 a) (dpr1 b)), g ∘ dpr2 a = dpr2 b)
-- (λ a b c g f, dpair (dpr1 g ∘ dpr1 f)
-- (show (dpr1 g ∘ dpr1 f) ∘ dpr2 a = dpr2 c,
-- proof
-- calc
-- (dpr1 g ∘ dpr1 f) ∘ dpr2 a = dpr1 g ∘ (dpr1 f ∘ dpr2 a): symm !assoc
-- ... = dpr1 g ∘ dpr2 b : {dpr2 f}
-- ... = dpr2 c : {dpr2 g}
-- qed))
-- (λ a, dpair id !id_left)
-- (λ a b c d h g f, dpair_eq !assoc !proof_irrel)
-- (λ a b f, sigma.equal !id_left !proof_irrel)
-- (λ a b f, sigma.equal !id_right !proof_irrel)
-- -- theorem slice_coslice_opp {ob : Type} (C : category ob) (c : ob) :
-- -- coslice C c = opposite (slice (opposite C) c) :=
-- -- sorry
-- end coslice
section arrow
open sigma eq.ops
-- theorem concat_commutative_squares {ob : Type} {C : category ob} {a1 a2 a3 b1 b2 b3 : ob}
-- {f1 : a1 => b1} {f2 : a2 => b2} {f3 : a3 => b3} {g2 : a2 => a3} {g1 : a1 => a2}
-- {h2 : b2 => b3} {h1 : b1 => b2} (H1 : f2 ∘ g1 = h1 ∘ f1) (H2 : f3 ∘ g2 = h2 ∘ f2)
-- : f3 ∘ (g2 ∘ g1) = (h2 ∘ h1) ∘ f1 :=
-- calc
-- f3 ∘ (g2 ∘ g1) = (f3 ∘ g2) ∘ g1 : assoc
-- ... = (h2 ∘ f2) ∘ g1 : {H2}
-- ... = h2 ∘ (f2 ∘ g1) : symm assoc
-- ... = h2 ∘ (h1 ∘ f1) : {H1}
-- ... = (h2 ∘ h1) ∘ f1 : assoc
-- definition arrow {ob : Type} (C : category ob) : category (Σ(a b : ob), hom a b) :=
-- mk (λa b, Σ(g : hom (dpr1 a) (dpr1 b)) (h : hom (dpr2' a) (dpr2' b)),
-- dpr3 b ∘ g = h ∘ dpr3 a)
-- (λ a b c g f, dpair (dpr1 g ∘ dpr1 f) (dpair (dpr2' g ∘ dpr2' f) (concat_commutative_squares (dpr3 f) (dpr3 g))))
-- (λ a, dpair id (dpair id (id_right ⬝ (symm id_left))))
-- (λ a b c d h g f, dtrip_eq2 assoc assoc !proof_irrel)
-- (λ a b f, trip.equal2 id_left id_left !proof_irrel)
-- (λ a b f, trip.equal2 id_right id_right !proof_irrel)
-- make these definitions private?
variables {ob : Type} {C : category ob}
protected definition arrow_obs (ob : Type) (C : category ob) := Σ(a b : ob), hom a b
variables {a b : arrow_obs ob C}
protected definition src (a : arrow_obs ob C) : ob := sigma.pr1 a
protected definition dst (a : arrow_obs ob C) : ob := sigma.pr2' a
protected definition to_hom (a : arrow_obs ob C) : hom (src a) (dst a) := sigma.pr3 a
protected definition arrow_hom (a b : arrow_obs ob C) : Type :=
Σ (g : hom (src a) (src b)) (h : hom (dst a) (dst b)), to_hom b ∘ g = h ∘ to_hom a
protected definition hom_src (m : arrow_hom a b) : hom (src a) (src b) := sigma.pr1 m
protected definition hom_dst (m : arrow_hom a b) : hom (dst a) (dst b) := sigma.pr2' m
protected definition commute (m : arrow_hom a b) : to_hom b ∘ (hom_src m) = (hom_dst m) ∘ to_hom a
:= sigma.pr3 m
definition arrow (ob : Type) (C : category ob) : category (arrow_obs ob C) :=
mk (λa b, arrow_hom a b)
(λ a b c g f, sigma.mk (hom_src g ∘ hom_src f) (sigma.mk (hom_dst g ∘ hom_dst f)
(show to_hom c ∘ (hom_src g ∘ hom_src f) = (hom_dst g ∘ hom_dst f) ∘ to_hom a,
proof
calc
to_hom c ∘ (hom_src g ∘ hom_src f) = (to_hom c ∘ hom_src g) ∘ hom_src f : by rewrite assoc
... = (hom_dst g ∘ to_hom b) ∘ hom_src f : by rewrite commute
... = hom_dst g ∘ (to_hom b ∘ hom_src f) : by rewrite assoc
... = hom_dst g ∘ (hom_dst f ∘ to_hom a) : by rewrite commute
... = (hom_dst g ∘ hom_dst f) ∘ to_hom a : by rewrite assoc
qed)
))
(λ a, sigma.mk id (sigma.mk id (!id_right ⬝ (symm !id_left))))
(λ a b c d h g f, ndtrip_eq !assoc !assoc !proof_irrel)
(λ a b f, ndtrip_equal !id_left !id_left !proof_irrel)
(λ a b f, ndtrip_equal !id_right !id_right !proof_irrel)
end arrow
end category
-- definition foo : category (sorry) :=
-- mk (λa b, sorry)
-- (λ a b c g f, sorry)
-- (λ a, sorry)
-- (λ a b c d h g f, sorry)
-- (λ a b f, sorry)
-- (λ a b f, sorry)
|
77913659f76914525526aabc1668133c65554636 | bf532e3e865883a676110e756f800e0ddeb465be | /analysis/measure_theory/measurable_space.lean | a41eb00840a781725b2e7d96e2a84141f0831b31 | [
"Apache-2.0"
] | permissive | aqjune/mathlib | da42a97d9e6670d2efaa7d2aa53ed3585dafc289 | f7977ff5a6bcf7e5c54eec908364ceb40dafc795 | refs/heads/master | 1,631,213,225,595 | 1,521,089,840,000 | 1,521,089,840,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,405 | 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
Measurable spaces -- σ-algberas
-/
import data.set data.set.disjointed data.finset order.galois_connection data.set.countable
open classical set lattice
local attribute [instance] prop_decidable
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
{s t u : set α}
structure measurable_space (α : Type u) :=
(is_measurable : set α → Prop)
(is_measurable_empty : is_measurable ∅)
(is_measurable_compl : ∀s, is_measurable s → is_measurable (- s))
(is_measurable_Union : ∀f:ℕ → set α, (∀i, is_measurable (f i)) → is_measurable (⋃i, f i))
attribute [class] measurable_space
section
variable [m : measurable_space α]
include m
/-- `is_measurable s` means that `s` is measurable (in the ambient measure space on `α`) -/
def is_measurable : set α → Prop := m.is_measurable
lemma is_measurable_empty : is_measurable (∅ : set α) := m.is_measurable_empty
lemma is_measurable_compl : is_measurable s → is_measurable (-s) :=
m.is_measurable_compl s
lemma is_measurable_univ : is_measurable (univ : set α) :=
have is_measurable (- ∅ : set α), from is_measurable_compl is_measurable_empty,
by simp * at *
lemma is_measurable_Union_nat {f : ℕ → set α} : (∀i, is_measurable (f i)) → is_measurable (⋃i, f i) :=
m.is_measurable_Union f
lemma is_measurable_sUnion {s : set (set α)} (hs : countable s) (h : ∀t∈s, is_measurable t) :
is_measurable (⋃₀ s) :=
let ⟨f, hf⟩ := countable_iff_exists_surjective.mp hs in
have (⋃₀ s) = (⋃i, if f i ∈ s then f i else ∅),
from le_antisymm
(Sup_le $ assume u hu, let ⟨i, hi⟩ := hf hu in le_supr_of_le i $ by simp [hi, hu, le_refl])
(supr_le $ assume i, by_cases
(assume : f i ∈ s, by simp [this]; exact subset_sUnion_of_mem this)
(assume : f i ∉ s, by simp [this]; exact bot_le)),
begin
rw [this],
apply is_measurable_Union_nat _,
intro i,
by_cases h' : f i ∈ s; simp [h', h, is_measurable_empty]
end
lemma is_measurable_bUnion {f : β → set α} {s : set β} (hs : countable s)
(h : ∀b∈s, is_measurable (f b)) : is_measurable (⋃b∈s, f b) :=
have ⋃₀ (f '' s) = (⋃a∈s, f a), from lattice.Sup_image,
by rw [←this];
from (is_measurable_sUnion (countable_image hs) $ assume a ⟨s', hs', eq⟩, eq ▸ h s' hs')
lemma is_measurable_Union [encodable β] {f : β → set α} (h : ∀b, is_measurable (f b)) :
is_measurable (⋃b, f b) :=
have is_measurable (⋃b∈(univ : set β), f b),
from is_measurable_bUnion countable_encodable $ assume b _, h b,
by simp [*] at *
lemma is_measurable_sInter {s : set (set α)} (hs : countable s) (h : ∀t∈s, is_measurable t) :
is_measurable (⋂₀ s) :=
by rw [sInter_eq_comp_sUnion_compl, sUnion_image];
from is_measurable_compl (is_measurable_bUnion hs $ assume t ht, is_measurable_compl $ h t ht)
lemma is_measurable_bInter {f : β → set α} {s : set β} (hs : countable s)
(h : ∀b∈s, is_measurable (f b)) : is_measurable (⋂b∈s, f b) :=
have ⋂₀ (f '' s) = (⋂a∈s, f a), from lattice.Inf_image,
by rw [←this];
from (is_measurable_sInter (countable_image hs) $ assume a ⟨s', hs', eq⟩, eq ▸ h s' hs')
lemma is_measurable_Inter [encodable β] {f : β → set α} (h : ∀b, is_measurable (f b)) :
is_measurable (⋂b, f b) :=
by rw Inter_eq_comp_Union_comp;
from is_measurable_compl (is_measurable_Union $ assume b, is_measurable_compl $ h b)
lemma is_measurable_union {s₁ s₂ : set α}
(h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ ∪ s₂) :=
have s₁ ∪ s₂ = (⨆b ∈ ({tt, ff} : set bool), bool.cases_on b s₁ s₂),
by simp [lattice.supr_or, lattice.supr_sup_eq]; refl,
by rw [this]; from is_measurable_bUnion countable_encodable (assume b,
match b with
| tt := by simp [h₂]
| ff := by simp [h₁]
end)
lemma is_measurable_inter {s₁ s₂ : set α}
(h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ ∩ s₂) :=
by rw [inter_eq_compl_compl_union_compl];
from is_measurable_compl (is_measurable_union (is_measurable_compl h₁) (is_measurable_compl h₂))
lemma is_measurable_sdiff {s₁ s₂ : set α}
(h₁ : is_measurable s₁) (h₂ : is_measurable s₂) : is_measurable (s₁ \ s₂) :=
is_measurable_inter h₁ $ is_measurable_compl h₂
lemma is_measurable_sub {s₁ s₂ : set α} :
is_measurable s₁ → is_measurable s₂ → is_measurable (s₁ - s₂) :=
is_measurable_sdiff
lemma is_measurable_disjointed {f : ℕ → set α} {n : ℕ} (h : ∀i, is_measurable (f i)) :
is_measurable (disjointed f n) :=
disjointed_induct (h n) (assume t i ht, is_measurable_sub ht $ h _)
end
lemma measurable_space_eq :
∀{m₁ m₂ : measurable_space α}, (∀s:set α, m₁.is_measurable s ↔ m₂.is_measurable s) → m₁ = m₂
| ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h :=
have s₁ = s₂, from funext $ assume x, propext $ h x,
by subst this
namespace measurable_space
section complete_lattice
instance : partial_order (measurable_space α) :=
{ le := λm₁ m₂, m₁.is_measurable ≤ m₂.is_measurable,
le_refl := assume a b, le_refl _,
le_trans := assume a b c, le_trans,
le_antisymm := assume a b h₁ h₂, measurable_space_eq $ assume s, ⟨h₁ s, h₂ s⟩ }
section generated_from
/-- The smallest σ-algebra containing a collection `s` of basic sets -/
inductive generate_measurable (s : set (set α)) : set α → Prop
| basic : ∀u∈s, generate_measurable u
| empty : generate_measurable ∅
| compl : ∀s, generate_measurable s → generate_measurable (-s)
| union : ∀f:ℕ → set α, (∀n, generate_measurable (f n)) → generate_measurable (⋃i, f i)
/-- Construct the smallest measure space containing a collection of basic sets -/
def generate_from (s : set (set α)) : measurable_space α :=
{ is_measurable := generate_measurable s,
is_measurable_empty := generate_measurable.empty s,
is_measurable_compl := generate_measurable.compl,
is_measurable_Union := generate_measurable.union }
lemma is_measurable_generate_from {s : set (set α)} {t : set α} (ht : t ∈ s) :
(generate_from s).is_measurable t :=
generate_measurable.basic t ht
lemma generate_from_le {s : set (set α)} {m : measurable_space α} (h : ∀t∈s, m.is_measurable t) :
generate_from s ≤ m :=
assume t (ht : generate_measurable s t), ht.rec_on h
(is_measurable_empty m)
(assume s _ hs, is_measurable_compl m s hs)
(assume f _ hf, is_measurable_Union m f hf)
end generated_from
instance : has_top (measurable_space α) :=
⟨{is_measurable := λs, true,
is_measurable_empty := trivial,
is_measurable_compl := assume s hs, trivial,
is_measurable_Union := assume f hf, trivial }⟩
instance : has_bot (measurable_space α) :=
⟨{is_measurable := λs, s = ∅ ∨ s = univ,
is_measurable_empty := or.inl rfl,
is_measurable_compl := by simp [or_imp_distrib] {contextual := tt},
is_measurable_Union := assume f hf, by_cases
(assume h : ∃i, f i = univ,
let ⟨i, hi⟩ := h in
or.inr $ eq_univ_of_univ_subset $ hi ▸ le_supr f i)
(assume h : ¬ ∃i, f i = univ,
or.inl $ eq_empty_of_subset_empty $ Union_subset $ assume i,
(hf i).elim (by simp {contextual := tt}) (assume hi, false.elim $ h ⟨i, hi⟩)) }⟩
instance : has_inf (measurable_space α) :=
⟨λm₁ m₂, {
is_measurable := λs:set α, m₁.is_measurable s ∧ m₂.is_measurable s,
is_measurable_empty := ⟨m₁.is_measurable_empty, m₂.is_measurable_empty⟩,
is_measurable_compl := assume s ⟨h₁, h₂⟩, ⟨m₁.is_measurable_compl s h₁, m₂.is_measurable_compl s h₂⟩,
is_measurable_Union := assume f hf,
⟨m₁.is_measurable_Union f (λi, (hf i).left), m₂.is_measurable_Union f (λi, (hf i).right)⟩ }⟩
instance : has_Inf (measurable_space α) :=
⟨λx, {
is_measurable := λs:set α, ∀m:measurable_space α, m ∈ x → m.is_measurable s,
is_measurable_empty := assume m hm, m.is_measurable_empty,
is_measurable_compl := assume s hs m hm, m.is_measurable_compl s $ hs _ hm,
is_measurable_Union := assume f hf m hm, m.is_measurable_Union f $ assume i, hf _ _ hm }⟩
protected lemma le_Inf {s : set (measurable_space α)} {m : measurable_space α}
(h : ∀m'∈s, m ≤ m') : m ≤ Inf s :=
assume s hs m hm, h m hm s hs
protected lemma Inf_le {s : set (measurable_space α)} {m : measurable_space α}
(h : m ∈ s) : Inf s ≤ m :=
assume s hs, hs m h
instance : complete_lattice (measurable_space α) :=
{ sup := λa b, Inf {x | a ≤ x ∧ b ≤ x},
le_sup_left := assume a b, measurable_space.le_Inf $ assume x, assume h : a ≤ x ∧ b ≤ x, h.left,
le_sup_right := assume a b, measurable_space.le_Inf $ assume x, assume h : a ≤ x ∧ b ≤ x, h.right,
sup_le := assume a b c h₁ h₂,
measurable_space.Inf_le $ show c ∈ {x | a ≤ x ∧ b ≤ x}, from ⟨h₁, h₂⟩,
inf := (⊓),
le_inf := assume a b h h₁ h₂ s hs, ⟨h₁ s hs, h₂ s hs⟩,
inf_le_left := assume a b s ⟨h₁, h₂⟩, h₁,
inf_le_right := assume a b s ⟨h₁, h₂⟩, h₂,
top := ⊤,
le_top := assume a t ht, trivial,
bot := ⊥,
bot_le := assume a s hs, hs.elim
(assume h, h.symm ▸ a.is_measurable_empty)
(assume h, begin rw [h, ←compl_empty], exact a.is_measurable_compl _ a.is_measurable_empty end),
Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t},
le_Sup := assume s f h, measurable_space.le_Inf $ assume t ht, ht _ h,
Sup_le := assume s f h, measurable_space.Inf_le $ assume t ht, h _ ht,
Inf := Inf,
le_Inf := assume s a, measurable_space.le_Inf,
Inf_le := assume s a, measurable_space.Inf_le,
..measurable_space.partial_order }
instance : inhabited (measurable_space α) := ⟨⊤⟩
end complete_lattice
section functors
variables {m m₁ m₂ : measurable_space α} {m' : measurable_space β} {f : α → β} {g : β → α}
/-- The forward image of a measure space under a function. `map f m` contains the sets `s : set β`
whose preimage under `f` is measurable. -/
protected def map (f : α → β) (m : measurable_space α) : measurable_space β :=
{ is_measurable := λs, m.is_measurable $ f ⁻¹' s,
is_measurable_empty := m.is_measurable_empty,
is_measurable_compl := assume s hs, m.is_measurable_compl _ hs,
is_measurable_Union := assume f hf, by rw [preimage_Union]; exact m.is_measurable_Union _ hf }
@[simp] lemma map_id : m.map id = m :=
measurable_space_eq $ assume s, iff.refl _
@[simp] lemma map_comp {f : α → β} {g : β → γ} : (m.map f).map g = m.map (g ∘ f) :=
measurable_space_eq $ assume s, iff.refl _
/-- The reverse image of a measure space under a function. `comap f m` contains the sets `s : set α`
such that `s` is the `f`-preimage of a measurable set in `β`. -/
protected def comap (f : α → β) (m : measurable_space β) : measurable_space α :=
{ is_measurable := λs, ∃s', m.is_measurable s' ∧ f ⁻¹' s' = s,
is_measurable_empty := ⟨∅, m.is_measurable_empty, rfl⟩,
is_measurable_compl := assume s ⟨s', h₁, h₂⟩, ⟨-s', m.is_measurable_compl _ h₁, h₂ ▸ rfl⟩,
is_measurable_Union := assume s hs,
let ⟨s', hs'⟩ := axiom_of_choice hs in
have ∀i, f ⁻¹' s' i = s i, from assume i, (hs' i).right,
⟨⋃i, s' i, m.is_measurable_Union _ (λi, (hs' i).left), by simp [this] ⟩ }
@[simp] lemma comap_id : m.comap id = m :=
measurable_space_eq $ assume s, ⟨assume ⟨s', hs', h⟩, h ▸ hs', assume h, ⟨s, h, rfl⟩⟩
@[simp] lemma comap_comp {f : β → α} {g : γ → β} : (m.comap f).comap g = m.comap (f ∘ g) :=
measurable_space_eq $ assume s,
⟨assume ⟨t, ⟨u, h, hu⟩, ht⟩, ⟨u, h, ht ▸ hu ▸ rfl⟩, assume ⟨t, h, ht⟩, ⟨f ⁻¹' t, ⟨_, h, rfl⟩, ht⟩⟩
lemma comap_le_iff_le_map {f : α → β} : m'.comap f ≤ m ↔ m' ≤ m.map f :=
⟨assume h s hs, h _ ⟨_, hs, rfl⟩, assume h s ⟨t, ht, heq⟩, heq ▸ h _ ht⟩
lemma gc_comap_map (f : α → β) :
galois_connection (measurable_space.comap f) (measurable_space.map f) :=
assume f g, comap_le_iff_le_map
lemma map_mono (h : m₁ ≤ m₂) : m₁.map f ≤ m₂.map f := (gc_comap_map f).monotone_u h
lemma monotone_map : monotone (measurable_space.map f) := assume a b h, map_mono h
lemma comap_mono (h : m₁ ≤ m₂) : m₁.comap g ≤ m₂.comap g := (gc_comap_map g).monotone_l h
lemma monotone_comap : monotone (measurable_space.comap g) := assume a b h, comap_mono h
@[simp] lemma comap_bot : (⊥:measurable_space α).comap g = ⊥ := (gc_comap_map g).l_bot
@[simp] lemma comap_sup : (m₁ ⊔ m₂).comap g = m₁.comap g ⊔ m₂.comap g := (gc_comap_map g).l_sup
@[simp] lemma comap_supr {m : ι → measurable_space α} :(⨆i, m i).comap g = (⨆i, (m i).comap g) :=
(gc_comap_map g).l_supr
@[simp] lemma map_top : (⊤:measurable_space α).map f = ⊤ := (gc_comap_map f).u_top
@[simp] lemma map_inf : (m₁ ⊓ m₂).map f = m₁.map f ⊓ m₂.map f := (gc_comap_map f).u_inf
@[simp] lemma map_infi {m : ι → measurable_space α} : (⨅i, m i).map f = (⨅i, (m i).map f) :=
(gc_comap_map f).u_infi
lemma comap_map_le : (m.map f).comap f ≤ m := (gc_comap_map f).decreasing_l_u _
lemma le_map_comap : m ≤ (m.comap g).map g := (gc_comap_map g).increasing_u_l _
end functors
lemma comap_generate_from {f : α → β} {s : set (set β)} :
(generate_from s).comap f = generate_from (preimage f '' s) :=
le_antisymm
(assume u ⟨v, (hv : generate_measurable s v), eq⟩,
begin
rw [←eq], clear eq,
induction hv,
case generate_measurable.basic : u hu { exact (generate_measurable.basic _ $ ⟨u, hu, rfl⟩) },
case generate_measurable.empty { simp [measurable_space.is_measurable_empty] },
case generate_measurable.compl : u hu ih {
rw [preimage_compl],
exact measurable_space.is_measurable_compl _ _ ih },
case generate_measurable.union : u hu ih {
rw [preimage_Union],
exact measurable_space.is_measurable_Union _ _ ih }
end)
(generate_from_le $ assume t ⟨u, hu, eq⟩, eq ▸ ⟨u, generate_measurable.basic _ hu, rfl⟩)
lemma generate_from_le_generate_from {s t : set (set α)} (h : s ⊆ t) :
generate_from s ≤ generate_from t :=
generate_from_le $ assume s hs, generate_measurable.basic s $ h hs
lemma generate_from_sup_generate_from {s t : set (set α)} :
generate_from s ⊔ generate_from t = generate_from (s ∪ t) :=
le_antisymm
(sup_le (generate_from_le_generate_from $ subset_union_left _ _)
(generate_from_le_generate_from $ subset_union_right _ _))
(generate_from_le $ assume u hu, hu.elim
(assume hu, have is_measurable (generate_from s) u, from generate_measurable.basic _ hu,
@le_sup_left (measurable_space α) _ _ _ _ this)
(assume hu, have is_measurable (generate_from t) u, from generate_measurable.basic _ hu,
@le_sup_right (measurable_space α) _ _ _ _ this))
end measurable_space
section measurable_functions
open measurable_space
/-- A function `f` between measurable spaces is measurable if the preimage of every
measurable set is measurable. -/
def measurable [m₁ : measurable_space α] [m₂ : measurable_space β] (f : α → β) : Prop :=
m₂ ≤ m₁.map f
lemma measurable_id [measurable_space α] : measurable (@id α) := le_refl _
lemma measurable_comp [measurable_space α] [measurable_space β] [measurable_space γ]
{f : α → β} {g : β → γ} (hf : measurable f) (hg : measurable g) : measurable (g ∘ f) :=
le_trans hg $ map_mono hf
lemma measurable_generate_from [measurable_space α] {s : set (set β)} {f : α → β}
(h : ∀t∈s, is_measurable (f ⁻¹' t)) : @measurable _ _ _ (generate_from s) f :=
generate_from_le h
end measurable_functions
section constructions
instance : measurable_space empty := ⊤
instance : measurable_space unit := ⊤
instance : measurable_space bool := ⊤
instance : measurable_space ℕ := ⊤
instance : measurable_space ℤ := ⊤
section subtype
instance {p : α → Prop} [m : measurable_space α] : measurable_space (subtype p) :=
m.comap subtype.val
lemma measurable_subtype_val [measurable_space α] [measurable_space β] {p : β → Prop}
{f : α → subtype p} (hf : measurable f) : measurable (λa:α, (f a).val) :=
measurable_comp hf $ measurable_space.comap_le_iff_le_map.mp $ le_refl _
lemma measurable_subtype_mk [measurable_space α] [measurable_space β] {p : β → Prop}
{f : α → subtype p} (hf : measurable (λa, (f a).val)) : measurable f :=
measurable_space.comap_le_iff_le_map.mpr $ by rw [measurable_space.map_comp]; exact hf
end subtype
section prod
instance [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α × β) :=
m₁.comap prod.fst ⊔ m₂.comap prod.snd
lemma measurable_fst [measurable_space α] [measurable_space β] [measurable_space γ]
{f : α → β × γ} (hf : measurable f) : measurable (λa:α, (f a).1) :=
measurable_comp hf $ measurable_space.comap_le_iff_le_map.mp $ le_sup_left
lemma measurable_snd [measurable_space α] [measurable_space β] [measurable_space γ]
{f : α → β × γ} (hf : measurable f) : measurable (λa:α, (f a).2) :=
measurable_comp hf $ measurable_space.comap_le_iff_le_map.mp $ le_sup_right
lemma measurable_prod [measurable_space α] [measurable_space β] [measurable_space γ]
{f : α → β × γ} (hf₁ : measurable (λa, (f a).1)) (hf₂ : measurable (λa, (f a).2)) :
measurable f :=
sup_le
(by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₁)
(by rw [measurable_space.comap_le_iff_le_map, measurable_space.map_comp]; exact hf₂)
lemma measurable_prod_mk [measurable_space α] [measurable_space β] [measurable_space γ]
{f : α → β} {g : α → γ} (hf : measurable f) (hg : measurable g) : measurable (λa:α, (f a, g a)) :=
measurable_prod hf hg
lemma is_measurable_set_prod [measurable_space α] [measurable_space β] {s : set α} {t : set β}
(hs : is_measurable s) (ht : is_measurable t) : is_measurable (set.prod s t) :=
is_measurable_inter (measurable_fst measurable_id _ hs) (measurable_snd measurable_id _ ht)
end prod
instance [m₁ : measurable_space α] [m₂ : measurable_space β] : measurable_space (α ⊕ β) :=
m₁.map sum.inl ⊓ m₂.map sum.inr
instance {β : α → Type v} [m : Πa, measurable_space (β a)] : measurable_space (sigma β) :=
⨅a, (m a).map (sigma.mk a)
end constructions
namespace measurable_space
/-- Dynkin systems
The main purpose of Dynkin systems is to provide a powerful induction rule for σ-algebras generated
by intersection stable set systems.
-/
structure dynkin_system (α : Type*) :=
(has : set α → Prop)
(has_empty : has ∅)
(has_compl : ∀{a}, has a → has (-a))
(has_Union : ∀{f:ℕ → set α}, pairwise (disjoint on f) → (∀i, has (f i)) → has (⋃i, f i))
namespace dynkin_system
lemma dynkin_system_eq :
∀{d₁ d₂ : dynkin_system α}, (∀s:set α, d₁.has s ↔ d₂.has s) → d₁ = d₂
| ⟨s₁, _, _, _⟩ ⟨s₂, _, _, _⟩ h :=
have s₁ = s₂, from funext $ assume x, propext $ h x,
by subst this
instance : partial_order (dynkin_system α) :=
{ le := λm₁ m₂, m₁.has ≤ m₂.has,
le_refl := assume a b, le_refl _,
le_trans := assume a b c, le_trans,
le_antisymm := assume a b h₁ h₂, dynkin_system_eq $ assume s, ⟨h₁ s, h₂ s⟩ }
def of_measurable_space (m : measurable_space α) : dynkin_system α :=
{ has := m.is_measurable,
has_empty := m.is_measurable_empty,
has_compl := m.is_measurable_compl,
has_Union := assume f _ hf, m.is_measurable_Union f hf }
lemma of_measurable_space_le_of_measurable_space_iff {m₁ m₂ : measurable_space α} :
of_measurable_space m₁ ≤ of_measurable_space m₂ ↔ m₁ ≤ m₂ :=
iff.refl _
/-- The least Dynkin system containing a collection of basic sets. -/
inductive generate_has (s : set (set α)) : set α → Prop
| basic : ∀t∈s, generate_has t
| empty : generate_has ∅
| compl : ∀{a}, generate_has a → generate_has (-a)
| Union : ∀{f:ℕ → set α}, (∀i j, i ≠ j → f i ∩ f j = ∅) →
(∀i, generate_has (f i)) → generate_has (⋃i, f i)
def generate (s : set (set α)) : dynkin_system α :=
{ has := generate_has s,
has_empty := generate_has.empty s,
has_compl := assume a, generate_has.compl,
has_Union := assume f, generate_has.Union }
section internal
parameters {δ : Type*} (d : dynkin_system δ)
lemma has_univ : d.has univ :=
have d.has (- ∅), from d.has_compl d.has_empty,
by simp * at *
lemma has_union {s₁ s₂ : set δ} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₁ ∩ s₂ = ∅) : d.has (s₁ ∪ s₂) :=
let f := [s₁, s₂].inth in
have hf0 : f 0 = s₁, from rfl,
have hf1 : f 1 = s₂, from rfl,
have hf2 : ∀n:ℕ, f n.succ.succ = ∅, from assume n, rfl,
have (∀i j, i ≠ j → f i ∩ f j = ∅),
from assume i, i.two_step_induction
(assume j, j.two_step_induction (by simp) (by simp [hf0, hf1, h]) (by simp [hf2]))
(assume j, j.two_step_induction (by simp [hf0, hf1, h, inter_comm]) (by simp) (by simp [hf2]))
(by simp [hf2]),
have eq : s₁ ∪ s₂ = (⋃i, f i),
from subset.antisymm (union_subset (le_supr f 0) (le_supr f 1)) $
Union_subset $ assume i,
match i with
| 0 := subset_union_left _ _
| 1 := subset_union_right _ _
| nat.succ (nat.succ n) := by simp [hf2]
end,
by rw [eq]; exact d.has_Union this (assume i,
match i with
| 0 := h₁
| 1 := h₂
| nat.succ (nat.succ n) := by simp [d.has_empty, hf2]
end)
lemma has_sdiff {s₁ s₂ : set δ} (h₁ : d.has s₁) (h₂ : d.has s₂) (h : s₂ ⊆ s₁) : d.has (s₁ - s₂) :=
have d.has (- (s₂ ∪ -s₁)),
from d.has_compl $ d.has_union h₂ (d.has_compl h₁) $ eq_empty_iff_forall_not_mem.2 $
assume x ⟨h₁, h₂⟩, h₂ $ h h₁,
have s₁ - s₂ = - (s₂ ∪ -s₁),
by rw [compl_union, compl_compl, inter_comm]; refl,
by rwa [this]
def to_measurable_space (h_inter : ∀s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) :=
{ measurable_space .
is_measurable := d.has,
is_measurable_empty := d.has_empty,
is_measurable_compl := assume s h, d.has_compl h,
is_measurable_Union := assume f hf,
have h_diff : ∀{s₁ s₂}, d.has s₁ → d.has s₂ → d.has (s₁ - s₂),
from assume s₁ s₂ h₁ h₂, h_inter _ _ h₁ (d.has_compl h₂),
have ∀n, d.has (disjointed f n),
from assume n, disjointed_induct (hf n) (assume t i h, h_diff h $ hf i),
have d.has (⋃n, disjointed f n), from d.has_Union disjoint_disjointed this,
by rwa [disjointed_Union] at this }
lemma of_measurable_space_to_measurable_space
(h_inter : ∀s₁ s₂, d.has s₁ → d.has s₂ → d.has (s₁ ∩ s₂)) :
of_measurable_space (d.to_measurable_space h_inter) = d :=
dynkin_system_eq $ assume s, iff.refl _
def restrict_on {s : set δ} (h : d.has s) : dynkin_system δ :=
{ has := λt, d.has (t ∩ s),
has_empty := by simp [d.has_empty],
has_compl := assume t hts,
have -t ∩ s = (- (t ∩ s)) \ -s,
from set.ext $ assume x, by by_cases x ∈ s; simp [h],
by rw [this]; from d.has_sdiff (d.has_compl hts) (d.has_compl h)
(compl_subset_compl_iff_subset.mpr $ inter_subset_right _ _),
has_Union := assume f hd hf,
begin
rw [inter_comm, inter_distrib_Union_left],
apply d.has_Union,
exact assume i j h,
have f i ∩ f j = ∅, from hd i j h,
calc s ∩ f i ∩ (s ∩ f j) = s ∩ s ∩ (f i ∩ f j) : by cc
... = ∅ : by rw [this]; simp,
intro i, rw [inter_comm], exact hf i
end }
lemma generate_le {s : set (set δ)} (h : ∀t∈s, d.has t) : generate s ≤ d :=
assume t ht,
ht.rec_on h d.has_empty (assume a _ h, d.has_compl h) (assume f hd _ hf, d.has_Union hd hf)
end internal
lemma generate_inter {s : set (set α)}
(hs : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s) {t₁ t₂ : set α}
(ht₁ : (generate s).has t₁) (ht₂ : (generate s).has t₂) : (generate s).has (t₁ ∩ t₂) :=
have generate s ≤ (generate s).restrict_on ht₂,
from generate_le _ $ assume s₁ hs₁,
have (generate s).has s₁, from generate_has.basic s₁ hs₁,
have generate s ≤ (generate s).restrict_on this,
from generate_le _ $ assume s₂ hs₂,
show (generate s).has (s₂ ∩ s₁),
from by_cases
(assume : s₂ ∩ s₁ = ∅, by rw [this]; exact generate_has.empty _)
(assume : s₂ ∩ s₁ ≠ ∅, generate_has.basic _ (hs _ _ hs₂ hs₁ this)),
have (generate s).has (t₂ ∩ s₁), from this _ ht₂,
show (generate s).has (s₁ ∩ t₂), by rwa [inter_comm],
this _ ht₁
lemma generate_from_eq {s : set (set α)}
(hs : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s) :
generate_from s = (generate s).to_measurable_space (assume t₁ t₂, generate_inter hs) :=
le_antisymm
(generate_from_le $ assume t ht, generate_has.basic t ht)
(of_measurable_space_le_of_measurable_space_iff.mp $
by rw [of_measurable_space_to_measurable_space];
from (generate_le _ $ assume t ht, is_measurable_generate_from ht))
end dynkin_system
lemma induction_on_inter {C : set α → Prop} {s : set (set α)} {m : measurable_space α}
(h_eq : m = generate_from s)
(h_inter : ∀t₁ t₂, t₁ ∈ s → t₂ ∈ s → t₁ ∩ t₂ ≠ ∅ → t₁ ∩ t₂ ∈ s)
(h_empty : C ∅) (h_basic : ∀t∈s, C t) (h_compl : ∀t, m.is_measurable t → C t → C (- t))
(h_union : ∀f:ℕ → set α, (∀i j, i ≠ j → f i ∩ f j = ∅) →
(∀i, m.is_measurable (f i)) → (∀i, C (f i)) → C (⋃i, f i)) :
∀{t}, m.is_measurable t → C t :=
have eq : m.is_measurable = dynkin_system.generate_has s,
by rw [h_eq, dynkin_system.generate_from_eq h_inter]; refl,
assume t ht,
have dynkin_system.generate_has s t, by rwa [eq] at ht,
this.rec_on h_basic h_empty
(assume t ht, h_compl t $ by rw [eq]; exact ht)
(assume f hf ht, h_union f hf $ assume i, by rw [eq]; exact ht _)
end measurable_space
|
ebfb355da4e29407c3757f125bc7e092d4a1c444 | 55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5 | /src/category_theory/opposites.lean | 390ad9bca29a523f951d982502f6e69ddb72c3c2 | [
"Apache-2.0"
] | permissive | dupuisf/mathlib | 62de4ec6544bf3b79086afd27b6529acfaf2c1bb | 8582b06b0a5d06c33ee07d0bdf7c646cae22cf36 | refs/heads/master | 1,669,494,854,016 | 1,595,692,409,000 | 1,595,692,409,000 | 272,046,630 | 0 | 0 | Apache-2.0 | 1,592,066,143,000 | 1,592,066,142,000 | null | UTF-8 | Lean | false | false | 9,666 | lean | /-
Copyright (c) 2017 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Stephen Morgan, Scott Morrison
-/
import category_theory.types
import category_theory.equivalence
import data.opposite
universes v₁ v₂ u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation
namespace category_theory
open opposite
variables {C : Type u₁}
section has_hom
variables [has_hom.{v₁} C]
/-- The hom types of the opposite of a category (or graph).
As with the objects, we'll make this irreducible below.
Use `f.op` and `f.unop` to convert between morphisms of C
and morphisms of Cᵒᵖ.
-/
instance has_hom.opposite : has_hom Cᵒᵖ :=
{ hom := λ X Y, unop Y ⟶ unop X }
def has_hom.hom.op {X Y : C} (f : X ⟶ Y) : op Y ⟶ op X := f
def has_hom.hom.unop {X Y : Cᵒᵖ} (f : X ⟶ Y) : unop Y ⟶ unop X := f
attribute [irreducible] has_hom.opposite
lemma has_hom.hom.op_inj {X Y : C} :
function.injective (has_hom.hom.op : (X ⟶ Y) → (op Y ⟶ op X)) :=
λ _ _ H, congr_arg has_hom.hom.unop H
lemma has_hom.hom.unop_inj {X Y : Cᵒᵖ} :
function.injective (has_hom.hom.unop : (X ⟶ Y) → (unop Y ⟶ unop X)) :=
λ _ _ H, congr_arg has_hom.hom.op H
@[simp] lemma has_hom.hom.unop_op {X Y : C} {f : X ⟶ Y} : f.op.unop = f := rfl
@[simp] lemma has_hom.hom.op_unop {X Y : Cᵒᵖ} {f : X ⟶ Y} : f.unop.op = f := rfl
end has_hom
variables [category.{v₁} C]
instance category.opposite : category.{v₁} Cᵒᵖ :=
{ comp := λ _ _ _ f g, (g.unop ≫ f.unop).op,
id := λ X, (𝟙 (unop X)).op }
@[simp] lemma op_comp {X Y Z : C} {f : X ⟶ Y} {g : Y ⟶ Z} :
(f ≫ g).op = g.op ≫ f.op := rfl
@[simp] lemma op_id {X : C} : (𝟙 X).op = 𝟙 (op X) := rfl
@[simp] lemma unop_comp {X Y Z : Cᵒᵖ} {f : X ⟶ Y} {g : Y ⟶ Z} :
(f ≫ g).unop = g.unop ≫ f.unop := rfl
@[simp] lemma unop_id {X : Cᵒᵖ} : (𝟙 X).unop = 𝟙 (unop X) := rfl
@[simp] lemma unop_id_op {X : C} : (𝟙 (op X)).unop = 𝟙 X := rfl
@[simp] lemma op_id_unop {X : Cᵒᵖ} : (𝟙 (unop X)).op = 𝟙 X := rfl
/-- The functor from the double-opposite of a category to the underlying category. -/
@[simps]
def op_op : (Cᵒᵖ)ᵒᵖ ⥤ C :=
{ obj := λ X, unop (unop X),
map := λ X Y f, f.unop.unop }
/-- The functor from a category to its double-opposite. -/
@[simps]
def unop_unop : C ⥤ Cᵒᵖᵒᵖ :=
{ obj := λ X, op (op X),
map := λ X Y f, f.op.op }
/-- The double opposite category is equivalent to the original. -/
@[simps]
def op_op_equivalence : Cᵒᵖᵒᵖ ≌ C :=
{ functor := op_op,
inverse := unop_unop,
unit_iso := iso.refl (𝟭 Cᵒᵖᵒᵖ),
counit_iso := iso.refl (unop_unop ⋙ op_op) }
def is_iso_of_op {X Y : C} (f : X ⟶ Y) [is_iso f.op] : is_iso f :=
{ inv := (inv (f.op)).unop,
hom_inv_id' := has_hom.hom.op_inj (by simp),
inv_hom_id' := has_hom.hom.op_inj (by simp) }
namespace functor
section
variables {D : Type u₂} [category.{v₂} D]
variables {C D}
protected definition op (F : C ⥤ D) : Cᵒᵖ ⥤ Dᵒᵖ :=
{ obj := λ X, op (F.obj (unop X)),
map := λ X Y f, (F.map f.unop).op }
@[simp] lemma op_obj (F : C ⥤ D) (X : Cᵒᵖ) : (F.op).obj X = op (F.obj (unop X)) := rfl
@[simp] lemma op_map (F : C ⥤ D) {X Y : Cᵒᵖ} (f : X ⟶ Y) : (F.op).map f = (F.map f.unop).op := rfl
protected definition unop (F : Cᵒᵖ ⥤ Dᵒᵖ) : C ⥤ D :=
{ obj := λ X, unop (F.obj (op X)),
map := λ X Y f, (F.map f.op).unop }
@[simp] lemma unop_obj (F : Cᵒᵖ ⥤ Dᵒᵖ) (X : C) : (F.unop).obj X = unop (F.obj (op X)) := rfl
@[simp] lemma unop_map (F : Cᵒᵖ ⥤ Dᵒᵖ) {X Y : C} (f : X ⟶ Y) : (F.unop).map f = (F.map f.op).unop := rfl
variables (C D)
definition op_hom : (C ⥤ D)ᵒᵖ ⥤ (Cᵒᵖ ⥤ Dᵒᵖ) :=
{ obj := λ F, (unop F).op,
map := λ F G α,
{ app := λ X, (α.unop.app (unop X)).op,
naturality' := λ X Y f, has_hom.hom.unop_inj $ eq.symm (α.unop.naturality f.unop) } }
@[simp] lemma op_hom.obj (F : (C ⥤ D)ᵒᵖ) : (op_hom C D).obj F = (unop F).op := rfl
@[simp] lemma op_hom.map_app {F G : (C ⥤ D)ᵒᵖ} (α : F ⟶ G) (X : Cᵒᵖ) :
((op_hom C D).map α).app X = (α.unop.app (unop X)).op := rfl
definition op_inv : (Cᵒᵖ ⥤ Dᵒᵖ) ⥤ (C ⥤ D)ᵒᵖ :=
{ obj := λ F, op F.unop,
map := λ F G α, has_hom.hom.op
{ app := λ X, (α.app (op X)).unop,
naturality' := λ X Y f, has_hom.hom.op_inj $ eq.symm (α.naturality f.op) } }
@[simp] lemma op_inv.obj (F : Cᵒᵖ ⥤ Dᵒᵖ) : (op_inv C D).obj F = op F.unop := rfl
@[simp] lemma op_inv.map_app {F G : Cᵒᵖ ⥤ Dᵒᵖ} (α : F ⟶ G) (X : C) :
(((op_inv C D).map α).unop).app X = (α.app (op X)).unop := rfl
-- TODO show these form an equivalence
variables {C D}
protected definition left_op (F : C ⥤ Dᵒᵖ) : Cᵒᵖ ⥤ D :=
{ obj := λ X, unop (F.obj (unop X)),
map := λ X Y f, (F.map f.unop).unop }
@[simp] lemma left_op_obj (F : C ⥤ Dᵒᵖ) (X : Cᵒᵖ) : (F.left_op).obj X = unop (F.obj (unop X)) := rfl
@[simp] lemma left_op_map (F : C ⥤ Dᵒᵖ) {X Y : Cᵒᵖ} (f : X ⟶ Y) :
(F.left_op).map f = (F.map f.unop).unop :=
rfl
protected definition right_op (F : Cᵒᵖ ⥤ D) : C ⥤ Dᵒᵖ :=
{ obj := λ X, op (F.obj (op X)),
map := λ X Y f, (F.map f.op).op }
@[simp] lemma right_op_obj (F : Cᵒᵖ ⥤ D) (X : C) : (F.right_op).obj X = op (F.obj (op X)) := rfl
@[simp] lemma right_op_map (F : Cᵒᵖ ⥤ D) {X Y : C} (f : X ⟶ Y) :
(F.right_op).map f = (F.map f.op).op :=
rfl
-- TODO show these form an equivalence
instance {F : C ⥤ D} [full F] : full F.op :=
{ preimage := λ X Y f, (F.preimage f.unop).op }
instance {F : C ⥤ D} [faithful F] : faithful F.op :=
{ map_injective' := λ X Y f g h,
has_hom.hom.unop_inj $ by simpa using map_injective F (has_hom.hom.op_inj h) }
/-- If F is faithful then the right_op of F is also faithful. -/
instance right_op_faithful {F : Cᵒᵖ ⥤ D} [faithful F] : faithful F.right_op :=
{ map_injective' := λ X Y f g h, has_hom.hom.op_inj (map_injective F (has_hom.hom.op_inj h)) }
/-- If F is faithful then the left_op of F is also faithful. -/
instance left_op_faithful {F : C ⥤ Dᵒᵖ} [faithful F] : faithful F.left_op :=
{ map_injective' := λ X Y f g h, has_hom.hom.unop_inj (map_injective F (has_hom.hom.unop_inj h)) }
end
end functor
namespace nat_trans
variables {D : Type u₂} [category.{v₂} D]
section
variables {F G : C ⥤ D}
local attribute [semireducible] has_hom.opposite
@[simps] protected definition op (α : F ⟶ G) : G.op ⟶ F.op :=
{ app := λ X, (α.app (unop X)).op,
naturality' := begin tidy, erw α.naturality, refl, end }
@[simp] lemma op_id (F : C ⥤ D) : nat_trans.op (𝟙 F) = 𝟙 (F.op) := rfl
@[simps] protected definition unop (α : F.op ⟶ G.op) : G ⟶ F :=
{ app := λ X, (α.app (op X)).unop,
naturality' :=
begin
intros X Y f,
have := congr_arg has_hom.hom.op (α.naturality f.op),
dsimp at this,
erw this,
refl,
end }
@[simp] lemma unop_id (F : C ⥤ D) : nat_trans.unop (𝟙 F.op) = 𝟙 F := rfl
end
section
variables {F G : C ⥤ Dᵒᵖ}
local attribute [semireducible] has_hom.opposite
protected definition left_op (α : F ⟶ G) : G.left_op ⟶ F.left_op :=
{ app := λ X, (α.app (unop X)).unop,
naturality' := begin tidy, erw α.naturality, refl, end }
@[simp] lemma left_op_app (α : F ⟶ G) (X) :
(nat_trans.left_op α).app X = (α.app (unop X)).unop :=
rfl
protected definition right_op (α : F.left_op ⟶ G.left_op) : G ⟶ F :=
{ app := λ X, (α.app (op X)).op,
naturality' :=
begin
intros X Y f,
have := congr_arg has_hom.hom.op (α.naturality f.op),
dsimp at this,
erw this
end }
@[simp] lemma right_op_app (α : F.left_op ⟶ G.left_op) (X) :
(nat_trans.right_op α).app X = (α.app (op X)).op :=
rfl
end
end nat_trans
namespace iso
variables {X Y : C}
protected definition op (α : X ≅ Y) : op Y ≅ op X :=
{ hom := α.hom.op,
inv := α.inv.op,
hom_inv_id' := has_hom.hom.unop_inj α.inv_hom_id,
inv_hom_id' := has_hom.hom.unop_inj α.hom_inv_id }
@[simp] lemma op_hom {α : X ≅ Y} : α.op.hom = α.hom.op := rfl
@[simp] lemma op_inv {α : X ≅ Y} : α.op.inv = α.inv.op := rfl
end iso
namespace nat_iso
variables {D : Type u₂} [category.{v₂} D]
variables {F G : C ⥤ D}
/-- The natural isomorphism between opposite functors `G.op ≅ F.op` induced by a natural
isomorphism between the original functors `F ≅ G`. -/
protected definition op (α : F ≅ G) : G.op ≅ F.op :=
{ hom := nat_trans.op α.hom,
inv := nat_trans.op α.inv,
hom_inv_id' := begin ext, dsimp, rw ←op_comp, rw α.inv_hom_id_app, refl, end,
inv_hom_id' := begin ext, dsimp, rw ←op_comp, rw α.hom_inv_id_app, refl, end }
@[simp] lemma op_hom (α : F ≅ G) : (nat_iso.op α).hom = nat_trans.op α.hom := rfl
@[simp] lemma op_inv (α : F ≅ G) : (nat_iso.op α).inv = nat_trans.op α.inv := rfl
/-- The natural isomorphism between functors `G ≅ F` induced by a natural isomorphism
between the opposite functors `F.op ≅ G.op`. -/
protected definition unop (α : F.op ≅ G.op) : G ≅ F :=
{ hom := nat_trans.unop α.hom,
inv := nat_trans.unop α.inv,
hom_inv_id' := begin ext, dsimp, rw ←unop_comp, rw α.inv_hom_id_app, refl, end,
inv_hom_id' := begin ext, dsimp, rw ←unop_comp, rw α.hom_inv_id_app, refl, end }
@[simp] lemma unop_hom (α : F.op ≅ G.op) : (nat_iso.unop α).hom = nat_trans.unop α.hom := rfl
@[simp] lemma unop_inv (α : F.op ≅ G.op) : (nat_iso.unop α).inv = nat_trans.unop α.inv := rfl
end nat_iso
end category_theory
|
ce516e13f0b6273c72869f09c3e2149662a75038 | f09e92753b1d3d2eb3ce2cfb5288a7f5d1d4bd89 | /src/valuation_spectrum.lean | 844f5a971d47577aa9a0b3938d4f0dc27153a5c5 | [
"Apache-2.0"
] | permissive | PatrickMassot/lean-perfectoid-spaces | 7f63c581db26461b5a92d968e7563247e96a5597 | 5f70b2020b3c6d508431192b18457fa988afa50d | refs/heads/master | 1,625,797,721,782 | 1,547,308,357,000 | 1,547,309,364,000 | 136,658,414 | 0 | 1 | Apache-2.0 | 1,528,486,100,000 | 1,528,486,100,000 | null | UTF-8 | Lean | false | false | 4,206 | lean | import valuations
import analysis.topology.topological_space
import data.finsupp
import group_theory.quotient_group
universes u₁ u₂ u₃
local attribute [instance] classical.prop_decidable
variables {R : Type u₁} [comm_ring R] [decidable_eq R]
structure Valuation (R : Type u₁) [comm_ring R] :=
(Γ : Type u₁)
(grp : linear_ordered_comm_group Γ)
(val : @valuation R _ Γ grp)
namespace Valuation
open valuation
instance : has_coe_to_fun (Valuation R) :=
{ F := λ v, R → with_zero v.Γ, coe := λ v, v.val.val }
instance linear_ordered_value_group {v : Valuation R} : linear_ordered_comm_group v.Γ := v.grp
def of_valuation {Γ : Type u₂} [linear_ordered_comm_group Γ] (v : valuation R Γ) : Valuation R :=
{ Γ := (minimal_value_group v).Γ,
grp := minimal_value_group.linear_ordered_comm_group v,
val := v.minimal_valuation }
section
variables (R)
instance : setoid (Valuation R) :=
{ r := λ v₁ v₂, ∀ r s, v₁ r ≤ v₁ s ↔ v₂ r ≤ v₂ s,
iseqv := begin
split,
{ intros v r s, refl },
split,
{ intros v₁ v₂ h r s, symmetry, exact h r s },
{ intros v₁ v₂ v₃ h₁ h₂ r s,
exact iff.trans (h₁ r s) (h₂ r s) }
end }
end
lemma ne_zero_of_equiv_ne_zero {Γ₁ : Type u₂} [linear_ordered_comm_group Γ₁] {Γ₂ : Type u₃} [linear_ordered_comm_group Γ₂]
{v₁ : valuation R Γ₁} {v₂ : valuation R Γ₂} {r : R} (heq : valuation.is_equiv v₁ v₂) (H : v₁ r ≠ 0) : v₂ r ≠ 0 :=
begin
intro h,
rw [eq_zero_iff_le_zero, ← heq r 0, ← eq_zero_iff_le_zero] at h,
exact H h
end
end Valuation
section
variables (R)
definition Spv := {ineq : R → R → Prop // ∃ (v : Valuation R), ∀ r s : R, v r ≤ v s ↔ ineq r s}
end
namespace Spv
open valuation
definition mk (v : Valuation R) : Spv R := ⟨λ r s, v r ≤ v s, ⟨v, λ _ _, iff.rfl⟩⟩
definition mk' {Γ : Type u₂} [linear_ordered_comm_group Γ] (v : valuation R Γ) : Spv R := mk (Valuation.of_valuation v)
noncomputable definition out (v : Spv R) : Valuation R :=
subtype.cases_on v (λ ineq hv, classical.some hv)
noncomputable definition lift {β : Type u₃}
(f : Valuation R → β) (H : ∀ v₁ v₂ : Valuation R, v₁ ≈ v₂ → f v₁ = f v₂) : Spv R → β :=
f ∘ out
lemma out_mk {v : Valuation R} : out (mk v) ≈ v := classical.some_spec (mk v).property
@[simp] lemma mk_out {v : Spv R} : mk (out v) = v :=
begin
cases v with ineq hv,
rw subtype.ext,
ext,
exact classical.some_spec hv _ _
end
lemma lift_mk {β : Type u₃} {f : Valuation R → β} {H : ∀ v₁ v₂ : Valuation R, v₁ ≈ v₂ → f v₁ = f v₂} (v : Valuation R) :
lift f H (mk v) = f v := H _ _ out_mk
lemma exists_rep (v : Spv R) : ∃ v' : Valuation R, mk v' = v := ⟨out v, mk_out⟩
lemma ind {f : Spv R → Prop} (H : ∀ v, f (mk v)) : ∀ v, f v :=
λ v, by simpa using H (out v)
lemma sound {v₁ v₂ : Valuation R} (heq : v₁ ≈ v₂) : mk v₁ = mk v₂ :=
begin
rw subtype.ext,
funext,
ext,
exact heq _ _
end
noncomputable instance : has_coe (Spv R) (Valuation R) := ⟨out⟩
end Spv
-- TODO:
-- Also might need a variant of Wedhorn 1.27 (ii) -/
/-
theorem equiv_value_group_map (R : Type) [comm_ring R] (v w : valuations R) (H : v ≈ w) :
∃ φ : value_group v.f → value_group w.f, is_group_hom φ ∧ function.bijective φ :=
begin
existsi _,tactic.swap,
{ intro g,
cases g with g Hg,
unfold value_group at Hg,
unfold group.closure at Hg,
dsimp at Hg,
induction Hg,
},
{sorry
}
end
-/
namespace Spv
variables {A : Type u₁} [comm_ring A] [decidable_eq A]
definition basic_open (r s : A) : set (Spv A) :=
{v | v r ≤ v s ∧ v s ≠ 0}
lemma mk_mem_basic_open {r s : A} (v : Valuation A) : mk v ∈ basic_open r s ↔ v r ≤ v s ∧ v s ≠ 0 :=
begin
split; intro h; split,
{ exact (out_mk r s).mp h.left },
{ exact Valuation.ne_zero_of_equiv_ne_zero out_mk h.right },
{ exact (out_mk r s).mpr h.left },
{ exact Valuation.ne_zero_of_equiv_ne_zero (setoid.symm out_mk) h.right }
end
instance : topological_space (Spv A) :=
topological_space.generate_from {U : set (Spv A) | ∃ r s : A, U = basic_open r s}
end Spv |
86ee206a5595b9d864c8a20aaf4512663a7f3ba9 | 4727251e0cd73359b15b664c3170e5d754078599 | /src/topology/metric_space/contracting.lean | 8daeb4f8b0b6ea97e949bd4d2f7a7460a27c4b42 | [
"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 | 15,825 | lean | /-
Copyright (c) 2019 Rohan Mitta. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Rohan Mitta, Kevin Buzzard, Alistair Tucker, Johannes Hölzl, Yury Kudryashov
-/
import analysis.specific_limits.basic
import data.setoid.basic
import dynamics.fixed_points.topology
/-!
# Contracting maps
A Lipschitz continuous self-map with Lipschitz constant `K < 1` is called a *contracting map*.
In this file we prove the Banach fixed point theorem, some explicit estimates on the rate
of convergence, and some properties of the map sending a contracting map to its fixed point.
## Main definitions
* `contracting_with K f` : a Lipschitz continuous self-map with `K < 1`;
* `efixed_point` : given a contracting map `f` on a complete emetric space and a point `x`
such that `edist x (f x) ≠ ∞`, `efixed_point f hf x hx` is the unique fixed point of `f`
in `emetric.ball x ∞`;
* `fixed_point` : the unique fixed point of a contracting map on a complete nonempty metric space.
## Tags
contracting map, fixed point, Banach fixed point theorem
-/
open_locale nnreal topological_space classical ennreal
open filter function
variables {α : Type*}
/-- A map is said to be `contracting_with K`, if `K < 1` and `f` is `lipschitz_with K`. -/
def contracting_with [emetric_space α] (K : ℝ≥0) (f : α → α) :=
(K < 1) ∧ lipschitz_with K f
namespace contracting_with
variables [emetric_space α] [cs : complete_space α] {K : ℝ≥0} {f : α → α}
open emetric set
lemma to_lipschitz_with (hf : contracting_with K f) : lipschitz_with K f := hf.2
lemma one_sub_K_pos' (hf : contracting_with K f) : (0:ℝ≥0∞) < 1 - K := by simp [hf.1]
lemma one_sub_K_ne_zero (hf : contracting_with K f) : (1:ℝ≥0∞) - K ≠ 0 :=
ne_of_gt hf.one_sub_K_pos'
lemma one_sub_K_ne_top : (1:ℝ≥0∞) - K ≠ ∞ :=
by { norm_cast, exact ennreal.coe_ne_top }
lemma edist_inequality (hf : contracting_with K f) {x y} (h : edist x y ≠ ∞) :
edist x y ≤ (edist x (f x) + edist y (f y)) / (1 - K) :=
suffices edist x y ≤ edist x (f x) + edist y (f y) + K * edist x y,
by rwa [ennreal.le_div_iff_mul_le (or.inl hf.one_sub_K_ne_zero) (or.inl one_sub_K_ne_top),
mul_comm, ennreal.sub_mul (λ _ _, h), one_mul, tsub_le_iff_right],
calc edist x y ≤ edist x (f x) + edist (f x) (f y) + edist (f y) y : edist_triangle4 _ _ _ _
... = edist x (f x) + edist y (f y) + edist (f x) (f y) : by rw [edist_comm y, add_right_comm]
... ≤ edist x (f x) + edist y (f y) + K * edist x y : add_le_add le_rfl (hf.2 _ _)
lemma edist_le_of_fixed_point (hf : contracting_with K f) {x y}
(h : edist x y ≠ ∞) (hy : is_fixed_pt f y) :
edist x y ≤ (edist x (f x)) / (1 - K) :=
by simpa only [hy.eq, edist_self, add_zero] using hf.edist_inequality h
lemma eq_or_edist_eq_top_of_fixed_points (hf : contracting_with K f) {x y}
(hx : is_fixed_pt f x) (hy : is_fixed_pt f y) :
x = y ∨ edist x y = ∞ :=
begin
refine or_iff_not_imp_right.2 (λ h, edist_le_zero.1 _),
simpa only [hx.eq, edist_self, add_zero, ennreal.zero_div]
using hf.edist_le_of_fixed_point h hy
end
/-- If a map `f` is `contracting_with K`, and `s` is a forward-invariant set, then
restriction of `f` to `s` is `contracting_with K` as well. -/
lemma restrict (hf : contracting_with K f) {s : set α} (hs : maps_to f s s) :
contracting_with K (hs.restrict f s s) :=
⟨hf.1, λ x y, hf.2 x y⟩
include cs
/-- Banach fixed-point theorem, contraction mapping theorem, `emetric_space` version.
A contracting map on a complete metric space has a fixed point.
We include more conclusions in this theorem to avoid proving them again later.
The main API for this theorem are the functions `efixed_point` and `fixed_point`,
and lemmas about these functions. -/
theorem exists_fixed_point (hf : contracting_with K f) (x : α) (hx : edist x (f x) ≠ ∞) :
∃ y, is_fixed_pt f y ∧ tendsto (λ n, f^[n] x) at_top (𝓝 y) ∧
∀ n:ℕ, edist (f^[n] x) y ≤ (edist x (f x)) * K^n / (1 - K) :=
have cauchy_seq (λ n, f^[n] x),
from cauchy_seq_of_edist_le_geometric K (edist x (f x)) (ennreal.coe_lt_one_iff.2 hf.1)
hx (hf.to_lipschitz_with.edist_iterate_succ_le_geometric x),
let ⟨y, hy⟩ := cauchy_seq_tendsto_of_complete this in
⟨y, is_fixed_pt_of_tendsto_iterate hy hf.2.continuous.continuous_at, hy,
edist_le_of_edist_le_geometric_of_tendsto K (edist x (f x))
(hf.to_lipschitz_with.edist_iterate_succ_le_geometric x) hy⟩
variable (f) -- avoid `efixed_point _` in pretty printer
/-- Let `x` be a point of a complete emetric space. Suppose that `f` is a contracting map,
and `edist x (f x) ≠ ∞`. Then `efixed_point` is the unique fixed point of `f`
in `emetric.ball x ∞`. -/
noncomputable def efixed_point (hf : contracting_with K f) (x : α) (hx : edist x (f x) ≠ ∞) :
α :=
classical.some $ hf.exists_fixed_point x hx
variables {f}
lemma efixed_point_is_fixed_pt (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) :
is_fixed_pt f (efixed_point f hf x hx) :=
(classical.some_spec $ hf.exists_fixed_point x hx).1
lemma tendsto_iterate_efixed_point (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) :
tendsto (λn, f^[n] x) at_top (𝓝 $ efixed_point f hf x hx) :=
(classical.some_spec $ hf.exists_fixed_point x hx).2.1
lemma apriori_edist_iterate_efixed_point_le (hf : contracting_with K f)
{x : α} (hx : edist x (f x) ≠ ∞) (n : ℕ) :
edist (f^[n] x) (efixed_point f hf x hx) ≤ (edist x (f x)) * K^n / (1 - K) :=
(classical.some_spec $ hf.exists_fixed_point x hx).2.2 n
lemma edist_efixed_point_le (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) :
edist x (efixed_point f hf x hx) ≤ (edist x (f x)) / (1 - K) :=
by { convert hf.apriori_edist_iterate_efixed_point_le hx 0, simp only [pow_zero, mul_one] }
lemma edist_efixed_point_lt_top (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞) :
edist x (efixed_point f hf x hx) < ∞ :=
(hf.edist_efixed_point_le hx).trans_lt (ennreal.mul_lt_top hx $
ennreal.inv_ne_top.2 hf.one_sub_K_ne_zero)
lemma efixed_point_eq_of_edist_lt_top (hf : contracting_with K f) {x : α} (hx : edist x (f x) ≠ ∞)
{y : α} (hy : edist y (f y) ≠ ∞) (h : edist x y ≠ ∞) :
efixed_point f hf x hx = efixed_point f hf y hy :=
begin
refine (hf.eq_or_edist_eq_top_of_fixed_points _ _).elim id (λ h', false.elim (ne_of_lt _ h'));
try { apply efixed_point_is_fixed_pt },
change edist_lt_top_setoid.rel _ _,
transitivity x, by { symmetry, exact hf.edist_efixed_point_lt_top hx },
transitivity y,
exacts [lt_top_iff_ne_top.2 h, hf.edist_efixed_point_lt_top hy]
end
omit cs
/-- Banach fixed-point theorem for maps contracting on a complete subset. -/
theorem exists_fixed_point' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s)
(hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) :
∃ y ∈ s, is_fixed_pt f y ∧ tendsto (λ n, f^[n] x) at_top (𝓝 y) ∧
∀ n:ℕ, edist (f^[n] x) y ≤ (edist x (f x)) * K^n / (1 - K) :=
begin
haveI := hsc.complete_space_coe,
rcases hf.exists_fixed_point ⟨x, hxs⟩ hx with ⟨y, hfy, h_tendsto, hle⟩,
refine ⟨y, y.2, subtype.ext_iff_val.1 hfy, _, λ n, _⟩,
{ convert (continuous_subtype_coe.tendsto _).comp h_tendsto, ext n,
simp only [(∘), maps_to.iterate_restrict, maps_to.coe_restrict_apply, subtype.coe_mk] },
{ convert hle n,
rw [maps_to.iterate_restrict, eq_comm, maps_to.coe_restrict_apply, subtype.coe_mk] }
end
variable (f) -- avoid `efixed_point _` in pretty printer
/-- Let `s` be a complete forward-invariant set of a self-map `f`. If `f` contracts on `s`
and `x ∈ s` satisfies `edist x (f x) ≠ ∞`, then `efixed_point'` is the unique fixed point
of the restriction of `f` to `s ∩ emetric.ball x ∞`. -/
noncomputable def efixed_point' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s)
(hf : contracting_with K $ hsf.restrict f s s) (x : α) (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) :
α :=
classical.some $ hf.exists_fixed_point' hsc hsf hxs hx
variables {f}
lemma efixed_point_mem' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s)
(hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) :
efixed_point' f hsc hsf hf x hxs hx ∈ s :=
(classical.some_spec $ hf.exists_fixed_point' hsc hsf hxs hx).fst
lemma efixed_point_is_fixed_pt' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s)
(hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) :
is_fixed_pt f (efixed_point' f hsc hsf hf x hxs hx) :=
(classical.some_spec $ hf.exists_fixed_point' hsc hsf hxs hx).snd.1
lemma tendsto_iterate_efixed_point' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s)
(hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) :
tendsto (λn, f^[n] x) at_top (𝓝 $ efixed_point' f hsc hsf hf x hxs hx) :=
(classical.some_spec $ hf.exists_fixed_point' hsc hsf hxs hx).snd.2.1
lemma apriori_edist_iterate_efixed_point_le' {s : set α} (hsc : is_complete s)
(hsf : maps_to f s s) (hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s)
(hx : edist x (f x) ≠ ∞) (n : ℕ) :
edist (f^[n] x) (efixed_point' f hsc hsf hf x hxs hx) ≤ (edist x (f x)) * K^n / (1 - K) :=
(classical.some_spec $ hf.exists_fixed_point' hsc hsf hxs hx).snd.2.2 n
lemma edist_efixed_point_le' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s)
(hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) :
edist x (efixed_point' f hsc hsf hf x hxs hx) ≤ (edist x (f x)) / (1 - K) :=
by { convert hf.apriori_edist_iterate_efixed_point_le' hsc hsf hxs hx 0,
rw [pow_zero, mul_one] }
lemma edist_efixed_point_lt_top' {s : set α} (hsc : is_complete s) (hsf : maps_to f s s)
(hf : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞) :
edist x (efixed_point' f hsc hsf hf x hxs hx) < ∞ :=
(hf.edist_efixed_point_le' hsc hsf hxs hx).trans_lt (ennreal.mul_lt_top hx $
ennreal.inv_ne_top.2 hf.one_sub_K_ne_zero)
/-- If a globally contracting map `f` has two complete forward-invariant sets `s`, `t`,
and `x ∈ s` is at a finite distance from `y ∈ t`, then the `efixed_point'` constructed by `x`
is the same as the `efixed_point'` constructed by `y`.
This lemma takes additional arguments stating that `f` contracts on `s` and `t` because this way
it can be used to prove the desired equality with non-trivial proofs of these facts. -/
lemma efixed_point_eq_of_edist_lt_top' (hf : contracting_with K f)
{s : set α} (hsc : is_complete s) (hsf : maps_to f s s)
(hfs : contracting_with K $ hsf.restrict f s s) {x : α} (hxs : x ∈ s) (hx : edist x (f x) ≠ ∞)
{t : set α} (htc : is_complete t) (htf : maps_to f t t)
(hft : contracting_with K $ htf.restrict f t t) {y : α} (hyt : y ∈ t) (hy : edist y (f y) ≠ ∞)
(hxy : edist x y ≠ ∞) :
efixed_point' f hsc hsf hfs x hxs hx = efixed_point' f htc htf hft y hyt hy :=
begin
refine (hf.eq_or_edist_eq_top_of_fixed_points _ _).elim id (λ h', false.elim (ne_of_lt _ h'));
try { apply efixed_point_is_fixed_pt' },
change edist_lt_top_setoid.rel _ _,
transitivity x, by { symmetry, apply edist_efixed_point_lt_top' },
transitivity y,
exact lt_top_iff_ne_top.2 hxy,
apply edist_efixed_point_lt_top'
end
end contracting_with
namespace contracting_with
variables [metric_space α] {K : ℝ≥0} {f : α → α} (hf : contracting_with K f)
include hf
lemma one_sub_K_pos (hf : contracting_with K f) : (0:ℝ) < 1 - K := sub_pos.2 hf.1
lemma dist_le_mul (x y : α) : dist (f x) (f y) ≤ K * dist x y :=
hf.to_lipschitz_with.dist_le_mul x y
lemma dist_inequality (x y) : dist x y ≤ (dist x (f x) + dist y (f y)) / (1 - K) :=
suffices dist x y ≤ dist x (f x) + dist y (f y) + K * dist x y,
by rwa [le_div_iff hf.one_sub_K_pos, mul_comm, sub_mul, one_mul, sub_le_iff_le_add],
calc dist x y ≤ dist x (f x) + dist y (f y) + dist (f x) (f y) : dist_triangle4_right _ _ _ _
... ≤ dist x (f x) + dist y (f y) + K * dist x y :
add_le_add_left (hf.dist_le_mul _ _) _
lemma dist_le_of_fixed_point (x) {y} (hy : is_fixed_pt f y) :
dist x y ≤ (dist x (f x)) / (1 - K) :=
by simpa only [hy.eq, dist_self, add_zero] using hf.dist_inequality x y
theorem fixed_point_unique' {x y} (hx : is_fixed_pt f x) (hy : is_fixed_pt f y) : x = y :=
(hf.eq_or_edist_eq_top_of_fixed_points hx hy).resolve_right (edist_ne_top _ _)
/-- Let `f` be a contracting map with constant `K`; let `g` be another map uniformly
`C`-close to `f`. If `x` and `y` are their fixed points, then `dist x y ≤ C / (1 - K)`. -/
lemma dist_fixed_point_fixed_point_of_dist_le' (g : α → α)
{x y} (hx : is_fixed_pt f x) (hy : is_fixed_pt g y) {C} (hfg : ∀ z, dist (f z) (g z) ≤ C) :
dist x y ≤ C / (1 - K) :=
calc dist x y = dist y x : dist_comm x y
... ≤ (dist y (f y)) / (1 - K) : hf.dist_le_of_fixed_point y hx
... = (dist (f y) (g y)) / (1 - K) : by rw [hy.eq, dist_comm]
... ≤ C / (1 - K) : (div_le_div_right hf.one_sub_K_pos).2 (hfg y)
noncomputable theory
variables [nonempty α] [complete_space α]
variable (f)
/-- The unique fixed point of a contracting map in a nonempty complete metric space. -/
def fixed_point : α :=
efixed_point f hf _ (edist_ne_top (classical.choice ‹nonempty α›) _)
variable {f}
/-- The point provided by `contracting_with.fixed_point` is actually a fixed point. -/
lemma fixed_point_is_fixed_pt : is_fixed_pt f (fixed_point f hf) :=
hf.efixed_point_is_fixed_pt _
lemma fixed_point_unique {x} (hx : is_fixed_pt f x) : x = fixed_point f hf :=
hf.fixed_point_unique' hx hf.fixed_point_is_fixed_pt
lemma dist_fixed_point_le (x) : dist x (fixed_point f hf) ≤ (dist x (f x)) / (1 - K) :=
hf.dist_le_of_fixed_point x hf.fixed_point_is_fixed_pt
/-- Aposteriori estimates on the convergence of iterates to the fixed point. -/
lemma aposteriori_dist_iterate_fixed_point_le (x n) :
dist (f^[n] x) (fixed_point f hf) ≤ (dist (f^[n] x) (f^[n+1] x)) / (1 - K) :=
by { rw [iterate_succ'], apply hf.dist_fixed_point_le }
lemma apriori_dist_iterate_fixed_point_le (x n) :
dist (f^[n] x) (fixed_point f hf) ≤ (dist x (f x)) * K^n / (1 - K) :=
le_trans (hf.aposteriori_dist_iterate_fixed_point_le x n) $
(div_le_div_right hf.one_sub_K_pos).2 $
hf.to_lipschitz_with.dist_iterate_succ_le_geometric x n
lemma tendsto_iterate_fixed_point (x) :
tendsto (λn, f^[n] x) at_top (𝓝 $ fixed_point f hf) :=
begin
convert tendsto_iterate_efixed_point hf (edist_ne_top x _),
refine (fixed_point_unique _ _).symm,
apply efixed_point_is_fixed_pt
end
lemma fixed_point_lipschitz_in_map {g : α → α} (hg : contracting_with K g)
{C} (hfg : ∀ z, dist (f z) (g z) ≤ C) :
dist (fixed_point f hf) (fixed_point g hg) ≤ C / (1 - K) :=
hf.dist_fixed_point_fixed_point_of_dist_le' g hf.fixed_point_is_fixed_pt
hg.fixed_point_is_fixed_pt hfg
omit hf
/-- If a map `f` has a contracting iterate `f^[n]`, then the fixed point of `f^[n]` is also a fixed
point of `f`. -/
lemma is_fixed_pt_fixed_point_iterate {n : ℕ} (hf : contracting_with K (f^[n])) :
is_fixed_pt f (hf.fixed_point (f^[n])) :=
begin
set x := hf.fixed_point (f^[n]),
have hx : (f^[n] x) = x := hf.fixed_point_is_fixed_pt,
have := hf.to_lipschitz_with.dist_le_mul x (f x),
rw [← iterate_succ_apply, iterate_succ_apply', hx] at this,
contrapose! this,
have := dist_pos.2 (ne.symm this),
simpa only [nnreal.coe_one, one_mul, nnreal.val_eq_coe] using (mul_lt_mul_right this).mpr hf.left
end
end contracting_with
|
69935347e168d5e48f15e77f2ec726cdffb01a68 | fe25de614feb5587799621c41487aaee0d083b08 | /stage0/src/Lean/Elab/Structure.lean | dbef2064dabf4898f67b004d3da21eda15a5858d | [
"Apache-2.0"
] | permissive | pollend/lean4 | e8469c2f5fb8779b773618c3267883cf21fb9fac | c913886938c4b3b83238a3f99673c6c5a9cec270 | refs/heads/master | 1,687,973,251,481 | 1,628,039,739,000 | 1,628,039,739,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 29,223 | 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.Parser.Command
import Lean.Meta.Closure
import Lean.Meta.SizeOf
import Lean.Meta.Injective
import Lean.Elab.Command
import Lean.Elab.DeclModifiers
import Lean.Elab.DeclUtil
import Lean.Elab.Inductive
import Lean.Elab.DeclarationRange
import Lean.Elab.Binders
namespace Lean.Elab.Command
open Meta
/- Recall that the `structure command syntax is
```
leading_parser (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> optional (" := " >> optional structCtor >> structFields)
```
-/
structure StructCtorView where
ref : Syntax
modifiers : Modifiers
inferMod : Bool -- true if `{}` is used in the constructor declaration
name : Name
declName : Name
structure StructFieldView where
ref : Syntax
modifiers : Modifiers
binderInfo : BinderInfo
inferMod : Bool
declName : Name
name : Name
binders : Syntax
type? : Option Syntax
value? : Option Syntax
structure StructView where
ref : Syntax
modifiers : Modifiers
scopeLevelNames : List Name -- All `universe` declarations in the current scope
allUserLevelNames : List Name -- `scopeLevelNames` ++ explicit universe parameters provided in the `structure` command
isClass : Bool
declName : Name
scopeVars : Array Expr -- All `variable` declaration in the current scope
params : Array Expr -- Explicit parameters provided in the `structure` command
parents : Array Syntax
type : Syntax
ctor : StructCtorView
fields : Array StructFieldView
inductive StructFieldKind where
| newField | fromParent | subobject
deriving Inhabited, BEq
structure StructFieldInfo where
name : Name
declName : Name -- Remark: this field value doesn't matter for fromParent fields.
fvar : Expr
kind : StructFieldKind
inferMod : Bool := false
value? : Option Expr := none
deriving Inhabited
def StructFieldInfo.isFromParent (info : StructFieldInfo) : Bool :=
match info.kind with
| StructFieldKind.fromParent => true
| _ => false
def StructFieldInfo.isSubobject (info : StructFieldInfo) : Bool :=
match info.kind with
| StructFieldKind.subobject => true
| _ => false
/- Auxiliary declaration for `mkProjections` -/
structure ProjectionInfo where
declName : Name
inferMod : Bool
structure ElabStructResult where
decl : Declaration
projInfos : List ProjectionInfo
projInstances : List Name -- projections (to parent classes) that must be marked as instances.
mctx : MetavarContext
lctx : LocalContext
localInsts : LocalInstances
defaultAuxDecls : Array (Name × Expr × Expr)
private def defaultCtorName := `mk
/-
The structure constructor syntax is
```
leading_parser try (declModifiers >> ident >> optional inferMod >> " :: ")
```
-/
private def expandCtor (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : TermElabM StructCtorView := do
let useDefault := do
let declName := structDeclName ++ defaultCtorName
addAuxDeclarationRanges declName structStx[2] structStx[2]
pure { ref := structStx, modifiers := {}, inferMod := false, name := defaultCtorName, declName }
if structStx[5].isNone then
useDefault
else
let optCtor := structStx[5][1]
if optCtor.isNone then
useDefault
else
let ctor := optCtor[0]
withRef ctor do
let ctorModifiers ← elabModifiers ctor[0]
checkValidCtorModifier ctorModifiers
if ctorModifiers.isPrivate && structModifiers.isPrivate then
throwError "invalid 'private' constructor in a 'private' structure"
if ctorModifiers.isProtected && structModifiers.isPrivate then
throwError "invalid 'protected' constructor in a 'private' structure"
let inferMod := !ctor[2].isNone
let name := ctor[1].getId
let declName := structDeclName ++ name
let declName ← applyVisibility ctorModifiers.visibility declName
addDocString' declName ctorModifiers.docString?
addAuxDeclarationRanges declName ctor[1] ctor[1]
pure { ref := ctor, name, modifiers := ctorModifiers, inferMod, declName }
def checkValidFieldModifier (modifiers : Modifiers) : TermElabM Unit := do
if modifiers.isNoncomputable then
throwError "invalid use of 'noncomputable' in field declaration"
if modifiers.isPartial then
throwError "invalid use of 'partial' in field declaration"
if modifiers.isUnsafe then
throwError "invalid use of 'unsafe' in field declaration"
if modifiers.attrs.size != 0 then
throwError "invalid use of attributes in field declaration"
/-
```
def structExplicitBinder := leading_parser atomic (declModifiers true >> "(") >> many1 ident >> optional inferMod >> 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 many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder)
```
-/
private def expandFields (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : TermElabM (Array StructFieldView) :=
let fieldBinders := if structStx[5].isNone then #[] else structStx[5][2][0].getArgs
fieldBinders.foldlM (init := #[]) fun (views : Array StructFieldView) fieldBinder => withRef fieldBinder do
let mut fieldBinder := fieldBinder
if fieldBinder.getKind == ``Parser.Command.structSimpleBinder then
fieldBinder := Syntax.node ``Parser.Command.structExplicitBinder
#[ fieldBinder[0], mkAtomFrom fieldBinder "(", mkNullNode #[ fieldBinder[1] ], fieldBinder[2], fieldBinder[3], fieldBinder[4], mkAtomFrom fieldBinder ")" ]
let k := fieldBinder.getKind
let binfo ←
if k == ``Parser.Command.structExplicitBinder then pure BinderInfo.default
else if k == ``Parser.Command.structImplicitBinder then pure BinderInfo.implicit
else if k == ``Parser.Command.structInstBinder then pure BinderInfo.instImplicit
else throwError "unexpected kind of structure field"
let fieldModifiers ← elabModifiers fieldBinder[0]
checkValidFieldModifier fieldModifiers
if fieldModifiers.isPrivate && structModifiers.isPrivate then
throwError "invalid 'private' field in a 'private' structure"
if fieldModifiers.isProtected && structModifiers.isPrivate then
throwError "invalid 'protected' field in a 'private' structure"
let inferMod := !fieldBinder[3].isNone
let (binders, type?) ←
if binfo == BinderInfo.default then
let (binders, type?) := expandOptDeclSig fieldBinder[4]
let optBinderTacticDefault := fieldBinder[5]
if optBinderTacticDefault.isNone then
pure (binders, type?)
else if optBinderTacticDefault[0].getKind != ``Parser.Term.binderTactic then
pure (binders, type?)
else
let binderTactic := optBinderTacticDefault[0]
match type? with
| none => throwErrorAt binderTactic "invalid field declaration, type must be provided when auto-param (tactic) is used"
| some type =>
let tac := binderTactic[2]
let name ← Term.declareTacticSyntax tac
-- The tactic should be for binders+type.
-- It is safe to reset the binders to a "null" node since there is no value to be elaborated
let type ← `(forall $(binders.getArgs):bracketedBinder*, $type)
let type ← `(autoParam $type $(mkIdentFrom tac name))
pure (mkNullNode, some type)
else
let (binders, type) := expandDeclSig fieldBinder[4]
pure (binders, some type)
let value? ←
if binfo != BinderInfo.default then
pure none
else
let optBinderTacticDefault := fieldBinder[5]
-- trace[Elab.struct] ">>> {optBinderTacticDefault}"
if optBinderTacticDefault.isNone then
pure none
else if optBinderTacticDefault[0].getKind == ``Parser.Term.binderTactic then
pure none
else
-- binderDefault := leading_parser " := " >> termParser
pure (some optBinderTacticDefault[0][1])
let idents := fieldBinder[2].getArgs
idents.foldlM (init := views) fun (views : Array StructFieldView) ident => withRef ident do
let name := ident.getId
let declName := structDeclName ++ name
let declName ← applyVisibility fieldModifiers.visibility declName
addDocString' declName fieldModifiers.docString?
return views.push {
ref := ident
modifiers := fieldModifiers
binderInfo := binfo
inferMod
declName
name
binders
type?
value?
}
private def validStructType (type : Expr) : Bool :=
match type with
| Expr.sort .. => true
| _ => false
private def checkParentIsStructure (parent : Expr) : TermElabM Name :=
match parent.getAppFn with
| Expr.const c _ _ => do
unless isStructure (← getEnv) c do
throwError "'{c}' is not a structure"
pure c
| _ => throwError "expected structure"
private def findFieldInfo? (infos : Array StructFieldInfo) (fieldName : Name) : Option StructFieldInfo :=
infos.find? fun info => info.name == fieldName
private def containsFieldName (infos : Array StructFieldInfo) (fieldName : Name) : Bool :=
(findFieldInfo? infos fieldName).isSome
private partial def processSubfields (structDeclName : Name) (parentFVar : Expr) (parentStructName : Name) (subfieldNames : Array Name)
(infos : Array StructFieldInfo) (k : Array StructFieldInfo → TermElabM α) : TermElabM α :=
let rec loop (i : Nat) (infos : Array StructFieldInfo) := do
if h : i < subfieldNames.size then
let subfieldName := subfieldNames.get ⟨i, h⟩
if containsFieldName infos subfieldName then
throwError "field '{subfieldName}' from '{parentStructName}' has already been declared"
let val ← mkProjection parentFVar subfieldName
let type ← inferType val
withLetDecl subfieldName type val fun subfieldFVar =>
/- The following `declName` is only used for creating the `_default` auxiliary declaration name when
its default value is overwritten in the structure. -/
let declName := structDeclName ++ subfieldName
let infos := infos.push { name := subfieldName, declName, fvar := subfieldFVar, kind := StructFieldKind.fromParent }
loop (i+1) infos
else
k infos
loop 0 infos
private partial def withParents (view : StructView) (i : Nat) (infos : Array StructFieldInfo) (k : Array StructFieldInfo → TermElabM α) : TermElabM α := do
if h : i < view.parents.size then
let parentStx := view.parents.get ⟨i, h⟩
withRef parentStx do
let parent ← Term.elabType parentStx
let parentName ← checkParentIsStructure parent
let toParentName := Name.mkSimple $ "to" ++ parentName.eraseMacroScopes.getString! -- erase macro scopes?
if containsFieldName infos toParentName then
throwErrorAt parentStx "field '{toParentName}' has already been declared"
let env ← getEnv
let binfo := if view.isClass && isClass env parentName then BinderInfo.instImplicit else BinderInfo.default
withLocalDecl toParentName binfo parent fun parentFVar =>
let infos := infos.push { name := toParentName, declName := view.declName ++ toParentName, fvar := parentFVar, kind := StructFieldKind.subobject }
let subfieldNames := getStructureFieldsFlattened env parentName
processSubfields view.declName parentFVar parentName subfieldNames infos fun infos => withParents view (i+1) infos k
else
k infos
private def elabFieldTypeValue (view : StructFieldView) : TermElabM (Option Expr × Option Expr) := do
Term.withAutoBoundImplicit <| Term.elabBinders view.binders.getArgs fun params => do
match view.type? with
| none =>
match view.value? with
| none => return (none, none)
| some valStx =>
Term.synthesizeSyntheticMVarsNoPostponing
let params ← Term.addAutoBoundImplicits params
let value ← Term.elabTerm valStx none
let value ← mkLambdaFVars params value
return (none, value)
| some typeStx =>
let type ← Term.elabType typeStx
Term.synthesizeSyntheticMVarsNoPostponing
let params ← Term.addAutoBoundImplicits params
match view.value? with
| none =>
let type ← mkForallFVars params type
return (type, none)
| some valStx =>
let value ← Term.elabTermEnsuringType valStx type
Term.synthesizeSyntheticMVarsNoPostponing
let type ← mkForallFVars params type
let value ← mkLambdaFVars params value
return (type, value)
private partial def withFields
(views : Array StructFieldView) (i : Nat) (infos : Array StructFieldInfo) (k : Array StructFieldInfo → TermElabM α) : TermElabM α := do
if h : i < views.size then
let view := views.get ⟨i, h⟩
withRef view.ref $
match findFieldInfo? infos view.name with
| none => do
let (type?, value?) ← elabFieldTypeValue view
match type?, value? with
| none, none => throwError "invalid field, type expected"
| some type, _ =>
withLocalDecl view.name view.binderInfo type fun fieldFVar =>
let infos := infos.push { name := view.name, declName := view.declName, fvar := fieldFVar, value? := value?,
kind := StructFieldKind.newField, inferMod := view.inferMod }
withFields views (i+1) infos k
| none, some value =>
let type ← inferType value
withLocalDecl view.name view.binderInfo type fun fieldFVar =>
let infos := infos.push { name := view.name, declName := view.declName, fvar := fieldFVar, value? := value,
kind := StructFieldKind.newField, inferMod := view.inferMod }
withFields views (i+1) infos k
| some info =>
match info.kind with
| StructFieldKind.newField => throwError "field '{view.name}' has already been declared"
| StructFieldKind.fromParent =>
match view.value? with
| none => throwError "field '{view.name}' has been declared in parent structure"
| some valStx => do
if let some type := view.type? then
throwErrorAt type "omit field '{view.name}' type to set default value"
else
let mut valStx := valStx
if view.binders.getArgs.size > 0 then
valStx ← `(fun $(view.binders.getArgs)* => $valStx:term)
let fvarType ← inferType info.fvar
let value ← Term.elabTermEnsuringType valStx fvarType
let infos := infos.push { info with value? := value }
withFields views (i+1) infos k
| StructFieldKind.subobject => unreachable!
else
k infos
private def getResultUniverse (type : Expr) : TermElabM Level := do
let type ← whnf type
match type with
| Expr.sort u _ => pure u
| _ => throwError "unexpected structure resulting type"
private def collectUsed (params : Array Expr) (fieldInfos : Array StructFieldInfo) : StateRefT CollectFVars.State MetaM Unit := do
params.forM fun p => do
let type ← inferType p
Term.collectUsedFVars type
fieldInfos.forM fun info => do
let fvarType ← inferType info.fvar
Term.collectUsedFVars fvarType
match info.value? with
| none => pure ()
| some value => Term.collectUsedFVars value
private def removeUnused (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo)
: TermElabM (LocalContext × LocalInstances × Array Expr) := do
let (_, used) ← (collectUsed params fieldInfos).run {}
Term.removeUnused scopeVars used
private def withUsed {α} (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) (k : Array Expr → TermElabM α)
: TermElabM α := do
let (lctx, localInsts, vars) ← removeUnused scopeVars params fieldInfos
withLCtx lctx localInsts <| k vars
private def levelMVarToParamFVar (fvar : Expr) : StateRefT Nat TermElabM Unit := do
let type ← inferType fvar
discard <| Term.levelMVarToParam' type
private def levelMVarToParamFVars (fvars : Array Expr) : StateRefT Nat TermElabM Unit :=
fvars.forM levelMVarToParamFVar
private def levelMVarToParamAux (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo)
: StateRefT Nat TermElabM (Array StructFieldInfo) := do
levelMVarToParamFVars scopeVars
levelMVarToParamFVars params
fieldInfos.mapM fun info => do
levelMVarToParamFVar info.fvar
match info.value? with
| none => pure info
| some value =>
let value ← Term.levelMVarToParam' value
pure { info with value? := value }
private def levelMVarToParam (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : TermElabM (Array StructFieldInfo) :=
(levelMVarToParamAux scopeVars params fieldInfos).run' 1
private partial def collectUniversesFromFields (r : Level) (rOffset : Nat) (fieldInfos : Array StructFieldInfo) : TermElabM (Array Level) := do
fieldInfos.foldlM (init := #[]) fun (us : Array Level) (info : StructFieldInfo) => do
let type ← inferType info.fvar
let u ← getLevel type
let u ← instantiateLevelMVars u
accLevelAtCtor u r rOffset us
private def updateResultingUniverse (fieldInfos : Array StructFieldInfo) (type : Expr) : TermElabM Expr := do
let r ← getResultUniverse type
let rOffset : Nat := r.getOffset
let r : Level := r.getLevelOffset
match r with
| Level.mvar mvarId _ =>
let us ← collectUniversesFromFields r rOffset fieldInfos
let rNew := mkResultUniverse us rOffset
assignLevelMVar mvarId rNew
instantiateMVars type
| _ => throwError "failed to compute resulting universe level of structure, provide universe explicitly"
private def collectLevelParamsInFVar (s : CollectLevelParams.State) (fvar : Expr) : TermElabM CollectLevelParams.State := do
let type ← inferType fvar
let type ← instantiateMVars type
return collectLevelParams s type
private def collectLevelParamsInFVars (fvars : Array Expr) (s : CollectLevelParams.State) : TermElabM CollectLevelParams.State :=
fvars.foldlM collectLevelParamsInFVar s
private def collectLevelParamsInStructure (structType : Expr) (scopeVars : Array Expr) (params : Array Expr) (fieldInfos : Array StructFieldInfo)
: TermElabM (Array Name) := do
let s := collectLevelParams {} structType
let s ← collectLevelParamsInFVars scopeVars s
let s ← collectLevelParamsInFVars params s
let s ← fieldInfos.foldlM (init := s) fun s info => collectLevelParamsInFVar s info.fvar
return s.params
private def addCtorFields (fieldInfos : Array StructFieldInfo) : Nat → Expr → TermElabM Expr
| 0, type => pure type
| i+1, type => do
let info := fieldInfos[i]
let decl ← Term.getFVarLocalDecl! info.fvar
let type ← instantiateMVars type
let type := type.abstract #[info.fvar]
match info.kind with
| StructFieldKind.fromParent =>
let val := decl.value
addCtorFields fieldInfos i (type.instantiate1 val)
| _ =>
addCtorFields fieldInfos i (mkForall decl.userName decl.binderInfo decl.type type)
private def mkCtor (view : StructView) (levelParams : List Name) (params : Array Expr) (fieldInfos : Array StructFieldInfo) : TermElabM Constructor :=
withRef view.ref do
let type := mkAppN (mkConst view.declName (levelParams.map mkLevelParam)) params
let type ← addCtorFields fieldInfos fieldInfos.size type
let type ← mkForallFVars params type
let type ← instantiateMVars type
let type := type.inferImplicit params.size !view.ctor.inferMod
pure { name := view.ctor.declName, type }
@[extern "lean_mk_projections"]
private constant mkProjections (env : Environment) (structName : Name) (projs : List ProjectionInfo) (isClass : Bool) : Except KernelException Environment
private def addProjections (structName : Name) (projs : List ProjectionInfo) (isClass : Bool) : TermElabM Unit := do
let env ← getEnv
match mkProjections env structName projs isClass with
| Except.ok env => setEnv env
| Except.error ex => throwKernelException ex
private def registerStructure (structName : Name) (infos : Array StructFieldInfo) : TermElabM Unit :=
modifyEnv fun env => Lean.registerStructure env {
structName
fields := infos.filterMap fun info =>
if info.kind == StructFieldKind.fromParent then
none
else
some {
fieldName := info.name
projFn := info.declName
subobject? :=
if info.kind == StructFieldKind.subobject then
match env.find? info.declName with
| some (ConstantInfo.defnInfo val) =>
match val.type.getForallBody.getAppFn with
| Expr.const parentName .. => some parentName
| _ => panic! "ill-formed structure"
| _ => panic! "ill-formed environment"
else
none
}
}
private def mkAuxConstructions (declName : Name) : TermElabM Unit := do
let env ← getEnv
let hasUnit := env.contains `PUnit
let hasEq := env.contains `Eq
let hasHEq := env.contains `HEq
mkRecOn declName
if hasUnit then mkCasesOn declName
if hasUnit && hasEq && hasHEq then mkNoConfusion declName
private def addDefaults (lctx : LocalContext) (defaultAuxDecls : Array (Name × Expr × Expr)) : TermElabM Unit := do
let localInsts ← getLocalInstances
withLCtx lctx localInsts do
defaultAuxDecls.forM fun (declName, type, value) => do
let value ← instantiateMVars value
if value.hasExprMVar then
throwError "invalid default value for field, it contains metavariables{indentExpr value}"
/- The identity function is used as "marker". -/
let value ← mkId value
discard <| mkAuxDefinition declName type value (zeta := true)
setReducibleAttribute declName
private def elabStructureView (view : StructView) : TermElabM Unit := do
view.fields.forM fun field => do
if field.declName == view.ctor.declName then
throwErrorAt field.ref "invalid field name '{field.name}', it is equal to structure constructor name"
addAuxDeclarationRanges field.declName field.ref field.ref
let numExplicitParams := view.params.size
let type ← Term.elabType view.type
unless validStructType type do throwErrorAt view.type "expected Type"
withRef view.ref do
withParents view 0 #[] fun fieldInfos =>
withFields view.fields 0 fieldInfos fun fieldInfos => do
Term.synthesizeSyntheticMVarsNoPostponing
let u ← getResultUniverse type
let inferLevel ← shouldInferResultUniverse u
withUsed view.scopeVars view.params fieldInfos fun scopeVars => do
let numParams := scopeVars.size + numExplicitParams
let fieldInfos ← levelMVarToParam scopeVars view.params fieldInfos
let type ← withRef view.ref do
if inferLevel then
updateResultingUniverse fieldInfos type
else
checkResultingUniverse (← getResultUniverse type)
pure type
trace[Elab.structure] "type: {type}"
let usedLevelNames ← collectLevelParamsInStructure type scopeVars view.params fieldInfos
match sortDeclLevelParams view.scopeLevelNames view.allUserLevelNames usedLevelNames with
| Except.error msg => withRef view.ref <| throwError msg
| Except.ok levelParams =>
let params := scopeVars ++ view.params
let ctor ← mkCtor view levelParams params fieldInfos
let type ← mkForallFVars params type
let type ← instantiateMVars type
let indType := { name := view.declName, type := type, ctors := [ctor] : InductiveType }
let decl := Declaration.inductDecl levelParams params.size [indType] view.modifiers.isUnsafe
Term.ensureNoUnassignedMVars decl
addDecl decl
let projInfos := (fieldInfos.filter fun (info : StructFieldInfo) => !info.isFromParent).toList.map fun (info : StructFieldInfo) =>
{ declName := info.declName, inferMod := info.inferMod : ProjectionInfo }
addProjections view.declName projInfos view.isClass
registerStructure view.declName fieldInfos
mkAuxConstructions view.declName
let instParents ← fieldInfos.filterM fun info => do
let decl ← Term.getFVarLocalDecl! info.fvar
pure (info.isSubobject && decl.binderInfo.isInstImplicit)
let projInstances := instParents.toList.map fun info => info.declName
Term.applyAttributesAt view.declName view.modifiers.attrs AttributeApplicationTime.afterTypeChecking
projInstances.forM fun declName => addInstance declName AttributeKind.global (eval_prio default)
let lctx ← getLCtx
let fieldsWithDefault := fieldInfos.filter fun info => info.value?.isSome
let defaultAuxDecls ← fieldsWithDefault.mapM fun info => do
let type ← inferType info.fvar
pure (info.declName ++ `_default, type, info.value?.get!)
/- The `lctx` and `defaultAuxDecls` are used to create the auxiliary `_default` declarations
The parameters `params` for these definitions must be marked as implicit, and all others as explicit. -/
let lctx :=
params.foldl (init := lctx) fun (lctx : LocalContext) (p : Expr) =>
lctx.setBinderInfo p.fvarId! BinderInfo.implicit
let lctx :=
fieldInfos.foldl (init := lctx) fun (lctx : LocalContext) (info : StructFieldInfo) =>
if info.isFromParent then lctx -- `fromParent` fields are elaborated as let-decls, and are zeta-expanded when creating `_default`.
else lctx.setBinderInfo info.fvar.fvarId! BinderInfo.default
addDefaults lctx defaultAuxDecls
/-
leading_parser (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> " := " >> optional structCtor >> structFields >> optDeriving
where
def «extends» := leading_parser " extends " >> sepBy1 termParser ", "
def typeSpec := leading_parser " : " >> termParser
def optType : Parser := optional typeSpec
def structFields := leading_parser many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder)
def structCtor := leading_parser try (declModifiers >> ident >> optional inferMod >> " :: ")
-/
def elabStructure (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do
checkValidInductiveModifier modifiers
let isClass := stx[0].getKind == ``Parser.Command.classTk
let modifiers := if isClass then modifiers.addAttribute { name := `class } else modifiers
let declId := stx[1]
let params := stx[2].getArgs
let exts := stx[3]
let parents := if exts.isNone then #[] else exts[0][1].getSepArgs
let optType := stx[4]
let derivingClassViews ← getOptDerivingClasses stx[6]
let type ← if optType.isNone then `(Sort _) else pure optType[0][1]
let declName ←
runTermElabM none fun scopeVars => do
let scopeLevelNames ← Term.getLevelNames
let ⟨name, declName, allUserLevelNames⟩ ← Elab.expandDeclId (← getCurrNamespace) scopeLevelNames declId modifiers
addDeclarationRanges declName stx
Term.withDeclName declName do
let ctor ← expandCtor stx modifiers declName
let fields ← expandFields stx modifiers declName
Term.withLevelNames allUserLevelNames <| Term.withAutoBoundImplicit <|
Term.elabBinders params fun params => do
Term.synthesizeSyntheticMVarsNoPostponing
let params ← Term.addAutoBoundImplicits params
let allUserLevelNames ← Term.getLevelNames
elabStructureView {
ref := stx
modifiers
scopeLevelNames
allUserLevelNames
declName
isClass
scopeVars
params
parents
type
ctor
fields
}
unless isClass do
mkSizeOfInstances declName
mkInjectiveTheorems declName
return declName
derivingClassViews.forM fun view => view.applyHandlers #[declName]
builtin_initialize registerTraceClass `Elab.structure
end Lean.Elab.Command
|
9ef03f53f57a7edab6b9a8596f25fa95ad328a39 | d450724ba99f5b50b57d244eb41fef9f6789db81 | /src/mywork/lectures/lecture_9.lean | 56baf9dc6188245b79e4bceab0c4f4842b7f0e43 | [] | no_license | jakekauff/CS2120F21 | 4f009adeb4ce4a148442b562196d66cc6c04530c | e69529ec6f5d47a554291c4241a3d8ec4fe8f5ad | refs/heads/main | 1,693,841,880,030 | 1,637,604,848,000 | 1,637,604,848,000 | 399,946,698 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,228 | lean | /-
Negation
-/
/-
Given an proposition, P, we can form a new
proposition, usually written as ¬P, which we
pronounce as "not P," and which we define in
such as way as to assert that P is not true.
-/
/-
So what does it mean when we say that *it is
true that P is not true*? Or, equivalently, "it
is true that P is false?"
Clearly we don't mean that the proposition P
*is* (equal to) the proposition, *false*. No,
in this sense of false, what we mean by P is
false is that there are no proofs of P, so we
have to *judge* it to be logically false. We
distinguish between truth judgments and the
proposition, true.
-/
/-
So what we mean by ¬P is that there is no
proof of P. The trick is now in how to show
that there can be no proof of P. We prove
it somewhat indirectly by proving P → false.
Suppose P → false is true. What this says is
that if P is true then false is true. But the
latter is absurd because we've defined false
to the proposition with no proofs, so it can't
be true.
So if P → false is is true, then the consequence
of that is that there *can be no proofs of P*.
-/
/-
So let's launch a little exploration of proofs
involving false and proofs of false.
-/
/-
First, what's your intuition? Is the follow
proposition true or false?
false → false
We'll let's see what happens when we try to
prove it. What the implication says is that
*if* false is true, then false is true. To
prove it, assume false is true and that we
have a proof of it, f. All that remains is
to produce a proof of false: just f itself.
-/
example : false → false :=
begin
assume f,
exact f,
end
/-
Does false imply true? If the impossible happens,
is true still true? Turns out it is. Assume we've
got a proof that false is true. Now all we have to
do is to produce a proof of true. But it's an axiom
that there is one: in Lean called true.intro. So
it's true that false is true!
-/
example : false → true :=
begin
assume f,
exact true.intro,
end
/-
Does true imply true? This one's easy as they get.
-/
example : true → true :=
begin
assume t,
exact true.intro,
end
/-
Finally, does true imply false? Can you turn
any proof of true somehow into a proof of false?
If so, you have a proof of true → false. But the
answer is no. Suppose you have a proof of true.
Well, it's of no use at all in deriving a proof
of false, and in fact you can never derive one
from true premises, because our logic is sound(*).
-/
example : true → false :=
begin
assume t,
-- stuck: *there is no proof of false*
end
/-
So, wow, we just gained a lot of insight!
true → true true
true → false false
false → true true
false → false true
-/
/-
Having built some intuition, let's get back to
the meaning of ¬P. For any proposition, P, we
*define* ¬P to be the proposition, P → false.
So ¬P is true exactly when P → false is true,
and that is true exactly when P is false, when
there are no proofs of it. If you can produce a
proof of P → false, then you can conclude ¬P.
This is the introduction rule for ¬.
-/
#check not -- see definition in Lean library
/-
So how do you prove P → false? It's just like any
other implication: *assume* that P is true and show
that with that you can construct a proof of false.
-/
/-
Example. Prove ¬ 0 = 1.
Ok, we'll let's make another little diversion.
-/
example : false :=
begin -- no way to prove this
end
example : ¬ false :=
begin
assume f, -- REMEMBER: ¬P *means* P → false, so *assume* P, show false.
exact f,
end
example : ¬ (0 = 1) :=
begin
assume h,
-- what do we do now?
end
/-
To understand how to finish off this last
proof, we need to talk about *case analysis*
again. Remember that we used it to reason
from a proof of a disjunction. Suppose we
want to know that P ∨ Q → R. We start by
assuming that we have a proof, pq, of P ∨ Q,
then we need to show that R follows from
the truth of P ∨ Q as a logical consequence.
But to know whether R follows from P ∨ Q, we
need to know that it follows from each of them.
Well, there are exactly two possible forms that
a proof, pq, of P ∨ Q can take (or.intro_left p)
and (or.intro_right q), where p and q are proofs
of P and of Q, respectively. What we therefore
need to show is that no matter which of the two
cases, a proof of R follows.
The rest of the proof is by case analysis on
the proof, pq, of P ∨ Q. In the first case, we
assume P ∨ Q is true because P is (or.intro_left
was used to create the proof). In this case we
need to show that P → R. In the second case,
P ∨ Q is true because Q is (or.intro_right was
used to create the proof of P ∨ Q. Now we need
to show that having a proof of Q gets us to a
proof of R, i.e., that Q → R.
-/
-- template: proof of disjunction by case analysis
example : ∀ (P Q R : Prop), P ∨ Q → R :=
begin
assume P Q R,
assume pq, -- assuming we have a proof, (pq : P ∨ Q)
cases pq, -- proceed by case analysis
-- stuck here of course
end
/-
Case analysis is a broadly useful proof
technique. Here we use it to prove true
→ true. We assume the premise, true and
have to show true. The true.intro rule
will work, but let's instead try "case
analysis" on the assumed proof of true.
What we'll see is that there is only one
case (whereas with a proof of P ∨ Q, case
analysis presents two cases to consider.)
-/
example : true → true :=
begin
assume t,
cases t,
exact true.intro,
end
/-
The general principle is this: if we have
an assumed/arbitrary proof of X and need
to show Y, we can try to do this by doing
case analysis on the proof of X. If we can
show that Y is true *in all cases* (in a
context in which we have *some* proof of
X) then we have shown that Y must be true
in this context.
-/
/-
The most interesting example of the preceding
principle occurs when you're given or you can
derive a proof of false. For then all you have
to do to show that some proposition, P, follows
is to show that it's true for all possible ways
in which that proof of false could have been
constructed. Remember, there are two ways to
construct a proof of P ∨ Q, so case analysis
results in two cases to consider; and one way
to construct a proof of true, so there's only
one case to consider. Now, with a proof of false
there are *zero* ways to construct proof, *and
so there are zero cases to consider, and the
truth of your conclusion follows automatically!
-/
/-
Here we prove false → false again, but this
time instead of using the assumed proof of
false to prove false, we do case analysis on
the given proof of false. There are no cases
to consider, so the proof is complete!
-/
example : false → false :=
begin
assume f,
cases f, -- instead of exact f, do case analysis
end
/-
In fact, it doesn't matter what your conclusion
is: it will always be true in a context in which
you have a proof of false. And this makes sense,
because if you have a proof of false, then false
is true, so whether a given proposition is true
or false, it's true, because even if it's false,
well, false is true, so it's also true!
-/
/-
Here, then, is the general principle for false
elimination: how you *use* a proof of false that
you have been given, that you've assumed, or that
you've derived from a contradiction (as we will
see).
The theorem states that if you're given any
proposition, P, and a proof, f, of false, then
in that context, P has a proof and is true.
Another way to think about what's going on here
is that if you have a proof of false, you are
already in a situation that can't possible happen
"in reality" -- there is no proof of false -- so
you can just ignore this situation.
-/
theorem false_elim (P : Prop) (f : false) : P :=
begin
cases f,
end
/-
The elimination principle for false is called
false.elim in Lean. If you are given or can
derive a proof, f, of false, then all you have
to do to finish your proof is to say, "this is
situation can't happen, so we need not consider
it any further." Or, formally, (false.elim f).
-/
example : false → false :=
begin
assume f,
exact false.elim f, -- Using Lean's version
end
|
1f12e8948a0226eeb86938fab675e629bc74ba09 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/topology/algebra/order/liminf_limsup.lean | 80a2df2c1a6b1aeb81df8142ce585f0e2eea9752 | [
"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 | 18,694 | 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
import order.filter.archimedean
/-!
# 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
lemma is_bounded_le_at_bot (α : Type*) [hα : nonempty α] [preorder α] :
(at_bot : filter α).is_bounded (≤) :=
is_bounded_iff.2 ⟨set.Iic hα.some, mem_at_bot _, hα.some, λ x hx, hx⟩
lemma filter.tendsto.is_bounded_under_le_at_bot {α : Type*} [nonempty α] [preorder α]
{f : filter β} {u : β → α} (h : tendsto u f at_bot) :
f.is_bounded_under (≤) u :=
(is_bounded_le_at_bot α).mono h
lemma bdd_above_range_of_tendsto_at_top_at_bot {α : Type*} [nonempty α] [semilattice_sup α]
{u : ℕ → α} (hx : tendsto u at_top at_bot) : bdd_above (set.range u) :=
(filter.tendsto.is_bounded_under_le_at_bot hx).bdd_above_range
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
lemma is_bounded_ge_at_top (α : Type*) [hα : nonempty α] [preorder α] :
(at_top : filter α).is_bounded (≥) :=
is_bounded_le_at_bot αᵒᵈ
lemma filter.tendsto.is_bounded_under_ge_at_top {α : Type*} [nonempty α] [preorder α]
{f : filter β} {u : β → α} (h : tendsto u f at_top) :
f.is_bounded_under (≥) u :=
(is_bounded_ge_at_top α).mono h
lemma bdd_below_range_of_tendsto_at_top_at_top {α : Type*} [nonempty α] [semilattice_inf α]
{u : ℕ → α} (hx : tendsto u at_top at_top) : bdd_below (set.range u) :=
(filter.tendsto.is_bounded_under_ge_at_top hx).bdd_below_range
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
section indicator
open_locale big_operators
lemma limsup_eq_tendsto_sum_indicator_nat_at_top (s : ℕ → set α) :
limsup at_top s =
{ω | tendsto (λ n, ∑ k in finset.range n, (s (k + 1)).indicator (1 : α → ℕ) ω) at_top at_top} :=
begin
ext ω,
simp only [limsup_eq_infi_supr_of_nat, ge_iff_le, set.supr_eq_Union,
set.infi_eq_Inter, set.mem_Inter, set.mem_Union, exists_prop],
split,
{ intro hω,
refine tendsto_at_top_at_top_of_monotone' (λ n m hnm, finset.sum_mono_set_of_nonneg
(λ i, set.indicator_nonneg (λ _ _, zero_le_one) _) (finset.range_mono hnm)) _,
rintro ⟨i, h⟩,
simp only [mem_upper_bounds, set.mem_range, forall_exists_index, forall_apply_eq_imp_iff'] at h,
induction i with k hk,
{ obtain ⟨j, hj₁, hj₂⟩ := hω 1,
refine not_lt.2 (h $ j + 1) (lt_of_le_of_lt
(finset.sum_const_zero.symm : 0 = ∑ k in finset.range (j + 1), 0).le _),
refine finset.sum_lt_sum (λ m _, set.indicator_nonneg (λ _ _, zero_le_one) _)
⟨j - 1, finset.mem_range.2 (lt_of_le_of_lt (nat.sub_le _ _) j.lt_succ_self), _⟩,
rw [nat.sub_add_cancel hj₁, set.indicator_of_mem hj₂],
exact zero_lt_one },
{ rw imp_false at hk,
push_neg at hk,
obtain ⟨i, hi⟩ := hk,
obtain ⟨j, hj₁, hj₂⟩ := hω (i + 1),
replace hi : ∑ k in finset.range i, (s (k + 1)).indicator 1 ω = k + 1 := le_antisymm (h i) hi,
refine not_lt.2 (h $ j + 1) _,
rw [← finset.sum_range_add_sum_Ico _ (i.le_succ.trans (hj₁.trans j.le_succ)), hi],
refine lt_add_of_pos_right _ _,
rw (finset.sum_const_zero.symm : 0 = ∑ k in finset.Ico i (j + 1), 0),
refine finset.sum_lt_sum (λ m _, set.indicator_nonneg (λ _ _, zero_le_one) _)
⟨j - 1, finset.mem_Ico.2
⟨(nat.le_sub_iff_right (le_trans ((le_add_iff_nonneg_left _).2 zero_le') hj₁)).2 hj₁,
lt_of_le_of_lt (nat.sub_le _ _) j.lt_succ_self⟩, _⟩,
rw [nat.sub_add_cancel (le_trans ((le_add_iff_nonneg_left _).2 zero_le') hj₁),
set.indicator_of_mem hj₂],
exact zero_lt_one } },
{ rintro hω i,
rw [set.mem_set_of_eq, tendsto_at_top_at_top] at hω,
by_contra hcon,
push_neg at hcon,
obtain ⟨j, h⟩ := hω (i + 1),
have : ∑ k in finset.range j, (s (k + 1)).indicator 1 ω ≤ i,
{ have hle : ∀ j ≤ i, ∑ k in finset.range j, (s (k + 1)).indicator 1 ω ≤ i,
{ refine λ j hij, (finset.sum_le_card_nsmul _ _ _ _ : _ ≤ (finset.range j).card • 1).trans _,
{ exact λ m hm, set.indicator_apply_le' (λ _, le_rfl) (λ _, zero_le_one) },
{ simpa only [finset.card_range, smul_eq_mul, mul_one] } },
by_cases hij : j < i,
{ exact hle _ hij.le },
{ rw ← finset.sum_range_add_sum_Ico _ (not_lt.1 hij),
suffices : ∑ k in finset.Ico i j, (s (k + 1)).indicator 1 ω = 0,
{ rw [this, add_zero],
exact hle _ le_rfl },
rw finset.sum_eq_zero (λ m hm, _),
exact set.indicator_of_not_mem (hcon _ $ (finset.mem_Ico.1 hm).1.trans m.le_succ) _ } },
exact not_le.2 (lt_of_lt_of_le i.lt_succ_self $ h _ le_rfl) this }
end
lemma limsup_eq_tendsto_sum_indicator_at_top
(R : Type*) [ordered_semiring R] [nontrivial R] [archimedean R] (s : ℕ → set α) :
limsup at_top s =
{ω | tendsto (λ n, ∑ k in finset.range n, (s (k + 1)).indicator (1 : α → R) ω) at_top at_top} :=
begin
rw limsup_eq_tendsto_sum_indicator_nat_at_top s,
ext ω,
simp only [set.mem_set_of_eq],
rw (_ : (λ n, ∑ k in finset.range n, (s (k + 1)).indicator (1 : α → R) ω) =
(λ n, ↑(∑ k in finset.range n, (s (k + 1)).indicator (1 : α → ℕ) ω))),
{ exact tendsto_coe_nat_at_top_iff.symm },
{ ext n,
simp only [set.indicator, pi.one_apply, finset.sum_boole, nat.cast_id] }
end
end indicator
|
1ddb097d4323b48d242a4ac4cbf7ba5a07bed506 | 77c5b91fae1b966ddd1db969ba37b6f0e4901e88 | /src/algebra/lie/basic.lean | 4f3e262a2cdaa9db342a6757c136b770098bd9f5 | [
"Apache-2.0"
] | permissive | dexmagic/mathlib | ff48eefc56e2412429b31d4fddd41a976eb287ce | 7a5d15a955a92a90e1d398b2281916b9c41270b2 | refs/heads/master | 1,693,481,322,046 | 1,633,360,193,000 | 1,633,360,193,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,728 | lean | /-
Copyright (c) 2019 Oliver Nash. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Oliver Nash
-/
import data.bracket
import algebra.algebra.basic
import tactic.noncomm_ring
/-!
# Lie algebras
This file defines Lie rings and Lie algebras over a commutative ring together with their
modules, morphisms and equivalences, as well as various lemmas to make these definitions usable.
## Main definitions
* `lie_ring`
* `lie_algebra`
* `lie_ring_module`
* `lie_module`
* `lie_hom`
* `lie_equiv`
* `lie_module_hom`
* `lie_module_equiv`
## Notation
Working over a fixed commutative ring `R`, we introduce the notations:
* `L →ₗ⁅R⁆ L'` for a morphism of Lie algebras,
* `L ≃ₗ⁅R⁆ L'` for an equivalence of Lie algebras,
* `M →ₗ⁅R,L⁆ N` for a morphism of Lie algebra modules `M`, `N` over a Lie algebra `L`,
* `M ≃ₗ⁅R,L⁆ N` for an equivalence of Lie algebra modules `M`, `N` over a Lie algebra `L`.
## Implementation notes
Lie algebras are defined as modules with a compatible Lie ring structure and thus, like modules,
are partially unbundled.
## References
* [N. Bourbaki, *Lie Groups and Lie Algebras, Chapters 1--3*](bourbaki1975)
## Tags
lie bracket, jacobi identity, lie ring, lie algebra, lie module
-/
universes u v w w₁ w₂
/-- A Lie ring is an additive group with compatible product, known as the bracket, satisfying the
Jacobi identity. -/
@[protect_proj] class lie_ring (L : Type v) extends add_comm_group L, has_bracket L L :=
(add_lie : ∀ (x y z : L), ⁅x + y, z⁆ = ⁅x, z⁆ + ⁅y, z⁆)
(lie_add : ∀ (x y z : L), ⁅x, y + z⁆ = ⁅x, y⁆ + ⁅x, z⁆)
(lie_self : ∀ (x : L), ⁅x, x⁆ = 0)
(leibniz_lie : ∀ (x y z : L), ⁅x, ⁅y, z⁆⁆ = ⁅⁅x, y⁆, z⁆ + ⁅y, ⁅x, z⁆⁆)
/-- A Lie algebra is a module with compatible product, known as the bracket, satisfying the Jacobi
identity. Forgetting the scalar multiplication, every Lie algebra is a Lie ring. -/
@[protect_proj] class lie_algebra (R : Type u) (L : Type v) [comm_ring R] [lie_ring L]
extends module R L :=
(lie_smul : ∀ (t : R) (x y : L), ⁅x, t • y⁆ = t • ⁅x, y⁆)
/-- A Lie ring module is an additive group, together with an additive action of a
Lie ring on this group, such that the Lie bracket acts as the commutator of endomorphisms.
(For representations of Lie *algebras* see `lie_module`.) -/
@[protect_proj] class lie_ring_module (L : Type v) (M : Type w)
[lie_ring L] [add_comm_group M] extends has_bracket L M :=
(add_lie : ∀ (x y : L) (m : M), ⁅x + y, m⁆ = ⁅x, m⁆ + ⁅y, m⁆)
(lie_add : ∀ (x : L) (m n : M), ⁅x, m + n⁆ = ⁅x, m⁆ + ⁅x, n⁆)
(leibniz_lie : ∀ (x y : L) (m : M), ⁅x, ⁅y, m⁆⁆ = ⁅⁅x, y⁆, m⁆ + ⁅y, ⁅x, m⁆⁆)
/-- A Lie module is a module over a commutative ring, together with a linear action of a Lie
algebra on this module, such that the Lie bracket acts as the commutator of endomorphisms. -/
@[protect_proj] class lie_module (R : Type u) (L : Type v) (M : Type w)
[comm_ring R] [lie_ring L] [lie_algebra R L] [add_comm_group M] [module R M]
[lie_ring_module L M] :=
(smul_lie : ∀ (t : R) (x : L) (m : M), ⁅t • x, m⁆ = t • ⁅x, m⁆)
(lie_smul : ∀ (t : R) (x : L) (m : M), ⁅x, t • m⁆ = t • ⁅x, m⁆)
section basic_properties
variables {R : Type u} {L : Type v} {M : Type w} {N : Type w₁}
variables [comm_ring R] [lie_ring L] [lie_algebra R L]
variables [add_comm_group M] [module R M] [lie_ring_module L M] [lie_module R L M]
variables [add_comm_group N] [module R N] [lie_ring_module L N] [lie_module R L N]
variables (t : R) (x y z : L) (m n : M)
@[simp] lemma add_lie : ⁅x + y, m⁆ = ⁅x, m⁆ + ⁅y, m⁆ := lie_ring_module.add_lie x y m
@[simp] lemma lie_add : ⁅x, m + n⁆ = ⁅x, m⁆ + ⁅x, n⁆ := lie_ring_module.lie_add x m n
@[simp] lemma smul_lie : ⁅t • x, m⁆ = t • ⁅x, m⁆ := lie_module.smul_lie t x m
@[simp] lemma lie_smul : ⁅x, t • m⁆ = t • ⁅x, m⁆ := lie_module.lie_smul t x m
lemma leibniz_lie : ⁅x, ⁅y, m⁆⁆ = ⁅⁅x, y⁆, m⁆ + ⁅y, ⁅x, m⁆⁆ := lie_ring_module.leibniz_lie x y m
@[simp] lemma lie_zero : ⁅x, 0⁆ = (0 : M) := (add_monoid_hom.mk' _ (lie_add x)).map_zero
@[simp] lemma zero_lie : ⁅(0 : L), m⁆ = 0 :=
(add_monoid_hom.mk' (λ (x : L), ⁅x, m⁆) (λ x y, add_lie x y m)).map_zero
@[simp] lemma lie_self : ⁅x, x⁆ = 0 := lie_ring.lie_self x
instance lie_ring_self_module : lie_ring_module L L := { ..(infer_instance : lie_ring L) }
@[simp] lemma lie_skew : -⁅y, x⁆ = ⁅x, y⁆ :=
have h : ⁅x + y, x⁆ + ⁅x + y, y⁆ = 0, { rw ← lie_add, apply lie_self, },
by simpa [neg_eq_iff_add_eq_zero] using h
/-- Every Lie algebra is a module over itself. -/
instance lie_algebra_self_module : lie_module R L L :=
{ smul_lie := λ t x m, by rw [←lie_skew, ←lie_skew x m, lie_algebra.lie_smul, smul_neg],
lie_smul := by apply lie_algebra.lie_smul, }
@[simp] lemma neg_lie : ⁅-x, m⁆ = -⁅x, m⁆ :=
by { rw [←sub_eq_zero, sub_neg_eq_add, ←add_lie], simp, }
@[simp] lemma lie_neg : ⁅x, -m⁆ = -⁅x, m⁆ :=
by { rw [←sub_eq_zero, sub_neg_eq_add, ←lie_add], simp, }
@[simp] lemma sub_lie : ⁅x - y, m⁆ = ⁅x, m⁆ - ⁅y, m⁆ :=
by simp [sub_eq_add_neg]
@[simp] lemma lie_sub : ⁅x, m - n⁆ = ⁅x, m⁆ - ⁅x, n⁆ :=
by simp [sub_eq_add_neg]
@[simp] lemma nsmul_lie (n : ℕ) : ⁅n • x, m⁆ = n • ⁅x, m⁆ :=
add_monoid_hom.map_nsmul ⟨λ (x : L), ⁅x, m⁆, zero_lie m, λ _ _, add_lie _ _ _⟩ _ _
@[simp] lemma lie_nsmul (n : ℕ) : ⁅x, n • m⁆ = n • ⁅x, m⁆ :=
add_monoid_hom.map_nsmul ⟨λ (m : M), ⁅x, m⁆, lie_zero x, λ _ _, lie_add _ _ _⟩ _ _
@[simp] lemma gsmul_lie (a : ℤ) : ⁅a • x, m⁆ = a • ⁅x, m⁆ :=
add_monoid_hom.map_gsmul ⟨λ (x : L), ⁅x, m⁆, zero_lie m, λ _ _, add_lie _ _ _⟩ _ _
@[simp] lemma lie_gsmul (a : ℤ) : ⁅x, a • m⁆ = a • ⁅x, m⁆ :=
add_monoid_hom.map_gsmul ⟨λ (m : M), ⁅x, m⁆, lie_zero x, λ _ _, lie_add _ _ _⟩ _ _
@[simp] lemma lie_lie : ⁅⁅x, y⁆, m⁆ = ⁅x, ⁅y, m⁆⁆ - ⁅y, ⁅x, m⁆⁆ :=
by rw [leibniz_lie, add_sub_cancel]
lemma lie_jacobi : ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0 :=
by { rw [← neg_neg ⁅x, y⁆, lie_neg z, lie_skew y x, ← lie_skew, lie_lie], abel, }
instance lie_ring.int_lie_algebra : lie_algebra ℤ L :=
{ lie_smul := λ n x y, lie_gsmul x y n, }
instance : lie_ring_module L (M →ₗ[R] N) :=
{ bracket := λ x f,
{ to_fun := λ m, ⁅x, f m⁆ - f ⁅x, m⁆,
map_add' := λ m n, by { simp only [lie_add, linear_map.map_add], abel, },
map_smul' := λ t m, by simp only [smul_sub, linear_map.map_smul, lie_smul, ring_hom.id_apply] },
add_lie := λ x y f, by
{ ext n, simp only [add_lie, linear_map.coe_mk, linear_map.add_apply, linear_map.map_add],
abel, },
lie_add := λ x f g, by
{ ext n, simp only [linear_map.coe_mk, lie_add, linear_map.add_apply], abel, },
leibniz_lie := λ x y f, by
{ ext n,
simp only [lie_lie, linear_map.coe_mk, linear_map.map_sub, linear_map.add_apply, lie_sub],
abel, }, }
@[simp] lemma lie_hom.lie_apply (f : M →ₗ[R] N) (x : L) (m : M) :
⁅x, f⁆ m = ⁅x, f m⁆ - f ⁅x, m⁆ :=
rfl
instance : lie_module R L (M →ₗ[R] N) :=
{ smul_lie := λ t x f, by
{ ext n,
simp only [smul_sub, smul_lie, linear_map.smul_apply, lie_hom.lie_apply,
linear_map.map_smul], },
lie_smul := λ t x f, by
{ ext n, simp only [smul_sub, linear_map.smul_apply, lie_hom.lie_apply, lie_smul], }, }
end basic_properties
set_option old_structure_cmd true
/-- A morphism of Lie algebras is a linear map respecting the bracket operations. -/
structure lie_hom (R : Type u) (L : Type v) (L' : Type w)
[comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L']
extends L →ₗ[R] L' :=
(map_lie' : ∀ {x y : L}, to_fun ⁅x, y⁆ = ⁅to_fun x, to_fun y⁆)
attribute [nolint doc_blame] lie_hom.to_linear_map
notation L ` →ₗ⁅`:25 R:25 `⁆ `:0 L':0 := lie_hom R L L'
namespace lie_hom
variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁}
variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃]
variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃]
instance : has_coe (L₁ →ₗ⁅R⁆ L₂) (L₁ →ₗ[R] L₂) := ⟨lie_hom.to_linear_map⟩
/-- see Note [function coercion] -/
instance : has_coe_to_fun (L₁ →ₗ⁅R⁆ L₂) := ⟨_, lie_hom.to_fun⟩
initialize_simps_projections lie_hom (to_fun → apply)
@[simp, norm_cast] lemma coe_to_linear_map (f : L₁ →ₗ⁅R⁆ L₂) : ((f : L₁ →ₗ[R] L₂) : L₁ → L₂) = f :=
rfl
@[simp] lemma to_fun_eq_coe (f : L₁ →ₗ⁅R⁆ L₂) : f.to_fun = ⇑f := rfl
@[simp] lemma map_smul (f : L₁ →ₗ⁅R⁆ L₂) (c : R) (x : L₁) : f (c • x) = c • f x :=
linear_map.map_smul (f : L₁ →ₗ[R] L₂) c x
@[simp] lemma map_add (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f (x + y) = (f x) + (f y) :=
linear_map.map_add (f : L₁ →ₗ[R] L₂) x y
@[simp] lemma map_sub (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f (x - y) = (f x) - (f y) :=
linear_map.map_sub (f : L₁ →ₗ[R] L₂) x y
@[simp] lemma map_neg (f : L₁ →ₗ⁅R⁆ L₂) (x : L₁) : f (-x) = -(f x) :=
linear_map.map_neg (f : L₁ →ₗ[R] L₂) x
@[simp] lemma map_lie (f : L₁ →ₗ⁅R⁆ L₂) (x y : L₁) : f ⁅x, y⁆ = ⁅f x, f y⁆ := lie_hom.map_lie' f
@[simp] lemma map_zero (f : L₁ →ₗ⁅R⁆ L₂) : f 0 = 0 := (f : L₁ →ₗ[R] L₂).map_zero
/-- The identity map is a morphism of Lie algebras. -/
def id : L₁ →ₗ⁅R⁆ L₁ :=
{ map_lie' := λ x y, rfl,
.. (linear_map.id : L₁ →ₗ[R] L₁) }
@[simp] lemma coe_id : ((id : L₁ →ₗ⁅R⁆ L₁) : L₁ → L₁) = _root_.id := rfl
lemma id_apply (x : L₁) : (id : L₁ →ₗ⁅R⁆ L₁) x = x := rfl
/-- The constant 0 map is a Lie algebra morphism. -/
instance : has_zero (L₁ →ₗ⁅R⁆ L₂) := ⟨{ map_lie' := by simp, ..(0 : L₁ →ₗ[R] L₂)}⟩
@[norm_cast, simp] lemma coe_zero : ((0 : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) = 0 := rfl
lemma zero_apply (x : L₁) : (0 : L₁ →ₗ⁅R⁆ L₂) x = 0 := rfl
/-- The identity map is a Lie algebra morphism. -/
instance : has_one (L₁ →ₗ⁅R⁆ L₁) := ⟨id⟩
@[simp] lemma coe_one : ((1 : (L₁ →ₗ⁅R⁆ L₁)) : L₁ → L₁) = _root_.id := rfl
lemma one_apply (x : L₁) : (1 : (L₁ →ₗ⁅R⁆ L₁)) x = x := rfl
instance : inhabited (L₁ →ₗ⁅R⁆ L₂) := ⟨0⟩
lemma coe_injective : @function.injective (L₁ →ₗ⁅R⁆ L₂) (L₁ → L₂) coe_fn :=
by rintro ⟨f, _⟩ ⟨g, _⟩ ⟨h⟩; congr
@[ext] lemma ext {f g : L₁ →ₗ⁅R⁆ L₂} (h : ∀ x, f x = g x) : f = g :=
coe_injective $ funext h
lemma ext_iff {f g : L₁ →ₗ⁅R⁆ L₂} : f = g ↔ ∀ x, f x = g x :=
⟨by { rintro rfl x, refl }, ext⟩
lemma congr_fun {f g : L₁ →ₗ⁅R⁆ L₂} (h : f = g) (x : L₁) : f x = g x := h ▸ rfl
@[simp] lemma mk_coe (f : L₁ →ₗ⁅R⁆ L₂) (h₁ h₂ h₃) :
(⟨f, h₁, h₂, h₃⟩ : L₁ →ₗ⁅R⁆ L₂) = f :=
by { ext, refl, }
@[simp] lemma coe_mk (f : L₁ → L₂) (h₁ h₂ h₃) :
((⟨f, h₁, h₂, h₃⟩ : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) = f := rfl
@[norm_cast, simp] lemma coe_linear_mk (f : L₁ →ₗ[R] L₂) (h₁ h₂ h₃) :
((⟨f, h₁, h₂, h₃⟩ : L₁ →ₗ⁅R⁆ L₂) : L₁ →ₗ[R] L₂) = ⟨f, h₁, h₂⟩ :=
by { ext, refl, }
/-- The composition of morphisms is a morphism. -/
def comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) : L₁ →ₗ⁅R⁆ L₃ :=
{ map_lie' := λ x y, by { change f (g ⁅x, y⁆) = ⁅f (g x), f (g y)⁆, rw [map_lie, map_lie], },
..linear_map.comp f.to_linear_map g.to_linear_map }
lemma comp_apply (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) (x : L₁) :
f.comp g x = f (g x) := rfl
@[norm_cast, simp]
lemma coe_comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) :
(f.comp g : L₁ → L₃) = f ∘ g :=
rfl
@[norm_cast, simp]
lemma coe_linear_map_comp (f : L₂ →ₗ⁅R⁆ L₃) (g : L₁ →ₗ⁅R⁆ L₂) :
(f.comp g : L₁ →ₗ[R] L₃) = (f : L₂ →ₗ[R] L₃).comp (g : L₁ →ₗ[R] L₂) :=
rfl
@[simp] lemma comp_id (f : L₁ →ₗ⁅R⁆ L₂) : f.comp (id : L₁ →ₗ⁅R⁆ L₁) = f :=
by { ext, refl, }
@[simp] lemma id_comp (f : L₁ →ₗ⁅R⁆ L₂) : (id : L₂ →ₗ⁅R⁆ L₂).comp f = f :=
by { ext, refl, }
/-- The inverse of a bijective morphism is a morphism. -/
def inverse (f : L₁ →ₗ⁅R⁆ L₂) (g : L₂ → L₁)
(h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : L₂ →ₗ⁅R⁆ L₁ :=
{ map_lie' := λ x y,
calc g ⁅x, y⁆ = g ⁅f (g x), f (g y)⁆ : by { conv_lhs { rw [←h₂ x, ←h₂ y], }, }
... = g (f ⁅g x, g y⁆) : by rw map_lie
... = ⁅g x, g y⁆ : (h₁ _),
..linear_map.inverse f.to_linear_map g h₁ h₂ }
end lie_hom
/-- An equivalence of Lie algebras is a morphism which is also a linear equivalence. We could
instead define an equivalence to be a morphism which is also a (plain) equivalence. However it is
more convenient to define via linear equivalence to get `.to_linear_equiv` for free. -/
structure lie_equiv (R : Type u) (L : Type v) (L' : Type w)
[comm_ring R] [lie_ring L] [lie_algebra R L] [lie_ring L'] [lie_algebra R L']
extends L →ₗ⁅R⁆ L', L ≃ₗ[R] L'
attribute [nolint doc_blame] lie_equiv.to_lie_hom
attribute [nolint doc_blame] lie_equiv.to_linear_equiv
notation L ` ≃ₗ⁅`:50 R `⁆ ` L' := lie_equiv R L L'
namespace lie_equiv
variables {R : Type u} {L₁ : Type v} {L₂ : Type w} {L₃ : Type w₁}
variables [comm_ring R] [lie_ring L₁] [lie_ring L₂] [lie_ring L₃]
variables [lie_algebra R L₁] [lie_algebra R L₂] [lie_algebra R L₃]
instance has_coe_to_lie_hom : has_coe (L₁ ≃ₗ⁅R⁆ L₂) (L₁ →ₗ⁅R⁆ L₂) := ⟨to_lie_hom⟩
instance has_coe_to_linear_equiv : has_coe (L₁ ≃ₗ⁅R⁆ L₂) (L₁ ≃ₗ[R] L₂) := ⟨to_linear_equiv⟩
/-- see Note [function coercion] -/
instance : has_coe_to_fun (L₁ ≃ₗ⁅R⁆ L₂) := ⟨_, to_fun⟩
@[simp, norm_cast] lemma coe_to_lie_equiv (e : L₁ ≃ₗ⁅R⁆ L₂) : ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) = e :=
rfl
@[simp, norm_cast] lemma coe_to_linear_equiv (e : L₁ ≃ₗ⁅R⁆ L₂) :
((e : L₁ ≃ₗ[R] L₂) : L₁ → L₂) = e := rfl
instance : has_one (L₁ ≃ₗ⁅R⁆ L₁) :=
⟨{ map_lie' := λ x y,
by { change ((1 : L₁→ₗ[R] L₁) ⁅x, y⁆) = ⁅(1 : L₁→ₗ[R] L₁) x, (1 : L₁→ₗ[R] L₁) y⁆, simp, },
..(1 : L₁ ≃ₗ[R] L₁)}⟩
@[simp] lemma one_apply (x : L₁) : (1 : (L₁ ≃ₗ⁅R⁆ L₁)) x = x := rfl
instance : inhabited (L₁ ≃ₗ⁅R⁆ L₁) := ⟨1⟩
/-- Lie algebra equivalences are reflexive. -/
@[refl]
def refl : L₁ ≃ₗ⁅R⁆ L₁ := 1
@[simp] lemma refl_apply (x : L₁) : (refl : L₁ ≃ₗ⁅R⁆ L₁) x = x := rfl
/-- Lie algebra equivalences are symmetric. -/
@[symm]
def symm (e : L₁ ≃ₗ⁅R⁆ L₂) : L₂ ≃ₗ⁅R⁆ L₁ :=
{ ..lie_hom.inverse e.to_lie_hom e.inv_fun e.left_inv e.right_inv,
..e.to_linear_equiv.symm }
@[simp] lemma symm_symm (e : L₁ ≃ₗ⁅R⁆ L₂) : e.symm.symm = e :=
by { cases e, refl, }
@[simp] lemma apply_symm_apply (e : L₁ ≃ₗ⁅R⁆ L₂) : ∀ x, e (e.symm x) = x :=
e.to_linear_equiv.apply_symm_apply
@[simp] lemma symm_apply_apply (e : L₁ ≃ₗ⁅R⁆ L₂) : ∀ x, e.symm (e x) = x :=
e.to_linear_equiv.symm_apply_apply
/-- Lie algebra equivalences are transitive. -/
@[trans]
def trans (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) : L₁ ≃ₗ⁅R⁆ L₃ :=
{ ..lie_hom.comp e₂.to_lie_hom e₁.to_lie_hom,
..linear_equiv.trans e₁.to_linear_equiv e₂.to_linear_equiv }
@[simp] lemma trans_apply (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) (x : L₁) :
(e₁.trans e₂) x = e₂ (e₁ x) := rfl
@[simp] lemma symm_trans_apply (e₁ : L₁ ≃ₗ⁅R⁆ L₂) (e₂ : L₂ ≃ₗ⁅R⁆ L₃) (x : L₃) :
(e₁.trans e₂).symm x = e₁.symm (e₂.symm x) := rfl
lemma bijective (e : L₁ ≃ₗ⁅R⁆ L₂) : function.bijective ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) :=
e.to_linear_equiv.bijective
lemma injective (e : L₁ ≃ₗ⁅R⁆ L₂) : function.injective ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) :=
e.to_linear_equiv.injective
lemma surjective (e : L₁ ≃ₗ⁅R⁆ L₂) : function.surjective ((e : L₁ →ₗ⁅R⁆ L₂) : L₁ → L₂) :=
e.to_linear_equiv.surjective
end lie_equiv
section lie_module_morphisms
variables (R : Type u) (L : Type v) (M : Type w) (N : Type w₁) (P : Type w₂)
variables [comm_ring R] [lie_ring L] [lie_algebra R L]
variables [add_comm_group M] [add_comm_group N] [add_comm_group P]
variables [module R M] [module R N] [module R P]
variables [lie_ring_module L M] [lie_ring_module L N] [lie_ring_module L P]
variables [lie_module R L M] [lie_module R L N] [lie_module R L P]
set_option old_structure_cmd true
/-- A morphism of Lie algebra modules is a linear map which commutes with the action of the Lie
algebra. -/
structure lie_module_hom extends M →ₗ[R] N :=
(map_lie' : ∀ {x : L} {m : M}, to_fun ⁅x, m⁆ = ⁅x, to_fun m⁆)
attribute [nolint doc_blame] lie_module_hom.to_linear_map
notation M ` →ₗ⁅`:25 R,L:25 `⁆ `:0 N:0 := lie_module_hom R L M N
namespace lie_module_hom
variables {R L M N P}
instance : has_coe (M →ₗ⁅R,L⁆ N) (M →ₗ[R] N) := ⟨lie_module_hom.to_linear_map⟩
/-- see Note [function coercion] -/
instance : has_coe_to_fun (M →ₗ⁅R,L⁆ N) := ⟨_, lie_module_hom.to_fun⟩
@[simp, norm_cast] lemma coe_to_linear_map (f : M →ₗ⁅R,L⁆ N) : ((f : M →ₗ[R] N) : M → N) = f :=
rfl
@[simp] lemma map_smul (f : M →ₗ⁅R,L⁆ N) (c : R) (x : M) : f (c • x) = c • f x :=
linear_map.map_smul (f : M →ₗ[R] N) c x
@[simp] lemma map_add (f : M →ₗ⁅R,L⁆ N) (x y : M) : f (x + y) = (f x) + (f y) :=
linear_map.map_add (f : M →ₗ[R] N) x y
@[simp] lemma map_sub (f : M →ₗ⁅R,L⁆ N) (x y : M) : f (x - y) = (f x) - (f y) :=
linear_map.map_sub (f : M →ₗ[R] N) x y
@[simp] lemma map_neg (f : M →ₗ⁅R,L⁆ N) (x : M) : f (-x) = -(f x) :=
linear_map.map_neg (f : M →ₗ[R] N) x
@[simp] lemma map_lie (f : M →ₗ⁅R,L⁆ N) (x : L) (m : M) : f ⁅x, m⁆ = ⁅x, f m⁆ :=
lie_module_hom.map_lie' f
lemma map_lie₂ (f : M →ₗ⁅R,L⁆ N →ₗ[R] P) (x : L) (m : M) (n : N) :
⁅x, f m n⁆ = f ⁅x, m⁆ n + f m ⁅x, n⁆ :=
by simp only [sub_add_cancel, map_lie, lie_hom.lie_apply]
@[simp] lemma map_zero (f : M →ₗ⁅R,L⁆ N) : f 0 = 0 :=
linear_map.map_zero (f : M →ₗ[R] N)
/-- The constant 0 map is a Lie module morphism. -/
instance : has_zero (M →ₗ⁅R,L⁆ N) := ⟨{ map_lie' := by simp, ..(0 : M →ₗ[R] N) }⟩
@[norm_cast, simp] lemma coe_zero : ((0 : M →ₗ⁅R,L⁆ N) : M → N) = 0 := rfl
lemma zero_apply (m : M) : (0 : M →ₗ⁅R,L⁆ N) m = 0 := rfl
/-- The identity map is a Lie module morphism. -/
instance : has_one (M →ₗ⁅R,L⁆ M) := ⟨{ map_lie' := by simp, ..(1 : M →ₗ[R] M) }⟩
instance : inhabited (M →ₗ⁅R,L⁆ N) := ⟨0⟩
lemma coe_injective : @function.injective (M →ₗ⁅R,L⁆ N) (M → N) coe_fn :=
by { rintros ⟨f, _⟩ ⟨g, _⟩ ⟨h⟩, congr, }
@[ext] lemma ext {f g : M →ₗ⁅R,L⁆ N} (h : ∀ m, f m = g m) : f = g :=
coe_injective $ funext h
lemma ext_iff {f g : M →ₗ⁅R,L⁆ N} : f = g ↔ ∀ m, f m = g m :=
⟨by { rintro rfl m, refl, }, ext⟩
lemma congr_fun {f g : M →ₗ⁅R,L⁆ N} (h : f = g) (x : M) : f x = g x := h ▸ rfl
@[simp] lemma mk_coe (f : M →ₗ⁅R,L⁆ N) (h₁ h₂ h₃) :
(⟨f, h₁, h₂, h₃⟩ : M →ₗ⁅R,L⁆ N) = f :=
by { ext, refl, }
@[simp] lemma coe_mk (f : M → N) (h₁ h₂ h₃) :
((⟨f, h₁, h₂, h₃⟩ : M →ₗ⁅R,L⁆ N) : M → N) = f := rfl
@[norm_cast, simp] lemma coe_linear_mk (f : M →ₗ[R] N) (h₁ h₂ h₃) :
((⟨f, h₁, h₂, h₃⟩ : M →ₗ⁅R,L⁆ N) : M →ₗ[R] N) = ⟨f, h₁, h₂⟩ :=
by { ext, refl, }
/-- The composition of Lie module morphisms is a morphism. -/
def comp (f : N →ₗ⁅R,L⁆ P) (g : M →ₗ⁅R,L⁆ N) : M →ₗ⁅R,L⁆ P :=
{ map_lie' := λ x m, by { change f (g ⁅x, m⁆) = ⁅x, f (g m)⁆, rw [map_lie, map_lie], },
..linear_map.comp f.to_linear_map g.to_linear_map }
lemma comp_apply (f : N →ₗ⁅R,L⁆ P) (g : M →ₗ⁅R,L⁆ N) (m : M) :
f.comp g m = f (g m) := rfl
@[norm_cast, simp] lemma coe_comp (f : N →ₗ⁅R,L⁆ P) (g : M →ₗ⁅R,L⁆ N) :
(f.comp g : M → P) = f ∘ g :=
rfl
@[norm_cast, simp] lemma coe_linear_map_comp (f : N →ₗ⁅R,L⁆ P) (g : M →ₗ⁅R,L⁆ N) :
(f.comp g : M →ₗ[R] P) = (f : N →ₗ[R] P).comp (g : M →ₗ[R] N) :=
rfl
/-- The inverse of a bijective morphism of Lie modules is a morphism of Lie modules. -/
def inverse (f : M →ₗ⁅R,L⁆ N) (g : N → M)
(h₁ : function.left_inverse g f) (h₂ : function.right_inverse g f) : N →ₗ⁅R,L⁆ M :=
{ map_lie' := λ x n,
calc g ⁅x, n⁆ = g ⁅x, f (g n)⁆ : by rw h₂
... = g (f ⁅x, g n⁆) : by rw map_lie
... = ⁅x, g n⁆ : (h₁ _),
..linear_map.inverse f.to_linear_map g h₁ h₂ }
instance : has_add (M →ₗ⁅R,L⁆ N) :=
{ add := λ f g, { map_lie' := by simp, ..((f : M →ₗ[R] N) + (g : M →ₗ[R] N)) }, }
instance : has_sub (M →ₗ⁅R,L⁆ N) :=
{ sub := λ f g, { map_lie' := by simp, ..((f : M →ₗ[R] N) - (g : M →ₗ[R] N)) }, }
instance : has_neg (M →ₗ⁅R,L⁆ N) :=
{ neg := λ f, { map_lie' := by simp, ..(-(f : (M →ₗ[R] N))) }, }
@[norm_cast, simp] lemma coe_add (f g : M →ₗ⁅R,L⁆ N) : ⇑(f + g) = f + g := rfl
lemma add_apply (f g : M →ₗ⁅R,L⁆ N) (m : M) : (f + g) m = f m + g m := rfl
@[norm_cast, simp] lemma coe_sub (f g : M →ₗ⁅R,L⁆ N) : ⇑(f - g) = f - g := rfl
lemma sub_apply (f g : M →ₗ⁅R,L⁆ N) (m : M) : (f - g) m = f m - g m := rfl
@[norm_cast, simp] lemma coe_neg (f : M →ₗ⁅R,L⁆ N) : ⇑(-f) = -f := rfl
lemma neg_apply (f : M →ₗ⁅R,L⁆ N) (m : M) : (-f) m = -(f m) := rfl
instance : add_comm_group (M →ₗ⁅R,L⁆ N) :=
{ zero := 0,
add := (+),
neg := has_neg.neg,
sub := has_sub.sub,
nsmul := λ n f, { map_lie' := λ x m, by simp, ..(n • (f : M →ₗ[R] N)) },
nsmul_zero' := λ f, by { ext, simp, },
nsmul_succ' := λ n f, by { ext, simp [nat.succ_eq_one_add, add_nsmul], },
..(coe_injective.add_comm_group _ coe_zero coe_add coe_neg coe_sub :
add_comm_group (M →ₗ⁅R,L⁆ N)) }
instance : has_scalar R (M →ₗ⁅R,L⁆ N) :=
{ smul := λ t f, { map_lie' := by simp, ..(t • (f : M →ₗ[R] N)) }, }
@[norm_cast, simp] lemma coe_smul (t : R) (f : M →ₗ⁅R,L⁆ N) : ⇑(t • f) = t • f := rfl
lemma smul_apply (t : R) (f : M →ₗ⁅R,L⁆ N) (m : M) : (t • f) m = t • (f m) := rfl
instance : module R (M →ₗ⁅R,L⁆ N) :=
function.injective.module R ⟨to_fun, rfl, coe_add⟩ coe_injective coe_smul
end lie_module_hom
/-- An equivalence of Lie algebra modules is a linear equivalence which is also a morphism of
Lie algebra modules. -/
structure lie_module_equiv extends M ≃ₗ[R] N, M →ₗ⁅R,L⁆ N, M ≃ N
attribute [nolint doc_blame] lie_module_equiv.to_equiv
attribute [nolint doc_blame] lie_module_equiv.to_lie_module_hom
attribute [nolint doc_blame] lie_module_equiv.to_linear_equiv
notation M ` ≃ₗ⁅`:25 R,L:25 `⁆ `:0 N:0 := lie_module_equiv R L M N
namespace lie_module_equiv
variables {R L M N P}
instance has_coe_to_equiv : has_coe (M ≃ₗ⁅R,L⁆ N) (M ≃ N) := ⟨to_equiv⟩
instance has_coe_to_lie_module_hom : has_coe (M ≃ₗ⁅R,L⁆ N) (M →ₗ⁅R,L⁆ N) := ⟨to_lie_module_hom⟩
instance has_coe_to_linear_equiv : has_coe (M ≃ₗ⁅R,L⁆ N) (M ≃ₗ[R] N) := ⟨to_linear_equiv⟩
/-- see Note [function coercion] -/
instance : has_coe_to_fun (M ≃ₗ⁅R,L⁆ N) := ⟨_, to_fun⟩
@[simp] lemma coe_mk (f : M → N) (h₁ h₂ F h₃ h₄ h₅) :
((⟨f, h₁, h₂, F, h₃, h₄, h₅⟩ : M ≃ₗ⁅R,L⁆ N) : M → N) = f := rfl
@[simp, norm_cast] lemma coe_to_lie_module_hom (e : M ≃ₗ⁅R,L⁆ N) :
((e : M →ₗ⁅R,L⁆ N) : M → N) = e := rfl
@[simp, norm_cast] lemma coe_to_linear_equiv (e : M ≃ₗ⁅R,L⁆ N) : ((e : M ≃ₗ[R] N) : M → N) = e :=
rfl
lemma to_equiv_injective : function.injective (to_equiv : (M ≃ₗ⁅R,L⁆ N) → M ≃ N) :=
λ ⟨_, _, _, _, _, _, _⟩ ⟨_, _, _, _, _, _, _⟩ h, lie_module_equiv.mk.inj_eq.mpr (equiv.mk.inj h)
@[ext] lemma ext (e₁ e₂ : M ≃ₗ⁅R,L⁆ N) (h : ∀ m, e₁ m = e₂ m) : e₁ = e₂ :=
to_equiv_injective (equiv.ext h)
instance : has_one (M ≃ₗ⁅R,L⁆ M) := ⟨{ map_lie' := λ x m, rfl, ..(1 : M ≃ₗ[R] M) }⟩
@[simp] lemma one_apply (m : M) : (1 : (M ≃ₗ⁅R,L⁆ M)) m = m := rfl
instance : inhabited (M ≃ₗ⁅R,L⁆ M) := ⟨1⟩
/-- Lie module equivalences are reflexive. -/
@[refl] def refl : M ≃ₗ⁅R,L⁆ M := 1
@[simp] lemma refl_apply (m : M) : (refl : M ≃ₗ⁅R,L⁆ M) m = m := rfl
/-- Lie module equivalences are syemmtric. -/
@[symm] def symm (e : M ≃ₗ⁅R,L⁆ N) : N ≃ₗ⁅R,L⁆ M :=
{ ..lie_module_hom.inverse e.to_lie_module_hom e.inv_fun e.left_inv e.right_inv,
..(e : M ≃ₗ[R] N).symm }
@[simp] lemma symm_symm (e : M ≃ₗ⁅R,L⁆ N) : e.symm.symm = e :=
by { cases e, refl, }
@[simp] lemma apply_symm_apply (e : M ≃ₗ⁅R,L⁆ N) : ∀ x, e (e.symm x) = x :=
e.to_linear_equiv.apply_symm_apply
@[simp] lemma symm_apply_apply (e : M ≃ₗ⁅R,L⁆ N) : ∀ x, e.symm (e x) = x :=
e.to_linear_equiv.symm_apply_apply
/-- Lie module equivalences are transitive. -/
@[trans] def trans (e₁ : M ≃ₗ⁅R,L⁆ N) (e₂ : N ≃ₗ⁅R,L⁆ P) : M ≃ₗ⁅R,L⁆ P :=
{ ..lie_module_hom.comp e₂.to_lie_module_hom e₁.to_lie_module_hom,
..linear_equiv.trans e₁.to_linear_equiv e₂.to_linear_equiv }
@[simp] lemma trans_apply (e₁ : M ≃ₗ⁅R,L⁆ N) (e₂ : N ≃ₗ⁅R,L⁆ P) (m : M) :
(e₁.trans e₂) m = e₂ (e₁ m) := rfl
@[simp] lemma symm_trans_apply (e₁ : M ≃ₗ⁅R,L⁆ N) (e₂ : N ≃ₗ⁅R,L⁆ P) (p : P) :
(e₁.trans e₂).symm p = e₁.symm (e₂.symm p) := rfl
end lie_module_equiv
end lie_module_morphisms
|
fdc50df8457150f2526d9cdfd360bf0eef0b6d1c | dc253be9829b840f15d96d986e0c13520b085033 | /archive/smash_old.hlean | c4ed1a03edd374bf735befaf34fec2bba59b2aad | [
"Apache-2.0"
] | permissive | cmu-phil/Spectral | 4ce68e5c1ef2a812ffda5260e9f09f41b85ae0ea | 3b078f5f1de251637decf04bd3fc8aa01930a6b3 | refs/heads/master | 1,685,119,195,535 | 1,684,169,772,000 | 1,684,169,772,000 | 46,450,197 | 42 | 13 | null | 1,505,516,767,000 | 1,447,883,921,000 | Lean | UTF-8 | Lean | false | false | 8,316 | hlean | /- below are some old tries to compute (A ∧ S¹) directly -/
exit
/- smash A S¹ = red_susp A -/
definition red_susp_of_smash_pcircle [unfold 2] (x : smash A S¹*) : red_susp A :=
begin
induction x using smash.elim,
{ induction b, exact base, exact equator a },
{ exact base },
{ exact base },
{ reflexivity },
{ exact circle_elim_constant equator_pt b }
end
definition smash_pcircle_of_red_susp [unfold 2] (x : red_susp A) : smash A S¹* :=
begin
induction x,
{ exact pt },
{ exact gluel' pt a ⬝ ap (smash.mk a) loop ⬝ gluel' a pt },
{ refine !con.right_inv ◾ _ ◾ !con.right_inv,
exact ap_is_constant gluer loop ⬝ !con.right_inv }
end
definition smash_pcircle_of_red_susp_of_smash_pcircle_pt [unfold 3] (a : A) (x : S¹*) :
smash_pcircle_of_red_susp (red_susp_of_smash_pcircle (smash.mk a x)) = smash.mk a x :=
begin
induction x,
{ exact gluel' pt a },
{ exact abstract begin apply eq_pathover,
refine ap_compose smash_pcircle_of_red_susp _ _ ⬝ph _,
refine ap02 _ (elim_loop pt (equator a)) ⬝ !elim_equator ⬝ph _,
-- make everything below this a lemma defined by path induction?
refine !con_idp⁻¹ ⬝pv _, refine !con.assoc⁻¹ ⬝ph _, apply whisker_bl, apply whisker_lb,
apply whisker_tl, apply hrfl end end }
end
definition concat2o [unfold 10] {A B : Type} {f g h : A → B} {q : f ~ g} {r : g ~ h} {a a' : A}
{p : a = a'} (s : q a =[p] q a') (t : r a =[p] r a') : q a ⬝ r a =[p] q a' ⬝ r a' :=
by induction p; exact idpo
definition apd_con_fn [unfold 10] {A B : Type} {f g h : A → B} {q : f ~ g} {r : g ~ h} {a a' : A}
(p : a = a') : apd (λa, q a ⬝ r a) p = concat2o (apd q p) (apd r p) :=
by induction p; reflexivity
-- definition apd_con_fn_constant [unfold 10] {A B : Type} {f : A → B} {b b' : B} {q : Πa, f a = b}
-- {r : b = b'} {a a' : A} (p : a = a') :
-- apd (λa, q a ⬝ r) p = concat2o (apd q p) (pathover_of_eq _ idp) :=
-- by induction p; reflexivity
definition smash_pcircle_pequiv_red [constructor] (A : Type*) : smash A S¹* ≃* red_susp A :=
begin
fapply pequiv_of_equiv,
{ fapply equiv.MK,
{ exact red_susp_of_smash_pcircle },
{ exact smash_pcircle_of_red_susp },
{ exact abstract begin intro x, induction x,
{ reflexivity },
{ apply eq_pathover, apply hdeg_square,
refine ap_compose red_susp_of_smash_pcircle _ _ ⬝ ap02 _ !elim_equator ⬝ _ ⬝ !ap_id⁻¹,
refine !ap_con ⬝ (!ap_con ⬝ !elim_gluel' ◾ !ap_compose'⁻¹) ◾ !elim_gluel' ⬝ _,
esimp, exact !idp_con ⬝ !elim_loop },
{ exact sorry } end end },
{ intro x, induction x,
{ exact smash_pcircle_of_red_susp_of_smash_pcircle_pt a b },
{ exact gluel pt },
{ exact gluer pt },
{ apply eq_pathover_id_right,
refine ap_compose smash_pcircle_of_red_susp _ _ ⬝ph _,
unfold [red_susp_of_smash_pcircle],
refine ap02 _ !elim_gluel ⬝ph _,
esimp, apply whisker_rt, exact vrfl },
{ apply eq_pathover_id_right,
refine ap_compose smash_pcircle_of_red_susp _ _ ⬝ph _,
unfold [red_susp_of_smash_pcircle],
-- not sure why so many implicit arguments are needed here...
refine ap02 _ (@smash.elim_gluer A S¹* _ (λa, circle.elim red_susp.base (equator a)) red_susp.base red_susp.base (λa, refl red_susp.base) (circle_elim_constant equator_pt) b) ⬝ph _,
apply square_of_eq, induction b,
{ exact whisker_right _ !con.right_inv },
{ apply eq_pathover_dep, refine !apd_con_fn ⬝pho _ ⬝hop !apd_con_fn⁻¹,
refine ap (λx, concat2o x _) !rec_loop ⬝pho _ ⬝hop (ap011 concat2o (apd_compose1 (λa b, ap smash_pcircle_of_red_susp b) (circle_elim_constant equator_pt) loop) !apd_constant')⁻¹,
exact sorry }
}}},
{ reflexivity }
end
/- smash A S¹ = susp A -/
open susp
definition susp_of_smash_pcircle [unfold 2] (x : smash A S¹*) : susp A :=
begin
induction x using smash.elim,
{ induction b, exact pt, exact merid a ⬝ (merid pt)⁻¹ },
{ exact pt },
{ exact pt },
{ reflexivity },
{ induction b, reflexivity, apply eq_pathover_constant_right, apply hdeg_square,
exact !elim_loop ⬝ !con.right_inv }
end
definition smash_pcircle_of_susp [unfold 2] (x : susp A) : smash A S¹* :=
begin
induction x,
{ exact pt },
{ exact pt },
{ exact gluel' pt a ⬝ (ap (smash.mk a) loop ⬝ gluel' a pt) },
end
-- the definitions below compile, but take a long time to do so and have sorry's in them
definition smash_pcircle_of_susp_of_smash_pcircle_pt [unfold 3] (a : A) (x : S¹*) :
smash_pcircle_of_susp (susp_of_smash_pcircle (smash.mk a x)) = smash.mk a x :=
begin
induction x,
{ exact gluel' pt a },
{ exact abstract begin apply eq_pathover,
refine ap_compose smash_pcircle_of_susp _ _ ⬝ph _,
refine ap02 _ (elim_loop north (merid a ⬝ (merid pt)⁻¹)) ⬝ph _,
refine !ap_con ⬝ (!elim_merid ◾ (!ap_inv ⬝ !elim_merid⁻²)) ⬝ph _,
-- make everything below this a lemma defined by path induction?
exact sorry,
-- refine !con_idp⁻¹ ⬝pv _, apply whisker_tl, refine !con.assoc⁻¹ ⬝ph _,
-- apply whisker_bl, apply whisker_lb,
-- refine !con_idp⁻¹ ⬝pv _, apply whisker_tl, apply hrfl
-- refine !con_idp⁻¹ ⬝pv _, apply whisker_tl,
-- refine !con.assoc⁻¹ ⬝ph _, apply whisker_bl, apply whisker_lb, apply hrfl
-- apply square_of_eq, rewrite [+con.assoc], apply whisker_left, apply whisker_left,
-- symmetry, apply con_eq_of_eq_inv_con, esimp, apply con_eq_of_eq_con_inv,
-- refine _⁻² ⬝ !con_inv, refine _ ⬝ !con.assoc,
-- refine _ ⬝ whisker_right _ !inv_con_cancel_right⁻¹, refine _ ⬝ !con.right_inv⁻¹,
-- refine !con.right_inv ◾ _, refine _ ◾ !con.right_inv,
-- refine !ap_mk_right ⬝ !con.right_inv
end end }
end
-- definition smash_pcircle_of_susp_of_smash_pcircle_gluer_base (b : S¹*)
-- : square (smash_pcircle_of_susp_of_smash_pcircle_pt (Point A) b)
-- (gluer pt)
-- (ap smash_pcircle_of_susp (ap (λ a, susp_of_smash_pcircle a) (gluer b)))
-- (gluer b) :=
-- begin
-- refine ap02 _ !elim_gluer ⬝ph _,
-- induction b,
-- { apply square_of_eq, exact whisker_right _ !con.right_inv },
-- { apply square_pathover', exact sorry }
-- end
exit
definition smash_pcircle_pequiv [constructor] (A : Type*) : smash A S¹* ≃* susp A :=
begin
fapply pequiv_of_equiv,
{ fapply equiv.MK,
{ exact susp_of_smash_pcircle },
{ exact smash_pcircle_of_susp },
{ exact abstract begin intro x, induction x,
{ reflexivity },
{ exact merid pt },
{ apply eq_pathover_id_right,
refine ap_compose susp_of_smash_pcircle _ _ ⬝ph _,
refine ap02 _ !elim_merid ⬝ph _,
rewrite [↑gluel', +ap_con, +ap_inv, -ap_compose'],
refine (_ ◾ _⁻² ◾ _ ◾ (_ ◾ _⁻²)) ⬝ph _,
rotate 5, do 2 (unfold [susp_of_smash_pcircle]; apply elim_gluel),
esimp, apply elim_loop, do 2 (unfold [susp_of_smash_pcircle]; apply elim_gluel),
refine idp_con (merid a ⬝ (merid (Point A))⁻¹) ⬝ph _,
apply square_of_eq, refine !idp_con ⬝ _⁻¹, apply inv_con_cancel_right } end end },
{ intro x, induction x using smash.rec,
{ exact smash_pcircle_of_susp_of_smash_pcircle_pt a b },
{ exact gluel pt },
{ exact gluer pt },
{ apply eq_pathover_id_right,
refine ap_compose smash_pcircle_of_susp _ _ ⬝ph _,
unfold [susp_of_smash_pcircle],
refine ap02 _ !elim_gluel ⬝ph _,
esimp, apply whisker_rt, exact vrfl },
{ apply eq_pathover_id_right,
refine ap_compose smash_pcircle_of_susp _ _ ⬝ph _,
unfold [susp_of_smash_pcircle],
refine ap02 _ !elim_gluer ⬝ph _,
induction b,
{ apply square_of_eq, exact whisker_right _ !con.right_inv },
{ exact sorry}
}}},
{ reflexivity }
end
end smash
|
0f99af408fa3989731c98fef1cea78e7375fbc0f | a4673261e60b025e2c8c825dfa4ab9108246c32e | /src/Lean/ProjFns.lean | 5f23e2dcf01341da97f1f72c880b027f43226dee | [
"Apache-2.0"
] | permissive | jcommelin/lean4 | c02dec0cc32c4bccab009285475f265f17d73228 | 2909313475588cc20ac0436e55548a4502050d0a | refs/heads/master | 1,674,129,550,893 | 1,606,415,348,000 | 1,606,415,348,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,378 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Environment
namespace Lean
/- Given a structure `S`, Lean automatically creates an auxiliary definition (projection function)
for each field. This structure caches information about these auxiliary definitions. -/
structure ProjectionFunctionInfo :=
(ctorName : Name) -- Constructor associated with the auxiliary projection function.
(nparams : Nat) -- Number of parameters in the structure
(i : Nat) -- The field index associated with the auxiliary projection function.
(fromClass : Bool) -- `true` if the structure is a class
@[export lean_mk_projection_info]
def mkProjectionInfoEx (ctorName : Name) (nparams : Nat) (i : Nat) (fromClass : Bool) : ProjectionFunctionInfo :=
{ctorName := ctorName, nparams := nparams, i := i, fromClass := fromClass }
@[export lean_projection_info_from_class]
def ProjectionFunctionInfo.fromClassEx (info : ProjectionFunctionInfo) : Bool := info.fromClass
instance : Inhabited ProjectionFunctionInfo :=
⟨{ ctorName := arbitrary, nparams := arbitrary, i := 0, fromClass := false }⟩
builtin_initialize projectionFnInfoExt : SimplePersistentEnvExtension (Name × ProjectionFunctionInfo) (NameMap ProjectionFunctionInfo) ←
registerSimplePersistentEnvExtension {
name := `projinfo,
addImportedFn := fun as => {},
addEntryFn := fun s p => s.insert p.1 p.2,
toArrayFn := fun es => es.toArray.qsort (fun a b => Name.quickLt a.1 b.1)
}
@[export lean_add_projection_info]
def addProjectionFnInfo (env : Environment) (projName : Name) (ctorName : Name) (nparams : Nat) (i : Nat) (fromClass : Bool) : Environment :=
projectionFnInfoExt.addEntry env (projName, { ctorName := ctorName, nparams := nparams, i := i, fromClass := fromClass })
namespace Environment
@[export lean_get_projection_info]
def getProjectionFnInfo? (env : Environment) (projName : Name) : Option ProjectionFunctionInfo :=
match env.getModuleIdxFor? projName with
| some modIdx =>
match (projectionFnInfoExt.getModuleEntries env modIdx).binSearch (projName, arbitrary) (fun a b => Name.quickLt a.1 b.1) with
| some e => some e.2
| none => none
| none => (projectionFnInfoExt.getState env).find? projName
def isProjectionFn (env : Environment) (n : Name) : Bool :=
match env.getModuleIdxFor? n with
| some modIdx => (projectionFnInfoExt.getModuleEntries env modIdx).binSearchContains (n, arbitrary) (fun a b => Name.quickLt a.1 b.1)
| none => (projectionFnInfoExt.getState env).contains n
/-- If `projName` is the name of a projection function, return the associated structure name -/
def getProjectionStructureName? (env : Environment) (projName : Name) : Option Name :=
match env.getProjectionFnInfo? projName with
| none => none
| some projInfo =>
match env.find? projInfo.ctorName with
| some (ConstantInfo.ctorInfo val) => some val.induct
| _ => none
end Environment
def isProjectionFn [MonadEnv m] [Monad m] (declName : Name) : m Bool :=
return (← getEnv).isProjectionFn declName
def getProjectionFnInfo? [MonadEnv m] [Monad m] (declName : Name) : m (Option ProjectionFunctionInfo) :=
return (← getEnv).getProjectionFnInfo? declName
end Lean
|
9b5b1a9a59f0faf08ce21d92a78474fe3a9ec7cf | 63abd62053d479eae5abf4951554e1064a4c45b4 | /src/topology/algebra/group_completion.lean | 0497ac2da0f704f7cfe14d22fdb8001887b17070 | [
"Apache-2.0"
] | permissive | Lix0120/mathlib | 0020745240315ed0e517cbf32e738d8f9811dd80 | e14c37827456fc6707f31b4d1d16f1f3a3205e91 | refs/heads/master | 1,673,102,855,024 | 1,604,151,044,000 | 1,604,151,044,000 | 308,930,245 | 0 | 0 | Apache-2.0 | 1,604,164,710,000 | 1,604,163,547,000 | null | UTF-8 | Lean | false | false | 4,142 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Johannes Hölzl
Completion of topological groups:
-/
import topology.uniform_space.completion
import topology.algebra.uniform_group
noncomputable theory
universes u v
section group
open uniform_space Cauchy filter set
variables {α : Type u} [uniform_space α]
instance [has_zero α] : has_zero (completion α) := ⟨(0 : α)⟩
instance [has_neg α] : has_neg (completion α) := ⟨completion.map (λa, -a : α → α)⟩
instance [has_add α] : has_add (completion α) := ⟨completion.map₂ (+)⟩
-- TODO: switch sides once #1103 is fixed
@[norm_cast]
lemma uniform_space.completion.coe_zero [has_zero α] : ((0 : α) : completion α) = 0 := rfl
end group
namespace uniform_space.completion
section uniform_add_group
open uniform_space uniform_space.completion
variables {α : Type*} [uniform_space α] [add_group α] [uniform_add_group α]
@[norm_cast]
lemma coe_neg (a : α) : ((- a : α) : completion α) = - a :=
(map_coe uniform_continuous_neg a).symm
@[norm_cast]
lemma coe_add (a b : α) : ((a + b : α) : completion α) = a + b :=
(map₂_coe_coe a b (+) uniform_continuous_add).symm
instance : add_group (completion α) :=
{ zero_add := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ continuous_const continuous_id) continuous_id)
(assume a, show 0 + (a : completion α) = a, by rw_mod_cast zero_add),
add_zero := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ continuous_id continuous_const) continuous_id)
(assume a, show (a : completion α) + 0 = a, by rw_mod_cast add_zero),
add_left_neg := assume a, completion.induction_on a
(is_closed_eq (continuous_map₂ completion.continuous_map continuous_id) continuous_const)
(assume a, show - (a : completion α) + a = 0, by { rw_mod_cast add_left_neg, refl }),
add_assoc := assume a b c, completion.induction_on₃ a b c
(is_closed_eq
(continuous_map₂
(continuous_map₂ continuous_fst (continuous_fst.comp continuous_snd)) (continuous_snd.comp continuous_snd))
(continuous_map₂ continuous_fst
(continuous_map₂ (continuous_fst.comp continuous_snd) (continuous_snd.comp continuous_snd))))
(assume a b c, show (a : completion α) + b + c = a + (b + c),
by repeat { rw_mod_cast add_assoc }),
.. completion.has_zero, .. completion.has_neg, ..completion.has_add }
instance : uniform_add_group (completion α) :=
⟨(uniform_continuous_map₂ (+)).bicompl uniform_continuous_id uniform_continuous_map⟩
instance is_add_group_hom_coe : is_add_group_hom (coe : α → completion α) :=
{ map_add := coe_add }
variables {β : Type v} [uniform_space β] [add_group β] [uniform_add_group β]
lemma is_add_group_hom_extension [complete_space β] [separated_space β]
{f : α → β} [is_add_group_hom f] (hf : continuous f) : is_add_group_hom (completion.extension f) :=
have hf : uniform_continuous f, from uniform_continuous_of_continuous hf,
{ map_add := assume a b, completion.induction_on₂ a b
(is_closed_eq
(continuous_extension.comp continuous_add)
((continuous_extension.comp continuous_fst).add (continuous_extension.comp continuous_snd)))
(assume a b, by rw_mod_cast [extension_coe hf, extension_coe hf, extension_coe hf, is_add_hom.map_add f]) }
lemma is_add_group_hom_map
{f : α → β} [is_add_group_hom f] (hf : continuous f) : is_add_group_hom (completion.map f) :=
@is_add_group_hom_extension _ _ _ _ _ _ _ _ _ _ _ (is_add_group_hom.comp _ _)
((continuous_coe _).comp hf)
instance {α : Type u} [uniform_space α] [add_comm_group α] [uniform_add_group α] : add_comm_group (completion α) :=
{ add_comm := assume a b, completion.induction_on₂ a b
(is_closed_eq (continuous_map₂ continuous_fst continuous_snd) (continuous_map₂ continuous_snd continuous_fst))
(assume x y, by { change ↑x + ↑y = ↑y + ↑x, rw [← coe_add, ← coe_add, add_comm]}),
.. completion.add_group }
end uniform_add_group
end uniform_space.completion
|
8467beba3b3c6d89c41304d613a0250f15561582 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/analysis/calculus/local_extr_auto.lean | 77d8ec71ef6a09ecad376b3467c4167ed104cd96 | [] | 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 | 14,137 | lean | /-
Copyright (c) 2019 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.local_extr
import Mathlib.analysis.calculus.deriv
import Mathlib.PostPort
universes u
namespace Mathlib
/-!
# Local extrema of smooth functions
## Main definitions
In a real normed space `E` we define `pos_tangent_cone_at (s : set E) (x : E)`.
This would be the same as `tangent_cone_at ℝ≥0 s x` if we had a theory of normed semifields.
This set is used in the proof of Fermat's Theorem (see below), and can be used to formalize
[Lagrange multipliers](https://en.wikipedia.org/wiki/Lagrange_multiplier) and/or
[Karush–Kuhn–Tucker conditions](https://en.wikipedia.org/wiki/Karush–Kuhn–Tucker_conditions).
## Main statements
For each theorem name listed below, we also prove similar theorems for `min`, `extr` (if applicable)`,
and `(f)deriv` instead of `has_fderiv`.
* `is_local_max_on.has_fderiv_within_at_nonpos` : `f' y ≤ 0` whenever `a` is a local maximum
of `f` on `s`, `f` has derivative `f'` at `a` within `s`, and `y` belongs to the positive tangent
cone of `s` at `a`.
* `is_local_max_on.has_fderiv_within_at_eq_zero` : In the settings of the previous theorem, if both
`y` and `-y` belong to the positive tangent cone, then `f' y = 0`.
* `is_local_max.has_fderiv_at_eq_zero` :
[Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points)),
the derivative of a differentiable function at a local extremum point equals zero.
* `exists_has_deriv_at_eq_zero` :
[Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem): given a function `f` continuous
on `[a, b]` and differentiable on `(a, b)`, there exists `c ∈ (a, b)` such that `f' c = 0`.
## Implementation notes
For each mathematical fact we prove several versions of its formalization:
* for maxima and minima;
* using `has_fderiv*`/`has_deriv*` or `fderiv*`/`deriv*`.
For the `fderiv*`/`deriv*` versions we omit the differentiability condition whenever it is possible
due to the fact that `fderiv` and `deriv` are defined to be zero for non-differentiable functions.
## References
* [Fermat's Theorem](https://en.wikipedia.org/wiki/Fermat's_theorem_(stationary_points));
* [Rolle's Theorem](https://en.wikipedia.org/wiki/Rolle's_theorem);
* [Tangent cone](https://en.wikipedia.org/wiki/Tangent_cone);
## Tags
local extremum, Fermat's Theorem, Rolle's Theorem
-/
/-- "Positive" tangent cone to `s` at `x`; the only difference from `tangent_cone_at`
is that we require `c n → ∞` instead of `∥c n∥ → ∞`. One can think about `pos_tangent_cone_at`
as `tangent_cone_at nnreal` but we have no theory of normed semifields yet. -/
def pos_tangent_cone_at {E : Type u} [normed_group E] [normed_space ℝ E] (s : set E) (x : E) :
set E :=
set_of
fun (y : E) =>
∃ (c : ℕ → ℝ),
∃ (d : ℕ → E),
filter.eventually (fun (n : ℕ) => x + d n ∈ s) filter.at_top ∧
filter.tendsto c filter.at_top filter.at_top ∧
filter.tendsto (fun (n : ℕ) => c n • d n) filter.at_top (nhds y)
theorem pos_tangent_cone_at_mono {E : Type u} [normed_group E] [normed_space ℝ E] {a : E} :
monotone fun (s : set E) => pos_tangent_cone_at s a :=
sorry
theorem mem_pos_tangent_cone_at_of_segment_subset {E : Type u} [normed_group E] [normed_space ℝ E]
{s : set E} {x : E} {y : E} (h : segment x y ⊆ s) : y - x ∈ pos_tangent_cone_at s x :=
sorry
theorem pos_tangent_cone_at_univ {E : Type u} [normed_group E] [normed_space ℝ E] {a : E} :
pos_tangent_cone_at set.univ a = set.univ :=
sorry
/-- If `f` has a local max on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and
`y` belongs to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/
theorem is_local_max_on.has_fderiv_within_at_nonpos {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {f' : continuous_linear_map ℝ E ℝ} {s : set E} (h : is_local_max_on f s a)
(hf : has_fderiv_within_at f f' s a) {y : E} (hy : y ∈ pos_tangent_cone_at s a) :
coe_fn f' y ≤ 0 :=
sorry
/-- If `f` has a local max on `s` at `a` and `y` belongs to the positive tangent cone
of `s` at `a`, then `f' y ≤ 0`. -/
theorem is_local_max_on.fderiv_within_nonpos {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {s : set E} (h : is_local_max_on f s a) {y : E}
(hy : y ∈ pos_tangent_cone_at s a) : coe_fn (fderiv_within ℝ f s a) y ≤ 0 :=
sorry
/-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and
both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/
theorem is_local_max_on.has_fderiv_within_at_eq_zero {E : Type u} [normed_group E]
[normed_space ℝ E] {f : E → ℝ} {a : E} {f' : continuous_linear_map ℝ E ℝ} {s : set E}
(h : is_local_max_on f s a) (hf : has_fderiv_within_at f f' s a) {y : E}
(hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : coe_fn f' y = 0 :=
sorry
/-- If `f` has a local max on `s` at `a` and both `y` and `-y` belong to the positive tangent cone
of `s` at `a`, then `f' y = 0`. -/
theorem is_local_max_on.fderiv_within_eq_zero {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {s : set E} (h : is_local_max_on f s a) {y : E}
(hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) :
coe_fn (fderiv_within ℝ f s a) y = 0 :=
sorry
/-- If `f` has a local min on `s` at `a`, `f'` is the derivative of `f` at `a` within `s`, and
`y` belongs to the positive tangent cone of `s` at `a`, then `0 ≤ f' y`. -/
theorem is_local_min_on.has_fderiv_within_at_nonneg {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {f' : continuous_linear_map ℝ E ℝ} {s : set E} (h : is_local_min_on f s a)
(hf : has_fderiv_within_at f f' s a) {y : E} (hy : y ∈ pos_tangent_cone_at s a) :
0 ≤ coe_fn f' y :=
sorry
/-- If `f` has a local min on `s` at `a` and `y` belongs to the positive tangent cone
of `s` at `a`, then `0 ≤ f' y`. -/
theorem is_local_min_on.fderiv_within_nonneg {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {s : set E} (h : is_local_min_on f s a) {y : E}
(hy : y ∈ pos_tangent_cone_at s a) : 0 ≤ coe_fn (fderiv_within ℝ f s a) y :=
sorry
/-- If `f` has a local max on `s` at `a`, `f'` is a derivative of `f` at `a` within `s`, and
both `y` and `-y` belong to the positive tangent cone of `s` at `a`, then `f' y ≤ 0`. -/
theorem is_local_min_on.has_fderiv_within_at_eq_zero {E : Type u} [normed_group E]
[normed_space ℝ E] {f : E → ℝ} {a : E} {f' : continuous_linear_map ℝ E ℝ} {s : set E}
(h : is_local_min_on f s a) (hf : has_fderiv_within_at f f' s a) {y : E}
(hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) : coe_fn f' y = 0 :=
sorry
/-- If `f` has a local min on `s` at `a` and both `y` and `-y` belong to the positive tangent cone
of `s` at `a`, then `f' y = 0`. -/
theorem is_local_min_on.fderiv_within_eq_zero {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {s : set E} (h : is_local_min_on f s a) {y : E}
(hy : y ∈ pos_tangent_cone_at s a) (hy' : -y ∈ pos_tangent_cone_at s a) :
coe_fn (fderiv_within ℝ f s a) y = 0 :=
sorry
/-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/
theorem is_local_min.has_fderiv_at_eq_zero {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {f' : continuous_linear_map ℝ E ℝ} (h : is_local_min f a)
(hf : has_fderiv_at f f' a) : f' = 0 :=
sorry
/-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/
theorem is_local_min.fderiv_eq_zero {E : Type u} [normed_group E] [normed_space ℝ E] {f : E → ℝ}
{a : E} (h : is_local_min f a) : fderiv ℝ f a = 0 :=
dite (differentiable_at ℝ f a)
(fun (hf : differentiable_at ℝ f a) =>
is_local_min.has_fderiv_at_eq_zero h (differentiable_at.has_fderiv_at hf))
fun (hf : ¬differentiable_at ℝ f a) => fderiv_zero_of_not_differentiable_at hf
/-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/
theorem is_local_max.has_fderiv_at_eq_zero {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {f' : continuous_linear_map ℝ E ℝ} (h : is_local_max f a)
(hf : has_fderiv_at f f' a) : f' = 0 :=
iff.mp neg_eq_zero
(is_local_min.has_fderiv_at_eq_zero (is_local_max.neg h) (has_fderiv_at.neg hf))
/-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/
theorem is_local_max.fderiv_eq_zero {E : Type u} [normed_group E] [normed_space ℝ E] {f : E → ℝ}
{a : E} (h : is_local_max f a) : fderiv ℝ f a = 0 :=
dite (differentiable_at ℝ f a)
(fun (hf : differentiable_at ℝ f a) =>
is_local_max.has_fderiv_at_eq_zero h (differentiable_at.has_fderiv_at hf))
fun (hf : ¬differentiable_at ℝ f a) => fderiv_zero_of_not_differentiable_at hf
/-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/
theorem is_local_extr.has_fderiv_at_eq_zero {E : Type u} [normed_group E] [normed_space ℝ E]
{f : E → ℝ} {a : E} {f' : continuous_linear_map ℝ E ℝ} (h : is_local_extr f a) :
has_fderiv_at f f' a → f' = 0 :=
is_local_extr.elim h is_local_min.has_fderiv_at_eq_zero is_local_max.has_fderiv_at_eq_zero
/-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/
theorem is_local_extr.fderiv_eq_zero {E : Type u} [normed_group E] [normed_space ℝ E] {f : E → ℝ}
{a : E} (h : is_local_extr f a) : fderiv ℝ f a = 0 :=
is_local_extr.elim h is_local_min.fderiv_eq_zero is_local_max.fderiv_eq_zero
/-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/
theorem is_local_min.has_deriv_at_eq_zero {f : ℝ → ℝ} {f' : ℝ} {a : ℝ} (h : is_local_min f a)
(hf : has_deriv_at f f' a) : f' = 0 :=
sorry
/-- Fermat's Theorem: the derivative of a function at a local minimum equals zero. -/
theorem is_local_min.deriv_eq_zero {f : ℝ → ℝ} {a : ℝ} (h : is_local_min f a) : deriv f a = 0 :=
dite (differentiable_at ℝ f a)
(fun (hf : differentiable_at ℝ f a) =>
is_local_min.has_deriv_at_eq_zero h (differentiable_at.has_deriv_at hf))
fun (hf : ¬differentiable_at ℝ f a) => deriv_zero_of_not_differentiable_at hf
/-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/
theorem is_local_max.has_deriv_at_eq_zero {f : ℝ → ℝ} {f' : ℝ} {a : ℝ} (h : is_local_max f a)
(hf : has_deriv_at f f' a) : f' = 0 :=
iff.mp neg_eq_zero (is_local_min.has_deriv_at_eq_zero (is_local_max.neg h) (has_deriv_at.neg hf))
/-- Fermat's Theorem: the derivative of a function at a local maximum equals zero. -/
theorem is_local_max.deriv_eq_zero {f : ℝ → ℝ} {a : ℝ} (h : is_local_max f a) : deriv f a = 0 :=
dite (differentiable_at ℝ f a)
(fun (hf : differentiable_at ℝ f a) =>
is_local_max.has_deriv_at_eq_zero h (differentiable_at.has_deriv_at hf))
fun (hf : ¬differentiable_at ℝ f a) => deriv_zero_of_not_differentiable_at hf
/-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/
theorem is_local_extr.has_deriv_at_eq_zero {f : ℝ → ℝ} {f' : ℝ} {a : ℝ} (h : is_local_extr f a) :
has_deriv_at f f' a → f' = 0 :=
is_local_extr.elim h is_local_min.has_deriv_at_eq_zero is_local_max.has_deriv_at_eq_zero
/-- Fermat's Theorem: the derivative of a function at a local extremum equals zero. -/
theorem is_local_extr.deriv_eq_zero {f : ℝ → ℝ} {a : ℝ} (h : is_local_extr f a) : deriv f a = 0 :=
is_local_extr.elim h is_local_min.deriv_eq_zero is_local_max.deriv_eq_zero
/-- A continuous function on a closed interval with `f a = f b` takes either its maximum
or its minimum value at a point in the interior of the interval. -/
theorem exists_Ioo_extr_on_Icc (f : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b)
(hfc : continuous_on f (set.Icc a b)) (hfI : f a = f b) :
∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), is_extr_on f (set.Icc a b) c :=
sorry
/-- A continuous function on a closed interval with `f a = f b` has a local extremum at some
point of the corresponding open interval. -/
theorem exists_local_extr_Ioo (f : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b)
(hfc : continuous_on f (set.Icc a b)) (hfI : f a = f b) :
∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), is_local_extr f c :=
sorry
/-- Rolle's Theorem `has_deriv_at` version -/
theorem exists_has_deriv_at_eq_zero (f : ℝ → ℝ) (f' : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b)
(hfc : continuous_on f (set.Icc a b)) (hfI : f a = f b)
(hff' : ∀ (x : ℝ), x ∈ set.Ioo a b → has_deriv_at f (f' x) x) :
∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), f' c = 0 :=
sorry
/-- Rolle's Theorem `deriv` version -/
theorem exists_deriv_eq_zero (f : ℝ → ℝ) {a : ℝ} {b : ℝ} (hab : a < b)
(hfc : continuous_on f (set.Icc a b)) (hfI : f a = f b) :
∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), deriv f c = 0 :=
sorry
theorem exists_has_deriv_at_eq_zero' {f : ℝ → ℝ} {f' : ℝ → ℝ} {a : ℝ} {b : ℝ} (hab : a < b) {l : ℝ}
(hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds l))
(hfb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds l))
(hff' : ∀ (x : ℝ), x ∈ set.Ioo a b → has_deriv_at f (f' x) x) :
∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), f' c = 0 :=
sorry
theorem exists_deriv_eq_zero' {f : ℝ → ℝ} {a : ℝ} {b : ℝ} (hab : a < b) {l : ℝ}
(hfa : filter.tendsto f (nhds_within a (set.Ioi a)) (nhds l))
(hfb : filter.tendsto f (nhds_within b (set.Iio b)) (nhds l)) :
∃ (c : ℝ), ∃ (H : c ∈ set.Ioo a b), deriv f c = 0 :=
sorry
end Mathlib |
69020b4291d2f67e00843c4cddd756e084aedd04 | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/algebra/pointwise.lean | c5eb76eefe19d7c4d1cc747343ec7fa7efbab386 | [
"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 | 35,127 | lean | /-
Copyright (c) 2019 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin, Floris van Doorn
-/
import algebra.module.basic
import data.set.finite
import group_theory.submonoid.basic
/-!
# Pointwise addition, multiplication, and scalar multiplication of sets.
This file defines pointwise algebraic operations on sets.
* For a type `α` with multiplication, multiplication is defined on `set α` by taking
`s * t` to be the set of all `x * y` where `x ∈ s` and `y ∈ t`. Similarly for addition.
* For `α` a semigroup, `set α` is a semigroup.
* If `α` is a (commutative) monoid, we define an alias `set_semiring α` for `set α`, which then
becomes a (commutative) semiring with union as addition and pointwise multiplication as
multiplication.
* For a type `β` with scalar multiplication by another type `α`, this
file defines a scalar multiplication of `set β` by `set α` and a separate scalar
multiplication of `set β` by `α`.
* We also define pointwise multiplication on `finset`.
Appropriate definitions and results are also transported to the additive theory via `to_additive`.
## Implementation notes
* The following expressions are considered in simp-normal form in a group:
`(λ h, h * g) ⁻¹' s`, `(λ h, g * h) ⁻¹' s`, `(λ h, h * g⁻¹) ⁻¹' s`, `(λ h, g⁻¹ * h) ⁻¹' s`,
`s * t`, `s⁻¹`, `(1 : set _)` (and similarly for additive variants).
Expressions equal to one of these will be simplified.
* We put all instances in the locale `pointwise`, so that these instances are not available by
default. Note that we do not mark them as reducible (as argued by note [reducible non-instances])
since we expect the locale to be open whenever the instances are actually used (and making the
instances reducible changes the behavior of `simp`).
## Tags
set multiplication, set addition, pointwise addition, pointwise multiplication
-/
namespace set
open function
variables {α : Type*} {β : Type*} {s s₁ s₂ t t₁ t₂ u : set α} {a b : α} {x y : β}
/-! ### Properties about 1 -/
/-- The set `(1 : set α)` is defined as `{1}` in locale `pointwise`. -/
@[to_additive
/-"The set `(0 : set α)` is defined as `{0}` in locale `pointwise`. "-/]
protected def has_one [has_one α] : has_one (set α) := ⟨{1}⟩
localized "attribute [instance] set.has_one set.has_zero" in pointwise
@[to_additive]
lemma singleton_one [has_one α] : ({1} : set α) = 1 := rfl
@[simp, to_additive]
lemma mem_one [has_one α] : a ∈ (1 : set α) ↔ a = 1 := iff.rfl
@[to_additive]
lemma one_mem_one [has_one α] : (1 : α) ∈ (1 : set α) := eq.refl _
@[simp, to_additive]
theorem one_subset [has_one α] : 1 ⊆ s ↔ (1 : α) ∈ s := singleton_subset_iff
@[to_additive]
theorem one_nonempty [has_one α] : (1 : set α).nonempty := ⟨1, rfl⟩
@[simp, to_additive]
theorem image_one [has_one α] {f : α → β} : f '' 1 = {f 1} := image_singleton
/-! ### Properties about multiplication -/
/-- The set `(s * t : set α)` is defined as `{x * y | x ∈ s, y ∈ t}` in locale `pointwise`. -/
@[to_additive
/-" The set `(s + t : set α)` is defined as `{x + y | x ∈ s, y ∈ t}` in locale `pointwise`."-/]
protected def has_mul [has_mul α] : has_mul (set α) := ⟨image2 has_mul.mul⟩
localized "attribute [instance] set.has_mul set.has_add" in pointwise
@[simp, to_additive]
lemma image2_mul [has_mul α] : image2 has_mul.mul s t = s * t := rfl
@[to_additive]
lemma mem_mul [has_mul α] : a ∈ s * t ↔ ∃ x y, x ∈ s ∧ y ∈ t ∧ x * y = a := iff.rfl
@[to_additive]
lemma mul_mem_mul [has_mul α] (ha : a ∈ s) (hb : b ∈ t) : a * b ∈ s * t := mem_image2_of_mem ha hb
@[to_additive add_image_prod]
lemma image_mul_prod [has_mul α] : (λ x : α × α, x.fst * x.snd) '' s.prod t = s * t := image_prod _
@[simp, to_additive]
lemma image_mul_left [group α] : (λ b, a * b) '' t = (λ b, a⁻¹ * b) ⁻¹' t :=
by { rw image_eq_preimage_of_inverse; intro c; simp }
@[simp, to_additive]
lemma image_mul_right [group α] : (λ a, a * b) '' t = (λ a, a * b⁻¹) ⁻¹' t :=
by { rw image_eq_preimage_of_inverse; intro c; simp }
@[to_additive]
lemma image_mul_left' [group α] : (λ b, a⁻¹ * b) '' t = (λ b, a * b) ⁻¹' t := by simp
@[to_additive]
lemma image_mul_right' [group α] : (λ a, a * b⁻¹) '' t = (λ a, a * b) ⁻¹' t := by simp
@[simp, to_additive]
lemma preimage_mul_left_singleton [group α] : ((*) a) ⁻¹' {b} = {a⁻¹ * b} :=
by rw [← image_mul_left', image_singleton]
@[simp, to_additive]
lemma preimage_mul_right_singleton [group α] : (* a) ⁻¹' {b} = {b * a⁻¹} :=
by rw [← image_mul_right', image_singleton]
@[simp, to_additive]
lemma preimage_mul_left_one [group α] : (λ b, a * b) ⁻¹' 1 = {a⁻¹} :=
by rw [← image_mul_left', image_one, mul_one]
@[simp, to_additive]
lemma preimage_mul_right_one [group α] : (λ a, a * b) ⁻¹' 1 = {b⁻¹} :=
by rw [← image_mul_right', image_one, one_mul]
@[to_additive]
lemma preimage_mul_left_one' [group α] : (λ b, a⁻¹ * b) ⁻¹' 1 = {a} := by simp
@[to_additive]
lemma preimage_mul_right_one' [group α] : (λ a, a * b⁻¹) ⁻¹' 1 = {b} := by simp
@[simp, to_additive]
lemma mul_singleton [has_mul α] : s * {b} = (λ a, a * b) '' s := image2_singleton_right
@[simp, to_additive]
lemma singleton_mul [has_mul α] : {a} * t = (λ b, a * b) '' t := image2_singleton_left
@[simp, to_additive]
lemma singleton_mul_singleton [has_mul α] : ({a} : set α) * {b} = {a * b} := image2_singleton
@[to_additive]
protected lemma mul_comm [comm_semigroup α] : s * t = t * s :=
by simp only [← image2_mul, image2_swap _ s, mul_comm]
/-- `set α` is a `mul_one_class` under pointwise operations if `α` is. -/
@[to_additive /-"`set α` is an `add_zero_class` under pointwise operations if `α` is."-/]
protected def mul_one_class [mul_one_class α] : mul_one_class (set α) :=
{ mul_one := λ s, by { simp only [← singleton_one, mul_singleton, mul_one, image_id'] },
one_mul := λ s, by { simp only [← singleton_one, singleton_mul, one_mul, image_id'] },
..set.has_one, ..set.has_mul }
/-- `set α` is a `semigroup` under pointwise operations if `α` is. -/
@[to_additive /-"`set α` is an `add_semigroup` under pointwise operations if `α` is. "-/]
protected def semigroup [semigroup α] : semigroup (set α) :=
{ mul_assoc := λ _ _ _, image2_assoc mul_assoc,
..set.has_mul }
/-- `set α` is a `monoid` under pointwise operations if `α` is. -/
@[to_additive /-"`set α` is an `add_monoid` under pointwise operations if `α` is. "-/]
protected def monoid [monoid α] : monoid (set α) :=
{ ..set.semigroup,
..set.mul_one_class }
/-- `set α` is a `comm_monoid` under pointwise operations if `α` is. -/
@[to_additive /-"`set α` is an `add_comm_monoid` under pointwise operations if `α` is. "-/]
protected def comm_monoid [comm_monoid α] : comm_monoid (set α) :=
{ mul_comm := λ _ _, set.mul_comm, ..set.monoid }
localized "attribute [instance] set.mul_one_class set.add_zero_class set.semigroup set.add_semigroup
set.monoid set.add_monoid set.comm_monoid set.add_comm_monoid" in pointwise
lemma pow_mem_pow [monoid α] (ha : a ∈ s) (n : ℕ) :
a ^ n ∈ s ^ n :=
begin
induction n with n ih,
{ rw pow_zero,
exact set.mem_singleton 1 },
{ rw pow_succ,
exact set.mul_mem_mul ha ih },
end
/-- Under `[has_mul M]`, the `singleton` map from `M` to `set M` as a `mul_hom`, that is, a map
which preserves multiplication. -/
@[to_additive "Under `[has_add A]`, the `singleton` map from `A` to `set A` as an `add_hom`,
that is, a map which preserves addition.", simps]
def singleton_mul_hom [has_mul α] : mul_hom α (set α) :=
{ to_fun := singleton,
map_mul' := λ a b, singleton_mul_singleton.symm }
@[simp, to_additive]
lemma empty_mul [has_mul α] : ∅ * s = ∅ := image2_empty_left
@[simp, to_additive]
lemma mul_empty [has_mul α] : s * ∅ = ∅ := image2_empty_right
lemma empty_pow [monoid α] (n : ℕ) (hn : n ≠ 0) : (∅ : set α) ^ n = ∅ :=
by rw [← tsub_add_cancel_of_le (nat.succ_le_of_lt $ nat.pos_of_ne_zero hn), pow_succ, empty_mul]
instance decidable_mem_mul [monoid α] [fintype α] [decidable_eq α]
[decidable_pred (∈ s)] [decidable_pred (∈ t)] :
decidable_pred (∈ s * t) :=
λ _, decidable_of_iff _ mem_mul.symm
instance decidable_mem_pow [monoid α] [fintype α] [decidable_eq α]
[decidable_pred (∈ s)] (n : ℕ) :
decidable_pred (∈ (s ^ n)) :=
begin
induction n with n ih,
{ simp_rw [pow_zero, mem_one], apply_instance },
{ letI := ih, rw pow_succ, apply_instance }
end
@[to_additive]
lemma mul_subset_mul [has_mul α] (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ * s₂ ⊆ t₁ * t₂ :=
image2_subset h₁ h₂
lemma pow_subset_pow [monoid α] (hst : s ⊆ t) (n : ℕ) :
s ^ n ⊆ t ^ n :=
begin
induction n with n ih,
{ rw pow_zero,
exact subset.rfl },
{ rw [pow_succ, pow_succ],
exact mul_subset_mul hst ih },
end
@[to_additive]
lemma union_mul [has_mul α] : (s ∪ t) * u = (s * u) ∪ (t * u) := image2_union_left
@[to_additive]
lemma mul_union [has_mul α] : s * (t ∪ u) = (s * t) ∪ (s * u) := image2_union_right
@[to_additive]
lemma Union_mul_left_image [has_mul α] : (⋃ a ∈ s, (λ x, a * x) '' t) = s * t :=
Union_image_left _
@[to_additive]
lemma Union_mul_right_image [has_mul α] : (⋃ a ∈ t, (λ x, x * a) '' s) = s * t :=
Union_image_right _
@[to_additive]
lemma Union_mul {ι : Sort*} [has_mul α] (s : ι → set α) (t : set α) :
(⋃ i, s i) * t = ⋃ i, (s i * t) :=
image2_Union_left _ _ _
@[to_additive]
lemma mul_Union {ι : Sort*} [has_mul α] (t : set α) (s : ι → set α) :
t * (⋃ i, s i) = ⋃ i, (t * s i) :=
image2_Union_right _ _ _
@[simp, to_additive]
lemma univ_mul_univ [monoid α] : (univ : set α) * univ = univ :=
begin
have : ∀x, ∃a b : α, a * b = x := λx, ⟨x, ⟨1, mul_one x⟩⟩,
simpa only [mem_mul, eq_univ_iff_forall, mem_univ, true_and]
end
/-- `singleton` is a monoid hom. -/
@[to_additive singleton_add_hom "singleton is an add monoid hom"]
def singleton_hom [monoid α] : α →* set α :=
{ to_fun := singleton, map_one' := rfl, map_mul' := λ a b, singleton_mul_singleton.symm }
@[to_additive]
lemma nonempty.mul [has_mul α] : s.nonempty → t.nonempty → (s * t).nonempty := nonempty.image2
@[to_additive]
lemma finite.mul [has_mul α] (hs : finite s) (ht : finite t) : finite (s * t) :=
hs.image2 _ ht
/-- multiplication preserves finiteness -/
@[to_additive "addition preserves finiteness"]
def fintype_mul [has_mul α] [decidable_eq α] (s t : set α) [hs : fintype s] [ht : fintype t] :
fintype (s * t : set α) :=
set.fintype_image2 _ s t
@[to_additive]
lemma bdd_above_mul [ordered_comm_monoid α] {A B : set α} :
bdd_above A → bdd_above B → bdd_above (A * B) :=
begin
rintros ⟨bA, hbA⟩ ⟨bB, hbB⟩,
use bA * bB,
rintros x ⟨xa, xb, hxa, hxb, rfl⟩,
exact mul_le_mul' (hbA hxa) (hbB hxb),
end
section big_operators
open_locale big_operators
variables {ι : Type*} [comm_monoid α]
/-- The n-ary version of `set.mem_mul`. -/
@[to_additive /-" The n-ary version of `set.mem_add`. "-/]
lemma mem_finset_prod (t : finset ι) (f : ι → set α) (a : α) :
a ∈ ∏ i in t, f i ↔ ∃ (g : ι → α) (hg : ∀ {i}, i ∈ t → g i ∈ f i), ∏ i in t, g i = a :=
begin
classical,
induction t using finset.induction_on with i is hi ih generalizing a,
{ simp_rw [finset.prod_empty, set.mem_one],
exact ⟨λ h, ⟨λ i, a, λ i, false.elim, h.symm⟩, λ ⟨f, _, hf⟩, hf.symm⟩ },
rw [finset.prod_insert hi, set.mem_mul],
simp_rw [finset.prod_insert hi],
simp_rw ih,
split,
{ rintros ⟨x, y, hx, ⟨g, hg, rfl⟩, rfl⟩,
refine ⟨function.update g i x, λ j hj, _, _⟩,
obtain rfl | hj := finset.mem_insert.mp hj,
{ rw function.update_same, exact hx },
{ rw update_noteq (ne_of_mem_of_not_mem hj hi), exact hg hj, },
rw [finset.prod_update_of_not_mem hi, function.update_same], },
{ rintros ⟨g, hg, rfl⟩,
exact ⟨g i, is.prod g, hg (is.mem_insert_self _),
⟨g, λ i hi, hg (finset.mem_insert_of_mem hi), rfl⟩, rfl⟩ },
end
/-- A version of `set.mem_finset_prod` with a simpler RHS for products over a fintype. -/
@[to_additive /-" A version of `set.mem_finset_sum` with a simpler RHS for sums over a fintype. "-/]
lemma mem_fintype_prod [fintype ι] (f : ι → set α) (a : α) :
a ∈ ∏ i, f i ↔ ∃ (g : ι → α) (hg : ∀ i, g i ∈ f i), ∏ i, g i = a :=
by { rw mem_finset_prod, simp }
/-- The n-ary version of `set.mul_mem_mul`. -/
@[to_additive /-" The n-ary version of `set.add_mem_add`. "-/]
lemma finset_prod_mem_finset_prod (t : finset ι) (f : ι → set α)
(g : ι → α) (hg : ∀ i ∈ t, g i ∈ f i) :
∏ i in t, g i ∈ ∏ i in t, f i :=
by { rw mem_finset_prod, exact ⟨g, hg, rfl⟩ }
/-- The n-ary version of `set.mul_subset_mul`. -/
@[to_additive /-" The n-ary version of `set.add_subset_add`. "-/]
lemma finset_prod_subset_finset_prod (t : finset ι) (f₁ f₂ : ι → set α)
(hf : ∀ {i}, i ∈ t → f₁ i ⊆ f₂ i) :
∏ i in t, f₁ i ⊆ ∏ i in t, f₂ i :=
begin
intro a,
rw [mem_finset_prod, mem_finset_prod],
rintro ⟨g, hg, rfl⟩,
exact ⟨g, λ i hi, hf hi $ hg hi, rfl⟩
end
/-! TODO: define `decidable_mem_finset_prod` and `decidable_mem_finset_sum`. -/
end big_operators
/-! ### Properties about inversion -/
/-- The set `(s⁻¹ : set α)` is defined as `{x | x⁻¹ ∈ s}` in locale `pointwise`.
It is equal to `{x⁻¹ | x ∈ s}`, see `set.image_inv`. -/
@[to_additive
/-" The set `(-s : set α)` is defined as `{x | -x ∈ s}` in locale `pointwise`.
It is equal to `{-x | x ∈ s}`, see `set.image_neg`. "-/]
protected def has_inv [has_inv α] : has_inv (set α) :=
⟨preimage has_inv.inv⟩
localized "attribute [instance] set.has_inv set.has_neg" in pointwise
@[simp, to_additive]
lemma inv_empty [has_inv α] : (∅ : set α)⁻¹ = ∅ := rfl
@[simp, to_additive]
lemma inv_univ [has_inv α] : (univ : set α)⁻¹ = univ := rfl
@[simp, to_additive]
lemma nonempty_inv [group α] {s : set α} : s⁻¹.nonempty ↔ s.nonempty :=
inv_involutive.surjective.nonempty_preimage
@[to_additive] lemma nonempty.inv [group α] {s : set α} (h : s.nonempty) : s⁻¹.nonempty :=
nonempty_inv.2 h
@[simp, to_additive]
lemma mem_inv [has_inv α] : a ∈ s⁻¹ ↔ a⁻¹ ∈ s := iff.rfl
@[to_additive]
lemma inv_mem_inv [group α] : a⁻¹ ∈ s⁻¹ ↔ a ∈ s :=
by simp only [mem_inv, inv_inv]
@[simp, to_additive]
lemma inv_preimage [has_inv α] : has_inv.inv ⁻¹' s = s⁻¹ := rfl
@[simp, to_additive]
lemma image_inv [group α] : has_inv.inv '' s = s⁻¹ :=
by { simp only [← inv_preimage], rw [image_eq_preimage_of_inverse]; intro; simp only [inv_inv] }
@[simp, to_additive]
lemma inter_inv [has_inv α] : (s ∩ t)⁻¹ = s⁻¹ ∩ t⁻¹ := preimage_inter
@[simp, to_additive]
lemma union_inv [has_inv α] : (s ∪ t)⁻¹ = s⁻¹ ∪ t⁻¹ := preimage_union
@[simp, to_additive]
lemma compl_inv [has_inv α] : (sᶜ)⁻¹ = (s⁻¹)ᶜ := preimage_compl
@[simp, to_additive]
protected lemma inv_inv [group α] : s⁻¹⁻¹ = s :=
by { simp only [← inv_preimage, preimage_preimage, inv_inv, preimage_id'] }
@[simp, to_additive]
protected lemma univ_inv [group α] : (univ : set α)⁻¹ = univ := preimage_univ
@[simp, to_additive]
lemma inv_subset_inv [group α] {s t : set α} : s⁻¹ ⊆ t⁻¹ ↔ s ⊆ t :=
(equiv.inv α).surjective.preimage_subset_preimage_iff
@[to_additive] lemma inv_subset [group α] {s t : set α} : s⁻¹ ⊆ t ↔ s ⊆ t⁻¹ :=
by { rw [← inv_subset_inv, set.inv_inv] }
@[to_additive] lemma finite.inv [group α] {s : set α} (hs : finite s) : finite s⁻¹ :=
hs.preimage $ inv_injective.inj_on _
@[to_additive] lemma inv_singleton {β : Type*} [group β] (x : β) : ({x} : set β)⁻¹ = {x⁻¹} :=
by { ext1 y, rw [mem_inv, mem_singleton_iff, mem_singleton_iff, inv_eq_iff_inv_eq, eq_comm], }
/-! ### Properties about scalar multiplication -/
/-- The scaling of a set `(x • s : set β)` by a scalar `x ∶ α` is defined as `{x • y | y ∈ s}`
in locale `pointwise`. -/
@[to_additive has_vadd_set "The translation of a set `(x +ᵥ s : set β)` by a scalar `x ∶ α` is
defined as `{x +ᵥ y | y ∈ s}` in locale `pointwise`."]
protected def has_scalar_set [has_scalar α β] : has_scalar α (set β) :=
⟨λ a, image (has_scalar.smul a)⟩
/-- The pointwise scalar multiplication `(s • t : set β)` by a set of scalars `s ∶ set α`
is defined as `{x • y | x ∈ s, y ∈ t}` in locale `pointwise`. -/
@[to_additive has_vadd "The pointwise translation `(s +ᵥ t : set β)` by a set of constants
`s ∶ set α` is defined as `{x +ᵥ y | x ∈ s, y ∈ t}` in locale `pointwise`."]
protected def has_scalar [has_scalar α β] : has_scalar (set α) (set β) :=
⟨image2 has_scalar.smul⟩
localized "attribute [instance] set.has_scalar_set set.has_scalar" in pointwise
localized "attribute [instance] set.has_vadd_set set.has_vadd" in pointwise
@[simp, to_additive]
lemma image_smul [has_scalar α β] {t : set β} : (λ x, a • x) '' t = a • t := rfl
@[to_additive]
lemma mem_smul_set [has_scalar α β] {t : set β} : x ∈ a • t ↔ ∃ y, y ∈ t ∧ a • y = x := iff.rfl
@[to_additive]
lemma smul_mem_smul_set [has_scalar α β] {t : set β} (hy : y ∈ t) : a • y ∈ a • t :=
⟨y, hy, rfl⟩
@[to_additive]
lemma smul_set_union [has_scalar α β] {s t : set β} : a • (s ∪ t) = a • s ∪ a • t :=
by simp only [← image_smul, image_union]
@[to_additive]
lemma smul_set_inter [group α] [mul_action α β] {s t : set β} :
a • (s ∩ t) = a • s ∩ a • t :=
(image_inter $ mul_action.injective a).symm
lemma smul_set_inter₀ [group_with_zero α] [mul_action α β] {s t : set β} (ha : a ≠ 0) :
a • (s ∩ t) = a • s ∩ a • t :=
show units.mk0 a ha • _ = _, from smul_set_inter
@[to_additive]
lemma smul_set_inter_subset [has_scalar α β] {s t : set β} :
a • (s ∩ t) ⊆ a • s ∩ a • t := image_inter_subset _ _ _
@[simp, to_additive]
lemma smul_set_empty [has_scalar α β] (a : α) : a • (∅ : set β) = ∅ :=
by rw [← image_smul, image_empty]
@[to_additive]
lemma smul_set_mono [has_scalar α β] {s t : set β} (h : s ⊆ t) : a • s ⊆ a • t :=
by { simp only [← image_smul, image_subset, h] }
@[simp, to_additive]
lemma image2_smul [has_scalar α β] {t : set β} : image2 has_scalar.smul s t = s • t := rfl
@[to_additive]
lemma mem_smul [has_scalar α β] {t : set β} : x ∈ s • t ↔ ∃ a y, a ∈ s ∧ y ∈ t ∧ a • y = x :=
iff.rfl
lemma mem_smul_of_mem [has_scalar α β] {t : set β} {a} {b} (ha : a ∈ s) (hb : b ∈ t) :
a • b ∈ s • t :=
⟨a, b, ha, hb, rfl⟩
@[to_additive]
lemma image_smul_prod [has_scalar α β] {t : set β} :
(λ x : α × β, x.fst • x.snd) '' s.prod t = s • t :=
image_prod _
@[to_additive]
theorem range_smul_range [has_scalar α β] {ι κ : Type*} (b : ι → α) (c : κ → β) :
range b • range c = range (λ p : ι × κ, b p.1 • c p.2) :=
ext $ λ x, ⟨λ hx, let ⟨p, q, ⟨i, hi⟩, ⟨j, hj⟩, hpq⟩ := set.mem_smul.1 hx in
⟨(i, j), hpq ▸ hi ▸ hj ▸ rfl⟩,
λ ⟨⟨i, j⟩, h⟩, set.mem_smul.2 ⟨b i, c j, ⟨i, rfl⟩, ⟨j, rfl⟩, h⟩⟩
@[simp, to_additive]
lemma singleton_smul [has_scalar α β] {t : set β} : ({a} : set α) • t = a • t :=
image2_singleton_left
@[to_additive]
instance smul_comm_class_set {γ : Type*}
[has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] :
smul_comm_class α (set β) (set γ) :=
{ smul_comm := λ a T T',
by simp only [←image2_smul, ←image_smul, image2_image_right, image_image2, smul_comm] }
@[to_additive]
instance smul_comm_class_set' {γ : Type*}
[has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] :
smul_comm_class (set α) β (set γ) :=
by haveI := smul_comm_class.symm α β γ; exact smul_comm_class.symm _ _ _
@[to_additive]
instance smul_comm_class {γ : Type*}
[has_scalar α γ] [has_scalar β γ] [smul_comm_class α β γ] :
smul_comm_class (set α) (set β) (set γ) :=
{ smul_comm := λ T T' T'', begin
simp only [←image2_smul, image2_swap _ T],
exact image2_assoc (λ b c a, smul_comm a b c),
end }
instance is_scalar_tower {γ : Type*}
[has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] :
is_scalar_tower α β (set γ) :=
{ smul_assoc := λ a b T, by simp only [←image_smul, image_image, smul_assoc] }
instance is_scalar_tower' {γ : Type*}
[has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] :
is_scalar_tower α (set β) (set γ) :=
{ smul_assoc := λ a T T',
by simp only [←image_smul, ←image2_smul, image_image2, image2_image_left, smul_assoc] }
instance is_scalar_tower'' {γ : Type*}
[has_scalar α β] [has_scalar α γ] [has_scalar β γ] [is_scalar_tower α β γ] :
is_scalar_tower (set α) (set β) (set γ) :=
{ smul_assoc := λ T T' T'', image2_assoc smul_assoc }
section monoid
/-! ### `set α` as a `(∪,*)`-semiring -/
/-- An alias for `set α`, which has a semiring structure given by `∪` as "addition" and pointwise
multiplication `*` as "multiplication". -/
@[derive inhabited] def set_semiring (α : Type*) : Type* := set α
/-- The identitiy function `set α → set_semiring α`. -/
protected def up (s : set α) : set_semiring α := s
/-- The identitiy function `set_semiring α → set α`. -/
protected def set_semiring.down (s : set_semiring α) : set α := s
@[simp] protected lemma down_up {s : set α} : s.up.down = s := rfl
@[simp] protected lemma up_down {s : set_semiring α} : s.down.up = s := rfl
instance set_semiring.add_comm_monoid : add_comm_monoid (set_semiring α) :=
{ add := λ s t, (s ∪ t : set α),
zero := (∅ : set α),
add_assoc := union_assoc,
zero_add := empty_union,
add_zero := union_empty,
add_comm := union_comm, }
instance set_semiring.non_unital_non_assoc_semiring [has_mul α] :
non_unital_non_assoc_semiring (set_semiring α) :=
{ zero_mul := λ s, empty_mul,
mul_zero := λ s, mul_empty,
left_distrib := λ _ _ _, mul_union,
right_distrib := λ _ _ _, union_mul,
..set.has_mul, ..set_semiring.add_comm_monoid }
instance set_semiring.non_assoc_semiring [mul_one_class α] : non_assoc_semiring (set_semiring α) :=
{ ..set_semiring.non_unital_non_assoc_semiring, ..set.mul_one_class }
instance set_semiring.non_unital_semiring [semigroup α] : non_unital_semiring (set_semiring α) :=
{ ..set_semiring.non_unital_non_assoc_semiring, ..set.semigroup }
instance set_semiring.semiring [monoid α] : semiring (set_semiring α) :=
{ ..set_semiring.non_assoc_semiring, ..set_semiring.non_unital_semiring }
instance set_semiring.comm_semiring [comm_monoid α] : comm_semiring (set_semiring α) :=
{ ..set.comm_monoid, ..set_semiring.semiring }
/-- A multiplicative action of a monoid on a type β gives also a
multiplicative action on the subsets of β. -/
@[to_additive "An additive action of an additive monoid on a type β gives also an additive action
on the subsets of β."]
protected def mul_action_set [monoid α] [mul_action α β] : mul_action α (set β) :=
{ mul_smul := by { intros, simp only [← image_smul, image_image, ← mul_smul] },
one_smul := by { intros, simp only [← image_smul, image_eta, one_smul, image_id'] },
..set.has_scalar_set }
localized "attribute [instance] set.mul_action_set set.add_action_set" in pointwise
section mul_hom
variables [has_mul α] [has_mul β] (m : mul_hom α β)
@[to_additive]
lemma image_mul : m '' (s * t) = m '' s * m '' t :=
by { simp only [← image2_mul, image_image2, image2_image_left, image2_image_right, m.map_mul] }
@[to_additive]
lemma preimage_mul_preimage_subset {s t : set β} : m ⁻¹' s * m ⁻¹' t ⊆ m ⁻¹' (s * t) :=
by { rintros _ ⟨_, _, _, _, rfl⟩, exact ⟨_, _, ‹_›, ‹_›, (m.map_mul _ _).symm ⟩ }
end mul_hom
/-- The image of a set under function is a ring homomorphism
with respect to the pointwise operations on sets. -/
def image_hom [monoid α] [monoid β] (f : α →* β) : set_semiring α →+* set_semiring β :=
{ to_fun := image f,
map_zero' := image_empty _,
map_one' := by simp only [← singleton_one, image_singleton, f.map_one],
map_add' := image_union _,
map_mul' := λ _ _, image_mul f.to_mul_hom }
end monoid
end set
open set
open_locale pointwise
section
variables {α : Type*} {β : Type*}
/-- A nonempty set is scaled by zero to the singleton set containing 0. -/
lemma zero_smul_set [has_zero α] [has_zero β] [smul_with_zero α β] {s : set β} (h : s.nonempty) :
(0 : α) • s = (0 : set β) :=
by simp only [← image_smul, image_eta, zero_smul, h.image_const, singleton_zero]
lemma zero_smul_subset [has_zero α] [has_zero β] [smul_with_zero α β] (s : set β) :
(0 : α) • s ⊆ 0 :=
image_subset_iff.2 $ λ x _, zero_smul α x
lemma subsingleton_zero_smul_set [has_zero α] [has_zero β] [smul_with_zero α β] (s : set β) :
((0 : α) • s).subsingleton :=
subsingleton_singleton.mono (zero_smul_subset s)
section group
variables [group α] [mul_action α β]
@[simp, to_additive]
lemma smul_mem_smul_set_iff {a : α} {A : set β} {x : β} : a • x ∈ a • A ↔ x ∈ A :=
⟨λ h, begin
rw [←inv_smul_smul a x, ←inv_smul_smul a A],
exact smul_mem_smul_set h,
end, smul_mem_smul_set⟩
@[to_additive]
lemma mem_smul_set_iff_inv_smul_mem {a : α} {A : set β} {x : β} : x ∈ a • A ↔ a⁻¹ • x ∈ A :=
show x ∈ mul_action.to_perm a '' A ↔ _, from mem_image_equiv
@[to_additive]
lemma mem_inv_smul_set_iff {a : α} {A : set β} {x : β} : x ∈ a⁻¹ • A ↔ a • x ∈ A :=
by simp only [← image_smul, mem_image, inv_smul_eq_iff, exists_eq_right]
@[to_additive]
lemma preimage_smul (a : α) (t : set β) : (λ x, a • x) ⁻¹' t = a⁻¹ • t :=
((mul_action.to_perm a).symm.image_eq_preimage _).symm
@[to_additive]
lemma preimage_smul_inv (a : α) (t : set β) : (λ x, a⁻¹ • x) ⁻¹' t = a • t :=
preimage_smul (to_units a)⁻¹ t
@[simp, to_additive]
lemma set_smul_subset_set_smul_iff {a : α} {A B : set β} : a • A ⊆ a • B ↔ A ⊆ B :=
image_subset_image_iff $ mul_action.injective _
@[to_additive]
lemma set_smul_subset_iff {a : α} {A B : set β} : a • A ⊆ B ↔ A ⊆ a⁻¹ • B :=
(image_subset_iff).trans $ iff_of_eq $ congr_arg _ $
preimage_equiv_eq_image_symm _ $ mul_action.to_perm _
@[to_additive]
lemma subset_set_smul_iff {a : α} {A B : set β} : A ⊆ a • B ↔ a⁻¹ • A ⊆ B :=
iff.symm $ (image_subset_iff).trans $ iff.symm $ iff_of_eq $ congr_arg _ $
image_equiv_eq_preimage_symm _ $ mul_action.to_perm _
end group
section group_with_zero
variables [group_with_zero α] [mul_action α β]
@[simp] lemma smul_mem_smul_set_iff₀ {a : α} (ha : a ≠ 0) (A : set β)
(x : β) : a • x ∈ a • A ↔ x ∈ A :=
show units.mk0 a ha • _ ∈ _ ↔ _, from smul_mem_smul_set_iff
lemma mem_smul_set_iff_inv_smul_mem₀ {a : α} (ha : a ≠ 0) (A : set β) (x : β) :
x ∈ a • A ↔ a⁻¹ • x ∈ A :=
show _ ∈ units.mk0 a ha • _ ↔ _, from mem_smul_set_iff_inv_smul_mem
lemma mem_inv_smul_set_iff₀ {a : α} (ha : a ≠ 0) (A : set β) (x : β) : x ∈ a⁻¹ • A ↔ a • x ∈ A :=
show _ ∈ (units.mk0 a ha)⁻¹ • _ ↔ _, from mem_inv_smul_set_iff
lemma preimage_smul₀ {a : α} (ha : a ≠ 0) (t : set β) : (λ x, a • x) ⁻¹' t = a⁻¹ • t :=
preimage_smul (units.mk0 a ha) t
lemma preimage_smul_inv₀ {a : α} (ha : a ≠ 0) (t : set β) :
(λ x, a⁻¹ • x) ⁻¹' t = a • t :=
preimage_smul ((units.mk0 a ha)⁻¹) t
@[simp] lemma set_smul_subset_set_smul_iff₀ {a : α} (ha : a ≠ 0) {A B : set β} :
a • A ⊆ a • B ↔ A ⊆ B :=
show units.mk0 a ha • _ ⊆ _ ↔ _, from set_smul_subset_set_smul_iff
lemma set_smul_subset_iff₀ {a : α} (ha : a ≠ 0) {A B : set β} : a • A ⊆ B ↔ A ⊆ a⁻¹ • B :=
show units.mk0 a ha • _ ⊆ _ ↔ _, from set_smul_subset_iff
lemma subset_set_smul_iff₀ {a : α} (ha : a ≠ 0) {A B : set β} : A ⊆ a • B ↔ a⁻¹ • A ⊆ B :=
show _ ⊆ units.mk0 a ha • _ ↔ _, from subset_set_smul_iff
end group_with_zero
end
namespace finset
variables {α : Type*} [decidable_eq α]
/-- The pointwise product of two finite sets `s` and `t`:
`st = s ⬝ t = s * t = { x * y | x ∈ s, y ∈ t }`. -/
@[to_additive /-"The pointwise sum of two finite sets `s` and `t`:
`s + t = { x + y | x ∈ s, y ∈ t }`. "-/]
protected def has_mul [has_mul α] : has_mul (finset α) :=
⟨λ s t, (s.product t).image (λ p : α × α, p.1 * p.2)⟩
localized "attribute [instance] finset.has_mul finset.has_add" in pointwise
@[to_additive]
lemma mul_def [has_mul α] {s t : finset α} :
s * t = (s.product t).image (λ p : α × α, p.1 * p.2) := rfl
@[to_additive]
lemma mem_mul [has_mul α] {s t : finset α} {x : α} :
x ∈ s * t ↔ ∃ y z, y ∈ s ∧ z ∈ t ∧ y * z = x :=
by { simp only [finset.mul_def, and.assoc, mem_image, exists_prop, prod.exists, mem_product] }
@[simp, norm_cast, to_additive]
lemma coe_mul [has_mul α] {s t : finset α} : (↑(s * t) : set α) = ↑s * ↑t :=
by { ext, simp only [mem_mul, set.mem_mul, mem_coe] }
@[to_additive]
lemma mul_mem_mul [has_mul α] {s t : finset α} {x y : α} (hx : x ∈ s) (hy : y ∈ t) :
x * y ∈ s * t :=
by { simp only [finset.mem_mul], exact ⟨x, y, hx, hy, rfl⟩ }
@[to_additive]
lemma mul_card_le [has_mul α] {s t : finset α} : (s * t).card ≤ s.card * t.card :=
by { convert finset.card_image_le, rw [finset.card_product, mul_comm] }
open_locale classical
/-- A finite set `U` contained in the product of two sets `S * S'` is also contained in the product
of two finite sets `T * T' ⊆ S * S'`. -/
@[to_additive]
lemma subset_mul {M : Type*} [monoid M] {S : set M} {S' : set M} {U : finset M} (f : ↑U ⊆ S * S') :
∃ (T T' : finset M), ↑T ⊆ S ∧ ↑T' ⊆ S' ∧ U ⊆ T * T' :=
begin
apply finset.induction_on' U,
{ use [∅, ∅], simp only [finset.empty_subset, finset.coe_empty, set.empty_subset, and_self], },
rintros a s haU hs has ⟨T, T', hS, hS', h⟩,
obtain ⟨x, y, hx, hy, ha⟩ := set.mem_mul.1 (f haU),
use [insert x T, insert y T'],
simp only [finset.coe_insert],
repeat { rw [set.insert_subset], },
use [hx, hS, hy, hS'],
refine finset.insert_subset.mpr ⟨_, _⟩,
{ rw finset.mem_mul,
use [x,y],
simpa only [true_and, true_or, eq_self_iff_true, finset.mem_insert], },
{ suffices g : (s : set M) ⊆ insert x T * insert y T', { norm_cast at g, assumption, },
transitivity ↑(T * T'),
apply h,
rw finset.coe_mul,
apply set.mul_subset_mul (set.subset_insert x T) (set.subset_insert y T'), },
end
end finset
/-! Some lemmas about pointwise multiplication and submonoids. Ideally we put these in
`group_theory.submonoid.basic`, but currently we cannot because that file is imported by this. -/
namespace submonoid
variables {M : Type*} [monoid M]
@[to_additive]
lemma mul_subset {s t : set M} {S : submonoid M} (hs : s ⊆ S) (ht : t ⊆ S) : s * t ⊆ S :=
by { rintro _ ⟨p, q, hp, hq, rfl⟩, exact submonoid.mul_mem _ (hs hp) (ht hq) }
@[to_additive]
lemma mul_subset_closure {s t u : set M} (hs : s ⊆ u) (ht : t ⊆ u) :
s * t ⊆ submonoid.closure u :=
mul_subset (subset.trans hs submonoid.subset_closure) (subset.trans ht submonoid.subset_closure)
@[to_additive]
lemma coe_mul_self_eq (s : submonoid M) : (s : set M) * s = s :=
begin
ext x,
refine ⟨_, λ h, ⟨x, 1, h, s.one_mem, mul_one x⟩⟩,
rintros ⟨a, b, ha, hb, rfl⟩,
exact s.mul_mem ha hb
end
@[to_additive]
lemma closure_mul_le (S T : set M) : closure (S * T) ≤ closure S ⊔ closure T :=
Inf_le $ λ x ⟨s, t, hs, ht, hx⟩, hx ▸ (closure S ⊔ closure T).mul_mem
(set_like.le_def.mp le_sup_left $ subset_closure hs)
(set_like.le_def.mp le_sup_right $ subset_closure ht)
@[to_additive]
lemma sup_eq_closure (H K : submonoid M) : H ⊔ K = closure (H * K) :=
le_antisymm
(sup_le
(λ h hh, subset_closure ⟨h, 1, hh, K.one_mem, mul_one h⟩)
(λ k hk, subset_closure ⟨1, k, H.one_mem, hk, one_mul k⟩))
(by conv_rhs { rw [← closure_eq H, ← closure_eq K] }; apply closure_mul_le)
end submonoid
namespace group
lemma card_pow_eq_card_pow_card_univ_aux {f : ℕ → ℕ} (h1 : monotone f)
{B : ℕ} (h2 : ∀ n, f n ≤ B) (h3 : ∀ n, f n = f (n + 1) → f (n + 1) = f (n + 2)) :
∀ k, B ≤ k → f k = f B :=
begin
have key : ∃ n : ℕ, n ≤ B ∧ f n = f (n + 1),
{ contrapose! h2,
suffices : ∀ n : ℕ, n ≤ B + 1 → n ≤ f n,
{ exact ⟨B + 1, this (B + 1) (le_refl (B + 1))⟩ },
exact λ n, nat.rec (λ h, nat.zero_le (f 0)) (λ n ih h, lt_of_le_of_lt (ih (n.le_succ.trans h))
(lt_of_le_of_ne (h1 n.le_succ) (h2 n (nat.succ_le_succ_iff.mp h)))) n },
{ obtain ⟨n, hn1, hn2⟩ := key,
replace key : ∀ k : ℕ, f (n + k) = f (n + k + 1) ∧ f (n + k) = f n :=
λ k, nat.rec ⟨hn2, rfl⟩ (λ k ih, ⟨h3 _ ih.1, ih.1.symm.trans ih.2⟩) k,
replace key : ∀ k : ℕ, n ≤ k → f k = f n :=
λ k hk, (congr_arg f (add_tsub_cancel_of_le hk)).symm.trans (key (k - n)).2,
exact λ k hk, (key k (hn1.trans hk)).trans (key B hn1).symm },
end
variables {G : Type*} [group G] [fintype G] (S : set G)
lemma card_pow_eq_card_pow_card_univ [∀ (k : ℕ), decidable_pred (∈ (S ^ k))] :
∀ k, fintype.card G ≤ k → fintype.card ↥(S ^ k) = fintype.card ↥(S ^ (fintype.card G)) :=
begin
have hG : 0 < fintype.card G := fintype.card_pos_iff.mpr ⟨1⟩,
by_cases hS : S = ∅,
{ intros k hk,
congr' 2,
rw [hS, empty_pow _ (ne_of_gt (lt_of_lt_of_le hG hk)), empty_pow _ (ne_of_gt hG)] },
obtain ⟨a, ha⟩ := set.ne_empty_iff_nonempty.mp hS,
classical,
have key : ∀ a (s t : set G), (∀ b : G, b ∈ s → a * b ∈ t) → fintype.card s ≤ fintype.card t,
{ refine λ a s t h, fintype.card_le_of_injective (λ ⟨b, hb⟩, ⟨a * b, h b hb⟩) _,
rintros ⟨b, hb⟩ ⟨c, hc⟩ hbc,
exact subtype.ext (mul_left_cancel (subtype.ext_iff.mp hbc)) },
have mono : monotone (λ n, fintype.card ↥(S ^ n) : ℕ → ℕ) :=
monotone_nat_of_le_succ (λ n, key a _ _ (λ b hb, set.mul_mem_mul ha hb)),
convert card_pow_eq_card_pow_card_univ_aux mono (λ n, set_fintype_card_le_univ (S ^ n))
(λ n h, le_antisymm (mono (n + 1).le_succ) (key a⁻¹ _ _ _)),
{ simp only [finset.filter_congr_decidable, fintype.card_of_finset] },
replace h : {a} * S ^ n = S ^ (n + 1),
{ refine set.eq_of_subset_of_card_le _ (le_trans (ge_of_eq h) _),
{ exact mul_subset_mul (set.singleton_subset_iff.mpr ha) set.subset.rfl },
{ convert key a (S ^ n) ({a} * S ^ n) (λ b hb, set.mul_mem_mul (set.mem_singleton a) hb) } },
rw [pow_succ', ←h, mul_assoc, ←pow_succ', h],
rintros _ ⟨b, c, hb, hc, rfl⟩,
rwa [set.mem_singleton_iff.mp hb, inv_mul_cancel_left],
end
end group
|
6a7100e158379613287934f919e003ab9ca5ccb4 | fbef61907e2e2afef7e1e91395505ed27448a945 | /LeanProtocPlugin.lean | 16ce23d9c4120dcc70e45a1b784d1f5df5c2cf41 | [
"MIT"
] | permissive | yatima-inc/Protoc.lean | 7edecca1f5b33909e6dfef1bcebcbbf9a322ca8c | 3ad7839ec911906e19984e5b60958ee9c866b7ec | refs/heads/master | 1,682,196,995,576 | 1,620,507,180,000 | 1,620,507,180,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 6,570 | lean | import LeanProtocPlugin.Google.Protobuf.Compiler.Plugin
import LeanProtocPlugin.Google.Protobuf.Descriptor
import LeanProto
import LeanProtocPlugin.Helpers
import LeanProtocPlugin.ProtoGenM
import LeanProtocPlugin.Logic
import Lean.Elab.Term
import Lean.Hygiene
import Init.System.IO
import Std.Data.RBTree
import Std.Data.RBMap
open LeanProtocPlugin.Google.Protobuf.Compiler
open LeanProtocPlugin.Google.Protobuf
open IO (FS.Stream)
open Lean
open Lean.Elab.Term
-- deriving instance Inhabited for Std.RBMap
def defaultImports (sourceFilename: String) : Array String := #[
"-- Generated by the Lean protobuf compiler. Do not edit manually.",
s!"-- source: {sourceFilename}",
"",
"import LeanProto",
"import Std.Data.AssocList"
]
def defaultSettings : Array String := #[
"set_option maxHeartbeats 10000000",
"set_option maxRecDepth 2048",
"set_option synthInstance.maxHeartbeats 10000000",
"set_option genSizeOfSpec false",
"",
"open Std (AssocList)"
]
def addImportDeps (f: FileDescriptorProto): ProtoGenM Unit := do
addLine ""
let namespacePrefix := (← read).namespacePrefix
let paths := f.dependency.map (fun s => namespacePrefix ++ "." ++ filePathToPackage s)
let importCommands ← paths.mapM (s!"import {·}")
addLines importCommands
-- Building the context: the incoming proto request doesn't contain
-- enough information to do generation in a streaming way, so first
-- we go through everything and build up a context
namespace ContextPrep
structure ContextPrepState where
enums : Array (String × (ASTPath × EnumDescriptorProto)) := #[]
oneofs : Array (String × (ASTPath × OneofDescriptorProto)) := #[]
messages : Array (String × ASTPath) := #[]
deriving Inhabited
def prepareContextRecurseFile (d: ASTPath) (_: Unit): StateM ContextPrepState Unit := do
let mut s ← get
let extraS : ContextPrepState := match d.revMessages.head? with
| some m => {
enums := m.enumType.map (fun e => (d.protoFullPath ++ "." ++ e.name, (d, e))),
oneofs := m.oneofDecl.map (fun e => (d.protoFullPath ++ "." ++ e.name, (d, e))),
messages := #[(d.protoFullPath, d)]
}
| none => {
enums := d.file.enumType.map (fun e => (d.protoFullPath ++ "." ++ e.name, (d, e))),
}
set {
enums := s.enums ++ extraS.enums,
oneofs := s.oneofs ++ extraS.oneofs,
messages := s.messages ++ extraS.messages
: ContextPrepState}
def prepareContext (r: Compiler.CodeGeneratorRequest) (rootPackage: String): ContextPrepState :=
let rec doWork (f: FileDescriptorProto) : StateM ContextPrepState PUnit := do
recurseM ((ASTPath.init f rootPackage), ()) (wrapRecurseFn prepareContextRecurseFile true)
let doWorkM := r.protoFile.forM $ doWork
StateT.run doWorkM {} |> Id.run |> Prod.snd
end ContextPrep
-- Generate code for a single .proto file
def doWork (file: FileDescriptorProto) : ProtoGenM $ List CodeGeneratorResponse_File := do
addLines $ defaultImports file.name
addImportDeps file
addLine ""
addLines $ defaultSettings
addLine ""
let package := (← read).namespacePrefix ++ "." ++ protoPackageToLeanPackagePrefix file.package
addLine s!"namespace {package}"
addLine ""
generateEnumDeclarations file
addLines #["", "mutual", ""]
generateMessageDeclarations file
addLines #["", "end", ""]
generateMessageManualDerives file
addLine ""
generateLeanDeriveStatements file
addLines #["", "mutual", ""]
generateDeserializers file
addLines #["", "end", ""]
-- addLine s!"set_option trace.compiler.ir.result true"
addLine s!""
addLines #["", "mutual", ""]
generateSerializers file
addLines #["", "end", ""]
-- addLine s!"set_option trace.compiler.ir.result false"
addLine s!""
generateSerDeserInstances file
addLine ""
addLine s!"end {package}"
let protoFile := CodeGeneratorResponse_File.mk
(outputFilePath file (← read).namespacePrefix) "" ("\n".intercalate (← get).lines.toList) none
let services := file.service
if services.size == 0 then return [protoFile]
-- Reset line state
set { (← get) with lines := #[] }
addLines $ defaultImports file.name
addImportDeps file
let protoMod := (← read).namespacePrefix ++ "." ++ filePathToPackage file.name
addLine s!"import LeanGRPC"
addLine s!"import {protoMod}"
addLine ""
addLine s!"namespace {package}"
addLine ""
generateGRPC file
addLine ""
addLine s!"end {package}"
let grpcFile := CodeGeneratorResponse_File.mk
(grpcOutputFilePath file (← read).namespacePrefix) "" ("\n".intercalate (← get).lines.toList) none
return [protoFile, grpcFile]
def main(argv: List String): IO UInt32 := do
let i ← IO.getStdin
let o ← IO.getStdout
let makeErrorResponse (s: String) := CodeGeneratorResponse.mk s 0 #[]
let bytes ← i.readBinToEnd
let request ← monadLift $ LeanProto.ProtoDeserialize.deserialize (α:=CodeGeneratorRequest) bytes
let namespacePrefix ← match request.parameter with
| "" => do
let out ← LeanProto.ProtoSerialize.serialize $ makeErrorResponse "Must provide root package name as protoc parameter"
o.write out
return 0
| _ => request.parameter
let filesToWrite := rbtreeOfC request.fileToGenerate
let fileProtos := rbmapOfC (request.protoFile.map fun x => (x.name, x))
let fileProtosToWrite := request.protoFile.filter fun v => filesToWrite.contains v.name
let contextAux := ContextPrep.prepareContext request namespacePrefix
let ctxForFile (f: FileDescriptorProto) := ProtoGenContext.mk namespacePrefix fileProtos f
(rbmapOfC contextAux.enums)
(rbmapOfC contextAux.oneofs)
(rbmapOfC contextAux.messages)
let processFile (f: FileDescriptorProto) : IO $ List CodeGeneratorResponse_File :=
StateT.run' (ReaderT.run (doWork f) (ctxForFile f)) {}
let response : CodeGeneratorResponse ← do try
let processedNested : Array $ List CodeGeneratorResponse_File ← fileProtosToWrite.mapM processFile
let mut processed := processedNested.foldl (fun acc curr => acc ++ curr.toArray) #[]
let allNewModules := processed.foldl (fun acc s => (filePathToPackage s.name) :: acc) []
let rootFileText := "\n".intercalate $ allNewModules.map (s!"import {·}")
processed := processed.push $ CodeGeneratorResponse_File.mk (namespacePrefix ++ ".lean") "" rootFileText none
CodeGeneratorResponse.mk "" 0 processed
catch e: IO.Error =>
makeErrorResponse e.toString
let r ← LeanProto.ProtoSerialize.serialize response
o.write r
return 0 |
f82d42bfadb110e850a00002fc4fcdadbeb93636 | ca37452420f42eb9a19643a034c532fb39187bd0 | /lemmas1.lean | 40bd40d0efc18dde6ab017a15176a1d659716067 | [] | no_license | minchaowu/Kruskal.lean | 0096fd12c6732dcb55214907e7241df27d22a51e | 0ec4a724c18d8fa2bc1e81dfc026a045888526d5 | refs/heads/master | 1,589,975,750,330 | 1,481,928,508,000 | 1,481,928,508,000 | 70,121,484 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 8,660 | lean | import data.fin
open classical fin nat function prod subtype set
noncomputable theory
theorem lt_or_eq_of_lt_succ {n m : ℕ} (H : n < succ m) : n < m ∨ n = m := lt_or_eq_of_le (le_of_lt_succ H)
theorem imp_of_not_and {p q : Prop} (H : ¬ (p ∧ q)) : p → ¬ q :=
assume Hp Hq, H (and.intro Hp Hq)
theorem not_and_of_imp {p q : Prop} (H : p → ¬ q) : ¬ (p ∧ q) :=
assume H1, have ¬ q, from H (and.left H1), this (and.right H1)
theorem and_of_not_imp {p q : Prop} (H : ¬ (p → ¬ q)) : p ∧ q :=
by_contradiction
(suppose ¬ (p ∧ q),
have p → ¬ q, from imp_of_not_and this,
H this)
theorem existence_of_nat_gt (n : ℕ) : ∃ m, n < m :=
have n < succ n, from lt_succ_self n,
exists.intro (succ n) this
namespace kruskal
structure quasiorder [class] (A : Type) extends has_le A : Type :=
(refl : ∀ a, le a a)
(trans : ∀ a b c, le a b → le b c → le a c)
proposition le_refl {A : Type} [quasiorder A] (a : A) : a ≤ a :=
quasiorder.refl a
proposition le_trans {A : Type} [H : quasiorder A] {a b c : A} (H₁ : a ≤ b) (H₂ : b ≤ c) : a ≤ c := quasiorder.trans _ _ _ H₁ H₂
structure wqo [class] (A : Type) extends quasiorder A : Type :=
(is_good : ∀ f : ℕ → A, ∃ i j : ℕ, i < j ∧ le (f i) (f j))
check @wqo.le
definition is_good {A : Type} (f : ℕ → A) (o : A → A → Prop) := ∃ i j : ℕ, i < j ∧ o (f i) (f j)
definition o_for_pairs {A B : Type} (o₁ : A → A → Prop) (o₂ : B → B → Prop) (s : A × B) (t : A × B) := o₁ (pr1 s) (pr1 t) ∧ o₂ (pr2 s) (pr2 t)
proposition qo_prod [instance] {A B: Type} [quasiorder A] [quasiorder B] : quasiorder (A × B) :=
let op := o_for_pairs quasiorder.le quasiorder.le in
have refl : ∀ p : A × B, op p p, by intro; split; repeat apply quasiorder.refl,
have trans : ∀ a b c, op a b → op b c → op a c,
begin intros with x y z h1 h2, split,
apply !quasiorder.trans (and.left h1) (and.left h2),
apply !quasiorder.trans (and.right h1) (and.right h2)
end,
quasiorder.mk op refl trans
definition terminal {A : Type} (o : A → A → Prop) (f : ℕ → A) (m : ℕ) := ∀ n, m < n → ¬ o (f m) (f n)
theorem prop_of_non_terminal {A : Type} {o : A → A → Prop} {f : ℕ → A} {m : ℕ} (H : ¬ @terminal _ o f m) : ∃ n, m < n ∧ o (f m) (f n) :=
obtain n h, from exists_not_of_not_forall H,
have m < n ∧ o (f m) (f n), from and_of_not_imp h,
exists.intro n this
section
parameter {A : Type}
parameter [wqo A]
parameter f : ℕ → A
section
parameter H : ∀ N, ∃ r, N < r ∧ terminal wqo.le f r
definition find_terminal_index (n : ℕ) : {x : ℕ | n < x ∧ terminal wqo.le f x} :=
nat.rec_on n (let i := some (H 0) in tag i (some_spec (H 0)))
(λ pred index',
let i' := (elt_of index') in
let i := some (H i') in
have p : i' < i ∧ terminal wqo.le f i, from some_spec (H i'),
have lt : i' < i, from and.left p,
have pred < i', from and.left (has_property index'),
have succ pred ≤ i', from (and.left (lt_iff_succ_le pred i')) this,
have succ pred < i, from lt_of_le_of_lt this lt,
have succ pred < i ∧ terminal wqo.le f i, from and.intro this (and.right p),
tag i this)
lemma increasing_fti {n m : ℕ} : n < m → elt_of (find_terminal_index n) < elt_of (find_terminal_index m) :=
nat.induction_on m
(assume H, absurd H dec_trivial)
(take a, assume IH, assume lt,
have disj : n < a ∨ n = a, from lt_or_eq_of_lt_succ lt,
have elt_of (find_terminal_index a) < elt_of (find_terminal_index (succ a)), from and.left (some_spec (H (elt_of (find_terminal_index a)))),
or.elim disj
(λ Hl, lt.trans (IH Hl) this)
(λ Hr, by+ simp))
private definition g (n : ℕ) := f (elt_of (find_terminal_index n))
theorem terminal_g (n : ℕ) : terminal wqo.le g n :=
have ∀ n', (elt_of (find_terminal_index n)) < n' → ¬ wqo.le (f (elt_of (find_terminal_index n))) (f n'), from and.right (has_property (find_terminal_index n)),
take n', assume h, this (elt_of (find_terminal_index n')) (increasing_fti h)
theorem bad_g : ¬ is_good g wqo.le :=
have H1 : ∀ i j, i < j → ¬ wqo.le (g i) (g j), from λ i, λ j, λ h, (terminal_g i) j h,
suppose ∃ i j, i < j ∧ wqo.le (g i) (g j),
obtain (i j) h, from this,
have ¬ wqo.le (g i) (g j), from H1 i j (and.left h),
this (and.right h)
theorem local_contradiction : false := bad_g (wqo.is_good g)
end
theorem finite_terminal : ∃ N, ∀ r, N < r → ¬ terminal wqo.le f r :=
have ¬ ∀ N, ∃ r, N < r ∧ @terminal A wqo.le f r, by apply local_contradiction,
have ∃ N, ¬ ∃ r, N < r ∧ @terminal A wqo.le f r, from exists_not_of_not_forall this,
obtain n h, from this,
have ∀ r, ¬ (n < r ∧ @terminal A wqo.le f r), by+ rewrite forall_iff_not_exists at h;exact h,
have ∀ r, n < r → ¬ @terminal A wqo.le f r, from λ r, imp_of_not_and (this r),
exists.intro n this
end
section
parameters {A B : Type}
parameter [wqo A]
parameter [wqo B]
section
parameter f : ℕ → A × B
theorem finite_terminal_on_A : ∃ N, ∀ r, N < r → ¬ @terminal A wqo.le (pr1 ∘ f) r :=
finite_terminal (pr1 ∘ f)
definition sentinel := some finite_terminal_on_A
definition h_helper (n : ℕ) : {x : ℕ |sentinel < x ∧ ¬ @terminal A wqo.le (pr1 ∘ f) x} :=
nat.rec_on n
(have ∃ m, sentinel < m, by apply existence_of_nat_gt,
let i := some this in
have ge : sentinel < i, from some_spec this,
have ¬ @terminal A wqo.le (pr1 ∘ f) i, from (some_spec finite_terminal_on_A) i ge,
have sentinel < i ∧ ¬ terminal wqo.le (pr1 ∘ f) i, from proof and.intro ge this qed,
tag i this)
(λ pred h',
let i' := elt_of h' in
have lt' : sentinel < i', from and.left (has_property h'),
have ¬ terminal wqo.le (pr1 ∘ f) i', from and.right (has_property h'),
have ∃ n, i' < n ∧ ((pr1 ∘ f) i') ≤ ((pr1 ∘ f) n), from prop_of_non_terminal this,
let i := some this in
have i' < i, from proof and.left (some_spec this) qed,
have lt : sentinel < i, from proof lt.trans lt' this qed,
have ∀ r, sentinel < r → ¬ terminal wqo.le (pr1 ∘ f) r, from some_spec finite_terminal_on_A,
have ¬ terminal wqo.le (pr1 ∘ f) i, from this i lt,
have sentinel < i ∧ ¬ terminal wqo.le (pr1 ∘ f) i, from proof and.intro lt this qed,
tag i this)
private definition h (n : ℕ) := elt_of (h_helper n)
private lemma foo (a : ℕ) : h a < h (succ a) ∧ (pr1 ∘ f) (h a) ≤ (pr1 ∘ f) (h (succ a)) :=
have ¬ terminal wqo.le (pr1 ∘ f) (h a), from and.right (has_property (h_helper a)),
have ∃ n, (h a) < n ∧ ((pr1 ∘ f) (h a)) ≤ ((pr1 ∘ f) n), from prop_of_non_terminal this,
some_spec this
theorem property_of_h {i j : ℕ} : i < j → (pr1 ∘ f) (h i) ≤ (pr1 ∘ f) (h j) :=
nat.induction_on j
(assume H, absurd H dec_trivial)
(take a, assume IH, assume lt,
have H1 : (pr1 ∘ f) (h a) ≤ (pr1 ∘ f) (h (succ a)), from and.right (foo a),
have disj : i < a ∨ i = a, from lt_or_eq_of_lt_succ lt,
or.elim disj
(λ Hl, !wqo.trans (IH Hl) H1)
(λ Hr, by+ rewrite -Hr at H1{1};exact H1))
theorem increasing_h {i j : ℕ} : i < j → h i < h j :=
nat.induction_on j
(assume H, absurd H dec_trivial)
(take a, assume IH, assume lt,
have H1 : (h a) < h (succ a), from and.left (foo a),
have disj : i < a ∨ i = a, from lt_or_eq_of_lt_succ lt,
or.elim disj
(λ Hl, lt.trans (IH Hl) H1)
(λ Hr, by+ simp))
theorem good_f : is_good f (o_for_pairs wqo.le wqo.le) :=
have ∃ i j : ℕ, i < j ∧ (pr2 ∘ f ∘ h) i ≤ (pr2 ∘ f ∘ h) j, from wqo.is_good (pr2 ∘ f ∘ h),
obtain (i j) H, from this,
have (pr1 ∘ f) (h i) ≤ (pr1 ∘ f) (h j), from property_of_h (and.left H),
have Hr : (pr1 ∘ f) (h i) ≤ (pr1 ∘ f) (h j) ∧ (pr2 ∘ f) (h i) ≤ (pr2 ∘ f) (h j), from and.intro this (and.right H),
have h i < h j, from increasing_h (and.left H),
have h i < h j ∧ (pr1 ∘ f) (h i) ≤ (pr1 ∘ f) (h j) ∧ (pr2 ∘ f) (h i) ≤ (pr2 ∘ f) (h j), from and.intro this Hr,
by+ fapply exists.intro; exact h i; fapply exists.intro; exact h j; exact this
end
theorem good_pairs : ∀ f : ℕ → A × B, is_good f (o_for_pairs wqo.le wqo.le) := good_f
end
definition wqo_prod [instance] {A B : Type} [HA : wqo A] [HB : wqo B] : wqo (A × B) :=
let op := o_for_pairs wqo.le wqo.le in
have refl : ∀ p : A × B, op p p, by intro;split;repeat apply wqo.refl,
have trans : ∀ a b c, op a b → op b c → op a c,
begin intros with x y z h1 h2, split,
apply !wqo.trans (and.left h1) (and.left h2),
apply !wqo.trans (and.right h1) (and.right h2),
end,
wqo.mk op refl trans good_pairs
end kruskal
|
ef4c90a586fe5b85917a3364aa58614c861a6edb | 4a092885406df4e441e9bb9065d9405dacb94cd8 | /src/Huber_ring/localization.lean | 1c26ab014e84bb73050ecf5a54272e2aa7ed6481 | [
"Apache-2.0"
] | permissive | semorrison/lean-perfectoid-spaces | 78c1572cedbfae9c3e460d8aaf91de38616904d8 | bb4311dff45791170bcb1b6a983e2591bee88a19 | refs/heads/master | 1,588,841,765,494 | 1,554,805,620,000 | 1,554,805,620,000 | 180,353,546 | 0 | 1 | null | 1,554,809,880,000 | 1,554,809,880,000 | null | UTF-8 | Lean | false | false | 15,283 | lean | import ring_theory.localization
import tactic.tidy
import tactic.ring
import Huber_ring.basic
import for_mathlib.finset
import for_mathlib.topological_rings
import for_mathlib.algebra
import for_mathlib.submodule
import for_mathlib.subgroup
import for_mathlib.nonarchimedean.basic
import for_mathlib.group
universes u v
local attribute [instance, priority 0] classical.prop_decidable
local attribute [instance] set.pointwise_mul_comm_semiring
namespace Huber_ring
open localization algebra topological_ring submodule set topological_add_group
variables {A : Type u} [comm_ring A] [topological_space A ] [topological_ring A]
variables (T : set A) (s : A)
/-
Our goal is to define a topology on (away s), which is the localization of A at s.
This topology will depend on T, and should not depend on the ring of definition.
In the literature, this ring is commonly denoted with A(T/s) to indicate the
dependence on T. For the same reason, we start by defining a wrapper type that
includes T in its assumptions.
-/
/--The localization at `s`, endowed with a topology that depends on `T`-/
def away (T : set A) (s : A) := away s
local notation `ATs` := away T s
namespace away
instance : comm_ring ATs := by delta away; apply_instance
instance : module A ATs := by delta away; apply_instance
instance : algebra A ATs := by delta away; apply_instance
instance : has_coe A ATs := ⟨λ a, (of_id A ATs : A → ATs) a⟩
/--An auxiliary subring, used to define the topology on `away T s`-/
def D.aux : set ATs :=
let s_inv : ATs := ((to_units ⟨s, ⟨1, by simp⟩⟩)⁻¹ : units ATs) in
ring.closure (s_inv • of_id A ATs '' T)
local notation `D` := D.aux T s
instance : is_subring D := by delta D.aux; apply_instance
local notation `Dspan` U := span D (of_id A ATs '' (U : set A))
/-
To put a topology on `away T s` we want to use the construction
`topology_of_submodules_comm` which needs a directed family of
submodules of `ATs = away T s` viewed as `D`-algebra.
This directed family has two satisfy two extra conditions.
Proving these two conditions takes up the beef of this file.
Initially we only assume that `A` is a nonarchimedean ring,
but towards the end we need to strengthen this assumption to Huber ring.
-/
set_option class.instance_max_depth 50
/-The submodules spanned by the open subgroups of `A` form a directed family-/
lemma directed (U₁ U₂ : open_add_subgroup A) :
∃ (U : open_add_subgroup A), (Dspan U) ≤ (Dspan U₁) ⊓ (Dspan U₂) :=
begin
use U₁ ⊓ U₂,
apply lattice.le_inf _ _;
rw span_le;
refine subset.trans (image_subset _ _) subset_span,
{ apply inter_subset_left },
{ apply inter_subset_right },
end
/-For every open subgroup `U` of `A` and every `a : A`,
there exists an open subgroup `V` of `A`,
such that `a • (span D V)` is contained in the `D`-span of `U`.-/
lemma exists_mul_left_subset (h : nonarchimedean A) (U : open_add_subgroup A) (a : A) :
∃ V : open_add_subgroup A, (a : ATs) • (Dspan V) ≤ (Dspan U) :=
begin
cases h _ _ with V hV,
use V,
work_on_goal 0 {
erw [smul_singleton, ← span_image, span_le, ← image_comp, ← algebra.map_lmul_left, image_comp],
refine subset.trans (image_subset (of_id A ATs : A → ATs) _) subset_span,
rw image_subset_iff,
exact hV },
apply mem_nhds_sets (continuous_mul_left _ _ U.is_open),
{ rw [mem_preimage_eq, mul_zero],
apply is_add_submonoid.zero_mem }
end
/-For every open subgroup `U` of `A`, there exists an open subgroup `V` of `A`,
such that the multiplication map sends the `D`-span of `V` into the `D`-span of `U`.-/
lemma mul_le (h : nonarchimedean A) (U : open_add_subgroup A) :
∃ (V : open_add_subgroup A), (Dspan V) * (Dspan V) ≤ (Dspan U) :=
begin
rcases nonarchimedean.mul_subset h U with ⟨V, hV⟩,
use V,
rw span_mul_span,
apply span_mono,
rw ← is_semiring_hom.map_mul (image (of_id A ATs : A → ATs)),
exact image_subset _ hV,
end
lemma K.aux (L : finset A) (h : (↑L : set A) ⊆ ideal.span T) :
∃ (K : finset A), (↑L : set A) ⊆ (↑(span ℤ (T * ↑K)) : set A) :=
begin
delta ideal.span at h,
erw span_eq_map_lc at h,
choose s hs using finset.exists_finset_of_subset_image L _ _ h,
use s.bind (λ f, f.frange),
rcases hs with ⟨rfl, hs⟩,
intros l hl,
rcases finset.mem_image.mp hl with ⟨f, hf, rfl⟩,
apply submodule.sum_mem_span,
intros t ht,
refine ⟨t, _, _, _, mul_comm _ _⟩,
{ replace hf := hs hf,
erw lc.mem_supported at hf,
exact hf ht },
{ erw [linear_map.id_apply, finset.mem_bind],
use [f, hf],
erw finsupp.mem_support_iff at ht,
erw finsupp.mem_frange,
exact ⟨ht, ⟨t, rfl⟩⟩ }
end
end away
end Huber_ring
namespace Huber_ring
open localization algebra topological_ring submodule set topological_add_group
variables {A : Type u} [Huber_ring A]
variables (T : set A) (s : A)
namespace away
local notation `ATs` := away T s
local notation `D` := D.aux T s
local notation `Dspan` U := span D (of_id A ATs '' (U : set A))
set_option class.instance_max_depth 80
/- Wedhorn 6.20 for n = 1-/
lemma mul_T_open (hT : is_open (↑(ideal.span T) : set A)) (U : open_add_subgroup A) :
is_open (↑(T • span ℤ (U : set A)) : set A) :=
begin
-- Choose an ideal I ⊆ span T
rcases exists_pod_subset _ (mem_nhds_sets hT $ ideal.zero_mem $ ideal.span T)
with ⟨A₀, _, _, _, ⟨_, emb, hf, I, fg, top⟩, hI⟩,
resetI, dsimp only at hI,
-- Choose a generating set L ⊆ I
cases fg with L hL,
rw ← hL at hI,
-- Observe L ⊆ span T
have Lsub : (↑(L.image (to_fun A)) : set A) ⊆ ↑(ideal.span T) :=
by { rw finset.coe_image, exact set.subset.trans (image_subset _ subset_span) hI },
-- Choose a finite set K such that L ⊆ span (T * K)
cases K.aux _ _ Lsub with K hK,
-- Choose V such that K * V ⊆ U
let nonarch := Huber_ring.nonarchimedean,
let V := K.inf (λ k : A, classical.some (nonarch.left_mul_subset U k)),
cases (is_ideal_adic_iff I).mp top with H₁ H₂,
have hV : ↑K * (V : set A) ⊆ U,
{ rintros _ ⟨k, hk, v, hv, rfl⟩,
apply classical.some_spec (nonarch.left_mul_subset U k),
refine ⟨k, set.mem_singleton _, v, _, rfl⟩,
apply (finset.inf_le hk : V ≤ _),
exact hv },
replace hV : span ℤ _ ≤ span ℤ _ := span_mono hV,
erw [← span_mul_span, ← submodule.smul_def] at hV,
-- Choose m such that I^m ⊆ V
cases H₂ _ (mem_nhds_sets (emb.continuous _ V.is_open) _) with m hm,
work_on_goal 1 {
erw [mem_preimage_eq, is_ring_hom.map_zero (to_fun A)],
{ exact V.zero_mem },
apply_instance },
rw ← image_subset_iff at hm,
erw [← add_subgroup_eq_spanℤ (V : set A), ← add_subgroup_eq_spanℤ (↑(I^m) : set A₀)] at hm,
change (submodule.map (alg_hom_int $ to_fun A).to_linear_map _) ≤ _ at hm,
work_on_goal 1 {apply_instance},
-- It suffices to provide an open subgroup
apply @open_add_subgroup.is_open_of_open_add_subgroup A _ _ _ _
(submodule.submodule_is_add_subgroup _),
refine ⟨⟨to_fun A '' ↑(I^(m+1)), _, _⟩, _⟩,
work_on_goal 2 {assumption},
all_goals { try {apply_instance} },
{ exact embedding_open emb hf (H₁ _) },
-- And now we start a long calculation
-- Unfortunately it seems to be hard to express in calc mode
-- First observe: I^(m+1) = L • I^m as A₀-ideal, but also as ℤ-submodule
erw [subtype.coe_mk, pow_succ, ← hL, ← submodule.smul_def, hL, smul_eq_smul_spanℤ],
change (submodule.map (alg_hom_int $ to_fun A).to_linear_map _) ≤ _,
work_on_goal 1 {apply_instance},
-- Now we map the above equality through the canonical map A₀ → A
erw [submodule.map_mul, ← span_image, ← submodule.smul_def],
erw [finset.coe_image] at hK,
-- Next observe: L • I^m ≤ (T * K) • V
refine le_trans (smul_le_smul hK hm) _,
-- Also observe: T • (K • V) ≤ T • U
refine (le_trans (le_of_eq _) (smul_le_smul (le_refl T) hV)),
change span _ _ * _ = _,
erw [span_span, ← mul_smul],
refl
end
set_option class.instance_max_depth 80
lemma mul_left.aux₁ (hT : is_open (↑(ideal.span T) : set A)) (U : open_add_subgroup A) :
∃ (V : open_add_subgroup A),
(↑((to_units ⟨s, ⟨1, pow_one s⟩⟩)⁻¹ : units ATs) : ATs) • (Dspan ↑V) ≤ Dspan ↑U :=
begin
refine ⟨⟨_, mul_T_open _ hT U, by apply_instance⟩, _⟩,
erw [subtype.coe_mk (↑(T • span ℤ ↑U) : set A), @submodule.smul_def ℤ, span_mul_span],
change _ • span _ ↑(submodule.map (alg_hom_int $ (of_id A ATs : A → ATs)).to_linear_map _) ≤ _,
erw [← span_image, span_spanℤ, submodule.smul_def, span_mul_span, span_le],
rintros _ ⟨s_inv, hs_inv, tu, htu, rfl⟩,
erw mem_image at htu,
rcases htu with ⟨_, ⟨t, ht, u, hu, rfl⟩, rfl⟩,
rw submodule.mem_coe,
convert (span _ _).smul_mem _ _ using 1,
work_on_goal 3 { exact subset_span ⟨u, hu, rfl⟩ },
work_on_goal 1 { constructor },
work_on_goal 0 {
change s_inv * (algebra_map _ _) = _ • (algebra_map _ _),
rw [algebra.map_mul, ← mul_assoc],
congr },
{ apply ring.mem_closure, exact ⟨s_inv, hs_inv, _, ⟨t, ht, rfl⟩, rfl⟩ }
end
lemma mul_left.aux₂ (hT : is_open (↑(ideal.span T) : set A))
(s' : powers s) (U : open_add_subgroup A) :
∃ (V : open_add_subgroup A),
(↑((to_units s')⁻¹ : units ATs) : ATs) • (Dspan (V : set A)) ≤ Dspan (U : set A) :=
begin
rcases s' with ⟨_, ⟨n, rfl⟩⟩,
induction n with k hk,
{ use U,
simp only [pow_zero],
change (1 : ATs) • _ ≤ _,
rw one_smul,
exact le_refl _ },
cases hk with W hW,
cases mul_left.aux₁ T s hT W with V hV,
use V,
refine le_trans _ hW,
refine le_trans (le_of_eq _) (smul_le_smul (le_refl _) hV),
change _ = (_ : ATs) • _,
rw ← mul_smul,
congr' 1,
change ⟦((1 : A), _)⟧ = ⟦(1 * 1, _)⟧,
simpa [pow_succ'],
end
lemma mul_left (hT : is_open (↑(ideal.span T) : set A)) (a : ATs) (U : open_add_subgroup A) :
∃ (V : open_add_subgroup A), a • (Dspan (V : set A)) ≤ Dspan (U : set A) :=
begin
apply localization.induction_on a,
intros a' s',
clear a,
cases mul_left.aux₂ _ _ hT s' U with W hW,
cases exists_mul_left_subset T s Huber_ring.nonarchimedean W a' with V hV,
use V,
erw [localization.mk_eq, mul_comm, mul_smul],
exact le_trans (smul_le_smul (le_refl _) hV) hW
end
instance (hT : is_open (↑(ideal.span T) : set A)) :
topological_space ATs :=
topology_of_submodules_comm
(λ U : open_add_subgroup A, span D (of_id A ATs '' U.1))
(directed T s) (mul_left T s hT) (mul_le T s Huber_ring.nonarchimedean)
instance (hT : is_open (↑(ideal.span T) : set A)) :
ring_with_zero_nhd ATs :=
of_submodules_comm
(λ U : open_add_subgroup A, span D (of_id A ATs '' U.1))
(directed T s) (mul_left T s hT) (mul_le T s Huber_ring.nonarchimedean)
section
variables {B : Type*} [comm_ring B] [topological_space B] [topological_ring B]
variables (hB : nonarchimedean B) {f : A → B} [is_ring_hom f] (hf : continuous f)
variables {fs_inv : units B} (hs : fs_inv.inv = f s)
variables (hT : is_open (↑(ideal.span T) : set A))
variables (hTB : is_power_bounded_subset ((↑fs_inv : B) • f '' T))
include hs
lemma is_unit : is_unit (f s) :=
by rw [← hs, ← units.coe_inv]; exact is_unit_unit _
noncomputable def lift : ATs → B := localization.away.lift f (is_unit s hs)
instance : is_ring_hom (lift T s hs : ATs → B) :=
localization.away.lift.is_ring_hom f _
@[simp] lemma lift_of (a : A) :
lift T s hs (of a) = f a := localization.away.lift_of _ _ _
@[simp] lemma lift_coe (a : A) :
lift T s hs a = f a := localization.away.lift_of _ _ _
@[simp] lemma lift_comp_of :
lift T s hs ∘ of = f := localization.lift'_comp_of _ _ _
lemma of_continuous (hT : is_open (↑(ideal.span T) : set A)) :
@continuous _ _ _ (away.topological_space T s hT) (of : A → ATs) :=
begin
apply of_submodules_comm.continuous _,
all_goals {try {apply_instance}},
intro U,
apply open_add_subgroup.is_open_of_open_add_subgroup _,
all_goals {try {apply_instance}},
{ use U,
rw ← image_subset_iff,
exact subset_span },
{ apply is_add_group_hom.preimage _ _,
all_goals {apply_instance} }
end
include hB hf hT hTB
lemma lift_continuous : @continuous _ _ (away.topological_space T s hT) _ (lift T s hs) :=
begin
apply continuous_of_continuous_at_zero _ _,
all_goals {try {apply_instance}},
intros U hU,
rw is_ring_hom.map_zero (lift T s hs) at hU,
rw filter.mem_map_sets_iff,
let hF := power_bounded.ring.closure' hB _ hTB,
erw is_bounded_add_subgroup_iff hB at hF,
rcases hF U hU with ⟨V, hVF⟩,
let hV := V.mem_nhds_zero,
rw ← is_ring_hom.map_zero f at hV,
replace hV := hf.tendsto 0 hV,
rw filter.mem_map_sets_iff at hV,
rcases hV with ⟨W, hW, hWV⟩,
cases Huber_ring.nonarchimedean W hW with Y hY,
refine ⟨↑(Dspan Y), _, _⟩,
{ apply mem_nhds_sets,
{ convert of_submodules_comm.is_open Y },
{ exact (Dspan ↑Y).zero_mem } },
{ refine set.subset.trans _ hVF,
rintros _ ⟨x, hx, rfl⟩,
apply span_induction hx,
{ rintros _ ⟨a, ha, rfl⟩,
erw [lift_of, ← mul_one (f a)],
refine mul_mem_mul (subset_span $ hWV $ ⟨a, hY ha, rfl⟩)
(subset_span $ is_submonoid.one_mem _) },
{ rw is_ring_hom.map_zero (lift T s hs),
exact is_add_submonoid.zero_mem _ },
{ intros a b ha hb,
rw is_ring_hom.map_add (lift T s hs),
exact is_add_submonoid.add_mem ha hb },
{ rw [submodule.smul_def, span_mul_span],
intros d a ha,
rw [(show d • a = ↑d * a, from rfl), is_ring_hom.map_mul (lift T s hs), mul_comm],
rcases mem_span_iff_lc.mp ha with ⟨l, hl₁, hl₂⟩,
rw lc.mem_supported at hl₁,
rw [← hl₂, lc.total_apply] at ha ⊢,
rw finsupp.sum_mul,
apply sum_mem_span,
intros b hb',
show (↑(_ : ℤ) * _) * _ ∈ _,
rcases hl₁ hb' with ⟨v, hv, b, hb, rfl⟩,
refine ⟨↑(l (v * b)) * v, _, b * lift T s hs ↑d, _, _⟩,
{ rw ← gsmul_eq_mul, exact is_add_subgroup.gsmul_mem hv },
{ refine is_submonoid.mul_mem hb _,
cases d with d hd,
rw subtype.coe_mk,
apply ring.in_closure.rec_on hd,
{ rw is_ring_hom.map_one (lift T s hs), exact is_submonoid.one_mem _ },
{ rw [is_ring_hom.map_neg (lift T s hs), is_ring_hom.map_one (lift T s hs)],
exact is_add_subgroup.neg_mem (is_submonoid.one_mem _) },
{ rintros _ ⟨sinv, hsinv, _, ⟨t, ht, rfl⟩, rfl⟩ b hb,
rw is_ring_hom.map_mul (lift T s hs),
refine is_submonoid.mul_mem _ hb,
apply ring.mem_closure,
erw [is_ring_hom.map_mul (lift T s hs), lift_of],
refine ⟨_, _, _, ⟨t, ht, rfl⟩, rfl⟩,
rw mem_singleton_iff at hsinv ⊢,
subst hsinv,
erw [← units.coe_map (lift T s hs), ← units.ext_iff, is_group_hom.inv (units.map _),
inv_eq_iff_inv_eq, units.ext_iff, units.coe_inv, hs],
{ symmetry, exact lift_of T s hs s },
{ apply_instance } },
{ intros a b ha hb,
rw is_ring_hom.map_add (lift T s hs),
exact is_add_submonoid.add_mem ha hb } },
{ repeat {rw mul_assoc} } } }
end
end
end away
end Huber_ring
|
a211f55429ec4a380875ccad7f590aaa25852a18 | e38d5e91d30731bef617cc9b6de7f79c34cdce9a | /src/examples/permutation.lean | 141ab89e5568eb65537e19e04f0e997fd123746a | [
"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 | 391 | lean | /-
Copyright (c) 2019 Bruno Bentzen. All rights reserved.
Released under the Apache License 2.0 (see "License");
Author: Bruno Bentzen
-/
import ..core.interval
open interval
-- permutation (exchange) for type and terms
example {A : I → I → Type} : I → I → Type := λ j i, A i j
example {A : I → I → Type} (a : Π j i, A i j) : Π j i, A j i := λ j i, a i j
|
f5d694f7bddf046adf10587747c1573b15a6b3a2 | b7f22e51856f4989b970961f794f1c435f9b8f78 | /tests/lean/run/example1.lean | 87393445b29b98e08a3a842dd5df909bd14249f9 | [
"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 | 295 | lean | import logic
inductive day :=
monday | tuesday | wednesday | thursday | friday | saturday | sunday
namespace day
definition next_weekday d :=
day.rec_on d tuesday wednesday thursday friday monday monday monday
example : next_weekday (next_weekday saturday) = tuesday :=
rfl
end day
|
0828f74a3e3a1dca5195498074772663f13b0214 | 626e312b5c1cb2d88fca108f5933076012633192 | /src/algebra/group/basic.lean | 5c616733aedde06263c6cea826c9c4bc22bc17bc | [
"Apache-2.0"
] | permissive | Bioye97/mathlib | 9db2f9ee54418d29dd06996279ba9dc874fd6beb | 782a20a27ee83b523f801ff34efb1a9557085019 | refs/heads/master | 1,690,305,956,488 | 1,631,067,774,000 | 1,631,067,774,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 17,209 | lean | /-
Copyright (c) 2014 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, Leonardo de Moura, Simon Hudon, Mario Carneiro
-/
import algebra.group.defs
import logic.function.basic
/-!
# Basic lemmas about semigroups, monoids, and groups
This file lists various basic lemmas about semigroups, monoids, and groups. Most proofs are
one-liners from the corresponding axioms. For the definitions of semigroups, monoids and groups, see
`algebra/group/defs.lean`.
-/
universe u
section associative
variables {α : Type u} (f : α → α → α) [is_associative α f] (x y : α)
/--
Composing two associative operations of `f : α → α → α` on the left
is equal to an associative operation on the left.
-/
lemma comp_assoc_left : (f x) ∘ (f y) = (f (f x y)) :=
by { ext z, rw [function.comp_apply, @is_associative.assoc _ f] }
/--
Composing two associative operations of `f : α → α → α` on the right
is equal to an associative operation on the right.
-/
lemma comp_assoc_right : (λ z, f z x) ∘ (λ z, f z y) = (λ z, f z (f y x)) :=
by { ext z, rw [function.comp_apply, @is_associative.assoc _ f] }
end associative
section semigroup
variables {α : Type*}
/--
Composing two multiplications on the left by `y` then `x`
is equal to a multiplication on the left by `x * y`.
-/
@[simp, to_additive
"Composing two additions on the left by `y` then `x`
is equal to a addition on the left by `x + y`."]
lemma comp_mul_left [semigroup α] (x y : α) :
((*) x) ∘ ((*) y) = ((*) (x * y)) :=
comp_assoc_left _ _ _
/--
Composing two multiplications on the right by `y` and `x`
is equal to a multiplication on the right by `y * x`.
-/
@[simp, to_additive
"Composing two additions on the right by `y` and `x`
is equal to a addition on the right by `y + x`."]
lemma comp_mul_right [semigroup α] (x y : α) :
(* x) ∘ (* y) = (* (y * x)) :=
comp_assoc_right _ _ _
end semigroup
section mul_one_class
variables {M : Type u} [mul_one_class M]
@[to_additive]
lemma ite_mul_one {P : Prop} [decidable P] {a b : M} :
ite P (a * b) 1 = ite P a 1 * ite P b 1 :=
by { by_cases h : P; simp [h], }
@[to_additive]
lemma eq_one_iff_eq_one_of_mul_eq_one {a b : M} (h : a * b = 1) : a = 1 ↔ b = 1 :=
by split; { rintro rfl, simpa using h }
@[to_additive]
lemma one_mul_eq_id : ((*) (1 : M)) = id := funext one_mul
@[to_additive]
lemma mul_one_eq_id : (* (1 : M)) = id := funext mul_one
end mul_one_class
section comm_semigroup
variables {G : Type u} [comm_semigroup G]
@[no_rsimp, to_additive]
lemma mul_left_comm : ∀ a b c : G, a * (b * c) = b * (a * c) :=
left_comm has_mul.mul mul_comm mul_assoc
attribute [no_rsimp] add_left_comm
@[to_additive]
lemma mul_right_comm : ∀ a b c : G, a * b * c = a * c * b :=
right_comm has_mul.mul mul_comm mul_assoc
@[to_additive]
theorem mul_mul_mul_comm (a b c d : G) : (a * b) * (c * d) = (a * c) * (b * d) :=
by simp only [mul_left_comm, mul_assoc]
end comm_semigroup
local attribute [simp] mul_assoc sub_eq_add_neg
section add_monoid
variables {M : Type u} [add_monoid M] {a b c : M}
@[simp] lemma bit0_zero : bit0 (0 : M) = 0 := add_zero _
@[simp] lemma bit1_zero [has_one M] : bit1 (0 : M) = 1 :=
by rw [bit1, bit0_zero, zero_add]
end add_monoid
section comm_monoid
variables {M : Type u} [comm_monoid M] {x y z : M}
@[to_additive] lemma inv_unique (hy : x * y = 1) (hz : x * z = 1) : y = z :=
left_inv_eq_right_inv (trans (mul_comm _ _) hy) hz
end comm_monoid
section left_cancel_monoid
variables {M : Type u} [left_cancel_monoid M] {a b : M}
@[simp, to_additive] lemma mul_right_eq_self : a * b = a ↔ b = 1 :=
calc a * b = a ↔ a * b = a * 1 : by rw mul_one
... ↔ b = 1 : mul_left_cancel_iff
@[simp, to_additive] lemma self_eq_mul_right : a = a * b ↔ b = 1 :=
eq_comm.trans mul_right_eq_self
end left_cancel_monoid
section right_cancel_monoid
variables {M : Type u} [right_cancel_monoid M] {a b : M}
@[simp, to_additive] lemma mul_left_eq_self : a * b = b ↔ a = 1 :=
calc a * b = b ↔ a * b = 1 * b : by rw one_mul
... ↔ a = 1 : mul_right_cancel_iff
@[simp, to_additive] lemma self_eq_mul_left : b = a * b ↔ a = 1 :=
eq_comm.trans mul_left_eq_self
end right_cancel_monoid
section div_inv_monoid
variables {G : Type u} [div_inv_monoid G]
@[to_additive]
lemma inv_eq_one_div (x : G) :
x⁻¹ = 1 / x :=
by rw [div_eq_mul_inv, one_mul]
@[to_additive]
lemma mul_one_div (x y : G) :
x * (1 / y) = x / y :=
by rw [div_eq_mul_inv, one_mul, div_eq_mul_inv]
lemma mul_div_assoc {a b c : G} : a * b / c = a * (b / c) :=
by rw [div_eq_mul_inv, div_eq_mul_inv, mul_assoc _ _ _]
lemma mul_div_assoc' (a b c : G) : a * (b / c) = (a * b) / c :=
mul_div_assoc.symm
@[simp, to_additive] lemma one_div (a : G) : 1 / a = a⁻¹ :=
(inv_eq_one_div a).symm
end div_inv_monoid
section group
variables {G : Type u} [group G] {a b c : G}
@[simp, to_additive]
lemma inv_mul_cancel_right (a b : G) : a * b⁻¹ * b = a :=
by simp [mul_assoc]
@[simp, to_additive neg_zero]
lemma one_inv : 1⁻¹ = (1 : G) :=
inv_eq_of_mul_eq_one (one_mul 1)
@[to_additive]
theorem left_inverse_inv (G) [group G] :
function.left_inverse (λ a : G, a⁻¹) (λ a, a⁻¹) :=
inv_inv
@[simp, to_additive]
lemma inv_involutive : function.involutive (has_inv.inv : G → G) := inv_inv
@[simp, to_additive]
lemma inv_surjective : function.surjective (has_inv.inv : G → G) :=
inv_involutive.surjective
@[to_additive]
lemma inv_injective : function.injective (has_inv.inv : G → G) :=
inv_involutive.injective
@[simp, to_additive] theorem inv_inj : a⁻¹ = b⁻¹ ↔ a = b := inv_injective.eq_iff
@[simp, to_additive]
lemma mul_inv_cancel_left (a b : G) : a * (a⁻¹ * b) = b :=
by rw [← mul_assoc, mul_right_inv, one_mul]
@[to_additive]
theorem mul_left_surjective (a : G) : function.surjective ((*) a) :=
λ x, ⟨a⁻¹ * x, mul_inv_cancel_left a x⟩
@[to_additive]
theorem mul_right_surjective (a : G) : function.surjective (λ x, x * a) :=
λ x, ⟨x * a⁻¹, inv_mul_cancel_right x a⟩
@[simp, to_additive neg_add_rev]
lemma mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ :=
inv_eq_of_mul_eq_one $ by simp
@[to_additive]
lemma eq_inv_of_eq_inv (h : a = b⁻¹) : b = a⁻¹ :=
by simp [h]
@[to_additive]
lemma eq_inv_of_mul_eq_one (h : a * b = 1) : a = b⁻¹ :=
have a⁻¹ = b, from inv_eq_of_mul_eq_one h,
by simp [this.symm]
@[to_additive]
lemma eq_mul_inv_of_mul_eq (h : a * c = b) : a = b * c⁻¹ :=
by simp [h.symm]
@[to_additive]
lemma eq_inv_mul_of_mul_eq (h : b * a = c) : a = b⁻¹ * c :=
by simp [h.symm]
@[to_additive]
lemma inv_mul_eq_of_eq_mul (h : b = a * c) : a⁻¹ * b = c :=
by simp [h]
@[to_additive]
lemma mul_inv_eq_of_eq_mul (h : a = c * b) : a * b⁻¹ = c :=
by simp [h]
@[to_additive]
lemma eq_mul_of_mul_inv_eq (h : a * c⁻¹ = b) : a = b * c :=
by simp [h.symm]
@[to_additive]
lemma eq_mul_of_inv_mul_eq (h : b⁻¹ * a = c) : a = b * c :=
by simp [h.symm, mul_inv_cancel_left]
@[to_additive]
lemma mul_eq_of_eq_inv_mul (h : b = a⁻¹ * c) : a * b = c :=
by rw [h, mul_inv_cancel_left]
@[to_additive]
lemma mul_eq_of_eq_mul_inv (h : a = c * b⁻¹) : a * b = c :=
by simp [h]
@[simp, to_additive]
theorem inv_eq_one : a⁻¹ = 1 ↔ a = 1 :=
by rw [← @inv_inj _ _ a 1, one_inv]
@[simp, to_additive]
theorem one_eq_inv : 1 = a⁻¹ ↔ a = 1 :=
by rw [eq_comm, inv_eq_one]
@[to_additive]
theorem inv_ne_one : a⁻¹ ≠ 1 ↔ a ≠ 1 :=
not_congr inv_eq_one
@[to_additive]
theorem eq_inv_iff_eq_inv : a = b⁻¹ ↔ b = a⁻¹ :=
⟨eq_inv_of_eq_inv, eq_inv_of_eq_inv⟩
@[to_additive]
theorem inv_eq_iff_inv_eq : a⁻¹ = b ↔ b⁻¹ = a :=
eq_comm.trans $ eq_inv_iff_eq_inv.trans eq_comm
@[to_additive]
theorem mul_eq_one_iff_eq_inv : a * b = 1 ↔ a = b⁻¹ :=
⟨eq_inv_of_mul_eq_one, λ h, by rw [h, mul_left_inv]⟩
@[to_additive]
theorem mul_eq_one_iff_inv_eq : a * b = 1 ↔ a⁻¹ = b :=
by rw [mul_eq_one_iff_eq_inv, eq_inv_iff_eq_inv, eq_comm]
@[to_additive]
theorem eq_inv_iff_mul_eq_one : a = b⁻¹ ↔ a * b = 1 :=
mul_eq_one_iff_eq_inv.symm
@[to_additive]
theorem inv_eq_iff_mul_eq_one : a⁻¹ = b ↔ a * b = 1 :=
mul_eq_one_iff_inv_eq.symm
@[to_additive]
theorem eq_mul_inv_iff_mul_eq : a = b * c⁻¹ ↔ a * c = b :=
⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩
@[to_additive]
theorem eq_inv_mul_iff_mul_eq : a = b⁻¹ * c ↔ b * a = c :=
⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩
@[to_additive]
theorem inv_mul_eq_iff_eq_mul : a⁻¹ * b = c ↔ b = a * c :=
⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩
@[to_additive]
theorem mul_inv_eq_iff_eq_mul : a * b⁻¹ = c ↔ a = c * b :=
⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩
@[to_additive]
theorem mul_inv_eq_one : a * b⁻¹ = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inv]
@[to_additive]
theorem inv_mul_eq_one : a⁻¹ * b = 1 ↔ a = b :=
by rw [mul_eq_one_iff_eq_inv, inv_inj]
@[to_additive]
lemma div_left_injective : function.injective (λ a, a / b) :=
by simpa only [div_eq_mul_inv] using λ a a' h, mul_left_injective (b⁻¹) h
@[to_additive]
lemma div_right_injective : function.injective (λ a, b / a) :=
by simpa only [div_eq_mul_inv] using λ a a' h, inv_injective (mul_right_injective b h)
-- The unprimed version is used by `group_with_zero`. This is the preferred choice.
-- See https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/.60div_one'.60
@[simp, to_additive sub_zero]
lemma div_one' (a : G) : a / 1 = a :=
calc a / 1 = a * 1⁻¹ : div_eq_mul_inv a 1
... = a * 1 : congr_arg _ one_inv
... = a : mul_one a
end group
section add_group
-- TODO: Generalize the contents of this section with to_additive as per
-- https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.238667
variables {G : Type u} [add_group G] {a b c d : G}
@[simp] lemma sub_self (a : G) : a - a = 0 :=
by rw [sub_eq_add_neg, add_right_neg a]
@[simp] lemma sub_add_cancel (a b : G) : a - b + b = a :=
by rw [sub_eq_add_neg, neg_add_cancel_right a b]
@[simp] lemma add_sub_cancel (a b : G) : a + b - b = a :=
by rw [sub_eq_add_neg, add_neg_cancel_right a b]
lemma add_sub_assoc (a b c : G) : a + b - c = a + (b - c) :=
by rw [sub_eq_add_neg, add_assoc, ←sub_eq_add_neg]
lemma eq_of_sub_eq_zero (h : a - b = 0) : a = b :=
calc a = a - b + b : (sub_add_cancel a b).symm
... = b : by rw [h, zero_add]
lemma sub_ne_zero_of_ne (h : a ≠ b) : a - b ≠ 0 :=
mt eq_of_sub_eq_zero h
@[simp] lemma sub_neg_eq_add (a b : G) : a - (-b) = a + b :=
by rw [sub_eq_add_neg, neg_neg]
@[simp] lemma neg_sub (a b : G) : -(a - b) = b - a :=
neg_eq_of_add_eq_zero (by rw [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left,
add_right_neg])
local attribute [simp] add_assoc
lemma add_sub (a b c : G) : a + (b - c) = a + b - c :=
by simp
lemma sub_add_eq_sub_sub_swap (a b c : G) : a - (b + c) = a - c - b :=
by simp
@[simp] lemma add_sub_add_right_eq_sub (a b c : G) : (a + c) - (b + c) = a - b :=
by rw [sub_add_eq_sub_sub_swap]; simp
lemma eq_sub_of_add_eq (h : a + c = b) : a = b - c :=
by simp [← h]
lemma sub_eq_of_eq_add (h : a = c + b) : a - b = c :=
by simp [h]
lemma eq_add_of_sub_eq (h : a - c = b) : a = b + c :=
by simp [← h]
lemma add_eq_of_eq_sub (h : a = c - b) : a + b = c :=
by simp [h]
@[simp] lemma sub_right_inj : a - b = a - c ↔ b = c :=
sub_right_injective.eq_iff
@[simp] lemma sub_left_inj : b - a = c - a ↔ b = c :=
by { rw [sub_eq_add_neg, sub_eq_add_neg], exact add_left_inj _ }
lemma sub_add_sub_cancel (a b c : G) : (a - b) + (b - c) = a - c :=
by rw [← add_sub_assoc, sub_add_cancel]
lemma sub_sub_sub_cancel_right (a b c : G) : (a - c) - (b - c) = a - b :=
by rw [← neg_sub c b, sub_neg_eq_add, sub_add_sub_cancel]
theorem sub_sub_assoc_swap : a - (b - c) = a + c - b :=
by simp
theorem sub_eq_zero : a - b = 0 ↔ a = b :=
⟨eq_of_sub_eq_zero, λ h, by rw [h, sub_self]⟩
alias sub_eq_zero ↔ _ sub_eq_zero_of_eq
theorem sub_ne_zero : a - b ≠ 0 ↔ a ≠ b :=
not_congr sub_eq_zero
@[simp] theorem sub_eq_self : a - b = a ↔ b = 0 :=
by rw [sub_eq_add_neg, add_right_eq_self, neg_eq_zero]
theorem eq_sub_iff_add_eq : a = b - c ↔ a + c = b :=
by rw [sub_eq_add_neg, eq_add_neg_iff_add_eq]
theorem sub_eq_iff_eq_add : a - b = c ↔ a = c + b :=
by rw [sub_eq_add_neg, add_neg_eq_iff_eq_add]
theorem eq_iff_eq_of_sub_eq_sub (H : a - b = c - d) : a = b ↔ c = d :=
by rw [← sub_eq_zero, H, sub_eq_zero]
theorem left_inverse_sub_add_left (c : G) : function.left_inverse (λ x, x - c) (λ x, x + c) :=
assume x, add_sub_cancel x c
theorem left_inverse_add_left_sub (c : G) : function.left_inverse (λ x, x + c) (λ x, x - c) :=
assume x, sub_add_cancel x c
theorem left_inverse_add_right_neg_add (c : G) :
function.left_inverse (λ x, c + x) (λ x, - c + x) :=
assume x, add_neg_cancel_left c x
theorem left_inverse_neg_add_add_right (c : G) :
function.left_inverse (λ x, - c + x) (λ x, c + x) :=
assume x, neg_add_cancel_left c x
end add_group
section comm_group
variables {G : Type u} [comm_group G]
@[to_additive neg_add]
lemma mul_inv (a b : G) : (a * b)⁻¹ = a⁻¹ * b⁻¹ :=
by rw [mul_inv_rev, mul_comm]
@[to_additive]
lemma div_eq_of_eq_mul' {a b c : G} (h : a = b * c) : a / b = c :=
by rw [h, div_eq_mul_inv, mul_comm, inv_mul_cancel_left]
@[to_additive]
lemma div_mul_comm (a b c d : G) : a / b * (c / d) = a * c / (b * d) :=
by rw [div_eq_mul_inv, div_eq_mul_inv, div_eq_mul_inv, mul_inv_rev, mul_assoc, mul_assoc,
mul_left_cancel_iff, mul_comm, mul_assoc]
end comm_group
section add_comm_group
-- TODO: Generalize the contents of this section with to_additive as per
-- https://leanprover.zulipchat.com/#narrow/stream/144837-PR-reviews/topic/.238667
variables {G : Type u} [add_comm_group G] {a b c d : G}
local attribute [simp] add_assoc add_comm add_left_comm sub_eq_add_neg
lemma sub_add_eq_sub_sub (a b c : G) : a - (b + c) = a - b - c :=
by simp
lemma neg_add_eq_sub (a b : G) : -a + b = b - a :=
by simp
lemma sub_add_eq_add_sub (a b c : G) : a - b + c = a + c - b :=
by simp
lemma sub_sub (a b c : G) : a - b - c = a - (b + c) :=
by simp
lemma sub_add (a b c : G) : a - b + c = a - (b - c) :=
by simp
@[simp] lemma add_sub_add_left_eq_sub (a b c : G) : (c + a) - (c + b) = a - b :=
by simp
lemma eq_sub_of_add_eq' (h : c + a = b) : a = b - c :=
by simp [h.symm]
lemma eq_add_of_sub_eq' (h : a - b = c) : a = b + c :=
by simp [h.symm]
lemma add_eq_of_eq_sub' (h : b = c - a) : a + b = c :=
begin simp [h], rw [add_comm c, add_neg_cancel_left] end
lemma sub_sub_self (a b : G) : a - (a - b) = b :=
begin simp, rw [add_comm b, add_neg_cancel_left] end
lemma add_sub_comm (a b c d : G) : a + b - (c + d) = (a - c) + (b - d) :=
by simp
lemma sub_eq_sub_add_sub (a b c : G) : a - b = c - b + (a - c) :=
begin simp, rw [add_left_comm c], simp end
lemma neg_neg_sub_neg (a b : G) : - (-a - -b) = a - b :=
by simp
@[simp] lemma sub_sub_cancel (a b : G) : a - (a - b) = b := sub_sub_self a b
lemma sub_eq_neg_add (a b : G) : a - b = -b + a :=
by rw [sub_eq_add_neg, add_comm _ _]
theorem neg_add' (a b : G) : -(a + b) = -a - b :=
by rw [sub_eq_add_neg, neg_add a b]
@[simp]
lemma neg_sub_neg (a b : G) : -a - -b = b - a :=
by simp [sub_eq_neg_add, add_comm]
lemma eq_sub_iff_add_eq' : a = b - c ↔ c + a = b :=
by rw [eq_sub_iff_add_eq, add_comm]
lemma sub_eq_iff_eq_add' : a - b = c ↔ a = b + c :=
by rw [sub_eq_iff_eq_add, add_comm]
@[simp]
lemma add_sub_cancel' (a b : G) : a + b - a = b :=
by rw [sub_eq_neg_add, neg_add_cancel_left]
@[simp]
lemma add_sub_cancel'_right (a b : G) : a + (b - a) = b :=
by rw [← add_sub_assoc, add_sub_cancel']
-- This lemma is in the `simp` set under the name `add_neg_cancel_comm_assoc`,
-- defined in `algebra/group/commute`
lemma add_add_neg_cancel'_right (a b : G) : a + (b + -a) = b :=
by rw [← sub_eq_add_neg, add_sub_cancel'_right a b]
lemma sub_right_comm (a b c : G) : a - b - c = a - c - b :=
by { repeat { rw sub_eq_add_neg }, exact add_right_comm _ _ _ }
@[simp] lemma add_add_sub_cancel (a b c : G) : (a + c) + (b - c) = a + b :=
by rw [add_assoc, add_sub_cancel'_right]
@[simp] lemma sub_add_add_cancel (a b c : G) : (a - c) + (b + c) = a + b :=
by rw [add_left_comm, sub_add_cancel, add_comm]
@[simp] lemma sub_add_sub_cancel' (a b c : G) : (a - b) + (c - a) = c - b :=
by rw add_comm; apply sub_add_sub_cancel
@[simp] lemma add_sub_sub_cancel (a b c : G) : (a + b) - (a - c) = b + c :=
by rw [← sub_add, add_sub_cancel']
@[simp] lemma sub_sub_sub_cancel_left (a b c : G) : (c - a) - (c - b) = b - a :=
by rw [← neg_sub b c, sub_neg_eq_add, add_comm, sub_add_sub_cancel]
lemma sub_eq_sub_iff_add_eq_add : a - b = c - d ↔ a + d = c + b :=
begin
rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, eq_comm, sub_eq_iff_eq_add'],
simp only [add_comm, eq_comm]
end
lemma sub_eq_sub_iff_sub_eq_sub : a - b = c - d ↔ a - c = b - d :=
by rw [sub_eq_iff_eq_add, sub_add_eq_add_sub, sub_eq_iff_eq_add', add_sub_assoc]
end add_comm_group
|
07f2eb73bea8e6ef7d2d3c30a014994d5da9ae49 | ea5678cc400c34ff95b661fa26d15024e27ea8cd | /scratch.lean | 55adf9ef3bc0cdb852ad26e503cb17e39cfc64bb | [] | 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 | 220,608 | lean | import tactic
#print thunk
open set
open_locale nnreal
#print nat.strong_induction_on
def SUBMISSION : Prop := Pi (x : ℝ≥0) (P : set ℝ≥0) (hP : is_open P)
(ih : ∀ x : ℝ≥0, (∀ y, y < x → y ∈ P) → x ∈ P), x ∈ P
lemma nnreal_induction_on (x : ℝ≥0) (P : set ℝ≥0) (hP : is_open P)
(ih : ∀ x : ℝ≥0, (∀ y, y < x → y ∈ P) → x ∈ P) : x ∈ P :=
classical.by_contradiction $ λ hx,
have hbI : bdd_below Pᶜ, from ⟨0, λ _, by simp⟩,
have hI : Inf Pᶜ ∈ Pᶜ,
from is_closed.cInf_mem (is_closed_compl_iff.mpr hP) ⟨x, hx⟩ hbI,
hI (ih _ (λ y hyI, by_contradiction $ λ hy, not_le_of_gt hyI (cInf_le hbI hy)))
def G : SUBMISSION := @nnreal_induction_on
#print axioms G
lemma nnreal_recursion {α : Type*} [topological_space α]
(ih : Π x : ℝ≥0, (∀ y, y < x → α) → α) (Hih : count(x : ℝ≥0) : α :=
#exit
import data.nat.prime data.nat.parity tactic
#print nat.even_pow
theorem even_of_prime_succ_pow (a b : ℕ) (ha : a > 1) (hb : b > 1)
(hp : nat.prime (a^b + 1)) : 2 ∣ a :=
have ¬ even (a ^ b + 1),
from hp.eq_two_or_odd.elim
(λ h, begin
have : 1 < a ^ b, from nat.one_lt_pow _ _ (by linarith) ha,
linarith
end)
(by simp [nat.not_even_iff]),
begin
rw [nat.even_add, iff_false_intro nat.not_even_one, iff_false, not_not,
nat.even_pow] at this,
exact this.1
end
#exit
import category_theory.limits.shapes.pullbacks
open category_theory
open category_theory.limits
example {A B C : Prop} : A ∧ (B ∨ C) ↔ (A ∧ B) ∨ (A ∧ C) :=
⟨λ h, h.right.elim (λ hB, or.inl ⟨h.left, hB⟩) (λ hC, or.inr ⟨h.left, hC⟩),
λ h, h.elim (λ hAB, ⟨hAB.left, or.inl hAB.right⟩) (λ hAC, ⟨hAC.left, or.inr hAC.right⟩)⟩
example {A B C : Prop} : A ∨ (B ∧ C) ↔ (A ∨ B) ∧ (A ∨ C) :=
⟨λ h, h.elim (λ hA, ⟨or.inl hA, or.inl hA⟩) (λ hBC, ⟨or.inr hBC.left, or.inr hBC.right⟩),
λ h, h.left.elim or.inl (λ hB, h.right.elim or.inl (λ hC, or.inr ⟨hB, hC⟩))⟩
universes v u
variables {C : Type u} [category.{v} C]
def pushout_of_epi {X Y : C} (f : X ⟶ Y) [epi f] :
is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f) :=
pushout_cocone.is_colimit.mk
_
_
_
(λ s, s.inl)
(by simp)
(λ s, @epi.left_cancellation _ _ _ _ f _ _ _ _
(by simp [s.condition]))
(by simp { contextual := tt })
theorem epi_of_pushout {X Y : C} (f : X ⟶ Y)
(is_colim : is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f)) : epi f :=
{ left_cancellation := λ Z g h H,
(is_colim.fac (pushout_cocone.mk _ _ H) (walking_span.left)).symm.trans
(is_colim.fac (pushout_cocone.mk _ _ H) (walking_span.right))}
#exit
import tactic
def arith_sum : ℕ → ℕ
| 0 := 0
| (nat.succ n) := nat.succ n + arith_sum n
def arith_formula (n : ℕ) : ℕ := n * (n + 1) / 2
theorem arith_eq_aux (n : ℕ) : arith_sum n * 2 = n * (n + 1) :=
begin
induction n with n ih,
{ refl },
{ rw [arith_sum, add_mul, ih, nat.succ_eq_add_one],
ring }
end
theorem arith_eq (n : ℕ) : arith_formula n = arith_sum n :=
nat.div_eq_of_eq_mul_left (by norm_num) (arith_eq_aux n).symm
#exit
import category_theory.limits.shapes.pullbacks
open category_theory
open category_theory.limits
universes v u
variables {C : Type u} [category.{v} C]
#print limit.lift
#print walking_cospan
#print pullback_cone
#print cone
def right_is_pullback {X Y Z U V : C} (f : X ⟶ Y) (g : X ⟶ Z) (u₁ : Y ⟶ U) (u₂ : Z ⟶ U)
(v₁ : Y ⟶ V) (v₂ : Z ⟶ V) (hu : f ≫ u₁ = g ≫ u₂) (hv : f ≫ v₁ = g ≫ v₂)
(is_pullback : is_limit (pullback_cone.mk _ _ hu))
(is_pushout : is_colimit (pushout_cocone.mk _ _ hv)) :
is_limit (pullback_cone.mk _ _ hv) :=
let h : V ⟶ U := is_pushout.desc (pushout_cocone.mk u₁ u₂ hu) in
have Hh₁ : v₁ ≫ h = u₁, from is_pushout.fac (pushout_cocone.mk u₁ u₂ hu) walking_span.left,
have Hh₂ : v₂ ≫ h = u₂, from is_pushout.fac (pushout_cocone.mk u₁ u₂ hu) walking_span.right,
let S : pullback_cone v₁ v₂ → pullback_cone u₁ u₂ :=
λ s, pullback_cone.mk s.fst s.snd
(by rw [← Hh₁, ← Hh₂, ← category.assoc, ← category.assoc, s.condition]) in
pullback_cone.is_limit.mk _ _ _
(λ s, is_pullback.lift (S s))
(λ s, is_pullback.fac (S s) walking_cospan.left)
(λ s, is_pullback.fac (S s) walking_cospan.right)
begin
assume s (m : s.X ⟶ X) h₁ h₂,
refine is_pullback.uniq (S s) m (λ j, _),
cases j,
{ dsimp,
simp [← h₁] },
{ cases j; dsimp; simp * }
end
#exit
import algebra.group
class my_group (G : Type*) extends semigroup G :=
( middle : ∀ (x : G), ∃! (y : G), x * y * x = x)
variables {G : Type*} [my_group G] --[nonempty G]
noncomputable theory
namespace my_group
instance A : has_inv G := ⟨λ x, classical.some (my_group.middle x)⟩
lemma mul_inv_mul (x : G) : x * x⁻¹ * x = x :=
(classical.some_spec (my_group.middle x)).1
lemma inv_unique {x y : G} (h : x * y * x = x) : y = x⁻¹ :=
(classical.some_spec (my_group.middle x)).2 _ h
variable [nonempty G]
open_locale classical
variables (x y z : G)
def one := x * x⁻¹
lemma one_inv : (one x)⁻¹ = one x :=
(inv_unique begin
delta one,
assoc_rw [mul_inv_mul, mul_inv_mul],
end).symm
example : (x * y)⁻¹ = y⁻¹ * x⁻¹ :=
eq.symm (inv_unique _)
example : x * x⁻¹ * y * y⁻¹ = (y⁻¹ * y)⁻¹ :=
inv_unique _
lemma one_eq_one : one x = (y * y⁻¹) :=
begin
end
@[simp] lemma inv_inv : x⁻¹⁻¹ = x :=
(inv_unique (inv_unique (by assoc_rw [mul_inv_mul, mul_inv_mul]))).symm
lemma inv_mul_inv : x⁻¹ * x * x⁻¹ = x⁻¹ :=
calc x⁻¹ * x * x⁻¹ = x⁻¹ * x⁻¹⁻¹ * x⁻¹ : by rw inv_inv
... = x⁻¹ : mul_inv_mul (x⁻¹)
lemma one_eq_one : one (x⁻¹) = one x :=
begin
rw [← one_inv x, one, one],
refine (inv_unique _),
simp only [mul_assoc],
refine congr_arg _ _,
refine (inv_unique _),
assoc_rw [mul_inv_mul],
rw inv_inv,
end
example : (x * y)⁻¹ = y⁻¹ * x⁻¹ :=
(inv_unique _).symm
example : x⁻¹ * 1 = x⁻¹ :=
inv_unique _
example : y⁻¹ * x⁻¹ * x = y⁻¹ :=
inv_unique _
lemma mul_one_mul_arbitrary {x : G} : x * 1 * classical.arbitrary G = x * classical.arbitrary G :=
lemma mul_one (x : G) : x * x⁻¹ = 1 :=
end my_group
#exit
import data.real.basic
/- We first define uniform convergence -/
def unif_convergence (f : ℕ → ℝ → ℝ) (g : ℝ → ℝ) :=
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, ∀ x : ℝ, abs (f n x - g x) < ε
/- Use the notation f → g to denote that the sequence fₙ converges uniformly to g
You can type ⟶ by writing \hom -/
notation f `⟶` g := unif_convergence f g
/- We also define the notion of the limit of a function ℝ → ℝ at a point -/
def fun_limit (f : ℝ → ℝ) (a l) :=
∀ ε > 0, ∃ δ > 0, ∀ x : ℝ, x ≠ a → abs (x - a) < δ → abs (f x - l) < ε
/- And the notion of the limit of a sequence -/
def seq_limit (f : ℕ → ℝ) (l) :=
∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, abs (f n - l) < ε
/- If fₙ is a sequence of functions which converge uniformly to a function g
and if lim_{x → a} fₙ(x) exists for each n then lim_{x → a} g(x) exists
and is equal to lim_{n → ∞} lim_{x → a} fₙ(x). -/
theorem limits_commute (f : ℕ → ℝ → ℝ) (g : ℝ → ℝ) (l : ℕ → ℝ) (a : ℝ) :
(f ⟶ g) → (∀ n, fun_limit (f n) a (l n)) → ∃ l', fun_limit g a l' ∧ seq_limit l l' :=
begin
rw [unif_convergence],
delta fun_limit seq_limit,
assume hfg hs,
end
#exit
import data.fintype.basic
import set_theory.ordinal_arithmetic
import order.order_iso_nat
universe u
variable {α : Type u}
--open ordinal
attribute [elab_as_eliminator] well_founded.fix
open_locale classical
noncomputable theory
#print ordinal.omega
noncomputable def nat_embedding [infinite α] (r : α → α → Prop)
(hrwo : is_well_order α r) : ℕ → α
| n := well_founded.min hrwo.wf
(finset.univ.image (λ m : fin n, have wf : m.1 < n := m.2, nat_embedding m.1))ᶜ
(infinite.exists_not_mem_finset
(finset.univ.image (λ m : fin n, have wf : m.1 < n := m.2, nat_embedding m.1)))
#print rel_embedding.swap
theorem fintype_of_well_order (r : α → α → Prop)
(hrwo : is_well_order α r) (hrwo' : is_well_order α (λ x y, r y x)) :
nonempty (fintype α) :=
classical.by_contradiction $ λ h,
have cardinal.omega ≤ (ordinal.type r).card,
from le_of_not_gt (mt cardinal.lt_omega_iff_fintype.1 h),
have ordinal.omega ≤ ordinal.type r,
by rwa [← cardinal.ord_omega, cardinal.ord_le],
have f : nonempty (((>) : ℕ → ℕ → Prop) ↪r function.swap r),
begin
rw [ordinal.omega, ← ordinal.lift_id (ordinal.type r)] at this,
rcases (ordinal.lift_type_le.{0 u}.1 this) with ⟨f, hf⟩,
exact ⟨f.swap⟩,
end,
rel_embedding.well_founded_iff_no_descending_seq.1 hrwo'.wf f
theorem fintype_of_well_order (r : α → α → Prop)
(hrwo : is_well_order α r) (hrwo' : is_well_order α (λ x y, r y x)) :
nonempty (fintype α) :=
classical.by_contradiction $ λ h,
have infin : infinite α, from ⟨h ∘ nonempty.intro⟩,
let a : α := classical.choice (@infinite.nonempty α ⟨h ∘ nonempty.intro⟩) in
let f : ℕ → α := λ n, well_founded.fix (well_founded.min hrwo.wf set.univ ⟨a, trivial⟩)
(well_founded.min _ _)
#exit
/-- The hat color announced by the dwarf with the number `n` upon seeing the colors of the hats in
front of him: `see i` is the color of the hat of the dwarf with the number `n + i + 1`. -/
noncomputable def announce (n : ℕ) (see : ℕ → α) : α :=
choose_fun (λ m, see (m - n - 1)) n
-- see' :
-- announce (n + 1) (λ n, see (n + 1)) = see 0
/-- Only finitely many dwarves announce an incorrect color. -/
theorem announce_correct_solution (col : ℕ → α) :
∃ N : ℕ, ∀ n ≥ N, col n = announce n (λ m, col (m + n + 1)) :=
have ∀ n : ℕ, choose_fun (λ m : ℕ, col (m - n - 1 + n + 1)) = choose_fun col,
from λ n, choose_fun_eq ⟨n + 1, λ k hk,
by rw [add_assoc, nat.sub_sub, nat.sub_add_cancel hk]⟩,
begin
delta announce,
simp only [this],
exact choose_fun_spec _
end
#exit
import category_theory.functor
import category_theory.types
import category_theory.monad
import algebra.module
open category_theory monad
@[simps]
def P : Type ⥤ Type :=
{ obj := λ X, set X,
map := λ X Y, set.image }
variables {R : Type 1} [comm_ring R] {M : Type} [add_comm_group M]
#check module R M
#check Σ (M : Type) [add_comm_group M], by exactI module R M
instance powerset_monad : monad P :=
{ η :=
{ app := λ X x, ({x} : set X),
naturality' :=
λ X Y f, (funext (@set.image_singleton _ _ f)).symm },
μ :=
{ app := @set.sUnion,
naturality' := begin
intros X Y f,
ext,
simp,
dsimp [P_obj],
tauto
end },
assoc' := begin
intro X,
ext,
simp,
dsimp,
tauto
end,
left_unit' := begin
intro X,
dsimp,
ext,
simp,
end,
right_unit' := begin
intro X,
dsimp,
ext,
simp
end }
#exit
inductive tm : Type
| zro : tm
| scc : tm -> tm
| pls : tm -> tm -> tm
| nul : tm
| cns : tm -> tm -> tm
| len : tm -> tm
| idx : tm -> tm -> tm
| stn : tm -> tm
open tm
inductive nvalue : tm -> Prop
| nv_zro : nvalue zro
| nv_scc : Π v1, nvalue v1 -> nvalue (scc v1)
open nvalue
inductive lvalue : tm -> Prop
| lv_nul : lvalue nul
| lv_cns : Π v1 v2,
nvalue v1 -> lvalue v2 -> lvalue (cns v1 v2)
def value (t : tm) := nvalue t ∨ lvalue t
inductive step : tm -> tm -> Prop
notation x ` ⟶ `:100 y := step x y
| ST_Scc : ∀ t1 t1',
(t1 ⟶ t1') →
scc t1 ⟶ scc t1'
| ST_PlsZro : ∀ v1,
nvalue v1 →
pls zro v1 ⟶ v1
| ST_PlsScc : ∀ v1 v2,
nvalue v1 →
nvalue v2 →
pls (scc v1) v2 ⟶ scc (pls v1 v2)
| ST_Pls2 : ∀ v1 t2 t2',
nvalue v1 →
(t2 ⟶ t2') →
pls v1 t2 ⟶ pls v1 t2'
| ST_Pls1 : ∀ t1 t1' t2,
(t1 ⟶ t1') →
pls t1 t2 ⟶ pls t1' t2
| ST_Cns2 : ∀ v1 t2 t2',
nvalue v1 →
(t2 ⟶ t2') →
cns v1 t2 ⟶ cns v1 t2'
| ST_Cns1 : ∀ t1 t1' t2,
(t1 ⟶ t1') →
cns t1 t2 ⟶ cns t1' t2
| ST_LenNul :
len nul ⟶ zro
| ST_LenCns : ∀ v1 v2,
nvalue v1 →
lvalue v2 →
len (cns v1 v2) ⟶ scc (len v2)
| ST_Len : ∀ t1 t1',
(t1 ⟶ t1') →
len t1 ⟶ len t1'
| ST_IdxZro : ∀ v1 v2,
nvalue v1 →
lvalue v2 →
idx zro (cns v1 v2) ⟶ v1
| ST_IdxScc : ∀ v1 v2 v3,
nvalue v1 →
nvalue v2 →
lvalue v3 →
idx (scc v1) (cns v2 v3) ⟶ idx v1 v3
| ST_Idx2 : ∀ v1 t2 t2',
nvalue v1 →
(t2 ⟶ t2') →
idx v1 t2 ⟶ idx v1 t2'
| ST_Idx1 : ∀ t1 t1' t2,
(t1 ⟶ t1') →
idx t1 t2 ⟶ idx t1' t2
| ST_StnNval : ∀ v1,
nvalue v1 →
stn v1 ⟶ cns v1 nul
| ST_Stn : ∀ t1 t1',
(t1 ⟶ t1') →
stn t1 ⟶ stn t1'
open step
infix ` ⟶ `:100 := step
def relation (X : Type) := X → X → Prop
def deterministic {X : Type} (R : relation X) :=
∀ x y1 y2 : X, R x y1 → R x y2 → y1 = y2
inductive ty : Type
| Nat : ty
| List : ty
open ty
inductive has_type : tm → ty → Prop
notation `⊢ `:79 t ` :∈ `:80 T := has_type t T
| T_Zro :
⊢ zro :∈ Nat
| T_Scc : ∀ t1,
(⊢ t1 :∈ Nat) →
⊢ scc t1 :∈ Nat
| T_Pls : ∀ t1 t2,
(⊢ t1 :∈ Nat) →
(⊢ t2 :∈ Nat) →
⊢ pls t1 t2 :∈ Nat
| T_Nul :
⊢ nul :∈ List
| T_Cns : ∀ t1 t2,
(⊢ t1 :∈ Nat) →
(⊢ t2 :∈ List) →
⊢ cns t1 t2 :∈ List
| T_Len : ∀ t1,
(⊢ t1 :∈ List) →
(⊢ len t1 :∈ Nat)
| T_Idx : ∀ t1 t2,
(⊢ t1 :∈ Nat) →
(⊢ t2 :∈ List) →
⊢ idx t1 t2 :∈ Nat
| T_Stn : ∀ t1,
(⊢ t1 :∈ Nat) →
⊢ stn t1 :∈ List
open has_type
notation `⊢ `:19 t ` :∈ `:20 T := has_type t T
def progress := ∀ t T,
(⊢ t :∈ T) →
value t ∨ ∃ t', t ⟶ t'
def preservation := ∀ t t' T,
(⊢ t :∈ T) →
t ⟶ t' →
⊢ t' :∈ T
inductive multi {X : Type} (R : relation X) : relation X
| multi_refl : ∀ (x : X), multi x x
| multi_step : ∀ (x y z : X),
R x y →
multi y z →
multi x z
def multistep := (multi step).
infix ` ⟶* `:100 := (multistep)
def normal_form {X : Type} (R : relation X) (t : X) : Prop :=
¬ ∃ t', R t t'
notation `step_normal_form` := (normal_form step)
def stuck (t : tm) : Prop :=
step_normal_form t ∧ ¬ value t
def soundness := ∀ t t' T,
(⊢ t :∈ T) →
t ⟶* t' →
¬ stuck t'
theorem step_deterministic : deterministic step :=
begin
unfold deterministic,
assume x y1 y2 h1 h2,
induction h1; induction h2; simp *,
end
/- Uncomment one of the following two: -/
-- theorem progress_dec : progress := sorry
-- theorem progress_dec : ¬ progress := sorry
/- Uncomment one of the following two: -/
-- theorem preservation_dec : preservation := sorry
-- theorem preservation_dec : ¬ preservation := sorry
/- Uncomment one of the following two: -/
-- lemma soundness_dec : soundness := sorry
-- lemma soundness_dec : ¬ soundness := sorry
#exit
import data.list
import data.stream
import data.nat.basic
import data.nat.fib
import data.nat.parity
open nat
def fib_aux : ℕ → ℕ → ℕ → ℕ
| a b 0 := a
| a b (n + 1) := fib_aux b (a + b) n
def fib2 (n : ℕ) : ℕ := fib_aux 0 1 n
def fib_from (a b : ℕ) : ℕ → ℕ
| 0 := a
| 1 := b
| (n+2) := fib_from n + fib_from (n+1)
lemma fib_from_thing (a b : ℕ) : ∀ n, fib_from b (a + b) n = fib_from a b n.succ
| 0 := rfl
| 1 := rfl
| (n+2) := begin
rw [fib_from, fib_from_thing, fib_from, fib_from_thing],
end
lemma fib_aux_eq : ∀ a b n : ℕ, fib_aux a b n = fib_from a b n
| a b 0 := rfl
| a b 1 := rfl
| a b (n+2) := begin
rw [fib_aux, fib_from, fib_aux_eq],
rw [fib_from_thing, fib_from],
end
lemma fib_from_eq_fib : ∀ n, fib_from 0 1 n = fib n
| 0 := rfl
| 1 := rfl
| (n+2 ) := begin
rw [fib_from, fib_from_eq_fib, fib_succ_succ, fib_from_eq_fib],
end
theorem fib_eq (n : ℕ) : fib2 n = fib n :=
begin
rw [fib2, fib_aux_eq, fib_from_eq_fib],
end
theorem fib_fast_correct (n : ℕ) : fib_fast n = fib n :=
begin
end
#exit
import category_theory.functor
import category_theory.types
import category_theory.monad
import data.set.basic
@[simps]
def P : Type ⥤ Type :=
{ obj := λ X, set X,
map := λ X Y, set.image }
open category_theory monad
#print has_singleton
instance powerset_monad : monad P :=
{ η :=
{ app := λ X, as_hom (has_singleton.singleton : X → set X) },
μ :=
{ app := λ X, set.sUnion,
naturality' := begin
assume X Y f,
ext,
dsimp [P_obj, P_map],
end } }
open category_theory monad
universe u
-- A suggested solution method.
-- You are *not* required to use this.
@[simps]
def contravariant_powerset : (Type u)ᵒᵖ ⥤ Type u :=
{ obj := λ X, set X.unop,
map := λ X Y f, as_hom (set.preimage f.unop),
map_id' := λ x, by dsimp; refl,
map_comp' := λ X Y Z f g, by { dsimp at *, ext1, dsimp at *, ext1, simp at * } }
#print op_op
instance : is_right_adjoint contravariant_powerset :=
{ left := unop_unop (Type u) ⋙ contravariant_powerset.op,
adj := adjunction.mk_of_unit_counit
{ unit :=
{ app := λ X (x : X), ({S : set X | x ∈ S} : set (set X)),
naturality' := by { intros X Y f, refl} },
counit :=
{ app := λ X,
let f : X.unop ⟶ set (set (opposite.unop X)) :=
as_hom (λ x : X.unop, ({S : set X.unop | x ∈ S} : set (set X.unop))) in
begin
have := f.op,
end,
naturality' := begin
intros X Y f,
refine has_hom.hom.unop_inj _,
dsimp at *, refl,
end },
left_triangle' := begin
dsimp at *, simp at *, ext1, dsimp at *, ext1, dsimp at *, simp at *,
refine has_hom.hom.unop_inj _,
dsimp at *, refl
end,
right_triangle' := by { dsimp at *, simp at *, ext1, dsimp at *, ext1, dsimp at *, refl} } }
@[simps]
def PP : Type u ⥤ Type u :=
{ obj := λ X, set (set X),
map := λ X Y f, set.preimage (set.preimage f) }
def PP1 : Type u ⥤ Type u :=
unop_unop (Type u) ⋙ contravariant_powerset.op ⋙ contravariant_powerset
lemma PP_eq_PP1 : PP.{u} = PP1 := rfl
instance double_powerset_monad : category_theory.monad PP :=
by rw [PP_eq_PP1]; exact adjunction.monad _
#exit
import data.fintype.basic
universe u
variable {α : Type u}
local attribute [elab_as_eliminator] well_founded.fix
theorem fintype_of_well_order (r : α → α → Prop)
(hrwo : is_well_order α r) (hrwo' : is_well_order α (λ x y, r y x)) :
nonempty (fintype α) :=
classical.by_contradiction $ λ h,
have ∀ a : α, ¬ nonempty (fintype {b // r a b}),
from sorry,
def SUBMISSION : Prop :=
∀ {G : Type} [group G] {a b : G} (h : by exactI a * b * a * b^2 = 1),
by exactI a * b = b * a
set_option profiler true
lemma F {G : Type} [group G] {a b : G} (h : a * b * a * b ^ 2 = 1) :
a * b = b * a :=
calc a * b = (b⁻¹ * a⁻¹ * (a * b * a * b^2)
* a * b * b * (a * b * a * b^2)⁻¹ * b⁻¹) * b * a : by group
... = b * a : by rw h; group
lemma G {G : Type} [group G] {a b : G} (n : ℕ) (h : (a * b) ^ n * b = 1) :
a * b = b * a :=
calc a * b = a * b^2 * ((a * b) ^ n * b) * b^ (-2 : ℤ) * a⁻¹ * b *
((a * b) ^ n * b)⁻¹ * b⁻¹ * b * a :
begin
simp only [mul_assoc, pow_two, mul_inv_cancel_left, mul_inv_rev, gpow_neg,
pow_bit0, gpow_bit0, gpow_one, pow_one],
rw [← mul_assoc b⁻¹ a⁻¹, ← mul_assoc _ ((a * b) ^n)⁻¹, ← mul_inv_rev,
← mul_inv_rev, ← pow_succ', pow_succ],
simp [mul_assoc]
end
... = _ : by rw h; group
#exit
import tactic
variables {G : Type} [group G]
def fib_thing (b₁ b₀ : G) : ℕ → G
| 0 := b₁
| 1 := b₁ * b₀
| (n+2) := fib_thing (n + 1) * fib_thing n
lemma a_mul_fib_thing {a b₁ b₀ : G} (hab₁ : a * b₁ = b₁ * b₀ * a)
(hab₀ : a * b₀ = b₁ * a) : ∀ n : ℕ,
a * fib_thing b₁ b₀ n = fib_thing b₁ b₀ (n + 1) * a
| 0 := by simp [fib_thing, hab₁]
| 1 := begin
unfold fib_thing,
rw [← mul_assoc, hab₁, mul_assoc, hab₀, mul_assoc, mul_assoc, mul_assoc],
end
| (n+2) := by rw [fib_thing, ← mul_assoc, a_mul_fib_thing, mul_assoc, a_mul_fib_thing,
fib_thing, fib_thing, fib_thing, mul_assoc, mul_assoc, mul_assoc]
lemma X (a b₁ b₀ : G) (hab₁ : a * b₁ = b₁ * b₀ * a)
(hab₀ : a * b₀ = b₁ * a) :
∀ (n : ℕ), a^n * b₁ = fib_thing b₁ b₀ n * a ^ n
| 0 := by simp [fib_thing]
| (n+1):= by rw [pow_succ, mul_assoc, X, ← mul_assoc, a_mul_fib_thing hab₁ hab₀,
mul_assoc]
lemma Y (a b₀ bₙ₁ : G) (hab₁ : a⁻¹ * bₙ₁ = bₙ₁⁻¹ * b₀ * a)
(hab₀ : a⁻¹ * b₀ = bₙ₁ * a) :
#exit
import linear_algebra.tensor_algebra
import data.real.basic
/--
attempt to unmathlibify
-/
variables (R : Type) [ring R] (M : Type) [add_comm_group M] [module R M]
/-
semimodule.add_comm_monoid_to_add_comm_group :
Π (R : Type u) {M : Type w} [_inst_1 : ring R] [_inst_2 : add_comm_monoid M]
[_inst_3 : semimodule R M], add_comm_group M
-/
def typealias (α : Type) := ℤ
local attribute [irreducible] tensor_algebra
-- def foo : add_comm_group (tensor_algebra ℤ ℤ) := by apply_instance -- tensor_algebra.ring ℤ
-- def bar : add_comm_group (typealias bool) := by unfold typealias; apply_instance
-- def foo' : add_comm_group (tensor_algebra ℤ ℤ) :=
-- semimodule.add_comm_monoid_to_add_comm_group ℤ
-- def bar' : add_comm_group (typealias bool) := by unfold typealias;
-- exact semimodule.add_comm_monoid_to_add_comm_group ℤ
--instance foo'' : ring (tensor_algebra ℤ ℤ) := by apply_instance
#print tactic.dsimp_config
local attribute [irreducible] typealias
local attribute [irreducible] tensor_algebra
instance foo' : ring (tensor_algebra ℤ ℤ) :=
{ ..semimodule.add_comm_monoid_to_add_comm_group (tensor_algebra ℤ ℤ),
..(infer_instance : semiring (tensor_algebra ℤ ℤ)) }
instance foo : ring (tensor_algebra ℤ ℤ) := tensor_algebra.ring ℤ
example : derive_handler := by library_search
@[derive ring] def C := int
#print d_array
#print C.ring
set_option pp.implicit true
set_option pp.proofs true
--lemma X : @ring.add_zero _ foo = @ring.add_zero _ foo' := rfl
#print declaration
#print expr
run_cmd tactic.add_decl
(declaration.thm `X [] `(@ring.add_zero _ foo = @ring.add_zero _ foo')
(pure `(eq.refl (@ring.add_zero _ foo))))
#print X
-- works when `tensor_algebra` is not irreducible
-- example : @add_comm_group.add_zero _ foo = @add_comm_group.add_zero _ foo' := rfl
-- works when `typealias` is not irreducible, but *statement* doesn't compile if it is
example : @add_comm_group.add_zero _ bar = @add_comm_group.add_zero _ bar' :=
rfl
#exit
#print list.range
example {G : Type*} [group G] (a b : G) (hab : a * b = b * a⁻¹ * b * a^2) :
a * a * a * b = sorry :=
have hab' : ∀ g : G, a * (b * g) = b * (a⁻¹ * (b * (a * (a * g)))),
from λ g, by rw [← mul_assoc, hab]; simp [pow_two, mul_assoc],
begin
simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left],
try { rw [hab] <|> rw hab'},
simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left],
try { rw [hab] <|> rw hab'},
simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left],
try { rw [hab] <|> rw hab'},
simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left],
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
try { rw [hab] <|> rw hab'},
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
rw [hab] <|> rw hab',
try { simp only [mul_assoc, pow_two, mul_left_inv, mul_right_inv, one_inv,
mul_inv_cancel_left, inv_mul_cancel_left] },
end
example {G : Type*} [group G] (a b : G) (hab : a * b = b * a⁻¹ * b * a^2) (n : ℕ) :
a^n * b = ((list.range n).map (λ i, b ^ (2 ^ i) * a⁻¹)).prod * b
* a ^ (2 ^ (n + 1))
example {G : Type*} [group G] (a b : G) (hab : a * b = b * a⁻¹ * b * a^2) (n : ℕ) :
a^(n + 1) * b = ((list.range (n+1)).map (λ i, b ^ (2 ^ i) * a⁻¹)).prod * b
* a ^ (2 ^ (n + 1)) :=
begin
induction n with n ih,
{ simp [list.range, list.range_core, hab] },
{ rw [pow_succ, mul_assoc, ih, eq_comm, list.range_concat, list.map_append],
simp, }
end
#exit
notation `C∞` := multiplicative ℤ
open multiplicative
lemma to_add_injective {A : Type*} : function.injective (to_add : A → multiplicative A) :=
λ _ _, id
@[derive group] def Z_half : Type :=
multiplicative (localization.away (2 : ℤ))
def phi : C∞ →* mul_aut Z_half :=
gpowers_hom _
(show mul_aut Z_half, from
{ to_fun := λ x, of_add (to_add x * 2),
inv_fun := λ x, of_add (to_add x * localization.away.inv_self 2),
left_inv := λ _, to_add_injective sorry,
right_inv := λ _, to_add_injective sorry,
map_mul' := λ _ _, to_add_injective sorry })
@[derive group] def BS : Type := Z_half ⋊[phi] C∞
@[simp] lemma zero_denom : (0 : ℚ).denom = 1 := rfl
def lift {G : Type*} [group G] (a b : G) (hab : a * b * a⁻¹ = b^2) : BS →* G :=
semidirect_product.lift
_
(gpowers_hom _ a)
_
#exit
@[derive decidable_eq] inductive BS : Type
| neg : ℕ+ → ℤ → BS
| nonneg : ℤ → ℕ → BS
namespace BS
private def one : BS := nonneg 0 0
private def inv : BS → BS
| (neg n i) := nonneg (-i) n
| (nonneg i n) :=
if hn : 0 < n then neg ⟨n, hn⟩ (-i) else nonneg (-i) 0
private def mul : BS → BS → BS
| (nonneg i₁ n₁) (nonneg i₂ n₂) := nonneg (i₁ + i₂ + i₂.sign * max n₁ i₂.nat_abs) (n₁ + n₂)
| (nonneg i₁ n₁) (neg n₂ i₂) :=
if hn : (n₂ : ℕ) ≤ n₁
then nonneg (i₁ + i₂ + i₂.sign * max (n₁ - n₂) i₂.nat_abs) (n₁ - n₂)
else neg ⟨n₂ - n₁, nat.sub_pos_of_lt (lt_of_not_ge hn)⟩
(i₁.sign * max (n₂ - n₁) i₁.nat_abs + i₁ + i₂)
| (neg n₁ i₁) (nonneg i₂ n₂) :=
if hn : (n₁ : ℕ) ≤ n₂
then nonneg _ (n₂ - n₁)
else _
end BS
#exit
private def mul (a b : B) : B :=
if b.conj +
private def one : BS := ⟨0, 0, 0, dec_trivial⟩
instance : has_one BS := ⟨one⟩
private def inv (a : BS) : BS :=
⟨a.right, -a.middle, a.left, by cases a; simp; tauto⟩
instance : has_inv BS := ⟨inv⟩
private def mul (a b : BS) : BS :=
let x : {x : ℕ × ℕ // a.middle + b.middle = 0 → x.1 = 0 ∨ x.2 = 0} :=
let m : ℤ := a.right - b.left in
if h : a.middle + b.middle = 0
then let k : ℤ := a.left + b.left - a.right - b.right in
if 0 ≤ k
then ⟨(k.to_nat, 0), λ _, or.inr rfl⟩
else ⟨(0, k.nat_abs), λ _, or.inl rfl⟩
else if 0 ≤ m
then if 0 ≤ b.middle
then let n := min m.to_nat b.middle.to_nat in
⟨(a.left + n, a.right + b.right), false.elim ∘ h⟩
else let n := min m.to_nat b.middle.nat_abs in
⟨(a.left, b.right + n + m.to_nat), false.elim ∘ h⟩
else if 0 ≤ a.middle
then let n := min m.nat_abs a.middle.to_nat in
⟨(a.left + n + m.nat_abs, b.right), false.elim ∘ h⟩
else let n := min m.nat_abs a.middle.nat_abs in
⟨(a.left + b.left, b.right + n), false.elim ∘ h⟩ in
⟨x.1.1, a.middle + b.middle, x.1.2, x.2⟩
instance : has_mul BS := ⟨mul⟩
@[simp] lemma int.coe_nat_le_zero (a : ℕ) : (a : ℤ) ≤ 0 ↔ a = 0 := sorry
private lemma mul_one (a : BS) : mul a one = a :=
begin
cases a,
simp [mul, one],
split_ifs; finish [nat.not_lt_zero]
end
private lemma mul_inv (a : BS) : mul a (inv a) = one :=
begin
cases a,
simp [mul, one, inv]
end
private lemma mul_left (a b : BS) : (mul a b).left =
let m : ℤ := a.right - b.left in
if h : a.middle + b.middle = 0
then let k : ℤ := a.left + b.left - a.right - b.right in
if 0 ≤ k
then k.to_nat
else 0
else if 0 ≤ m
then if 0 ≤ b.middle
then a.left + min m.to_nat b.middle.to_nat
else a.left
else if 0 ≤ a.middle
then a.left + min m.nat_abs a.middle.to_nat + m.nat_abs
else a.left + b.left :=
by simp [mul]; split_ifs; simp
@[simp] private lemma mul_middle (a b : BS) : (mul a b).middle = a.middle + b.middle := rfl
private lemma mul_assoc (a b c : BS) : mul (mul a b) c = mul a (mul b c) :=
begin
cases a, cases b, cases c,
ext,
{ simp only [mul_left, mul_middle, add_assoc],
dsimp,
by_cases h : a_middle + b_middle + c_middle = 0,
{ rw [dif_pos h, dif_pos ((add_assoc _ _ _).symm.trans h)],
split_ifs, }
}
end
#exit
import analysis.special_functions.trigonometric
open complex real
example (θ φ : ℝ) (h1 : θ ≤ pi) (h2 : -pi < θ)
(h3 : 0 < cos φ) : arg (exp (I * θ) * cos φ) = θ :=
by rw [mul_comm, ← of_real_cos, arg_real_mul _ h3, mul_comm,
exp_mul_I, arg_cos_add_sin_mul_I h2 h1]
#print prod.lex
#print is_lawful_singleton
example (A B : Prop) (h : A → ¬ ¬ B) (hnnA : ¬ ¬ A) : ¬ ¬ B :=
λ hB, hnnA (λ hA, h hA hB)
lemma X : ¬ ¬ (∀ p, p ∨ ¬ p) → ∀ p, p ∨ ¬ p :=
begin
isafe, -- goals accomplished
end
#print axioms not_forall
#print X
example : ¬¬(∀ p, p ∨ ¬ p) := by ifinish
example (A B C : Prop) :
A ∨ B ∨ C →
(A → ¬ B ∧ ¬ C) →
(B → ¬ A ∧ ¬ C) →
(C → ¬ A ∧ ¬ B) →
(A ↔ A) →
(B ↔ (A ∨ B)) →
(C ↔ (B ∨ C)) →
C :=
λ h hA1 hB1 hC1 hA2 hB2 hC2,
have A → B, by tauto!, by tauto!
example (A B C : Prop) :
A ∨ B ∨ C →
(A → ¬ B ∧ ¬ C) →
(B → ¬ A ∧ ¬ C) →
(C → ¬ A ∧ ¬ B) →
(A ↔ A) →
(B ↔ (A ∨ B)) →
(C ↔ (B ∨ C)) →
C :=
λ h hA1 hB1 hC1 hA2 hB2 hC2,
match h with
| (or.inl hA) := ((hA1 hA).1 (hB2.2 (or.inl hA))).elim
| (or.inr (or.inl hB)) := ((hB1 hB).2 (hC2.2 (or.inl hB))).elim
| (or.inr (or.inr hC)) := hC
end
#exit
import data.list.basic
import data.list
import tactic
import order.lexicographic
variables {α : Type*} (r : α → α → Prop) (wf : well_founded r)
lemma list.lex.cons_iff' {a₁ a₂ : α} {l₁ l₂ : list α} :
list.lex r (a₁ :: l₁) (a₂ :: l₂) ↔ r a₁ a₂ ∨ a₁ = a₂ ∧ list.lex r l₁ l₂ :=
begin
split,
{ assume h,
cases h; simp * },
{ assume h,
rcases h with hr | ⟨rfl, h⟩,
{ exact list.lex.rel hr },
{ exact list.lex.cons h } }
end
lemma lex_wf_aux : ∀ (n : ℕ),
well_founded
(inv_image (list.lex r)
(subtype.val : {l : list α // l.length = n} → list α))
| 0 := subrelation.wf
(begin
rintros l₁ ⟨l₂, hl₂⟩,
simp [inv_image, empty_relation, list.length_eq_zero.1 hl₂]
end)
empty_wf
| (n+1) :=
let f : {l : list α // l.length = n + 1} → α × {l : list α // l.length = n} :=
λ l, (l.val.nth_le 0 (by rw [l.2]; exact nat.succ_pos _),
subtype.mk l.1.tail $ by simp [list.length_tail, l.prop]) in
subrelation.wf
(begin
rintros ⟨l₁, hl₁⟩ ⟨l₂, hl₂⟩,
cases l₁,
{ exact (nat.succ_ne_zero _ hl₁.symm).elim },
cases l₂,
{ exact (nat.succ_ne_zero _ hl₂.symm).elim },
simp [inv_image, list.lex.cons_iff', prod.lex_def]
end)
(inv_image.wf f (prod.lex_wf wf (lex_wf_aux n)))
lemma psigma.lex_def {α : Sort*} {β : α → Sort*}
{r : α → α → Prop} {s : Π a, β a → β a → Prop} {a b : psigma β} :
psigma.lex r s a b ↔ r a.fst b.fst ∨
∃ h : a.fst = b.fst, s b.fst (cast (congr_arg β h) a.snd) b.snd :=
begin
split,
{ intro h,
induction h; simp * },
{ intro h,
cases a with a₁ a₂,
cases b with b₁ b₂,
dsimp at h,
rcases h with hr | ⟨rfl, h⟩,
{ exact psigma.lex.left _ _ hr },
{ exact psigma.lex.right _ h } }
end
#print list.lex
lemma list.lex_wf : well_founded (list.lex r) :=
let f : list α → Σ' n : ℕ, {l : list α // l.length = n} :=
λ l, ⟨l.length, l, rfl⟩ in
subrelation.wf (show subrelation _
(inv_image (psigma.lex (<) (λ n, inv_image (list.lex r)
(subtype.val : {l' : list α // l'.length = n} → list α))) f),
begin
intros l₁ l₂ h,
dsimp only [inv_image, f],
induction h with a l a l₁ l₂ h ih,
{ exact psigma.lex.left _ _ (nat.succ_pos _) },
{ rw [psigma.lex_def] at ih,
rcases ih with hr | ⟨hl, hlex⟩,
{ exact psigma.lex.left _ _ (nat.succ_lt_succ hr) },
{ dsimp at hl,
simp only [psigma.lex_def, lt_irrefl, hl, list.length_cons, false_or,
exists_prop_of_true, set_coe_cast, subtype.coe_mk],
exact list.lex.cons (by simpa [hl] using hlex) } },
{ }
end)
(inv_image.wf f (psigma.lex_wf
(show well_founded has_lt.lt, from nat.lt_wf)
(lex_wf_aux r wf)))
def lex_wf_fun : list α → α ⊕ unit
| [] := sum.inr ()
| (a::l) := sum.inl a
#print list.lex
example {l₁ l₂ : list α} (h : list.lex r l₁ l₂) :
prod.lex (<) (sum.lex r (λ _ _, false))
(l₁.length, lex_wf_fun l₁)
(l₂.length, lex_wf_fun l₂) :=
begin
induction h,
{ simp [lex_wf_fun, prod.lex_def] },
{ simp [lex_wf_fun, prod.lex_def] at *, admit },
{ simp [lex_wf_fun, prod.lex_def, *] at * }
end
lemma acc_lex_nil : acc (list.lex r) [] :=
acc.intro _ (λ l hl, (list.lex.not_nil_right _ _ hl).elim)
lemma list.lex.cons_iff' {a₁ a₂ : α} {l₁ l₂ : list α} :
list.lex r (a₁ :: l₁) (a₂ :: l₂) ↔ r a₁ a₂ ∨ a₁ = a₂ ∧ list.lex r l₁ l₂ :=
begin
split,
{ assume h,
cases h; simp * },
{ assume h,
rcases h with hr | ⟨rfl, h⟩,
{ exact list.lex.rel hr },
{ exact list.lex.cons h } }
end
#print psigma.lex
#print pi.lex
include wf
local attribute [elab_as_eliminator] well_founded.fix
example (l : list α) : acc (list.lex r) l :=
begin
induction l with a₁ l₁ ih,
{ exact acc_lex_nil _ },
{ refine acc.intro _ (λ l₂ hl₂, _),
induction l₂ with a₂ l₂ ih₂,
{ exact acc_lex_nil _ },
{ rw [list.lex.cons_iff'] at hl₂,
rcases hl₂ with hr | ⟨rfl, h⟩,
{ refine well_founded.fix wf _ a₂,
assume x ih,
refine acc.intro _ (λ l₃ hl₃, _),
induction hl₃,
{ exact acc_lex_nil _ },
{ } }
}
}
end
example (P : ℤ → Prop) (h8 : ∀ n, P n → P (n + 8))
(h3 : ∀ n, P n → P (n - 3)) : ∀ n, P n → P (n + 1) :=
λ n hn, begin
have := h8 _ (h8 _ (h3 _ (h3 _ (h3 _ (h3 _ (h3 n hn)))))),
ring at this,
exact this
end
#exit
#print list.lex
meta def ack_list : list ℕ → list ℕ
| [] := []
| [n] := [n]
| (n::0::l) := ack_list ((n+1)::l)
| (0::(m+1)::l) := ack_list (1::m::l)
| ((n+1)::(m+1)::l) := ack_list (n::(m+1)::m::l)
#eval ack_list [2, 3]
#exit
import algebra.ring
inductive nat2 : Type
| zero : nat2
| succ : nat2 → nat2
namespace nat2
variables {α : Type} (z : α) (s : α → α)
def lift (n : nat2) : α :=
nat2.rec_on n z (λ _, s)
@[simp] lemma lift_zero :
lift z s zero = z := rfl
@[simp] lemma lift_succ (n : nat2) :
lift z s (succ n) = s (lift z s n):= rfl
attribute [irreducible] lift
lemma hom_ext {f g : nat2 → α}
(hfz : f zero = z) (hfs : ∀ n, f (succ n) = s (f n))
(hgz : g zero = z) (hgs : ∀ n, g (succ n) = s (g n))
(n : nat2) :
f n = g n :=
begin
induction n with n ih,
{ rw [hfz, hgz] },
{ rw [hfs, hgs, ih] }
end
@[simp] lemma lift_zero_succ (n : nat2) : lift zero succ n = n :=
hom_ext zero succ (by simp) (by simp) rfl (by simp) n
def add (n : nat2) : nat2 → nat2 := lift n succ
infix ` + ` := add
-- Prove adding on the left is a hom
@[simp] lemma add_zero (n : nat2) : n + zero = n :=
lift_zero _ _
@[simp] lemma add_succ (m n : nat2) : m + succ n = succ (m + n) :=
lift_succ _ _ _
-- Prove adding on the right is a hom
@[simp] lemma zero_add (n : nat2) : zero + n = n :=
hom_ext zero succ
(by simp [add])
(by simp [add])
(by simp [add])
(λ n, by simp [add])
n
@[simp] lemma succ_add (m n : nat2) : succ m + n = succ (m + n) :=
hom_ext (succ m) succ
(by simp [add])
(by simp [add])
(by simp [add])
(by simp [add])
n
lemma add_comm (m n : nat2) : m + n = n + m :=
hom_ext m succ (add_zero _) (add_succ _) (zero_add _) (λ n, succ_add n m) _
lemma add_assoc (a b c : nat2): (a + b) + c = a + (b + c) :=
hom_ext (a + b) succ
(by simp)
(by simp)
(by simp)
(by simp)
c
lemma add_left_comm (a b c : nat2): a + (b + c) = b + (a + c) :=
by rw [← add_assoc, add_comm a b, add_assoc]
def mul (n : nat2) : nat2 → nat2 := lift zero (+ n)
infix ` * ` := mul
-- Prove multiplication on the left is a hom
@[simp] lemma mul_zero (n : nat2) : n * zero = zero := by simp [mul]
@[simp] lemma mul_succ (m n : nat2) : m * succ n = (m * n) + m := by simp [mul]
-- Prove multiplication on the right is a hom
@[simp] lemma zero_mul (n : nat2) : zero * n = zero :=
hom_ext zero (+ zero)
(by simp [mul])
(by simp [mul])
(by simp [mul])
(by simp [mul])
n
@[simp] lemma succ_mul (m n : nat2) : succ m * n = n + (m * n):=
hom_ext zero (+ m.succ)
(by simp)
(by simp [mul])
(by simp [mul])
(by simp [mul, add_comm, add_assoc])
n
lemma mul_comm (m n : nat2) : m * n = n * m :=
hom_ext zero (+ m)
(by simp)
(by simp)
(by simp)
(by simp [add_comm])
n
lemma mul_add (a b c : nat2) : a * (b + c) = a * b + a * c :=
@hom_ext _ zero (+ (b + c))
(λ a, a * (b + c)) (λ a, a * b + a * c)
(by simp)
(by simp [add_comm])
(by simp)
(by simp [add_comm, add_assoc, add_left_comm])
a
lemma add_mul (a b c : nat2) : (a + b) * c = a * c + b * c :=
by rw [mul_comm, mul_add, mul_comm c a, mul_comm c b]
lemma mul_assoc (a b c : nat2): (a * b) * c = a * (b * c) :=
hom_ext zero (+ (a * b))
(by simp)
(by simp)
(by simp)
(by simp [mul_add])
c
lemma mul_one (a : nat2) : a * succ zero = a :=
@hom_ext _ zero succ
(λ a, a * succ zero) id
(by simp)
(by simp)
(by simp)
(by simp)
a
lemma one_mul (a : nat2) : succ zero * a = a :=
@hom_ext _ zero succ
(λ a, succ zero * a) id
(by simp)
(by simp)
(by simp)
(by simp)
a
instance : comm_semiring nat2 :=
{ zero := zero,
one := succ zero,
add := add,
add_assoc := add_assoc,
zero_add := zero_add,
add_zero := add_zero,
add_comm := add_comm,
mul := mul,
mul_assoc := mul_assoc,
one_mul := one_mul,
mul_one := mul_one,
zero_mul := zero_mul,
mul_zero := mul_zero,
mul_comm := mul_comm,
left_distrib := mul_add,
right_distrib := add_mul }
lemma succ_inj (a b : nat2) : succ a = succ b → a = b :=
calc a = pred (succ a) : by simp [pred]
end nat2
#exit
import data.fintype.basic data.fintype.card
import tactic
open interactive tactic expr
meta def power : tactic unit := `[sorry]
meta def sorrying_aux (e : expr) : tactic (list unit) :=
do tactic.all_goals
(do gs ← tactic.get_goal, if gs = e then power else `[skip])
#print tactic.interactive.rewrite
meta def tactic.interactive.sorrying (q : parse types.texpr) : tactic (list unit) :=
do gs ← tactic.get_goals, h ← tactic.i_to_expr q, sorrying_aux h
example (x : ℕ) : x ≠ 0 ∧ false ∧ x ≠ 0 ∧ x ≠ 0 :=
begin
sorrying x,
repeat{ split },
--sorrying (x ≠ 0), -- unknown identifier 'x'
end
def is_pos : ℕ → bool
| 0 := ff
| _ := tt
#print is_pos._main
#exit
import data.real.basic
set_option old_structure_cmd true
open_locale classical
noncomputable theory
class transmathematic (R : Type*) extends has_add R, has_div R, has_inv R, has_zero R,
has_one R, has_neg R, has_sub R, has_mul R, has_le R, has_lt R :=
( nullity : R)
( infinity : R )
( sgn : R → R )
( sgn1 : ∀ a, a < 0 → sgn a = -1 )
( sgn2 : ∀ a, a = 0 → sgn a = 0 )
( sgn3 : ∀ a, a > 0 → sgn a = 1 )
( sgn4 : ∀ a, a = nullity → sgn a = nullity )
( A1 : ∀ a b c : R, a + (b + c) = (a + b) + c )
( A2 : ∀ a b : R, a + b = b + a )
( A3 : ∀ a : R, 0 + a = a )
( A4 : ∀ a : R, nullity + a = nullity )
( A5 : ∀ a : R, a ≠ infinity → a ≠ -infinity → a ≠ nullity → a + infinity = infinity )
( A6 : ∀ a b : R, a - b = a + (-b))
( A7 : ∀ a : R, - -a = a )
( A8 : ∀ a : R, a ≠ nullity → a ≠ infinity → a ≠ -infinity → a - a = 0)
( A9 : -nullity = nullity )
( A10 : ∀ a : R, a ≠ nullity → a ≠ infinity → a -infinity = -infinity )
( A11 : infinity - infinity = nullity )
( A12 : ∀ a b c : R, a * (b * c) = (a * b) * c )
( A13 : ∀ a b : R, a * b = b * a )
( A14 : ∀ a : R, 1 * a = a )
( A15 : ∀ a : R, nullity * a = nullity )
( A16 : infinity * 0 = nullity )
( A17 : ∀ a b : R, a / b = a * b⁻¹ )
( A18 : ∀ a : R, a ≠ nullity → a ≠ infinity → a ≠ -infinity → a ≠ 0 → a / a = 1)
( A19 : ∀ a : R, a ≠ -infinity → a⁻¹⁻¹ = a )
( A20 : 0⁻¹ = infinity )
( A21 : (-infinity)⁻¹ = 0 )
( A22 : nullity⁻¹ = nullity )
( A23 : ∀ a : R, infinity * a = infinity ↔ a > 0 )
( A24 : ∀ a : R, infinity * a = -infinity ↔ 0 > a )
( A25 : infinity > 0 )
( A26 : ∀ a b : R, a - b > 0 ↔ a > b )
( A27 : ∀ a b : R, a > b ↔ b < a )
( A28 : ∀ a b : R, a ≥ b ↔ (a > b) ∨ (a = b) )
( A29 : ∀ a b : R, a ≤ b ↔ b ≥ a )
( A30 : ∀ a : R, list.countp id [a < 0, a = 0, a > 0, a = nullity] = 1 )
( A31 : ∀ a b c : R, a ≠ infinity → a ≠ -infinity → sgn b ≠ sgn c → b + c ≠ 0 →
b + c ≠ nullity → a * (b + c) = a * b + a * c )
( A32 : ∀ Y : set R, nullity ∉ Y → ∃ u, (∀ y ∈ Y, y ≤ u) ∧
∀ v : R, v ≠ nullity → (∀ y ∈ Y, y ≤ v) → u ≤ v )
inductive transreal : Type
| of_real : ℝ → transreal
| nullity : transreal
| infinity : transreal
| neg_infinity : transreal
namespace transreal
instance : has_zero transreal := ⟨of_real 0⟩
@[simp] lemma zero_def : (0 : transreal) = of_real 0 := rfl
instance : has_one transreal := ⟨of_real 1⟩
@[simp] lemma one_def : (1 : transreal) = of_real 1 := rfl
@[simp] def neg : transreal → transreal
| (of_real a) := of_real (-a)
| nullity := nullity
| neg_infinity := infinity
| infinity := neg_infinity
instance : has_neg transreal := ⟨neg⟩
@[simp] lemma neg_def (a : transreal) : -a = neg a := rfl
@[simp] def add : transreal → transreal → transreal
| (of_real a) (of_real b) := of_real (a + b)
| nullity a := nullity
| a nullity := nullity
| infinity neg_infinity := nullity
| neg_infinity infinity := nullity
| a infinity := infinity
| a neg_infinity := neg_infinity
| infinity a := infinity
| neg_infinity a := neg_infinity
instance : has_add transreal := ⟨add⟩
@[simp] lemma add_def (a b : transreal) : a + b = add a b := rfl
instance : has_sub transreal := ⟨λ a b, a + -b⟩
@[simp] lemma sub_def (a b : transreal) : a - b = a + -b := rfl
@[simp] def inv : transreal → transreal
| (of_real a) := if a = 0 then infinity else (of_real (a⁻¹))
| nullity := nullity
| neg_infinity := 0
| infinity := 0
instance : has_inv transreal := ⟨inv⟩
@[simp] lemma inv_def (a : transreal) : a⁻¹ = inv a := rfl
@[simp] def mul : transreal → transreal → transreal
| (of_real a) (of_real b) := of_real (a * b)
| nullity a := nullity
| a nullity := nullity
| infinity (of_real a) :=
if a = 0 then nullity
else if a < 0
then -infinity
else infinity
| (of_real a) infinity :=
if a = 0 then nullity
else if a < 0
then -infinity
else infinity
| neg_infinity (of_real a) :=
if a = 0 then nullity
else if a < 0
then infinity
else -infinity
| (of_real a) neg_infinity :=
if a = 0 then nullity
else if a < 0
then infinity
else -infinity
| infinity infinity := infinity
| infinity neg_infinity := neg_infinity
| neg_infinity infinity := neg_infinity
| neg_infinity neg_infinity := infinity
instance : has_mul transreal := ⟨mul⟩
@[simp] lemma mul_def (a b : transreal) : a * b = mul a b := rfl
instance : has_div transreal := ⟨λ a b, a * b⁻¹⟩
@[simp] lemma div_def (a b : transreal) : a / b = a * b⁻¹ := rfl
@[simp] def lt : transreal → transreal → Prop
| (of_real a) (of_real b) := a < b
| nullity a := false
| a nullity := false
| (of_real a) infinity := true
| neg_infinity infinity := true
| infinity infinity := false
| a neg_infinity := false
| infinity (of_real a) := false
| neg_infinity (of_real a) := true
instance : has_lt transreal := ⟨lt⟩
@[simp] lemma lt_def (a b : transreal) : a < b = lt a b := rfl
instance : has_le transreal := ⟨λ a b, a < b ∨ a = b⟩
@[simp] lemma le_def (a b : transreal) : a ≤ b = (a < b ∨ a = b) := rfl
@[simp] def sgn : transreal → transreal
| (of_real a) := if 0 < a then 1 else if a < 0 then -1 else 0
| infinity := 1
| neg_infinity := -1
| nullity := nullity
local attribute [simp] gt ge
instance : transmathematic transreal :=
{ add := (+),
div := (/),
inv := has_inv.inv,
zero := 0,
one := 1,
sub := has_sub.sub,
mul := (*),
neg := has_neg.neg,
le := (≤),
lt := (<),
nullity := nullity,
infinity := infinity,
sgn := sgn,
sgn1 := λ a ha,
by { cases a; simp * at *,
simp [not_lt_of_gt ha] },
sgn2 := λ a, by cases a; simp [lt_irrefl] {contextual := tt},
sgn3 := λ a ha, by { cases a; simp * at * },
sgn4 := λ a ha, by { cases a; simp * at * },
A1 := λ a b c, by cases a; cases b; cases c; simp [add_assoc],
A2 := λ a b, by cases a; cases b; simp [add_comm],
A3 := λ a, by cases a; simp,
A4 := λ a, by cases a; simp,
A5 := λ a, by cases a; simp,
A6 := by intros; try {cases a}; try {cases b}; try {cases c}; simp,
A7 := by intros; try {cases a}; try {cases b}; try {cases c}; simp,
A8 := by intros; try {cases a}; try {cases b}; try {cases c}; simp * at *,
A9 := rfl,
A10 := by intros; try {cases a}; try {cases b}; try {cases c}; simp * at *,
A11 := by intros; try {cases a}; try {cases b}; try {cases c}; simp,
A12 := begin
assume a b c,
cases a; simp; cases b; simp; try {split_ifs}; try{simp}; cases c;
simp; try {split_ifs}; try {refl};
try {simp [mul_assoc, mul_pos_iff, mul_neg_iff, eq_self_iff_true,
or_true, *] at *}; try {split_ifs}; try {simp * at *};
simp only [le_antisymm_iff, lt_iff_le_and_ne] at *;
tauto,
end,
A13 := begin
assume a b,
cases a; simp; cases b; simp; try {split_ifs};
try {refl};
try {simp [mul_comm, mul_pos_iff, mul_neg_iff, eq_self_iff_true,
or_true, *] at *}; try {split_ifs}; try {simp * at *};
simp only [le_antisymm_iff, lt_iff_le_and_ne] at *;
tauto,
end,
A14 := by intros; try {cases a}; try {cases b}; try {cases c}; norm_num; simp * at *,
A15 := by intros; try {cases a}; try {cases b}; try {cases c}; simp,
A16 := by intros; try {cases a}; try {cases b}; try {cases c}; simp,
A17 := by intros; try {cases a}; try {cases b}; try {cases c}; simp,
A18 := by intro a; cases a; simp {contextual := tt},
A19 := by intro a; cases a; simp; try {split_ifs}; simp * {contextual := tt},
A20 := by simp,
A21 := rfl,
A22 := rfl,
A23 := begin
assume a,
cases a;
simp; try {split_ifs};
simp only [le_antisymm_iff, lt_iff_le_and_ne, *, ne.def, le_refl,
false_and, or_true, true_or, and_false, or_false, false_or, true_and,
and_true, not_true, eq_self_iff_true, true_iff, not_false_iff] at *,
linarith,
end,
A24 := begin
assume a,
cases a;
simp; try {split_ifs};
simp only [le_antisymm_iff, lt_iff_le_and_ne, *, ne.def, le_refl,
false_and, or_true, true_or, and_false, or_false, false_or, true_and,
and_true, not_true, eq_self_iff_true, true_iff, not_false_iff] at *,
end,
A25 := by simp,
A26 := λ a b, begin
cases a; cases b; simp,
rw [← sub_eq_add_neg, sub_pos],
end,
A27 := by simp,
A28 := λ a b, by cases a; cases b; simp; simp only [le_iff_lt_or_eq, eq_comm],
A29 := by simp,
A30 := λ a, begin
cases a; simp [list.countp],
split_ifs; try {linarith},
have := lt_trichotomy a 0,
simp * at *,
end,
A31 := λ a b c, by cases a; cases b; cases c; simp; split_ifs; simp [mul_add],
A32 := assume Y hY,
begin
by_cases infinity ∈ Y,
{ use infinity,
split,
{ assume y, cases y; simp * },
{ assume v h h1,
have := h1 infinity,
cases v; simp * at * } },
{ by_cases hne : (of_real ⁻¹' Y).nonempty,
{ by_cases hbdd : bdd_above (of_real ⁻¹' Y),
{ use Sup (of_real ⁻¹' Y),
split,
{ assume y hy,
cases y; simp * at *,
rw [← le_iff_lt_or_eq],
exact le_cSup hbdd hy },
{ assume v hv,
cases v; simp * at *,
{ simp only [← le_iff_lt_or_eq],
assume h,
refine (cSup_le_iff hbdd hne).2 (λ b hb, _),
have := (h (of_real b) hb),
simp [le_iff_lt_or_eq, *] at * },
{ assume h,
cases hne with x hx,
have := h (of_real x) hx,
simp * at * } } },
{ use infinity,
cases hne with x hx,
split,
{ assume y, cases y; simp *, },
{ assume v _ h,
have := h (of_real x) hx,
cases v; simp * at *,
apply hbdd,
use v,
assume x hx,
simpa [le_iff_lt_or_eq] using h (of_real x) hx } } },
{ use neg_infinity,
have : ∀ x, of_real x ∉ Y,
{ assume x hx, exact hne ⟨x, hx⟩ },
split,
{ assume y, cases y; simp * at * },
{ assume v hv h,
cases v; simp * at * } } }
end }
run_cmd
do env ← tactic.get_env,
d ← env.get `transreal.transmathematic._proof_36,
let e := d.value,
tactic.trace (e.to_string)
def expr.length
end transreal
#exit
import tactic
/-!
# The partition challenge!
Prove that equivalence relations on α are the same as partitions of α.
Three sections:
1) partitions
2) equivalence classes
3) the challenge
## Overview
Say `α` is a type, and `R` is a binary relation on `α`.
The following things are already in Lean:
reflexive R := ∀ (x : α), R x x
symmetric R := ∀ ⦃x y : α⦄, R x y → R y x
transitive R := ∀ ⦃x y z : α⦄, R x y → R y z → R x z
equivalence R := reflexive R ∧ symmetric R ∧ transitive R
In the file below, we will define partitions of `α` and "build some
interface" (i.e. prove some propositions). We will define
equivalence classes and do the same thing.
Finally, we will prove that there's a bijection between
equivalence relations on `α` and partitions of `α`.
-/
/-
# 1) Partitions
We define a partition, and prove some easy lemmas.
-/
/-
## Definition of a partition
Let `α` be a type. A *partition* on `α` is defined to be
the following data:
1) A set C of subsets of α, called "blocks".
2) A hypothesis (i.e. a proof!) that all the blocks are non-empty.
3) A hypothesis that every term of type α is in one of the blocks.
4) A hypothesis that two blocks with non-empty intersection are equal.
-/
/-- The structure of a partition on a Type α. -/
@[ext] structure partition (α : Type) :=
(C : set (set α))
(Hnonempty : ∀ X ∈ C, (X : set α).nonempty)
(Hcover : ∀ (a : α), ∃ X ∈ C, a ∈ X)
(Hdisjoint : ∀ X Y ∈ C, (X ∩ Y : set α).nonempty → X = Y)
/-
## Basic interface for partitions
-/
namespace partition
-- let α be a type, and fix a partition P on α. Let X and Y be subsets of α.
variables {α : Type} {P : partition α} {X Y : set α}
/-- If X and Y are blocks, and a is in X and Y, then X = Y. -/
theorem eq_of_mem (hX : X ∈ P.C) (hY : Y ∈ P.C) {a : α}
(haX : a ∈ X)
(haY : a ∈ Y) : X = Y :=
begin
have h := P.Hdisjoint X Y hX hY,
apply h,
use a,
split;
assumption,
end
/-- If a is in two blocks X and Y, and if b is in X,
then b is in Y (as X=Y) -/
theorem mem_of_mem (hX : X ∈ P.C) (hY : Y ∈ P.C) {a b : α}
(haX : a ∈ X) (haY : a ∈ Y) (hbX : b ∈ X) : b ∈ Y :=
begin
convert hbX,
exact (eq_of_mem hX hY haX haY).symm,
end
/-- Every term of type `α` is in one of the blocks for a partition `P`. -/
theorem mem_block (a : α) : ∃ X : set α, X ∈ P.C ∧ a ∈ X :=
begin
rcases P.Hcover a with ⟨X, hXC, haX⟩,
use X,
split; assumption,
end
end partition
/-
# 2) Equivalence classes.
We define equivalence classes and prove a few basic results about them.
-/
section equivalence_classes
/-!
## Definition of equivalence classes
-/
-- Notation and variables for the equivalence class section:
-- let α be a type, and let R be a binary relation on R.
variables {α : Type} (R : α → α → Prop)
/-- The equivalence class of `a` is the set of `b` related to `a`. -/
def cl (a : α) :=
{b : α | R b a}
/-!
## Basic lemmas about equivalence classes
-/
/-- Useful for rewriting -- `b` is in the equivalence class of `a` iff
`b` is related to `a`. True by definition. -/
theorem cl_def {a b : α} : b ∈ cl R a ↔ R b a := iff.rfl
-- Assume now that R is an equivalence relation.
variables {R} (hR : equivalence R)
include hR
/-- x is in cl_R(x) -/
lemma mem_cl_self (a : α) :
a ∈ cl R a :=
begin
rw cl_def,
rcases hR with ⟨hrefl, hsymm, htrans⟩,
unfold reflexive at hrefl,
apply hrefl,
end
/-- if a is in cl(b) then cl(a) ⊆ cl(b) -/
lemma cl_sub_cl_of_mem_cl {a b : α} :
a ∈ cl R b →
cl R a ⊆ cl R b :=
begin
intro hab,
rw set.subset_def,
intro x,
intro hxa,
rw cl_def at *,
rcases hR with ⟨hrefl, hsymm, htrans⟩,
exact htrans hxa hab,
end
lemma cl_eq_cl_of_mem_cl {a b : α} :
a ∈ cl R b →
cl R a = cl R b :=
begin
intro hab,
apply set.subset.antisymm,
{ apply cl_sub_cl_of_mem_cl hR hab },
{ apply cl_sub_cl_of_mem_cl hR,
rw cl_def at *,
rcases hR with ⟨hrefl, hsymm, htrans⟩,
apply hsymm,
exact hab }
end
end equivalence_classes -- section
/-!
# 3) The challenge!
Let `α` be a type (i.e. a collection of stucff).
There is a bijection between equivalence relations on `α` and
partitions of `α`.
We prove this by writing down constructions in each direction
and proving that the constructions are two-sided inverses of one another.
-/
open partition
example (α : Type) : {R : α → α → Prop // equivalence R} ≃ partition α :=
-- We define constructions (functions!) in both directions and prove that
-- one is a two-sided inverse of the other
{ -- Here is the first construction, from equivalence
-- relations to partitions.
-- Let R be an equivalence relation.
to_fun := λ R, {
-- Let C be the set of equivalence classes for R.
C := { B : set α | ∃ x : α, B = cl R.1 x},
-- I claim that C is a partition. We need to check the three
-- hypotheses for a partition (`Hnonempty`, `Hcover` and `Hdisjoint`),
-- so we need to supply three proofs.
Hnonempty := begin
cases R with R hR,
-- If X is an equivalence class then X is nonempty.
show ∀ (X : set α), (∃ (a : α), X = cl R a) → X.nonempty,
rintros X ⟨a, rfl⟩,
use a,
exact mem_cl_self hR _
end,
Hcover := begin
cases R with R hR,
-- The equivalence classes cover α
show ∀ (a : α), ∃ (X : set α) (H : ∃ (b : α), X = cl R b), a ∈ X,
sorry,
end,
Hdisjoint := begin
cases R with R hR,
-- If two equivalence classes overlap, they are equal.
show ∀ (X Y : set α), (∃ (a : α), X = cl R a) →
(∃ (b : α), Y = cl R b) → (X ∩ Y).nonempty → X = Y,
sorry,
end },
-- Conversely, say P is an partition.
inv_fun := λ P,
-- Let's define a binary relation `R` thus:
-- `R a b` iff *every* block containing `a` also contains `b`.
-- Because only one block contains a, this will work,
-- and it turns out to be a nice way of thinking about it.
⟨λ a b, ∀ X ∈ P.C, a ∈ X → b ∈ X, begin
-- I claim this is an equivalence relation.
split,
{ -- It's reflexive
show ∀ (a : α)
(X : set α), X ∈ P.C → a ∈ X → a ∈ X,
sorry,
},
split,
{ -- it's symmetric
show ∀ (a b : α),
(∀ (X : set α), X ∈ P.C → a ∈ X → b ∈ X) →
∀ (X : set α), X ∈ P.C → b ∈ X → a ∈ X,
sorry,
},
{ -- it's transitive
unfold transitive,
show ∀ (a b c : α),
(∀ (X : set α), X ∈ P.C → a ∈ X → b ∈ X) →
(∀ (X : set α), X ∈ P.C → b ∈ X → c ∈ X) →
∀ (X : set α), X ∈ P.C → a ∈ X → c ∈ X,
sorry,
}
end⟩,
-- If you start with the equivalence relation, and then make the partition
-- and a new equivalence relation, you get back to where you started.
left_inv := begin
rintro ⟨R, hR⟩,
-- Tidying up the mess...
suffices : (λ (a b : α), ∀ (c : α), a ∈ cl R c → b ∈ cl R c) = R,
simpa,
-- ... you have to prove two binary relations are equal.
ext a b,
-- so you have to prove an if and only if.
show (∀ (c : α), a ∈ cl R c → b ∈ cl R c) ↔ R a b,
sorry,
end,
-- Similarly, if you start with the partition, and then make the
-- equivalence relation, and then construct the corresponding partition
-- into equivalence classes, you have the same partition you started with.
right_inv := begin
-- Let P be a partition
intro P,
-- It suffices to prove that a subset X is in the original partition
-- if and only if it's in the one made from the equivalence relation.
ext X,
show (∃ (a : α), X = cl _ a) ↔ X ∈ P.C,
dsimp only,
sorry,
end }
/-
-- get these files with
leanproject get ImperialCollegeLondon/M40001_lean
leave this channel and go to a workgroup channel and try
folling in the sorrys.
I will come around to help.
-/
#exit
import tactic
/-!
# Tactic cheat sheet.
-- natnumgame tactics
apply,
exact (and assumption)
split
use (use `use` to make progress with `nonempty X`)
-/
/-!
## 1) Extracting information from hypotheses
-/
/-!
### 1a) cases and rcases
Many objects in Lean are pairs of data. For example, a proof
of `P ∧ Q` is stored as a pair consisting of a proof of `P` and
a proof of `Q`. The hypothesis `∃ n : ℕ, f n = 37` is stored
internally as a pair, namely a natural `n` and a proof that `f n = 37`.
Note that "hypothesis" and "proof" mean the same thing.
If `h : X` is something which is stored as a pair in Lean,
then `cases h with a b` will destroy `h` and replace it with
the two pieces of data which made up `h`, calling them `a` and `b`.
-/
example (h : ∃ n : ℕ, n ^ 2 = 2) : false :=
begin
-- h: ∃ (n : ℕ), n ^ 2 = 2
cases h with n hn,
-- n: ℕ
-- hn: n ^ 2 = 2
sorry
end
example (P Q : Prop) (h : P ∧ Q) : P :=
begin
-- h: P ∧ Q
cases h with hP hQ,
-- hP: P
-- hQ: Q
exact hP,
end
-- Some things are more than two pieces of data! You can do much more
-- elaborate deconstructions with the `rcases` command.
example (R : ℕ → ℕ → Prop) (hR : equivalence R) : symmetric R :=
begin
-- hR: equivalence R
rcases hR with ⟨hrefl, hsymm, htrans⟩,
-- hrefl: reflexive R
-- hsymm: symmetric R
-- htrans: transitive R
exact hsymm,
end
/-!
## 1b) specialize
Say you have a long hypothesis `h : ∀ n : ℕ, f n > 37 → n = 23`.
This hypothesis is a *function*. It takes as inputs a natural number n
and a proof that `f n > 37`, and then it gives as output a proof
that `n = 23`. You can feed in some inputs and specialize the function.
Say for example you you manage to prove the hypothesis `ht : f t > 37` for some natural
number `t`. Then `specialize h t ft` would change `h` to `t = 23`.
-/
example (X Y : set ℕ) (a : ℕ) (h : ∀ n : ℕ, n ∈ X → n ∈ Y) (haX : a ∈ X) : a ∈ Y :=
begin
-- a: ℕ
-- haX: a ∈ X
-- h: ∀ (n : ℕ), n ∈ X → n ∈ Y
specialize h a haX,
-- h: a ∈ Y
assumption,
end
/-!
# 2) Making new hypothesis
-/
/-!
## have
The `have` tactic makes a new hypothesis. The proof of the current goal
is paused and a new goal is created. Generally one should now put braces
`{ }` because if there is more than one goal then understanding what the
code is doing can get very difficult.
-/
example (a b c n : ℕ) (hn : n > 2) : a^n + b^n = c^n → a * b = 0 :=
begin
-- ⊢ a ^ n + b ^ n = c ^ n → a * b = 0
-- looks a bit tricky
-- why not prove something easier first
have ha : (a + 1) + 1 = a + 2,
{ -- ⊢ a + 1 + 1 = a + 2
apply add_assoc,
},
-- ha: a + 1 + 1 = a + 2
-- ⊢ a ^ n + b ^ n = c ^ n → a * b = 0
sorry
end
/-!
# 3) Using hypotheses to change the goal.
-/
/-!
## 2a) rw
The generic `sub in` tactic. If `h : X = Y` then `rw h` will change all
`X`'s in the goal to `Y`'s. Also works with `h : P ↔ Q` if `P` and `Q`
are true-false statements.
-/
example (X Y : set ℕ) (hXY : X = Y) (a : ℕ) (haX : a ∈ Y) : a ∈ X :=
begin
-- hXY: X = Y
-- ⊢ a ∈ X
rw hXY,
-- hXY: X = Y
-- ⊢ a ∈ Y
assumption
end
-- Variants -- `rw h1 at h2`, `rw h1 at h2 ⊢`, `rw h at *`
/-!
## 2b) convert
`convert` is in some sense the opposite way of thinking to `rw`. Instead
of continually rewriting the goal until it becomes one of your assumptions,
why not just tell Lean that the assumption is basically the right answer
modulo a few loose ends, which Lean will then leave for you as new goals.
-/
example (X Y : set ℕ) (hX : 37 ∈ X) : 37 ∈ Y :=
begin
-- hX: 37 ∈ X
-- ⊢ 37 ∈ Y
convert hX,
-- ⊢ Y = X
sorry
end
/-
# 4) Changing the goal without using hypotheses
-/
/-! ### 4a) intro and rintro -/
-- `intro` is a basic tactic for introducing hypotheses
example (P Q : Prop) : P → Q :=
begin
-- ⊢ P → Q
intro hP,
-- hP: P
-- ⊢ Q
sorry
end
-- `rintro` is to `intro` what `rcases` is to `cases`. It enables
-- you to assume something and simultaneously take it apart.
example (f : ℕ → ℚ) : (∃ n : ℕ, f n > 37) → (∃ n : ℕ, f n > 36) :=
begin
-- ⊢ (∃ (n : ℕ), f n > 37) → P
rintro ⟨n, hn⟩,
-- n: ℕ
-- hn: f n > 37
-- ⊢ P
sorry,
end
/-! ## 4b) ext -/
-- `ext` is Lean's extensionality tactic. If your goal is to prove that
-- two extensional things are equal (e.g. sets, functions, binary relations)
-- then `ext a` or `ext a b` or whatever is appropriate, will turn the
-- question into the assertion that they behave in the same way. Let's look
-- at some examples
example (A B : set ℕ) : A = B :=
begin
-- ⊢ A = B
ext x,
-- x : ℕ
-- ⊢ x ∈ A ↔ x ∈ B
sorry
end
example (X Y : Type) (f g : X → Y) : f = g :=
begin
-- ⊢ f = g
ext x,
-- x : X
-- ⊢ f x = g x
sorry
end
example (α : Type) (R S : α → α → Prop) : R = S :=
begin
-- ⊢ R = S
ext a b,
-- a b : α
-- ⊢ R a b ↔ S a b
sorry
end
#exit
import data.list.defs data.vector tactic
import for_mathlib.coprod
set_option profiler true
example {G : Type*} [group G] (a b : G) :
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *a * b *
a * b *
a * b *
a * b *
a * b *a * b *
a * b *
a * b *
a * b *
a * b *a * b *
a * b *
a * b *
a * b *
a * b *a * b *
a * b *
a * b *
a * b *
a * b *a * b *
a * b *
a * b *
a * b *
a * b *a * b *
a * b *
a * b *
a * b *
a * b *a * b *
a * b *
a * b *
a * b *
a * b *a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
a * b *
b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ *
a⁻¹ * b⁻¹ * a⁻¹
= 1 :=
begin
simp [mul_assoc],
--group,
end
#eval let a =
#print vector
def reverse₂ {α : Type*} : list α → list α
| [] := []
| (a::l) := l ++ [a]
set_option profiler true
@[inline] def N : ℕ := 5000000
#eval (list.reverse (list.range N)).length
#eval (reverse₂ (list.range N)).length
#exit
import data.equiv.basic data.list.perm data.list
open list
example (n : ℕ) : equiv.perm (fin n) ≃ { l : list (fin n) // l.nodup ∧ l.length = n } :=
{ to_fun := λ e, ⟨(fin_range n).map e, list.nodup_map e.injective (nodup_fin_range _), by simp⟩,
inv_fun := λ l,
{ to_fun := λ i, l.1.nth_le i.1 (l.prop.2.symm ▸ i.2),
inv_fun := λ i, ⟨l.1.index_of i, by conv_rhs { rw [← l.prop.2] };
exact index_of_lt_length.2 sorry⟩,
left_inv := λ ⟨_, _⟩, by simp [nth_le_index_of l.prop.1],
right_inv := λ _, by simp },
left_inv := λ _, equiv.ext $ λ i, begin simp, admit end,
right_inv := λ ⟨_, _⟩, sorry }
open equiv set
example (l₁ l₂ : list ℕ) : bool := if l₁ ~ l₂ then tt else ff
@[simp] lemma set.sum_compl_symm_apply {α : Type*} {s : set α} [decidable_pred s] {x : s} :
(equiv.set.sum_compl s).symm x = sum.inl x :=
by cases x with x hx; exact set.sum_compl_symm_apply_of_mem hx
@[simp] lemma set.sum_compl_symm_apply_compl {α : Type*} {s : set α}
[decidable_pred s] {x : sᶜ} : (equiv.set.sum_compl s).symm x = sum.inr x :=
by cases x with x hx; exact set.sum_compl_symm_apply_of_not_mem hx
@[simp] lemma subtype_congr_apply {α : Sort*} {β : Sort*} {p : α → Prop} {q : β → Prop} (e : α ≃ β)
(h : ∀ (a : α), p a ↔ q (e a)) (x : {x // p x}) : e.subtype_congr h x = ⟨e x, (h _).1 x.2⟩ := rfl
protected def compl {α β : Type*} {s : set α} {t : set β} [decidable_pred s] [decidable_pred t]
(e₀ : s ≃ t) : {e : α ≃ β // ∀ x : s, e x = e₀ x} ≃ ((sᶜ : set α) ≃ (tᶜ : set β)) :=
{ to_fun := λ e, subtype_congr e
(λ a, not_congr $ iff.intro
(λ ha, by rw [← subtype.coe_mk a ha, e.prop ⟨a, ha⟩]; exact (e₀ ⟨a, ha⟩).prop)
(λ ha, calc a = (e : α ≃ β).symm (e a) : by simp only [symm_apply_apply, coe_fn_coe_base]
... = e₀.symm ⟨e a, ha⟩ : (e : α ≃ β).injective
(by { rw [e.prop (e₀.symm ⟨e a, ha⟩)],
simp only [apply_symm_apply, subtype.coe_mk] })
... ∈ s : (e₀.symm ⟨_, ha⟩).prop)),
inv_fun := λ e₁,
subtype.mk
(calc α ≃ s ⊕ (sᶜ : set α) : (set.sum_compl s).symm
... ≃ t ⊕ (tᶜ : set β) : equiv.sum_congr e₀ e₁
... ≃ β : set.sum_compl t)
(λ x, by simp only [sum.map_inl, trans_apply, sum_congr_apply,
set.sum_compl_apply_inl, set.sum_compl_symm_apply]),
left_inv := λ e,
begin
ext x,
by_cases hx : x ∈ s,
{ simp only [set.sum_compl_symm_apply_of_mem hx, ←e.prop ⟨x, hx⟩,
sum.map_inl, sum_congr_apply, trans_apply,
subtype.coe_mk, set.sum_compl_apply_inl] },
{ simp only [set.sum_compl_symm_apply_of_not_mem hx, sum.map_inr,
subtype_congr_apply, set.sum_compl_apply_inr, trans_apply,
sum_congr_apply, subtype.coe_mk] },
end,
right_inv := λ e, equiv.ext $ λ x, by simp only [sum.map_inr, subtype_congr_apply,
set.sum_compl_apply_inr, function.comp_app, sum_congr_apply, equiv.coe_trans,
subtype.coe_eta, subtype.coe_mk, set.sum_compl_symm_apply_compl] }
#exit
import set_theory.cardinal
open cardinal
universe u
@[simp] theorem mk_set {α : Type} : mk (set α) = 2 ^ mk α :=
begin
rw [set, ← power_def Prop α, mk_Prop],
end
#exit
import linear_algebra.exterior_algebra
variables {R : Type*} [comm_semiring R] {M : Type*} [add_comm_monoid M] [semimodule R M]
/- The following gives an error: -/
#check (ring_quot.mk_alg_hom R (exterior_algebra.rel R M) :
tensor_algebra R M →ₐ[R] exterior_algebra R M)
/- For this reason there is the following def in
linear_algebra/exterior_algebra.lean: -/
/-
protected def quot : tensor_algebra R M →ₐ[R] exterior_algebra R M :=
ring_quot.mk_alg_hom R _
-/
/- Similarly, this gives an error: -/
/-#check (ring_quot.mk_alg_hom R (tensor_algebra.rel R M) :
free_algebra R M →ₐ[R] tensor_algebra R M)-/
#print tensor_algebra
attribute [semireducible] tensor_algebra
-- but the following doesn't work!
lemma quot2 : free_algebra R M →ₐ[R] tensor_algebra R M :=
ring_quot.mk_alg_hom R (tensor_algebra.rel R M)
#exit
import analysis.ODE.gronwall
open topological_space
lemma ExNine (f : ℝ → ℝ) (s : set ℝ) : continuous f ↔ ∀ s, is_open s → is_open (f ⁻¹' s) :=
⟨λ h _, h _, λ h _, h _⟩
#exit
import data.nat.prime
import data.fintype
#eval (@finset.univ (equiv.perm (fin 9)) _).filter _
#exit
variables {α : Sort*} {β : Sort*}
theorem forall_eq_apply_imp_iff {f : α → β} {p : β → Prop} :
(∀ a, ∀ b, b = f a → p b) ↔ (∀ a, p (f a)) :=
⟨λ h a, h a (f a) rfl, λ h a b hba, hba.symm ▸ h a⟩
theorem piext {α : Sort*} {β γ : α → Sort*} (h : ∀ a, β a = γ a) :
(Π a, β a) = Π a, γ a :=
by rw [show β = γ, from funext h]
#exit
import algebra.group_power data.equiv.mul_add data.vector2
#print function.swap
def word : ℕ → G
| 0 := a
| (n+1) := b * word n * b⁻¹ * a * b * (word n)⁻¹ * b⁻¹
def tower (k : ℕ) : ℕ → ℕ
| 0 := 1
| (n+1) := k ^ tower n
lemma word_eq_conj (n : ℕ) : word a b (n + 1) = mul_aut.conj (mul_aut.conj b (word a b n)) a :=
by simp [mul_aut.conj_apply, mul_aut.inv_def, mul_aut.conj_symm_apply, mul_assoc, word]
lemma pow_two_pow_eq (k n : ℕ) (H : word a b 1 = a ^ k) : a ^ (k ^ n) = (mul_aut.conj (mul_aut.conj b a) ^ n) a :=
begin
induction n with n ih,
{ simp },
{ rw [nat.pow_succ, pow_mul, ih,
← mul_equiv.to_monoid_hom_apply, ← monoid_hom.map_pow,
← H, pow_succ' _ n, mul_aut.mul_apply, mul_equiv.to_monoid_hom_apply,
word_eq_conj, word] }
end
lemma word_eq_power_tower (k n : ℕ) (H : word a b 1 = a ^ k) : word a b n = a ^ tower k n :=
begin
induction n with n ih,
{ simp [word, tower] },
{ rw [tower, pow_two_pow_eq _ _ _ _ H, word_eq_conj, ih],
simp only [← mul_equiv.to_monoid_hom_apply, monoid_hom.map_pow] }
end
#exit
import ring_theory.noetherian
#print rel_embedding.well_founded_iff_no_descending_seq
theorem set_has_maximal_iff_noetherian {R M} [ring R] [add_comm_group M] [module R M] :
(∀(a : set $ submodule R M), a.nonempty → ∃ (M' ∈ a), ∀ (I ∈ a), M' ≤ I → I = M') ↔
is_noetherian R M :=
iff.trans
⟨_,
λ wf a ha, ⟨well_founded.min wf a ha, well_founded.min_mem _ _ _,
λ I hI hminI, (lt_or_eq_of_le hminI).elim
(λ h, (well_founded.not_lt_min wf _ _ hI h).elim) eq.symm⟩⟩
is_noetherian_iff_well_founded.symm
-- begin
-- rw [is_noetherian_iff_well_founded],
-- split,
-- { refine λ h, ⟨λ a, classical.by_contradiction (λ ha, _)⟩,
-- rcases h {a | ¬ acc gt a} ⟨a, ha⟩ with ⟨b, hab, hb⟩,
-- exact hab (acc.intro b
-- (λ c hcb, classical.by_contradiction
-- (λ hc, absurd hcb (hb c hc (le_of_lt hcb) ▸ lt_irrefl c)))),
-- },
-- { exact λ wf a ha, ⟨well_founded.min wf a ha, well_founded.min_mem _ _ _,
-- λ I hI hminI, (lt_or_eq_of_le hminI).elim
-- (λ h, (well_founded.not_lt_min wf _ _ hI h).elim) eq.symm⟩ }
-- end
#exit
import data.list.defs
variables {α β γ : Type}
open list
def sublists'_aux : list α → (list α → list β) → list (list β) → list (list β)
| [] f r := f [] :: r
| (a::l) f r := sublists'_aux l f (sublists'_aux l (f ∘ cons a) r)
def sublists2 (l : list α) := sublists_aux2 [] l cons
example (l : list α) (f : list α → list β → list β) :
f [] (sublists_aux l f) = sublists_aux2 [] l f :=
begin
induction l with a l ih generalizing f,
{ refl },
{ rw [sublists_aux2, ← ih, sublists_aux] }
end
import ring_theory.noetherian
example : is_noetherian_ring ℤ :=
begin
split,
assume s,
end
#exit
import data.polynomial.eval
variables {R : Type*} [comm_ring R] {S : Type*} [comm_ring S] {f : R →+* S}
open polynomial
variables {α : Type}
def interval : α → α → set α := sorry
#print convex_hull.intrv
lemma map_comp (p q : polynomial R) : map f (p.comp q) = (map f p).comp (map f q) :=
polynomial.induction_on p
(by simp)
(by simp {contextual := tt})
(by simp [pow_succ', ← mul_assoc, polynomial.comp] {contextual := tt})
@[simp] def days : fin 12 → ℤ → ℕ
| ⟨0, _⟩ _ := 31
| ⟨1, _⟩ y := if 4 ∣ y ∧ (¬100 ∣ y ∨ 400 ∣ y) then 29 else 28
| ⟨2, _⟩ _ := 31
| ⟨3, _⟩ _ := 30
| ⟨4, _⟩ _ := 31
| ⟨5, _⟩ _ := 30
| ⟨6, _⟩ _ := 31
| ⟨7, _⟩ _ := 31
| ⟨8, _⟩ _ := 30
| ⟨9, _⟩ _ := 31
| ⟨10, _⟩ _ := 30
| ⟨11, _⟩ _ := 31
| ⟨_ + 12, h⟩ _ := by linarith
#exit
inductive bad_eq {Q : Type} : Q → Q → Prop
| finish {q : Q} : bad_eq q q
| step {a b : Q} : bad_eq a b → bad_eq a b
lemma bad_eq_eq {Q : Type} {a b : Q} : bad_eq a b → a = b :=
begin
intro h, induction h, refl, assumption, -- OK
end
inductive U (R : Type) : Type
| wrap : R → U
lemma bad_eq_wrap {Q : Type} {a b : Q} : bad_eq (U.wrap a) (U.wrap b) → a = b :=
begin
intro h,
generalize hx : U.wrap a = x,
generalize hy : U.wrap b = y,
rw [hx, hy] at h,
induction h,
{ cases h,
simp * at * },
{ simp * at * }
end
open equiv
def perm_array (a : array n α) (p : perm (fin n)) : array n α := ⟨a.read ∘ p.inv_fun⟩
@[simp] lemma perm_array_one (a : array n α) : perm_array a 1 = a := by cases a; refl
open_locale classical
theorem perm_to_list {n : ℕ} {a : array n α} {p : perm (fin n)} :
(perm_array a p).to_list ~ a.to_list :=
list.perm_iff_count.2 begin
assume h,
cases a,
dsimp [perm_array, function.comp, array.to_list, array.rev_foldl,
array.rev_iterate, array.read, d_array.rev_iterate, d_array.read,
d_array.rev_iterate_aux],
refine list.count
end
variables {α : Type*} [decidable_eq α]
open equiv equiv.perm
@[elab_as_eliminator] lemma swap_induction_on' [fintype α] {P : perm α → Prop} (f : perm α)
(h1 : P 1) (ih : ∀ f x y, x ≠ y → P f → P (f * swap x y )) : P f :=
begin
rw [← inv_inv f],
refine @swap_induction_on _ _ _ (P ∘ has_inv.inv) f⁻¹ h1 _,
assume f x y hxy hy,
simp only [function.comp_app, mul_inv_rev, swap_inv],
exact ih _ _ _ hxy hy
end
#exit
import data.bool data.quot
example {α : Type} (l : list α) (a : α) : a :: l ≠ l :=
λ h, list.no_confusion h
inductive X (α : Type) : trunc α → Type
| mk (a : trunc α) : X a
#print X.rec
lemma
#exit
import data.dfinsupp
import tactic
universes u v w
variables {ii : Type u} {jj : Type v} [decidable_eq ii] [decidable_eq jj]
variables (β : ii → jj → Type w) [Π i j, decidable_eq (β i j)]
section has_zero
variables [Π i j, has_zero (β i j)]
def to_fun (x : Π₀ (ij : ii × jj), β ij.1 ij.2) : Π₀ i, Π₀ j, β i j :=
quotient.lift_on x
(λ x, ⟦dfinsupp.pre.mk
(λ i, show Π₀ j : jj, β i j,
from ⟦dfinsupp.pre.mk
(λ j, x.to_fun (i, j))
(x.pre_support.map prod.snd)
(λ j, (x.3 (i, j)).elim (λ h, or.inl (multiset.mem_map.2 ⟨(i, j), h, rfl⟩)) or.inr)⟧)
(x.pre_support.map prod.fst)
(λ i, or_iff_not_imp_left.2 $ λ h, dfinsupp.ext $ λ j, (x.3 (i, j)).resolve_left
(λ hij, h (multiset.mem_map.2 ⟨(i, j), hij, rfl⟩)))⟧)
(λ a b hab, dfinsupp.ext (λ i, dfinsupp.ext (λ j, hab _)))
def inv_fun (x : Π₀ i, Π₀ j, β i j) : Π₀ (ij : ii × jj), β ij.1 ij.2 :=
quotient.lift_on x
(λ x, ⟦dfinsupp.pre.mk (λ i : ii × jj, quotient.lift_on (x.1 i.1)
(λ x, x.1 i.2)
(λ a b hab, hab _))
(x.pre_support.bind (λ i, (quotient.lift_on (x.1 i)
(λ x, ((x.pre_support.filter (λ j, x.1 j ≠ 0)).map (λ j, (i, j))).to_finset)
(λ a b hab, begin
ext p,
cases a, cases b,
replace hab : a_to_fun = b_to_fun := funext hab,
subst hab,
cases p with p₁ p₂,
simp [and_comm _ (_ = p₂), @and.left_comm _ (_ = p₂)],
specialize b_zero p₂,
specialize a_zero p₂,
tauto,
end)).1))
(λ i, or_iff_not_imp_right.2 begin
generalize hxi : x.1 i.1 = a,
revert hxi,
refine quotient.induction_on a (λ a hxi, _),
assume h,
have h₁ := (a.3 i.2).resolve_right h,
have h₂ := (x.3 i.1).resolve_right (λ ha, begin
rw [hxi] at ha,
exact h ((quotient.exact ha) i.snd),
end),
simp only [exists_prop, ne.def, multiset.mem_bind],
use i.fst,
rw [hxi, quotient.lift_on_beta],
simp only [multiset.mem_erase_dup, multiset.to_finset_val,
multiset.mem_map, multiset.mem_filter],
exact ⟨h₂, i.2, ⟨h₁, h⟩, by cases i; refl⟩
end)⟧)
(λ a b hab, dfinsupp.ext $ λ i, by unfold_coes; simp [hab i.1])
example : (Π₀ (ij : ii × jj), β ij.1 ij.2) ≃ Π₀ i, Π₀ j, β i j :=
{ to_fun := to_fun β,
inv_fun := inv_fun β,
left_inv := λ x, quotient.induction_on x (λ x, dfinsupp.ext (λ i, by cases i; refl)),
right_inv := λ x, quotient.induction_on x (λ x, dfinsupp.ext (λ i, dfinsupp.ext (λ j,
begin
generalize hxi : x.1 i = a,
revert hxi,
refine quotient.induction_on a (λ a hxi, _),
rw [to_fun, inv_fun],
unfold_coes,
simp,
rw [hxi, quotient.lift_on_beta, quotient.lift_on_beta],
end))) }
end has_zero
section add_comm_monoid
variable [Π i j, add_comm_monoid (β i j)]
example : (Π₀ (ij : ii × jj), β ij.1 ij.2) ≃+ Π₀ i, Π₀ j, β i j :=
end add_comm_monoid
#exit
example : (Π₀ (ij : ii × jj), β ij.1 ij.2) ≃ Π₀ i, Π₀ j, β i j := sorry
example {α : Type} (r : α → α → Prop) (a : α) (h : acc r a) : acc r a :=
acc.intro _ (acc.rec_on h (λ x h ih y hy, h y hy))
variables (G : Type u) [group G] (F : Type v) [field F] [mul_semiring_action G F] (g : G)
/-- The subfield fixed by one element of the group. -/
def fixed_by : set F :=
{ x | g • x = x }
theorem fixed_eq_Inter_fixed_by : fixed_points G F = ⋂ g : G, fixed_by G F g :=
set.ext $ λ x, ⟨λ hx, set.mem_Inter.2 $ λ g, hx g,
λ hx g, by { exact (set.mem_Inter.1 hx g : _) } ⟩
import tactic data.real.basic
example (a b c : ℝ) (h: a/b = a/c) (g : a ≠ 0) : 1/b = 1/c :=
by rwa [← mul_right_inj' g, one_div_eq_inv, one_div_eq_inv]
import data.nat.modeq
example : unit ≠ bool :=
begin
assume h,
have : ∀ x y : unit, x = y, { intros, cases x, cases y, refl },
rw h at this,
exact absurd (this tt ff) dec_trivial
end
example (p : ℕ) (hp : p % 4 = 2) : 4 ∣ p - 2 :=
⟨p / 4, _⟩
#exit
import tactic
open set
class topological_space (X : Type) :=
(is_open : set X → Prop)
(is_open_univ : is_open univ)
(is_open_inter : ∀ (U V : set X), is_open U → is_open V → is_open (U ∩ V))
(is_open_sUnion : ∀ (𝒞 : set (set X)), (∀U ∈ 𝒞, is_open U) → is_open (⋃₀ 𝒞))
namespace topological_space
variables {X : Type} [topological_space X]
lemma open_iff_locally_open (V : set X) :
is_open V ↔ ∀ x : X, x ∈ V → ∃ U : set X, x ∈ U ∧ is_open U ∧ U ⊆ V :=
begin
split,
{ intro hV,
intros x hx,
use [V, hx, hV] },
{ intro h,
let 𝒞 : set (set X) := {U : set X | ∃ (x : X) (hx : x ∈ V), U = classical.some (h x hx)},
have h𝒞 : ∀ U ∈ 𝒞, ∃ (x : X) (hx : x ∈ V), x ∈ U ∧ is_open U ∧ U ⊆ V,
{ intros U hU,
rcases hU with ⟨x, hx, rfl⟩,
use [x, hx],
exact classical.some_spec (h x hx) },
convert is_open_sUnion 𝒞 _,
{ ext x, split,
{ intro hx,
rw mem_sUnion,
use classical.some (h x hx),
split,
use [x, hx],
have h := classical.some_spec (h x hx),
exact h.1 },
{ intro hx,
rw mem_sUnion at hx,
rcases hx with ⟨U, hU, hxU⟩,
rcases h𝒞 U hU with ⟨_, _, _, _, hUV⟩,
apply hUV,
exact hxU }},
{ intros U hU,
rcases (h𝒞 U hU) with ⟨_, _, _, hU, _⟩,
exact hU },
},
end
set_option old_structure_cmd true
namespace lftcm
/-- `monoid M` is the type of monoid structures on a type `M`. -/
class monoid (M : Type) extends has_mul M, has_one M :=
(mul_assoc : ∀ (a b c : M), a * b * c = a * (b * c))
(one_mul : ∀ (a : M), 1 * a = a)
(mul_one : ∀ (a : M), a * 1 = a)
lemma one_mul {M : Type} [monoid M] (a : M) : 1 * a = a := monoid.one_mul _
lemma mul_assoc {M : Type} [monoid M] (a b c : M) :
a * b * c = a * (b * c) := monoid.mul_assoc _ _ _
/-- `group G` is the type of group structures on a type `G`. -/
class group (G : Type) extends monoid G, has_inv G :=
(mul_left_inv : ∀ (a : G), a⁻¹ * a = 1)
namespace group
variables {G : Type} [group G]
lemma mul_left_cancel (a b c : G) (Habac : a * b = a * c) : b = c :=
calc b = 1 * b : by rw lftcm.one_mul
... = (a⁻¹ * a) * b : by rw mul_left_inv
... = a⁻¹ * (a * b) : begin rw lftcm.mul_assoc, end -- ??
... = a⁻¹ * (a * c) : by rw Habac
... = (a⁻¹ * a) * c : begin rw mul_assoc, refl, end -- ??
... = 1 * c : by rw mul_left_inv
... = c : by rw one_mul
#exit
import data.polynomial
open polynomial
#print eval₂_hom
variables {R S T : Type} [comm_ring R] [comm_ring S] [comm_ring T]
noncomputable def eval₂' (f : R →+* S) (x : S) : polynomial R →+* S :=
by refine_struct { to_fun := polynomial.eval₂ f x }; simp
lemma eq_eval₂' (i : polynomial R →+* S) :
i = eval₂' (i.comp (ring_hom.of C)) (i X) :=
begin
ext f,
apply polynomial.induction_on f; simp [eval₂'] {contextual := tt},
end
example {f : R →+* S} {g : S →+* T} {p : polynomial R} (x : S):
eval₂' (g.comp f) (g x) p = g (eval₂' f x p) :=
begin
conv_rhs { rw [← ring_hom.comp_apply, eq_eval₂' (g.comp (eval₂' f x))] },
simp,
end
#exit
import data.nat.digits
lemma nat.div_lt_of_le : ∀ {n m k : ℕ} (h0 : n > 0) (h1 : m > 1) (hkn : k ≤ n), k / m < n
| 0 m k h0 h1 hkn := absurd h0 dec_trivial
| 1 m 0 h0 h1 hkn := by rwa nat.zero_div
| 1 m 1 h0 h1 hkn :=
have ¬ (0 < m ∧ m ≤ 1), from λ h, absurd (@lt_of_lt_of_le ℕ
(show preorder ℕ, from @partial_order.to_preorder ℕ (@linear_order.to_partial_order ℕ nat.linear_order))
_ _ _ h1 h.2) dec_trivial,
by rw [nat.div_def_aux, dif_neg this]; exact dec_trivial
| 1 m (k+2) h0 h1 hkn := absurd hkn dec_trivial
| (n+2) m k h0 h1 hkn := begin
rw [nat.div_def_aux],
cases decidable.em (0 < m ∧ m ≤ k) with h h,
{ rw [dif_pos h],
refine nat.succ_lt_succ _,
refine nat.div_lt_of_le (nat.succ_pos _) h1 _,
cases m with m,
{ exact absurd h.1 dec_trivial },
{ cases m with m,
{ exact absurd h1 dec_trivial },
{ clear h1 h,
induction m with m ih,
{ cases k with k,
{ exact nat.zero_le _ },
{ cases k with k,
{ exact nat.zero_le _ },
{ rw [nat.sub_succ, nat.sub_succ, nat.sub_zero, nat.pred_succ,
nat.pred_succ],
exact @linear_order.le_trans ℕ nat.linear_order _ _ _
(nat.le_succ k) (nat.le_of_succ_le_succ hkn) } } },
{ cases k with k,
{ rw [nat.zero_sub], exact nat.zero_le _ },
{ rw [nat.succ_sub_succ],
refine @linear_order.le_trans ℕ nat.linear_order _ _ _ _ ih,
refine nat.sub_le_sub_right _ _,
exact nat.le_succ _ } } } } },
{ rw dif_neg h,
exact nat.succ_pos _ }
end
lemma nat.div_lt_self'' {n m : ℕ} (h0 : n > 0) (hm : m > 1) : n / m < n :=
nat.div_lt_of_le h0 hm (le_refl _)
def f : ℕ → ℕ
| n :=
if h : 0 < n
then have n - 1 < n, from nat.sub_lt h zero_lt_one,
f (n - 1)
else 0
def digits_aux' (b : ℕ) (h : 2 ≤ b) : ℕ → list ℕ
| 0 := []
| (n+1) :=
have (n+1)/b < n+1 := nat.div_lt_self'' (nat.succ_pos _) h,
(n+1) % b :: digits_aux' ((n+1)/b)
def digits' : ℕ → ℕ → list ℕ
| 0 := digits_aux_0
| 1 := digits_aux_1
| (b+2) := digits_aux' (b+2) dec_trivial
theorem test (b n : ℕ) : digits' (b+2) (n+1) = (n+1)%(b+2) :: digits' (b+2) ((n+1)/(b+2)) := rfl -- works
theorem test' : digits' (0+2) (1+1) = (1+1)%(0+2) :: digits' (0+2) ((1+1)/(0+2)) := rfl
--#reduce digits (0+2) ((1+1)/(0+2))
variables (b n : ℕ)
#reduce digits' (b+2) (n+1)
#exit
import ring_theory.ideals
import ring_theory.principal_ideal_domain
import ring_theory.localization
import tactic
import order.bounded_lattice
import algebra.field_power
import order.conditionally_complete_lattice
universe u
class discrete_valuation_ring (R : Type u) [integral_domain R] [is_principal_ideal_ring R] :=
(prime_ideal' : ideal R)
(primality : prime_ideal'.is_prime)
(is_nonzero : prime_ideal' ≠ ⊥)
(unique_nonzero_prime_ideal : ∀ P : ideal R, P.is_prime → P = ⊥ ∨ P = prime_ideal')
namespace discrete_valuation_ring
def prime_ideal (R : Type u) [integral_domain R] [is_principal_ideal_ring R] [discrete_valuation_ring R] : ideal R :=
discrete_valuation_ring.prime_ideal'
instance is_prime (R : Type*) [integral_domain R] [is_principal_ideal_ring R] [discrete_valuation_ring R] : (prime_ideal R).is_prime :=
primality
variables {R : Type u} [integral_domain R] [is_principal_ideal_ring R] [discrete_valuation_ring R]
open discrete_valuation_ring
lemma prime_ideal_is_maximal : (prime_ideal R).is_maximal :=
begin
have f : prime_ideal R ≠ ⊥,
{ apply discrete_valuation_ring.is_nonzero },
apply is_prime.to_maximal_ideal,
exact f,
end
lemma unique_max_ideal : ∃! I : ideal R, I.is_maximal :=
begin
use prime_ideal R,
split,
{ exact prime_ideal_is_maximal },
{ intros y a,
cases discrete_valuation_ring.unique_nonzero_prime_ideal y a.is_prime,
{ exfalso,
rw h at a,
apply discrete_valuation_ring.primality.left,
exact a.right (prime_ideal R) (bot_lt_iff_ne_bot.2 discrete_valuation_ring.is_nonzero) },
{ assumption } }
end
instance is_local_ring : local_ring R := local_of_unique_max_ideal unique_max_ideal
open local_ring
noncomputable theory
open_locale classical
class discrete_valuation_field (K : Type*) [field K] :=
(v : K -> with_top ℤ )
(mul : ∀ (x y : K), v(x*y) = v(x) + v(y) )
(add : ∀ (x y : K), min (v(x)) (v(y)) ≤ v(x + y) )
(non_zero : ∀ (x : K), v(x) = ⊤ ↔ x = 0 )
namespace discrete_valuation_field
definition valuation (K : Type*) [field K] [ discrete_valuation_field K ] : K -> with_top ℤ := v
variables {K : Type*} [field K] [discrete_valuation_field K]
lemma with_top.cases (a : with_top ℤ) : a = ⊤ ∨ ∃ n : ℤ, a = n :=
begin
cases a with n,
{ -- a = ⊤ case
left,
refl, -- true by definition
},
{ -- ℤ case
right,
use n,
refl, -- true by definition
}
end
lemma sum_zero_iff_zero (a : with_top ℤ) : a + a = 0 ↔ a = 0 :=
begin
split,
{ -- the hard way
intro h, -- h is a proof of a+a=0
-- split into cases
cases (with_top.cases a) with htop hn,
{ -- a = ⊤
rw htop at h,
-- h is false
cases h,
-- no cases!
},
{ -- a = n
cases hn with n hn,
rw hn at h ⊢,
-- now h says n+n=0 and our goal is n=0
-- but these are equalities in `with_top ℤ
-- so we need to get them into ℤ
-- A tactic called `norm_cast` does this
norm_cast at h ⊢,
-- we finally have a hypothesis n + n = 0
-- and a goal n = 0
-- and everything is an integer
rw add_self_eq_zero at h,
assumption
}
},
{ -- the easy way
intro ha,
rw ha,
simp
}
end
--Thanks Kevin!
lemma val_one_eq_zero : v(1 : K) = 0 :=
begin
have h : (1 : K) * 1 = 1,
simp,
apply_fun v at h,
rw mul at h,
-- now we know v(1)+v(1)=v(1) and we want to deduce v(1)=0 (i.e. rule out v(1)=⊤)
rcases (with_top.cases (v(1:K))) with h1 | ⟨n, h2⟩, -- do all the cases in one go
{ rw non_zero at h1,
cases (one_ne_zero h1)
},
{ rw h2 at *,
norm_cast at *,
-- library_search found the next line
exact add_left_eq_self.mp (congr_arg (has_add.add n) (congr_arg (has_add.add n) h)),
},
end
lemma val_minus_one_is_zero : v((-1) : K) = 0 :=
begin
have f : (-1:K)*(-1:K) = (1 : K),
simp,
have g : v((-1 : K)*(-1 : K)) = v(1 : K),
simp,
have k : v((-1 : K)*(-1 : K)) = v(-1 : K) + v(-1 : K),
{
apply mul,
},
rw k at g,
rw val_one_eq_zero at g,
rw <-sum_zero_iff_zero,
exact g,
end
@[simp] lemma val_zero : v(0:K) = ⊤ :=
begin
rw non_zero,
end
lemma with_top.transitivity (a b c : with_top ℤ) : a ≤ b -> b ≤ c -> a ≤ c :=
begin
rintros,
cases(with_top.cases c) with h1 h2,
{
rw h1,
simp,
},
{
cases h2 with n h2,
cases(with_top.cases a) with k1 k2,
{
rw [k1, h2],
rw k1 at a_1,
rw h2 at a_2,
cases(with_top.cases b) with l1 l2,
{
rw l1 at a_2,
exact a_2,
},
{
cases l2 with m l2,
rw l2 at a_1,
exfalso,
apply with_top.not_top_le_coe m,
exact a_1,
},
},
{
cases k2 with m k2,
cases(with_top.cases b) with l1 l2,
{
rw [l1,h2] at a_2,
exfalso,
apply with_top.not_top_le_coe n,
exact a_2,
},
{
cases l2 with k l2,
rw [k2,l2] at a_1,
rw [l2,h2] at a_2,
rw [k2,h2],
rw with_top.coe_le_coe,
rw with_top.coe_le_coe at a_1,
rw with_top.coe_le_coe at a_2,
transitivity k,
exact a_1,
exact a_2,
},
},
},
end
def val_ring (K : Type*) [field K] [discrete_valuation_field K] := { x : K | 0 ≤ v x }
instance (K : Type*) [field K] [discrete_valuation_field K] : is_add_subgroup (val_ring K) :=
{
zero_mem := begin
unfold val_ring,
simp,
end,
add_mem := begin
unfold val_ring,
simp only [set.mem_set_of_eq],
rintros,
have g : min (v(a)) (v(b)) ≤ v(a + b),
{
apply add,
},
rw min_le_iff at g,
cases g,
{
exact with_top.transitivity _ _ _ a_1 g,
},
{
exact with_top.transitivity _ _ _ a_2 g,
},
end,
neg_mem := begin
unfold val_ring,
rintros,
simp only [set.mem_set_of_eq],
simp only [set.mem_set_of_eq] at a_1,
have f : -a = a * (-1 : K) := by simp,
rw [f, mul, val_minus_one_is_zero],
simp [a_1],
end,
}
instance (K:Type*) [field K] [discrete_valuation_field K] : is_submonoid (val_ring K) :=
{ one_mem := begin
unfold val_ring,
simp,
rw val_one_eq_zero,
norm_num,
end,
mul_mem := begin
unfold val_ring,
rintros,
simp,
simp at a_1,
simp at a_2,
rw mul,
apply add_nonneg' a_1 a_2,
end, }
instance valuation_ring (K:Type*) [field K] [discrete_valuation_field K] : is_subring (val_ring K) :=
{}
instance is_domain (K:Type*) [field K] [discrete_valuation_field K] : integral_domain (val_ring K) :=
subring.domain (val_ring K)
def unif (K:Type*) [field K] [discrete_valuation_field K] : set K := { π | v π = 1 }
variables (π : K) (hπ : π ∈ unif K)
lemma val_unif_eq_one (hπ : π ∈ unif K) : v(π) = 1 :=
begin
unfold unif at hπ,
simp at hπ,
exact hπ,
end
lemma unif_ne_zero (hπ : π ∈ unif K) : π ≠ 0 :=
begin
simp,
unfold unif at hπ,
simp at hπ,
intro g,
rw <-non_zero at g,
rw hπ at g,
cases g,
end
lemma with_top.add_happens (a b c : with_top ℤ) (ne_top : a ≠ ⊤) : b=c ↔ a+b = a+c :=
begin
cases with_top.cases a,
{
exfalso,
apply ne_top,
exact h,
},
cases h with n h,
rw h,
split,
{
rintros,
rw a_1,
},
cases with_top.cases b,
{
rw h_1,
rw with_top.add_top,
rintros,
have b_1 : ↑n + c = ⊤,
exact eq.symm a_1,
rw with_top.add_eq_top at b_1,
cases b_1,
{
exfalso,
apply with_top.coe_ne_top,
{
exact b_1,
},
},
exact eq.symm b_1,
},
{
cases h_1 with m h_1,
rw h_1,
cases with_top.cases c,
{
rw h_2,
rintros,
rw with_top.add_top at a_1,
rw with_top.add_eq_top at a_1,
cases a_1,
{
exfalso,
apply with_top.coe_ne_top,
exact a_1,
},
{
exact a_1,
},
},
cases h_2 with l h_2,
rw h_2,
rintros,
norm_cast,
norm_cast at a_1,
simp at a_1,
assumption,
}
end
lemma with_top.add_le_happens (a b c : with_top ℤ) (ne_top : a ≠ ⊤) : b ≤ c ↔ a + b ≤ a+c :=
begin
rcases(with_top.cases a) with rfl | ⟨a, rfl⟩;
rcases(with_top.cases b) with rfl | ⟨b, rfl⟩;
rcases(with_top.cases c) with rfl | ⟨n, rfl⟩;
try {simp},
simp at ne_top,
assumption,
simp at ne_top,
exfalso,
assumption,
rw <-with_top.coe_add,
apply with_top.coe_ne_top,
repeat{rw <-with_top.coe_add,},
rw with_top.coe_le_coe,
simp,
end
lemma with_top.distrib (a b c : with_top ℤ) (na : a ≠ ⊤) (nb : b ≠ ⊤) (nc : c ≠ ⊤) : (a + b)*c = a*c + b*c :=
begin
rcases(with_top.cases a) with rfl | ⟨a, rfl⟩;
rcases(with_top.cases b) with rfl | ⟨b, rfl⟩;
rcases(with_top.cases c) with rfl | ⟨n, rfl⟩;
try {simp},
repeat
{
simp at na,
exfalso,
exact na,
},
{
simp at nb,
exfalso,
exact nb,
},
{
simp at nc,
exfalso,
exact nc,
},
rw <-with_top.coe_add,
repeat {rw <-with_top.coe_mul},
rw <-with_top.coe_add,
rw with_top.coe_eq_coe,
rw right_distrib,
end
lemma one_mul (a : with_top ℤ) : 1 * a = a :=
begin
cases (with_top.cases) a with a ha,
{
rw a,
simp,
},
{
cases ha with n ha,
rw ha,
norm_cast,
simp,
}
end
lemma nat_ne_top (n :ℕ) : (n : with_top ℤ) ≠ ⊤ :=
begin
simp,
end
lemma val_inv (x : K) (nz : x ≠ 0) : v(x) + v(x)⁻¹ = 0 :=
begin
rw <- mul,
rw mul_inv_cancel,
{
rw val_one_eq_zero,
},
exact nz,
end
lemma with_top.sub_add_eq_zero (n : ℕ) : ((-n : ℤ) : with_top ℤ) + (n : with_top ℤ) = 0 :=
begin
rw <-with_top.coe_nat,
rw <-with_top.coe_add,
simp only [add_left_neg, int.nat_cast_eq_coe_nat, with_top.coe_zero],
end
lemma with_top.add_sub_eq_zero (n : ℕ) : (n : with_top ℤ) + ((-n : ℤ) : with_top ℤ) = 0 :=
begin
rw <-with_top.coe_nat,
rw <-with_top.coe_add,
simp only [add_right_neg, int.nat_cast_eq_coe_nat, with_top.coe_zero],
end
lemma contra_non_zero (x : K) (n : ℕ) (nz : n ≠ 0) : v(x^n) ≠ ⊤ ↔ x ≠ 0 :=
begin
split,
{
contrapose,
simp,
intro,
rw a,
rw zero_pow',
{
exact val_zero,
},
{
exact nz,
},
},
{
contrapose,
simp,
intro,
rw non_zero at a,
contrapose a,
apply pow_ne_zero,
exact a,
},
end
lemma contra_non_zero_one (x : K) : v(x) ≠ ⊤ ↔ x ≠ 0 :=
begin
split,
{
intro,
rw <-pow_one x at a,
rw contra_non_zero x 1 at a,
exact a,
simp,
},
{
contrapose,
simp,
rw non_zero,
simp,
},
end
lemma val_nat_power (a : K) (nz : a ≠ 0) : ∀ n : ℕ, v(a^n) = (n : with_top ℤ)*v(a) :=
begin
rintros,
induction n with d hd,
{
rw pow_zero,
rw val_one_eq_zero,
simp,
},
{
rw nat.succ_eq_add_one,
rw pow_succ',
rw mul,
rw hd,
norm_num,
rw with_top.distrib,
rw one_mul,
apply nat_ne_top,
apply with_top.one_ne_top,
intro,
rw non_zero at a_1,
apply nz,
exact a_1,
}
end
lemma val_int_power (a : K) (nz : a ≠ 0) : ∀ n : ℤ, v(a^n) = (n : with_top ℤ)*v(a) :=
begin
rintros,
cases n,
{
rw fpow_of_nat,
rw val_nat_power,
{
simp only [int.of_nat_eq_coe],
rw <-with_top.coe_nat,
simp only [int.nat_cast_eq_coe_nat],
},
exact nz,
},
{
simp only [fpow_neg_succ_of_nat],
rw nat.succ_eq_add_one,
rw with_top.add_happens (v (a ^ (n + 1))) (v (a ^ (n + 1))⁻¹) (↑-[1+ n] * v a),
{
rw val_inv,
{
rw val_nat_power,
{
simp only [nat.cast_add, nat.cast_one],
rw <-with_top.distrib,
{
simp only [zero_eq_mul],
left,
rw int.neg_succ_of_nat_coe',
rw sub_eq_add_neg,
rw with_top.coe_add,
rw add_comm (↑-↑n),
rw <-add_assoc,
rw add_comm,
rw add_assoc,
rw <-with_top.coe_one,
rw <-with_top.coe_add,
simp,
rw with_top.sub_add_eq_zero,
},
{
norm_cast,
apply with_top.nat_ne_top,
},
{
simp,
},
{
intro,
simp_rw [non_zero, nz] at a_1,
exact a_1,
},
},
{
exact nz,
},
},
{
apply pow_ne_zero,
exact nz,
},
},
{
rw contra_non_zero,
{
exact nz,
},
{
simp,
},
},
},
end
lemma unit_iff_val_zero (α : K) (hα : α ∈ val_ring K) (nzα : α ≠ 0) : v (α) = 0 ↔ ∃ β ∈ val_ring K, α * β = 1 :=
begin
split,
{
rintros,
use α⁻¹,
split,
{
{
unfold val_ring,
simp,
rw <-with_top.coe_zero,
rw with_top.coe_le_iff,
rintros,
rw with_top.add_happens (v(α)) _ _ at a_1,
{
rw val_inv at a_1,
{
rw a at a_1,
simp only [with_top.zero_eq_coe, zero_add] at a_1,
rw a_1,
},
exact nzα,
},
simp_rw [contra_non_zero_one],
exact nzα,
},
},
{
rw mul_inv_cancel,
exact nzα,
},
},
{
rintros,
cases a with b a,
simp at a,
cases a,
unfold val_ring at a_left,
simp at a_left,
have f : v((α)*(b)) = v(1:K),
{
rw a_right,
},
rw mul at f,
rw val_one_eq_zero at f,
rw add_eq_zero_iff' at f,
{
cases f,
exact f_left,
},
{
erw val_ring at hα,
simp at hα,
exact hα,
},
{
exact a_left,
},
},
end
lemma val_eq_iff_asso (x y : K) (hx : x ∈ val_ring K) (hy : y ∈ val_ring K) (nzx : x ≠ 0) (nzy : y ≠ 0) : v(x) = v(y) ↔ ∃ β ∈ val_ring K, v(β) = 0 ∧ x * β = y :=
begin
split,
intros,
use (x⁻¹*y),
{
{
unfold val_ring,
simp,
rw mul,
rw with_top.add_happens (v(x⁻¹)) _ _ at a,
{
rw add_comm at a,
rw val_inv at a,
{
rw <-a,
norm_num,
rw mul_inv_cancel_assoc_right,
exact nzx,
},
exact nzx,
},
{
intro f,
rw non_zero at f,
simp at f,
apply nzx,
exact f,
},
},
},
{
rintros,
cases a with z a,
simp at a,
cases a,
cases a_right with a_1 a_2,
apply_fun v at a_2,
rw mul at a_2,
rw a_1 at a_2,
simp at a_2,
exact a_2,
},
end
lemma unif_assoc (x : K) (hx : x �� val_ring K) (nz : x ≠ 0) (hπ : π ∈ unif K) : ∃ β ∈ val_ring K, (v(β) = 0 ∧ ∃! n : ℤ, x * β = π^n) :=
begin
have hπ' : π ≠ 0,
{
apply unif_ne_zero,
exact hπ,
},
unfold unif at hπ,
simp at hπ,
cases (with_top.cases) (v(x)),
{
rw non_zero at h,
exfalso,
apply nz,
exact h,
},
{
cases h with n h,
split,
let y := x⁻¹ * π^n,
have g : v(y) = 0,
{
rw [mul, val_int_power π, hπ, add_comm],
norm_cast,
simp,
rw [<-h, val_inv],
exact nz,
exact hπ',
},
have f : y ∈ val_ring K,
{
unfold val_ring,
simp,
rw g,
norm_num,
},
{
use f,
split,
{
exact g,
},
rw mul_inv_cancel_assoc_right,
use n,
{
split,
simp only [eq_self_iff_true],
rintros,
apply_fun v at a,
rw [val_int_power, val_int_power, hπ] at a,
{
norm_cast at a,
simp at a,
exact eq.symm a,
},
exact hπ',
exact hπ',
},
exact nz,
},
},
end
lemma blah (n : ℤ) : n < n -> false :=
begin
simp only [forall_prop_of_false, not_lt],
end
lemma val_is_nat (hπ : π ∈ unif K) (x : val_ring K) (nzx : x ≠ 0) : ∃ m : ℕ, v(x:K) = ↑m :=
begin
cases with_top.cases (v(x:K)),
{
rw h,
simp,
rw non_zero at h,
apply nzx,
exact subtype.eq h,
},
{
cases h with n h,
cases n,
{
use n,
simp_rw h,
simp,
rw <-with_top.coe_nat,
simp,
},
{
have H : 0 ≤ v(x:K),
exact x.2,
rw h at H,
norm_cast at H,
exfalso,
contrapose H,
simp,
tidy,
exact int.neg_succ_lt_zero n,
},
},
end
lemma is_pir (hπ : π ∈ unif K) : is_principal_ideal_ring (val_ring K) :=
begin
split,
rintros,
rintros,
by_cases S = ⊥,
{
rw h,
use 0,
apply eq.symm,
rw submodule.span_singleton_eq_bot,
},
let Q := {n : ℕ | ∃ x ∈ S, (n : with_top ℤ) = v(x:K) },
have g : v(π ^(Inf Q)) = ↑(Inf Q),
{
rw val_nat_power,
rw val_unif_eq_one,
rw <-with_top.coe_one,
rw <-with_top.coe_nat,
rw <-with_top.coe_mul,
rw mul_one,
exact hπ,
apply unif_ne_zero,
exact hπ,
},
have nz : π^(Inf Q) ≠ 0,
{
assume a,
apply_fun v at a,
rw g at a,
rw val_zero at a,
apply with_top.nat_ne_top (Inf Q),
exact a,
},
use π^(Inf Q),
{
unfold val_ring,
simp,
rw g,
rw <-with_top.coe_nat,
norm_cast,
norm_num,
},
apply submodule.ext,
rintros,
split,
{
rintros,
rw submodule.mem_span_singleton,
use (x * (π^(Inf Q))⁻¹),
{
dunfold val_ring,
simp,
rw mul,
by_cases x = 0,
{
rw h,
simp,
},
rw with_top.add_le_happens (v(π^(Inf Q))),
{
norm_num,
rw add_left_comm,
rw val_inv,
simp,
rw g,
have f' : ∃ m : ℕ, v(x:K) = ↑m,
{
apply val_is_nat,
use hπ,
exact h,
},
cases f' with m f',
rw f',
rw <-with_top.coe_nat,
rw <-with_top.coe_nat,
norm_cast,
have h' : m ∈ Q,
{
split,
simp,
split,
use a,
use [eq.symm f'],
},
by { rw [nat.Inf_def ⟨m, h'⟩], exact nat.find_min' ⟨m, h'⟩ h' },
assumption,
},
rw g,
exact with_top.nat_ne_top _,
},
{
tidy,
assoc_rw inv_mul_cancel nz,
simp,
},
},
{
rw submodule.mem_span,
rintros,
specialize a S,
apply a,
have f : ∃ z ∈ S, v(z : K) = ↑(Inf Q),
{
have f' : ∃ x ∈ S, v(x : K) ≠ ⊤,
{
contrapose h,
simp at h,
simp,
apply ideal.ext,
rintros,
simp only [submodule.mem_bot],
split,
rintros,
specialize h x_1,
simp at h,
have q : v(x_1 : K) = ⊤,
apply h,
exact a_1,
rw non_zero at q,
exact subtype.ext q,
rintros,
rw a_1,
simp,
},
have p : Inf Q ∈ Q,
{
apply nat.Inf_mem,
contrapose h,
simp,
by_contradiction,
cases f' with x' f',
have f_1 : ∃ m : ℕ, v(x':K) = ↑(m),
{
apply val_is_nat,
exact hπ,
cases f',
contrapose f'_h,
simp,
rw non_zero,
simp at f'_h,
rw f'_h,
simp,
},
cases f_1 with m' f_1,
have g' : m' ∈ Q,
{
simp,
use x',
simp,
split,
cases f',
assumption,
exact eq.symm f_1,
},
apply h,
use m',
apply g',
},
simp at p,
cases p with z p,
cases p,
use z,
cases p_left,
assumption,
split,
cases p_left,
assumption,
simp,
exact eq.symm p_right,
},
cases f with z f,
rw <-g at f,
simp at f,
cases f,
rw val_eq_iff_asso at f_right,
{
cases f_right with w f_1,
cases f_1 with f_1 f_2,
cases f_2 with f_2 f_3,
rw set.singleton_subset_iff,
simp only [submodule.mem_coe],
simp_rw [← f_3],
change z * ⟨w,f_1⟩ ∈ S,
apply ideal.mul_mem_right S f_left,
},
simp,
{
unfold val_ring,
simp,
rw g,
rw <-with_top.coe_nat,
norm_cast,
simp,
},
{
rw g at f_right,
contrapose f_right,
simp at f_right,
rw <-non_zero at f_right,
rw f_right,
simp,
},
{
exact nz,
},
},
recover,
end
end discrete_valuation_field
end discrete_valuation_ring
#exit
import set_theory.cardinal
universes u v
example : cardinal.lift.{u v} = cardinal.lift.{u (max u v)} :=
funext $ λ x, quotient.induction_on x
(λ x, quotient.sound ⟨⟨λ ⟨x⟩, ⟨x⟩, λ ⟨x⟩, ⟨x⟩, λ ⟨_,⟩, rfl, λ ⟨_⟩, rfl⟩⟩)
import tactic.rcases
lemma L1 : forall (n m: ℕ) (p : ℕ → Prop), (p n ∧ ∃ (u:ℕ), p u ∧ p m) ∨ (¬p n ∧ p m) → n = m :=
begin
intros n m p H,
rcases H with ⟨H1, u, H2, H3⟩ | ⟨H1, H2⟩,
end
#exit
import data.polynomial
example {K L M : Type*} [field K] [field L] [field M]
(i : K →+* L) (j : L →+* M) (f : polynomial K)
(h : ∃ x, f.eval₂ i x)
#exit
import ring_theory.eisenstein_criterion
variables {R : Type*} [integral_domain R]
lemma dvd_mul_prime {x a p : R} (hp : prime p) : x ∣ a * p → x ∣ a ∨ p ∣ x :=
λ ⟨y, hy⟩, (hp.div_or_div ⟨a, hy.symm.trans (mul_comm _ _)⟩).elim
or.inr
begin
rintros ⟨b, rfl⟩,
rw [mul_left_comm, mul_comm, domain.mul_right_inj hp.ne_zero] at hy,
rw [hy],
exact or.inl (dvd_mul_right _ _)
end
#print well_founded.m
in
lemma left_dvd_or_dvd_right_of_dvd_prime_mul {a : R} :
∀ {b p : R}, prime p → a ∣ p * b → p ∣ a ∨ a ∣ b :=
begin
rintros b p hp ⟨c, hc⟩,
rcases hp.2.2 a c (hc ▸ dvd_mul_right _ _) with h | ⟨x, rfl⟩,
{ exact or.inl h },
{ rw [mul_left_comm, domain.mul_right_inj hp.ne_zero] at hc,
exact or.inr (hc.symm ▸ dvd_mul_right _ _) }
end
#exit
import data.nat.basic data.quot
inductive rel : ℕ ⊕ ℕ → ℕ ⊕ ℕ → Prop
| zero : rel (sum.inl 0) (sum.inr 0)
| refl : ∀ x, rel x x
| symm : ∀ {x y}, rel x y → rel y x
| trans : ∀ {x y z}, rel x y → rel y z → rel x z
attribute [refl] rel.refl
attribute [symm] rel.symm
attribute [trans] rel.trans
instance srel : setoid (ℕ ⊕ ℕ) :=
{ r := rel,
iseqv := ⟨rel.refl, @rel.symm, @rel.trans⟩ }
def int' := quotient srel
#exit
import data.finset data.fintype.card
example (n m : ℕ) (hn : n ≠ 0) (hm : n ≤ m) : m ≠ 0 := λ h, by simp * at *
#print discrete
universe u
variables {α : Type u} [add_comm_monoid α]
open_locale big_operators
ʗ∁ C
example {u : Type*} {v : Type*} [fintype u] [fintype v] (f : u × v -> α) :
∑ (i : u), ∑ (j : v), f (i, j) = ∑ (p : u × v), f p :=
begin
rw <-finset.sum_product,
repeat { rw finset.univ },
sorry,
end
#exit
import data.set.finite tactic
variables {α : Type*} (r : α → α → Prop)
lemma well_founded_of_finite [is_irrefl α r] [is_trans α r]
(h : ∀ a₀, set.finite {a | r a a₀}) : well_founded r :=
⟨λ a₀, acc.intro _ (λ b hb, begin
cases h a₀ with fint,
refine @well_founded.fix {a | r a a₀} (λ b, acc r b) (λ x y : {a | r a a₀}, r x y)
(@fintype.well_founded_of_trans_of_irrefl _ fint
(λ x y : {a | r a a₀}, r x y) ⟨λ x y z h₁ h₂, trans h₁ h₂⟩
⟨λ x, irrefl x⟩) _ ⟨b, hb⟩,
rintros ⟨b, hb⟩ ih,
exact acc.intro _ (λ y hy, ih ⟨y, trans hy hb⟩ hy)
end)⟩
#exit
import algebra.group_power
theorem pow_eq_zero_1 {R : Type} [domain R] {r : R} {n : ℕ} : r ^ (n + 1) = 0 → r = 0
:= begin
rw (show r ^ (n + 1) = r ^ n * r,
by {
sorry, }),
sorry,
end
theorem pow_eq_zero_2 {R : Type} [domain R] {r : R} {n : ℕ} : r ^ (n + 1) = 0 → r = 0
:= pow_eq_zero -- it's in mathlib
import tactic
def five : ℕ := 5
meta def tac : tactic unit := tactic.focus1 `[tactic.intro1, tactic.applyc `five]
run_cmd add_interactive [`tac]
def C : ℕ → ℕ :=
by tac
#print C
inductive palindrome {α : Type} : list α → Prop
| nil : palindrome []
| singleton : \al palindrom []
def reverse {α : Type} : list α → list α
| [] := []
| (x :: xs) := reverse xs ++ [x]
end
#exit
import group_theory.subgroup ring_theory.ideal_operations
--attribute [irreducible] subgroup.normal
example {R : Type} [comm_ring R] (P : ideal R) (hP : P.is_prime) : P.is_prime :=
by apply_instance
example {G : Type} [group G] (N : subgroup G) (hN : N.normal) : N.normal :=
by apply_instance
#print subgroup.normal
#exit
import ring_theory.ideal_operations data.polynomial ring_theory.ideals tactic.apply_fun
open polynomial ideal.quotient
open_locale classical
variables {R : Type*} [integral_domain R]
open polynomial ideal.quotient
open finset
open_locale big_operators
lemma mul_eq_mul_prime_prod {α : Type*} [decidable_eq α] {x y a : R} {s : finset α}
{p : α → R} (hp : ∀ i ∈ s, prime (p i)) (hx : x * y = a * s.prod p) :
∃ t u b c, t ∪ u = s ∧ disjoint t u ∧ b * c = a ∧
x = b * t.prod p ∧ y = c * u.prod p :=
begin
induction s using finset.induction with i s his ih generalizing x y a,
{ exact ⟨∅, ∅, x, y, by simp [hx]⟩ },
{ rw [prod_insert his, ← mul_assoc] at hx,
have hpi : prime (p i), { exact hp i (mem_insert_self _ _) },
rcases ih (λ i hi, hp i (mem_insert_of_mem hi)) hx with
⟨t, u, b, c, htus, htu, hbc, rfl, rfl⟩,
have hpibc : p i ∣ b ∨ p i ∣ c,
from hpi.div_or_div ⟨a, by rw [hbc, mul_comm]⟩,
have hit : i ∉ t, from λ hit, his (htus ▸ mem_union_left _ hit),
have hiu : i ∉ u, from λ hiu, his (htus ▸ mem_union_right _ hiu),
rcases hpibc with ⟨d, rfl⟩ | ⟨d, rfl⟩,
{ rw [mul_assoc, mul_comm a, domain.mul_right_inj hpi.ne_zero] at hbc,
exact ⟨insert i t, u, d, c, by rw [insert_union, htus],
disjoint_insert_left.2 ⟨hiu, htu⟩,
by simp [← hbc, prod_insert hit, mul_assoc, mul_comm, mul_left_comm]⟩ },
{ rw [← mul_assoc, mul_right_comm b, domain.mul_left_inj hpi.ne_zero] at hbc,
exact ⟨t, insert i u, b, d, by rw [union_insert, htus],
disjoint_insert_right.2 ⟨hit, htu⟩,
by simp [← hbc, prod_insert hiu, mul_assoc, mul_comm, mul_left_comm]⟩ } }
end
lemma mul_eq_mul_prime_pow {x y a p : R} {n : ℕ} (hp : prime p) (hx : x * y = a * p ^ n) :
∃ i j b c, i + j = n ∧ b * c = a ∧ x = b * p ^ i ∧ y = c * p ^ j :=
begin
rcases mul_eq_mul_prime_prod (λ _ _, hp)
(show x * y = a * (range n).prod (λ _, p), by simpa) with
⟨t, u, b, c, htus, htu, rfl, rfl, rfl⟩,
exact ⟨t.card, u.card, b, c, by rw [← card_disjoint_union htu, htus, card_range], by simp⟩,
end
lemma eisenstein {f : polynomial R} {P : ideal R} (hP : P.is_prime)
(hfl : f.leading_coeff ∉ P)
(hfP : ∀ n : ℕ, ↑n < degree f → f.coeff n ∈ P)
(hfd0 : 0 < degree f) (h0 : f.coeff 0 ∉ P^2)
(hu : ∀ x : R, C x ∣ f → is_unit x) : irreducible f :=
have hf0 : f ≠ 0, from λ _, by simp * at *,
have hf : f.map (mk_hom P) =
C (mk_hom P (leading_coeff f)) * X ^ nat_degree f,
from polynomial.ext (λ n, begin
rcases lt_trichotomy ↑n (degree f) with h | h | h,
{ erw [coeff_map, ← mk_eq_mk_hom, eq_zero_iff_mem.2 (hfP n h),
coeff_C_mul, coeff_X_pow, if_neg, mul_zero],
rintro rfl, exact not_lt_of_ge degree_le_nat_degree h },
{ have : nat_degree f = n, from nat_degree_eq_of_degree_eq_some h.symm,
rw [coeff_C_mul, coeff_X_pow, if_pos this.symm, mul_one, leading_coeff, this, coeff_map] },
{ rw [coeff_eq_zero_of_degree_lt, coeff_eq_zero_of_degree_lt],
{ refine lt_of_le_of_lt (degree_C_mul_X_pow_le _ _) _,
rwa ← degree_eq_nat_degree hf0 },
{ exact lt_of_le_of_lt (degree_map_le _) h } }
end),
have hfd0 : 0 < f.nat_degree, from with_bot.coe_lt_coe.1
(lt_of_lt_of_le hfd0 degree_le_nat_degree),
⟨mt degree_eq_zero_of_is_unit (λ h, by simp [*, lt_irrefl] at *),
begin
rintros p q rfl,
rw [map_mul] at hf,
have : map (mk_hom P) p ∣ C (mk_hom P (p * q).leading_coeff) * X ^ (p * q).nat_degree,
from ⟨map (mk_hom P) q, hf.symm⟩,
rcases mul_eq_mul_prime_pow (show prime (X : polynomial (ideal.quotient P)),
from prime_of_degree_eq_one_of_monic degree_X monic_X) hf with
⟨m, n, b, c, hmnd, hbc, hp, hq⟩,
have hmn : 0 < m → 0 < n → false,
{ assume hm0 hn0,
have hp0 : p.eval 0 ∈ P,
{ rw [← coeff_zero_eq_eval_zero, ← eq_zero_iff_mem, mk_eq_mk_hom, ← coeff_map],
simp [hp, coeff_zero_eq_eval_zero, zero_pow hm0] },
have hq0 : q.eval 0 ∈ P,
{ rw [← coeff_zero_eq_eval_zero, ← eq_zero_iff_mem, mk_eq_mk_hom, ← coeff_map],
simp [hq, coeff_zero_eq_eval_zero, zero_pow hn0] },
apply h0,
rw [coeff_zero_eq_eval_zero, eval_mul, pow_two],
exact ideal.mul_mem_mul hp0 hq0 },
have hpql0 : (mk_hom P) (p * q).leading_coeff ≠ 0,
{ rwa [← mk_eq_mk_hom, ne.def, eq_zero_iff_mem] },
have hp0 : p ≠ 0, from λ h, by simp * at *,
have hq0 : q ≠ 0, from λ h, by simp * at *,
have hmn0 : m = 0 ∨ n = 0,
{ rwa [nat.pos_iff_ne_zero, nat.pos_iff_ne_zero, imp_false, not_not,
← or_iff_not_imp_left] at hmn },
have hbc0 : degree b = 0 ∧ degree c = 0,
{ apply_fun degree at hbc,
rwa [degree_C hpql0, degree_mul_eq, nat.with_bot.add_eq_zero_iff] at hbc },
have hmp : m ≤ nat_degree p,
from with_bot.coe_le_coe.1
(calc ↑m = degree (p.map (mk_hom P)) : by simp [hp, hbc0.1]
... ≤ degree p : degree_map_le _
... ≤ nat_degree p : degree_le_nat_degree),
have hmp : n ≤ nat_degree q,
from with_bot.coe_le_coe.1
(calc ↑n = degree (q.map (mk_hom P)) : by simp [hq, hbc0.2]
... ≤ degree q : degree_map_le _
... ≤ nat_degree q : degree_le_nat_degree),
have hpmqn : p.nat_degree = m ∧ q.nat_degree = n,
{ rw [nat_degree_mul_eq hp0 hq0] at hmnd, omega },
rcases hmn0 with rfl | rfl,
{ left,
rw [eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpmqn.1),
is_unit_C],
refine hu _ _,
rw [← eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpmqn.1)],
exact dvd_mul_right _ _ },
{ right,
rw [eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpmqn.2),
is_unit_C],
refine hu _ _,
rw [← eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpmqn.2)],
exact dvd_mul_left _ _ }
end⟩
#print axioms eisenstein
#exit
import algebra.ring
universe u
variables {R : Type} [comm_ring R] (M : submonoid R)
set_option pp.all true
#print comm_ring.zero_add
instance : comm_monoid (submonoid.localization M) :=
(submonoid.localization.r M).comm_monoid
@[elab_as_eliminator]
protected def lift_on₂ {α : Type*} [monoid α] {β} {c : con α } (q r : c.quotient) (f : α → α → β)
(h : ∀ a₁ a₂ b₁ b₂, c a₁ b₁ → c a₂ b₂ → f a₁ a₂ = f b₁ b₂) : β := quotient.lift_on₂' q r f h
def submonoid.localization.mk : R → M → submonoid.localization M :=
λ x y, (submonoid.localization.r M).mk' (x, y)
theorem r_of_eq {x y : R × M} (h : y.1 * x.2 = x.1 * y.2) :
submonoid.localization.r M x y :=
submonoid.localization.r_iff_exists.2 ⟨1, by rw h⟩
instance : has_zero (submonoid.localization M) :=
⟨submonoid.localization.mk M 0 1⟩
instance : has_add (submonoid.localization M) :=
⟨λ z w, lift_on₂ z w
(λ x y : R × M, submonoid.localization.mk M ((x.2 : R) * y.1 + y.2 * x.1) (x.2 * y.2)) $
λ r1 r2 r3 r4 h1 h2, (con.eq _).2
begin
rw submonoid.localization.r_eq_r' at h1 h2 ⊢,
cases h1 with t₅ ht₅,
cases h2 with t₆ ht₆,
use t₆ * t₅,
calc ((r1.2 : R) * r2.1 + r2.2 * r1.1) * (r3.2 * r4.2) * (t₆ * t₅) =
(r2.1 * r4.2 * t₆) * (r1.2 * r3.2 * t₅) + (r1.1 * r3.2 * t₅) * (r2.2 * r4.2 * t₆) : by ring
... = (r3.2 * r4.1 + r4.2 * r3.1) * (r1.2 * r2.2) * (t₆ * t₅) : by rw [ht₆, ht₅]; ring
end⟩
instance : has_neg (submonoid.localization M) :=
⟨λ z, con.lift_on z (λ x : R × M, submonoid.localization.mk M (-x.1) x.2) $
λ r1 r2 h, (con.eq _).2
begin
rw submonoid.localization.r_eq_r' at h ⊢,
cases h with t ht,
use t,
rw [neg_mul_eq_neg_mul_symm, neg_mul_eq_neg_mul_symm, ht],
ring,
end⟩
instance : add_semigroup (submonoid.localization M) := by apply_instance
set_option pp.all true
#print comm_ring.zero_add
@[instance]lemma C : comm_ring (submonoid.localization M) :=
{ zero := (0 : submonoid.localization M),
one := (1 : submonoid.localization M),
add := (+),
mul := (*),
zero_add := λ y : submonoid.localization M, quotient.induction_on' y _,
add_zero := λ y : submonoid.localization M, quotient.induction_on' y _,
add_assoc := λ m n k : submonoid.localization M,
quotient.induction_on₃' m n k _,
neg := has_neg.neg,
add_left_neg := λ y : submonoid.localization M, quotient.induction_on' y _,
add_comm := λ y z : submonoid.localization M, quotient.induction_on₂' z y _,
left_distrib := λ m n k : submonoid.localization M, quotient.induction_on₃' m n k _,
right_distrib := λ m n k : submonoid.localization M, quotient.induction_on₃' m n k _,
..submonoid.localization.comm_monoid M }
-- { intros,
-- refine quotient.sound (r_of_eq M _),
-- simp only [prod.snd_mul, prod.fst_mul, submonoid.coe_mul],
-- ring }
-- end
example (y m n k : submonoid.localization M) : Prop := @eq.{1} M.localization
(@has_add.add.{0} M.localization
(@add_semigroup.to_has_add.{0} M.localization
(@add_semigroup.mk.{0} M.localization
(@has_add.add.{0} M.localization (@submonoid.localization.has_add R _inst_1 M))
_
-- (λ (m n k : M.localization),
-- @quotient.induction_on₃'.{1 1 1}
-- (prod.{0 0} R
-- (@coe_sort.{1 2}
-- (@submonoid.{0} R
-- (@comm_monoid.to_monoid.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))))
-- (@submonoid.has_coe_to_sort.{0} R
-- (@comm_monoid.to_monoid.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))))
-- M))
-- (prod.{0 0} R
-- (@coe_sort.{1 2}
-- (@submonoid.{0} R
-- (@comm_monoid.to_monoid.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))))
-- (@submonoid.has_coe_to_sort.{0} R
-- (@comm_monoid.to_monoid.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))))
-- M))
-- (prod.{0 0} R
-- (@coe_sort.{1 2}
-- (@submonoid.{0} R
-- (@comm_monoid.to_monoid.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))))
-- (@submonoid.has_coe_to_sort.{0} R
-- (@comm_monoid.to_monoid.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))))
-- M))
-- (@submonoid.localization.r.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))
-- M).to_setoid
-- (@submonoid.localization.r.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))
-- M).to_setoid
-- (@submonoid.localization.r.{0} R
-- (@comm_semiring.to_comm_monoid.{0} R (@comm_ring.to_comm_semiring.{0} R _inst_1))
-- M).to_setoid
-- (λ (_x _x_1 _x_2 : M.localization),
-- @eq.{1} M.localization
-- (@has_add.add.{0} M.localization
-- (@has_add.mk.{0} M.localization
-- (@has_add.add.{0} M.localization (@submonoid.localization.has_add R _inst_1 M)))
-- (@has_add.add.{0} M.localization
-- (@has_add.mk.{0} M.localization
-- (@has_add.add.{0} M.localization (@submonoid.localization.has_add R _inst_1 M)))
-- _x
-- _x_1)
-- _x_2)
-- (@has_add.add.{0} M.localization
-- (@has_add.mk.{0} M.localization
-- (@has_add.add.{0} M.localization (@submonoid.localization.has_add R _inst_1 M)))
-- _x
-- (@has_add.add.{0} M.localization
-- (@has_add.mk.{0} M.localization
-- (@has_add.add.{0} M.localization (@submonoid.localization.has_add R _inst_1 M)))
-- _x_1
-- _x_2)))
-- m
-- n
-- k
-- _)
)) _
-- (@has_zero.zero.{0} M.localization
-- (@has_zero.mk.{0} M.localization
-- (@has_zero.zero.{0} M.localization (@submonoid.localization.has_zero R _inst_1 M))))
y
)
y
#exit
import category_theory.limits.shapes.pullbacks
open category_theory
open category_theory.limits
universes v u
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
#print is_colimit
def pushout_of_epi {X Y : C} (f : X ⟶ Y) [epi f] :
is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f) :=
{ desc := λ s, s.ι.app walking_span.left,
fac' := λ s j, option.cases_on j
(by { tidy, convert s.w walking_span.hom.fst })
(λ j, walking_pair.cases_on j (by tidy) begin
tidy,
end) }
theorem epi_of_pushout {X Y : C} (f : X ⟶ Y)
(is_colim : is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f)) : epi f := sorry
#exit
import data.fintype.basic
#exit
import algebra.ring tactic
def add : Π l₁ l₂ : list nat, list nat
| [] l₂ := l₂
| l₁ [] := l₁
| (a::l₁) (b::l₂) :=
if h : b < a then b :: add (a :: l₁) l₂
else a :: add l₁ (b :: l₂)
#exit
namespace tactic
meta def protect (n : name) : tactic unit :=
do env ← get_env, set_env $ env.mk_protected n
end tactic
namespace nat
private lemma X : true := trivial
run_cmd tactic.protect `nat.X
example : true := X
end nat
open category_theory
instance A : is_semiring_hom (coe : ℤ → ℚ) :=
by refine_struct { .. }; simp
@[reducible] def icast : ℤ →+* ℚ := ring_hom.of coe
lemma unique_hom {R : Type*} [ring R] (f g : ℚ →+* R) : f = g :=
begin
ext,
refine rat.num_denom_cases_on x (λ n d hd0 _, _),
have hd0 : (d : ℚ) ≠ 0, { simpa [nat.pos_iff_ne_zero] using hd0 },
have hf : ∀ n : ℤ, f n = n, from λ _, (f.comp icast).eq_int_cast _,
have hg : ∀ n : ℤ, g n = n, from λ _, (g.comp icast).eq_int_cast _,
have : is_unit ((d : ℤ) : R),
from ⟨⟨f d, f (1 / d), by rw [← ring_hom.map_mul, mul_div_cancel' _ hd0, f.map_one],
by rw [← ring_hom.map_mul, div_mul_cancel _ hd0, f.map_one]⟩,
by simp [hf]⟩ ,
rw [rat.mk_eq_div, div_eq_mul_inv, ring_hom.map_mul, ring_hom.map_mul, hf, hg,
← this.mul_left_inj],
conv_lhs { rw ← hf d },
rw [← hg d, mul_assoc, mul_assoc, ← f.map_mul, ← g.map_mul, int.cast_coe_nat,
inv_mul_cancel hd0],
simp
end
theorem mono_epi_not_iso : ∃ (A B : Ring.{0}) (f : A ⟶ B),
mono.{0} f ∧ epi.{0} f ∧ (is_iso.{0} f → false) :=
⟨Ring.of ℤ, Ring.of ℚ, icast,
⟨begin
intros,
ext,
tidy,
rw [function.funext_iff] at h_1,
erw [← @int.cast_inj ℚ],
exact h_1 _
end⟩,
⟨λ _ _ _ _, unique_hom _ _⟩,
λ h,
have (2 : ℤ) ∣ 1,
from ⟨(h.inv : ℚ →+* ℤ) (show ℚ, from (1 : ℚ) / 2),
have (2 : ℤ) = (h.inv : ℚ →+* ℤ) (2 : ℚ), by simp [bit0],
begin
rw [this, ← ring_hom.map_mul],
norm_num,
end⟩,
absurd this (by norm_num)⟩
#exit
import ring_theory.ideal_operations data.polynomial ring_theory.ideals tactic
section comm_ring
variables {R : Type*} {S : Type*} [comm_ring R] [comm_ring S]
open polynomial ideal.quotient
open_locale classical
lemma thingy {R : Type*} [comm_ring R] (I : ideal R)
{a : R} (hab : a ∉ I^2) (hu : ∀ u ∉ I, u ∣ a → is_unit u)
(ha : ¬ is_unit a) : irreducible a :=
⟨ha, λ x y hxy,
have hxPyP : x ∈ I → y ∈ I → false,
from λ hxP hyP, hab (by rw [hxy, pow_two]; exact ideal.mul_mem_mul hxP hyP),
(show x ∉ I ∨ y ∉ I, by rwa [or_iff_not_imp_left, not_not]).elim
(λ hx, or.inl (hu x hx $ by simp [hxy]))
(λ hy, or.inr (hu y hy $ by simp [hxy]))⟩
lemma thingy2 {R : Type*} [comm_ring R] (I : ideal R)
{a : R} (hab : a ∉ I^2) (hu : ∀ x ∉ I, x ∣ a → ∃ u, 1 - u * x ∈ I)
(ha : ¬ is_unit a) : irreducible a :=
⟨ha, λ x y hxy,
have hxPyP : x ∈ I → y ∈ I → false,
from λ hxP hyP, hab (by rw [hxy, pow_two]; exact ideal.mul_mem_mul hxP hyP),
(show x ∉ I ∨ y ∉ I, by rwa [or_iff_not_imp_left, not_not]).elim
(λ hx, begin
cases hu x hx (by simp [hxy]) with u hu,
end)
(λ hy, or.inr (hu y hy $ by simp [hxy]))⟩
lemma ideal.sup_pow_two {I J : ideal R} : (I ⊔ J) ^ 2 = I ^ 2 ⊔ I * J ⊔ J ^ 2 :=
by simp [ideal.sup_mul, ideal.mul_sup, mul_comm, pow_two, sup_assoc]
#print ring_hom.of
lemma eisenstein {R : Type*} [integral_domain R] {f : polynomial R}
{P : ideal R} --(hP : P.is_prime) --(hfl : f.leading_coeff ∉ P)
--(hfP : ∀ n : ℕ, ↑n < degree f → f.coeff n ∈ P)
--(hfd0 : 0 < degree f)
(h0 : f.coeff 0 ∉ P^2)
(hu : ∀ x : R, C x ∣ f → is_unit x) : irreducible f :=
have eq_id : (ring_hom.of (eval (0 : R))).comp (ring_hom.of C) = ring_hom.id _,
by ext; simp,
have h_ker : ideal.span {(X : polynomial R)} ≤ (ring_hom.of (eval (0 : R))).ker,
from ideal.span_le.2 (λ _, by simp [ring_hom.mem_ker] {contextual := tt}),
thingy (P.map (ring_hom.of C) ⊔ ideal.span {X})
(λ hf, h0 $
begin
have := @ideal.mem_map_of_mem _ _ _ _ (ring_hom.of (eval 0)) _ _ hf,
rwa [pow_two, ideal.map_mul, ideal.map_sup, ideal.map_map, eq_id, ideal.map_id,
map_eq_bot_iff_le_ker.1 h_ker, sup_bot_eq, ring_hom.coe_of,
← coeff_zero_eq_eval_zero, ← pow_two] at this
end)
begin
assume x hx,
end
_
example {R : Type*} [comm_ring R] {P Q : ideal R} (hP : P.is_prime) (hQ : Q.is_prime)
(hPQ : (P ⊔ Q).is_prime) {a : R} (ha : a ∈ P ⊔ Q^2) (hab : a ∉ P ^ 2 ⊔ Q) (haP : a ∉ P)
(hu : ∀ u ∉ Q, u ∣ a → is_unit u) : irreducible a :=
⟨sorry, λ x y hxy,
have hxQyQ : x ∈ P ⊔ Q → y ∈ P ⊔ Q → false,
from λ hxQ hyQ, hab begin
have haPQ: a ∈ ((P ⊔ Q) * (P ⊔ Q)),
from hxy.symm ▸ (ideal.mul_mem_mul hxQ hyQ),
have : ((P ⊔ Q) * (P ⊔ Q)) ≤ P ^ 2 ⊔ Q,
{ rw [ideal.mul_sup, ideal.sup_mul, ideal.sup_mul, sup_assoc,
← @sup_assoc _ _ (Q * P), mul_comm Q P, sup_idem, ← ideal.sup_mul, pow_two],
exact sup_le_sup (le_refl _) ideal.mul_le_left },
exact this haPQ
end,
begin
subst a,
end⟩
end comm_ring
variables {R : Type*} [integral_domain R]
open polynomial ideal.quotient
@[simp] lemma nat.with_bot.coe_nonneg {n : ℕ} : 0 ≤ (n : with_bot ℕ) :=
by rw [← with_bot.coe_zero, with_bot.coe_le_coe]; exact nat.zero_le _
@[simp] lemma nat.with_bot.lt_zero (n : with_bot ℕ) : n < 0 ↔ n = ⊥ :=
option.cases_on n dec_trivial (λ n, iff_of_false
(by simp [with_bot.some_eq_coe]) (λ h, option.no_confusion h))
example (n : with_bot ℕ) : n.lt_zero
lemma degree_nonneg_iff_ne_zero {R : Type*} [comm_semiring R]
{f : polynomial R} : 0 ≤ degree f ↔ f ≠ 0 :=
⟨λ h0f hf0, absurd h0f (by rw [hf0, degree_zero]; exact dec_trivial),
λ hf0, le_of_not_gt (λ h, by simp [gt, degree_eq_bot, *] at *)⟩
lemma nat_degree_eq_zero_iff_degree_le_zero {R : Type*} [comm_semiring R]
{p : polynomial R} : p.nat_degree = 0 ↔ p.degree ≤ 0 :=
if hp0 : p = 0 then by simp [hp0]
else by rw [degree_eq_nat_degree hp0, ← with_bot.coe_zero, with_bot.coe_le_coe,
nat.le_zero_iff]
lemma eq_one_of_is_unit_of_monic {R : Type*} [comm_semiring R]
{p : polynomial R} (hm : monic p) (hpu : is_unit p) : p = 1 :=
have degree p ≤ 0,
from calc degree p ≤ degree (1 : polynomial R) :
let ⟨u, hu⟩ := is_unit_iff_dvd_one.1 hpu in
if hu0 : u = 0
then begin
rw [hu0, mul_zero] at hu,
rw [← mul_one p, hu, mul_zero],
simp
end
else have p.leading_coeff * u.leading_coeff ≠ 0,
by rw [hm.leading_coeff, one_mul, ne.def, leading_coeff_eq_zero];
exact hu0,
by rw [hu, degree_mul_eq' this];
exact le_add_of_nonneg_right' (degree_nonneg_iff_ne_zero.2 hu0)
... ≤ 0 : degree_one_le,
by rw [eq_C_of_degree_le_zero this, ← nat_degree_eq_zero_iff_degree_le_zero.2 this,
← leading_coeff, hm.leading_coeff, C_1]
open finset
lemma dvd_mul_prime {x a p : R} (hp : prime p) : x ∣ a * p → x ∣ a ∨ p ∣ x :=
λ ⟨y, hy⟩, (hp.div_or_div ⟨a, hy.symm.trans (mul_comm _ _)⟩).elim
or.inr
begin
rintros ⟨b, rfl⟩,
rw [mul_left_comm, mul_comm, domain.mul_right_inj hp.ne_zero] at hy,
rw [hy],
exact or.inl (dvd_mul_right _ _)
end
lemma dvd_mul_prime_prod {α : Type*} {x a : R} {s : finset α}
{p : α → R} (hp : ∀ i ∈ s, prime (p i)) (hx : x ∣ a * s.prod p) :
∃ t b, t ⊆ s ∧ b ∣ a ∧ x = b * t.prod p :=
begin
classical,
rcases hx with ⟨y, hy⟩,
induction s using finset.induction with i s his ih generalizing x y a,
{ exact ⟨∅, x, finset.subset.refl _, ⟨y, hy ▸ by simp⟩, by simp⟩ },
{ rw [prod_insert his, ← mul_assoc] at hy,
have hpi : prime (p i), { exact hp i (mem_insert_self _ _) },
rcases ih (λ i hi, hp i (mem_insert_of_mem hi)) _ hy with ⟨t, b, hts, hb, rfl⟩,
rcases dvd_mul_prime hpi hb with hba | ⟨c, rfl⟩,
{ exact ⟨t, b, trans hts (subset_insert _ _), hba, rfl⟩ },
{ exact ⟨insert i t, c, insert_subset_insert _ hts,
by rwa [mul_comm, mul_dvd_mul_iff_right hpi.ne_zero] at hb,
by rw [prod_insert (mt (λ x, hts x) his), mul_left_comm, mul_assoc]⟩, } }
end
lemma mul_eq_mul_prime_prod {α : Type*} [decidable_eq α] {x y a : R} {s : finset α}
{p : α → R} (hp : ∀ i ∈ s, prime (p i)) (hx : x * y = a * s.prod p) :
∃ t u b c, t ∪ u = s ∧ disjoint t u ∧ b * c = a ∧
x = b * t.prod p ∧ y = c * u.prod p :=
begin
induction s using finset.induction with i s his ih generalizing x y a,
{ exact ⟨∅, ∅, x, y, by simp [hx]⟩ },
{ rw [prod_insert his, ← mul_assoc] at hx,
have hpi : prime (p i), { exact hp i (mem_insert_self _ _) },
rcases ih (λ i hi, hp i (mem_insert_of_mem hi)) hx with
⟨t, u, b, c, htus, htu, hbc, rfl, rfl⟩,
have hpibc : p i ∣ b ∨ p i ∣ c,
from hpi.div_or_div ⟨a, by rw [hbc, mul_comm]⟩,
have hit : i ∉ t, from λ hit, his (htus ▸ mem_union_left _ hit),
have hiu : i ∉ u, from λ hiu, his (htus ▸ mem_union_right _ hiu),
rcases hpibc with ⟨d, rfl⟩ | ⟨d, rfl⟩,
{ rw [mul_assoc, mul_comm a, domain.mul_right_inj hpi.ne_zero] at hbc,
exact ⟨insert i t, u, d, c, by rw [insert_union, htus],
disjoint_insert_left.2 ⟨hiu, htu⟩,
by simp [← hbc, prod_insert hit, mul_assoc, mul_comm, mul_left_comm]⟩ },
{ rw [← mul_assoc, mul_right_comm b, domain.mul_left_inj hpi.ne_zero] at hbc,
exact ⟨t, insert i u, b, d, by rw [union_insert, htus],
disjoint_insert_right.2 ⟨hit, htu⟩,
by simp [← hbc, prod_insert hiu, mul_assoc, mul_comm, mul_left_comm]⟩ } }
end
lemma mul_eq_mul_prime_pow {x y a p : R} {n : ℕ} (hp : prime p) (hx : x * y = a * p ^ n) :
∃ i j b c, i + j = n ∧ b * c = a ∧ x = b * p ^ i ∧ y = c * p ^ j :=
begin
rcases mul_eq_mul_prime_prod (λ _ _, hp)
(show x * y = a * (range n).prod (λ _, p), by simpa) with
⟨t, u, b, c, htus, htu, rfl, rfl, rfl⟩,
exact ⟨t.card, u.card, b, c, by rw [← card_disjoint_union htu, htus, card_range], by simp⟩,
end
lemma eisenstein {f : polynomial R} {P : ideal R} (hP : P.is_prime)
(hfl : f.leading_coeff ∉ P)
(hfP : ∀ n : ℕ, ↑n < degree f → f.coeff n ∈ P)
(hfd0 : 0 < degree f) (h0 : f.coeff 0 ∉ P^2)
(hu : ∀ x : R, C x ∣ f → is_unit x) : irreducible f :=
have hf0 : f ≠ 0, from λ _, by simp * at *,
have hf : f.map (mk_hom P) =
C (mk_hom P (leading_coeff f)) * X ^ nat_degree f,
from polynomial.ext (λ n, begin
rcases lt_trichotomy ↑n (degree f) with h | h | h,
{ erw [coeff_map, ← mk_eq_mk_hom, eq_zero_iff_mem.2 (hfP n h),
coeff_C_mul, coeff_X_pow, if_neg, mul_zero],
rintro rfl, exact not_lt_of_ge degree_le_nat_degree h },
{ have : nat_degree f = n, from nat_degree_eq_of_degree_eq_some h.symm,
rw [coeff_C_mul, coeff_X_pow, if_pos this.symm, mul_one, leading_coeff, this, coeff_map] },
{ rw [coeff_eq_zero_of_degree_lt, coeff_eq_zero_of_degree_lt],
{ refine lt_of_le_of_lt (degree_C_mul_X_pow_le _ _) _,
rwa ← degree_eq_nat_degree hf0 },
{ exact lt_of_le_of_lt (degree_map_le _) h } }
end),
have hfd0 : 0 < f.nat_degree, from with_bot.coe_lt_coe.1
(lt_of_lt_of_le hfd0 degree_le_nat_degree),
⟨mt degree_eq_zero_of_is_unit (�� h, by simp [*, lt_irrefl] at *),
begin
rintros p q rfl,
rw [map_mul] at hf,
have : map (mk_hom P) p ∣ C (mk_hom P (p * q).leading_coeff) * X ^ (p * q).nat_degree,
from ⟨map (mk_hom P) q, hf.symm⟩,
rcases mul_eq_mul_prime_pow (show prime (X : polynomial (ideal.quotient P)),
from prime_of_degree_eq_one_of_monic degree_X monic_X) hf with
⟨m, n, b, c, hmnd, hbc, hp, hq⟩,
have hmn : 0 < m → 0 < n → false,
{ assume hm0 hn0,
have hp0 : p.eval 0 ∈ P,
{ rw [← coeff_zero_eq_eval_zero, ← eq_zero_iff_mem, mk_eq_mk_hom, ← coeff_map],
simp [hp, coeff_zero_eq_eval_zero, zero_pow hm0] },
have hq0 : q.eval 0 ∈ P,
{ rw [← coeff_zero_eq_eval_zero, ← eq_zero_iff_mem, mk_eq_mk_hom, ← coeff_map],
simp [hq, coeff_zero_eq_eval_zero, zero_pow hn0] },
apply h0,
rw [coeff_zero_eq_eval_zero, eval_mul, pow_two],
exact ideal.mul_mem_mul hp0 hq0 },
have hpql0 : (mk_hom P) (p * q).leading_coeff ≠ 0,
{ rwa [← mk_eq_mk_hom, ne.def, eq_zero_iff_mem] },
have hp0 : p ≠ 0, from λ h, by simp * at *,
have hq0 : q ≠ 0, from λ h, by simp * at *,
have hmn0 : m = 0 ∨ n = 0,
{ rwa [nat.pos_iff_ne_zero, nat.pos_iff_ne_zero, imp_false, not_not,
← or_iff_not_imp_left] at hmn },
have hbc0 : degree b = 0 ∧ degree c = 0,
{ apply_fun degree at hbc,
rwa [degree_C hpql0, degree_mul_eq, nat.with_bot.add_eq_zero_iff] at hbc },
have hmp : m ≤ nat_degree p,
from with_bot.coe_le_coe.1
(calc ↑m = degree (p.map (mk_hom P)) : by simp [hp, hbc0.1]
... ≤ degree p : degree_map_le _
... ≤ nat_degree p : degree_le_nat_degree),
have hmp : n ≤ nat_degree q,
from with_bot.coe_le_coe.1
(calc ↑n = degree (q.map (mk_hom P)) : by simp [hq, hbc0.2]
... ≤ degree q : degree_map_le _
... ≤ nat_degree q : degree_le_nat_degree),
have hpmqn : p.nat_degree = m ∧ q.nat_degree = n,
{ rw [nat_degree_mul_eq hp0 hq0] at hmnd, omega },
rcases hmn0 with rfl | rfl,
{ left,
rw [eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpmqn.1),
is_unit_C],
refine hu _ _,
rw [← eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpmqn.1)],
exact dvd_mul_right _ _ },
{ right,
rw [eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpmqn.2),
is_unit_C],
refine hu _ _,
rw [← eq_C_of_degree_le_zero (nat_degree_eq_zero_iff_degree_le_zero.1 hpmqn.2)],
exact dvd_mul_left _ _ }
end⟩
#print axioms eisenstein
example (a b c d e : ℤ) (hab : a ^ 2 = b ^ 2 + 1) : a ^3 =
def X : ℕ × ℕ → ℕ :=
λ ⟨a, b⟩, let ⟨x, y⟩ := (3,4) in
begin
exact _x.1
end
#eval 274 % 6
#eval 4 * 3 * 5 * 7 * 2 * 3
set_option class.instance_max_depth 10000
instance : fact (0 < 27720) := by norm_num
#eval 1624 % 420
example : ∀ x : zmod 2520,
x^7 + 21* x^6 + 175 * x^5 + 735 * x^4 + 1624 * x^3 + 1764 * x^2 + 720 x
dec_trivial
#eval X (5,4)
#exit
import data.polynomial
#print subtyp
example : 1= 1 := rfl
#print subtype.forall
variables {R : Type*} [comm_ring R]
open polynomial
open_locale classical
example {f : polynomial R} (n : ℕ)
(h : ∀ m, n ≤ m → polynomial.coeff f m = 0) :
degree f < n :=
if hf0 : f = 0 then by simp [hf0, with_bot.bot_lt_coe]
else lt_of_not_ge (λ hfn, mt leading_coeff_eq_zero.1 hf0 (h (nat_degree f)
(with_bot.coe_le_coe.1 (by simpa only [ge, degree_eq_nat_degree hf0] using hfn)))
#exit
import group_theory.quotient_group data.fintype.basic set_theory.cardinal
universe u
open_locale classical
theorem normal_of_index_2 {G : Type u} [group G] (S : set G) [is_subgroup S]
(HS1 : ∃ g ∉ S, ∀ x, x ∈ S ∨ g * x ∈ S)
(HS2 : bool ≃ quotient_group.quotient S)
[fintype (quotient_group.quotient S)] (HS3 : fintype.card (quotient_group.quotient S) = 2)
(HS4 : cardinal.mk (quotient_group.quotient S) = 2) : normal_subgroup S :=
let ⟨x, hxS, hx⟩ := HS1 in
have ∀ g h, g * h ∈ S → g ∈ S → h ∈ S,
from λ g h ghS gS, (hx h).resolve_right
(λ xhS, hxS $
suffices (x * h) * (g * h)⁻¹ * g ∈ S, by simpa [mul_assoc, mul_inv_rev],
is_submonoid.mul_mem
(is_submonoid.mul_mem xhS (is_subgroup.inv_mem ghS)) gS),
have ∀ g h, (g ∈ S ↔ h ∈ S) → g * h ∈ S,
from λ g h ghS, (hx g).elim
(λ gS, is_submonoid.mul_mem gS (ghS.1 gS))
(λ xgS, (hx h).elim sorry
(λ xhS, (is_subgroup.mul_mem_cancel_left S ((hx x).resolve_left hxS)).1
_)),
{ normal :=
λ n hnS g, (hx g).elim sorry
(λ h, begin
end)
λ n hnS g, (hx g).elim
(λ hgS, is_submonoid.mul_mem (is_submonoid.mul_mem hgS hnS) (is_subgroup.inv_mem hgS))
begin
assume hxgS,
have hgS : g ∉ S, from sorry,
end
}
#exit
import linear_algebra.basic tactic
universes u v
open linear_map
variables {R : Type u} [ring R] {M : Type v} [add_comm_group M] [module R M]
lemma equiv_ker_prod_range (p : M →ₗ[R] M) (hp : p.comp p = p) : M ≃ₗ[R] ker p × range p :=
have h : ∀ m, p (p m) = p m := linear_map.ext_iff.1 hp,
{ to_fun := λ m, (⟨m - p m, mem_ker.2 $ by simp [h]⟩, ⟨p m, mem_range.2 ⟨m, rfl⟩⟩),
inv_fun := λ x, x.1 + x.2,
left_inv := λ m, by simp,
right_inv := by { rintros ⟨⟨x, hx⟩, ⟨_, y, hy, rfl⟩⟩, simp [h, mem_ker.1 hx] },
add := λ _ _, by simp [subtype.coe_ext, add_comm, add_left_comm, add_assoc, sub_eq_add_neg],
smul := λ _ _, by simp [subtype.coe_ext, smul_sub] }
#exit
import tactic
set_option profiler true
example : 0 < 1 := by norm_num
example : 0 < 1 := zero_lt_one
#exit
import data.nat.prime data.nat.parity data.real.pi
example (x : ℕ) (h : (λ y : ℕ, 0 < y) x) : 0 < x := h
open nat --real
example : real.pi < 47 := by pi_upper_bound [1.9]
theorem goldbach_disproof : ¬ goldbach :=
begin
assume h,
rcases h 0 (by norm_num) with ⟨p, q, hp, hq, h⟩,
simp at h,
exact absurd hp (h.1.symm ▸ by norm_num)
end
variables {R : Type*} [comm_ring R]
lemma pow_dvd_pow_of_dvd {a b : R} (h : a ∣ b) (n : ℕ) : a ^ n ∣ b ^ n :=
let ⟨d, hd⟩ := h in ⟨d ^ n, hd.symm ▸ mul_pow _ _ _⟩
@[simp] lemma nat.cast_dvd {a b : ℕ} : a ∣ b → (a : R) ∣ (b : R) :=
λ ⟨n, hn⟩, ⟨n, by simp [hn]⟩
lemma dvd_sub_add_pow (p : ℕ) [hp : fact p.prime] (a b : R) :
(p : R) ∣ (a + b)^p - (a ^ p + b ^ p) :=
begin
rw [add_pow],
conv in (finset.range (p + 1)) { rw ← nat.sub_add_cancel hp.pos },
rw [finset.sum_range_succ, finset.sum_range_succ',
nat.sub_add_cancel hp.pos],
suffices : ↑p ∣ (finset.range (p - 1)).sum (λ i, a ^ (i + 1) * b ^
(p - (i + 1)) * nat.choose p (i + 1)),
{ simpa },
refine finset.dvd_sum _,
intros,
refine dvd_mul_of_dvd_right (nat.cast_dvd
(nat.prime.dvd_choose (nat.succ_pos _) (by simp at *; omega) hp)) _
end
lemma dvd_add_pow_iff (p : ℕ) [fact p.prime] (a b : R) :
(p : R) ∣ (a + b)^p ↔ (p : R) ∣ (a ^ p + b ^ p) :=
(dvd_add_iff_left ((dvd_neg _ _).2 (dvd_sub_add_pow p a b))).trans (by simp)
lemma dvd_sub_pow_of_dvd_sub
: ∀ (k : ℕ) (p : ℕ) (a b : R) (c d : ℕ)
(h : (p : R) ∣ c * a - d * b),
(p^(k+1) : R) ∣ c * a^(p^k) - d * b^(p^k)
| 0 p a b c d := by simp
| (k+1) p a b c d := λ h, begin
have :=
(dvd_sub_pow_of_dvd_sub k p a b c d h),
simp at this,
rw [dvd_add_pow_iff] at this,
have := dvd_sub_pow_of_dvd_sub _ _ _ _ this,
simp at this,
end
lemma dvd_sub_pow_of_dvd_sub (R : Type*) [comm_ring R]
: ∀ (k : ℕ) (p : ℕ) (a b : R) (h : (p : R) ∣ a - b),
(p^(k+1) : R) ∣ a^(p^k) - b^(p^k)
| 0 := by simp
| (k+1) := λ p a b h, begin
have := dvd_sub_pow_of_dvd_sub k p a b h,
rw [← nat.cast_pow] at this,
have := dvd_sub_pow_of_dvd_sub _ _ _ _ this,
simp at this,
end
import data.analysis.topology tactic
example {α : Type*} [ring α] {f₁ f₂ g₁ g₂ df₁ df₂ dg₁ dg₂ Pf Pg: α}
(hf : f₁ - f₂ = df₁* Pf + Pf * df₂)
(hg : g₁ - g₂ = dg₁* Pg + Pg * dg₂) :
g₁ * f₁ - g₂ * f₂ = 0 :=
begin
rw [sub_eq_iff_eq_add] at hf hg,
substs f₁ g₁,
simp only [mul_add, add_mul, mul_assoc, neg_mul_eq_mul_neg, ← mul_neg_eq_neg_mul_symm],
simp only [add_left_comm, sub_eq_add_neg, add_assoc],
abel,
end
#exit
import data.nat.prime tactic
lemma ahj (x : ℤ): (x + 1)^2 = x^2 + 2 * x + 1 :=
by ring
#print ahj
theorem subrel_acc {α : Type*} {r s : α → α → Prop} {x : α}
(hrs : ∀ x y, s x y → r x y) (h : acc r x) : acc s x :=
acc.rec_on h (λ x hx ih, acc.intro x (λ y hsyx, ih y (hrs y x hsyx)))
theorem acc_of_lt {α : Type*} {r : α → α → Prop} {x y : α} (hr : r y x)
(h : acc r x) : acc r y :=
by cases h; tauto
#print nat.factors
import algebra.associated
def SUBMISSION := Π {R : Type*} [comm_ring R] {u r : R} {n : ℕ},
by exactI Π (hr : r ^ n = 0) (hu : is_unit u), is_unit (u + r)
notation `SUBMISSION` := SUBMISSION
theorem unit_add_nilpotent {R : Type*} [comm_ring R] {u r : R} {n : ℕ} (hr : r ^ n = 0)
(hu : is_unit u) : is_unit (u + r) := sorry
theorem submission : SUBMISSION := @unit_add_nilpotent
#print axioms unit_add_nilpotent
#exit
import data.polynomial
variables {R : Type*} {S : Type*}
open function polynomial
example [integral_domain R] [integral_domain S] (i : R →+* S) (hf : injective i)
{f : polynomial R} (hf0 : 0 < degree f) (hfm : f.monic) :
(irreducible (f.map i) ∧ ∀ x : R, C x ∣ f → is_unit (C x)) ↔
irreducible f :=
begin
split,
{ rintros ⟨hifm, hfC⟩,
split,
{ exact mt degree_eq_zero_of_is_unit (λ h, absurd hf0 (h ▸ lt_irrefl _)) },
{ rintros g h rfl,
cases hifm.2 (g.map i) (h.map i) (map_mul _) with hug huh,
{ have := degree_eq_zero_of_is_unit hug,
rw [degree_map_eq_of_injective hf] at this,
rw [eq_C_of_degree_eq_zero this],
refine or.inl (hfC _ _),
rw ← eq_C_of_degree_eq_zero this,
exact dvd_mul_right _ _ },
{ have := degree_eq_zero_of_is_unit huh,
rw [degree_map_eq_of_injective hf] at this,
rw [eq_C_of_degree_eq_zero this],
refine or.inr (hfC _ _),
rw ← eq_C_of_degree_eq_zero this,
exact dvd_mul_left _ _ } } },
{ assume hif,
split,
{ split,
{ refine mt degree_eq_zero_of_is_unit (λ h, _),
rw [degree_map_eq_of_injective hf] at h,
exact absurd hf0 (h ▸ lt_irrefl _) },
{ rintros g h hm,
} } }
end
#exit
import algebra.big_operators field_theory.finite field_theory.finite_card
variables {G R : Type} [group G] [integral_domain R] [fintype G] [decidable_eq G] [decidable_eq R]
open_locale big_operators add_monoid
open finset
def to_hom_units {G M : Type*} [group G] [monoid M] (f : G →* M) : G →* units M :=
{ to_fun := λ g,
⟨f g, f (g⁻¹),
by rw [← monoid_hom.map_mul, mul_inv_self, monoid_hom.map_one],
by rw [← monoid_hom.map_mul, inv_mul_self, monoid_hom.map_one]⟩,
map_one' := units.ext (monoid_hom.map_one _),
map_mul' := λ _ _, units.ext (monoid_hom.map_mul _ _ _) }
@[simp] lemma coe_to_hom_units {G M : Type*} [group G] [monoid M] (f : G →* M) (g : G):
(to_hom_units f g : M) = f g := rfl
def preimage_equiv {H : Type} [group H] (f : G →* H) (x y : H) :
f ⁻¹' {x} ≃ f ⁻¹' {y} := sorry
lemma sum_subtype {α M : Type*} [add_comm_monoid M]
{p : α → Prop} {F : fintype (subtype p)} {s : finset α} (h : ∀ x, x ∈ s ↔ p x) {f : α → M} :
∑ a in s, f a = ∑ a : subtype p, f a :=
have (∈ s) = p, from set.ext h,
begin
rw ← sum_attach,
resetI,
subst p,
congr,
simp [finset.ext]
end
variable (G)
lemma is_cyclic.exists_monoid_generator [is_cyclic G] :
∃ x : G, ∀ y : G, y ∈ powers x := sorry
open_locale classical
lemma sum_units_subgroup (f : G →* R) (hf : f ≠ 1) : ∑ g : G, f g = 0 :=
let ⟨x, hx⟩ := is_cyclic.exists_monoid_generator (set.range (to_hom_units f)) in
-- have hx1 : x ≠ 1, from sorry,
calc ∑ g : G, f g
= ∑ g : G, to_hom_units f g : rfl
... = ∑ b : units R in univ.image (to_hom_units f),
(univ.filter (λ a, to_hom_units f a = b)).card • b :
sum_comp (coe : units R → R) (to_hom_units f)
... = ∑ b : units R in univ.image (to_hom_units f),
fintype.card (to_hom_units f ⁻¹' {b}) • b :
sum_congr rfl (λ b hb, congr_arg2 _ (fintype.card_of_finset' _ (by simp)).symm rfl)
... = ∑ b : units R in univ.image (to_hom_units f),
fintype.card (to_hom_units f ⁻¹' {x}) • b :
sum_congr rfl (λ b hb, congr_arg2 _ (fintype.card_congr (preimage_equiv _ _ _)) rfl)
... = ∑ b : set.range (to_hom_units f),
fintype.card (to_hom_units f ⁻¹' {x}) • ↑b : sum_subtype (by simp)
... = fintype.card (to_hom_units f ⁻¹' {x}) * ∑ b : set.range (to_hom_units f), (b : R) :
by simp [mul_sum, add_monoid.smul_eq_mul]
... = (fintype.card (to_hom_units f ⁻¹' {x}) : R) * 0 : (congr_arg2 _ rfl $
calc ∑ b : set.range (to_hom_units f), (b : R)
= ∑ n in range (order_of x), x ^ n :
eq.symm $ sum_bij (λ n _, x ^ n) (by simp) (by simp)
(λ m n hm hn, pow_injective_of_lt_order_of _ (by simpa using hm) (by simpa using hn))
(λ b hb, let ⟨n, hn⟩ := hx b in ⟨n % order_of x, mem_range.2 (nat.mod_lt _ (order_of_pos _)),
by rw [← pow_eq_mod_order_of, hn]⟩)
... = _ : begin end)
... = 0 : mul_zero _
#print order_of_
import tactic
#print nat.prime
example : ∀ {p : ℕ} [fact p.prime] : ∀ m, m ∣ p → m = 1 ∨ m = p := by library_search
theorem a_pow_4_sub_b_pow_4 (a b : ℕ) : a ^ 4 - b ^ 4 = (a - b) * (a + b) * (a ^ 2 + b ^ 2) :=
if h : b ≤ a
then
have b ^ 4 ≤ a ^ 4, from nat.pow_le_pow_of_le_left h _,
int.coe_nat_inj $ by simp [int.coe_nat_sub h, int.coe_nat_sub this]; ring
else
have a ^ 4 ≤ b ^ 4, from nat.pow_le_pow_of_le_left (le_of_not_ge h) _,
by rw [nat.sub_eq_zero_of_le (le_of_not_ge h), nat.sub_eq_zero_of_le this]; simp
#exit
import group_theory.sylow
theorem order_of_eq_prime {G : Type*} [group G] [fintype G] [decidable_eq G] {g : G} {p : ℕ}
(h : p.prime) (hg : g^p = 1) (hg1 : g ≠ 1) : order_of g = p :=
(h.2 _ (order_of_dvd_of_pow_eq_one hg)).resolve_left (mt order_of_eq_one_iff.1 hg1)
open_locale classical
theorem zagier (R : Type) [ring R]
[fintype (units R)] : fintype.card (units R) ≠ 5 :=
λ h5 : fintype.card (units R) = 5,
let ⟨x, hx⟩ := sylow.exists_prime_order_of_dvd_card (show nat.prime 5, by norm_num)
(show 5 ∣ fintype.card (units R), by rw h5) in
have hx5 : (x : R)^5 = 1,
by rw [← units.coe_pow, ← hx, pow_order_of_eq_one, units.coe_one],
if h2 : (2 : R) = 0
then
have ((x : R)^3 + x^2 + 1) ^ 3 = 1,
from calc ((x : R)^3 + x^2 + 1)^3
= (x : R)^5 * (x^4 + 3 * x^3 + 3 * x^2 + 4 * x + 6) + 3 * x^4
+ 3 * x^3 + 3 * x^2 + 1 :
by simp [mul_add, add_mul, pow_succ, add_comm, mul_assoc,
add_assoc, add_left_comm, bit0, bit1]
... = 2 * (2 * x^4 + 3 * x^3 + 3 * x^2 + 2 * x + 3) + 1 :
by rw hx5; simp [mul_add, add_mul, pow_succ, add_comm, mul_assoc,
add_assoc, add_left_comm, bit0, bit1]
... = 1 : by rw [h2, zero_mul, zero_add],
let y : units R := units.mk
((x : R)^3 + x^2 + 1)
(((x : R)^3 + x^2 + 1)^2)
(eq.symm (this.symm.trans $ pow_succ _ _))
(eq.symm (this.symm.trans $ pow_succ' _ _)) in
have hx1 : x ≠ 1,
from λ hx1, absurd hx (by simp [hx1]; norm_num),
have hx0 : (x : R)^2 * (x + 1) ≠ 0,
from λ h, hx1 $ units.ext $ calc
(x : R) = x - (x ^ (-2 : ℤ) : units R) * ((x ^ 2) * (x + 1)) :
by rw [h, mul_zero, sub_zero]
... = x - (x ^ (-2 : ℤ) * x ^ (2 : ℤ) : units R) * (x + 1) :
by rw [units.coe_mul, mul_assoc]; refl
... = (x : R) - (x + 1) : by simp
... = 1 - 2 : by simp [mul_add, add_mul, pow_succ, add_comm, mul_assoc,
add_assoc, add_left_comm, bit0, bit1]; abel
... = 1 : by rw [h2, sub_zero],
have hy1 : y ≠ 1, from mt (congr_arg (coe : units R → R)) $
calc (y : R) = ((x : R)^3 + x^2 + 1) : rfl
... = (x^2 * (x + 1)) + 1 : by simp [mul_add, add_mul, pow_succ, add_comm, mul_assoc,
add_assoc, add_left_comm, bit0, bit1]
... ≠ 0 + (1 : units R) : mt add_right_cancel hx0
... = 1 : by simp,
have hy3 : order_of y = 3, from order_of_eq_prime (by norm_num) (units.ext this) hy1,
absurd (show 3 ∣ 5, by rw [← h5, ← hy3]; exact order_of_dvd_card_univ) (by norm_num)
else
have hn1 : (-1 : R) ≠ 1,
from λ h, h2 $
calc (2 : R) = 1 + 1 : by norm_num
... = 1 + 1 : by norm_num
... = 1 + -1 : by rw h
... = 0 : by norm_num,
have hn1 : order_of (-1 : units R) = 2,
from order_of_eq_prime (by norm_num)
(units.ext $ by norm_num)
(mt (congr_arg (coe : units R → R)) (by convert hn1)),
absurd (show 2 ∣ 5, by rw [← h5, ← hn1]; exact order_of_dvd_card_univ) (by norm_num)
#exit
import algebra.big_operators
variables {α : Type*} {β : Type*}
def list.coproduct (s : list α) (t : list β) : list (α ⊕ β) :=
s.map sum.inl ++ t.map sum.inr
lemma list.nodup_coproduct {s : list α} {t : list β} :
(s.coproduct t).nodup ↔ s.nodup ∧ t.nodup :=
by simp [list.coproduct, list.nodup_append,
list.nodup_map_iff (@sum.inl.inj _ _),
list.nodup_map_iff (@sum.inr.inj _ _),
list.disjoint]
lemma list.sum_coproduct {γ : Type*} [add_monoid γ] {s : list α} {t : list β} (f : α ⊕ β → γ) :
((s.coproduct t).map f).sum = (s.map (λ a, f (sum.inl a))).sum + (t.map (λ b, f (sum.inr b))).sum :=
by simp [list.coproduct]
def multiset.coproduct (s : multiset α) (t : multiset β) : multiset (α ⊕ β) :=
s.map sum.inl + t.map sum.inr
lemma multiset.nodup_coproduct {s : multiset α} {t : multiset β} :
(s.coproduct t).nodup ↔ s.nodup ∧ t.nodup :=
quotient.induction_on₂ s t (λ _ _, list.nodup_coproduct)
lemma multiset.sum_coproduct {γ : Type*} [add_comm_monoid γ] {s : multiset α} {t : multiset β}
(f : α ⊕ β → γ) :
((s.coproduct t).map f).sum =
(s.map (λ a, f (sum.inl a))).sum + (t.map (λ b, f (sum.inr b))).sum :=
by simp [multiset.coproduct]
def finset.coproduct (s : finset α) (t : finset β) : finset (α ⊕ β) :=
⟨multiset.coproduct s.1 t.1, multiset.nodup_coproduct.2 ⟨s.2, t.2⟩⟩
open_locale big_operators
lemma finset.sum_coproduct {γ : Type*} [add_comm_monoid γ]
{s : finset α} {t : finset β} (f : α ⊕ β → γ) :
∑ x in s.coproduct t, f x = ∑ a in s, f (sum.inl a) + ∑ b in t, f (sum.inr b) :=
multiset.sum_coproduct _
#exit
import data.quot data.setoid data.fintype.basic
instance decidable_is_empty' (α : Type*) [decidable_eq α] [fintype α]
(S : set α) [decidable_pred S] : decidable (S = ∅) :=
decidable_of_iff (∀ x : α, x ∉ S) (by simp [set.ext_iff])
meta def quotient_choice {α β : Type} {s : setoid β}
(f : α → quotient s) : quotient (@pi_setoid _ _ (λ a : α, s)) :=
quotient.mk (λ a : α, quot.unquot (f a))
example : false :=
let x : Π (quotient_choice : Π {α β : Type} [s : setoid β]
(f : α → quotient s), quotient (@pi_setoid _ _ (λ a : α, s))),
decidable false := λ quotient_choice,
-- ⊤ is the always true relation
by letI : setoid bool := ⊤; exact
quot.rec_on_subsingleton (@quotient_choice (@quotient bool ⊤) bool ⊤ id)
(λ f, decidable_of_iff (f ⟦ff⟧ ≠ f ⟦tt⟧)
(iff_false_intro (not_not_intro (congr_arg f (quotient.sound trivial))))) in
@of_as_true _ (x @quotient_choice) begin
change x (@quotient_choice) with is_true _,
end
#exit
axiom callcc (α β : Prop) : ((α → β) → α) → α
example {p : Prop} : p ∨ ¬ p :=
callcc _ false (λ h, or.inr (h ∘ or.inl))
#exit
import tactic
#simp only [int.coe_nat_succ]
variables {α : Type*}
def le' (τ σ : α → α → Prop) := ∀ a b : α, τ a b → σ a b
notation τ ` ⊆ ` σ := le' τ σ
/- We now define the composition of two binary relations τ and σ
(denoted τ ∘ σ) as : for all a b, (τ ∘ σ) a b if and only if there
exists c, such that τ a c ∧ σ c b -/
def comp (τ σ : α → α → Prop) :=
λ a b : α, ∃ c : α, τ a c ∧ σ c b
notation τ ∘ σ := comp τ σ
/- Prove that ⊆ is both reflexive and transitive -/
theorem le'_refl : @reflexive (α → α → Prop) le' :=
by dunfold reflexive; tauto
theorem le'_trans : @transitive (α → α → Prop) le' :=
by dunfold transitive; tauto
/- Prove that if two binary relations are reflexive, then so are their
compositions-/
theorem comp_refl {τ σ : α → α → Prop}
(h₀ : reflexive τ) (h₁ : reflexive σ) :
reflexive (τ ∘ σ) :=
by dunfold comp reflexive; tauto
/- Prove that composition is associative -/
theorem comp_assoc : @associative (α → α → Prop) comp :=
by simp [function.funext_iff, comp, associative]; tauto
/- Prove that a binary relation τ is transitive if and only if
(τ ∘ τ) ⊆ τ -/
theorem trans_iff_comp_le' {τ : α → α → Prop} :
transitive τ ↔ (τ ∘ τ) ⊆ τ :=
⟨by dunfold transitive comp le'; tauto,
λ h x y z hxy hyz, h _ _ ⟨y, hxy, hyz⟩⟩
theorem sum_xx14n1 : ∀ n : ℕ,
6 * (range (n + 1)).sum (λ n : ℕ, n * (2 * n - 1)) = n * (n + 1) * (4 * n - 1)
| 0 := rfl
| 1 := rfl
| (n+2) :=
have h1 : 0 < 4 * (n + 2),
from mul_pos (by norm_num) (nat.succ_pos _),
have h2 : 0 < 2 * (n + 2),
from mul_pos (by norm_num) (nat.succ_pos _),
have h3 : 0 < 4 * (n + 1),
from mul_pos (by norm_num) (nat.succ_pos _),
begin
rw [sum_range_succ, mul_add, sum_xx14n1],
refine int.coe_nat_inj _,
push_cast,
rw [int.coe_nat_sub h1, int.coe_nat_sub h2, int.coe_nat_sub h3],
push_cast,
ring
end
#exit
import data.zmod.basic
example : 1 = 1 := rfl
#eval let n := 14 in ((finset.range n).filter $ λ r, 3 ∣ r ∨ 5 ∣ r).sum (λ n, n)
def solution' : fin 15 → ℕ
| ⟨0, _⟩ := 0
| ⟨1, _⟩ := 0
| ⟨2, _⟩ := 0
| ⟨3, _⟩ := 0
| ⟨4, _⟩ := 3
| ⟨5, _⟩ := 3
| ⟨6, _⟩ := 8
| ⟨7, _⟩ := 14
| ⟨8, _⟩ := 14
| ⟨9, _⟩ := 14
| ⟨10, _⟩ := 23
| ⟨11, _⟩ := 33
| ⟨12, _⟩ := 33
| ⟨13, _⟩ := 45
| ⟨14, _⟩ := 45
| ⟨n + 15, h⟩ := absurd h dec_trivial
theorem solution_valid (n : ℕ) : solution n =
((finset.range n).filter $ λ r, 3 ∣ r ∨ 5 ∣ r).sum (λ n, n) := rfl
#exit
#eval (∀ a b : zmod 74, a^2 - 37 * b^2 ≠ 3 : bool)
theorem no_solns : ¬ ∃ (a b : ℤ), a^2 - 37 * b^2 = 3 :=
lemma p11 : nat.prime 11 := by norm_num
lemma p71 : nat.prime 71 := by norm_num
open_locale classical
def totient' (n : ℕ) : ℕ := ((nat.factors n).map (λ n, n-1)).prod
def ptotient (n : ℕ+) : ℕ+ := ⟨totient' n, sorry⟩
def mult_5 (n : ℕ+) : ℕ := (multiplicity 5 n.1).get sorry
def compute_mod_p (p : ℕ+) :=
--if p = 101 then 0 else
let p₁ := p - 1 in
let m₁ := mult_5 p₁ in
let m₁5 := 5^m₁ in
let p₁m₁ : ℕ+ := ⟨p₁ / m₁5, sorry⟩ in
let p₂ := ptotient p₁m₁ in
let i := (5 : zmod p) ^
((5^(6 - 1 - m₁ : zmod p₂).1 : zmod p₁m₁).1 * m₁5) in
(i^4 + i^3 + i^2 + i + 1).1
#eval let x := 5^5^3 in let a := x^4 + x^3 + x^2 + x + 1 in a
#eval 5^5^3
#eval (62500 : ℚ) / 5^6
#eval ptotient 1122853751
#eval mult_5 1122853750
#eval let x := 2^3^4 in let a := x^2 + x + 1 in
(multiplicity 3 (nat.min_fac a - 1)).get sorry
-- #eval nat.find (show ∃ n, let p : ℕ+ := ⟨10 * n + 1, nat.succ_pos _⟩ in nat.prime p ∧
-- compute_mod_p p = 0, from sorry)
meta def akkgnd : ℕ+ → tactic unit :=
λ n, if nat.prime n ∧ compute_mod_p n = 0
then tactic.trace "Answer is " >> tactic.trace n.1
else akkgnd (n - 10)
--#eval (list.range 500).filter (λ n, nat.prime n ∧ n % 5 = 1)
--#eval (list.range 1).map (λ n : ℕ, (compute_mod_p ⟨10 * n + 1, sorry⟩).map fin.val fin.val)
--#eval (nat.prime 1111 : bool)
lemma pow_eq_mod_card {α : Type*} [group α] [fintype α] (a : α) (n : ℕ) :
a ^ n = a ^ (n % fintype.card α) :=
calc a ^ n = a ^ (n % order_of a) : pow_eq_mod_order_of
... = a ^ (n % fintype.card α % order_of a) :
congr_arg2 _ rfl (nat.mod_mod_of_dvd _ order_of_dvd_card_univ).symm
... = a ^ (n % fintype.card α) : eq.symm pow_eq_mod_order_of
#eval (5 ^ 105 : zmod 131)
#eval (52 ^ 5 : zmod 131)
#eval (finset.range 200).filter (λ n : ℕ, n.prime ∧ n % 5 = 1 )
#eval nat.totient 26
-- #eval 5 ^ 26 % 31
-- #eval 25 ^ 5 % 31
lemma fivefiveeqone (n : ℕ) (hn : 0 < n): (5 ^ 5 ^ n : zmod 11) = -1 :=
have (5 : zmodp 11 p11) ^ 5 = 1, from rfl,
suffices units.mk0 (5 : zmodp 11 p11) dec_trivial ^ 5 ^ n = 1,
by rw [units.ext_iff] at this; simpa,
have h1 : (5 ^ n : zmod 10) = 5,
begin
cases n with n,
{ simp [*, lt_irrefl] at * },
clear hn,
induction n,
{ refl },
{ rw [pow_succ, n_ih],
refl }
end,
have h2 : 5 ^ n % 10 = 5,
from calc 5 ^ n % 10 = 5 % 10 :
(zmod.eq_iff_modeq_nat' dec_trivial).1 (by simpa)
... = 5 : rfl,
have h3 : fintype.card (units (zmodp 11 p11)) = 10,
by rw zmodp.card_units_zmodp; refl,
by rw [pow_eq_mod_card, h3, h2]; refl
@[simp] lemma coe_unit_of_coprime {n : ℕ+} (x : ℕ) (hxn : x.coprime n) :
(zmod.unit_of_coprime x hxn : zmod n) = x := rfl
lemma pow_eq_pow_mod_totient {n : ℕ+} {x p : ℕ} (hxn : nat.coprime x n)
(t : ℕ+) (ht : nat.totient n = t) : (x : zmod n) ^ p = x ^ (p : zmod t).1 :=
suffices zmod.unit_of_coprime x hxn ^ p =
zmod.unit_of_coprime x hxn ^ (p : zmod ⟨nat.totient n, nat.totient_pos n.2⟩).1,
begin
cases t with t ht, simp at ht,subst t,
rwa [units.ext_iff, units.coe_pow, coe_unit_of_coprime, units.coe_pow, coe_unit_of_coprime] at this,
end,
begin
rw [pow_eq_mod_card],
refine congr_arg2 _ rfl _,
rw [zmod.val_cast_nat, zmod.card_units_eq_totient],
refl
end
#eval 5^70 % 71
lemma poly_div : ∀ x : ℕ, 1 < x → (x^5 - 1) / (x - 1) =
x^4 + x^3 + x^2 + x + 1 :=
λ x hx, have 1 ≤ x ^ 5, from nat.pow_pos (by linarith) _,
nat.div_eq_of_eq_mul_left
(nat.sub_pos_of_lt hx)
(by { rw [nat.mul_sub_left_distrib, mul_one],
symmetry,
apply nat.sub_eq_of_eq_add,
rw [← nat.add_sub_assoc this],
symmetry,
apply nat.sub_eq_of_eq_add,
ring })
lemma fivefivefiveeq : ∃ n : ℕ, ((5^5^5^5^5-1)/(5^5^(5^5^5-1)-1)) =
(5 ^ 5 ^ n)^4 + (5 ^ 5 ^ n)^3 + (5 ^ 5 ^ n)^2 + (5 ^ 5 ^ n) + 1 :=
have hpos : 1 < 5^5^(5^5^5-1),
from calc 1 = 1 ^ 5^(5^5^5-1) : by simp
... < 5^5^(5^5^5-1) : nat.pow_left_strict_mono
(nat.pow_pos (by norm_num) _) (by norm_num),
⟨(5^5^5-1), begin
rw [← poly_div (5 ^ 5 ^(5^5^5-1)) hpos, ← nat.pow_mul,
← nat.pow_succ, ← nat.succ_sub, nat.succ_sub_one],
exact nat.pow_pos (by norm_num) _
end⟩
theorem fivefives :
¬ nat.prime ((5^5^5^5^5-1)/(5^5^(5^5^5-1)-1)) :=
begin
cases fivefivefiveeq with n hn,
rw hn, clear hn,
end
#exit
import tactic
class incidence (point line : Type) (incident_with : point → line → Prop) :=
(I₁ : ∀ P Q, P ≠ Q → ∃! l, incident_with P l ∧ incident_with Q l)
(I₂ : ∀ l, ∃ P Q, P ≠ Q ∧ incident_with P l ∧ incident_with Q l)
(I₃ : ∃ P Q R, P ≠ Q ∧ Q ≠ R ∧ P ≠ R ∧
∀ l, ¬(incident_with P l ∧ incident_with Q l ∧ incident_with R l))
theorem thm_3p6p8 (point line : Type) (incident_with : point → line → Prop)
[incidence point line incident_with] (P Q : point) (hPQ : P ≠ Q) :
∃ R, ∀ l, ¬(incident_with P l ∧ incident_with Q l ∧ incident_with R l) :=
begin
rcases @incidence.I₃ _ _ incident_with _ with ⟨A, B, C, hAB, hBC, hAC, h⟩,
rcases @incidence.I₁ _ _ incident_with _ _ _ hPQ with ⟨l, hPQl, lunique⟩,
have : ¬ incident_with A l ∨ ¬ incident_with B l ∨ ¬ incident_with C l,
{ finish using h l },
rcases this with hA | hB | hC,
{ use A, finish },
{ use B, finish },
{ use C, finish }
end
#print thm_3p6p8
#exit
import category_theory.limits.shapes.pullbacks
open category_theory
open category_theory.limits
universes v u
variables {C : Type u} [𝒞 : category.{v} C]
include 𝒞
#print cancel_epi
#print walking_span
def pushout_of_epi {X Y : C} (f : X ⟶ Y) [epi f] :
is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f) :=
pushout_cocone.is_colimit.mk _
pushout_cocone.inl
(by intro; erw [category.id_comp])
(begin
intro s,
rw cocone.w s,
rw ← cancel_epi f,
obviously,
end)
(begin simp, end)
theorem epi_of_pushout {X Y : C} (f : X ⟶ Y)
(is_colim : is_colimit (pushout_cocone.mk (𝟙 Y) (𝟙 Y) rfl : pushout_cocone f f)) : epi f := sorry
#exit
import group_theory.sylow
open finset mul_action
open_locale classical
#print equiv_of_unique_of_unique
theorem has_fixed_point {G : Type} [group G] [fintype G] (hG65 : fintype.card G = 65)
{M : Type} [fintype M] (hM27 : fintype.card M = 27) [mul_action G M] :
∃ m : M, ∀ g : G, g • m = m :=
have horbit : ∀ m : M, fintype.card (orbit G m) ∣ 65,
begin
assume m,
rw [fintype.card_congr (orbit_equiv_quotient_stabilizer G m), ← hG65],
exact card_quotient_dvd_card _,
end,
have hdvd65 : ∀ n, n ∣ 65 ↔ n ∈ ({1, 5, 13, 65} : finset ℕ) :=
λ n, ⟨λ h, have n ≤ 65 := nat.le_of_dvd (by norm_num) h,
by revert h; revert n; exact dec_trivial,
by revert n; exact dec_trivial⟩,
begin
letI := orbit_rel G M,
have horbit_card : ∀ m : quotient (orbit_rel G M),
fintype.card {x // ⟦x⟧ = m} ∣ 65,
{ assume m,
refine quotient.induction_on m (λ m, _),
convert horbit m,
exact set.ext (λ _, quotient.eq) },
have := fintype.card_congr
(equiv.sigma_preimage_equiv (quotient.mk : M → quotient (orbit_rel G M))),
rw [fintype.card_sigma] at this,
end,
-- begin
-- rcases @incidence.I₃ _ _ incident_with _ with ⟨A, B, C, hAB, hBC, hAC, hABC⟩,
-- have : P ≠ B ∧ P ≠ C ∨ P ≠ A ∧ P ≠ C ∨ P ≠ A ∧ P ≠ B, { finish },
-- wlog hP : P ≠ B ∧ P ≠ C := this using [B C, A C, A B],
-- { }
-- end
end
|
b4b25549cc7cfadb1a77d5f1ae9065faf08442a8 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/linear_algebra/clifford_algebra/equivs.lean | a7a3bbbcb4dc314f7bde4f7fbeb36d6df237d484 | [
"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,996 | 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.dual_number
import algebra.quaternion_basis
import data.complex.module
import linear_algebra.clifford_algebra.conjugation
import linear_algebra.clifford_algebra.star
import linear_algebra.quadratic_form.prod
/-!
# Other constructions isomorphic to Clifford Algebras
This file contains isomorphisms showing that other types are equivalent to some `clifford_algebra`.
## Rings
* `clifford_algebra_ring.equiv`: any ring is equivalent to a `clifford_algebra` over a
zero-dimensional vector space.
## Complex numbers
* `clifford_algebra_complex.equiv`: the `complex` numbers are equivalent as an `ℝ`-algebra to a
`clifford_algebra` over a one-dimensional vector space with a quadratic form that satisfies
`Q (ι Q 1) = -1`.
* `clifford_algebra_complex.to_complex`: the forward direction of this equiv
* `clifford_algebra_complex.of_complex`: the reverse direction of this equiv
We show additionally that this equivalence sends `complex.conj` to `clifford_algebra.involute` and
vice-versa:
* `clifford_algebra_complex.to_complex_involute`
* `clifford_algebra_complex.of_complex_conj`
Note that in this algebra `clifford_algebra.reverse` is the identity and so the clifford conjugate
is the same as `clifford_algebra.involute`.
## Quaternion algebras
* `clifford_algebra_quaternion.equiv`: a `quaternion_algebra` over `R` is equivalent as an
`R`-algebra to a clifford algebra over `R × R`, sending `i` to `(0, 1)` and `j` to `(1, 0)`.
* `clifford_algebra_quaternion.to_quaternion`: the forward direction of this equiv
* `clifford_algebra_quaternion.of_quaternion`: the reverse direction of this equiv
We show additionally that this equivalence sends `quaternion_algebra.conj` to the clifford conjugate
and vice-versa:
* `clifford_algebra_quaternion.to_quaternion_star`
* `clifford_algebra_quaternion.of_quaternion_conj`
## Dual numbers
* `clifford_algebra_dual_number.equiv`: `R[ε]` is is equivalent as an `R`-algebra to a clifford
algebra over `R` where `Q = 0`.
-/
open clifford_algebra
/-! ### The clifford algebra isomorphic to a ring -/
namespace clifford_algebra_ring
open_locale complex_conjugate
variables {R : Type*} [comm_ring R]
@[simp]
lemma ι_eq_zero : ι (0 : quadratic_form R unit) = 0 :=
subsingleton.elim _ _
/-- Since the vector space is empty the ring is commutative. -/
instance : comm_ring (clifford_algebra (0 : quadratic_form R unit)) :=
{ mul_comm := λ x y, begin
induction x using clifford_algebra.induction,
case h_grade0 : r { apply algebra.commutes, },
case h_grade1 : x { simp, },
case h_add : x₁ x₂ hx₁ hx₂ { rw [mul_add, add_mul, hx₁, hx₂], },
case h_mul : x₁ x₂ hx₁ hx₂ { rw [mul_assoc, hx₂, ←mul_assoc, hx₁, ←mul_assoc], },
end,
..clifford_algebra.ring _ }
lemma reverse_apply (x : clifford_algebra (0 : quadratic_form R unit)) : x.reverse = x :=
begin
induction x using clifford_algebra.induction,
case h_grade0 : r { exact reverse.commutes _},
case h_grade1 : x { rw [ι_eq_zero, linear_map.zero_apply, reverse.map_zero] },
case h_mul : x₁ x₂ hx₁ hx₂ { rw [reverse.map_mul, mul_comm, hx₁, hx₂] },
case h_add : x₁ x₂ hx₁ hx₂ { rw [reverse.map_add, hx₁, hx₂] },
end
@[simp] lemma reverse_eq_id :
(reverse : clifford_algebra (0 : quadratic_form R unit) →ₗ[R] _) = linear_map.id :=
linear_map.ext reverse_apply
@[simp] lemma involute_eq_id :
(involute : clifford_algebra (0 : quadratic_form R unit) →ₐ[R] _) = alg_hom.id R _ :=
by { ext, simp }
/-- The clifford algebra over a 0-dimensional vector space is isomorphic to its scalars. -/
protected def equiv : clifford_algebra (0 : quadratic_form R unit) ≃ₐ[R] R :=
alg_equiv.of_alg_hom
(clifford_algebra.lift (0 : quadratic_form R unit) $
⟨0, λ m : unit, (zero_mul (0 : R)).trans (algebra_map R _).map_zero.symm⟩)
(algebra.of_id R _)
(by { ext x, exact (alg_hom.commutes _ x), })
(by { ext : 1, rw [ι_eq_zero, linear_map.comp_zero, linear_map.comp_zero], })
end clifford_algebra_ring
/-! ### The clifford algebra isomorphic to the complex numbers -/
namespace clifford_algebra_complex
open_locale complex_conjugate
/-- The quadratic form sending elements to the negation of their square. -/
def Q : quadratic_form ℝ ℝ := -quadratic_form.sq
@[simp]
lemma Q_apply (r : ℝ) : Q r = - (r * r) := rfl
/-- Intermediate result for `clifford_algebra_complex.equiv`: clifford algebras over
`clifford_algebra_complex.Q` above can be converted to `ℂ`. -/
def to_complex : clifford_algebra Q →ₐ[ℝ] ℂ :=
clifford_algebra.lift Q ⟨linear_map.to_span_singleton _ _ complex.I, λ r, begin
dsimp [linear_map.to_span_singleton, linear_map.id],
rw mul_mul_mul_comm,
simp,
end⟩
@[simp]
lemma to_complex_ι (r : ℝ) : to_complex (ι Q r) = r • complex.I :=
clifford_algebra.lift_ι_apply _ _ r
/-- `clifford_algebra.involute` is analogous to `complex.conj`. -/
@[simp] lemma to_complex_involute (c : clifford_algebra Q) :
to_complex (c.involute) = conj (to_complex c) :=
begin
have : to_complex (involute (ι Q 1)) = conj (to_complex (ι Q 1)),
{ simp only [involute_ι, to_complex_ι, alg_hom.map_neg, one_smul, complex.conj_I] },
suffices : to_complex.comp involute = complex.conj_ae.to_alg_hom.comp to_complex,
{ exact alg_hom.congr_fun this c },
ext : 2,
exact this
end
/-- Intermediate result for `clifford_algebra_complex.equiv`: `ℂ` can be converted to
`clifford_algebra_complex.Q` above can be converted to. -/
def of_complex : ℂ →ₐ[ℝ] clifford_algebra Q :=
complex.lift ⟨
clifford_algebra.ι Q 1,
by rw [clifford_algebra.ι_sq_scalar, Q_apply, one_mul, ring_hom.map_neg, ring_hom.map_one]⟩
@[simp]
lemma of_complex_I : of_complex complex.I = ι Q 1 :=
complex.lift_aux_apply_I _ _
@[simp] lemma to_complex_comp_of_complex : to_complex.comp of_complex = alg_hom.id ℝ ℂ :=
begin
ext1,
dsimp only [alg_hom.comp_apply, subtype.coe_mk, alg_hom.id_apply],
rw [of_complex_I, to_complex_ι, one_smul],
end
@[simp] lemma to_complex_of_complex (c : ℂ) : to_complex (of_complex c) = c :=
alg_hom.congr_fun to_complex_comp_of_complex c
@[simp] lemma of_complex_comp_to_complex :
of_complex.comp to_complex = alg_hom.id ℝ (clifford_algebra Q) :=
begin
ext,
dsimp only [linear_map.comp_apply, subtype.coe_mk, alg_hom.id_apply,
alg_hom.to_linear_map_apply, alg_hom.comp_apply],
rw [to_complex_ι, one_smul, of_complex_I],
end
@[simp] lemma of_complex_to_complex (c : clifford_algebra Q) : of_complex (to_complex c) = c :=
alg_hom.congr_fun of_complex_comp_to_complex c
/-- The clifford algebras over `clifford_algebra_complex.Q` is isomorphic as an `ℝ`-algebra to
`ℂ`. -/
@[simps]
protected def equiv : clifford_algebra Q ≃ₐ[ℝ] ℂ :=
alg_equiv.of_alg_hom to_complex of_complex to_complex_comp_of_complex of_complex_comp_to_complex
/-- The clifford algebra is commutative since it is isomorphic to the complex numbers.
TODO: prove this is true for all `clifford_algebra`s over a 1-dimensional vector space. -/
instance : comm_ring (clifford_algebra Q) :=
{ mul_comm := λ x y, clifford_algebra_complex.equiv.injective $
by rw [alg_equiv.map_mul, mul_comm, alg_equiv.map_mul],
.. clifford_algebra.ring _ }
/-- `reverse` is a no-op over `clifford_algebra_complex.Q`. -/
lemma reverse_apply (x : clifford_algebra Q) : x.reverse = x :=
begin
induction x using clifford_algebra.induction,
case h_grade0 : r { exact reverse.commutes _},
case h_grade1 : x { rw [reverse_ι] },
case h_mul : x₁ x₂ hx₁ hx₂ { rw [reverse.map_mul, mul_comm, hx₁, hx₂] },
case h_add : x₁ x₂ hx₁ hx₂ { rw [reverse.map_add, hx₁, hx₂] },
end
@[simp]
lemma reverse_eq_id : (reverse : clifford_algebra Q →ₗ[ℝ] _) = linear_map.id :=
linear_map.ext reverse_apply
/-- `complex.conj` is analogous to `clifford_algebra.involute`. -/
@[simp] lemma of_complex_conj (c : ℂ) : of_complex (conj c) = (of_complex c).involute :=
clifford_algebra_complex.equiv.injective $
by rw [equiv_apply, equiv_apply, to_complex_involute, to_complex_of_complex,
to_complex_of_complex]
-- this name is too short for us to want it visible after `open clifford_algebra_complex`
attribute [protected] Q
end clifford_algebra_complex
/-! ### The clifford algebra isomorphic to the quaternions -/
namespace clifford_algebra_quaternion
open_locale quaternion
open quaternion_algebra
variables {R : Type*} [comm_ring R] (c₁ c₂ : R)
/-- `Q c₁ c₂` is a quadratic form over `R × R` such that `clifford_algebra (Q c₁ c₂)` is isomorphic
as an `R`-algebra to `ℍ[R,c₁,c₂]`. -/
def Q : quadratic_form R (R × R) :=
(c₁ • quadratic_form.sq).prod (c₂ • quadratic_form.sq)
@[simp]
lemma Q_apply (v : R × R) : Q c₁ c₂ v = c₁ * (v.1 * v.1) + c₂ * (v.2 * v.2) := rfl
/-- The quaternion basis vectors within the algebra. -/
@[simps i j k]
def quaternion_basis : quaternion_algebra.basis (clifford_algebra (Q c₁ c₂)) c₁ c₂ :=
{ i := ι (Q c₁ c₂) (1, 0),
j := ι (Q c₁ c₂) (0, 1),
k := ι (Q c₁ c₂) (1, 0) * ι (Q c₁ c₂) (0, 1),
i_mul_i := begin
rw [ι_sq_scalar, Q_apply, ←algebra.algebra_map_eq_smul_one],
simp,
end,
j_mul_j := begin
rw [ι_sq_scalar, Q_apply, ←algebra.algebra_map_eq_smul_one],
simp,
end,
i_mul_j := rfl,
j_mul_i := begin
rw [eq_neg_iff_add_eq_zero, ι_mul_ι_add_swap, quadratic_form.polar],
simp,
end }
variables {c₁ c₂}
/-- Intermediate result of `clifford_algebra_quaternion.equiv`: clifford algebras over
`clifford_algebra_quaternion.Q` can be converted to `ℍ[R,c₁,c₂]`. -/
def to_quaternion : clifford_algebra (Q c₁ c₂) →ₐ[R] ℍ[R,c₁,c₂] :=
clifford_algebra.lift (Q c₁ c₂) ⟨
{ to_fun := λ v, (⟨0, v.1, v.2, 0⟩ : ℍ[R,c₁,c₂]),
map_add' := λ v₁ v₂, by simp,
map_smul' := λ r v, by ext; simp },
λ v, begin
dsimp,
ext,
all_goals {dsimp, ring},
end⟩
@[simp]
lemma to_quaternion_ι (v : R × R) :
to_quaternion (ι (Q c₁ c₂) v) = (⟨0, v.1, v.2, 0⟩ : ℍ[R,c₁,c₂]) :=
clifford_algebra.lift_ι_apply _ _ v
/-- The "clifford conjugate" maps to the quaternion conjugate. -/
lemma to_quaternion_star (c : clifford_algebra (Q c₁ c₂)) :
to_quaternion (star c) = quaternion_algebra.conj (to_quaternion c) :=
begin
simp only [clifford_algebra.star_def'],
induction c using clifford_algebra.induction,
case h_grade0 : r
{ simp only [reverse.commutes, alg_hom.commutes, quaternion_algebra.coe_algebra_map,
quaternion_algebra.conj_coe], },
case h_grade1 : x
{ rw [reverse_ι, involute_ι, to_quaternion_ι, alg_hom.map_neg, to_quaternion_ι,
quaternion_algebra.neg_mk, conj_mk, neg_zero], },
case h_mul : x₁ x₂ hx₁ hx₂
{ simp only [reverse.map_mul, alg_hom.map_mul, hx₁, hx₂, quaternion_algebra.conj_mul] },
case h_add : x₁ x₂ hx₁ hx₂
{ simp only [reverse.map_add, alg_hom.map_add, hx₁, hx₂, quaternion_algebra.conj_add] },
end
/-- Map a quaternion into the clifford algebra. -/
def of_quaternion : ℍ[R,c₁,c₂] →ₐ[R] clifford_algebra (Q c₁ c₂) :=
(quaternion_basis c₁ c₂).lift_hom
@[simp] lemma of_quaternion_mk (a₁ a₂ a₃ a₄ : R) :
of_quaternion (⟨a₁, a₂, a₃, a₄⟩ : ℍ[R,c₁,c₂])
= algebra_map R _ a₁
+ a₂ • ι (Q c₁ c₂) (1, 0)
+ a₃ • ι (Q c₁ c₂) (0, 1)
+ a₄ • (ι (Q c₁ c₂) (1, 0) * ι (Q c₁ c₂) (0, 1)) := rfl
@[simp]
lemma of_quaternion_comp_to_quaternion :
of_quaternion.comp to_quaternion = alg_hom.id R (clifford_algebra (Q c₁ c₂)) :=
begin
ext : 1,
dsimp, -- before we end up with two goals and have to do this twice
ext,
all_goals
{ dsimp,
rw to_quaternion_ι,
dsimp,
simp only [to_quaternion_ι, zero_smul, one_smul, zero_add, add_zero, ring_hom.map_zero], },
end
@[simp] lemma of_quaternion_to_quaternion (c : clifford_algebra (Q c₁ c₂)) :
of_quaternion (to_quaternion c) = c :=
alg_hom.congr_fun
(of_quaternion_comp_to_quaternion : _ = alg_hom.id R (clifford_algebra (Q c₁ c₂))) c
@[simp]
lemma to_quaternion_comp_of_quaternion :
to_quaternion.comp of_quaternion = alg_hom.id R ℍ[R,c₁,c₂] :=
begin
apply quaternion_algebra.lift.symm.injective,
ext1; dsimp [quaternion_algebra.basis.lift]; simp,
end
@[simp] lemma to_quaternion_of_quaternion (q : ℍ[R,c₁,c₂]) : to_quaternion (of_quaternion q) = q :=
alg_hom.congr_fun (to_quaternion_comp_of_quaternion : _ = alg_hom.id R ℍ[R,c₁,c₂]) q
/-- The clifford algebra over `clifford_algebra_quaternion.Q c₁ c₂` is isomorphic as an `R`-algebra
to `ℍ[R,c₁,c₂]`. -/
@[simps]
protected def equiv : clifford_algebra (Q c₁ c₂) ≃ₐ[R] ℍ[R,c₁,c₂] :=
alg_equiv.of_alg_hom to_quaternion of_quaternion
to_quaternion_comp_of_quaternion
of_quaternion_comp_to_quaternion
/-- The quaternion conjugate maps to the "clifford conjugate" (aka `star`). -/
@[simp] lemma of_quaternion_conj (q : ℍ[R,c₁,c₂]) :
of_quaternion (q.conj) = star (of_quaternion q) :=
clifford_algebra_quaternion.equiv.injective $
by rw [equiv_apply, equiv_apply, to_quaternion_star, to_quaternion_of_quaternion,
to_quaternion_of_quaternion]
-- this name is too short for us to want it visible after `open clifford_algebra_quaternion`
attribute [protected] Q
end clifford_algebra_quaternion
/-! ### The clifford algebra isomorphic to the dual numbers -/
namespace clifford_algebra_dual_number
open_locale dual_number
open dual_number triv_sq_zero_ext
variables {R M : Type*} [comm_ring R] [add_comm_group M] [module R M]
lemma ι_mul_ι (r₁ r₂) : ι (0 : quadratic_form R R) r₁ * ι (0 : quadratic_form R R) r₂ = 0 :=
by rw [←mul_one r₁, ←mul_one r₂, ←smul_eq_mul R, ←smul_eq_mul R, linear_map.map_smul,
linear_map.map_smul, smul_mul_smul, ι_sq_scalar, quadratic_form.zero_apply,
ring_hom.map_zero, smul_zero]
/-- The clifford algebra over a 1-dimensional vector space with 0 quadratic form is isomorphic to
the dual numbers. -/
protected def equiv : clifford_algebra (0 : quadratic_form R R) ≃ₐ[R] R[ε] :=
alg_equiv.of_alg_hom
(clifford_algebra.lift (0 : quadratic_form R R) ⟨inr_hom R _, λ m, inr_mul_inr _ m m⟩)
(dual_number.lift ⟨ι _ (1 : R), ι_mul_ι (1 : R) 1⟩)
(by { ext x : 1, dsimp, rw [lift_apply_eps, subtype.coe_mk, lift_ι_apply, inr_hom_apply, eps] })
(by { ext : 2, dsimp, rw [lift_ι_apply, inr_hom_apply, ←eps, lift_apply_eps, subtype.coe_mk] })
@[simp] lemma equiv_ι (r : R) : clifford_algebra_dual_number.equiv (ι _ r) = r • ε :=
(lift_ι_apply _ _ r).trans (inr_eq_smul_eps _)
@[simp] lemma equiv_symm_eps :
clifford_algebra_dual_number.equiv.symm (eps : R[ε]) = ι (0 : quadratic_form R R) 1 :=
dual_number.lift_apply_eps _
end clifford_algebra_dual_number
|
69b54b82df2cde6dcb3bdbfeb33d35d3c6e84e7b | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/ring_theory/localization/inv_submonoid.lean | d1c056efbe04e7768bf0690f42cca60ccc775414 | [
"Apache-2.0"
] | permissive | alreadydone/mathlib | dc0be621c6c8208c581f5170a8216c5ba6721927 | c982179ec21091d3e102d8a5d9f5fe06c8fafb73 | refs/heads/master | 1,685,523,275,196 | 1,670,184,141,000 | 1,670,184,141,000 | 287,574,545 | 0 | 0 | Apache-2.0 | 1,670,290,714,000 | 1,597,421,623,000 | Lean | UTF-8 | Lean | false | false | 3,967 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro, Johan Commelin, Amelia Livingston, Anne Baanen
-/
import group_theory.submonoid.inverses
import ring_theory.finite_type
import ring_theory.localization.basic
import tactic.ring_exp
/-!
# Submonoid of inverses
## Main definitions
* `is_localization.inv_submonoid M S` is the submonoid of `S = M⁻¹R` consisting of inverses of
each element `x ∈ M`
## Implementation notes
See `src/ring_theory/localization/basic.lean` for a design overview.
## Tags
localization, ring localization, commutative ring localization, characteristic predicate,
commutative ring, field of fractions
-/
variables {R : Type*} [comm_ring R] (M : submonoid R) (S : Type*) [comm_ring S]
variables [algebra R S] {P : Type*} [comm_ring P]
open function
open_locale big_operators
namespace is_localization
section inv_submonoid
variables (M S)
/-- The submonoid of `S = M⁻¹R` consisting of `{ 1 / x | x ∈ M }`. -/
def inv_submonoid : submonoid S := (M.map (algebra_map R S)).left_inv
variable [is_localization M S]
lemma submonoid_map_le_is_unit : M.map (algebra_map R S) ≤ is_unit.submonoid S :=
by { rintros _ ⟨a, ha, rfl⟩, exact is_localization.map_units S ⟨_, ha⟩ }
/-- There is an equivalence of monoids between the image of `M` and `inv_submonoid`. -/
noncomputable
abbreviation equiv_inv_submonoid : M.map (algebra_map R S) ≃* inv_submonoid M S :=
((M.map (algebra_map R S)).left_inv_equiv (submonoid_map_le_is_unit M S)).symm
/-- There is a canonical map from `M` to `inv_submonoid` sending `x` to `1 / x`. -/
noncomputable
def to_inv_submonoid : M →* inv_submonoid M S :=
(equiv_inv_submonoid M S).to_monoid_hom.comp ((algebra_map R S : R →* S).submonoid_map M)
lemma to_inv_submonoid_surjective : function.surjective (to_inv_submonoid M S) :=
function.surjective.comp (equiv.surjective _) (monoid_hom.submonoid_map_surjective _ _)
@[simp]
lemma to_inv_submonoid_mul (m : M) : (to_inv_submonoid M S m : S) * (algebra_map R S m) = 1 :=
submonoid.left_inv_equiv_symm_mul _ _ _
@[simp]
lemma mul_to_inv_submonoid (m : M) : (algebra_map R S m) * (to_inv_submonoid M S m : S) = 1 :=
submonoid.mul_left_inv_equiv_symm _ _ ⟨_, _⟩
@[simp]
lemma smul_to_inv_submonoid (m : M) : m • (to_inv_submonoid M S m : S) = 1 :=
by { convert mul_to_inv_submonoid M S m, rw ← algebra.smul_def, refl }
variables {S}
lemma surj' (z : S) : ∃ (r : R) (m : M), z = r • to_inv_submonoid M S m :=
begin
rcases is_localization.surj M z with ⟨⟨r, m⟩, e : z * _ = algebra_map R S r⟩,
refine ⟨r, m, _⟩,
rw [algebra.smul_def, ← e, mul_assoc],
simp,
end
lemma to_inv_submonoid_eq_mk' (x : M) :
(to_inv_submonoid M S x : S) = mk' S 1 x :=
by { rw ← (is_localization.map_units S x).mul_left_inj, simp }
lemma mem_inv_submonoid_iff_exists_mk' (x : S) :
x ∈ inv_submonoid M S ↔ ∃ m : M, mk' S 1 m = x :=
begin
simp_rw ← to_inv_submonoid_eq_mk',
exact ⟨λ h, ⟨_, congr_arg subtype.val (to_inv_submonoid_surjective M S ⟨x, h⟩).some_spec⟩,
λ h, h.some_spec ▸ (to_inv_submonoid M S h.some).prop⟩
end
variables (S)
lemma span_inv_submonoid : submodule.span R (inv_submonoid M S : set S) = ⊤ :=
begin
rw eq_top_iff,
rintros x -,
rcases is_localization.surj' M x with ⟨r, m, rfl⟩,
exact submodule.smul_mem _ _ (submodule.subset_span (to_inv_submonoid M S m).prop),
end
lemma finite_type_of_monoid_fg [monoid.fg M] : algebra.finite_type R S :=
begin
have := monoid.fg_of_surjective _ (to_inv_submonoid_surjective M S),
rw monoid.fg_iff_submonoid_fg at this,
rcases this with ⟨s, hs⟩,
refine ⟨⟨s, _⟩⟩,
rw eq_top_iff,
rintro x -,
change x ∈ ((algebra.adjoin R _ : subalgebra R S).to_submodule : set S),
rw [algebra.adjoin_eq_span, hs, span_inv_submonoid],
trivial
end
end inv_submonoid
end is_localization
|
ee903901b25a225e34ddd6ce0620ef76f0aed872 | ce6917c5bacabee346655160b74a307b4a5ab620 | /src/ch5/ex0412.lean | ba7fcd684d99823f386a202632e03bb7bab2a69a | [] | 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 | 537 | lean | example (p q r : Prop) : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) :=
begin
apply iff.intro,
begin
intro h,
cases h.right with hq hr,
begin
show (p ∧ q) ∨ (p ∧ r),
exact or.inl ⟨h.left, hq⟩,
end,
show (p ∧ q) ∨ (p ∧ r),
exact or.inr ⟨h.left, hr⟩,
end,
intro h,
cases h with hpq hpr,
begin
show p ∧ (q ∨ r),
exact ⟨hpq.left, or.inl hpq.right⟩,
end,
show p ∧ (q ∨ r),
exact ⟨hpr.left, or.inr hpr.right⟩
end
|
c229e945fb430de0f1938ec96b5605a96b9f6555 | 5df84495ec6c281df6d26411cc20aac5c941e745 | /src/formal_ml/nnreal.lean | 1ececdf010016576bc5c45f4386cf6a5ddd28c6e | [
"Apache-2.0"
] | permissive | eric-wieser/formal-ml | e278df5a8df78aa3947bc8376650419e1b2b0a14 | 630011d19fdd9539c8d6493a69fe70af5d193590 | refs/heads/master | 1,681,491,589,256 | 1,612,642,743,000 | 1,612,642,743,000 | 360,114,136 | 0 | 0 | Apache-2.0 | 1,618,998,189,000 | 1,618,998,188,000 | null | UTF-8 | Lean | false | false | 15,597 | lean | /-
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
import data.real.nnreal
import data.real.ennreal
import data.complex.exponential
import formal_ml.nat
import formal_ml.real
import formal_ml.rat
lemma nnreal_add_sub_right (a b c:nnreal):a + b = c → c - b = a :=
begin
intro A1,
--apply linarith [A1],
have A2:(a + b) - b = a,
{
apply nnreal.add_sub_cancel,
},
rw A1 at A2,
exact A2,
end
lemma nnreal_add_sub_left (a b c:nnreal):a + b = c → c - a = b :=
begin
intro A1,
apply nnreal_add_sub_right,
rw nnreal.comm_semiring.add_comm,
exact A1,
end
lemma nnreal_sub_le_left (a b c:nnreal):b ≤ c → (a - c ≤ a - b) :=
begin
intro A1,
apply nnreal.of_real_le_of_real,
apply sub_le_sub_left,
apply A1,
end
noncomputable def nnreal.exp (x:real):nnreal := nnreal.of_real (real.exp x)
@[simp]
lemma nnreal_exp_eq (x:real):↑(nnreal.exp x) = real.exp x :=
begin
unfold nnreal.exp,
rw nnreal.coe_of_real,
apply le_of_lt,
apply real.exp_pos,
end
@[simp]
lemma nnreal.exp_zero_eq_one:(nnreal.exp 0) = 1 :=
begin
unfold nnreal.exp,
--rw nnreal.coe_eq_coe,
have h1:(1:nnreal) = (nnreal.of_real 1),
{ simp },
rw h1,
simp,
end
-- This one is hard (and technically has nothing to do with nnreal).
-- The rest are easy after it is finished.
-- A standard proof would be to calculate the first and second derivative.
-- If you prove that the second derivative is non-negative, then there is a minimum
-- where the first derivative is zero. In this way you can establish that the minimum
-- is at zero, and it is zero.
-- Note this should solve the problem. however, there is a TODO.
-- My current thinking about the proof:
lemma nnreal_pow_succ (x:nnreal) (k:ℕ):x ^ nat.succ k = x * (x ^ k) :=
begin
refl,
end
lemma nnreal_pow_mono (x y:nnreal) (k:ℕ):x ≤ y → x^k ≤ y^k :=
begin
intro A1,
induction k,
{
simp,
},
{
rw nnreal_pow_succ,
rw nnreal_pow_succ,
apply mul_le_mul,
{
exact A1,
},
{
apply k_ih,
},
{
simp,
},
{
simp,
},
}
end
lemma nnreal_eq (x y:nnreal):(x:real) = (y:real) → x = y :=
begin
intro A1,
apply nnreal.eq,
apply A1,
end
lemma nnreal_exp_pow (x:real) (k:ℕ): nnreal.exp (k * x) = (nnreal.exp x)^k :=
begin
apply nnreal.eq,
rw nnreal.coe_pow,
rw nnreal_exp_eq,
rw nnreal_exp_eq,
apply real.exp_nat_mul,
end
lemma to_ennreal_def (x:nnreal):(x:ennreal)= some x :=
begin
refl
end
lemma nnreal_lt_ennreal_top (x:nnreal):(x:ennreal) < (⊤:ennreal) :=
begin
rw to_ennreal_def,
apply with_top.some_lt_none,
end
lemma nnreal_lt_some_ennreal (x y:nnreal):(x < y) → (x:ennreal) < (some y:ennreal) :=
begin
intro A1,
apply ennreal.coe_lt_coe.mpr,
apply A1,
end
lemma some_ennreal_lt_nnreal (x y:nnreal):(x:ennreal) < (some y:ennreal) → (x < y) :=
begin
intro A1,
apply ennreal.coe_lt_coe.mp,
apply A1,
end
lemma nnreal_add_eq_zero_iff (a b:nnreal): a + b = 0 ↔ a = 0 ∧ b = 0 :=
begin
simp,
end
--Holds true for all commutative semirings?
lemma nnreal_add_monoid_smul_def{n:ℕ} {c:nnreal}: n •ℕ c = ↑(n) * c :=
begin
induction n,
{
simp,
},
{
simp,
}
end
lemma nnreal_sub_le_sub_of_le {a b c:nnreal}:b ≤ c → a - c ≤ a - b :=
begin
intro A1,
have A2:(a ≤ b)∨ (b ≤ a) := linear_order.le_total a b,
have A3:(a ≤ c)∨ (c ≤ a) := linear_order.le_total a c,
cases A2,
{
rw nnreal.sub_eq_zero,
simp,
apply le_trans A2 A1,
},
{
cases A3,
{
rw nnreal.sub_eq_zero,
simp,
exact A3,
},
rw ← nnreal.coe_le_coe,
rw nnreal.coe_sub,
rw nnreal.coe_sub,
apply sub_le_sub_left,
rw nnreal.coe_le_coe,
exact A1,
exact A2,
exact A3,
},
end
lemma nnreal_sub_lt_sub_of_lt {a b c:nnreal}:b < c → b < a → a - c < a - b :=
begin
intros A1 A2,
--have A2:(a ≤ b)∨ (b ≤ a) := linear_order.le_total a b,
have A3:(a ≤ c)∨ (c ≤ a) := linear_order.le_total a c,
{
cases A3,
{
rw nnreal.sub_eq_zero A3,
apply nnreal.sub_pos.mpr A2,
},
rw ← nnreal.coe_lt_coe,
rw nnreal.coe_sub,
rw nnreal.coe_sub,
apply sub_lt_sub_left,
rw nnreal.coe_lt_coe,
exact A1,
apply (le_of_lt A2),
exact A3,
},
end
lemma nnreal.lt_of_add_le_of_pos {x y z:nnreal}:x + y ≤ z → 0 < y → x < z :=
begin
cases x, cases y, cases z,
repeat {rw ← nnreal.coe_lt_coe}, rw ← nnreal.coe_le_coe,repeat {rw nnreal.coe_add},
simp, intros A1 A2,
linarith [A1, A2],
end
lemma nnreal.not_add_le_of_lt {a b:nnreal}:
(0 < b) → ¬(a + b) ≤ a :=
begin
intros A1 A2,
simp at A2,
subst A2,
apply lt_irrefl _ A1,
end
lemma nnreal.sub_lt_of_pos_of_pos {a b:nnreal}:(0 < a) →
(0 < b) → (a - b) < a :=
begin
intros A1 A2,
cases (le_total a b) with A3 A3,
{
rw nnreal.sub_eq_zero A3,
apply A1,
},
{
rw ← nnreal.coe_lt_coe,
rw nnreal.coe_sub A3,
rw ← nnreal.coe_lt_coe at A2,
rw sub_lt_self_iff,
apply A2,
}
end
lemma nnreal.sub_eq_of_add_of_le {a b c:nnreal}:a = b + c →
c ≤ a → a - c = b :=
begin
intros A1 A2,
have A3:a - c + c = b + c,
{
rw nnreal.sub_add_cancel_of_le A2,
apply A1,
},
apply add_right_cancel A3,
end
lemma nnreal.inverse_le_of_le {a b:nnreal}:
0 < a →
a ≤ b →
b⁻¹ ≤ a⁻¹ :=
begin
intros A1 A2,
have B1: (a⁻¹ * b⁻¹) * a ≤ (a⁻¹ * b⁻¹) * b,
{
apply mul_le_mul,
apply le_refl _,
apply A2,
simp,
simp,
},
rw mul_comm a⁻¹ b⁻¹ at B1,
rw mul_assoc at B1,
rw inv_mul_cancel at B1,
rw mul_comm b⁻¹ a⁻¹ at B1,
rw mul_assoc at B1,
rw mul_one at B1,
rw inv_mul_cancel at B1,
rw mul_one at B1,
apply B1,
{
have B1A := lt_of_lt_of_le A1 A2,
intro B1B,
subst b,
simp at B1A,
apply B1A,
},
{
intro C1,
subst a,
simp at A1,
apply A1,
},
end
lemma nnreal.unit_frac_pos (n:ℕ):0 < (1/((n:nnreal) + 1)) :=
begin
apply nnreal.div_pos,
apply zero_lt_one,
have A1:(0:nnreal) < (0:nnreal) + (1:nnreal),
{
simp,
apply zero_lt_one,
},
apply lt_of_lt_of_le A1,
rw add_comm (0:nnreal) 1,
rw add_comm _ (1:nnreal),
apply add_le_add_left,
simp,
end
lemma nnreal.real_le_real {q r:ℝ}:q ≤ r → (nnreal.of_real q ≤ nnreal.of_real r) :=
begin
intro A1,
cases (le_total 0 q) with A2 A2,
{
have A3 := le_trans A2 A1,
rw ← @nnreal.coe_le_coe,
rw nnreal.coe_of_real q A2,
rw nnreal.coe_of_real r A3,
apply A1,
},
{
have B1 := nnreal.of_real_of_nonpos A2,
rw B1,
simp,
},
end
lemma nnreal.rat_le_rat {q r:ℚ}:q ≤ r → (nnreal.of_real q ≤ nnreal.of_real r) :=
begin
rw real.rat_le_rat_iff,
apply nnreal.real_le_real,
end
lemma nnreal.real_lt_nnreal_of_real_le_real_of_real_lt_nnreal {q r:real} {s:nnreal}:
q ≤ r → (nnreal.of_real r) < s → (nnreal.of_real q) < s :=
begin
intros A1 A2,
apply lt_of_le_of_lt _ A2,
apply nnreal.real_le_real,
apply A1,
end
lemma nnreal.rat_lt_nnreal_of_rat_le_rat_of_rat_lt_nnreal {q r:ℚ} {s:nnreal}:
q ≤ r → (nnreal.of_real r) < s → (nnreal.of_real q) < s :=
begin
intros A1 A2,
rw real.rat_le_rat_iff at A1,
apply nnreal.real_lt_nnreal_of_real_le_real_of_real_lt_nnreal A1 A2,
end
lemma nnreal.of_real_inv_eq_inv_of_real {x:real}:(0 ≤ x) →
nnreal.of_real x⁻¹ = (nnreal.of_real x)⁻¹ :=
begin
intro A1,
rw ← nnreal.eq_iff,
simp,
have A2:0 ≤ x⁻¹,
{
apply inv_nonneg.2 A1,
},
rw nnreal.coe_of_real x A1,
rw nnreal.coe_of_real _ A2,
end
/--TODO: replace with div_eq_mul_inv.-/
lemma nnreal.div_def {x y:nnreal}:x/y = x * y⁻¹ := begin
rw div_eq_mul_inv,
end
lemma nnreal.one_div_eq_inv {x:nnreal}:1/x = x⁻¹ :=
begin
rw nnreal.div_def,
rw one_mul,
end
lemma nnreal.exists_unit_frac_lt_pos {ε:nnreal}:0 < ε → (∃ n:ℕ, (1/((n:nnreal) + 1)) < ε) :=
begin
intro A1,
have A2 := A1,
rw nnreal.lt_iff_exists_rat_btwn at A2,
cases A2 with q A2,
cases A2 with A2 A3,
cases A3 with A3 A4,
simp at A3,
have A5 := rat.exists_unit_frac_le_pos A3,
cases A5 with n A5,
apply exists.intro n,
simp at A5,
rw nnreal.one_div_eq_inv,
have B2:(0:ℝ) ≤ 1,
{
apply le_of_lt,
apply zero_lt_one,
},
have A7:nnreal.of_real 1 = (1:nnreal),
{
rw ← nnreal.coe_eq,
rw nnreal.coe_of_real,
rw nnreal.coe_one,
apply B2,
},
rw ← A7,
have A8:nnreal.of_real n = n,
{
rw ← nnreal.coe_eq,
rw nnreal.coe_of_real,
simp,
simp,
},
rw ← A8,
have B1:(0:ℝ) ≤ n,
{
simp,
},
have B3:(0:ℝ) ≤ n + (1:ℝ),
{
apply le_trans B1,
simp,
apply B2
},
rw ← nnreal.of_real_add B1 B2,
rw ← nnreal.of_real_inv_eq_inv_of_real B3,
have A9 := nnreal.rat_le_rat A5,
have A10:((n:ℝ) + 1)⁻¹ = (↑((n:ℚ) + 1)⁻¹:ℝ),
{
simp,
},
rw A10,
apply lt_of_le_of_lt A9 A4,
end
lemma nnreal.of_real_eq_coe_nat {n:ℕ}:nnreal.of_real n = n :=
begin
rw ← nnreal.coe_eq,
rw nnreal.coe_of_real,
repeat {simp},
end
lemma nnreal.sub_le_sub_of_le {a b c:nnreal}:b ≤ a →
c - a ≤ c - b :=
begin
intro A1,
cases (classical.em (a ≤ c)) with B1 B1,
{
rw ← nnreal.coe_le_coe,
rw nnreal.coe_sub B1,
have B2:=le_trans A1 B1,
rw nnreal.coe_sub B2,
rw ← nnreal.coe_le_coe at A1,
linarith [A1],
},
{
simp only [not_le] at B1,
have C1:= le_of_lt B1,
rw nnreal.sub_eq_zero C1,
simp
},
end
--Replace c < b with c ≤ a.
lemma nnreal.sub_lt_sub_of_lt_of_lt {a b c:nnreal}:a < b →
c ≤ a →
a - c < b - c :=
begin
intros A1 A2,
rw ← nnreal.coe_lt_coe,
rw nnreal.coe_sub A2,
have B1:=lt_of_le_of_lt A2 A1,
have B2 := le_of_lt B1,
rw nnreal.coe_sub B2,
apply real.sub_lt_sub_of_lt,
rw nnreal.coe_lt_coe,
apply A1,
end
lemma nnreal.sub_lt_sub_of_lt_of_le {a b c d:nnreal}:a < b →
c ≤ d →
d ≤ a →
a - d < b - c :=
begin
intros A1 A2 A3,
have A3:a - d < b - d,
{
apply nnreal.sub_lt_sub_of_lt_of_lt A1 A3,
},
apply lt_of_lt_of_le A3,
apply nnreal.sub_le_sub_of_le A2,
end
lemma nnreal.eq_add_of_sub_eq {a b c:nnreal}:b ≤ a →
a - b = c → a = b + c :=
begin
intros A1 A2,
apply nnreal.eq,
rw nnreal.coe_add,
rw ← nnreal.eq_iff at A2,
rw nnreal.coe_sub A1 at A2,
apply real.eq_add_of_sub_eq A2,
end
lemma nnreal.sub_add_sub {a b c:nnreal}:c ≤ b → b ≤ a → (a - b) + (b - c) = a - c :=
begin
intros A1 A2,
rw ← nnreal.coe_eq,
rw nnreal.coe_add,
repeat {rw nnreal.coe_sub},
apply real.sub_add_sub,
apply le_trans A1 A2,
apply A1,
apply A2,
end
lemma nnreal.add_lt_of_lt_sub {a b c:nnreal}:a < b - c → a + c < b :=
begin
cases (decidable.em (c ≤ b)) with B1 B1,
{
repeat {rw ← nnreal.coe_lt_coe <|> rw nnreal.coe_add},
rw nnreal.coe_sub B1,
intro B2,
linarith,
},
{
intros A1,
rw nnreal.sub_eq_zero at A1,
exfalso,
simp at A1,
apply A1,
apply le_of_not_le B1,
},
end
lemma nnreal.lt_sub_of_add_lt {a b c:nnreal}:a + c < b → a < b - c :=
begin
intro A1,
have B1:c ≤ b,
{
have B1A := le_of_lt A1,
have B1B:a + c ≤ a + b,
{
have B1BA:b ≤ a + b,
{
rw add_comm,
apply le_add_nonnegative _ _,
},
apply le_trans B1A B1BA,
},
simp at B1B,
apply B1B,
},
revert A1,
repeat {rw ← nnreal.coe_lt_coe <|> rw nnreal.coe_add},
rw nnreal.coe_sub B1,
intros,
linarith,
end
lemma nnreal.le_sub_add {a b c:nnreal}:b ≤ c → c ≤ a →
a ≤ a - b + c :=
begin
repeat {rw ← nnreal.coe_le_coe},
repeat {rw nnreal.coe_add},
intros A1 A2,
repeat {rw nnreal.coe_sub},
linarith,
apply le_trans A1 A2,
end
lemma nnreal.le_sub_add' {a b:nnreal}:a ≤ a - b + b :=
begin
cases decidable.em (b ≤ a),
{ apply nnreal.le_sub_add,
apply le_refl _,
apply h },
{ have h2:a≤ b,
{ apply le_of_not_ge h }, rw nnreal.sub_eq_zero,
rw zero_add,
apply h2,
apply h2 },
end
lemma nnreal.lt_of_add_le_of_le_of_sub_lt {a b c d e:nnreal}:
a + b ≤ c → d ≤ b → c - e < a → d < e :=
begin
rw ← nnreal.coe_le_coe,
rw ← nnreal.coe_le_coe,
rw ← nnreal.coe_lt_coe,
rw ← nnreal.coe_lt_coe,
rw nnreal.coe_add,
cases (le_or_lt e c) with B1 B1,
{
rw nnreal.coe_sub B1,
rw ← nnreal.coe_le_coe at B1,
intros,linarith,
},
{
rw nnreal.sub_eq_zero (le_of_lt B1),
rw ← nnreal.coe_lt_coe at B1,
rw nnreal.coe_zero,
intros,linarith,
},
end
lemma nnreal.inv_as_fraction {c:nnreal}:(1)/(c) = (c)⁻¹ :=
begin
rw nnreal.div_def,
rw one_mul,
end
lemma nnreal.lt_of_add_lt_of_pos {a b c:nnreal}:
b + c ≤ a →
0 < b →
c < a :=
begin
rw ← nnreal.coe_le_coe,
rw ← nnreal.coe_lt_coe,
rw ← nnreal.coe_lt_coe,
rw nnreal.coe_add,
rw nnreal.coe_zero,
intros,
linarith,
end
lemma nnreal.mul_le_mul_of_le_left {a b c:nnreal}:
a ≤ b → c * a ≤ c * b :=
begin
intro A1,
apply ordered_comm_monoid.mul_le_mul_left,
apply A1,
end
----- From Radon Nikodym ---------------------------------
lemma nnreal.mul_lt_mul_of_pos_of_lt
{a b c:nnreal}:(0 < a) → (b < c) → (a * b < a * c) :=
begin
intros A1 A2,
apply mul_lt_mul',
apply le_refl _,
apply A2,
simp,
apply A1,
end
lemma canonically_ordered_add_monoid.zero_lt_iff_ne_zero
{α:Type*} [canonically_ordered_add_monoid α] {x:α}:0 < x ↔ x ≠ 0 :=
begin
split,
{ intros h_zero_lt h_eq_zero,
rw h_eq_zero at h_zero_lt,
apply lt_irrefl _ h_zero_lt },
{ intros h_ne,
rw lt_iff_le_and_ne,
split,
apply zero_le,
symmetry,
apply h_ne },
end
lemma nnreal.zero_lt_iff_ne_zero {x:nnreal}:0 < x ↔ x ≠ 0 := begin
rw canonically_ordered_add_monoid.zero_lt_iff_ne_zero,
end
/-
It is hard to generalize this.
-/
lemma nnreal.mul_pos_iff_pos_pos
{a b:nnreal}:(0 < a * b) ↔ (0 < a)∧ (0 < b) :=
begin
split;intros A1,
{
rw nnreal.zero_lt_iff_ne_zero at A1,
repeat {rw nnreal.zero_lt_iff_ne_zero},
split;intro B1;apply A1,
{
rw B1,
rw zero_mul,
},
{
rw B1,
rw mul_zero,
},
},
{
have C1:0 ≤ a * 0,
{
simp,
},
apply lt_of_le_of_lt C1,
apply nnreal.mul_lt_mul_of_pos_of_lt,
apply A1.left,
apply A1.right,
},
end
lemma nnreal.inv_mul_eq_inv_mul_inv {a b:nnreal}:(a * b)⁻¹=a⁻¹ * b⁻¹ :=
begin
rw nnreal.mul_inv,
rw mul_comm,
end
-- TODO: replace with zero_lt_iff_ne_zero.
lemma nnreal.pos_iff {a:nnreal}:0 < a ↔ a ≠ 0 :=
begin
rw nnreal.zero_lt_iff_ne_zero,
end
lemma nnreal.add_pos_le_false {x ε:nnreal}: (0 < ε) → (x + ε ≤ x) → false :=
begin
cases x, cases ε,
rw ← nnreal.coe_le_coe,
rw ← nnreal.coe_lt_coe,
rw nnreal.coe_add,
simp,
intros h1 h2,
linarith [h1, h2],
end
lemma nnreal.lt_add_pos {x ε:nnreal}: (0 < ε) → (x < x + ε) :=
begin
cases ε; cases x; simp,
end
|
5b0aea5dd4fa84bd20db3f7cbb7f50e87303e112 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/topology/order.lean | 67bf35bd86b176b5063eeca75872ac7480f66165 | [] | 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 | 28,399 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro
-/
import Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.topology.tactic
import Mathlib.PostPort
universes u u_1 l v u_2 w u_3
namespace Mathlib
/-!
# Ordering on topologies and (co)induced topologies
Topologies on a fixed type `α` are ordered, by reverse inclusion.
That is, for topologies `t₁` and `t₂` on `α`, we write `t₁ ≤ t₂`
if every set open in `t₂` is also open in `t₁`.
(One also calls `t₁` finer than `t₂`, and `t₂` coarser than `t₁`.)
Any function `f : α → β` induces
`induced f : topological_space β → topological_space α`
and `coinduced f : topological_space α → topological_space β`.
Continuity, the ordering on topologies and (co)induced topologies are
related as follows:
* The identity map (α, t₁) → (α, t₂) is continuous iff t₁ ≤ t₂.
* A map f : (α, t) → (β, u) is continuous
iff t ≤ induced f u (`continuous_iff_le_induced`)
iff coinduced f t ≤ u (`continuous_iff_coinduced_le`).
Topologies on α form a complete lattice, with ⊥ the discrete topology
and ⊤ the indiscrete topology.
For a function f : α → β, (coinduced f, induced f) is a Galois connection
between topologies on α and topologies on β.
## Implementation notes
There is a Galois insertion between topologies on α (with the inclusion ordering)
and all collections of sets in α. The complete lattice structure on topologies
on α is defined as the reverse of the one obtained via this Galois insertion.
## Tags
finer, coarser, induced topology, coinduced topology
-/
namespace topological_space
/-- The open sets of the least topology containing a collection of basic sets. -/
inductive generate_open {α : Type u} (g : set (set α)) : set α → Prop
where
| basic : ∀ (s : set α), s ∈ g → generate_open g s
| univ : generate_open g set.univ
| inter : ∀ (s t : set α), generate_open g s → generate_open g t → generate_open g (s ∩ t)
| sUnion : ∀ (k : set (set α)), (∀ (s : set α), s ∈ k → generate_open g s) → generate_open g (⋃₀k)
/-- The smallest topological space containing the collection `g` of basic sets -/
def generate_from {α : Type u} (g : set (set α)) : topological_space α :=
mk (generate_open g) generate_open.univ generate_open.inter generate_open.sUnion
theorem nhds_generate_from {α : Type u} {g : set (set α)} {a : α} : nhds a = infi fun (s : set α) => infi fun (H : s ∈ set_of fun (s : set α) => a ∈ s ∧ s ∈ g) => filter.principal s := sorry
theorem tendsto_nhds_generate_from {α : Type u} {β : Type u_1} {m : α → β} {f : filter α} {g : set (set β)} {b : β} (h : ∀ (s : set β), s ∈ g → b ∈ s → m ⁻¹' s ∈ f) : filter.tendsto m f (nhds b) := sorry
/-- Construct a topology on α given the filter of neighborhoods of each point of α. -/
protected def mk_of_nhds {α : Type u} (n : α → filter α) : topological_space α :=
mk (fun (s : set α) => ∀ (a : α), a ∈ s → s ∈ n a) sorry sorry sorry
theorem nhds_mk_of_nhds {α : Type u} (n : α → filter α) (a : α) (h₀ : pure ≤ n) (h₁ : ∀ {a : α} {s : set α}, s ∈ n a → ∃ (t : set α), ∃ (H : t ∈ n a), t ⊆ s ∧ ∀ (a' : α), a' ∈ t → s ∈ n a') : nhds a = n a := sorry
end topological_space
/-- The inclusion ordering on topologies on α. We use it to get a complete
lattice instance via the Galois insertion method, but the partial order
that we will eventually impose on `topological_space α` is the reverse one. -/
def tmp_order {α : Type u} : partial_order (topological_space α) :=
partial_order.mk (fun (t s : topological_space α) => topological_space.is_open t ≤ topological_space.is_open s)
(preorder.lt._default fun (t s : topological_space α) => topological_space.is_open t ≤ topological_space.is_open s)
sorry sorry sorry
/- We'll later restate this lemma in terms of the correct order on `topological_space α`. -/
/-- If `s` equals the collection of open sets in the topology it generates,
then `s` defines a topology. -/
protected def mk_of_closure {α : Type u} (s : set (set α)) (hs : (set_of fun (u : set α) => topological_space.is_open (topological_space.generate_from s) u) = s) : topological_space α :=
topological_space.mk (fun (u : set α) => u ∈ s) sorry sorry sorry
theorem mk_of_closure_sets {α : Type u} {s : set (set α)} {hs : (set_of fun (u : set α) => topological_space.is_open (topological_space.generate_from s) u) = s} : Mathlib.mk_of_closure s hs = topological_space.generate_from s :=
topological_space_eq (Eq.symm hs)
/-- The Galois insertion between `set (set α)` and `topological_space α` whose lower part
sends a collection of subsets of α to the topology they generate, and whose upper part
sends a topology to its collection of open subsets. -/
def gi_generate_from (α : Type u_1) : galois_insertion topological_space.generate_from
fun (t : topological_space α) => set_of fun (s : set α) => topological_space.is_open t s :=
galois_insertion.mk
(fun (g : set (set α))
(hg : (set_of fun (s : set α) => topological_space.is_open (topological_space.generate_from g) s) ≤ g) =>
Mathlib.mk_of_closure g sorry)
sorry sorry sorry
theorem generate_from_mono {α : Type u_1} {g₁ : set (set α)} {g₂ : set (set α)} (h : g₁ ⊆ g₂) : topological_space.generate_from g₁ ≤ topological_space.generate_from g₂ :=
galois_connection.monotone_l (galois_insertion.gc (gi_generate_from α)) h
/-- The complete lattice of topological spaces, but built on the inclusion ordering. -/
def tmp_complete_lattice {α : Type u} : complete_lattice (topological_space α) :=
galois_insertion.lift_complete_lattice (gi_generate_from α)
/-- The ordering on topologies on the type `α`.
`t ≤ s` if every set open in `s` is also open in `t` (`t` is finer than `s`). -/
protected instance topological_space.partial_order {α : Type u} : partial_order (topological_space α) :=
partial_order.mk (fun (t s : topological_space α) => topological_space.is_open s ≤ topological_space.is_open t)
(preorder.lt._default fun (t s : topological_space α) => topological_space.is_open s ≤ topological_space.is_open t)
sorry sorry sorry
theorem le_generate_from_iff_subset_is_open {α : Type u} {g : set (set α)} {t : topological_space α} : t ≤ topological_space.generate_from g ↔ g ⊆ set_of fun (s : set α) => topological_space.is_open t s :=
generate_from_le_iff_subset_is_open
/-- Topologies on `α` form a complete lattice, with `⊥` the discrete topology
and `⊤` the indiscrete topology. The infimum of a collection of topologies
is the topology generated by all their open sets, while the supremem is the
topology whose open sets are those sets open in every member of the collection. -/
protected instance topological_space.complete_lattice {α : Type u} : complete_lattice (topological_space α) :=
order_dual.complete_lattice (topological_space α)
/-- A topological space is discrete if every set is open, that is,
its topology equals the discrete topology `⊥`. -/
class discrete_topology (α : Type u_1) [t : topological_space α]
where
eq_bot : t = ⊥
@[simp] theorem is_open_discrete {α : Type u} [topological_space α] [discrete_topology α] (s : set α) : is_open s :=
Eq.symm (discrete_topology.eq_bot α) ▸ trivial
@[simp] theorem is_closed_discrete {α : Type u} [topological_space α] [discrete_topology α] (s : set α) : is_closed s :=
Eq.symm (discrete_topology.eq_bot α) ▸ trivial
theorem continuous_of_discrete_topology {α : Type u} {β : Type v} [topological_space α] [discrete_topology α] [topological_space β] {f : α → β} : continuous f :=
iff.mpr continuous_def fun (s : set β) (hs : is_open s) => is_open_discrete (f ⁻¹' s)
theorem nhds_bot (α : Type u_1) : nhds = pure :=
le_antisymm (id fun (a : α) => id fun (s : set α) (hs : s ∈ pure a) => mem_nhds_sets trivial hs) pure_le_nhds
theorem nhds_discrete (α : Type u_1) [topological_space α] [discrete_topology α] : nhds = pure :=
Eq.symm (discrete_topology.eq_bot α) ▸ nhds_bot α
theorem le_of_nhds_le_nhds {α : Type u} {t₁ : topological_space α} {t₂ : topological_space α} (h : ∀ (x : α), nhds x ≤ nhds x) : t₁ ≤ t₂ := sorry
theorem eq_of_nhds_eq_nhds {α : Type u} {t₁ : topological_space α} {t₂ : topological_space α} (h : ∀ (x : α), nhds x = nhds x) : t₁ = t₂ :=
le_antisymm (le_of_nhds_le_nhds fun (x : α) => le_of_eq (h x))
(le_of_nhds_le_nhds fun (x : α) => le_of_eq (Eq.symm (h x)))
theorem eq_bot_of_singletons_open {α : Type u} {t : topological_space α} (h : ∀ (x : α), topological_space.is_open t (singleton x)) : t = ⊥ :=
bot_unique
fun (s : set α) (hs : topological_space.is_open ⊥ s) =>
set.bUnion_of_singleton s ▸ is_open_bUnion fun (x : α) (_x : x ∈ s) => h x
theorem forall_open_iff_discrete {X : Type u_1} [topological_space X] : (∀ (s : set X), is_open s) ↔ discrete_topology X := sorry
theorem singletons_open_iff_discrete {X : Type u_1} [topological_space X] : (∀ (a : X), is_open (singleton a)) ↔ discrete_topology X :=
{ mp := fun (h : ∀ (a : X), is_open (singleton a)) => discrete_topology.mk (eq_bot_of_singletons_open h),
mpr := fun (a : discrete_topology X) (_x : X) => is_open_discrete (singleton _x) }
/-- Given `f : α → β` and a topology on `β`, the induced topology on `α` is the collection of
sets that are preimages of some open set in `β`. This is the coarsest topology that
makes `f` continuous. -/
def topological_space.induced {α : Type u} {β : Type v} (f : α → β) (t : topological_space β) : topological_space α :=
topological_space.mk (fun (s : set α) => ∃ (s' : set β), topological_space.is_open t s' ∧ f ⁻¹' s' = s) sorry sorry
sorry
theorem is_open_induced_iff {α : Type u_1} {β : Type u_2} [t : topological_space β] {s : set α} {f : α → β} : is_open s ↔ ∃ (t_1 : set β), is_open t_1 ∧ f ⁻¹' t_1 = s :=
iff.rfl
theorem is_closed_induced_iff {α : Type u_1} {β : Type u_2} [t : topological_space β] {s : set α} {f : α → β} : is_closed s ↔ ∃ (t_1 : set β), is_closed t_1 ∧ s = f ⁻¹' t_1 := sorry
/-- Given `f : α → β` and a topology on `α`, the coinduced topology on `β` is defined
such that `s:set β` is open if the preimage of `s` is open. This is the finest topology that
makes `f` continuous. -/
def topological_space.coinduced {α : Type u} {β : Type v} (f : α → β) (t : topological_space α) : topological_space β :=
topological_space.mk (fun (s : set β) => topological_space.is_open t (f ⁻¹' s)) sorry sorry sorry
theorem is_open_coinduced {α : Type u_1} {β : Type u_2} {t : topological_space α} {s : set β} {f : α → β} : is_open s ↔ is_open (f ⁻¹' s) :=
iff.rfl
theorem continuous.coinduced_le {α : Type u_1} {β : Type u_2} {t : topological_space α} {t' : topological_space β} {f : α → β} (h : continuous f) : topological_space.coinduced f t ≤ t' :=
fun (s : set β) (hs : topological_space.is_open t' s) => iff.mp continuous_def h s hs
theorem coinduced_le_iff_le_induced {α : Type u_1} {β : Type u_2} {f : α → β} {tα : topological_space α} {tβ : topological_space β} : topological_space.coinduced f tα ≤ tβ ↔ tα ≤ topological_space.induced f tβ := sorry
theorem continuous.le_induced {α : Type u_1} {β : Type u_2} {t : topological_space α} {t' : topological_space β} {f : α → β} (h : continuous f) : t ≤ topological_space.induced f t' :=
iff.mp coinduced_le_iff_le_induced (continuous.coinduced_le h)
theorem gc_coinduced_induced {α : Type u_1} {β : Type u_2} (f : α → β) : galois_connection (topological_space.coinduced f) (topological_space.induced f) :=
fun (f_1 : topological_space α) (g : topological_space β) => coinduced_le_iff_le_induced
theorem induced_mono {α : Type u_1} {β : Type u_2} {t₁ : topological_space α} {t₂ : topological_space α} {g : β → α} (h : t₁ ≤ t₂) : topological_space.induced g t₁ ≤ topological_space.induced g t₂ :=
galois_connection.monotone_u (gc_coinduced_induced g) h
theorem coinduced_mono {α : Type u_1} {β : Type u_2} {t₁ : topological_space α} {t₂ : topological_space α} {f : α → β} (h : t₁ ≤ t₂) : topological_space.coinduced f t₁ ≤ topological_space.coinduced f t₂ :=
galois_connection.monotone_l (gc_coinduced_induced f) h
@[simp] theorem induced_top {α : Type u_1} {β : Type u_2} {g : β → α} : topological_space.induced g ⊤ = ⊤ :=
galois_connection.u_top (gc_coinduced_induced g)
@[simp] theorem induced_inf {α : Type u_1} {β : Type u_2} {t₁ : topological_space α} {t₂ : topological_space α} {g : β → α} : topological_space.induced g (t₁ ⊓ t₂) = topological_space.induced g t₁ ⊓ topological_space.induced g t₂ :=
galois_connection.u_inf (gc_coinduced_induced g)
@[simp] theorem induced_infi {α : Type u_1} {β : Type u_2} {g : β → α} {ι : Sort w} {t : ι → topological_space α} : topological_space.induced g (infi fun (i : ι) => t i) = infi fun (i : ι) => topological_space.induced g (t i) :=
galois_connection.u_infi (gc_coinduced_induced g)
@[simp] theorem coinduced_bot {α : Type u_1} {β : Type u_2} {f : α → β} : topological_space.coinduced f ⊥ = ⊥ :=
galois_connection.l_bot (gc_coinduced_induced f)
@[simp] theorem coinduced_sup {α : Type u_1} {β : Type u_2} {t₁ : topological_space α} {t₂ : topological_space α} {f : α → β} : topological_space.coinduced f (t₁ ⊔ t₂) = topological_space.coinduced f t₁ ⊔ topological_space.coinduced f t₂ :=
galois_connection.l_sup (gc_coinduced_induced f)
@[simp] theorem coinduced_supr {α : Type u_1} {β : Type u_2} {f : α → β} {ι : Sort w} {t : ι → topological_space α} : topological_space.coinduced f (supr fun (i : ι) => t i) = supr fun (i : ι) => topological_space.coinduced f (t i) :=
galois_connection.l_supr (gc_coinduced_induced f)
theorem induced_id {α : Type u_1} [t : topological_space α] : topological_space.induced id t = t := sorry
theorem induced_compose {α : Type u_1} {β : Type u_2} {γ : Type u_3} [tγ : topological_space γ] {f : α → β} {g : β → γ} : topological_space.induced f (topological_space.induced g tγ) = topological_space.induced (g ∘ f) tγ := sorry
theorem coinduced_id {α : Type u_1} [t : topological_space α] : topological_space.coinduced id t = t :=
topological_space_eq rfl
theorem coinduced_compose {α : Type u_1} {β : Type u_2} {γ : Type u_3} [tα : topological_space α] {f : α → β} {g : β → γ} : topological_space.coinduced g (topological_space.coinduced f tα) = topological_space.coinduced (g ∘ f) tα :=
topological_space_eq rfl
/- constructions using the complete lattice structure -/
protected instance inhabited_topological_space {α : Type u} : Inhabited (topological_space α) :=
{ default := ⊤ }
protected instance subsingleton.unique_topological_space {α : Type u} [subsingleton α] : unique (topological_space α) :=
unique.mk { default := ⊥ } sorry
protected instance subsingleton.discrete_topology {α : Type u} [t : topological_space α] [subsingleton α] : discrete_topology α :=
discrete_topology.mk (unique.eq_default t)
protected instance empty.topological_space : topological_space empty :=
⊥
protected instance empty.discrete_topology : discrete_topology empty :=
discrete_topology.mk rfl
protected instance pempty.topological_space : topological_space pempty :=
⊥
protected instance pempty.discrete_topology : discrete_topology pempty :=
discrete_topology.mk rfl
protected instance unit.topological_space : topological_space Unit :=
⊥
protected instance unit.discrete_topology : discrete_topology Unit :=
discrete_topology.mk rfl
protected instance bool.topological_space : topological_space Bool :=
⊥
protected instance bool.discrete_topology : discrete_topology Bool :=
discrete_topology.mk rfl
protected instance nat.topological_space : topological_space ℕ :=
⊥
protected instance nat.discrete_topology : discrete_topology ℕ :=
discrete_topology.mk rfl
protected instance int.topological_space : topological_space ℤ :=
⊥
protected instance int.discrete_topology : discrete_topology ℤ :=
discrete_topology.mk rfl
protected instance sierpinski_space : topological_space Prop :=
topological_space.generate_from (singleton (singleton True))
theorem le_generate_from {α : Type u} {t : topological_space α} {g : set (set α)} (h : ∀ (s : set α), s ∈ g → is_open s) : t ≤ topological_space.generate_from g :=
iff.mpr le_generate_from_iff_subset_is_open h
theorem induced_generate_from_eq {α : Type u_1} {β : Type u_2} {b : set (set β)} {f : α → β} : topological_space.induced f (topological_space.generate_from b) = topological_space.generate_from (set.preimage f '' b) := sorry
/-- This construction is left adjoint to the operation sending a topology on `α`
to its neighborhood filter at a fixed point `a : α`. -/
protected def topological_space.nhds_adjoint {α : Type u} (a : α) (f : filter α) : topological_space α :=
topological_space.mk (fun (s : set α) => a ∈ s → s ∈ f) sorry sorry sorry
theorem gc_nhds {α : Type u} (a : α) : galois_connection (topological_space.nhds_adjoint a) fun (t : topological_space α) => nhds a := sorry
theorem nhds_mono {α : Type u} {t₁ : topological_space α} {t₂ : topological_space α} {a : α} (h : t₁ ≤ t₂) : nhds a ≤ nhds a :=
galois_connection.monotone_u (gc_nhds a) h
theorem nhds_infi {α : Type u} {ι : Sort u_1} {t : ι → topological_space α} {a : α} : nhds a = infi fun (i : ι) => nhds a :=
galois_connection.u_infi (gc_nhds a)
theorem nhds_Inf {α : Type u} {s : set (topological_space α)} {a : α} : nhds a = infi fun (t : topological_space α) => infi fun (H : t ∈ s) => nhds a :=
galois_connection.u_Inf (gc_nhds a)
theorem nhds_inf {α : Type u} {t₁ : topological_space α} {t₂ : topological_space α} {a : α} : nhds a = nhds a ⊓ nhds a :=
galois_connection.u_inf (gc_nhds a)
theorem nhds_top {α : Type u} {a : α} : nhds a = ⊤ :=
galois_connection.u_top (gc_nhds a)
theorem continuous_iff_coinduced_le {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : topological_space β} : continuous f ↔ topological_space.coinduced f t₁ ≤ t₂ :=
iff.trans continuous_def iff.rfl
theorem continuous_iff_le_induced {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : topological_space β} : continuous f ↔ t₁ ≤ topological_space.induced f t₂ :=
iff.trans continuous_iff_coinduced_le (gc_coinduced_induced f t₁ t₂)
theorem continuous_generated_from {α : Type u} {β : Type v} {f : α → β} {t : topological_space α} {b : set (set β)} (h : ∀ (s : set β), s ∈ b → is_open (f ⁻¹' s)) : continuous f :=
iff.mpr continuous_iff_coinduced_le (le_generate_from h)
theorem continuous_induced_dom {α : Type u} {β : Type v} {f : α → β} {t : topological_space β} : continuous f :=
eq.mpr (id (Eq._oldrec (Eq.refl (continuous f)) (propext continuous_def)))
fun (s : set β) (h : is_open s) => Exists.intro s { left := h, right := rfl }
theorem continuous_induced_rng {α : Type u} {β : Type v} {γ : Type u_1} {f : α → β} {g : γ → α} {t₂ : topological_space β} {t₁ : topological_space γ} (h : continuous (f ∘ g)) : continuous g := sorry
theorem continuous_induced_rng' {α : Type u} {β : Type v} {γ : Type u_1} [topological_space α] [topological_space β] [topological_space γ] {g : γ → α} (f : α → β) (H : _inst_1 = topological_space.induced f _inst_2) (h : continuous (f ∘ g)) : continuous g :=
Eq.symm H ▸ continuous_induced_rng h
theorem continuous_coinduced_rng {α : Type u} {β : Type v} {f : α → β} {t : topological_space α} : continuous f :=
eq.mpr (id (Eq._oldrec (Eq.refl (continuous f)) (propext continuous_def))) fun (s : set β) (h : is_open s) => h
theorem continuous_coinduced_dom {α : Type u} {β : Type v} {γ : Type u_1} {f : α → β} {g : β → γ} {t₁ : topological_space α} {t₂ : topological_space γ} (h : continuous (g ∘ f)) : continuous g :=
eq.mpr (id (Eq._oldrec (Eq.refl (continuous g)) (propext continuous_def)))
fun (s : set γ) (hs : is_open s) => eq.mp (Eq._oldrec (Eq.refl (continuous (g ∘ f))) (propext continuous_def)) h s hs
theorem continuous_le_dom {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : topological_space α} {t₃ : topological_space β} (h₁ : t₂ ≤ t₁) (h₂ : continuous f) : continuous f :=
eq.mpr (id (Eq._oldrec (Eq.refl (continuous f)) (propext continuous_def)))
fun (s : set β) (h : is_open s) =>
h₁ (f ⁻¹' s) (eq.mp (Eq._oldrec (Eq.refl (continuous f)) (propext continuous_def)) h₂ s h)
theorem continuous_le_rng {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : topological_space β} {t₃ : topological_space β} (h₁ : t₂ ≤ t₃) (h₂ : continuous f) : continuous f :=
eq.mpr (id (Eq._oldrec (Eq.refl (continuous f)) (propext continuous_def)))
fun (s : set β) (h : is_open s) => eq.mp (Eq._oldrec (Eq.refl (continuous f)) (propext continuous_def)) h₂ s (h₁ s h)
theorem continuous_sup_dom {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : topological_space α} {t₃ : topological_space β} (h₁ : continuous f) (h₂ : continuous f) : continuous f := sorry
theorem continuous_sup_rng_left {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₃ : topological_space β} {t₂ : topological_space β} : continuous f → continuous f :=
continuous_le_rng le_sup_left
theorem continuous_sup_rng_right {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₃ : topological_space β} {t₂ : topological_space β} : continuous f → continuous f :=
continuous_le_rng le_sup_right
theorem continuous_Sup_dom {α : Type u} {β : Type v} {f : α → β} {t₁ : set (topological_space α)} {t₂ : topological_space β} (h : ∀ (t : topological_space α), t ∈ t₁ → continuous f) : continuous f :=
iff.mpr continuous_iff_le_induced
(Sup_le fun (t : topological_space α) (ht : t ∈ t₁) => iff.mp continuous_iff_le_induced (h t ht))
theorem continuous_Sup_rng {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : set (topological_space β)} {t : topological_space β} (h₁ : t ∈ t₂) (hf : continuous f) : continuous f :=
iff.mpr continuous_iff_coinduced_le (le_Sup_of_le h₁ (iff.mp continuous_iff_coinduced_le hf))
theorem continuous_supr_dom {α : Type u} {β : Type v} {f : α → β} {ι : Sort u_2} {t₁ : ι → topological_space α} {t₂ : topological_space β} (h : ι → continuous f) : continuous f := sorry
theorem continuous_supr_rng {α : Type u} {β : Type v} {f : α → β} {ι : Sort u_2} {t₁ : topological_space α} {t₂ : ι → topological_space β} {i : ι} (h : continuous f) : continuous f :=
continuous_Sup_rng (Exists.intro i rfl) h
theorem continuous_inf_rng {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : topological_space β} {t₃ : topological_space β} (h₁ : continuous f) (h₂ : continuous f) : continuous f :=
iff.mpr continuous_iff_coinduced_le
(le_inf (iff.mp continuous_iff_coinduced_le h₁) (iff.mp continuous_iff_coinduced_le h₂))
theorem continuous_inf_dom_left {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : topological_space α} {t₃ : topological_space β} : continuous f → continuous f :=
continuous_le_dom inf_le_left
theorem continuous_inf_dom_right {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : topological_space α} {t₃ : topological_space β} : continuous f → continuous f :=
continuous_le_dom inf_le_right
theorem continuous_Inf_dom {α : Type u} {β : Type v} {f : α → β} {t₁ : set (topological_space α)} {t₂ : topological_space β} {t : topological_space α} (h₁ : t ∈ t₁) : continuous f → continuous f :=
continuous_le_dom (Inf_le h₁)
theorem continuous_Inf_rng {α : Type u} {β : Type v} {f : α → β} {t₁ : topological_space α} {t₂ : set (topological_space β)} (h : ∀ (t : topological_space β), t ∈ t₂ → continuous f) : continuous f :=
iff.mpr continuous_iff_coinduced_le
(le_Inf fun (b : topological_space β) (hb : b ∈ t₂) => iff.mp continuous_iff_coinduced_le (h b hb))
theorem continuous_infi_dom {α : Type u} {β : Type v} {f : α → β} {ι : Sort u_2} {t₁ : ι → topological_space α} {t₂ : topological_space β} {i : ι} : continuous f → continuous f :=
continuous_le_dom (infi_le t₁ i)
theorem continuous_infi_rng {α : Type u} {β : Type v} {f : α → β} {ι : Sort u_2} {t₁ : topological_space α} {t₂ : ι → topological_space β} (h : ι → continuous f) : continuous f :=
iff.mpr continuous_iff_coinduced_le (le_infi fun (i : ι) => iff.mp continuous_iff_coinduced_le (h i))
theorem continuous_bot {α : Type u} {β : Type v} {f : α → β} {t : topological_space β} : continuous f :=
iff.mpr continuous_iff_le_induced bot_le
theorem continuous_top {α : Type u} {β : Type v} {f : α → β} {t : topological_space α} : continuous f :=
iff.mpr continuous_iff_coinduced_le le_top
/- 𝓝 in the induced topology -/
theorem mem_nhds_induced {α : Type u} {β : Type v} [T : topological_space α] (f : β → α) (a : β) (s : set β) : s ∈ nhds a ↔ ∃ (u : set α), ∃ (H : u ∈ nhds (f a)), f ⁻¹' u ⊆ s := sorry
theorem nhds_induced {α : Type u} {β : Type v} [T : topological_space α] (f : β → α) (a : β) : nhds a = filter.comap f (nhds (f a)) := sorry
theorem induced_iff_nhds_eq {α : Type u} {β : Type v} [tα : topological_space α] [tβ : topological_space β] (f : β → α) : tβ = topological_space.induced f tα ↔ ∀ (b : β), nhds b = filter.comap f (nhds (f b)) := sorry
theorem map_nhds_induced_of_surjective {α : Type u} {β : Type v} [T : topological_space α] {f : β → α} (hf : function.surjective f) (a : β) : filter.map f (nhds a) = nhds (f a) := sorry
theorem is_open_induced_eq {α : Type u_1} {β : Type u_2} [t : topological_space β] {f : α → β} {s : set α} : is_open s ↔ s ∈ set.preimage f '' set_of fun (s : set β) => is_open s :=
iff.rfl
theorem is_open_induced {α : Type u_1} {β : Type u_2} [t : topological_space β] {f : α → β} {s : set β} (h : is_open s) : topological_space.is_open (topological_space.induced f t) (f ⁻¹' s) :=
Exists.intro s { left := h, right := rfl }
theorem map_nhds_induced_eq {α : Type u_1} {β : Type u_2} [t : topological_space β] {f : α → β} {a : α} (h : set.range f ∈ nhds (f a)) : filter.map f (nhds a) = nhds (f a) :=
eq.mpr (id (Eq._oldrec (Eq.refl (filter.map f (nhds a) = nhds (f a))) (nhds_induced f a)))
(eq.mpr (id (Eq._oldrec (Eq.refl (filter.map f (filter.comap f (nhds (f a))) = nhds (f a))) (filter.map_comap h)))
(Eq.refl (nhds (f a))))
theorem closure_induced {α : Type u_1} {β : Type u_2} [t : topological_space β] {f : α → β} {a : α} {s : set α} (hf : ∀ (x y : α), f x = f y → x = y) : a ∈ closure s ↔ f a ∈ closure (f '' s) := sorry
@[simp] theorem is_open_singleton_true : is_open (singleton True) :=
topological_space.generate_open.basic (singleton True)
(eq.mpr (id (propext ((fun {α : Type} (a : α) => iff_true_intro (set.mem_singleton a)) (singleton True)))) trivial)
theorem continuous_Prop {α : Type u_1} [topological_space α] {p : α → Prop} : continuous p ↔ is_open (set_of fun (x : α) => p x) := sorry
theorem is_open_supr_iff {α : Type u} {ι : Type v} {t : ι → topological_space α} {s : set α} : is_open s ↔ ι → is_open s := sorry
theorem is_closed_infi_iff {α : Type u} {ι : Type v} {t : ι → topological_space α} {s : set α} : is_closed s ↔ ι → is_closed s :=
is_open_supr_iff
|
6a8c86722d3d340904fae3687b5883c0cb5de535 | e0e64c424bf126977aef10e58324934782979062 | /src/wk3/lin_ind.lean | d68d0a41597f6ee41eba86a322552b5312fa8279 | [] | no_license | jamesa9283/LiaLeanTutor | 34e9e133a4f7dd415f02c14c4a62351bb9fd8c21 | c7ac1400f26eb2992f5f1ee0aaafb54b74665072 | refs/heads/master | 1,686,146,337,422 | 1,625,227,392,000 | 1,625,227,392,000 | 373,130,175 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,557 | lean | import tactic linear_algebra.linear_independent
variables {ι : Type*} {R : Type*} {V' : Type*} {M : Type*}
section one
variables (a b : ℕ)
#check ({a} : set ℕ)
#check ({a, b} : set ℕ)
variables {v : ι → M} [semiring R] [add_comm_monoid M] [module R M] {a b : R}
#check false.elim
lemma linear_independent_empty_type' (h : ¬ nonempty ι) : linear_independent R v :=
begin
rw linear_independent_iff,
intros f hf,
ext i,
apply false.elim (h ⟨i⟩),
end
/-
variables (A : ({a} : set ℕ))
#check ⋃ (i : A), i
-/
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 set.finite_subset_Union ft ht with ⟨I, fi, hI⟩,
rcases hs.finset_le fi.to_finset with ⟨i, hi⟩,
exact (h i).mono (set.subset.trans hI $ set.bUnion_subset $
λ j hj, hi j (fi.mem_to_finset.2 hj)) },
{ refine (linear_independent_empty _ _).mono _,
rintro _ ⟨_, ⟨i, _⟩, _⟩, exact hη ⟨i⟩
}
end
end one
section two
open set module submodule
variables [ring R] [add_comm_group M] [module R M]
#check
#check
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,
sorry
end
end two
|
b36b9dc0e79a76509957fe29f82f21fc15268a14 | fe208a542cea7b2d6d7ff79f94d535f6d11d814a | /src/Logic/abhimanyu.lean | f305869247162eb85524ce821d94021067dc0238 | [] | no_license | ImperialCollegeLondon/M1F_room_342_questions | c4b98b14113fe900a7f388762269305faff73e63 | 63de9a6ab9c27a433039dd5530bc9b10b1d227f7 | refs/heads/master | 1,585,807,312,561 | 1,545,232,972,000 | 1,545,232,972,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 2,721 | lean | inductive fml
| atom (i : ℕ)
| imp (a b : fml)
| not (a : fml)
open fml
infixr ` →' `:50 := imp
local notation ` ¬' ` := fml.not
inductive prf : fml → Type
| axk (p q) : prf (p →' q →' p)
| axs (p q r) : prf $ (p →' q →' r) →' (p →' q) →' (p →' r)
| axn (p q) : prf $ (¬'q →' ¬'p) →' p →' q
| amp (p q) : prf (p →' (p →' q) →' q) -- internal modus ponnens, i couldn't prove it, can you?
| cn (p q r) : prf (q →' r) → prf (p →' q) → prf (p →' r)
| mp (p q) : prf p → prf (p →' q) → prf q
theorem reflex (P : fml) : prf (P →' P) :=
begin
have R : fml, exact P,
let Q : fml := R →' P,
have HPQ : prf (P →' Q),
change prf (P →' (R →' P)),
apply prf.axk,
have HPQP : prf (P →' (Q →' P)),
apply prf.axk,
have HPQPP : prf ((P →' Q) →' (P →' P)),
apply prf.mp (P →' Q →' P),
exact HPQP,
apply prf.axs,
apply prf.mp (P →' Q),
exact HPQ,
exact HPQPP,
end
lemma deduct (P Q : fml) : prf ((P →' Q) →' P →' Q) :=
begin
apply reflex,
end
lemma deduction (P Q : fml) : prf ((P →' (P →' Q)) →' (P →' Q)) :=
begin
apply prf.mp (P →' ((P →' Q) →' Q)),
apply prf.amp,
apply prf.axs,
end
lemma yesyes (Q : fml) : prf (¬'(¬'Q) →' Q) :=
begin
have H4213 : prf ((¬'(¬'(¬'(¬'Q))) →' ¬'(¬'Q)) →' (¬'Q →' ¬'(¬'(¬'Q)))),
apply prf.axn,
have H1320 : prf ((¬'Q →' ¬'(¬'(¬'Q))) →' (¬'(¬'Q) →' Q)),
apply prf.axn,
have H4220 : prf ((¬'(¬'(¬'(¬'Q))) →' ¬'(¬'Q)) →' (¬'(¬'Q) →' Q)),
apply prf.cn (¬'(¬'(¬'(¬'Q))) →' ¬'(¬'Q)) ((¬'Q →' ¬'(¬'(¬'Q)))) (¬'(¬'Q) →' Q),
exact H1320, exact H4213,
have H242 : prf (¬'(¬'Q) →' ((¬'(¬'(¬'(¬'Q)))) →'(¬'(¬'Q)))),
apply prf.axk,
have H2020 : prf ((¬' (¬' Q) →' Q) →' (¬' (¬' Q) →' Q)),
apply reflex,
have H220 : prf (¬' (¬' Q) →' (¬' (¬' Q) →' Q)),
apply prf.cn _ ((¬'(¬'(¬'(¬'Q)))) →'(¬'(¬'Q))),
exact H4220,
exact H242,
apply prf.mp (¬' (¬' Q) →' ¬' (¬' Q) →' Q),
exact H220,
apply deduction,
end
theorem notnot (P : fml) : prf (P →' ¬'(¬'P)) :=
begin
have H31 : prf (¬'(¬'(¬'P)) →' ¬' P),
apply yesyes,
apply prf.mp (¬' (¬' (¬' P)) →' ¬' P),
exact H31,
apply prf.axn,
end
|
b2c605af36b3359345619429712fff76b0c15841 | dd4e652c749fea9ac77e404005cb3470e5f75469 | /src/missing_mathlib/data/finsupp.lean | 47da20701f0d9e8e862da3069e555a7f5205500a | [] | no_license | skbaek/cvx | e32822ad5943541539966a37dee162b0a5495f55 | c50c790c9116f9fac8dfe742903a62bdd7292c15 | refs/heads/master | 1,623,803,010,339 | 1,618,058,958,000 | 1,618,058,958,000 | 176,293,135 | 3 | 2 | null | null | null | null | UTF-8 | Lean | false | false | 700 | lean | import data.finsupp
lemma finsupp.on_finset_mem_support {α β : Type*} [decidable_eq α] [decidable_eq β] [has_zero β]
(s : finset α) (f : α → β) (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) :
∀ a : α, a ∈ (finsupp.on_finset s f hf).support ↔ f a ≠ 0 :=
by { intro, rw [finsupp.mem_support_iff, finsupp.on_finset_apply] }
lemma finsupp.on_finset_support {α β : Type*} [decidable_eq α] [decidable_eq β] [has_zero β]
(s : finset α) (f : α → β) (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) :
(finsupp.on_finset s f hf).support = s.filter (λ a, f a ≠ 0) :=
begin
ext a,
rw [finsupp.on_finset_mem_support, finset.mem_filter],
specialize hf a,
finish
end
|
8212873d5ce6d53a31a6a10c425075f227db1dfa | 2eab05920d6eeb06665e1a6df77b3157354316ad | /src/field_theory/finite/basic.lean | 555fa1ef63a9af9db9637c0adb3ff495efecd0ac | [
"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 | 15,780 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Joey van Langen, Casper Putz
-/
import tactic.apply_fun
import data.equiv.ring
import data.zmod.algebra
import linear_algebra.finite_dimensional
import ring_theory.integral_domain
import field_theory.separable
import field_theory.splitting_field
/-!
# Finite fields
This file contains basic results about finite fields.
Throughout most of this file, `K` denotes a finite field
and `q` is notation for the cardinality of `K`.
See `ring_theory.integral_domain` for the fact that the unit group of a finite field is a
cyclic group, as well as the fact that every finite integral domain is a field
(`field_of_is_domain`).
## Main results
1. `fintype.card_units`: The unit group of a finite field is has cardinality `q - 1`.
2. `sum_pow_units`: The sum of `x^i`, where `x` ranges over the units of `K`, is
- `q-1` if `q-1 ∣ i`
- `0` otherwise
3. `finite_field.card`: The cardinality `q` is a power of the characteristic of `K`.
See `card'` for a variant.
## Notation
Throughout most of this file, `K` denotes a finite field
and `q` is notation for the cardinality of `K`.
## Implementation notes
While `fintype (units K)` can be inferred from `fintype K` in the presence of `decidable_eq K`,
in this file we take the `fintype (units K)` argument directly to reduce the chance of typeclass
diamonds, as `fintype` carries data.
-/
variables {K : Type*} {R : Type*}
local notation `q` := fintype.card K
open_locale big_operators
namespace finite_field
open finset function
section polynomial
variables [comm_ring R] [is_domain R]
open polynomial
/-- The cardinality of a field is at most `n` times the cardinality of the image of a degree `n`
polynomial -/
lemma card_image_polynomial_eval [decidable_eq R] [fintype R] {p : polynomial R}
(hp : 0 < p.degree) : fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card :=
finset.card_le_mul_card_image _ _
(λ a _, calc _ = (p - C a).roots.to_finset.card : congr_arg card
(by simp [finset.ext_iff, mem_roots_sub_C hp])
... ≤ (p - C a).roots.card : multiset.to_finset_card_le _
... ≤ _ : card_roots_sub_C' hp)
/-- If `f` and `g` are quadratic polynomials, then the `f.eval a + g.eval b = 0` has a solution. -/
lemma exists_root_sum_quadratic [fintype R] {f g : polynomial R} (hf2 : degree f = 2)
(hg2 : degree g = 2) (hR : fintype.card R % 2 = 1) : ∃ a b, f.eval a + g.eval b = 0 :=
by letI := classical.dec_eq R; exact
suffices ¬ disjoint (univ.image (λ x : R, eval x f)) (univ.image (λ x : R, eval x (-g))),
begin
simp only [disjoint_left, mem_image] at this,
push_neg at this,
rcases this with ⟨x, ⟨a, _, ha⟩, ⟨b, _, hb⟩⟩,
exact ⟨a, b, by rw [ha, ← hb, eval_neg, neg_add_self]⟩
end,
assume hd : disjoint _ _,
lt_irrefl (2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card) $
calc 2 * ((univ.image (λ x : R, eval x f)) ∪ (univ.image (λ x : R, eval x (-g)))).card
≤ 2 * fintype.card R : nat.mul_le_mul_left _ (finset.card_le_univ _)
... = fintype.card R + fintype.card R : two_mul _
... < nat_degree f * (univ.image (λ x : R, eval x f)).card +
nat_degree (-g) * (univ.image (λ x : R, eval x (-g))).card :
add_lt_add_of_lt_of_le
(lt_of_le_of_ne
(card_image_polynomial_eval (by rw hf2; exact dec_trivial))
(mt (congr_arg (%2)) (by simp [nat_degree_eq_of_degree_eq_some hf2, hR])))
(card_image_polynomial_eval (by rw [degree_neg, hg2]; exact dec_trivial))
... = 2 * (univ.image (λ x : R, eval x f) ∪ univ.image (λ x : R, eval x (-g))).card :
by rw [card_disjoint_union hd]; simp [nat_degree_eq_of_degree_eq_some hf2,
nat_degree_eq_of_degree_eq_some hg2, bit0, mul_add]
end polynomial
lemma prod_univ_units_id_eq_neg_one [field K] [fintype (units K)] :
(∏ x : units K, x) = (-1 : units K) :=
begin
classical,
have : (∏ x in (@univ (units K) _).erase (-1), x) = 1,
from prod_involution (λ x _, x⁻¹) (by simp)
(λ a, by simp [units.inv_eq_self_iff] {contextual := tt})
(λ a, by simp [@inv_eq_iff_inv_eq _ _ a, eq_comm] {contextual := tt})
(by simp),
rw [← insert_erase (mem_univ (-1 : units K)), prod_insert (not_mem_erase _ _),
this, mul_one]
end
section
variables [group_with_zero K] [fintype K]
lemma pow_card_sub_one_eq_one (a : K) (ha : a ≠ 0) : a ^ (q - 1) = 1 :=
calc a ^ (fintype.card K - 1) = (units.mk0 a ha ^ (fintype.card K - 1) : units K) :
by rw [units.coe_pow, units.coe_mk0]
... = 1 : by { classical, rw [← fintype.card_units, pow_card_eq_one], refl }
lemma pow_card (a : K) : a ^ q = a :=
begin
have hp : 0 < fintype.card K := lt_trans zero_lt_one fintype.one_lt_card,
by_cases h : a = 0, { rw h, apply zero_pow hp },
rw [← nat.succ_pred_eq_of_pos hp, pow_succ, nat.pred_eq_sub_one,
pow_card_sub_one_eq_one a h, mul_one],
end
lemma pow_card_pow (n : ℕ) (a : K) : a ^ q ^ n = a :=
begin
induction n with n ih,
{ simp, },
{ simp [pow_succ, pow_mul, ih, pow_card], },
end
end
variables (K) [field K] [fintype K]
theorem card (p : ℕ) [char_p K p] : ∃ (n : ℕ+), nat.prime p ∧ q = p^(n : ℕ) :=
begin
haveI hp : fact p.prime := ⟨char_p.char_is_prime K p⟩,
letI : module (zmod p) K := { .. (zmod.cast_hom dvd_rfl K).to_module },
obtain ⟨n, h⟩ := vector_space.card_fintype (zmod p) K,
rw zmod.card at h,
refine ⟨⟨n, _⟩, hp.1, h⟩,
apply or.resolve_left (nat.eq_zero_or_pos n),
rintro rfl,
rw pow_zero at h,
have : (0 : K) = 1, { apply fintype.card_le_one_iff.mp (le_of_eq h) },
exact absurd this zero_ne_one,
end
-- this statement doesn't use `q` because we want `K` to be an explicit parameter
theorem card' : ∃ (p : ℕ) (n : ℕ+), nat.prime p ∧ fintype.card K = p^(n : ℕ) :=
let ⟨p, hc⟩ := char_p.exists K in ⟨p, @finite_field.card K _ _ p hc⟩
@[simp] lemma cast_card_eq_zero : (q : K) = 0 :=
begin
rcases char_p.exists K with ⟨p, _char_p⟩, resetI,
rcases card K p with ⟨n, hp, hn⟩,
simp only [char_p.cast_eq_zero_iff K p, hn],
conv { congr, rw [← pow_one p] },
exact pow_dvd_pow _ n.2,
end
lemma forall_pow_eq_one_iff (i : ℕ) :
(∀ x : units K, x ^ i = 1) ↔ q - 1 ∣ i :=
begin
classical,
obtain ⟨x, hx⟩ := is_cyclic.exists_generator (units K),
rw [←fintype.card_units, ←order_of_eq_card_of_forall_mem_gpowers hx, order_of_dvd_iff_pow_eq_one],
split,
{ intro h, apply h },
{ intros h y,
simp_rw ← mem_powers_iff_mem_gpowers at hx,
rcases hx y with ⟨j, rfl⟩,
rw [← pow_mul, mul_comm, pow_mul, h, one_pow], }
end
/-- The sum of `x ^ i` as `x` ranges over the units of a finite field of cardinality `q`
is equal to `0` unless `(q - 1) ∣ i`, in which case the sum is `q - 1`. -/
lemma sum_pow_units [fintype (units K)] (i : ℕ) :
∑ x : units K, (x ^ i : K) = if (q - 1) ∣ i then -1 else 0 :=
begin
let φ : units K →* K :=
{ to_fun := λ x, x ^ i,
map_one' := by rw [units.coe_one, one_pow],
map_mul' := by { intros, rw [units.coe_mul, mul_pow] } },
haveI : decidable (φ = 1), { classical, apply_instance },
calc ∑ x : units K, φ x = if φ = 1 then fintype.card (units K) else 0 : sum_hom_units φ
... = if (q - 1) ∣ i then -1 else 0 : _,
suffices : (q - 1) ∣ i ↔ φ = 1,
{ simp only [this],
split_ifs with h h, swap, refl,
rw [fintype.card_units, nat.cast_sub, cast_card_eq_zero, nat.cast_one, zero_sub],
show 1 ≤ q, from fintype.card_pos_iff.mpr ⟨0⟩ },
rw [← forall_pow_eq_one_iff, monoid_hom.ext_iff],
apply forall_congr, intro x,
rw [units.ext_iff, units.coe_pow, units.coe_one, monoid_hom.one_apply],
refl,
end
/-- The sum of `x ^ i` as `x` ranges over a finite field of cardinality `q`
is equal to `0` if `i < q - 1`. -/
lemma sum_pow_lt_card_sub_one (i : ℕ) (h : i < q - 1) :
∑ x : K, x ^ i = 0 :=
begin
by_cases hi : i = 0,
{ simp only [hi, nsmul_one, sum_const, pow_zero, card_univ, cast_card_eq_zero], },
classical,
have hiq : ¬ (q - 1) ∣ i, { contrapose! h, exact nat.le_of_dvd (nat.pos_of_ne_zero hi) h },
let φ : units K ↪ K := ⟨coe, units.ext⟩,
have : univ.map φ = univ \ {0},
{ ext x,
simp only [true_and, embedding.coe_fn_mk, mem_sdiff, units.exists_iff_ne_zero,
mem_univ, mem_map, exists_prop_of_true, mem_singleton] },
calc ∑ x : K, x ^ i = ∑ x in univ \ {(0 : K)}, x ^ i :
by rw [← sum_sdiff ({0} : finset K).subset_univ, sum_singleton,
zero_pow (nat.pos_of_ne_zero hi), add_zero]
... = ∑ x : units K, x ^ i : by { rw [← this, univ.sum_map φ], refl }
... = 0 : by { rw [sum_pow_units K i, if_neg], exact hiq, }
end
section is_splitting_field
open polynomial
section
variables (K' : Type*) [field K'] {p n : ℕ}
lemma X_pow_card_sub_X_nat_degree_eq (hp : 1 < p) :
(X ^ p - X : polynomial K').nat_degree = p :=
begin
have h1 : (X : polynomial K').degree < (X ^ p : polynomial K').degree,
{ rw [degree_X_pow, degree_X],
exact_mod_cast hp },
rw [nat_degree_eq_of_degree_eq (degree_sub_eq_left_of_degree_lt h1), nat_degree_X_pow],
end
lemma X_pow_card_pow_sub_X_nat_degree_eq (hn : n ≠ 0) (hp : 1 < p) :
(X ^ p ^ n - X : polynomial K').nat_degree = p ^ n :=
X_pow_card_sub_X_nat_degree_eq K' $ nat.one_lt_pow _ _ (nat.pos_of_ne_zero hn) hp
lemma X_pow_card_sub_X_ne_zero (hp : 1 < p) : (X ^ p - X : polynomial K') ≠ 0 :=
ne_zero_of_nat_degree_gt $
calc 1 < _ : hp
... = _ : (X_pow_card_sub_X_nat_degree_eq K' hp).symm
lemma X_pow_card_pow_sub_X_ne_zero (hn : n ≠ 0) (hp : 1 < p) :
(X ^ p ^ n - X : polynomial K') ≠ 0 :=
X_pow_card_sub_X_ne_zero K' $ nat.one_lt_pow _ _ (nat.pos_of_ne_zero hn) hp
end
variables (p : ℕ) [fact p.prime] [char_p K p]
lemma roots_X_pow_card_sub_X : roots (X^q - X : polynomial K) = finset.univ.val :=
begin
classical,
have aux : (X^q - X : polynomial K) ≠ 0 := X_pow_card_sub_X_ne_zero K fintype.one_lt_card,
have : (roots (X^q - X : polynomial K)).to_finset = finset.univ,
{ rw eq_univ_iff_forall,
intro x,
rw [multiset.mem_to_finset, mem_roots aux, is_root.def, eval_sub, eval_pow, eval_X, sub_eq_zero,
pow_card] },
rw [←this, multiset.to_finset_val, eq_comm, multiset.erase_dup_eq_self],
apply nodup_roots,
rw separable_def,
convert is_coprime_one_right.neg_right,
rw [derivative_sub, derivative_X, derivative_X_pow, ←C_eq_nat_cast,
C_eq_zero.mpr (char_p.cast_card_eq_zero K), zero_mul, zero_sub],
end
instance : is_splitting_field (zmod p) K (X^q - X) :=
{ splits :=
begin
have h : (X^q - X : polynomial K).nat_degree = q :=
X_pow_card_sub_X_nat_degree_eq K fintype.one_lt_card,
rw [←splits_id_iff_splits, splits_iff_card_roots, map_sub, map_pow, map_X, h,
roots_X_pow_card_sub_X K, ←finset.card_def, finset.card_univ],
end,
adjoin_roots :=
begin
classical,
transitivity algebra.adjoin (zmod p) ((roots (X^q - X : polynomial K)).to_finset : set K),
{ simp only [map_pow, map_X, map_sub], convert rfl },
{ rw [roots_X_pow_card_sub_X, val_to_finset, coe_univ, algebra.adjoin_univ], }
end }
end is_splitting_field
variables {K}
theorem frobenius_pow {p : ℕ} [fact p.prime] [char_p K p] {n : ℕ} (hcard : q = p^n) :
(frobenius K p) ^ n = 1 :=
begin
ext, conv_rhs { rw [ring_hom.one_def, ring_hom.id_apply, ← pow_card x, hcard], }, clear hcard,
induction n, {simp},
rw [pow_succ, pow_succ', pow_mul, ring_hom.mul_def, ring_hom.comp_apply, frobenius_def, n_ih]
end
open polynomial
lemma expand_card (f : polynomial K) :
expand K q f = f ^ q :=
begin
cases char_p.exists K with p hp, letI := hp,
rcases finite_field.card K p with ⟨⟨n, npos⟩, ⟨hp, hn⟩⟩, haveI : fact p.prime := ⟨hp⟩,
dsimp at hn, rw hn at *,
rw ← map_expand_pow_char,
rw [frobenius_pow hn, ring_hom.one_def, map_id],
end
end finite_field
namespace zmod
open finite_field polynomial
lemma sq_add_sq (p : ℕ) [hp : fact p.prime] (x : zmod p) :
∃ a b : zmod p, a^2 + b^2 = x :=
begin
cases hp.1.eq_two_or_odd with hp2 hp_odd,
{ substI p, change fin 2 at x, fin_cases x, { use 0, simp }, { use [0, 1], simp } },
let f : polynomial (zmod p) := X^2,
let g : polynomial (zmod p) := X^2 - C x,
obtain ⟨a, b, hab⟩ : ∃ a b, f.eval a + g.eval b = 0 :=
@exists_root_sum_quadratic _ _ _ _ f g
(degree_X_pow 2) (degree_X_pow_sub_C dec_trivial _) (by rw [zmod.card, hp_odd]),
refine ⟨a, b, _⟩,
rw ← sub_eq_zero,
simpa only [eval_C, eval_X, eval_pow, eval_sub, ← add_sub_assoc] using hab,
end
end zmod
namespace char_p
lemma sq_add_sq (R : Type*) [comm_ring R] [is_domain R]
(p : ℕ) [fact (0 < p)] [char_p R p] (x : ℤ) :
∃ a b : ℕ, (a^2 + b^2 : R) = x :=
begin
haveI := char_is_prime_of_pos R p,
obtain ⟨a, b, hab⟩ := zmod.sq_add_sq p x,
refine ⟨a.val, b.val, _⟩,
simpa using congr_arg (zmod.cast_hom dvd_rfl R) hab
end
end char_p
open_locale nat
open zmod
/-- The **Fermat-Euler totient theorem**. `nat.modeq.pow_totient` is an alternative statement
of the same theorem. -/
@[simp] lemma zmod.pow_totient {n : ℕ} [fact (0 < n)] (x : units (zmod n)) : x ^ φ n = 1 :=
by rw [← card_units_eq_totient, pow_card_eq_one]
/-- The **Fermat-Euler totient theorem**. `zmod.pow_totient` is an alternative statement
of the same theorem. -/
lemma nat.modeq.pow_totient {x n : ℕ} (h : nat.coprime x n) : x ^ φ n ≡ 1 [MOD n] :=
begin
cases n, {simp},
rw ← zmod.eq_iff_modeq_nat,
let x' : units (zmod (n+1)) := zmod.unit_of_coprime _ h,
have := zmod.pow_totient x',
apply_fun (coe : units (zmod (n+1)) → zmod (n+1)) at this,
simpa only [-zmod.pow_totient, nat.succ_eq_add_one, nat.cast_pow, units.coe_one,
nat.cast_one, coe_unit_of_coprime, units.coe_pow],
end
section
variables {V : Type*} [fintype K] [division_ring K] [add_comm_group V] [module K V]
-- should this go in a namespace?
-- finite_dimensional would be natural,
-- but we don't assume it...
lemma card_eq_pow_finrank [fintype V] :
fintype.card V = q ^ (finite_dimensional.finrank K V) :=
begin
let b := is_noetherian.finset_basis K V,
rw [module.card_fintype b, ← finite_dimensional.finrank_eq_card_basis b],
end
end
open finite_field
namespace zmod
/-- A variation on Fermat's little theorem. See `zmod.pow_card_sub_one_eq_one` -/
@[simp] lemma pow_card {p : ℕ} [fact p.prime] (x : zmod p) : x ^ p = x :=
by { have h := finite_field.pow_card x, rwa zmod.card p at h }
@[simp] lemma pow_card_pow {n p : ℕ} [fact p.prime] (x : zmod p) : x ^ p ^ n = x :=
begin
induction n with n ih,
{ simp, },
{ simp [pow_succ, pow_mul, ih, pow_card], },
end
@[simp] lemma frobenius_zmod (p : ℕ) [fact p.prime] :
frobenius (zmod p) p = ring_hom.id _ :=
by { ext a, rw [frobenius_def, zmod.pow_card, ring_hom.id_apply] }
@[simp] lemma card_units (p : ℕ) [fact p.prime] : fintype.card (units (zmod p)) = p - 1 :=
by rw [fintype.card_units, card]
/-- **Fermat's Little Theorem**: for every unit `a` of `zmod p`, we have `a ^ (p - 1) = 1`. -/
theorem units_pow_card_sub_one_eq_one (p : ℕ) [fact p.prime] (a : units (zmod p)) :
a ^ (p - 1) = 1 :=
by rw [← card_units p, pow_card_eq_one]
/-- **Fermat's Little Theorem**: for all nonzero `a : zmod p`, we have `a ^ (p - 1) = 1`. -/
theorem pow_card_sub_one_eq_one {p : ℕ} [fact p.prime] {a : zmod p} (ha : a ≠ 0) :
a ^ (p - 1) = 1 :=
by { have h := pow_card_sub_one_eq_one a ha, rwa zmod.card p at h }
open polynomial
lemma expand_card {p : ℕ} [fact p.prime] (f : polynomial (zmod p)) :
expand (zmod p) p f = f ^ p :=
by { have h := finite_field.expand_card f, rwa zmod.card p at h }
end zmod
|
423ec1a8adacd9bb24bc8f973198edebf13c92f0 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/data/complex/exponential.lean | 21c7ea846b9e90aacfb945cf7c84f26b5c438db3 | [
"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 | 67,266 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Abhimanyu Pallavi Sudhir
-/
import algebra.geom_sum
import data.complex.basic
import data.nat.choose.sum
/-!
# Exponential, trigonometric and hyperbolic trigonometric functions
This file contains the definitions of the real and complex exponential, sine, cosine, tangent,
hyperbolic sine, hyperbolic cosine, and hyperbolic tangent functions.
-/
local notation `abs'` := has_abs.abs
open is_absolute_value
open_locale classical big_operators nat complex_conjugate
section
open real is_absolute_value finset
section
variables {α : Type*} {β : Type*} [ring β]
[linear_ordered_field α] [archimedean α] {abv : β → α} [is_absolute_value abv]
lemma is_cau_of_decreasing_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, |f n| ≤ a)
(hnm : ∀ n ≥ m, f n.succ ≤ f n) : is_cau_seq abs f :=
λ ε ε0,
let ⟨k, hk⟩ := archimedean.arch a ε0 in
have h : ∃ l, ∀ n ≥ m, a - l • ε < f n :=
⟨k + k + 1, λ n hnm, lt_of_lt_of_le
(show a - (k + (k + 1)) • ε < -|f n|,
from lt_neg.1 $ lt_of_le_of_lt (ham n hnm) (begin
rw [neg_sub, lt_sub_iff_add_lt, add_nsmul, add_nsmul, one_nsmul],
exact add_lt_add_of_le_of_lt hk (lt_of_le_of_lt hk
(lt_add_of_pos_right _ ε0)),
end))
(neg_le.2 $ (abs_neg (f n)) ▸ le_abs_self _)⟩,
let l := nat.find h in
have hl : ∀ (n : ℕ), n ≥ m → f n > a - l • ε := nat.find_spec h,
have hl0 : l ≠ 0 := λ hl0, not_lt_of_ge (ham m le_rfl)
(lt_of_lt_of_le (by have := hl m (le_refl m); simpa [hl0] using this) (le_abs_self (f m))),
begin
cases not_forall.1
(nat.find_min h (nat.pred_lt hl0)) with i hi,
rw [not_imp, not_lt] at hi,
existsi i,
assume j hj,
have hfij : f j ≤ f i := (nat.rel_of_forall_rel_succ_of_le_of_le (≥) hnm hi.1 hj).le,
rw [abs_of_nonpos (sub_nonpos.2 hfij), neg_sub, sub_lt_iff_lt_add'],
calc f i ≤ a - (nat.pred l) • ε : hi.2
... = a - l • ε + ε :
by conv {to_rhs, rw [← nat.succ_pred_eq_of_pos (nat.pos_of_ne_zero hl0), succ_nsmul',
sub_add, add_sub_cancel] }
... < f j + ε : add_lt_add_right (hl j (le_trans hi.1 hj)) _
end
lemma is_cau_of_mono_bounded (f : ℕ → α) {a : α} {m : ℕ} (ham : ∀ n ≥ m, |f n| ≤ a)
(hnm : ∀ n ≥ m, f n ≤ f n.succ) : is_cau_seq abs f :=
begin
refine @eq.rec_on (ℕ → α) _ (is_cau_seq abs) _ _
(-⟨_, @is_cau_of_decreasing_bounded _ _ _ (λ n, -f n) a m (by simpa) (by simpa)⟩ :
cau_seq α abs).2,
ext,
exact neg_neg _
end
end
section no_archimedean
variables {α : Type*} {β : Type*} [ring β]
[linear_ordered_field α] {abv : β → α} [is_absolute_value abv]
lemma is_cau_series_of_abv_le_cau {f : ℕ → β} {g : ℕ → α} (n : ℕ) :
(∀ m, n ≤ m → abv (f m) ≤ g m) →
is_cau_seq abs (λ n, ∑ i in range n, g i) →
is_cau_seq abv (λ n, ∑ i in range n, f i) :=
begin
assume hm hg ε ε0,
cases hg (ε / 2) (div_pos ε0 (by norm_num)) with i hi,
existsi max n i,
assume j ji,
have hi₁ := hi j (le_trans (le_max_right n i) ji),
have hi₂ := hi (max n i) (le_max_right n i),
have sub_le := abs_sub_le (∑ k in range j, g k) (∑ k in range i, g k)
(∑ k in range (max n i), g k),
have := add_lt_add hi₁ hi₂,
rw [abs_sub_comm (∑ k in range (max n i), g k), add_halves ε] at this,
refine lt_of_le_of_lt (le_trans (le_trans _ (le_abs_self _)) sub_le) this,
generalize hk : j - max n i = k,
clear this hi₂ hi₁ hi ε0 ε hg sub_le,
rw tsub_eq_iff_eq_add_of_le ji at hk,
rw hk,
clear hk ji j,
induction k with k' hi,
{ simp [abv_zero abv] },
{ simp only [nat.succ_add, sum_range_succ_comm, sub_eq_add_neg, add_assoc],
refine le_trans (abv_add _ _ _) _,
simp only [sub_eq_add_neg] at hi,
exact add_le_add (hm _ (le_add_of_nonneg_of_le (nat.zero_le _) (le_max_left _ _))) hi },
end
lemma is_cau_series_of_abv_cau {f : ℕ → β} : is_cau_seq abs (λ m, ∑ n in range m, abv (f n))
→ is_cau_seq abv (λ m, ∑ n in range m, f n) :=
is_cau_series_of_abv_le_cau 0 (λ n h, le_rfl)
end no_archimedean
section
variables {α : Type*} [linear_ordered_field α] [archimedean α]
lemma is_cau_geo_series {β : Type*} [ring β] [nontrivial β] {abv : β → α} [is_absolute_value abv]
(x : β) (hx1 : abv x < 1) : is_cau_seq abv (λ n, ∑ m in range n, x ^ m) :=
have hx1' : abv x ≠ 1 := λ h, by simpa [h, lt_irrefl] using hx1,
is_cau_series_of_abv_cau
begin
simp only [abv_pow abv, geom_sum_eq hx1'],
conv in (_ / _) { rw [← neg_div_neg_eq, neg_sub, neg_sub] },
refine @is_cau_of_mono_bounded _ _ _ _ ((1 : α) / (1 - abv x)) 0 _ _,
{ assume n hn,
rw abs_of_nonneg,
refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1)
(sub_le_self _ (abv_pow abv x n ▸ abv_nonneg _ _)),
refine div_nonneg (sub_nonneg.2 _) (sub_nonneg.2 $ le_of_lt hx1),
clear hn,
induction n with n ih,
{ simp },
{ rw [pow_succ, ← one_mul (1 : α)],
refine mul_le_mul (le_of_lt hx1) ih (abv_pow abv x n ▸ abv_nonneg _ _) (by norm_num) } },
{ assume n hn,
refine div_le_div_of_le (le_of_lt $ sub_pos.2 hx1) (sub_le_sub_left _ _),
rw [← one_mul (_ ^ n), pow_succ],
exact mul_le_mul_of_nonneg_right (le_of_lt hx1) (pow_nonneg (abv_nonneg _ _) _) }
end
lemma is_cau_geo_series_const (a : α) {x : α} (hx1 : |x| < 1) :
is_cau_seq abs (λ m, ∑ n in range m, a * x ^ n) :=
have is_cau_seq abs (λ m, a * ∑ n in range m, x ^ n) :=
(cau_seq.const abs a * ⟨_, is_cau_geo_series x hx1⟩).2,
by simpa only [mul_sum]
variables {β : Type*} [ring β] {abv : β → α} [is_absolute_value abv]
lemma series_ratio_test {f : ℕ → β} (n : ℕ) (r : α)
(hr0 : 0 ≤ r) (hr1 : r < 1) (h : ∀ m, n ≤ m → abv (f m.succ) ≤ r * abv (f m)) :
is_cau_seq abv (λ m, ∑ n in range m, f n) :=
have har1 : |r| < 1, by rwa abs_of_nonneg hr0,
begin
refine is_cau_series_of_abv_le_cau n.succ _
(is_cau_geo_series_const (abv (f n.succ) * r⁻¹ ^ n.succ) har1),
assume m hmn,
cases classical.em (r = 0) with r_zero r_ne_zero,
{ have m_pos := lt_of_lt_of_le (nat.succ_pos n) hmn,
have := h m.pred (nat.le_of_succ_le_succ (by rwa [nat.succ_pred_eq_of_pos m_pos])),
simpa [r_zero, nat.succ_pred_eq_of_pos m_pos, pow_succ] },
generalize hk : m - n.succ = k,
have r_pos : 0 < r := lt_of_le_of_ne hr0 (ne.symm r_ne_zero),
replace hk : m = k + n.succ := (tsub_eq_iff_eq_add_of_le hmn).1 hk,
induction k with k ih generalizing m n,
{ rw [hk, zero_add, mul_right_comm, inv_pow _ _, ← div_eq_mul_inv, mul_div_cancel],
exact (ne_of_lt (pow_pos r_pos _)).symm },
{ have kn : k + n.succ ≥ n.succ, by rw ← zero_add n.succ; exact add_le_add (zero_le _) (by simp),
rw [hk, nat.succ_add, pow_succ' r, ← mul_assoc],
exact le_trans (by rw mul_comm; exact h _ (nat.le_of_succ_le kn))
(mul_le_mul_of_nonneg_right (ih (k + n.succ) n h kn rfl) hr0) }
end
lemma sum_range_diag_flip {α : Type*} [add_comm_monoid α] (n : ℕ) (f : ℕ → ℕ → α) :
∑ m in range n, ∑ k in range (m + 1), f k (m - k) =
∑ m in range n, ∑ k in range (n - m), f m k :=
by rw [sum_sigma', sum_sigma']; exact sum_bij
(λ a _, ⟨a.2, a.1 - a.2⟩)
(λ a ha, have h₁ : a.1 < n := mem_range.1 (mem_sigma.1 ha).1,
have h₂ : a.2 < nat.succ a.1 := mem_range.1 (mem_sigma.1 ha).2,
mem_sigma.2 ⟨mem_range.2 (lt_of_lt_of_le h₂ h₁),
mem_range.2 ((tsub_lt_tsub_iff_right (nat.le_of_lt_succ h₂)).2 h₁)⟩)
(λ _ _, rfl)
(λ ⟨a₁, a₂⟩ ⟨b₁, b₂⟩ ha hb h,
have ha : a₁ < n ∧ a₂ ≤ a₁ :=
⟨mem_range.1 (mem_sigma.1 ha).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 ha).2)⟩,
have hb : b₁ < n ∧ b₂ ≤ b₁ :=
⟨mem_range.1 (mem_sigma.1 hb).1, nat.le_of_lt_succ (mem_range.1 (mem_sigma.1 hb).2)⟩,
have h : a₂ = b₂ ∧ _ := sigma.mk.inj h,
have h' : a₁ = b₁ - b₂ + a₂ := (tsub_eq_iff_eq_add_of_le ha.2).1 (eq_of_heq h.2),
sigma.mk.inj_iff.2
⟨tsub_add_cancel_of_le hb.2 ▸ h'.symm ▸ h.1 ▸ rfl,
(heq_of_eq h.1)⟩)
(λ ⟨a₁, a₂⟩ ha,
have ha : a₁ < n ∧ a₂ < n - a₁ :=
⟨mem_range.1 (mem_sigma.1 ha).1, (mem_range.1 (mem_sigma.1 ha).2)⟩,
⟨⟨a₂ + a₁, a₁⟩, ⟨mem_sigma.2 ⟨mem_range.2 (lt_tsub_iff_right.1 ha.2),
mem_range.2 (nat.lt_succ_of_le (nat.le_add_left _ _))⟩,
sigma.mk.inj_iff.2 ⟨rfl, heq_of_eq (add_tsub_cancel_right _ _).symm⟩⟩⟩)
end
section no_archimedean
variables {α : Type*} {β : Type*} [linear_ordered_field α] {abv : β → α}
section
variables [semiring β] [is_absolute_value abv]
lemma abv_sum_le_sum_abv {γ : Type*} (f : γ → β) (s : finset γ) :
abv (∑ k in s, f k) ≤ ∑ k in s, abv (f k) :=
by haveI := classical.dec_eq γ; exact
finset.induction_on s (by simp [abv_zero abv])
(λ a s has ih, by rw [sum_insert has, sum_insert has];
exact le_trans (abv_add abv _ _) (add_le_add_left ih _))
end
section
variables [ring β] [is_absolute_value abv]
lemma cauchy_product {a b : ℕ → β}
(ha : is_cau_seq abs (λ m, ∑ n in range m, abv (a n)))
(hb : is_cau_seq abv (λ m, ∑ n in range m, b n)) (ε : α) (ε0 : 0 < ε) :
∃ i : ℕ, ∀ j ≥ i, abv ((∑ k in range j, a k) * (∑ k in range j, b k) -
∑ n in range j, ∑ m in range (n + 1), a m * b (n - m)) < ε :=
let ⟨Q, hQ⟩ := cau_seq.bounded ⟨_, hb⟩ in
let ⟨P, hP⟩ := cau_seq.bounded ⟨_, ha⟩ in
have hP0 : 0 < P, from lt_of_le_of_lt (abs_nonneg _) (hP 0),
have hPε0 : 0 < ε / (2 * P),
from div_pos ε0 (mul_pos (show (2 : α) > 0, from by norm_num) hP0),
let ⟨N, hN⟩ := cau_seq.cauchy₂ ⟨_, hb⟩ hPε0 in
have hQε0 : 0 < ε / (4 * Q),
from div_pos ε0 (mul_pos (show (0 : α) < 4, by norm_num)
(lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))),
let ⟨M, hM⟩ := cau_seq.cauchy₂ ⟨_, ha⟩ hQε0 in
⟨2 * (max N M + 1), λ K hK,
have h₁ : ∑ m in range K, ∑ k in range (m + 1), a k * b (m - k) =
∑ m in range K, ∑ n in range (K - m), a m * b n,
by simpa using sum_range_diag_flip K (λ m n, a m * b n),
have h₂ : (λ i, ∑ k in range (K - i), a i * b k) = (λ i, a i * ∑ k in range (K - i), b k),
by simp [finset.mul_sum],
have h₃ : ∑ i in range K, a i * ∑ k in range (K - i), b k =
∑ i in range K, a i * (∑ k in range (K - i), b k - ∑ k in range K, b k)
+ ∑ i in range K, a i * ∑ k in range K, b k,
by rw ← sum_add_distrib; simp [(mul_add _ _ _).symm],
have two_mul_two : (4 : α) = 2 * 2, by norm_num,
have hQ0 : Q ≠ 0, from λ h, by simpa [h, lt_irrefl] using hQε0,
have h2Q0 : 2 * Q ≠ 0, from mul_ne_zero two_ne_zero hQ0,
have hε : ε / (2 * P) * P + ε / (4 * Q) * (2 * Q) = ε,
by rw [← div_div, div_mul_cancel _ (ne.symm (ne_of_lt hP0)),
two_mul_two, mul_assoc, ← div_div, div_mul_cancel _ h2Q0, add_halves],
have hNMK : max N M + 1 < K,
from lt_of_lt_of_le (by rw two_mul; exact lt_add_of_pos_left _ (nat.succ_pos _)) hK,
have hKN : N < K,
from calc N ≤ max N M : le_max_left _ _
... < max N M + 1 : nat.lt_succ_self _
... < K : hNMK,
have hsumlesum : ∑ i in range (max N M + 1), abv (a i) *
abv (∑ k in range (K - i), b k - ∑ k in range K, b k) ≤
∑ i in range (max N M + 1), abv (a i) * (ε / (2 * P)),
from sum_le_sum (λ m hmJ, mul_le_mul_of_nonneg_left
(le_of_lt (hN (K - m)
(le_tsub_of_add_le_left (le_trans
(by rw two_mul; exact add_le_add (le_of_lt (mem_range.1 hmJ))
(le_trans (le_max_left _ _) (le_of_lt (lt_add_one _)))) hK)) K
(le_of_lt hKN))) (abv_nonneg abv _)),
have hsumltP : ∑ n in range (max N M + 1), abv (a n) < P :=
calc ∑ n in range (max N M + 1), abv (a n)
= |∑ n in range (max N M + 1), abv (a n)| :
eq.symm (abs_of_nonneg (sum_nonneg (λ x h, abv_nonneg abv (a x))))
... < P : hP (max N M + 1),
begin
rw [h₁, h₂, h₃, sum_mul, ← sub_sub, sub_right_comm, sub_self, zero_sub, abv_neg abv],
refine lt_of_le_of_lt (abv_sum_le_sum_abv _ _) _,
suffices : ∑ i in range (max N M + 1),
abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) +
(∑ i in range K, abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k) -
∑ i in range (max N M + 1), abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k)) <
ε / (2 * P) * P + ε / (4 * Q) * (2 * Q),
{ rw hε at this, simpa [abv_mul abv] },
refine add_lt_add (lt_of_le_of_lt hsumlesum
(by rw [← sum_mul, mul_comm]; exact (mul_lt_mul_left hPε0).mpr hsumltP)) _,
rw sum_range_sub_sum_range (le_of_lt hNMK),
calc ∑ i in (range K).filter (λ k, max N M + 1 ≤ k),
abv (a i) * abv (∑ k in range (K - i), b k - ∑ k in range K, b k)
≤ ∑ i in (range K).filter (λ k, max N M + 1 ≤ k), abv (a i) * (2 * Q) :
sum_le_sum (λ n hn, begin
refine mul_le_mul_of_nonneg_left _ (abv_nonneg _ _),
rw sub_eq_add_neg,
refine le_trans (abv_add _ _ _) _,
rw [two_mul, abv_neg abv],
exact add_le_add (le_of_lt (hQ _)) (le_of_lt (hQ _)),
end)
... < ε / (4 * Q) * (2 * Q) :
by rw [← sum_mul, ← sum_range_sub_sum_range (le_of_lt hNMK)];
refine (mul_lt_mul_right $ by rw two_mul;
exact add_pos (lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))
(lt_of_le_of_lt (abv_nonneg _ _) (hQ 0))).2
(lt_of_le_of_lt (le_abs_self _)
(hM _ (le_trans (nat.le_succ_of_le (le_max_right _ _)) (le_of_lt hNMK)) _
(nat.le_succ_of_le (le_max_right _ _))))
end⟩
end
end no_archimedean
end
open finset
open cau_seq
namespace complex
lemma is_cau_abs_exp (z : ℂ) : is_cau_seq has_abs.abs
(λ n, ∑ m in range n, abs (z ^ m / m!)) :=
let ⟨n, hn⟩ := exists_nat_gt (abs z) in
have hn0 : (0 : ℝ) < n, from lt_of_le_of_lt (abs.nonneg _) hn,
series_ratio_test n (complex.abs z / n) (div_nonneg (abs.nonneg _) (le_of_lt hn0))
(by rwa [div_lt_iff hn0, one_mul])
(λ m hm,
by rw [abs_abs, abs_abs, nat.factorial_succ, pow_succ,
mul_comm m.succ, nat.cast_mul, ← div_div, mul_div_assoc,
mul_div_right_comm, abs.map_mul, map_div₀, abs_cast_nat];
exact mul_le_mul_of_nonneg_right
(div_le_div_of_le_left (abs.nonneg _) hn0
(nat.cast_le.2 (le_trans hm (nat.le_succ _)))) (abs.nonneg _))
noncomputable theory
lemma is_cau_exp (z : ℂ) :
is_cau_seq abs (λ n, ∑ m in range n, z ^ m / m!) :=
is_cau_series_of_abv_cau (is_cau_abs_exp z)
/-- The Cauchy sequence consisting of partial sums of the Taylor series of
the complex exponential function -/
@[pp_nodot] def exp' (z : ℂ) :
cau_seq ℂ complex.abs :=
⟨λ n, ∑ m in range n, z ^ m / m!, is_cau_exp z⟩
/-- The complex exponential function, defined via its Taylor series -/
@[pp_nodot] def exp (z : ℂ) : ℂ := lim (exp' z)
/-- The complex sine function, defined via `exp` -/
@[pp_nodot] def sin (z : ℂ) : ℂ := ((exp (-z * I) - exp (z * I)) * I) / 2
/-- The complex cosine function, defined via `exp` -/
@[pp_nodot] def cos (z : ℂ) : ℂ := (exp (z * I) + exp (-z * I)) / 2
/-- The complex tangent function, defined as `sin z / cos z` -/
@[pp_nodot] def tan (z : ℂ) : ℂ := sin z / cos z
/-- The complex hyperbolic sine function, defined via `exp` -/
@[pp_nodot] def sinh (z : ℂ) : ℂ := (exp z - exp (-z)) / 2
/-- The complex hyperbolic cosine function, defined via `exp` -/
@[pp_nodot] def cosh (z : ℂ) : ℂ := (exp z + exp (-z)) / 2
/-- The complex hyperbolic tangent function, defined as `sinh z / cosh z` -/
@[pp_nodot] def tanh (z : ℂ) : ℂ := sinh z / cosh z
end complex
namespace real
open complex
/-- The real exponential function, defined as the real part of the complex exponential -/
@[pp_nodot] def exp (x : ℝ) : ℝ := (exp x).re
/-- The real sine function, defined as the real part of the complex sine -/
@[pp_nodot] def sin (x : ℝ) : ℝ := (sin x).re
/-- The real cosine function, defined as the real part of the complex cosine -/
@[pp_nodot] def cos (x : ℝ) : ℝ := (cos x).re
/-- The real tangent function, defined as the real part of the complex tangent -/
@[pp_nodot] def tan (x : ℝ) : ℝ := (tan x).re
/-- The real hypebolic sine function, defined as the real part of the complex hyperbolic sine -/
@[pp_nodot] def sinh (x : ℝ) : ℝ := (sinh x).re
/-- The real hypebolic cosine function, defined as the real part of the complex hyperbolic cosine -/
@[pp_nodot] def cosh (x : ℝ) : ℝ := (cosh x).re
/-- The real hypebolic tangent function, defined as the real part of
the complex hyperbolic tangent -/
@[pp_nodot] def tanh (x : ℝ) : ℝ := (tanh x).re
end real
namespace complex
variables (x y : ℂ)
@[simp] lemma exp_zero : exp 0 = 1 :=
lim_eq_of_equiv_const $
λ ε ε0, ⟨1, λ j hj, begin
convert ε0,
cases j,
{ exact absurd hj (not_le_of_gt zero_lt_one) },
{ dsimp [exp'],
induction j with j ih,
{ dsimp [exp']; simp },
{ rw ← ih dec_trivial,
simp only [sum_range_succ, pow_succ],
simp } }
end⟩
lemma exp_add : exp (x + y) = exp x * exp y :=
show lim (⟨_, is_cau_exp (x + y)⟩ : cau_seq ℂ abs) =
lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp x⟩)
* lim (show cau_seq ℂ abs, from ⟨_, is_cau_exp y⟩),
from
have hj : ∀ j : ℕ, ∑ m in range j, (x + y) ^ m / m! =
∑ i in range j, ∑ k in range (i + 1), x ^ k / k! * (y ^ (i - k) / (i - k)!),
from assume j,
finset.sum_congr rfl (λ m hm, begin
rw [add_pow, div_eq_mul_inv, sum_mul],
refine finset.sum_congr rfl (λ i hi, _),
have h₁ : (m.choose i : ℂ) ≠ 0 := nat.cast_ne_zero.2
(pos_iff_ne_zero.1 (nat.choose_pos (nat.le_of_lt_succ (mem_range.1 hi)))),
have h₂ := nat.choose_mul_factorial_mul_factorial (nat.le_of_lt_succ $ finset.mem_range.1 hi),
rw [← h₂, nat.cast_mul, nat.cast_mul, mul_inv, mul_inv],
simp only [mul_left_comm (m.choose i : ℂ), mul_assoc, mul_left_comm (m.choose i : ℂ)⁻¹,
mul_comm (m.choose i : ℂ)],
rw inv_mul_cancel h₁,
simp [div_eq_mul_inv, mul_comm, mul_assoc, mul_left_comm]
end),
by rw lim_mul_lim;
exact eq.symm (lim_eq_lim_of_equiv (by dsimp; simp only [hj];
exact cauchy_product (is_cau_abs_exp x) (is_cau_exp y)))
attribute [irreducible] complex.exp
lemma exp_list_sum (l : list ℂ) : exp l.sum = (l.map exp).prod :=
@monoid_hom.map_list_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ l
lemma exp_multiset_sum (s : multiset ℂ) : exp s.sum = (s.map exp).prod :=
@monoid_hom.map_multiset_prod (multiplicative ℂ) ℂ _ _ ⟨exp, exp_zero, exp_add⟩ s
lemma exp_sum {α : Type*} (s : finset α) (f : α → ℂ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) :=
@monoid_hom.map_prod (multiplicative ℂ) α ℂ _ _ ⟨exp, exp_zero, exp_add⟩ f s
lemma exp_nat_mul (x : ℂ) : ∀ n : ℕ, exp(n*x) = (exp x)^n
| 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero]
| (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul]
lemma exp_ne_zero : exp x ≠ 0 :=
λ h, zero_ne_one $ by rw [← exp_zero, ← add_neg_self x, exp_add, h]; simp
lemma exp_neg : exp (-x) = (exp x)⁻¹ :=
by rw [← mul_right_inj' (exp_ne_zero x), ← exp_add];
simp [mul_inv_cancel (exp_ne_zero x)]
lemma exp_sub : exp (x - y) = exp x / exp y :=
by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv]
lemma exp_int_mul (z : ℂ) (n : ℤ) : complex.exp (n * z) = (complex.exp z) ^ n :=
begin
cases n,
{ apply complex.exp_nat_mul },
{ simpa [complex.exp_neg, add_comm, ← neg_mul]
using complex.exp_nat_mul (-z) (1 + n) },
end
@[simp] lemma exp_conj : exp (conj x) = conj (exp x) :=
begin
dsimp [exp],
rw [← lim_conj],
refine congr_arg lim (cau_seq.ext (λ _, _)),
dsimp [exp', function.comp, cau_seq_conj],
rw (star_ring_end _).map_sum,
refine sum_congr rfl (λ n hn, _),
rw [map_div₀, map_pow, ← of_real_nat_cast, conj_of_real]
end
@[simp] lemma of_real_exp_of_real_re (x : ℝ) : ((exp x).re : ℂ) = exp x :=
eq_conj_iff_re.1 $ by rw [← exp_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_exp (x : ℝ) : (real.exp x : ℂ) = exp x :=
of_real_exp_of_real_re _
@[simp] lemma exp_of_real_im (x : ℝ) : (exp x).im = 0 :=
by rw [← of_real_exp_of_real_re, of_real_im]
lemma exp_of_real_re (x : ℝ) : (exp x).re = real.exp x := rfl
lemma two_sinh : 2 * sinh x = exp x - exp (-x) :=
mul_div_cancel' _ two_ne_zero
lemma two_cosh : 2 * cosh x = exp x + exp (-x) :=
mul_div_cancel' _ two_ne_zero
@[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh]
@[simp] lemma sinh_neg : sinh (-x) = -sinh x :=
by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul]
private lemma sinh_add_aux {a b c d : ℂ} :
(a - b) * (c + d) + (a + b) * (c - d) = 2 * (a * c - b * d) := by ring
lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y :=
begin
rw [← mul_right_inj' (two_ne_zero' ℂ), two_sinh,
exp_add, neg_add, exp_add, eq_comm,
mul_add, ← mul_assoc, two_sinh, mul_left_comm, two_sinh,
← mul_right_inj' (two_ne_zero' ℂ), mul_add,
mul_left_comm, two_cosh, ← mul_assoc, two_cosh],
exact sinh_add_aux
end
@[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh]
@[simp] lemma cosh_neg : cosh (-x) = cosh x :=
by simp [add_comm, cosh, exp_neg]
private lemma cosh_add_aux {a b c d : ℂ} :
(a + b) * (c + d) + (a - b) * (c - d) = 2 * (a * c + b * d) := by ring
lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y :=
begin
rw [← mul_right_inj' (two_ne_zero' ℂ), two_cosh,
exp_add, neg_add, exp_add, eq_comm,
mul_add, ← mul_assoc, two_cosh, ← mul_assoc, two_sinh,
← mul_right_inj' (two_ne_zero' ℂ), mul_add,
mul_left_comm, two_cosh, mul_left_comm, two_sinh],
exact cosh_add_aux
end
lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y :=
by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg]
lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y :=
by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg]
lemma sinh_conj : sinh (conj x) = conj (sinh x) :=
by rw [sinh, ← ring_hom.map_neg, exp_conj, exp_conj, ← ring_hom.map_sub, sinh,
map_div₀, conj_bit0, ring_hom.map_one]
@[simp] lemma of_real_sinh_of_real_re (x : ℝ) : ((sinh x).re : ℂ) = sinh x :=
eq_conj_iff_re.1 $ by rw [← sinh_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_sinh (x : ℝ) : (real.sinh x : ℂ) = sinh x :=
of_real_sinh_of_real_re _
@[simp] lemma sinh_of_real_im (x : ℝ) : (sinh x).im = 0 :=
by rw [← of_real_sinh_of_real_re, of_real_im]
lemma sinh_of_real_re (x : ℝ) : (sinh x).re = real.sinh x := rfl
lemma cosh_conj : cosh (conj x) = conj (cosh x) :=
begin
rw [cosh, ← ring_hom.map_neg, exp_conj, exp_conj, ← ring_hom.map_add, cosh,
map_div₀, conj_bit0, ring_hom.map_one]
end
lemma of_real_cosh_of_real_re (x : ℝ) : ((cosh x).re : ℂ) = cosh x :=
eq_conj_iff_re.1 $ by rw [← cosh_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_cosh (x : ℝ) : (real.cosh x : ℂ) = cosh x :=
of_real_cosh_of_real_re _
@[simp] lemma cosh_of_real_im (x : ℝ) : (cosh x).im = 0 :=
by rw [← of_real_cosh_of_real_re, of_real_im]
@[simp] lemma cosh_of_real_re (x : ℝ) : (cosh x).re = real.cosh x := rfl
lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x := rfl
@[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh]
@[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div]
lemma tanh_conj : tanh (conj x) = conj (tanh x) :=
by rw [tanh, sinh_conj, cosh_conj, ← map_div₀, tanh]
@[simp] lemma of_real_tanh_of_real_re (x : ℝ) : ((tanh x).re : ℂ) = tanh x :=
eq_conj_iff_re.1 $ by rw [← tanh_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_tanh (x : ℝ) : (real.tanh x : ℂ) = tanh x :=
of_real_tanh_of_real_re _
@[simp] lemma tanh_of_real_im (x : ℝ) : (tanh x).im = 0 :=
by rw [← of_real_tanh_of_real_re, of_real_im]
lemma tanh_of_real_re (x : ℝ) : (tanh x).re = real.tanh x := rfl
@[simp] lemma cosh_add_sinh : cosh x + sinh x = exp x :=
by rw [← mul_right_inj' (two_ne_zero' ℂ), mul_add,
two_cosh, two_sinh, add_add_sub_cancel, two_mul]
@[simp] lemma sinh_add_cosh : sinh x + cosh x = exp x :=
by rw [add_comm, cosh_add_sinh]
@[simp] lemma exp_sub_cosh : exp x - cosh x = sinh x :=
sub_eq_iff_eq_add.2 (sinh_add_cosh x).symm
@[simp] lemma exp_sub_sinh : exp x - sinh x = cosh x :=
sub_eq_iff_eq_add.2 (cosh_add_sinh x).symm
@[simp] lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) :=
by rw [← mul_right_inj' (two_ne_zero' ℂ), mul_sub,
two_cosh, two_sinh, add_sub_sub_cancel, two_mul]
@[simp] lemma sinh_sub_cosh : sinh x - cosh x = -exp (-x) :=
by rw [← neg_sub, cosh_sub_sinh]
@[simp] lemma cosh_sq_sub_sinh_sq : cosh x ^ 2 - sinh x ^ 2 = 1 :=
by rw [sq_sub_sq, cosh_add_sinh, cosh_sub_sinh, ← exp_add, add_neg_self, exp_zero]
lemma cosh_sq : cosh x ^ 2 = sinh x ^ 2 + 1 :=
begin
rw ← cosh_sq_sub_sinh_sq x,
ring
end
lemma sinh_sq : sinh x ^ 2 = cosh x ^ 2 - 1 :=
begin
rw ← cosh_sq_sub_sinh_sq x,
ring
end
lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 :=
by rw [two_mul, cosh_add, sq, sq]
lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x :=
begin
rw [two_mul, sinh_add],
ring
end
lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x :=
begin
have h1 : x + 2 * x = 3 * x, by ring,
rw [← h1, cosh_add x (2 * x)],
simp only [cosh_two_mul, sinh_two_mul],
have h2 : sinh x * (2 * sinh x * cosh x) = 2 * cosh x * sinh x ^ 2, by ring,
rw [h2, sinh_sq],
ring
end
lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x :=
begin
have h1 : x + 2 * x = 3 * x, by ring,
rw [← h1, sinh_add x (2 * x)],
simp only [cosh_two_mul, sinh_two_mul],
have h2 : cosh x * (2 * sinh x * cosh x) = 2 * sinh x * cosh x ^ 2, by ring,
rw [h2, cosh_sq],
ring,
end
@[simp] lemma sin_zero : sin 0 = 0 := by simp [sin]
@[simp] lemma sin_neg : sin (-x) = -sin x :=
by simp [sin, sub_eq_add_neg, exp_neg, (neg_div _ _).symm, add_mul]
lemma two_sin : 2 * sin x = (exp (-x * I) - exp (x * I)) * I :=
mul_div_cancel' _ two_ne_zero
lemma two_cos : 2 * cos x = exp (x * I) + exp (-x * I) :=
mul_div_cancel' _ two_ne_zero
lemma sinh_mul_I : sinh (x * I) = sin x * I :=
by rw [← mul_right_inj' (two_ne_zero' ℂ), two_sinh,
← mul_assoc, two_sin, mul_assoc, I_mul_I, mul_neg_one,
neg_sub, neg_mul_eq_neg_mul]
lemma cosh_mul_I : cosh (x * I) = cos x :=
by rw [← mul_right_inj' (two_ne_zero' ℂ), two_cosh,
two_cos, neg_mul_eq_neg_mul]
lemma tanh_mul_I : tanh (x * I) = tan x * I :=
by rw [tanh_eq_sinh_div_cosh, cosh_mul_I, sinh_mul_I, mul_div_right_comm, tan]
lemma cos_mul_I : cos (x * I) = cosh x :=
by rw ← cosh_mul_I; ring_nf; simp
lemma sin_mul_I : sin (x * I) = sinh x * I :=
have h : I * sin (x * I) = -sinh x := by { rw [mul_comm, ← sinh_mul_I], ring_nf, simp },
by simpa only [neg_mul, div_I, neg_neg]
using cancel_factors.cancel_factors_eq_div h I_ne_zero
lemma tan_mul_I : tan (x * I) = tanh x * I :=
by rw [tan, sin_mul_I, cos_mul_I, mul_div_right_comm, tanh_eq_sinh_div_cosh]
lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y :=
by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I,
add_mul, add_mul, mul_right_comm, ← sinh_mul_I,
mul_assoc, ← sinh_mul_I, ← cosh_mul_I, ← cosh_mul_I, sinh_add]
@[simp] lemma cos_zero : cos 0 = 1 := by simp [cos]
@[simp] lemma cos_neg : cos (-x) = cos x :=
by simp [cos, sub_eq_add_neg, exp_neg, add_comm]
private lemma cos_add_aux {a b c d : ℂ} :
(a + b) * (c + d) - (b - a) * (d - c) * (-1) =
2 * (a * c + b * d) := by ring
lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y :=
by rw [← cosh_mul_I, add_mul, cosh_add, cosh_mul_I, cosh_mul_I,
sinh_mul_I, sinh_mul_I, mul_mul_mul_comm, I_mul_I,
mul_neg_one, sub_eq_add_neg]
lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y :=
by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg]
lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y :=
by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg]
lemma sin_add_mul_I (x y : ℂ) : sin (x + y*I) = sin x * cosh y + cos x * sinh y * I :=
by rw [sin_add, cos_mul_I, sin_mul_I, mul_assoc]
lemma sin_eq (z : ℂ) : sin z = sin z.re * cosh z.im + cos z.re * sinh z.im * I :=
by convert sin_add_mul_I z.re z.im; exact (re_add_im z).symm
lemma cos_add_mul_I (x y : ℂ) : cos (x + y*I) = cos x * cosh y - sin x * sinh y * I :=
by rw [cos_add, cos_mul_I, sin_mul_I, mul_assoc]
lemma cos_eq (z : ℂ) : cos z = cos z.re * cosh z.im - sin z.re * sinh z.im * I :=
by convert cos_add_mul_I z.re z.im; exact (re_add_im z).symm
theorem sin_sub_sin : sin x - sin y = 2 * sin((x - y)/2) * cos((x + y)/2) :=
begin
have s1 := sin_add ((x + y) / 2) ((x - y) / 2),
have s2 := sin_sub ((x + y) / 2) ((x - y) / 2),
rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, half_add_self] at s1,
rw [div_sub_div_same, ←sub_add, add_sub_cancel', half_add_self] at s2,
rw [s1, s2],
ring
end
theorem cos_sub_cos : cos x - cos y = -2 * sin((x + y)/2) * sin((x - y)/2) :=
begin
have s1 := cos_add ((x + y) / 2) ((x - y) / 2),
have s2 := cos_sub ((x + y) / 2) ((x - y) / 2),
rw [div_add_div_same, add_sub, add_right_comm, add_sub_cancel, half_add_self] at s1,
rw [div_sub_div_same, ←sub_add, add_sub_cancel', half_add_self] at s2,
rw [s1, s2],
ring,
end
lemma cos_add_cos : cos x + cos y = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) :=
begin
have h2 : (2:ℂ) ≠ 0 := by norm_num,
calc cos x + cos y = cos ((x + y) / 2 + (x - y) / 2) + cos ((x + y) / 2 - (x - y) / 2) : _
... = (cos ((x + y) / 2) * cos ((x - y) / 2) - sin ((x + y) / 2) * sin ((x - y) / 2))
+ (cos ((x + y) / 2) * cos ((x - y) / 2) + sin ((x + y) / 2) * sin ((x - y) / 2)) : _
... = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) : _,
{ congr; field_simp [h2]; ring },
{ rw [cos_add, cos_sub] },
ring,
end
lemma sin_conj : sin (conj x) = conj (sin x) :=
by rw [← mul_left_inj' I_ne_zero, ← sinh_mul_I,
← conj_neg_I, ← ring_hom.map_mul, ← ring_hom.map_mul, sinh_conj,
mul_neg, sinh_neg, sinh_mul_I, mul_neg]
@[simp] lemma of_real_sin_of_real_re (x : ℝ) : ((sin x).re : ℂ) = sin x :=
eq_conj_iff_re.1 $ by rw [← sin_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_sin (x : ℝ) : (real.sin x : ℂ) = sin x :=
of_real_sin_of_real_re _
@[simp] lemma sin_of_real_im (x : ℝ) : (sin x).im = 0 :=
by rw [← of_real_sin_of_real_re, of_real_im]
lemma sin_of_real_re (x : ℝ) : (sin x).re = real.sin x := rfl
lemma cos_conj : cos (conj x) = conj (cos x) :=
by rw [← cosh_mul_I, ← conj_neg_I, ← ring_hom.map_mul, ← cosh_mul_I,
cosh_conj, mul_neg, cosh_neg]
@[simp] lemma of_real_cos_of_real_re (x : ℝ) : ((cos x).re : ℂ) = cos x :=
eq_conj_iff_re.1 $ by rw [← cos_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_cos (x : ℝ) : (real.cos x : ℂ) = cos x :=
of_real_cos_of_real_re _
@[simp] lemma cos_of_real_im (x : ℝ) : (cos x).im = 0 :=
by rw [← of_real_cos_of_real_re, of_real_im]
lemma cos_of_real_re (x : ℝ) : (cos x).re = real.cos x := rfl
@[simp] lemma tan_zero : tan 0 = 0 := by simp [tan]
lemma tan_eq_sin_div_cos : tan x = sin x / cos x := rfl
lemma tan_mul_cos {x : ℂ} (hx : cos x ≠ 0) : tan x * cos x = sin x :=
by rw [tan_eq_sin_div_cos, div_mul_cancel _ hx]
@[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div]
lemma tan_conj : tan (conj x) = conj (tan x) :=
by rw [tan, sin_conj, cos_conj, ← map_div₀, tan]
@[simp] lemma of_real_tan_of_real_re (x : ℝ) : ((tan x).re : ℂ) = tan x :=
eq_conj_iff_re.1 $ by rw [← tan_conj, conj_of_real]
@[simp, norm_cast] lemma of_real_tan (x : ℝ) : (real.tan x : ℂ) = tan x :=
of_real_tan_of_real_re _
@[simp] lemma tan_of_real_im (x : ℝ) : (tan x).im = 0 :=
by rw [← of_real_tan_of_real_re, of_real_im]
lemma tan_of_real_re (x : ℝ) : (tan x).re = real.tan x := rfl
lemma cos_add_sin_I : cos x + sin x * I = exp (x * I) :=
by rw [← cosh_add_sinh, sinh_mul_I, cosh_mul_I]
lemma cos_sub_sin_I : cos x - sin x * I = exp (-x * I) :=
by rw [neg_mul, ← cosh_sub_sinh, sinh_mul_I, cosh_mul_I]
@[simp] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 :=
eq.trans
(by rw [cosh_mul_I, sinh_mul_I, mul_pow, I_sq, mul_neg_one, sub_neg_eq_add, add_comm])
(cosh_sq_sub_sinh_sq (x * I))
@[simp] lemma cos_sq_add_sin_sq : cos x ^ 2 + sin x ^ 2 = 1 :=
by rw [add_comm, sin_sq_add_cos_sq]
lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 :=
by rw [two_mul, cos_add, ← sq, ← sq]
lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 :=
by rw [cos_two_mul', eq_sub_iff_add_eq.2 (sin_sq_add_cos_sq x),
← sub_add, sub_add_eq_add_sub, two_mul]
lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x :=
by rw [two_mul, sin_add, two_mul, add_mul, mul_comm]
lemma cos_sq : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 :=
by simp [cos_two_mul, div_add_div_same, mul_div_cancel_left, two_ne_zero, -one_div]
lemma cos_sq' : cos x ^ 2 = 1 - sin x ^ 2 :=
by rw [←sin_sq_add_cos_sq x, add_sub_cancel']
lemma sin_sq : sin x ^ 2 = 1 - cos x ^ 2 :=
by rw [←sin_sq_add_cos_sq x, add_sub_cancel]
lemma inv_one_add_tan_sq {x : ℂ} (hx : cos x ≠ 0) : (1 + tan x ^ 2)⁻¹ = cos x ^ 2 :=
have cos x ^ 2 ≠ 0, from pow_ne_zero 2 hx,
by { rw [tan_eq_sin_div_cos, div_pow], field_simp [this] }
lemma tan_sq_div_one_add_tan_sq {x : ℂ} (hx : cos x ≠ 0) :
tan x ^ 2 / (1 + tan x ^ 2) = sin x ^ 2 :=
by simp only [← tan_mul_cos hx, mul_pow, ← inv_one_add_tan_sq hx, div_eq_mul_inv, one_mul]
lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x :=
begin
have h1 : x + 2 * x = 3 * x, by ring,
rw [← h1, cos_add x (2 * x)],
simp only [cos_two_mul, sin_two_mul, mul_add, mul_sub, mul_one, sq],
have h2 : 4 * cos x ^ 3 = 2 * cos x * cos x * cos x + 2 * cos x * cos x ^ 2, by ring,
rw [h2, cos_sq'],
ring
end
lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 :=
begin
have h1 : x + 2 * x = 3 * x, by ring,
rw [← h1, sin_add x (2 * x)],
simp only [cos_two_mul, sin_two_mul, cos_sq'],
have h2 : cos x * (2 * sin x * cos x) = 2 * sin x * cos x ^ 2, by ring,
rw [h2, cos_sq'],
ring
end
lemma exp_mul_I : exp (x * I) = cos x + sin x * I :=
(cos_add_sin_I _).symm
lemma exp_add_mul_I : exp (x + y * I) = exp x * (cos y + sin y * I) :=
by rw [exp_add, exp_mul_I]
lemma exp_eq_exp_re_mul_sin_add_cos : exp x = exp x.re * (cos x.im + sin x.im * I) :=
by rw [← exp_add_mul_I, re_add_im]
lemma exp_re : (exp x).re = real.exp x.re * real.cos x.im :=
by { rw [exp_eq_exp_re_mul_sin_add_cos], simp [exp_of_real_re, cos_of_real_re] }
lemma exp_im : (exp x).im = real.exp x.re * real.sin x.im :=
by { rw [exp_eq_exp_re_mul_sin_add_cos], simp [exp_of_real_re, sin_of_real_re] }
@[simp] lemma exp_of_real_mul_I_re (x : ℝ) : (exp (x * I)).re = real.cos x :=
by simp [exp_mul_I, cos_of_real_re]
@[simp] lemma exp_of_real_mul_I_im (x : ℝ) : (exp (x * I)).im = real.sin x :=
by simp [exp_mul_I, sin_of_real_re]
/-- **De Moivre's formula** -/
theorem cos_add_sin_mul_I_pow (n : ℕ) (z : ℂ) :
(cos z + sin z * I) ^ n = cos (↑n * z) + sin (↑n * z) * I :=
begin
rw [← exp_mul_I, ← exp_mul_I],
induction n with n ih,
{ rw [pow_zero, nat.cast_zero, zero_mul, zero_mul, exp_zero] },
{ rw [pow_succ', ih, nat.cast_succ, add_mul, add_mul, one_mul, exp_add] }
end
end complex
namespace real
open complex
variables (x y : ℝ)
@[simp] lemma exp_zero : exp 0 = 1 :=
by simp [real.exp]
lemma exp_add : exp (x + y) = exp x * exp y :=
by simp [exp_add, exp]
lemma exp_list_sum (l : list ℝ) : exp l.sum = (l.map exp).prod :=
@monoid_hom.map_list_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ l
lemma exp_multiset_sum (s : multiset ℝ) : exp s.sum = (s.map exp).prod :=
@monoid_hom.map_multiset_prod (multiplicative ℝ) ℝ _ _ ⟨exp, exp_zero, exp_add⟩ s
lemma exp_sum {α : Type*} (s : finset α) (f : α → ℝ) : exp (∑ x in s, f x) = ∏ x in s, exp (f x) :=
@monoid_hom.map_prod (multiplicative ℝ) α ℝ _ _ ⟨exp, exp_zero, exp_add⟩ f s
lemma exp_nat_mul (x : ℝ) : ∀ n : ℕ, exp(n*x) = (exp x)^n
| 0 := by rw [nat.cast_zero, zero_mul, exp_zero, pow_zero]
| (nat.succ n) := by rw [pow_succ', nat.cast_add_one, add_mul, exp_add, ←exp_nat_mul, one_mul]
lemma exp_ne_zero : exp x ≠ 0 :=
λ h, exp_ne_zero x $ by rw [exp, ← of_real_inj] at h; simp * at *
lemma exp_neg : exp (-x) = (exp x)⁻¹ :=
by rw [← of_real_inj, exp, of_real_exp_of_real_re, of_real_neg, exp_neg,
of_real_inv, of_real_exp]
lemma exp_sub : exp (x - y) = exp x / exp y :=
by simp [sub_eq_add_neg, exp_add, exp_neg, div_eq_mul_inv]
@[simp] lemma sin_zero : sin 0 = 0 := by simp [sin]
@[simp] lemma sin_neg : sin (-x) = -sin x :=
by simp [sin, exp_neg, (neg_div _ _).symm, add_mul]
lemma sin_add : sin (x + y) = sin x * cos y + cos x * sin y :=
by rw [← of_real_inj]; simp [sin, sin_add]
@[simp] lemma cos_zero : cos 0 = 1 := by simp [cos]
@[simp] lemma cos_neg : cos (-x) = cos x :=
by simp [cos, exp_neg]
@[simp] lemma cos_abs : cos (|x|) = cos x :=
by cases le_total x 0; simp only [*, _root_.abs_of_nonneg, abs_of_nonpos, cos_neg]
lemma cos_add : cos (x + y) = cos x * cos y - sin x * sin y :=
by rw ← of_real_inj; simp [cos, cos_add]
lemma sin_sub : sin (x - y) = sin x * cos y - cos x * sin y :=
by simp [sub_eq_add_neg, sin_add, sin_neg, cos_neg]
lemma cos_sub : cos (x - y) = cos x * cos y + sin x * sin y :=
by simp [sub_eq_add_neg, cos_add, sin_neg, cos_neg]
lemma sin_sub_sin : sin x - sin y = 2 * sin((x - y)/2) * cos((x + y)/2) :=
begin
rw ← of_real_inj,
simp only [sin, cos, of_real_sin_of_real_re, of_real_sub, of_real_add, of_real_div, of_real_mul,
of_real_one, of_real_bit0],
convert sin_sub_sin _ _;
norm_cast
end
theorem cos_sub_cos : cos x - cos y = -2 * sin((x + y)/2) * sin((x - y)/2) :=
begin
rw ← of_real_inj,
simp only [cos, neg_mul, of_real_sin, of_real_sub, of_real_add,
of_real_cos_of_real_re, of_real_div, of_real_mul, of_real_one, of_real_neg, of_real_bit0],
convert cos_sub_cos _ _,
ring,
end
lemma cos_add_cos : cos x + cos y = 2 * cos ((x + y) / 2) * cos ((x - y) / 2) :=
begin
rw ← of_real_inj,
simp only [cos, of_real_sub, of_real_add, of_real_cos_of_real_re, of_real_div, of_real_mul,
of_real_one, of_real_bit0],
convert cos_add_cos _ _;
norm_cast,
end
lemma tan_eq_sin_div_cos : tan x = sin x / cos x :=
by rw [← of_real_inj, of_real_tan, tan_eq_sin_div_cos, of_real_div, of_real_sin, of_real_cos]
lemma tan_mul_cos {x : ℝ} (hx : cos x ≠ 0) : tan x * cos x = sin x :=
by rw [tan_eq_sin_div_cos, div_mul_cancel _ hx]
@[simp] lemma tan_zero : tan 0 = 0 := by simp [tan]
@[simp] lemma tan_neg : tan (-x) = -tan x := by simp [tan, neg_div]
@[simp] lemma sin_sq_add_cos_sq : sin x ^ 2 + cos x ^ 2 = 1 :=
of_real_inj.1 $ by simp
@[simp] lemma cos_sq_add_sin_sq : cos x ^ 2 + sin x ^ 2 = 1 :=
by rw [add_comm, sin_sq_add_cos_sq]
lemma sin_sq_le_one : sin x ^ 2 ≤ 1 :=
by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_right (sq_nonneg _)
lemma cos_sq_le_one : cos x ^ 2 ≤ 1 :=
by rw ← sin_sq_add_cos_sq x; exact le_add_of_nonneg_left (sq_nonneg _)
lemma abs_sin_le_one : |sin x| ≤ 1 :=
abs_le_one_iff_mul_self_le_one.2 $ by simp only [← sq, sin_sq_le_one]
lemma abs_cos_le_one : |cos x| ≤ 1 :=
abs_le_one_iff_mul_self_le_one.2 $ by simp only [← sq, cos_sq_le_one]
lemma sin_le_one : sin x ≤ 1 :=
(abs_le.1 (abs_sin_le_one _)).2
lemma cos_le_one : cos x ≤ 1 :=
(abs_le.1 (abs_cos_le_one _)).2
lemma neg_one_le_sin : -1 ≤ sin x :=
(abs_le.1 (abs_sin_le_one _)).1
lemma neg_one_le_cos : -1 ≤ cos x :=
(abs_le.1 (abs_cos_le_one _)).1
lemma cos_two_mul : cos (2 * x) = 2 * cos x ^ 2 - 1 :=
by rw ← of_real_inj; simp [cos_two_mul]
lemma cos_two_mul' : cos (2 * x) = cos x ^ 2 - sin x ^ 2 :=
by rw ← of_real_inj; simp [cos_two_mul']
lemma sin_two_mul : sin (2 * x) = 2 * sin x * cos x :=
by rw ← of_real_inj; simp [sin_two_mul]
lemma cos_sq : cos x ^ 2 = 1 / 2 + cos (2 * x) / 2 :=
of_real_inj.1 $ by simpa using cos_sq x
lemma cos_sq' : cos x ^ 2 = 1 - sin x ^ 2 :=
by rw [←sin_sq_add_cos_sq x, add_sub_cancel']
lemma sin_sq : sin x ^ 2 = 1 - cos x ^ 2 :=
eq_sub_iff_add_eq.2 $ sin_sq_add_cos_sq _
lemma abs_sin_eq_sqrt_one_sub_cos_sq (x : ℝ) :
|sin x| = sqrt (1 - cos x ^ 2) :=
by rw [← sin_sq, sqrt_sq_eq_abs]
lemma abs_cos_eq_sqrt_one_sub_sin_sq (x : ℝ) :
|cos x| = sqrt (1 - sin x ^ 2) :=
by rw [← cos_sq', sqrt_sq_eq_abs]
lemma inv_one_add_tan_sq {x : ℝ} (hx : cos x ≠ 0) : (1 + tan x ^ 2)⁻¹ = cos x ^ 2 :=
have complex.cos x ≠ 0, from mt (congr_arg re) hx,
of_real_inj.1 $ by simpa using complex.inv_one_add_tan_sq this
lemma tan_sq_div_one_add_tan_sq {x : ℝ} (hx : cos x ≠ 0) :
tan x ^ 2 / (1 + tan x ^ 2) = sin x ^ 2 :=
by simp only [← tan_mul_cos hx, mul_pow, ← inv_one_add_tan_sq hx, div_eq_mul_inv, one_mul]
lemma inv_sqrt_one_add_tan_sq {x : ℝ} (hx : 0 < cos x) :
(sqrt (1 + tan x ^ 2))⁻¹ = cos x :=
by rw [← sqrt_sq hx.le, ← sqrt_inv, inv_one_add_tan_sq hx.ne']
lemma tan_div_sqrt_one_add_tan_sq {x : ℝ} (hx : 0 < cos x) :
tan x / sqrt (1 + tan x ^ 2) = sin x :=
by rw [← tan_mul_cos hx.ne', ← inv_sqrt_one_add_tan_sq hx, div_eq_mul_inv]
lemma cos_three_mul : cos (3 * x) = 4 * cos x ^ 3 - 3 * cos x :=
by rw ← of_real_inj; simp [cos_three_mul]
lemma sin_three_mul : sin (3 * x) = 3 * sin x - 4 * sin x ^ 3 :=
by rw ← of_real_inj; simp [sin_three_mul]
/-- The definition of `sinh` in terms of `exp`. -/
lemma sinh_eq (x : ℝ) : sinh x = (exp x - exp (-x)) / 2 :=
eq_div_of_mul_eq two_ne_zero $ by rw [sinh, exp, exp, complex.of_real_neg, complex.sinh, mul_two,
← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' ℂ), complex.sub_re]
@[simp] lemma sinh_zero : sinh 0 = 0 := by simp [sinh]
@[simp] lemma sinh_neg : sinh (-x) = -sinh x :=
by simp [sinh, exp_neg, (neg_div _ _).symm, add_mul]
lemma sinh_add : sinh (x + y) = sinh x * cosh y + cosh x * sinh y :=
by rw ← of_real_inj; simp [sinh_add]
/-- The definition of `cosh` in terms of `exp`. -/
lemma cosh_eq (x : ℝ) : cosh x = (exp x + exp (-x)) / 2 :=
eq_div_of_mul_eq two_ne_zero $ by rw [cosh, exp, exp, complex.of_real_neg, complex.cosh, mul_two,
← complex.add_re, ← mul_two, div_mul_cancel _ (two_ne_zero' ℂ), complex.add_re]
@[simp] lemma cosh_zero : cosh 0 = 1 := by simp [cosh]
@[simp] lemma cosh_neg : cosh (-x) = cosh x := of_real_inj.1 $ by simp
@[simp] lemma cosh_abs : cosh (|x|) = cosh x :=
by cases le_total x 0; simp [*, _root_.abs_of_nonneg, abs_of_nonpos]
lemma cosh_add : cosh (x + y) = cosh x * cosh y + sinh x * sinh y :=
by rw ← of_real_inj; simp [cosh_add]
lemma sinh_sub : sinh (x - y) = sinh x * cosh y - cosh x * sinh y :=
by simp [sub_eq_add_neg, sinh_add, sinh_neg, cosh_neg]
lemma cosh_sub : cosh (x - y) = cosh x * cosh y - sinh x * sinh y :=
by simp [sub_eq_add_neg, cosh_add, sinh_neg, cosh_neg]
lemma tanh_eq_sinh_div_cosh : tanh x = sinh x / cosh x :=
of_real_inj.1 $ by simp [tanh_eq_sinh_div_cosh]
@[simp] lemma tanh_zero : tanh 0 = 0 := by simp [tanh]
@[simp] lemma tanh_neg : tanh (-x) = -tanh x := by simp [tanh, neg_div]
@[simp] lemma cosh_add_sinh : cosh x + sinh x = exp x :=
by rw ← of_real_inj; simp
@[simp] lemma sinh_add_cosh : sinh x + cosh x = exp x :=
by rw [add_comm, cosh_add_sinh]
@[simp] lemma exp_sub_cosh : exp x - cosh x = sinh x :=
sub_eq_iff_eq_add.2 (sinh_add_cosh x).symm
@[simp] lemma exp_sub_sinh : exp x - sinh x = cosh x :=
sub_eq_iff_eq_add.2 (cosh_add_sinh x).symm
@[simp] lemma cosh_sub_sinh : cosh x - sinh x = exp (-x) :=
by { rw [← of_real_inj], simp }
@[simp] lemma sinh_sub_cosh : sinh x - cosh x = -exp (-x) :=
by rw [← neg_sub, cosh_sub_sinh]
@[simp] lemma cosh_sq_sub_sinh_sq (x : ℝ) : cosh x ^ 2 - sinh x ^ 2 = 1 :=
by rw ← of_real_inj; simp
lemma cosh_sq : cosh x ^ 2 = sinh x ^ 2 + 1 :=
by rw ← of_real_inj; simp [cosh_sq]
lemma cosh_sq' : cosh x ^ 2 = 1 + sinh x ^ 2 :=
(cosh_sq x).trans (add_comm _ _)
lemma sinh_sq : sinh x ^ 2 = cosh x ^ 2 - 1 :=
by rw ← of_real_inj; simp [sinh_sq]
lemma cosh_two_mul : cosh (2 * x) = cosh x ^ 2 + sinh x ^ 2 :=
by rw ← of_real_inj; simp [cosh_two_mul]
lemma sinh_two_mul : sinh (2 * x) = 2 * sinh x * cosh x :=
by rw ← of_real_inj; simp [sinh_two_mul]
lemma cosh_three_mul : cosh (3 * x) = 4 * cosh x ^ 3 - 3 * cosh x :=
by rw ← of_real_inj; simp [cosh_three_mul]
lemma sinh_three_mul : sinh (3 * x) = 4 * sinh x ^ 3 + 3 * sinh x :=
by rw ← of_real_inj; simp [sinh_three_mul]
open is_absolute_value
/-- This is an intermediate result that is later replaced by `real.add_one_le_exp`; use that lemma
instead. -/
lemma add_one_le_exp_of_nonneg {x : ℝ} (hx : 0 ≤ x) : x + 1 ≤ exp x :=
calc x + 1 ≤ lim (⟨(λ n : ℕ, ((exp' x) n).re), is_cau_seq_re (exp' x)⟩ : cau_seq ℝ has_abs.abs) :
le_lim (cau_seq.le_of_exists ⟨2,
λ j hj, show x + (1 : ℝ) ≤ (∑ m in range j, (x ^ m / m! : ℂ)).re,
from have h₁ : (((λ m : ℕ, (x ^ m / m! : ℂ)) ∘ nat.succ) 0).re = x, by simp,
have h₂ : ((x : ℂ) ^ 0 / 0!).re = 1, by simp,
begin
rw [← tsub_add_cancel_of_le hj, sum_range_succ', sum_range_succ',
add_re, add_re, h₁, h₂, add_assoc,
← coe_re_add_group_hom, (re_add_group_hom).map_sum, coe_re_add_group_hom ],
refine le_add_of_nonneg_of_le (sum_nonneg (λ m hm, _)) le_rfl,
rw [← of_real_pow, ← of_real_nat_cast, ← of_real_div, of_real_re],
exact div_nonneg (pow_nonneg hx _) (nat.cast_nonneg _),
end⟩)
... = exp x : by rw [exp, complex.exp, ← cau_seq_re, lim_re]
lemma one_le_exp {x : ℝ} (hx : 0 ≤ x) : 1 ≤ exp x :=
by linarith [add_one_le_exp_of_nonneg hx]
lemma exp_pos (x : ℝ) : 0 < exp x :=
(le_total 0 x).elim (lt_of_lt_of_le zero_lt_one ∘ one_le_exp)
(λ h, by rw [← neg_neg x, real.exp_neg];
exact inv_pos.2 (lt_of_lt_of_le zero_lt_one (one_le_exp (neg_nonneg.2 h))))
@[simp] lemma abs_exp (x : ℝ) : |exp x| = exp x :=
abs_of_pos (exp_pos _)
@[mono] lemma exp_strict_mono : strict_mono exp :=
λ x y h, by rw [← sub_add_cancel y x, real.exp_add];
exact (lt_mul_iff_one_lt_left (exp_pos _)).2
(lt_of_lt_of_le (by linarith) (add_one_le_exp_of_nonneg (by linarith)))
@[mono] lemma exp_monotone : monotone exp := exp_strict_mono.monotone
@[simp] lemma exp_lt_exp {x y : ℝ} : exp x < exp y ↔ x < y := exp_strict_mono.lt_iff_lt
@[simp] lemma exp_le_exp {x y : ℝ} : exp x ≤ exp y ↔ x ≤ y := exp_strict_mono.le_iff_le
lemma exp_injective : function.injective exp := exp_strict_mono.injective
@[simp] lemma exp_eq_exp {x y : ℝ} : exp x = exp y ↔ x = y := exp_injective.eq_iff
@[simp] lemma exp_eq_one_iff : exp x = 1 ↔ x = 0 := exp_injective.eq_iff' exp_zero
@[simp] lemma one_lt_exp_iff {x : ℝ} : 1 < exp x ↔ 0 < x :=
by rw [← exp_zero, exp_lt_exp]
@[simp] lemma exp_lt_one_iff {x : ℝ} : exp x < 1 ↔ x < 0 :=
by rw [← exp_zero, exp_lt_exp]
@[simp] lemma exp_le_one_iff {x : ℝ} : exp x ≤ 1 ↔ x ≤ 0 :=
exp_zero ▸ exp_le_exp
@[simp] lemma one_le_exp_iff {x : ℝ} : 1 ≤ exp x ↔ 0 ≤ x :=
exp_zero ▸ exp_le_exp
/-- `real.cosh` is always positive -/
lemma cosh_pos (x : ℝ) : 0 < real.cosh x :=
(cosh_eq x).symm ▸ half_pos (add_pos (exp_pos x) (exp_pos (-x)))
lemma sinh_lt_cosh : sinh x < cosh x :=
lt_of_pow_lt_pow 2 (cosh_pos _).le $ (cosh_sq x).symm ▸ lt_add_one _
end real
namespace complex
lemma sum_div_factorial_le {α : Type*} [linear_ordered_field α] (n j : ℕ) (hn : 0 < n) :
∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α) ≤ n.succ / (n! * n) :=
calc ∑ m in filter (λ k, n ≤ k) (range j), (1 / m! : α)
= ∑ m in range (j - n), 1 / (m + n)! :
sum_bij (λ m _, m - n)
(λ m hm, mem_range.2 $ (tsub_lt_tsub_iff_right (by simp at hm; tauto)).2
(by simp at hm; tauto))
(λ m hm, by rw tsub_add_cancel_of_le; simp at *; tauto)
(λ a₁ a₂ ha₁ ha₂ h,
by rwa [tsub_eq_iff_eq_add_of_le, tsub_add_eq_add_tsub, eq_comm, tsub_eq_iff_eq_add_of_le,
add_left_inj, eq_comm] at h;
simp at *; tauto)
(λ b hb, ⟨b + n,
mem_filter.2 ⟨mem_range.2 $ lt_tsub_iff_right.mp (mem_range.1 hb), nat.le_add_left _ _⟩,
by rw add_tsub_cancel_right⟩)
... ≤ ∑ m in range (j - n), (n! * n.succ ^ m)⁻¹ :
begin
refine sum_le_sum (assume m n, _),
rw [one_div, inv_le_inv],
{ rw [← nat.cast_pow, ← nat.cast_mul, nat.cast_le, add_comm],
exact nat.factorial_mul_pow_le_factorial },
{ exact nat.cast_pos.2 (nat.factorial_pos _) },
{ exact mul_pos (nat.cast_pos.2 (nat.factorial_pos _))
(pow_pos (nat.cast_pos.2 (nat.succ_pos _)) _) },
end
... = n!⁻¹ * ∑ m in range (j - n), n.succ⁻¹ ^ m :
by simp [mul_inv, mul_sum.symm, sum_mul.symm, -nat.factorial_succ, mul_comm, inv_pow]
... = (n.succ - n.succ * n.succ⁻¹ ^ (j - n)) / (n! * n) :
have h₁ : (n.succ : α) ≠ 1, from @nat.cast_one α _ ▸ mt nat.cast_inj.1
(mt nat.succ.inj (pos_iff_ne_zero.1 hn)),
have h₂ : (n.succ : α) ≠ 0, from nat.cast_ne_zero.2 (nat.succ_ne_zero _),
have h₃ : (n! * n : α) ≠ 0,
from mul_ne_zero (nat.cast_ne_zero.2 (pos_iff_ne_zero.1 (nat.factorial_pos _)))
(nat.cast_ne_zero.2 (pos_iff_ne_zero.1 hn)),
have h₄ : (n.succ - 1 : α) = n, by simp,
by rw [geom_sum_inv h₁ h₂, eq_div_iff_mul_eq h₃,
mul_comm _ (n! * n : α), ← mul_assoc (n!⁻¹ : α), ← mul_inv_rev, h₄,
← mul_assoc (n! * n : α), mul_comm (n : α) n!, mul_inv_cancel h₃];
simp [mul_add, add_mul, mul_assoc, mul_comm]
... ≤ n.succ / (n! * n) :
begin
refine iff.mpr (div_le_div_right (mul_pos _ _)) _,
exact nat.cast_pos.2 (nat.factorial_pos _),
exact nat.cast_pos.2 hn,
exact sub_le_self _
(mul_nonneg (nat.cast_nonneg _) (pow_nonneg (inv_nonneg.2 (nat.cast_nonneg _)) _))
end
lemma exp_bound {x : ℂ} (hx : abs x ≤ 1) {n : ℕ} (hn : 0 < n) :
abs (exp x - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) :=
begin
rw [← lim_const (∑ m in range n, _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs],
refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩),
simp_rw ← sub_eq_add_neg,
show abs (∑ m in range j, x ^ m / m! - ∑ m in range n, x ^ m / m!)
≤ abs x ^ n * (n.succ * (n! * n)⁻¹),
rw sum_range_sub_sum_range hj,
calc abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ m / m! : ℂ))
= abs (∑ m in (range j).filter (λ k, n ≤ k), (x ^ n * (x ^ (m - n) / m!) : ℂ)) :
begin
refine congr_arg abs (sum_congr rfl (λ m hm, _)),
rw [mem_filter, mem_range] at hm,
rw [← mul_div_assoc, ← pow_add, add_tsub_cancel_of_le hm.2]
end
... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs (x ^ n * (_ / m!)) : abv_sum_le_sum_abv _ _
... ≤ ∑ m in filter (λ k, n ≤ k) (range j), abs x ^ n * (1 / m!) :
begin
refine sum_le_sum (λ m hm, _),
rw [map_mul, map_pow, map_div₀, abs_cast_nat],
refine mul_le_mul_of_nonneg_left ((div_le_div_right _).2 _) _,
{ exact nat.cast_pos.2 (nat.factorial_pos _), },
{ rw abv_pow abs,
exact (pow_le_one _ (abs.nonneg _) hx), },
{ exact pow_nonneg (abs.nonneg _) _ },
end
... = abs x ^ n * (∑ m in (range j).filter (λ k, n ≤ k), (1 / m! : ℝ)) :
by simp [abs_mul, abv_pow abs, abs_div, mul_sum.symm]
... ≤ abs x ^ n * (n.succ * (n! * n)⁻¹) :
mul_le_mul_of_nonneg_left (sum_div_factorial_le _ _ hn) (pow_nonneg (abs.nonneg _) _)
end
lemma exp_bound' {x : ℂ} {n : ℕ} (hx : abs x / (n.succ) ≤ 1 / 2) :
abs (exp x - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n / (n!) * 2 :=
begin
rw [← lim_const (∑ m in range n, _), exp, sub_eq_add_neg, ← lim_neg, lim_add, ← lim_abs],
refine lim_le (cau_seq.le_of_exists ⟨n, λ j hj, _⟩),
simp_rw [←sub_eq_add_neg],
show abs (∑ m in range j, x ^ m / m! - ∑ m in range n, x ^ m / m!) ≤ abs x ^ n / (n!) * 2,
let k := j - n,
have hj : j = n + k := (add_tsub_cancel_of_le hj).symm,
rw [hj, sum_range_add_sub_sum_range],
calc abs (∑ (i : ℕ) in range k, x ^ (n + i) / ((n + i)! : ℂ))
≤ ∑ (i : ℕ) in range k, abs (x ^ (n + i) / ((n + i)! : ℂ)) : abv_sum_le_sum_abv _ _
... ≤ ∑ (i : ℕ) in range k, (abs x) ^ (n + i) / (n + i)! :
by simp only [complex.abs_cast_nat, map_div₀, abv_pow abs]
... ≤ ∑ (i : ℕ) in range k, (abs x) ^ (n + i) / (n! * n.succ ^ i) : _
... = ∑ (i : ℕ) in range k, (abs x) ^ (n) / (n!) * ((abs x)^i / n.succ ^ i) : _
... ≤ abs x ^ n / (↑n!) * 2 : _,
{ refine sum_le_sum (λ m hm, div_le_div (pow_nonneg (abs.nonneg x) (n + m)) le_rfl _ _),
{ exact_mod_cast mul_pos n.factorial_pos (pow_pos n.succ_pos _), },
{ exact_mod_cast (nat.factorial_mul_pow_le_factorial), }, },
{ refine finset.sum_congr rfl (λ _ _, _),
simp only [pow_add, div_eq_inv_mul, mul_inv, mul_left_comm, mul_assoc], },
{ rw [←mul_sum],
apply mul_le_mul_of_nonneg_left,
{ simp_rw [←div_pow],
rw [geom_sum_eq, div_le_iff_of_neg],
{ transitivity (-1 : ℝ),
{ linarith },
{ simp only [neg_le_sub_iff_le_add, div_pow, nat.cast_succ, le_add_iff_nonneg_left],
exact div_nonneg (pow_nonneg (abs.nonneg x) k)
(pow_nonneg (add_nonneg n.cast_nonneg zero_le_one) k) } },
{ linarith },
{ linarith }, },
{ exact div_nonneg (pow_nonneg (abs.nonneg x) n) (nat.cast_nonneg (n!)), }, },
end
lemma abs_exp_sub_one_le {x : ℂ} (hx : abs x ≤ 1) :
abs (exp x - 1) ≤ 2 * abs x :=
calc abs (exp x - 1) = abs (exp x - ∑ m in range 1, x ^ m / m!) :
by simp [sum_range_succ]
... ≤ abs x ^ 1 * ((nat.succ 1) * (1! * (1 : ℕ))⁻¹) :
exp_bound hx dec_trivial
... = 2 * abs x : by simp [two_mul, mul_two, mul_add, mul_comm]
lemma abs_exp_sub_one_sub_id_le {x : ℂ} (hx : abs x ≤ 1) :
abs (exp x - 1 - x) ≤ (abs x)^2 :=
calc abs (exp x - 1 - x) = abs (exp x - ∑ m in range 2, x ^ m / m!) :
by simp [sub_eq_add_neg, sum_range_succ_comm, add_assoc]
... ≤ (abs x)^2 * (nat.succ 2 * (2! * (2 : ℕ))⁻¹) :
exp_bound hx dec_trivial
... ≤ (abs x)^2 * 1 :
mul_le_mul_of_nonneg_left (by norm_num) (sq_nonneg (abs x))
... = (abs x)^2 :
by rw [mul_one]
end complex
namespace real
open complex finset
lemma exp_bound {x : ℝ} (hx : |x| ≤ 1) {n : ℕ} (hn : 0 < n) :
|exp x - ∑ m in range n, x ^ m / m!|≤ |x| ^ n * (n.succ / (n! * n)) :=
begin
have hxc : complex.abs x ≤ 1, by exact_mod_cast hx,
convert exp_bound hxc hn; norm_cast
end
lemma exp_bound' {x : ℝ} (h1 : 0 ≤ x) (h2 : x ≤ 1) {n : ℕ} (hn : 0 < n) :
real.exp x ≤ ∑ m in finset.range n, x ^ m / m! + x ^ n * (n + 1) / (n! * n) :=
begin
have h3 : |x| = x := by simpa,
have h4 : |x| ≤ 1 := by rwa h3,
have h' := real.exp_bound h4 hn,
rw h3 at h',
have h'' := (abs_sub_le_iff.1 h').1,
have t := sub_le_iff_le_add'.1 h'',
simpa [mul_div_assoc] using t
end
lemma abs_exp_sub_one_le {x : ℝ} (hx : |x| ≤ 1) : |exp x - 1| ≤ 2 * |x| :=
begin
have : complex.abs x ≤ 1 := by exact_mod_cast hx,
exact_mod_cast complex.abs_exp_sub_one_le this,
end
lemma abs_exp_sub_one_sub_id_le {x : ℝ} (hx : |x| ≤ 1) : |exp x - 1 - x| ≤ x ^ 2 :=
begin
rw ←_root_.sq_abs,
have : complex.abs x ≤ 1 := by exact_mod_cast hx,
exact_mod_cast complex.abs_exp_sub_one_sub_id_le this,
end
/-- A finite initial segment of the exponential series, followed by an arbitrary tail.
For fixed `n` this is just a linear map wrt `r`, and each map is a simple linear function
of the previous (see `exp_near_succ`), with `exp_near n x r ⟶ exp x` as `n ⟶ ∞`,
for any `r`. -/
def exp_near (n : ℕ) (x r : ℝ) : ℝ := ∑ m in range n, x ^ m / m! + x ^ n / n! * r
@[simp] theorem exp_near_zero (x r) : exp_near 0 x r = r := by simp [exp_near]
@[simp] theorem exp_near_succ (n x r) : exp_near (n + 1) x r = exp_near n x (1 + x / (n+1) * r) :=
by simp [exp_near, range_succ, mul_add, add_left_comm, add_assoc, pow_succ, div_eq_mul_inv,
mul_inv]; ac_refl
theorem exp_near_sub (n x r₁ r₂) : exp_near n x r₁ - exp_near n x r₂ = x ^ n / n! * (r₁ - r₂) :=
by simp [exp_near, mul_sub]
lemma exp_approx_end (n m : ℕ) (x : ℝ)
(e₁ : n + 1 = m) (h : |x| ≤ 1) :
|exp x - exp_near m x 0| ≤ |x| ^ m / m! * ((m+1)/m) :=
by { simp [exp_near], convert exp_bound h _ using 1, field_simp [mul_comm], linarith }
lemma exp_approx_succ {n} {x a₁ b₁ : ℝ} (m : ℕ)
(e₁ : n + 1 = m) (a₂ b₂ : ℝ)
(e : |1 + x / m * a₂ - a₁| ≤ b₁ - |x| / m * b₂)
(h : |exp x - exp_near m x a₂| ≤ |x| ^ m / m! * b₂) :
|exp x - exp_near n x a₁| ≤ |x| ^ n / n! * b₁ :=
begin
refine (_root_.abs_sub_le _ _ _).trans ((add_le_add_right h _).trans _),
subst e₁, rw [exp_near_succ, exp_near_sub, _root_.abs_mul],
convert mul_le_mul_of_nonneg_left (le_sub_iff_add_le'.1 e) _,
{ simp [mul_add, pow_succ', div_eq_mul_inv, _root_.abs_mul, _root_.abs_inv, ← pow_abs, mul_inv],
ac_refl },
{ simp [_root_.div_nonneg, _root_.abs_nonneg] }
end
lemma exp_approx_end' {n} {x a b : ℝ} (m : ℕ)
(e₁ : n + 1 = m) (rm : ℝ) (er : ↑m = rm) (h : |x| ≤ 1)
(e : |1 - a| ≤ b - |x| / rm * ((rm+1)/rm)) :
|exp x - exp_near n x a| ≤ |x| ^ n / n! * b :=
by subst er; exact
exp_approx_succ _ e₁ _ _ (by simpa using e) (exp_approx_end _ _ _ e₁ h)
lemma exp_1_approx_succ_eq {n} {a₁ b₁ : ℝ} {m : ℕ}
(en : n + 1 = m) {rm : ℝ} (er : ↑m = rm)
(h : |exp 1 - exp_near m 1 ((a₁ - 1) * rm)| ≤ |1| ^ m / m! * (b₁ * rm)) :
|exp 1 - exp_near n 1 a₁| ≤ |1| ^ n / n! * b₁ :=
begin
subst er,
refine exp_approx_succ _ en _ _ _ h,
field_simp [show (m : ℝ) ≠ 0, by norm_cast; linarith],
end
lemma exp_approx_start (x a b : ℝ)
(h : |exp x - exp_near 0 x a| ≤ |x| ^ 0 / 0! * b) :
|exp x - a| ≤ b :=
by simpa using h
lemma cos_bound {x : ℝ} (hx : |x| ≤ 1) :
|cos x - (1 - x ^ 2 / 2)| ≤ |x| ^ 4 * (5 / 96) :=
calc |cos x - (1 - x ^ 2 / 2)| = abs (complex.cos x - (1 - x ^ 2 / 2)) :
by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv]
... = abs ((complex.exp (x * I) + complex.exp (-x * I) - (2 - x ^ 2)) / 2) :
by simp [complex.cos, sub_div, add_div, neg_div, div_self (two_ne_zero' ℂ)]
... = abs (((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) +
((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!))) / 2) :
congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin
simp only [sum_range_succ],
simp [pow_succ],
apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring
end)
... ≤ abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) / 2) +
abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) / 2) :
by rw add_div; exact complex.abs.add_le _ _
... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 +
abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) :
by simp [map_div₀]
... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 +
(complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) :
add_le_add ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial))
((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial))
... ≤ |x| ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc]
lemma sin_bound {x : ℝ} (hx : |x| ≤ 1) :
|sin x - (x - x ^ 3 / 6)| ≤ |x| ^ 4 * (5 / 96) :=
calc |sin x - (x - x ^ 3 / 6)| = abs (complex.sin x - (x - x ^ 3 / 6)) :
by rw ← abs_of_real; simp [of_real_bit0, of_real_one, of_real_inv]
... = abs (((complex.exp (-x * I) - complex.exp (x * I)) * I - (2 * x - x ^ 3 / 3)) / 2) :
by simp [complex.sin, sub_div, add_div, neg_div, mul_div_cancel_left _ (two_ne_zero' ℂ),
div_div, show (3 : ℂ) * 2 = 6, by norm_num]
... = abs ((((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) -
(complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) * I) / 2) :
congr_arg abs (congr_arg (λ x : ℂ, x / 2) begin
simp only [sum_range_succ],
simp [pow_succ],
apply complex.ext; simp [div_eq_mul_inv, norm_sq]; ring
end)
... ≤ abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!) * I / 2) +
abs (-((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!) * I) / 2) :
by rw [sub_mul, sub_eq_add_neg, add_div]; exact complex.abs.add_le _ _
... = (abs ((complex.exp (x * I) - ∑ m in range 4, (x * I) ^ m / m!)) / 2 +
abs ((complex.exp (-x * I) - ∑ m in range 4, (-x * I) ^ m / m!)) / 2) :
by simp [add_comm, map_div₀]
... ≤ ((complex.abs (x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2 +
(complex.abs (-x * I) ^ 4 * (nat.succ 4 * (4! * (4 : ℕ))⁻¹)) / 2) :
add_le_add ((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial))
((div_le_div_right (by norm_num)).2 (complex.exp_bound (by simpa) dec_trivial))
... ≤ |x| ^ 4 * (5 / 96) : by norm_num; simp [mul_assoc, mul_comm, mul_left_comm, mul_div_assoc]
lemma cos_pos_of_le_one {x : ℝ} (hx : |x| ≤ 1) : 0 < cos x :=
calc 0 < (1 - x ^ 2 / 2) - |x| ^ 4 * (5 / 96) :
sub_pos.2 $ lt_sub_iff_add_lt.2
(calc |x| ^ 4 * (5 / 96) + x ^ 2 / 2
≤ 1 * (5 / 96) + 1 / 2 :
add_le_add
(mul_le_mul_of_nonneg_right (pow_le_one _ (abs_nonneg _) hx) (by norm_num))
((div_le_div_right (by norm_num)).2 (by rw [sq, ← abs_mul_self, _root_.abs_mul];
exact mul_le_one hx (abs_nonneg _) hx))
... < 1 : by norm_num)
... ≤ cos x : sub_le_comm.1 (abs_sub_le_iff.1 (cos_bound hx)).2
lemma sin_pos_of_pos_of_le_one {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 1) : 0 < sin x :=
calc 0 < x - x ^ 3 / 6 - |x| ^ 4 * (5 / 96) :
sub_pos.2 $ lt_sub_iff_add_lt.2
(calc |x| ^ 4 * (5 / 96) + x ^ 3 / 6
≤ x * (5 / 96) + x / 6 :
add_le_add
(mul_le_mul_of_nonneg_right
(calc |x| ^ 4 ≤ |x| ^ 1 : pow_le_pow_of_le_one (abs_nonneg _)
(by rwa _root_.abs_of_nonneg (le_of_lt hx0))
dec_trivial
... = x : by simp [_root_.abs_of_nonneg (le_of_lt (hx0))]) (by norm_num))
((div_le_div_right (by norm_num)).2
(calc x ^ 3 ≤ x ^ 1 : pow_le_pow_of_le_one (le_of_lt hx0) hx dec_trivial
... = x : pow_one _))
... < x : by linarith)
... ≤ sin x : sub_le_comm.1 (abs_sub_le_iff.1 (sin_bound
(by rwa [_root_.abs_of_nonneg (le_of_lt hx0)]))).2
lemma sin_pos_of_pos_of_le_two {x : ℝ} (hx0 : 0 < x) (hx : x ≤ 2) : 0 < sin x :=
have x / 2 ≤ 1, from (div_le_iff (by norm_num)).mpr (by simpa),
calc 0 < 2 * sin (x / 2) * cos (x / 2) :
mul_pos (mul_pos (by norm_num) (sin_pos_of_pos_of_le_one (half_pos hx0) this))
(cos_pos_of_le_one (by rwa [_root_.abs_of_nonneg (le_of_lt (half_pos hx0))]))
... = sin x : by rw [← sin_two_mul, two_mul, add_halves]
lemma cos_one_le : cos 1 ≤ 2 / 3 :=
calc cos 1 ≤ |(1 : ℝ)| ^ 4 * (5 / 96) + (1 - 1 ^ 2 / 2) :
sub_le_iff_le_add.1 (abs_sub_le_iff.1 (cos_bound (by simp))).1
... ≤ 2 / 3 : by norm_num
lemma cos_one_pos : 0 < cos 1 := cos_pos_of_le_one (le_of_eq abs_one)
lemma cos_two_neg : cos 2 < 0 :=
calc cos 2 = cos (2 * 1) : congr_arg cos (mul_one _).symm
... = _ : real.cos_two_mul 1
... ≤ 2 * (2 / 3) ^ 2 - 1 : sub_le_sub_right (mul_le_mul_of_nonneg_left
(by { rw [sq, sq], exact mul_self_le_mul_self (le_of_lt cos_one_pos) cos_one_le })
zero_le_two) _
... < 0 : by norm_num
lemma exp_bound_div_one_sub_of_interval_approx {x : ℝ} (h1 : 0 ≤ x) (h2 : x ≤ 1) :
∑ (j : ℕ) in finset.range 3, x ^ j / (j.factorial)
+ x ^ 3 * ((3 : ℕ) + 1) / ((3 : ℕ).factorial * (3 : ℕ))
≤ ∑ j in (finset.range 3), x ^ j :=
begin
norm_num [finset.sum],
rw [add_assoc, add_comm (x + 1) (x ^ 3 * 4 / 18), ← add_assoc, add_le_add_iff_right,
← add_le_add_iff_left (-(x ^ 2 / 2)), ← add_assoc, comm_ring.add_left_neg (x ^ 2 / 2),
zero_add, neg_add_eq_sub, sub_half, sq, pow_succ, sq],
have i1 : x * 4 / 18 ≤ 1 / 2 := by linarith,
have i2 : 0 ≤ x * 4 / 18 := by linarith,
have i3 := mul_le_mul h1 h1 le_rfl h1,
rw zero_mul at i3,
have t := mul_le_mul le_rfl i1 i2 i3,
rw ← mul_assoc,
rwa [mul_one_div, ← mul_div_assoc, ← mul_assoc] at t,
end
lemma exp_bound_div_one_sub_of_interval {x : ℝ} (h1 : 0 ≤ x) (h2 : x < 1) :
real.exp x ≤ 1 / (1 - x) :=
begin
have h : ∑ j in (finset.range 3), x ^ j ≤ 1 / (1 - x),
{ norm_num [finset.sum],
have h1x : 0 < 1 - x := by simpa,
rw le_div_iff h1x,
norm_num [← add_assoc, mul_sub_left_distrib, mul_one, add_mul,
sub_add_eq_sub_sub, pow_succ' x 2],
have hx3 : 0 ≤ x ^ 3,
{ norm_num,
exact h1 },
linarith },
exact (exp_bound' h1 h2.le $ by linarith).trans
((exp_bound_div_one_sub_of_interval_approx h1 h2.le).trans h),
end
lemma one_sub_le_exp_minus_of_pos {y : ℝ} (h : 0 ≤ y) : 1 - y ≤ real.exp (-y) :=
begin
rw real.exp_neg,
have r1 : (1 - y) * (real.exp y) ≤ 1,
{ cases le_or_lt (1 - y) 0,
{ have h'' : (1 - y) * y.exp ≤ 0,
{ rw mul_nonpos_iff,
right,
exact ⟨h_1, y.exp_pos.le⟩ },
linarith },
have hy1 : y < 1 := by linarith,
rw ← le_div_iff' h_1,
exact exp_bound_div_one_sub_of_interval h hy1 },
rw inv_eq_one_div,
rw le_div_iff' y.exp_pos,
rwa mul_comm at r1,
end
lemma add_one_le_exp_of_nonpos {x : ℝ} (h : x ≤ 0) : x + 1 ≤ real.exp x :=
begin
rw add_comm,
have h1 : 0 ≤ -x := by linarith,
simpa using one_sub_le_exp_minus_of_pos h1
end
lemma add_one_le_exp (x : ℝ) : x + 1 ≤ real.exp x :=
begin
cases le_or_lt 0 x,
{ exact real.add_one_le_exp_of_nonneg h },
exact add_one_le_exp_of_nonpos h.le,
end
end real
namespace tactic
open positivity real
/-- Extension for the `positivity` tactic: `real.exp` is always positive. -/
@[positivity]
meta def positivity_exp : expr → tactic strictness
| `(real.exp %%a) := positive <$> mk_app `real.exp_pos [a]
| e := pp e >>= fail ∘ format.bracket "The expression `" "` isn't of the form `real.exp r`"
end tactic
namespace complex
@[simp] lemma abs_cos_add_sin_mul_I (x : ℝ) : abs (cos x + sin x * I) = 1 :=
have _ := real.sin_sq_add_cos_sq x,
by simp [add_comm, abs, norm_sq, sq, *, sin_of_real_re, cos_of_real_re, mul_re] at *
@[simp] lemma abs_exp_of_real (x : ℝ) : abs (exp x) = real.exp x :=
by rw [← of_real_exp]; exact abs_of_nonneg (le_of_lt (real.exp_pos _))
@[simp] lemma abs_exp_of_real_mul_I (x : ℝ) : abs (exp (x * I)) = 1 :=
by rw [exp_mul_I, abs_cos_add_sin_mul_I]
lemma abs_exp (z : ℂ) : abs (exp z) = real.exp z.re :=
by rw [exp_eq_exp_re_mul_sin_add_cos, map_mul, abs_exp_of_real, abs_cos_add_sin_mul_I, mul_one]
lemma abs_exp_eq_iff_re_eq {x y : ℂ} : abs (exp x) = abs (exp y) ↔ x.re = y.re :=
by rw [abs_exp, abs_exp, real.exp_eq_exp]
end complex
|
6b1bc664679d8afbea06794017dd2f3998668fc2 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/polynomial/algebra_map.lean | fc08bdd5bb11fad8a1dd50f5e5a3ccc59462f41e | [
"Apache-2.0"
] | permissive | jcommelin/mathlib | d8456447c36c176e14d96d9e76f39841f69d2d9b | ee8279351a2e434c2852345c51b728d22af5a156 | refs/heads/master | 1,664,782,136,488 | 1,663,638,983,000 | 1,663,638,983,000 | 132,563,656 | 0 | 0 | Apache-2.0 | 1,663,599,929,000 | 1,525,760,539,000 | Lean | UTF-8 | Lean | false | false | 13,906 | lean | /-
Copyright (c) 2018 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Johannes Hölzl, Scott Morrison, Jens Wagemaker
-/
import ring_theory.adjoin.basic
import data.polynomial.eval
/-!
# Theory of univariate polynomials
We show that `polynomial A` is an R-algebra when `A` is an R-algebra.
We promote `eval₂` to an algebra hom in `aeval`.
-/
noncomputable theory
open finset
open_locale big_operators polynomial
namespace polynomial
universes u v w z
variables {R : Type u} {S : Type v} {T : Type w} {A : Type z} {A' B' : Type*} {a b : R} {n : ℕ}
variables [comm_semiring A'] [semiring B']
section comm_semiring
variables [comm_semiring R] {p q r : R[X]}
variables [semiring A] [algebra R A]
/-- Note that this instance also provides `algebra R R[X]`. -/
instance algebra_of_algebra : algebra R (polynomial A) :=
{ smul_def' := λ r p, to_finsupp_injective $ begin
dsimp only [ring_hom.to_fun_eq_coe, ring_hom.comp_apply],
rw [to_finsupp_smul, to_finsupp_mul, to_finsupp_C],
exact algebra.smul_def' _ _,
end,
commutes' := λ r p, to_finsupp_injective $ begin
dsimp only [ring_hom.to_fun_eq_coe, ring_hom.comp_apply],
simp_rw [to_finsupp_mul, to_finsupp_C],
convert algebra.commutes' r p.to_finsupp,
end,
to_ring_hom := C.comp (algebra_map R A) }
lemma algebra_map_apply (r : R) :
algebra_map R (polynomial A) r = C (algebra_map R A r) :=
rfl
@[simp] lemma to_finsupp_algebra_map (r : R) :
(algebra_map R (polynomial A) r).to_finsupp = algebra_map R _ r :=
show to_finsupp (C (algebra_map _ _ r)) = _, by { rw to_finsupp_C, refl }
lemma of_finsupp_algebra_map (r : R) :
(⟨algebra_map R _ r⟩ : A[X]) = algebra_map R (polynomial A) r :=
to_finsupp_injective (to_finsupp_algebra_map _).symm
/--
When we have `[comm_semiring R]`, the function `C` is the same as `algebra_map R R[X]`.
(But note that `C` is defined when `R` is not necessarily commutative, in which case
`algebra_map` is not available.)
-/
lemma C_eq_algebra_map (r : R) :
C r = algebra_map R R[X] r :=
rfl
variables {R}
/--
Extensionality lemma for algebra maps out of `polynomial A'` over a smaller base ring than `A'`
-/
@[ext] lemma alg_hom_ext' [algebra R A'] [algebra R B']
{f g : A'[X] →ₐ[R] B'}
(h₁ : f.comp (is_scalar_tower.to_alg_hom R A' (polynomial A')) =
g.comp (is_scalar_tower.to_alg_hom R A' (polynomial A')))
(h₂ : f X = g X) : f = g :=
alg_hom.coe_ring_hom_injective (polynomial.ring_hom_ext'
(congr_arg alg_hom.to_ring_hom h₁) h₂)
variable (R)
/-- Algebra isomorphism between `polynomial R` and `add_monoid_algebra R ℕ`. This is just an
implementation detail, but it can be useful to transfer results from `finsupp` to polynomials. -/
@[simps]
def to_finsupp_iso_alg : R[X] ≃ₐ[R] add_monoid_algebra R ℕ :=
{ commutes' := λ r,
begin
dsimp,
exact to_finsupp_algebra_map _,
end,
..to_finsupp_iso R }
variable {R}
instance [nontrivial A] : nontrivial (subalgebra R (polynomial A)) :=
⟨⟨⊥, ⊤, begin
rw [ne.def, set_like.ext_iff, not_forall],
refine ⟨X, _⟩,
simp only [algebra.mem_bot, not_exists, set.mem_range, iff_true, algebra.mem_top,
algebra_map_apply, not_forall],
intro x,
rw [ext_iff, not_forall],
refine ⟨1, _⟩,
simp [coeff_C],
end⟩⟩
@[simp]
lemma alg_hom_eval₂_algebra_map
{R A B : Type*} [comm_semiring R] [semiring A] [semiring B] [algebra R A] [algebra R B]
(p : R[X]) (f : A →ₐ[R] B) (a : A) :
f (eval₂ (algebra_map R A) a p) = eval₂ (algebra_map R B) (f a) p :=
begin
dsimp [eval₂, sum],
simp only [f.map_sum, f.map_mul, f.map_pow, eq_int_cast, map_int_cast,
alg_hom.commutes],
end
@[simp]
lemma eval₂_algebra_map_X {R A : Type*} [comm_semiring R] [semiring A] [algebra R A]
(p : R[X]) (f : R[X] →ₐ[R] A) :
eval₂ (algebra_map R A) (f X) p = f p :=
begin
conv_rhs { rw [←polynomial.sum_C_mul_X_eq p], },
dsimp [eval₂, sum],
simp only [f.map_sum, f.map_mul, f.map_pow, eq_int_cast, map_int_cast],
simp [polynomial.C_eq_algebra_map],
end
-- these used to be about `algebra_map ℤ R`, but now the simp-normal form is `int.cast_ring_hom R`.
@[simp]
lemma ring_hom_eval₂_cast_int_ring_hom {R S : Type*} [ring R] [ring S]
(p : ℤ[X]) (f : R →+* S) (r : R) :
f (eval₂ (int.cast_ring_hom R) r p) = eval₂ (int.cast_ring_hom S) (f r) p :=
alg_hom_eval₂_algebra_map p f.to_int_alg_hom r
@[simp]
lemma eval₂_int_cast_ring_hom_X {R : Type*} [ring R] (p : ℤ[X]) (f : ℤ[X] →+* R) :
eval₂ (int.cast_ring_hom R) (f X) p = f p :=
eval₂_algebra_map_X p f.to_int_alg_hom
end comm_semiring
section aeval
variables [comm_semiring R] {p q : R[X]}
variables [semiring A] [algebra R A]
variables {B : Type*} [semiring B] [algebra R B]
variables (x : A)
/-- Given a valuation `x` of the variable in an `R`-algebra `A`, `aeval R A x` is
the unique `R`-algebra homomorphism from `R[X]` to `A` sending `X` to `x`.
This is a stronger variant of the linear map `polynomial.leval`. -/
def aeval : R[X] →ₐ[R] A :=
{ commutes' := λ r, eval₂_C _ _,
..eval₂_ring_hom' (algebra_map R A) x (λ a, algebra.commutes _ _) }
variables {R A}
@[simp] lemma adjoin_X : algebra.adjoin R ({X} : set R[X]) = ⊤ :=
begin
refine top_unique (λ p hp, _),
set S := algebra.adjoin R ({X} : set R[X]),
rw [← sum_monomial_eq p], simp only [monomial_eq_smul_X, sum],
exact S.sum_mem (λ n hn, S.smul_mem (S.pow_mem (algebra.subset_adjoin rfl) _) _)
end
@[ext] lemma alg_hom_ext {f g : R[X] →ₐ[R] A} (h : f X = g X) : f = g :=
alg_hom.ext_of_adjoin_eq_top adjoin_X $ λ p hp, (set.mem_singleton_iff.1 hp).symm ▸ h
theorem aeval_def (p : R[X]) : aeval x p = eval₂ (algebra_map R A) x p := rfl
@[simp] lemma aeval_zero : aeval x (0 : R[X]) = 0 :=
alg_hom.map_zero (aeval x)
@[simp] lemma aeval_X : aeval x (X : R[X]) = x := eval₂_X _ x
@[simp] lemma aeval_C (r : R) : aeval x (C r) = algebra_map R A r := eval₂_C _ x
@[simp] lemma aeval_monomial {n : ℕ} {r : R} : aeval x (monomial n r) = (algebra_map _ _ r) * x^n :=
eval₂_monomial _ _
@[simp] lemma aeval_X_pow {n : ℕ} : aeval x ((X : R[X])^n) = x^n :=
eval₂_X_pow _ _
@[simp] lemma aeval_add : aeval x (p + q) = aeval x p + aeval x q :=
alg_hom.map_add _ _ _
@[simp] lemma aeval_one : aeval x (1 : R[X]) = 1 :=
alg_hom.map_one _
@[simp] lemma aeval_bit0 : aeval x (bit0 p) = bit0 (aeval x p) :=
alg_hom.map_bit0 _ _
@[simp] lemma aeval_bit1 : aeval x (bit1 p) = bit1 (aeval x p) :=
alg_hom.map_bit1 _ _
@[simp] lemma aeval_nat_cast (n : ℕ) : aeval x (n : R[X]) = n :=
map_nat_cast _ _
lemma aeval_mul : aeval x (p * q) = aeval x p * aeval x q :=
alg_hom.map_mul _ _ _
lemma aeval_comp {A : Type*} [comm_semiring A] [algebra R A] (x : A) :
aeval x (p.comp q) = (aeval (aeval x q) p) :=
eval₂_comp (algebra_map R A)
@[simp] lemma aeval_map {A : Type*} [comm_semiring A] [algebra R A] [algebra A B]
[is_scalar_tower R A B] (b : B) (p : R[X]) :
aeval b (p.map (algebra_map R A)) = aeval b p :=
by rw [aeval_def, eval₂_map, ←is_scalar_tower.algebra_map_eq, ←aeval_def]
theorem aeval_alg_hom (f : A →ₐ[R] B) (x : A) : aeval (f x) = f.comp (aeval x) :=
alg_hom_ext $ by simp only [aeval_X, alg_hom.comp_apply]
@[simp] theorem aeval_X_left : aeval (X : R[X]) = alg_hom.id R R[X] :=
alg_hom_ext $ aeval_X X
theorem aeval_X_left_apply (p : R[X]) : aeval X p = p :=
alg_hom.congr_fun (@aeval_X_left R _) p
theorem eval_unique (φ : R[X] →ₐ[R] A) (p) :
φ p = eval₂ (algebra_map R A) (φ X) p :=
by rw [← aeval_def, aeval_alg_hom, aeval_X_left, alg_hom.comp_id]
theorem aeval_alg_hom_apply (f : A →ₐ[R] B) (x : A) (p : R[X]) :
aeval (f x) p = f (aeval x p) :=
alg_hom.ext_iff.1 (aeval_alg_hom f x) p
theorem aeval_alg_equiv (f : A ≃ₐ[R] B) (x : A) : aeval (f x) = (f : A →ₐ[R] B).comp (aeval x) :=
aeval_alg_hom (f : A →ₐ[R] B) x
theorem aeval_alg_equiv_apply (f : A ≃ₐ[R] B) (x : A) (p : R[X]) :
aeval (f x) p = f (aeval x p) :=
aeval_alg_hom_apply (f : A →ₐ[R] B) x p
lemma aeval_algebra_map_apply (x : R) (p : R[X]) :
aeval (algebra_map R A x) p = algebra_map R A (p.eval x) :=
aeval_alg_hom_apply (algebra.of_id R A) x p
@[simp] lemma coe_aeval_eq_eval (r : R) :
(aeval r : R[X] → R) = eval r := rfl
@[simp] lemma aeval_fn_apply {X : Type*} (g : R[X]) (f : X → R) (x : X) :
((aeval f) g) x = aeval (f x) g :=
(aeval_alg_hom_apply (pi.eval_alg_hom _ _ x) f g).symm
@[norm_cast] lemma aeval_subalgebra_coe
(g : R[X]) {A : Type*} [semiring A] [algebra R A] (s : subalgebra R A) (f : s) :
(aeval f g : A) = aeval (f : A) g :=
(aeval_alg_hom_apply s.val f g).symm
lemma coeff_zero_eq_aeval_zero (p : R[X]) : p.coeff 0 = aeval 0 p :=
by simp [coeff_zero_eq_eval_zero]
lemma coeff_zero_eq_aeval_zero' (p : R[X]) :
algebra_map R A (p.coeff 0) = aeval (0 : A) p :=
by simp [aeval_def]
variable (R)
theorem _root_.algebra.adjoin_singleton_eq_range_aeval (x : A) :
algebra.adjoin R {x} = (polynomial.aeval x).range :=
by rw [← algebra.map_top, ← adjoin_X, alg_hom.map_adjoin, set.image_singleton, aeval_X]
variable {R}
section comm_semiring
variables [comm_semiring S] {f : R →+* S}
lemma aeval_eq_sum_range [algebra R S] {p : R[X]} (x : S) :
aeval x p = ∑ i in finset.range (p.nat_degree + 1), p.coeff i • x ^ i :=
by { simp_rw algebra.smul_def, exact eval₂_eq_sum_range (algebra_map R S) x }
lemma aeval_eq_sum_range' [algebra R S] {p : R[X]} {n : ℕ} (hn : p.nat_degree < n) (x : S) :
aeval x p = ∑ i in finset.range n, p.coeff i • x ^ i :=
by { simp_rw algebra.smul_def, exact eval₂_eq_sum_range' (algebra_map R S) hn x }
lemma is_root_of_eval₂_map_eq_zero
(hf : function.injective f) {r : R} : eval₂ f (f r) p = 0 → p.is_root r :=
begin
intro h,
apply hf,
rw [←eval₂_hom, h, f.map_zero],
end
lemma is_root_of_aeval_algebra_map_eq_zero [algebra R S] {p : R[X]}
(inj : function.injective (algebra_map R S))
{r : R} (hr : aeval (algebra_map R S r) p = 0) : p.is_root r :=
is_root_of_eval₂_map_eq_zero inj hr
section aeval_tower
variables [algebra S R] [algebra S A'] [algebra S B']
/-- Version of `aeval` for defining algebra homs out of `polynomial R` over a smaller base ring
than `R`. -/
def aeval_tower (f : R →ₐ[S] A') (x : A') : R[X] →ₐ[S] A' :=
{ commutes' := λ r, by simp [algebra_map_apply],
..eval₂_ring_hom ↑f x }
variables (g : R →ₐ[S] A') (y : A')
@[simp] lemma aeval_tower_X : aeval_tower g y X = y := eval₂_X _ _
@[simp] lemma aeval_tower_C (x : R) : aeval_tower g y (C x) = g x := eval₂_C _ _
@[simp] lemma aeval_tower_comp_C : ((aeval_tower g y : R[X] →+* A').comp C) = g :=
ring_hom.ext $ aeval_tower_C _ _
@[simp] lemma aeval_tower_algebra_map (x : R) :
aeval_tower g y (algebra_map R R[X] x) = g x := eval₂_C _ _
@[simp] lemma aeval_tower_comp_algebra_map :
(aeval_tower g y : R[X] →+* A').comp (algebra_map R R[X]) = g :=
aeval_tower_comp_C _ _
lemma aeval_tower_to_alg_hom (x : R) :
aeval_tower g y (is_scalar_tower.to_alg_hom S R R[X] x) = g x :=
aeval_tower_algebra_map _ _ _
@[simp] lemma aeval_tower_comp_to_alg_hom :
(aeval_tower g y).comp (is_scalar_tower.to_alg_hom S R R[X]) = g :=
alg_hom.coe_ring_hom_injective $ aeval_tower_comp_algebra_map _ _
@[simp] lemma aeval_tower_id : aeval_tower (alg_hom.id S S) = aeval :=
by { ext, simp only [eval_X, aeval_tower_X, coe_aeval_eq_eval], }
@[simp] lemma aeval_tower_of_id : aeval_tower (algebra.of_id S A') = aeval :=
by { ext, simp only [aeval_X, aeval_tower_X], }
end aeval_tower
end comm_semiring
section comm_ring
variables [comm_ring S] {f : R →+* S}
lemma dvd_term_of_dvd_eval_of_dvd_terms {z p : S} {f : S[X]} (i : ℕ)
(dvd_eval : p ∣ f.eval z) (dvd_terms : ∀ (j ≠ i), p ∣ f.coeff j * z ^ j) :
p ∣ f.coeff i * z ^ i :=
begin
by_cases hi : i ∈ f.support,
{ rw [eval, eval₂, sum] at dvd_eval,
rw [←finset.insert_erase hi, finset.sum_insert (finset.not_mem_erase _ _)] at dvd_eval,
refine (dvd_add_left _).mp dvd_eval,
apply finset.dvd_sum,
intros j hj,
exact dvd_terms j (finset.ne_of_mem_erase hj) },
{ convert dvd_zero p,
rw not_mem_support_iff at hi,
simp [hi] }
end
lemma dvd_term_of_is_root_of_dvd_terms {r p : S} {f : S[X]} (i : ℕ)
(hr : f.is_root r) (h : ∀ (j ≠ i), p ∣ f.coeff j * r ^ j) : p ∣ f.coeff i * r ^ i :=
dvd_term_of_dvd_eval_of_dvd_terms i (eq.symm hr ▸ dvd_zero p) h
end comm_ring
end aeval
section ring
variables [ring R]
/--
The evaluation map is not generally multiplicative when the coefficient ring is noncommutative,
but nevertheless any polynomial of the form `p * (X - monomial 0 r)` is sent to zero
when evaluated at `r`.
This is the key step in our proof of the Cayley-Hamilton theorem.
-/
lemma eval_mul_X_sub_C {p : R[X]} (r : R) :
(p * (X - C r)).eval r = 0 :=
begin
simp only [eval, eval₂, ring_hom.id_apply],
have bound := calc
(p * (X - C r)).nat_degree
≤ p.nat_degree + (X - C r).nat_degree : nat_degree_mul_le
... ≤ p.nat_degree + 1 : add_le_add_left (nat_degree_X_sub_C_le _) _
... < p.nat_degree + 2 : lt_add_one _,
rw sum_over_range' _ _ (p.nat_degree + 2) bound,
swap,
{ simp, },
rw sum_range_succ',
conv_lhs
{ congr, apply_congr, skip,
rw [coeff_mul_X_sub_C, sub_mul, mul_assoc, ←pow_succ], },
simp [sum_range_sub', coeff_monomial],
end
theorem not_is_unit_X_sub_C [nontrivial R] (r : R) : ¬ is_unit (X - C r) :=
λ ⟨⟨_, g, hfg, hgf⟩, rfl⟩, @zero_ne_one R _ _ $ by erw [← eval_mul_X_sub_C, hgf, eval_one]
end ring
lemma aeval_endomorphism {M : Type*}
[comm_ring R] [add_comm_group M] [module R M]
(f : M →ₗ[R] M) (v : M) (p : R[X]) :
aeval f p v = p.sum (λ n b, b • (f ^ n) v) :=
begin
rw [aeval_def, eval₂],
exact (linear_map.applyₗ v).map_sum,
end
end polynomial
|
c4cdde343ecb53f028e89de70351a4208139c334 | 8d65764a9e5f0923a67fc435eb1a5a1d02fd80e3 | /src/algebra/ring/basic.lean | a89bc409c4feb4af0cb74c0dd89fba395cadb13b | [
"Apache-2.0"
] | permissive | troyjlee/mathlib | e18d4b8026e32062ab9e89bc3b003a5d1cfec3f5 | 45e7eb8447555247246e3fe91c87066506c14875 | refs/heads/master | 1,689,248,035,046 | 1,629,470,528,000 | 1,629,470,528,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 47,644 | 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, Yury Kudryashov,
Neil Strickland
-/
import algebra.divisibility
import algebra.regular.basic
import data.set.basic
/-!
# Properties and homomorphisms of semirings and rings
This file proves simple properties of semirings, rings and domains and their unit groups. It also
defines bundled homomorphisms of semirings and rings. As with monoid and groups, we use the same
structure `ring_hom a β`, a.k.a. `α →+* β`, for both homomorphism types.
The unbundled homomorphisms are defined in `deprecated/ring`. They are deprecated and the plan is to
slowly remove them from mathlib.
## Main definitions
ring_hom, nonzero, domain, integral_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.
## Tags
`ring_hom`, `semiring_hom`, `semiring`, `comm_semiring`, `ring`, `comm_ring`, `domain`,
`integral_domain`, `nonzero`, `units`
-/
universes u v w x
variables {α : Type u} {β : Type v} {γ : Type w} {R : Type x}
set_option old_structure_cmd true
open function
/-!
### `distrib` class
-/
/-- A typeclass stating that multiplication is left and right distributive
over addition. -/
@[protect_proj, ancestor has_mul has_add]
class distrib (R : Type*) extends has_mul R, has_add R :=
(left_distrib : ∀ a b c : R, a * (b + c) = (a * b) + (a * c))
(right_distrib : ∀ a b c : R, (a + b) * c = (a * c) + (b * c))
lemma left_distrib [distrib R] (a b c : R) : a * (b + c) = a * b + a * c :=
distrib.left_distrib a b c
alias left_distrib ← mul_add
lemma right_distrib [distrib R] (a b c : R) : (a + b) * c = a * c + b * c :=
distrib.right_distrib a b c
alias right_distrib ← add_mul
/-- Pullback a `distrib` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.distrib {S} [has_mul R] [has_add R] [distrib S]
(f : R → S) (hf : injective f) (add : ∀ x y, f (x + y) = f x + f y)
(mul : ∀ x y, f (x * y) = f x * f y) :
distrib R :=
{ mul := (*),
add := (+),
left_distrib := λ x y z, hf $ by simp only [*, left_distrib],
right_distrib := λ x y z, hf $ by simp only [*, right_distrib] }
/-- Pushforward a `distrib` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.distrib {S} [distrib R] [has_add S] [has_mul S]
(f : R → S) (hf : surjective f) (add : ∀ x y, f (x + y) = f x + f y)
(mul : ∀ x y, f (x * y) = f x * f y) :
distrib S :=
{ mul := (*),
add := (+),
left_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, left_distrib],
right_distrib := hf.forall₃.2 $ λ x y z, by simp only [← add, ← mul, right_distrib] }
/-!
### Semirings
-/
/-- A not-necessarily-unital, not-necessarily-associative semiring. -/
@[protect_proj, ancestor add_comm_monoid distrib mul_zero_class]
class non_unital_non_assoc_semiring (α : Type u) extends
add_comm_monoid α, distrib α, mul_zero_class α
/-- An associative but not-necessarily unital semiring. -/
@[protect_proj, ancestor non_unital_non_assoc_semiring semigroup_with_zero]
class non_unital_semiring (α : Type u) extends
non_unital_non_assoc_semiring α, semigroup_with_zero α
/-- A unital but not-necessarily-associative semiring. -/
@[protect_proj, ancestor non_unital_non_assoc_semiring mul_zero_one_class]
class non_assoc_semiring (α : Type u) extends
non_unital_non_assoc_semiring α, mul_zero_one_class α
/-- A semiring is a type with the following structures: additive commutative monoid
(`add_comm_monoid`), multiplicative monoid (`monoid`), distributive laws (`distrib`), and
multiplication by zero law (`mul_zero_class`). The actual definition extends `monoid_with_zero`
instead of `monoid` and `mul_zero_class`. -/
@[protect_proj, ancestor non_unital_semiring non_assoc_semiring monoid_with_zero]
class semiring (α : Type u) extends non_unital_semiring α, non_assoc_semiring α, monoid_with_zero α
section injective_surjective_maps
variables [has_zero β] [has_add β] [has_mul β]
/-- Pullback a `non_unital_non_assoc_semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_unital_non_assoc_semiring
{α : Type u} [non_unital_non_assoc_semiring α]
(f : β → α) (hf : injective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
non_unital_non_assoc_semiring β :=
{ .. hf.mul_zero_class f zero mul, .. hf.add_comm_monoid f zero add, .. hf.distrib f add mul }
/-- Pullback a `non_unital_semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_unital_semiring
{α : Type u} [non_unital_semiring α]
(f : β → α) (hf : injective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
non_unital_semiring β :=
{ .. hf.non_unital_non_assoc_semiring f zero add mul, .. hf.semigroup_with_zero f zero mul }
/-- Pullback a `non_assoc_semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.non_assoc_semiring
{α : Type u} [non_assoc_semiring α] [has_one β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
non_assoc_semiring β :=
{ .. hf.non_unital_non_assoc_semiring f zero add mul, .. hf.mul_one_class f one mul }
/-- Pullback a `semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.semiring
{α : Type u} [semiring α] [has_one β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
semiring β :=
{ .. hf.monoid_with_zero f zero one mul, .. hf.add_comm_monoid f zero add,
.. hf.distrib f add mul }
/-- Pushforward a `non_unital_non_assoc_semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_unital_non_assoc_semiring
{α : Type u} [non_unital_non_assoc_semiring α]
(f : α → β) (hf : surjective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
non_unital_non_assoc_semiring β :=
{ .. hf.mul_zero_class f zero mul, .. hf.add_comm_monoid f zero add, .. hf.distrib f add mul }
/-- Pushforward a `non_unital_semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_unital_semiring
{α : Type u} [non_unital_semiring α]
(f : α → β) (hf : surjective f) (zero : f 0 = 0)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
non_unital_semiring β :=
{ .. hf.non_unital_non_assoc_semiring f zero add mul, .. hf.semigroup_with_zero f zero mul }
/-- Pushforward a `non_assoc_semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.non_assoc_semiring
{α : Type u} [non_assoc_semiring α] [has_one β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
non_assoc_semiring β :=
{ .. hf.non_unital_non_assoc_semiring f zero add mul, .. hf.mul_one_class f one mul }
/-- Pushforward a `semiring` instance along a surjective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.semiring
{α : Type u} [semiring α] [has_one β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
semiring β :=
{ .. hf.monoid_with_zero f zero one mul, .. hf.add_comm_monoid f zero add,
.. hf.distrib f add mul }
end injective_surjective_maps
section semiring
variables [semiring α]
lemma one_add_one_eq_two : 1 + 1 = (2 : α) :=
by unfold bit0
theorem two_mul (n : α) : 2 * n = n + n :=
eq.trans (right_distrib 1 1 n) (by simp)
lemma distrib_three_right (a b c d : α) : (a + b + c) * d = a * d + b * d + c * d :=
by simp [right_distrib]
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 `∑ x in s, (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
@[simp] lemma mul_boole {α} [non_assoc_semiring α] (P : Prop) [decidable P] (a : α) :
a * (if P then 1 else 0) = if P then a else 0 :=
by simp
@[simp] lemma boole_mul {α} [non_assoc_semiring α] (P : Prop) [decidable P] (a : α) :
(if P then 1 else 0) * a = if P then a else 0 :=
by simp
lemma ite_mul_zero_left {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) :
ite P (a * b) 0 = ite P a 0 * b :=
by { by_cases h : P; simp [h], }
lemma ite_mul_zero_right {α : Type*} [mul_zero_class α] (P : Prop) [decidable P] (a b : α) :
ite P (a * b) 0 = a * ite P b 0 :=
by { by_cases h : P; simp [h], }
/-- An element `a` of a semiring is even if there exists `k` such `a = 2*k`. -/
def even (a : α) : Prop := ∃ k, a = 2*k
lemma even_iff_two_dvd {a : α} : even a ↔ 2 ∣ a := iff.rfl
@[simp] lemma range_two_mul (α : Type*) [semiring α] :
set.range (λ x : α, 2 * x) = {a | even a} :=
by { ext x, simp [even, eq_comm] }
@[simp] lemma even_bit0 (a : α) : even (bit0 a) :=
⟨a, by rw [bit0, two_mul]⟩
/-- An element `a` of a semiring is odd if there exists `k` such `a = 2*k + 1`. -/
def odd (a : α) : Prop := ∃ k, a = 2*k + 1
@[simp] lemma odd_bit1 (a : α) : odd (bit1 a) :=
⟨a, by rw [bit1, bit0, two_mul]⟩
@[simp] lemma range_two_mul_add_one (α : Type*) [semiring α] :
set.range (λ x : α, 2 * x + 1) = {a | odd a} :=
by { ext x, simp [odd, eq_comm] }
theorem dvd_add {a b c : α} (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b + c :=
dvd.elim h₁ (λ d hd, dvd.elim h₂ (λ e he, dvd.intro (d + e) (by simp [left_distrib, hd, he])))
end semiring
namespace add_monoid_hom
/-- Left multiplication by an element of a (semi)ring is an `add_monoid_hom` -/
def mul_left {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : R →+ R :=
{ to_fun := (*) r,
map_zero' := mul_zero r,
map_add' := mul_add r }
@[simp] lemma coe_mul_left {R : Type*} [non_unital_non_assoc_semiring R] (r : R) :
⇑(mul_left r) = (*) r := rfl
/-- Right multiplication by an element of a (semi)ring is an `add_monoid_hom` -/
def mul_right {R : Type*} [non_unital_non_assoc_semiring R] (r : R) : R →+ R :=
{ to_fun := λ a, a * r,
map_zero' := zero_mul r,
map_add' := λ _ _, add_mul _ _ r }
@[simp] lemma coe_mul_right {R : Type*} [non_unital_non_assoc_semiring R] (r : R) :
⇑(mul_right r) = (* r) := rfl
lemma mul_right_apply {R : Type*} [non_unital_non_assoc_semiring R] (a r : R) :
mul_right r a = a * r := rfl
end add_monoid_hom
/-- Bundled semiring homomorphisms; use this for bundled ring homomorphisms too.
This extends from both `monoid_hom` and `monoid_with_zero_hom` in order to put the fields in a
sensible order, even though `monoid_with_zero_hom` already extends `monoid_hom`. -/
structure ring_hom (α : Type*) (β : Type*) [non_assoc_semiring α] [non_assoc_semiring β]
extends monoid_hom α β, add_monoid_hom α β, monoid_with_zero_hom α β
infixr ` →+* `:25 := ring_hom
/-- Reinterpret a ring homomorphism `f : R →+* S` as a `monoid_with_zero_hom R S`.
The `simp`-normal form is `(f : monoid_with_zero_hom R S)`. -/
add_decl_doc ring_hom.to_monoid_with_zero_hom
/-- Reinterpret a ring homomorphism `f : R →+* S` as a monoid homomorphism `R →* S`.
The `simp`-normal form is `(f : R →* S)`. -/
add_decl_doc ring_hom.to_monoid_hom
/-- Reinterpret a ring homomorphism `f : R →+* S` as an additive monoid homomorphism `R →+ S`.
The `simp`-normal form is `(f : R →+ S)`. -/
add_decl_doc ring_hom.to_add_monoid_hom
namespace ring_hom
section coe
/-!
Throughout this section, some `semiring` arguments are specified with `{}` instead of `[]`.
See note [implicit instance arguments].
-/
variables {rα : non_assoc_semiring α} {rβ : non_assoc_semiring β}
include rα rβ
instance : has_coe_to_fun (α →+* β) := ⟨_, ring_hom.to_fun⟩
initialize_simps_projections ring_hom (to_fun → apply)
@[simp] lemma to_fun_eq_coe (f : α →+* β) : f.to_fun = f := rfl
@[simp] lemma coe_mk (f : α → β) (h₁ h₂ h₃ h₄) : ⇑(⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) = f := rfl
instance has_coe_monoid_hom : has_coe (α →+* β) (α →* β) := ⟨ring_hom.to_monoid_hom⟩
@[simp, norm_cast] lemma coe_monoid_hom (f : α →+* β) : ⇑(f : α →* β) = f := rfl
@[simp] lemma to_monoid_hom_eq_coe (f : α →+* β) : f.to_monoid_hom = f := rfl
@[simp] lemma to_monoid_with_zero_hom_eq_coe (f : α →+* β) :
(f.to_monoid_with_zero_hom : α → β) = f := rfl
@[simp] lemma coe_monoid_hom_mk (f : α → β) (h₁ h₂ h₃ h₄) :
((⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) : α →* β) = ⟨f, h₁, h₂⟩ :=
rfl
instance has_coe_add_monoid_hom : has_coe (α →+* β) (α →+ β) := ⟨ring_hom.to_add_monoid_hom⟩
@[simp, norm_cast] lemma coe_add_monoid_hom (f : α →+* β) : ⇑(f : α →+ β) = f := rfl
@[simp] lemma to_add_monoid_hom_eq_coe (f : α →+* β) : f.to_add_monoid_hom = f := rfl
@[simp] lemma coe_add_monoid_hom_mk (f : α → β) (h₁ h₂ h₃ h₄) :
((⟨f, h₁, h₂, h₃, h₄⟩ : α →+* β) : α →+ β) = ⟨f, h₃, h₄⟩ :=
rfl
end coe
variables [rα : non_assoc_semiring α] [rβ : non_assoc_semiring β]
section
include rα rβ
variables (f : α →+* β) {x y : α} {rα rβ}
theorem congr_fun {f g : α →+* β} (h : f = g) (x : α) : f x = g x :=
congr_arg (λ h : α →+* β, h x) h
theorem congr_arg (f : α →+* β) {x y : α} (h : x = y) : f x = f y :=
congr_arg (λ x : α, f x) h
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⟩
@[simp] lemma mk_coe (f : α →+* β) (h₁ h₂ h₃ h₄) : ring_hom.mk f h₁ h₂ h₃ h₄ = f :=
ext $ λ _, rfl
theorem coe_add_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →+ β)) :=
λ f g h, ext (λ x, add_monoid_hom.congr_fun h x)
theorem coe_monoid_hom_injective : function.injective (coe : (α →+* β) → (α →* β)) :=
λ f g h, ext (λ x, monoid_hom.congr_fun h x)
/-- 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
/-- Ring homomorphisms preserve `bit0`. -/
@[simp] lemma map_bit0 (f : α →+* β) (a : α) : f (bit0 a) = bit0 (f a) := map_add _ _ _
/-- Ring homomorphisms preserve `bit1`. -/
@[simp] lemma map_bit1 (f : α →+* β) (a : α) : f (bit1 a) = bit1 (f a) :=
by simp [bit1]
/-- `f : R →+* S` has a trivial codomain iff `f 1 = 0`. -/
lemma codomain_trivial_iff_map_one_eq_zero : (0 : β) = 1 ↔ f 1 = 0 :=
by rw [map_one, eq_comm]
/-- `f : R →+* S` has a trivial codomain iff it has a trivial range. -/
lemma codomain_trivial_iff_range_trivial : (0 : β) = 1 ↔ (∀ x, f x = 0) :=
f.codomain_trivial_iff_map_one_eq_zero.trans
⟨λ h x, by rw [←mul_one x, map_mul, h, mul_zero], λ h, h 1⟩
/-- `f : R →+* S` has a trivial codomain iff its range is `{0}`. -/
lemma codomain_trivial_iff_range_eq_singleton_zero : (0 : β) = 1 ↔ set.range f = {0} :=
f.codomain_trivial_iff_range_trivial.trans
⟨ λ h, set.ext (λ y, ⟨λ ⟨x, hx⟩, by simp [←hx, h x], λ hy, ⟨0, by simpa using hy.symm⟩⟩),
λ h x, set.mem_singleton_iff.mp (h ▸ set.mem_range_self x)⟩
/-- `f : R →+* S` doesn't map `1` to `0` if `S` is nontrivial -/
lemma map_one_ne_zero [nontrivial β] : f 1 ≠ 0 :=
mt f.codomain_trivial_iff_map_one_eq_zero.mpr zero_ne_one
/-- If there is a homomorphism `f : R →+* S` and `S` is nontrivial, then `R` is nontrivial. -/
lemma domain_nontrivial [nontrivial β] : nontrivial α :=
⟨⟨1, 0, mt (λ h, show f 1 = 0, by rw [h, map_zero]) f.map_one_ne_zero⟩⟩
end
lemma is_unit_map [semiring α] [semiring β] (f : α →+* β) {a : α} (h : is_unit a) : is_unit (f a) :=
h.map f.to_monoid_hom
/-- The identity ring homomorphism from a semiring to itself. -/
def id (α : Type*) [non_assoc_semiring α] : α →+* α :=
by refine {to_fun := id, ..}; intros; refl
include rα
instance : inhabited (α →+* α) := ⟨id α⟩
@[simp] lemma id_apply (x : α) : ring_hom.id α x = x := rfl
@[simp] lemma coe_add_monoid_hom_id : (id α : α →+ α) = add_monoid_hom.id α := rfl
@[simp] lemma coe_monoid_hom_id : (id α : α →* α) = monoid_hom.id α := rfl
variable {rγ : non_assoc_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δ: non_assoc_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
omit rγ
@[simp] lemma comp_id (f : α →+* β) : f.comp (id α) = f := ext $ λ x, rfl
@[simp] lemma id_comp (f : α →+* β) : (id β).comp f = f := ext $ λ x, rfl
omit rβ
instance : monoid (α →+* α) :=
{ one := id α,
mul := comp,
mul_one := comp_id,
one_mul := id_comp,
mul_assoc := λ f g h, comp_assoc _ _ _ }
lemma one_def : (1 : α →+* α) = id α := rfl
@[simp] lemma coe_one : ⇑(1 : α →+* α) = _root_.id := rfl
lemma mul_def (f g : α →+* α) : f * g = f.comp g := rfl
@[simp] lemma coe_mul (f g : α →+* α) : ⇑(f * g) = f ∘ g := rfl
include rβ rγ
lemma cancel_right {g₁ g₂ : β →+* γ} {f : α →+* β} (hf : 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 : 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γ
end ring_hom
section semiring
variables [semiring α] {a : α}
@[simp] theorem two_dvd_bit0 : 2 ∣ bit0 a := ⟨a, bit0_eq_two_mul _⟩
lemma ring_hom.map_dvd [semiring β] (f : α →+* β) {a b : α} : a ∣ b → f a ∣ f b :=
f.to_monoid_hom.map_dvd
end semiring
/-- A commutative semiring is a `semiring` with commutative multiplication. In other words, it is a
type with the following structures: additive commutative monoid (`add_comm_monoid`), multiplicative
commutative monoid (`comm_monoid`), distributive laws (`distrib`), and multiplication by zero law
(`mul_zero_class`). -/
@[protect_proj, ancestor semiring comm_monoid]
class comm_semiring (α : Type u) extends semiring α, comm_monoid α
@[priority 100] -- see Note [lower instance priority]
instance comm_semiring.to_comm_monoid_with_zero [comm_semiring α] : comm_monoid_with_zero α :=
{ .. comm_semiring.to_comm_monoid α, .. comm_semiring.to_semiring α }
section comm_semiring
variables [comm_semiring α] [comm_semiring β] {a b c : α}
/-- Pullback a `semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.comm_semiring [has_zero γ] [has_one γ] [has_add γ] [has_mul γ]
(f : γ → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
comm_semiring γ :=
{ .. hf.semiring f zero one add mul, .. hf.comm_semigroup f mul }
/-- Pullback a `semiring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.comm_semiring [has_zero γ] [has_one γ] [has_add γ] [has_mul γ]
(f : α → γ) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y) :
comm_semiring γ :=
{ .. hf.semiring f zero one add mul, .. hf.comm_semigroup f mul }
lemma add_mul_self_eq (a b : α) : (a + b) * (a + b) = a*a + 2*a*b + b*b :=
by simp only [two_mul, add_mul, mul_add, add_assoc, mul_comm b]
end comm_semiring
/-!
### Rings
-/
/-- A ring is a type with the following structures: additive commutative group (`add_comm_group`),
multiplicative monoid (`monoid`), and distributive laws (`distrib`). Equivalently, a ring is a
`semiring` with a negation operation making it an additive group. -/
@[protect_proj, ancestor add_comm_group monoid distrib]
class ring (α : Type u) extends add_comm_group α, monoid α, distrib α
section ring
variables [ring α] {a b c d e : α}
/- The instance from `ring` to `semiring` happens often in linear algebra, for which all the basic
definitions are given in terms of semirings, but many applications use rings or fields. We increase
a little bit its priority above 100 to try it quickly, but remaining below the default 1000 so that
more specific instances are tried first. -/
@[priority 200]
instance ring.to_semiring : semiring α :=
{ zero_mul := λ a, add_left_cancel $ show 0 * a + 0 * a = 0 * a + 0,
by rw [← add_mul, zero_add, add_zero],
mul_zero := λ a, add_left_cancel $ show a * 0 + a * 0 = a * 0 + 0,
by rw [← mul_add, add_zero, add_zero],
..‹ring α› }
/-- Pullback a `ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
ring β :=
{ .. hf.add_comm_group f zero add neg sub, .. hf.monoid f one mul, .. hf.distrib f add mul }
/-- Pullback a `ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
ring β :=
{ .. hf.add_comm_group f zero add neg sub, .. hf.monoid f one mul, .. hf.distrib f add mul }
lemma neg_mul_eq_neg_mul (a b : α) : -(a * b) = -a * b :=
neg_eq_of_add_eq_zero
begin rw [← right_distrib, add_right_neg, zero_mul] end
lemma neg_mul_eq_mul_neg (a b : α) : -(a * b) = a * -b :=
neg_eq_of_add_eq_zero
begin rw [← left_distrib, add_right_neg, mul_zero] end
@[simp] lemma neg_mul_eq_neg_mul_symm (a b : α) : - a * b = - (a * b) :=
eq.symm (neg_mul_eq_neg_mul a b)
@[simp] lemma mul_neg_eq_neg_mul_symm (a b : α) : a * - b = - (a * b) :=
eq.symm (neg_mul_eq_mul_neg a b)
lemma neg_mul_neg (a b : α) : -a * -b = a * b :=
by simp
lemma neg_mul_comm (a b : α) : -a * b = a * -b :=
by simp
theorem neg_eq_neg_one_mul (a : α) : -a = -1 * a :=
by simp
lemma mul_sub_left_distrib (a b c : α) : a * (b - c) = a * b - a * c :=
by simpa only [sub_eq_add_neg, neg_mul_eq_mul_neg] using mul_add a b (-c)
alias mul_sub_left_distrib ← mul_sub
lemma mul_sub_right_distrib (a b c : α) : (a - b) * c = a * c - b * c :=
by simpa only [sub_eq_add_neg, neg_mul_eq_neg_mul] using add_mul a (-b) c
alias mul_sub_right_distrib ← sub_mul
/-- 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
end ring
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, norm_cast] protected theorem coe_neg (u : units α) : (↑-u : α) = -u := rfl
@[simp, norm_cast] protected theorem coe_neg_one : ((-1 : units α) : α) = -1 := 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
lemma is_unit.neg [ring α] {a : α} : is_unit a → is_unit (-a)
| ⟨x, hx⟩ := hx ▸ (-x).is_unit
namespace ring_hom
/-- Ring homomorphisms preserve additive inverse. -/
@[simp] theorem map_neg {α β} [ring α] [ring β] (f : α →+* β) (x : α) : f (-x) = -(f x) :=
(f : α →+ β).map_neg x
/-- Ring homomorphisms preserve subtraction. -/
@[simp] theorem map_sub {α β} [ring α] [ring β] (f : α →+* β) (x y : α) :
f (x - y) = (f x) - (f y) := (f : α →+ β).map_sub x y
/-- A ring homomorphism is injective iff its kernel is trivial. -/
theorem injective_iff {α β} [ring α] [non_assoc_semiring β] (f : α →+* β) :
function.injective f ↔ (∀ a, f a = 0 → a = 0) :=
(f : α →+ β).injective_iff
/-- A ring homomorphism is injective iff its kernel is trivial. -/
theorem injective_iff' {α β} [ring α] [non_assoc_semiring β] (f : α →+* β) :
function.injective f ↔ (∀ a, f a = 0 ↔ a = 0) :=
(f : α →+ β).injective_iff'
/-- Makes a ring homomorphism from a monoid homomorphism of rings which preserves addition. -/
def mk' {γ} [non_assoc_semiring α] [ring γ] (f : α →* γ)
(map_add : ∀ a b : α, f (a + b) = f a + f b) :
α →+* γ :=
{ to_fun := f,
.. add_monoid_hom.mk' f map_add, .. f }
end ring_hom
/-- A commutative ring is a `ring` with commutative multiplication. -/
@[protect_proj, ancestor ring comm_semigroup]
class comm_ring (α : Type u) extends ring α, comm_monoid α
@[priority 100] -- see Note [lower instance priority]
instance comm_ring.to_comm_semiring [s : comm_ring α] : comm_semiring α :=
{ mul_zero := mul_zero, zero_mul := zero_mul, ..s }
section ring
variables [ring α] {a b c : α}
theorem dvd_neg_of_dvd (h : a ∣ b) : (a ∣ -b) :=
dvd.elim h
(assume c, assume : b = a * c,
dvd.intro (-c) (by simp [this]))
theorem dvd_of_dvd_neg (h : a ∣ -b) : (a ∣ b) :=
let t := dvd_neg_of_dvd h in by rwa neg_neg at t
/-- An element a of a 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⟩
theorem neg_dvd_of_dvd (h : a ∣ b) : -a ∣ b :=
dvd.elim h
(assume c, assume : b = a * c,
dvd.intro (-c) (by simp [this]))
theorem dvd_of_neg_dvd (h : -a ∣ b) : a ∣ b :=
let t := neg_dvd_of_dvd h in by rwa neg_neg at t
/-- The additive inverse of an element a of a 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⟩
theorem dvd_sub (h₁ : a ∣ b) (h₂ : a ∣ c) : a ∣ b - c :=
by { rw sub_eq_add_neg, exact dvd_add h₁ (dvd_neg_of_dvd h₂) }
theorem dvd_add_iff_left (h : a ∣ c) : a ∣ b ↔ a ∣ b + c :=
⟨λh₂, dvd_add h₂ h, λH, by have t := dvd_sub H h; rwa add_sub_cancel at t⟩
theorem dvd_add_iff_right (h : a ∣ b) : a ∣ c ↔ a ∣ b + c :=
by rw add_comm; exact dvd_add_iff_left h
theorem two_dvd_bit1 : 2 ∣ bit1 a ↔ (2 : α) ∣ 1 := (dvd_add_iff_right (@two_dvd_bit0 _ _ a)).symm
/-- 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 (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 (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)
lemma dvd_iff_dvd_of_dvd_sub {a b c : α} (h : a ∣ (b - c)) : (a ∣ b ↔ a ∣ c) :=
begin
split,
{ intro h',
convert dvd_sub h' h,
exact eq.symm (sub_sub_self b c) },
{ intro h',
convert dvd_add h h',
exact eq_add_of_sub_eq rfl }
end
@[simp] theorem even_neg (a : α) : even (-a) ↔ even a :=
dvd_neg _ _
end ring
section comm_ring
variables [comm_ring α] {a b c : α}
/-- Pullback a `comm_ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.comm_ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
comm_ring β :=
{ .. hf.ring f zero one add mul neg sub, .. hf.comm_semigroup f mul }
/-- Pullback a `comm_ring` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.surjective.comm_ring
[has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β] [has_sub β]
(f : α → β) (hf : surjective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
comm_ring β :=
{ .. hf.ring f zero one add mul neg sub, .. hf.comm_semigroup f mul }
local attribute [simp] add_assoc add_comm add_left_comm mul_comm
/-- 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]
lemma mul_self_sub_one (a : α) : a * a - 1 = (a + 1) * (a - 1) :=
by rw [← mul_self_sub_mul_self, mul_one]
/-- 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
lemma dvd_mul_sub_mul {k a b x y : α} (hab : k ∣ a - b) (hxy : k ∣ x - y) :
k ∣ a * x - b * y :=
begin
convert dvd_add (dvd_mul_of_dvd_right hxy a) (dvd_mul_of_dvd_left hab y),
rw [mul_sub_left_distrib, mul_sub_right_distrib],
simp only [sub_eq_add_neg, add_assoc, neg_add_cancel_left],
end
end comm_ring
lemma succ_ne_self [ring α] [nontrivial α] (a : α) : a + 1 ≠ a :=
λ h, one_ne_zero ((add_right_inj a).mp (by simp [h]))
lemma pred_ne_self [ring α] [nontrivial α] (a : α) : a - 1 ≠ a :=
λ h, one_ne_zero (neg_injective ((add_right_inj a).mp (by simpa [sub_eq_add_neg] using h)))
/-- Left `mul` by a `k : α` over `[ring α]` is injective, if `k` is not a zero divisor.
The typeclass that restricts all terms of `α` to have this property is `no_zero_divisors`. -/
lemma is_left_regular_of_non_zero_divisor [ring α] (k : α)
(h : ∀ (x : α), k * x = 0 → x = 0) : is_left_regular k :=
begin
intros x y h',
rw ←sub_eq_zero,
refine h _ _,
rw [mul_sub, sub_eq_zero, h']
end
/-- Right `mul` by a `k : α` over `[ring α]` is injective, if `k` is not a zero divisor.
The typeclass that restricts all terms of `α` to have this property is `no_zero_divisors`. -/
lemma is_right_regular_of_non_zero_divisor [ring α] (k : α)
(h : ∀ (x : α), x * k = 0 → x = 0) : is_right_regular k :=
begin
intros x y h',
simp only at h',
rw ←sub_eq_zero,
refine h _ _,
rw [sub_mul, sub_eq_zero, h']
end
lemma is_regular_of_ne_zero' [ring α] [no_zero_divisors α] {k : α} (hk : k ≠ 0) :
is_regular k :=
⟨is_left_regular_of_non_zero_divisor k
(λ x h, (no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero h).resolve_left hk),
is_right_regular_of_non_zero_divisor k
(λ x h, (no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero h).resolve_right hk)⟩
/-- 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. -/
@[protect_proj] class domain (α : Type u) extends ring α, nontrivial α :=
(eq_zero_or_eq_zero_of_mul_eq_zero : ∀ a b : α, a * b = 0 → a = 0 ∨ b = 0)
section domain
variable [domain α]
@[priority 100] -- see Note [lower instance priority]
instance domain.to_no_zero_divisors : no_zero_divisors α :=
⟨domain.eq_zero_or_eq_zero_of_mul_eq_zero⟩
@[priority 100] -- see Note [lower instance priority]
instance domain.to_cancel_monoid_with_zero : cancel_monoid_with_zero α :=
{ mul_left_cancel_of_ne_zero := λ a b c ha,
@is_regular.left _ _ _ (is_regular_of_ne_zero' ha) _ _,
mul_right_cancel_of_ne_zero := λ a b c hb,
@is_regular.right _ _ _ (is_regular_of_ne_zero' hb) _ _,
.. (infer_instance : semiring α) }
/-- Pullback a `domain` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.domain [has_zero β] [has_one β] [has_add β] [has_mul β] [has_neg β]
[has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
domain β :=
{ .. hf.ring f zero one add mul neg sub, .. pullback_nonzero f zero one,
.. hf.no_zero_divisors f zero mul }
end domain
/-!
### Integral domains
-/
/-- An integral domain is a commutative ring with no zero divisors, i.e. satisfying the condition
`a * b = 0 ↔ a = 0 ∨ b = 0`. Alternatively, an integral domain is a domain with commutative
multiplication. -/
@[protect_proj, ancestor comm_ring domain]
class integral_domain (α : Type u) extends comm_ring α, domain α
section integral_domain
variables [integral_domain α] {a b c d e : α}
@[priority 100] -- see Note [lower instance priority]
instance integral_domain.to_comm_cancel_monoid_with_zero : comm_cancel_monoid_with_zero α :=
{ ..comm_semiring.to_comm_monoid_with_zero, ..domain.to_cancel_monoid_with_zero }
/-- Pullback an `integral_domain` instance along an injective function.
See note [reducible non-instances]. -/
@[reducible]
protected def function.injective.integral_domain [has_zero β] [has_one β] [has_add β] [has_mul β]
[has_neg β] [has_sub β] (f : β → α) (hf : injective f) (zero : f 0 = 0) (one : f 1 = 1)
(add : ∀ x y, f (x + y) = f x + f y) (mul : ∀ x y, f (x * y) = f x * f y)
(neg : ∀ x, f (-x) = -f x) (sub : ∀ x y, f (x - y) = f x - f y) :
integral_domain β :=
{ .. hf.comm_ring f zero one add mul neg sub, .. hf.domain f zero one add mul neg sub }
lemma mul_self_eq_mul_self_iff {a b : α} : a * a = b * b ↔ a = b ∨ a = -b :=
by rw [← sub_eq_zero, mul_self_sub_mul_self, mul_eq_zero, or_comm, sub_eq_zero,
add_eq_zero_iff_eq_neg]
lemma mul_self_eq_one_iff {a : α} : a * a = 1 ↔ a = 1 ∨ a = -1 :=
by rw [← mul_self_eq_mul_self_iff, one_mul]
/-- 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 { rw inv_eq_iff_mul_eq_one, simp only [units.ext_iff], push_cast, exact mul_self_eq_one_iff }
/--
Makes a ring homomorphism from an additive group homomorphism from a commutative ring to an integral
domain that commutes with self multiplication, assumes that two is nonzero and one is sent to one.
-/
def add_monoid_hom.mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α)
(h : ∀ x, f (x * x) = f x * f x) (h_two : (2 : α) ≠ 0) (h_one : f 1 = 1) : β →+* α :=
{ map_one' := h_one,
map_mul' := begin
intros x y,
have hxy := h (x + y),
rw [mul_add, add_mul, add_mul, f.map_add, f.map_add, f.map_add, f.map_add, h x, h y, add_mul,
mul_add, mul_add, ← sub_eq_zero, add_comm, ← sub_sub, ← sub_sub, ← sub_sub,
mul_comm y x, mul_comm (f y) (f x)] at hxy,
simp only [add_assoc, add_sub_assoc, add_sub_cancel'_right] at hxy,
rw [sub_sub, ← two_mul, ← add_sub_assoc, ← two_mul, ← mul_sub, mul_eq_zero, sub_eq_zero,
or_iff_not_imp_left] at hxy,
exact hxy h_two,
end,
..f }
@[simp]
lemma add_monoid_hom.coe_fn_mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β] (f : β →+ α)
(h h_two h_one) :
(f.mk_ring_hom_of_mul_self_of_two_ne_zero h h_two h_one : β → α) = f := rfl
@[simp]
lemma add_monoid_hom.coe_add_monoid_hom_mk_ring_hom_of_mul_self_of_two_ne_zero [comm_ring β]
(f : β →+ α) (h h_two h_one) :
(f.mk_ring_hom_of_mul_self_of_two_ne_zero h h_two h_one : β →+ α) = f := by {ext, simp}
end integral_domain
namespace ring
variables {M₀ : Type*} [monoid_with_zero M₀]
open_locale classical
/-- Introduce a function `inverse` on a monoid with zero `M₀`, which sends `x` to `x⁻¹` if `x` is
invertible and to `0` otherwise. This definition is somewhat ad hoc, but one needs a fully (rather
than partially) defined inverse function for some purposes, including for calculus.
Note that while this is in the `ring` namespace for brevity, it requires the weaker assumption
`monoid_with_zero M₀` instead of `ring M₀`. -/
noncomputable def inverse : M₀ → M₀ :=
λ x, if h : is_unit x then (((classical.some h)⁻¹ : units M₀) : M₀) else 0
/-- By definition, if `x` is invertible then `inverse x = x⁻¹`. -/
@[simp] lemma inverse_unit (u : units M₀) : inverse (u : M₀) = (u⁻¹ : units M₀) :=
begin
simp only [units.is_unit, inverse, dif_pos],
exact units.inv_unique (classical.some_spec u.is_unit)
end
/-- By definition, if `x` is not invertible then `inverse x = 0`. -/
@[simp] lemma inverse_non_unit (x : M₀) (h : ¬(is_unit x)) : inverse x = 0 := dif_neg h
end ring
/-- 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] extends nontrivial 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)
-- The linter does not recognize that is_integral_domain.to_nontrivial is a structure
-- projection, disable it
attribute [nolint def_lemma doc_blame] is_integral_domain.to_nontrivial
/-- 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) }
namespace semiconj_by
@[simp] lemma add_right [distrib R] {a x y x' y' : R}
(h : semiconj_by a x y) (h' : semiconj_by a x' y') :
semiconj_by a (x + x') (y + y') :=
by simp only [semiconj_by, left_distrib, right_distrib, h.eq, h'.eq]
@[simp] lemma add_left [distrib R] {a b x y : R}
(ha : semiconj_by a x y) (hb : semiconj_by b x y) :
semiconj_by (a + b) x y :=
by simp only [semiconj_by, left_distrib, right_distrib, ha.eq, hb.eq]
variables [ring R] {a b x y x' y' : R}
lemma neg_right (h : semiconj_by a x y) : semiconj_by a (-x) (-y) :=
by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm]
@[simp] lemma neg_right_iff : semiconj_by a (-x) (-y) ↔ semiconj_by a x y :=
⟨λ h, neg_neg x ▸ neg_neg y ▸ h.neg_right, semiconj_by.neg_right⟩
lemma neg_left (h : semiconj_by a x y) : semiconj_by (-a) x y :=
by simp only [semiconj_by, h.eq, neg_mul_eq_neg_mul_symm, mul_neg_eq_neg_mul_symm]
@[simp] lemma neg_left_iff : semiconj_by (-a) x y ↔ semiconj_by a x y :=
⟨λ h, neg_neg a ▸ h.neg_left, semiconj_by.neg_left⟩
@[simp] lemma neg_one_right (a : R) : semiconj_by a (-1) (-1) :=
(one_right a).neg_right
@[simp] lemma neg_one_left (x : R) : semiconj_by (-1) x x :=
(semiconj_by.one_left x).neg_left
@[simp] lemma sub_right (h : semiconj_by a x y) (h' : semiconj_by a x' y') :
semiconj_by a (x - x') (y - y') :=
by simpa only [sub_eq_add_neg] using h.add_right h'.neg_right
@[simp] lemma sub_left (ha : semiconj_by a x y) (hb : semiconj_by b x y) :
semiconj_by (a - b) x y :=
by simpa only [sub_eq_add_neg] using ha.add_left hb.neg_left
end semiconj_by
namespace commute
@[simp] theorem add_right [distrib R] {a b c : R} :
commute a b → commute a c → commute a (b + c) :=
semiconj_by.add_right
@[simp] theorem add_left [distrib R] {a b c : R} :
commute a c → commute b c → commute (a + b) c :=
semiconj_by.add_left
lemma bit0_right [distrib R] {x y : R} (h : commute x y) : commute x (bit0 y) :=
h.add_right h
lemma bit0_left [distrib R] {x y : R} (h : commute x y) : commute (bit0 x) y :=
h.add_left h
lemma bit1_right [semiring R] {x y : R} (h : commute x y) : commute x (bit1 y) :=
h.bit0_right.add_right (commute.one_right x)
lemma bit1_left [semiring R] {x y : R} (h : commute x y) : commute (bit1 x) y :=
h.bit0_left.add_left (commute.one_left y)
variables [ring R] {a b c : R}
theorem neg_right : commute a b → commute a (- b) := semiconj_by.neg_right
@[simp] theorem neg_right_iff : commute a (-b) ↔ commute a b := semiconj_by.neg_right_iff
theorem neg_left : commute a b → commute (- a) b := semiconj_by.neg_left
@[simp] theorem neg_left_iff : commute (-a) b ↔ commute a b := semiconj_by.neg_left_iff
@[simp] theorem neg_one_right (a : R) : commute a (-1) := semiconj_by.neg_one_right a
@[simp] theorem neg_one_left (a : R): commute (-1) a := semiconj_by.neg_one_left a
@[simp] theorem sub_right : commute a b → commute a c → commute a (b - c) := semiconj_by.sub_right
@[simp] theorem sub_left : commute a c → commute b c → commute (a - b) c := semiconj_by.sub_left
end commute
|
b3e40b5753dbe61f123c73c46e687a34c6bcf87e | c3de33d4701e6113627153fe1103b255e752ed7d | /algebra/lattice/bounded_lattice.lean | 211ac7401c822886a9a21ab88cbeb267fee66d26 | [] | no_license | jroesch/library_dev | 77d2b246ff47ab05d55cb9706a37d3de97038388 | 4faa0a45c6aa7eee6e661113c2072b8840bff79b | refs/heads/master | 1,611,281,606,352 | 1,495,661,644,000 | 1,495,661,644,000 | 92,340,430 | 0 | 0 | null | 1,495,663,344,000 | 1,495,663,344,000 | null | UTF-8 | Lean | false | false | 2,990 | 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
Defines bounded lattice type class hierarchy.
Includes the Prop and fun instances.
-/
import .basic
set_option old_structure_cmd true
universes u v
variable {α : Type u}
namespace lattice
/- Bounded lattices -/
class bounded_lattice (α : Type u) extends lattice α, order_top α, order_bot α
instance semilattice_inf_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_top α :=
{ bl with le_top := take x, @le_top α _ x }
instance semilattice_inf_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_inf_bot α :=
{ bl with bot_le := take x, @bot_le α _ x }
instance semilattice_sup_top_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_top α :=
{ bl with le_top := take x, @le_top α _ x }
instance semilattice_sup_bot_of_bounded_lattice (α : Type u) [bl : bounded_lattice α] : semilattice_sup_bot α :=
{ bl with bot_le := take x, @bot_le α _ x }
/- Prop instance -/
instance bounded_lattice_Prop : bounded_lattice Prop :=
{ lattice.bounded_lattice .
le := λa b, a → b,
le_refl := take _, id,
le_trans := take a b c f g, g ∘ f,
le_antisymm := take a b Hab Hba, propext ⟨Hab, Hba⟩,
sup := or,
le_sup_left := @or.inl,
le_sup_right := @or.inr,
sup_le := take a b c, or.rec,
inf := and,
inf_le_left := @and.left,
inf_le_right := @and.right,
le_inf := take a b c Hab Hac Ha, and.intro (Hab Ha) (Hac Ha),
top := true,
le_top := take a Ha, true.intro,
bot := false,
bot_le := @false.elim }
section logic
variable [weak_order α]
lemma monotone_and {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) :
monotone (λx, p x ∧ q x) :=
take a b h, and.imp (m_p h) (m_q h)
lemma monotone_or {p q : α → Prop} (m_p : monotone p) (m_q : monotone q) :
monotone (λx, p x ∨ q x) :=
take a b h, or.imp (m_p h) (m_q h)
end logic
/- Function lattices -/
/- TODO:
* build up the lattice hierarchy for `fun`-functor piecewise. semilattic_*, bounded_lattice, lattice ...
* can this be generalized to the dependent function space?
-/
instance bounded_lattice_fun {α : Type u} {β : Type v} [bounded_lattice β] :
bounded_lattice (α → β) :=
{ weak_order_fun with
sup := λf g a, sup (f a) (g a),
le_sup_left := take f g a, le_sup_left,
le_sup_right := take f g a, le_sup_right,
sup_le := take f g h Hfg Hfh a, sup_le (Hfg a) (Hfh a),
inf := λf g a, inf (f a) (g a),
inf_le_left := take f g a, inf_le_left,
inf_le_right := take f g a, inf_le_right,
le_inf := take f g h Hfg Hfh a, le_inf (Hfg a) (Hfh a),
top := λa, top,
le_top := take f a, le_top,
bot := λa, bot,
bot_le := take f a, bot_le }
end lattice
|
94125063f838fdd313af972c0d61ea920b88f81d | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/combinatorics/simple_graph/subgraph.lean | 8ca062543d1c22537f4782155cb418011fcc3441 | [
"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 | 27,158 | lean | /-
Copyright (c) 2021 Hunter Monroe. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Hunter Monroe, Kyle Miller, Alena Gusakov
-/
import combinatorics.simple_graph.basic
/-!
# Subgraphs of a simple graph
A subgraph of a simple graph consists of subsets of the graph's vertices and edges such that the
endpoints of each edge are present in the vertex subset. The edge subset is formalized as a
sub-relation of the adjacency relation of the simple graph.
## Main definitions
* `subgraph G` is the type of subgraphs of a `G : simple_graph`
* `subgraph.neighbor_set`, `subgraph.incidence_set`, and `subgraph.degree` are like their
`simple_graph` counterparts, but they refer to vertices from `G` to avoid subtype coercions.
* `subgraph.coe` is the coercion from a `G' : subgraph G` to a `simple_graph G'.verts`.
(This cannot be a `has_coe` instance since the destination type depends on `G'`.)
* `subgraph.is_spanning` for whether a subgraph is a spanning subgraph and
`subgraph.is_induced` for whether a subgraph is an induced subgraph.
* Instances for `lattice (subgraph G)` and `bounded_order (subgraph G)`.
* `simple_graph.to_subgraph`: If a `simple_graph` is a subgraph of another, then you can turn it
into a member of the larger graph's `simple_graph.subgraph` type.
* Graph homomorphisms from a subgraph to a graph (`subgraph.map_top`) and between subgraphs
(`subgraph.map`).
## Implementation notes
* Recall that subgraphs are not determined by their vertex sets, so `set_like` does not apply to
this kind of subobject.
## Todo
* Images of graph homomorphisms as subgraphs.
-/
universes u v
namespace simple_graph
/-- A subgraph of a `simple_graph` is a subset of vertices along with a restriction of the adjacency
relation that is symmetric and is supported by the vertex subset. They also form a bounded lattice.
Thinking of `V → V → Prop` as `set (V × V)`, a set of darts (i.e., half-edges), then
`subgraph.adj_sub` is that the darts of a subgraph are a subset of the darts of `G`. -/
@[ext]
structure subgraph {V : Type u} (G : simple_graph V) :=
(verts : set V)
(adj : V → V → Prop)
(adj_sub : ∀ {v w : V}, adj v w → G.adj v w)
(edge_vert : ∀ {v w : V}, adj v w → v ∈ verts)
(symm : symmetric adj . obviously)
namespace subgraph
variables {V : Type u} {W : Type v} {G : simple_graph V}
protected lemma loopless (G' : subgraph G) : irreflexive G'.adj :=
λ v h, G.loopless v (G'.adj_sub h)
lemma adj_comm (G' : subgraph G) (v w : V) : G'.adj v w ↔ G'.adj w v :=
⟨λ x, G'.symm x, λ x, G'.symm x⟩
@[symm] lemma adj_symm (G' : subgraph G) {u v : V} (h : G'.adj u v) : G'.adj v u := G'.symm h
protected lemma adj.symm {G' : subgraph G} {u v : V} (h : G'.adj u v) : G'.adj v u := G'.symm h
/-- Coercion from `G' : subgraph G` to a `simple_graph ↥G'.verts`. -/
@[simps] protected def coe (G' : subgraph G) : simple_graph G'.verts :=
{ adj := λ v w, G'.adj v w,
symm := λ v w h, G'.symm h,
loopless := λ v h, loopless G v (G'.adj_sub h) }
@[simp] lemma coe_adj_sub (G' : subgraph G) (u v : G'.verts) (h : G'.coe.adj u v) : G.adj u v :=
G'.adj_sub h
/-- A subgraph is called a *spanning subgraph* if it contains all the vertices of `G`. --/
def is_spanning (G' : subgraph G) : Prop := ∀ (v : V), v ∈ G'.verts
lemma is_spanning_iff {G' : subgraph G} : G'.is_spanning ↔ G'.verts = set.univ :=
set.eq_univ_iff_forall.symm
/-- Coercion from `subgraph G` to `simple_graph V`. If `G'` is a spanning
subgraph, then `G'.spanning_coe` yields an isomorphic graph.
In general, this adds in all vertices from `V` as isolated vertices. -/
@[simps] protected def spanning_coe (G' : subgraph G) : simple_graph V :=
{ adj := G'.adj,
symm := G'.symm,
loopless := λ v hv, G.loopless v (G'.adj_sub hv) }
@[simp] lemma adj.of_spanning_coe {G' : subgraph G} {u v : G'.verts}
(h : G'.spanning_coe.adj u v) : G.adj u v := G'.adj_sub h
/-- `spanning_coe` is equivalent to `coe` for a subgraph that `is_spanning`. -/
@[simps] def spanning_coe_equiv_coe_of_spanning (G' : subgraph G) (h : G'.is_spanning) :
G'.spanning_coe ≃g G'.coe :=
{ to_fun := λ v, ⟨v, h v⟩,
inv_fun := λ v, v,
left_inv := λ v, rfl,
right_inv := λ ⟨v, hv⟩, rfl,
map_rel_iff' := λ v w, iff.rfl }
/-- A subgraph is called an *induced subgraph* if vertices of `G'` are adjacent if
they are adjacent in `G`. -/
def is_induced (G' : subgraph G) : Prop :=
∀ {v w : V}, v ∈ G'.verts → w ∈ G'.verts → G.adj v w → G'.adj v w
/-- `H.support` is the set of vertices that form edges in the subgraph `H`. -/
def support (H : subgraph G) : set V := rel.dom H.adj
lemma mem_support (H : subgraph G) {v : V} : v ∈ H.support ↔ ∃ w, H.adj v w := iff.rfl
lemma support_subset_verts (H : subgraph G) : H.support ⊆ H.verts := λ v ⟨w, h⟩, H.edge_vert h
/-- `G'.neighbor_set v` is the set of vertices adjacent to `v` in `G'`. -/
def neighbor_set (G' : subgraph G) (v : V) : set V := set_of (G'.adj v)
lemma neighbor_set_subset (G' : subgraph G) (v : V) : G'.neighbor_set v ⊆ G.neighbor_set v :=
λ w h, G'.adj_sub h
lemma neighbor_set_subset_verts (G' : subgraph G) (v : V) : G'.neighbor_set v ⊆ G'.verts :=
λ _ h, G'.edge_vert (adj_symm G' h)
@[simp] lemma mem_neighbor_set (G' : subgraph G) (v w : V) : w ∈ G'.neighbor_set v ↔ G'.adj v w :=
iff.rfl
/-- A subgraph as a graph has equivalent neighbor sets. -/
def coe_neighbor_set_equiv {G' : subgraph G} (v : G'.verts) :
G'.coe.neighbor_set v ≃ G'.neighbor_set v :=
{ to_fun := λ w, ⟨w, by { obtain ⟨w', hw'⟩ := w, simpa using hw' }⟩,
inv_fun := λ w, ⟨⟨w, G'.edge_vert (G'.adj_symm w.2)⟩, by simpa using w.2⟩,
left_inv := λ w, by simp,
right_inv := λ w, by simp }
/-- The edge set of `G'` consists of a subset of edges of `G`. -/
def edge_set (G' : subgraph G) : set (sym2 V) := sym2.from_rel G'.symm
lemma edge_set_subset (G' : subgraph G) : G'.edge_set ⊆ G.edge_set :=
λ e, quotient.ind (λ e h, G'.adj_sub h) e
@[simp]
lemma mem_edge_set {G' : subgraph G} {v w : V} : ⟦(v, w)⟧ ∈ G'.edge_set ↔ G'.adj v w := iff.rfl
lemma mem_verts_if_mem_edge {G' : subgraph G} {e : sym2 V} {v : V}
(he : e ∈ G'.edge_set) (hv : v ∈ e) : v ∈ G'.verts :=
begin
refine quotient.ind (λ e he hv, _) e he hv,
cases e with v w,
simp only [mem_edge_set] at he,
cases sym2.mem_iff.mp hv with h h; subst h,
{ exact G'.edge_vert he, },
{ exact G'.edge_vert (G'.symm he), },
end
/-- The `incidence_set` is the set of edges incident to a given vertex. -/
def incidence_set (G' : subgraph G) (v : V) : set (sym2 V) := {e ∈ G'.edge_set | v ∈ e}
lemma incidence_set_subset_incidence_set (G' : subgraph G) (v : V) :
G'.incidence_set v ⊆ G.incidence_set v :=
λ e h, ⟨G'.edge_set_subset h.1, h.2⟩
lemma incidence_set_subset (G' : subgraph G) (v : V) : G'.incidence_set v ⊆ G'.edge_set :=
λ _ h, h.1
/-- Give a vertex as an element of the subgraph's vertex type. -/
@[reducible]
def vert (G' : subgraph G) (v : V) (h : v ∈ G'.verts) : G'.verts := ⟨v, h⟩
/--
Create an equal copy of a subgraph (see `copy_eq`) with possibly different definitional equalities.
See Note [range copy pattern].
-/
def copy (G' : subgraph G)
(V'' : set V) (hV : V'' = G'.verts)
(adj' : V → V → Prop) (hadj : adj' = G'.adj) :
subgraph G :=
{ verts := V'',
adj := adj',
adj_sub := λ _ _, hadj.symm ▸ G'.adj_sub,
edge_vert := λ _ _, hV.symm ▸ hadj.symm ▸ G'.edge_vert,
symm := hadj.symm ▸ G'.symm }
lemma copy_eq (G' : subgraph G)
(V'' : set V) (hV : V'' = G'.verts)
(adj' : V → V → Prop) (hadj : adj' = G'.adj) :
G'.copy V'' hV adj' hadj = G' :=
subgraph.ext _ _ hV hadj
/-- The union of two subgraphs. -/
def union (x y : subgraph G) : subgraph G :=
{ verts := x.verts ∪ y.verts,
adj := x.adj ⊔ y.adj,
adj_sub := λ v w h, or.cases_on h (λ h, x.adj_sub h) (λ h, y.adj_sub h),
edge_vert := λ v w h, or.cases_on h (λ h, or.inl (x.edge_vert h)) (λ h, or.inr (y.edge_vert h)),
symm := λ v w h, by rwa [pi.sup_apply, pi.sup_apply, x.adj_comm, y.adj_comm] }
/-- The intersection of two subgraphs. -/
def inter (x y : subgraph G) : subgraph G :=
{ verts := x.verts ∩ y.verts,
adj := x.adj ⊓ y.adj,
adj_sub := λ v w h, x.adj_sub h.1,
edge_vert := λ v w h, ⟨x.edge_vert h.1, y.edge_vert h.2⟩,
symm := λ v w h, by rwa [pi.inf_apply, pi.inf_apply, x.adj_comm, y.adj_comm] }
/-- The `top` subgraph is `G` as a subgraph of itself. -/
def top : subgraph G :=
{ verts := set.univ,
adj := G.adj,
adj_sub := λ v w h, h,
edge_vert := λ v w h, set.mem_univ v,
symm := G.symm }
/-- The `bot` subgraph is the subgraph with no vertices or edges. -/
def bot : subgraph G :=
{ verts := ∅,
adj := ⊥,
adj_sub := λ v w h, false.rec _ h,
edge_vert := λ v w h, false.rec _ h,
symm := λ u v h, h }
/-- The relation that one subgraph is a subgraph of another. -/
def is_subgraph (x y : subgraph G) : Prop := x.verts ⊆ y.verts ∧ ∀ ⦃v w : V⦄, x.adj v w → y.adj v w
instance : lattice (subgraph G) :=
{ le := is_subgraph,
sup := union,
inf := inter,
le_refl := λ x, ⟨rfl.subset, λ _ _ h, h⟩,
le_trans := λ x y z hxy hyz, ⟨hxy.1.trans hyz.1, λ _ _ h, hyz.2 (hxy.2 h)⟩,
le_antisymm := begin
intros x y hxy hyx,
ext1 v,
exact set.subset.antisymm hxy.1 hyx.1,
ext v w,
exact iff.intro (λ h, hxy.2 h) (λ h, hyx.2 h),
end,
sup_le := λ x y z hxy hyz,
⟨set.union_subset hxy.1 hyz.1,
(λ v w h, h.cases_on (λ h, hxy.2 h) (λ h, hyz.2 h))⟩,
le_sup_left := λ x y, ⟨set.subset_union_left x.verts y.verts, (λ v w h, or.inl h)⟩,
le_sup_right := λ x y, ⟨set.subset_union_right x.verts y.verts, (λ v w h, or.inr h)⟩,
le_inf := λ x y z hxy hyz, ⟨set.subset_inter hxy.1 hyz.1, (λ v w h, ⟨hxy.2 h, hyz.2 h⟩)⟩,
inf_le_left := λ x y, ⟨set.inter_subset_left x.verts y.verts, (λ v w h, h.1)⟩,
inf_le_right := λ x y, ⟨set.inter_subset_right x.verts y.verts, (λ v w h, h.2)⟩ }
instance : bounded_order (subgraph G) :=
{ top := top,
bot := bot,
le_top := λ x, ⟨set.subset_univ _, (λ v w h, x.adj_sub h)⟩,
bot_le := λ x, ⟨set.empty_subset _, (λ v w h, false.rec _ h)⟩ }
@[simps] instance subgraph_inhabited : inhabited (subgraph G) := ⟨⊥⟩
-- TODO simp lemmas for the other lattice operations on subgraphs
@[simp] lemma top_verts : (⊤ : subgraph G).verts = set.univ := rfl
@[simp] lemma top_adj_iff {v w : V} : (⊤ : subgraph G).adj v w ↔ G.adj v w := iff.rfl
@[simp] lemma bot_verts : (⊥ : subgraph G).verts = ∅ := rfl
@[simp] lemma not_bot_adj {v w : V} : ¬(⊥ : subgraph G).adj v w := not_false
@[simp] lemma inf_adj {H₁ H₂ : subgraph G} {v w : V} :
(H₁ ⊓ H₂).adj v w ↔ H₁.adj v w ∧ H₂.adj v w := iff.rfl
@[simp] lemma sup_adj {H₁ H₂ : subgraph G} {v w : V} :
(H₁ ⊔ H₂).adj v w ↔ H₁.adj v w ∨ H₂.adj v w := iff.rfl
@[simp] lemma edge_set_top : (⊤ : subgraph G).edge_set = G.edge_set := rfl
@[simp] lemma edge_set_bot : (⊥ : subgraph G).edge_set = ∅ :=
set.ext $ sym2.ind (by simp)
@[simp] lemma edge_set_inf {H₁ H₂ : subgraph G} : (H₁ ⊓ H₂).edge_set = H₁.edge_set ∩ H₂.edge_set :=
set.ext $ sym2.ind (by simp)
@[simp] lemma edge_set_sup {H₁ H₂ : subgraph G} : (H₁ ⊔ H₂).edge_set = H₁.edge_set ∪ H₂.edge_set :=
set.ext $ sym2.ind (by simp)
@[simp] lemma spanning_coe_top : (⊤ : subgraph G).spanning_coe = G :=
by { ext, refl }
@[simp] lemma spanning_coe_bot : (⊥ : subgraph G).spanning_coe = ⊥ := rfl
/-- Turn a subgraph of a `simple_graph` into a member of its subgraph type. -/
@[simps] def _root_.simple_graph.to_subgraph (H : simple_graph V) (h : H ≤ G) : G.subgraph :=
{ verts := set.univ,
adj := H.adj,
adj_sub := h,
edge_vert := λ v w h, set.mem_univ v,
symm := H.symm }
lemma support_mono {H H' : subgraph G} (h : H ≤ H') : H.support ⊆ H'.support :=
rel.dom_mono h.2
lemma _root_.simple_graph.to_subgraph.is_spanning (H : simple_graph V) (h : H ≤ G) :
(H.to_subgraph h).is_spanning := set.mem_univ
lemma spanning_coe_le_of_le {H H' : subgraph G} (h : H ≤ H') :
H.spanning_coe ≤ H'.spanning_coe := h.2
/-- The top of the `subgraph G` lattice is equivalent to the graph itself. -/
def top_equiv : (⊤ : subgraph G).coe ≃g G :=
{ to_fun := λ v, ↑v,
inv_fun := λ v, ⟨v, trivial⟩,
left_inv := λ ⟨v, _⟩, rfl,
right_inv := λ v, rfl,
map_rel_iff' := λ a b, iff.rfl }
/-- The bottom of the `subgraph G` lattice is equivalent to the empty graph on the empty
vertex type. -/
def bot_equiv : (⊥ : subgraph G).coe ≃g (⊥ : simple_graph empty) :=
{ to_fun := λ v, v.property.elim,
inv_fun := λ v, v.elim,
left_inv := λ ⟨_, h⟩, h.elim,
right_inv := λ v, v.elim,
map_rel_iff' := λ a b, iff.rfl }
lemma edge_set_mono {H₁ H₂ : subgraph G} (h : H₁ ≤ H₂) : H₁.edge_set ≤ H₂.edge_set :=
λ e, sym2.ind h.2 e
lemma _root_.disjoint.edge_set {H₁ H₂ : subgraph G}
(h : disjoint H₁ H₂) : disjoint H₁.edge_set H₂.edge_set :=
by simpa using edge_set_mono h
/-- Graph homomorphisms induce a covariant function on subgraphs. -/
@[simps]
protected def map {G' : simple_graph W} (f : G →g G') (H : G.subgraph) : G'.subgraph :=
{ verts := f '' H.verts,
adj := relation.map H.adj f f,
adj_sub := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact f.map_rel (H.adj_sub h) },
edge_vert := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact set.mem_image_of_mem _ (H.edge_vert h) },
symm := by { rintro _ _ ⟨u, v, h, rfl, rfl⟩, exact ⟨v, u, H.symm h, rfl, rfl⟩ } }
lemma map_monotone {G' : simple_graph W} (f : G →g G') : monotone (subgraph.map f) :=
begin
intros H H' h,
split,
{ intro,
simp only [map_verts, set.mem_image, forall_exists_index, and_imp],
rintro v hv rfl,
exact ⟨_, h.1 hv, rfl⟩ },
{ rintros _ _ ⟨u, v, ha, rfl, rfl⟩,
exact ⟨_, _, h.2 ha, rfl, rfl⟩ }
end
/-- Graph homomorphisms induce a contravariant function on subgraphs. -/
@[simps]
protected def comap {G' : simple_graph W} (f : G →g G') (H : G'.subgraph) : G.subgraph :=
{ verts := f ⁻¹' H.verts,
adj := λ u v, G.adj u v ∧ H.adj (f u) (f v),
adj_sub := by { rintros v w ⟨ga, ha⟩, exact ga },
edge_vert := by { rintros v w ⟨ga, ha⟩, simp [H.edge_vert ha] } }
lemma comap_monotone {G' : simple_graph W} (f : G →g G') : monotone (subgraph.comap f) :=
begin
intros H H' h,
split,
{ intro,
simp only [comap_verts, set.mem_preimage],
apply h.1, },
{ intros v w,
simp only [comap_adj, and_imp, true_and] { contextual := tt },
intro,
apply h.2, }
end
lemma map_le_iff_le_comap {G' : simple_graph W} (f : G →g G') (H : G.subgraph) (H' : G'.subgraph) :
H.map f ≤ H' ↔ H ≤ H'.comap f :=
begin
refine ⟨λ h, ⟨λ v hv, _, λ v w hvw, _⟩, λ h, ⟨λ v, _, λ v w, _⟩⟩,
{ simp only [comap_verts, set.mem_preimage],
exact h.1 ⟨v, hv, rfl⟩, },
{ simp only [H.adj_sub hvw, comap_adj, true_and],
exact h.2 ⟨v, w, hvw, rfl, rfl⟩, },
{ simp only [map_verts, set.mem_image, forall_exists_index, and_imp],
rintro w hw rfl,
exact h.1 hw, },
{ simp only [relation.map, map_adj, forall_exists_index, and_imp],
rintros u u' hu rfl rfl,
have := h.2 hu,
simp only [comap_adj] at this,
exact this.2, }
end
/-- Given two subgraphs, one a subgraph of the other, there is an induced injective homomorphism of
the subgraphs as graphs. -/
@[simps]
def inclusion {x y : subgraph G} (h : x ≤ y) : x.coe →g y.coe :=
{ to_fun := λ v, ⟨↑v, and.left h v.property⟩,
map_rel' := λ v w hvw, h.2 hvw }
lemma inclusion.injective {x y : subgraph G} (h : x ≤ y) : function.injective (inclusion h) :=
λ v w h, by { simp only [inclusion, rel_hom.coe_fn_mk, subtype.mk_eq_mk] at h, exact subtype.ext h }
/-- There is an induced injective homomorphism of a subgraph of `G` into `G`. -/
@[simps]
protected def hom (x : subgraph G) : x.coe →g G :=
{ to_fun := λ v, v,
map_rel' := λ v w hvw, x.adj_sub hvw }
lemma hom.injective {x : subgraph G} : function.injective x.hom :=
λ v w h, subtype.ext h
/-- There is an induced injective homomorphism of a subgraph of `G` as
a spanning subgraph into `G`. -/
@[simps] def spanning_hom (x : subgraph G) : x.spanning_coe →g G :=
{ to_fun := id,
map_rel' := λ v w hvw, x.adj_sub hvw }
lemma spanning_hom.injective {x : subgraph G} : function.injective x.spanning_hom :=
λ v w h, h
lemma neighbor_set_subset_of_subgraph {x y : subgraph G} (h : x ≤ y) (v : V) :
x.neighbor_set v ⊆ y.neighbor_set v :=
λ w h', h.2 h'
instance neighbor_set.decidable_pred (G' : subgraph G) [h : decidable_rel G'.adj] (v : V) :
decidable_pred (∈ G'.neighbor_set v) := h v
/-- If a graph is locally finite at a vertex, then so is a subgraph of that graph. -/
instance finite_at {G' : subgraph G} (v : G'.verts) [decidable_rel G'.adj]
[fintype (G.neighbor_set v)] : fintype (G'.neighbor_set v) :=
set.fintype_subset (G.neighbor_set v) (G'.neighbor_set_subset v)
/-- If a subgraph is locally finite at a vertex, then so are subgraphs of that subgraph.
This is not an instance because `G''` cannot be inferred. -/
def finite_at_of_subgraph {G' G'' : subgraph G} [decidable_rel G'.adj]
(h : G' ≤ G'') (v : G'.verts) [hf : fintype (G''.neighbor_set v)] :
fintype (G'.neighbor_set v) :=
set.fintype_subset (G''.neighbor_set v) (neighbor_set_subset_of_subgraph h v)
instance (G' : subgraph G) [fintype G'.verts]
(v : V) [decidable_pred (∈ G'.neighbor_set v)] : fintype (G'.neighbor_set v) :=
set.fintype_subset G'.verts (neighbor_set_subset_verts G' v)
instance coe_finite_at {G' : subgraph G} (v : G'.verts) [fintype (G'.neighbor_set v)] :
fintype (G'.coe.neighbor_set v) :=
fintype.of_equiv _ (coe_neighbor_set_equiv v).symm
lemma is_spanning.card_verts [fintype V] {G' : subgraph G} [fintype G'.verts]
(h : G'.is_spanning) : G'.verts.to_finset.card = fintype.card V :=
by { rw is_spanning_iff at h, simpa [h] }
/-- The degree of a vertex in a subgraph. It's zero for vertices outside the subgraph. -/
def degree (G' : subgraph G) (v : V) [fintype (G'.neighbor_set v)] : ℕ :=
fintype.card (G'.neighbor_set v)
lemma finset_card_neighbor_set_eq_degree {G' : subgraph G} {v : V} [fintype (G'.neighbor_set v)] :
(G'.neighbor_set v).to_finset.card = G'.degree v := by rw [degree, set.to_finset_card]
lemma degree_le (G' : subgraph G) (v : V)
[fintype (G'.neighbor_set v)] [fintype (G.neighbor_set v)] :
G'.degree v ≤ G.degree v :=
begin
rw ←card_neighbor_set_eq_degree,
exact set.card_le_of_subset (G'.neighbor_set_subset v),
end
lemma degree_le' (G' G'' : subgraph G) (h : G' ≤ G'') (v : V)
[fintype (G'.neighbor_set v)] [fintype (G''.neighbor_set v)] :
G'.degree v ≤ G''.degree v :=
set.card_le_of_subset (neighbor_set_subset_of_subgraph h v)
@[simp] lemma coe_degree (G' : subgraph G) (v : G'.verts)
[fintype (G'.coe.neighbor_set v)] [fintype (G'.neighbor_set v)] :
G'.coe.degree v = G'.degree v :=
begin
rw ←card_neighbor_set_eq_degree,
exact fintype.card_congr (coe_neighbor_set_equiv v),
end
@[simp] lemma degree_spanning_coe {G' : G.subgraph} (v : V) [fintype (G'.neighbor_set v)]
[fintype (G'.spanning_coe.neighbor_set v)] :
G'.spanning_coe.degree v = G'.degree v :=
by { rw [← card_neighbor_set_eq_degree, subgraph.degree], congr }
lemma degree_eq_one_iff_unique_adj {G' : subgraph G} {v : V} [fintype (G'.neighbor_set v)] :
G'.degree v = 1 ↔ ∃! (w : V), G'.adj v w :=
begin
rw [← finset_card_neighbor_set_eq_degree, finset.card_eq_one, finset.singleton_iff_unique_mem],
simp only [set.mem_to_finset, mem_neighbor_set],
end
/-! ## Subgraphs of subgraphs -/
/-- Given a subgraph of a subgraph of `G`, construct a subgraph of `G`. -/
@[reducible]
protected def coe_subgraph {G' : G.subgraph} : G'.coe.subgraph → G.subgraph := subgraph.map G'.hom
/-- Given a subgraph of `G`, restrict it to being a subgraph of another subgraph `G'` by
taking the portion of `G` that intersects `G'`. -/
@[reducible]
protected def restrict {G' : G.subgraph} : G.subgraph → G'.coe.subgraph := subgraph.comap G'.hom
lemma restrict_coe_subgraph {G' : G.subgraph} (G'' : G'.coe.subgraph) :
G''.coe_subgraph.restrict = G'' :=
begin
ext,
{ simp },
{ simp only [relation.map, comap_adj, coe_adj, subtype.coe_prop, hom_apply, map_adj,
set_coe.exists, subtype.coe_mk, exists_and_distrib_right, exists_eq_right_right,
subtype.coe_eta, exists_true_left, exists_eq_right, and_iff_right_iff_imp],
apply G''.adj_sub, }
end
lemma coe_subgraph_injective (G' : G.subgraph) :
function.injective (subgraph.coe_subgraph : G'.coe.subgraph → G.subgraph) :=
function.left_inverse.injective restrict_coe_subgraph
/-! ## Edge deletion -/
/-- Given a subgraph `G'` and a set of vertex pairs, remove all of the corresponding edges
from its edge set, if present.
See also: `simple_graph.delete_edges`. -/
def delete_edges (G' : G.subgraph) (s : set (sym2 V)) : G.subgraph :=
{ verts := G'.verts,
adj := G'.adj \ sym2.to_rel s,
adj_sub := λ a b h', G'.adj_sub h'.1,
edge_vert := λ a b h', G'.edge_vert h'.1,
symm := λ a b, by simp [G'.adj_comm, sym2.eq_swap] }
section delete_edges
variables {G' : G.subgraph} (s : set (sym2 V))
@[simp] lemma delete_edges_verts : (G'.delete_edges s).verts = G'.verts := rfl
@[simp] lemma delete_edges_adj (v w : V) :
(G'.delete_edges s).adj v w ↔ G'.adj v w ∧ ¬ ⟦(v, w)⟧ ∈ s := iff.rfl
@[simp] lemma delete_edges_delete_edges (s s' : set (sym2 V)) :
(G'.delete_edges s).delete_edges s' = G'.delete_edges (s ∪ s') :=
by ext; simp [and_assoc, not_or_distrib]
@[simp] lemma delete_edges_empty_eq : G'.delete_edges ∅ = G' :=
by ext; simp
@[simp] lemma delete_edges_spanning_coe_eq :
G'.spanning_coe.delete_edges s = (G'.delete_edges s).spanning_coe :=
by { ext, simp }
lemma delete_edges_coe_eq (s : set (sym2 G'.verts)) :
G'.coe.delete_edges s = (G'.delete_edges (sym2.map coe '' s)).coe :=
begin
ext ⟨v, hv⟩ ⟨w, hw⟩,
simp only [simple_graph.delete_edges_adj, coe_adj, subtype.coe_mk, delete_edges_adj,
set.mem_image, not_exists, not_and, and.congr_right_iff],
intro h,
split,
{ intros hs,
refine sym2.ind _,
rintro ⟨v', hv'⟩ ⟨w', hw'⟩,
simp only [sym2.map_pair_eq, subtype.coe_mk, quotient.eq],
contrapose!,
rintro (_ | _); simpa [sym2.eq_swap], },
{ intros h' hs,
exact h' _ hs rfl, },
end
lemma coe_delete_edges_eq (s : set (sym2 V)) :
(G'.delete_edges s).coe = G'.coe.delete_edges (sym2.map coe ⁻¹' s) :=
by { ext ⟨v, hv⟩ ⟨w, hw⟩, simp }
lemma delete_edges_le : G'.delete_edges s ≤ G' :=
by split; simp { contextual := tt }
lemma delete_edges_le_of_le {s s' : set (sym2 V)} (h : s ⊆ s') :
G'.delete_edges s' ≤ G'.delete_edges s :=
begin
split;
simp only [delete_edges_verts, delete_edges_adj, true_and, and_imp] {contextual := tt},
exact λ v w hvw hs' hs, hs' (h hs),
end
@[simp] lemma delete_edges_inter_edge_set_left_eq :
G'.delete_edges (G'.edge_set ∩ s) = G'.delete_edges s :=
by ext; simp [imp_false] { contextual := tt }
@[simp] lemma delete_edges_inter_edge_set_right_eq :
G'.delete_edges (s ∩ G'.edge_set) = G'.delete_edges s :=
by ext; simp [imp_false] { contextual := tt }
lemma coe_delete_edges_le :
(G'.delete_edges s).coe ≤ (G'.coe : simple_graph G'.verts) :=
λ v w, by simp { contextual := tt }
lemma spanning_coe_delete_edges_le (G' : G.subgraph) (s : set (sym2 V)) :
(G'.delete_edges s).spanning_coe ≤ G'.spanning_coe :=
spanning_coe_le_of_le (delete_edges_le s)
end delete_edges
/-! ## Induced subgraphs -/
/- Given a subgraph, we can change its vertex set while removing any invalid edges, which
gives induced subgraphs. See also `simple_graph.induce` for the `simple_graph` version, which,
unlike for subgraphs, results in a graph with a different vertex type. -/
/-- The induced subgraph of a subgraph. The expectation is that `s ⊆ G'.verts` for the usual
notion of an induced subgraph, but, in general, `s` is taken to be the new vertex set and edges
are induced from the subgraph `G'`. -/
@[simps]
def induce (G' : G.subgraph) (s : set V) : G.subgraph :=
{ verts := s,
adj := λ u v, u ∈ s ∧ v ∈ s ∧ G'.adj u v,
adj_sub := λ u v, by { rintro ⟨-, -, ha⟩, exact G'.adj_sub ha },
edge_vert := λ u v, by { rintro ⟨h, -, -⟩, exact h } }
lemma _root_.simple_graph.induce_eq_coe_induce_top (s : set V) :
G.induce s = ((⊤ : G.subgraph).induce s).coe :=
by { ext v w, simp }
section induce
variables {G' G'' : G.subgraph} {s s' : set V}
lemma induce_mono (hg : G' ≤ G'') (hs : s ⊆ s') : G'.induce s ≤ G''.induce s' :=
begin
split,
{ simp [hs], },
{ simp only [induce_adj, true_and, and_imp] { contextual := tt },
intros v w hv hw ha,
exact ⟨hs hv, hs hw, hg.2 ha⟩, },
end
@[mono]
lemma induce_mono_left (hg : G' ≤ G'') : G'.induce s ≤ G''.induce s := induce_mono hg (by refl)
@[mono]
lemma induce_mono_right (hs : s ⊆ s') : G'.induce s ≤ G'.induce s' := induce_mono (by refl) hs
@[simp] lemma induce_empty : G'.induce ∅ = ⊥ :=
by ext; simp
@[simp] lemma induce_self_verts : G'.induce G'.verts = G' :=
begin
ext,
{ simp },
{ split;
simp only [induce_adj, implies_true_iff, and_true] {contextual := tt},
exact λ ha, ⟨G'.edge_vert ha, G'.edge_vert ha.symm⟩ }
end
end induce
/-- Given a subgraph and a set of vertices, delete all the vertices from the subgraph,
if present. Any edges indicent to the deleted vertices are deleted as well. -/
@[reducible] def delete_verts (G' : G.subgraph) (s : set V) : G.subgraph := G'.induce (G'.verts \ s)
section delete_verts
variables {G' : G.subgraph} {s : set V}
lemma delete_verts_verts : (G'.delete_verts s).verts = G'.verts \ s := rfl
lemma delete_verts_adj {u v : V} :
(G'.delete_verts s).adj u v ↔
u ∈ G'.verts ∧ ¬ u ∈ s ∧ v ∈ G'.verts ∧ ¬ v ∈ s ∧ G'.adj u v :=
by simp [and_assoc]
@[simp] lemma delete_verts_delete_verts (s s' : set V) :
(G'.delete_verts s).delete_verts s' = G'.delete_verts (s ∪ s') :=
by ext; simp [not_or_distrib, and_assoc] { contextual := tt }
@[simp] lemma delete_verts_empty : G'.delete_verts ∅ = G' :=
by simp [delete_verts]
lemma delete_verts_le : G'.delete_verts s ≤ G' :=
by split; simp [set.diff_subset]
@[mono]
lemma delete_verts_mono {G' G'' : G.subgraph} (h : G' ≤ G'') :
G'.delete_verts s ≤ G''.delete_verts s :=
induce_mono h (set.diff_subset_diff_left h.1)
@[mono]
lemma delete_verts_anti {s s' : set V} (h : s ⊆ s') :
G'.delete_verts s' ≤ G'.delete_verts s :=
induce_mono (le_refl _) (set.diff_subset_diff_right h)
@[simp] lemma delete_verts_inter_verts_left_eq :
G'.delete_verts (G'.verts ∩ s) = G'.delete_verts s :=
by ext; simp [imp_false] { contextual := tt }
@[simp] lemma delete_verts_inter_verts_set_right_eq :
G'.delete_verts (s ∩ G'.verts) = G'.delete_verts s :=
by ext; simp [imp_false] { contextual := tt }
end delete_verts
end subgraph
end simple_graph
|
e22dfb0840e42a90861e2d74b8f4b4573ad00c7d | 05b503addd423dd68145d68b8cde5cd595d74365 | /src/ring_theory/localization.lean | 4a2017c58e374e213eaf288a6fe6a296206a43a1 | [
"Apache-2.0"
] | permissive | aestriplex/mathlib | 77513ff2b176d74a3bec114f33b519069788811d | e2fa8b2b1b732d7c25119229e3cdfba8370cb00f | refs/heads/master | 1,621,969,960,692 | 1,586,279,279,000 | 1,586,279,279,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 27,192 | lean | /-
Copyright (c) 2018 Kenny Lau. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Kenny Lau, Mario Carneiro, Johan Commelin
-/
import tactic.ring data.quot data.equiv.ring ring_theory.ideal_operations group_theory.submonoid
universes u v
local attribute [instance, priority 10] is_ring_hom.comp
namespace localization
variables (α : Type u) [comm_ring α] (S : set α) [is_submonoid S]
def r (x y : α × S) : Prop :=
∃ t ∈ S, ((x.2 : α) * y.1 - y.2 * x.1) * t = 0
local infix ≈ := r α S
section
variables {α S}
theorem r_of_eq {a₀ a₁ : α × S} (h : (a₀.2 : α) * a₁.1 = a₁.2 * a₀.1) : a₀ ≈ a₁ :=
⟨1, is_submonoid.one_mem S, by rw [h, sub_self, mul_one]⟩
end
theorem refl (x : α × S) : x ≈ x := r_of_eq rfl
theorem symm (x y : α × S) : x ≈ y → y ≈ x :=
λ ⟨t, hts, ht⟩, ⟨t, hts, by rw [← neg_sub, ← neg_mul_eq_neg_mul, ht, neg_zero]⟩
theorem trans : ∀ (x y z : α × S), x ≈ y → y ≈ z → x ≈ z :=
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨t, hts, ht⟩ ⟨t', hts', ht'⟩,
⟨s₂ * t' * t, is_submonoid.mul_mem (is_submonoid.mul_mem hs₂ hts') hts,
calc (s₁ * r₃ - s₃ * r₁) * (s₂ * t' * t) =
t' * s₃ * ((s₁ * r₂ - s₂ * r₁) * t) + t * s₁ * ((s₂ * r₃ - s₃ * r₂) * t') : by ring
... = 0 : by simp only [subtype.coe_mk] at ht ht'; rw [ht, ht']; simp⟩
instance : setoid (α × S) :=
⟨r α S, refl α S, symm α S, trans α S⟩
end localization
/-- The localization of a ring at a submonoid:
the elements of the submonoid become invertible in the localization.-/
def localization (α : Type u) [comm_ring α] (S : set α) [is_submonoid S] :=
quotient $ localization.setoid α S
namespace localization
variables (α : Type u) [comm_ring α] (S : set α) [is_submonoid S]
instance : has_add (localization α S) :=
⟨quotient.lift₂
(λ x y : α × S, (⟦⟨x.2 * y.1 + y.2 * x.1, x.2 * y.2⟩⟧ : localization α S)) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨r₄, s₄, hs₄⟩ ⟨t₅, hts₅, ht₅⟩ ⟨t₆, hts₆, ht₆⟩,
quotient.sound ⟨t₆ * t₅, is_submonoid.mul_mem hts₆ hts₅,
calc (s₁ * s₂ * (s₃ * r₄ + s₄ * r₃) - s₃ * s₄ * (s₁ * r₂ + s₂ * r₁)) * (t₆ * t₅) =
s₁ * s₃ * ((s₂ * r₄ - s₄ * r₂) * t₆) * t₅ + s₂ * s₄ * ((s₁ * r₃ - s₃ * r₁) * t₅) * t₆ : by ring
... = 0 : by simp only [subtype.coe_mk] at ht₅ ht₆; rw [ht₆, ht₅]; simp⟩⟩
instance : has_neg (localization α S) :=
⟨quotient.lift (λ x : α × S, (⟦⟨-x.1, x.2⟩⟧ : localization α S)) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨t, hts, ht⟩,
quotient.sound ⟨t, hts,
calc (s₁ * -r₂ - s₂ * -r₁) * t = -((s₁ * r₂ - s₂ * r₁) * t) : by ring
... = 0 : by simp only [subtype.coe_mk] at ht; rw ht; simp⟩⟩
instance : has_mul (localization α S) :=
⟨quotient.lift₂
(λ x y : α × S, (⟦⟨x.1 * y.1, x.2 * y.2⟩⟧ : localization α S)) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨r₃, s₃, hs₃⟩ ⟨r₄, s₄, hs₄⟩ ⟨t₅, hts₅, ht₅⟩ ⟨t₆, hts₆, ht₆⟩,
quotient.sound ⟨t₆ * t₅, is_submonoid.mul_mem hts₆ hts₅,
calc ((s₁ * s₂) * (r₃ * r₄) - (s₃ * s₄) * (r₁ * r₂)) * (t₆ * t₅) =
t₆ * ((s₁ * r₃ - s₃ * r₁) * t₅) * r₂ * s₄ + t₅ * ((s₂ * r₄ - s₄ * r₂) * t₆) * r₃ * s₁ :
by ring
... = 0 : by simp only [subtype.coe_mk] at ht₅ ht₆; rw [ht₅, ht₆]; simp⟩⟩
variables {α S}
def mk (r : α) (s : S) : localization α S := ⟦(r, s)⟧
/-- The natural map from the ring to the localization.-/
def of (r : α) : localization α S := mk r 1
instance : comm_ring (localization α S) :=
by refine
{ add := has_add.add,
add_assoc := λ m n k, quotient.induction_on₃ m n k _,
zero := of 0,
zero_add := quotient.ind _,
add_zero := quotient.ind _,
neg := has_neg.neg,
add_left_neg := quotient.ind _,
add_comm := quotient.ind₂ _,
mul := has_mul.mul,
mul_assoc := λ m n k, quotient.induction_on₃ m n k _,
one := of 1,
one_mul := quotient.ind _,
mul_one := quotient.ind _,
left_distrib := λ m n k, quotient.induction_on₃ m n k _,
right_distrib := λ m n k, quotient.induction_on₃ m n k _,
mul_comm := quotient.ind₂ _ };
{ intros,
try {rcases a with ⟨r₁, s₁, hs₁⟩},
try {rcases b with ⟨r₂, s₂, hs₂⟩},
try {rcases c with ⟨r₃, s₃, hs₃⟩},
refine (quotient.sound $ r_of_eq _),
simp only [is_submonoid.coe_mul, is_submonoid.coe_one, subtype.coe_mk],
ring }
instance : inhabited (localization α S) := ⟨0⟩
instance of.is_ring_hom : is_ring_hom (of : α → localization α S) :=
{ map_add := λ x y, quotient.sound $ by simp [add_comm],
map_mul := λ x y, quotient.sound $ by simp [add_comm],
map_one := rfl }
variables {S}
instance : has_coe_t α (localization α S) := ⟨of⟩ -- note [use has_coe_t]
instance coe.is_ring_hom : is_ring_hom (coe : α → localization α S) :=
localization.of.is_ring_hom
/-- The natural map from the submonoid to the unit group of the localization.-/
def to_units (s : S) : units (localization α S) :=
{ val := s,
inv := mk 1 s,
val_inv := quotient.sound $ r_of_eq $ mul_assoc _ _ _,
inv_val := quotient.sound $ r_of_eq $ show s.val * 1 * 1 = 1 * (1 * s.val), by simp }
@[simp] lemma to_units_coe (s : S) :
((to_units s) : localization α S) = ((s : α) : localization α S) := rfl
section
variables (α S) (x y : α) (n : ℕ)
@[simp] lemma of_zero : (of 0 : localization α S) = 0 := rfl
@[simp] lemma of_one : (of 1 : localization α S) = 1 := rfl
@[simp] lemma of_add : (of (x + y) : localization α S) = of x + of y :=
by apply is_ring_hom.map_add
@[simp] lemma of_sub : (of (x - y) : localization α S) = of x - of y :=
by apply is_ring_hom.map_sub
@[simp] lemma of_mul : (of (x * y) : localization α S) = of x * of y :=
by apply is_ring_hom.map_mul
@[simp] lemma of_neg : (of (-x) : localization α S) = -of x :=
by apply is_ring_hom.map_neg
@[simp] lemma of_pow : (of (x ^ n) : localization α S) = (of x) ^ n :=
by apply is_semiring_hom.map_pow
@[simp] lemma of_is_unit' (s ∈ S) : is_unit (of s : localization α S) :=
is_unit_unit $ to_units ⟨s, ‹s ∈ S›⟩
@[simp] lemma of_is_unit (s : S) : is_unit (of s : localization α S) :=
is_unit_unit $ to_units s
@[simp] lemma coe_zero : ((0 : α) : localization α S) = 0 := rfl
@[simp] lemma coe_one : ((1 : α) : localization α S) = 1 := rfl
@[simp] lemma coe_add : (↑(x + y) : localization α S) = x + y := of_add _ _ _ _
@[simp] lemma coe_sub : (↑(x - y) : localization α S) = x - y := of_sub _ _ _ _
@[simp] lemma coe_mul : (↑(x * y) : localization α S) = x * y := of_mul _ _ _ _
@[simp] lemma coe_neg : (↑(-x) : localization α S) = -x := of_neg _ _ _
@[simp] lemma coe_pow : (↑(x ^ n) : localization α S) = x ^ n := of_pow _ _ _ _
@[simp] lemma coe_is_unit' (s ∈ S) : is_unit ((s : α) : localization α S) := of_is_unit' _ _ _ ‹s ∈ S›
@[simp] lemma coe_is_unit (s : S) : is_unit ((s : α) : localization α S) := of_is_unit _ _ _
end
lemma mk_self {x : α} {hx : x ∈ S} :
(mk x ⟨x, hx⟩ : localization α S) = 1 :=
quotient.sound ⟨1, is_submonoid.one_mem S,
by simp only [subtype.coe_mk, is_submonoid.coe_one, mul_one, one_mul, sub_self]⟩
lemma mk_self' {s : S} :
(mk s s : localization α S) = 1 :=
by cases s; exact mk_self
lemma mk_self'' {s : S} :
(mk s.1 s : localization α S) = 1 :=
mk_self'
-- This lemma does not apply with simp, since (mk r s) simplifies to (r * s⁻¹).
-- However, it could apply with dsimp.
@[simp, nolint simp_nf]
lemma coe_mul_mk (x y : α) (s : S) :
↑x * mk y s = mk (x * y) s :=
quotient.sound $ r_of_eq $ by rw one_mul
lemma mk_eq_mul_mk_one (r : α) (s : S) :
mk r s = r * mk 1 s :=
by rw [coe_mul_mk, mul_one]
-- This lemma does not apply with simp, since (mk r s) simplifies to (r * s⁻¹).
-- However, it could apply with dsimp.
@[simp, nolint simp_nf]
lemma mk_mul_mk (x y : α) (s t : S) :
mk x s * mk y t = mk (x * y) (s * t) := rfl
lemma mk_mul_cancel_left (r : α) (s : S) :
mk (↑s * r) s = r :=
by rw [mk_eq_mul_mk_one, mul_comm ↑s, coe_mul,
mul_assoc, ← mk_eq_mul_mk_one, mk_self', mul_one]
lemma mk_mul_cancel_right (r : α) (s : S) :
mk (r * s) s = r :=
by rw [mul_comm, mk_mul_cancel_left]
@[simp] lemma mk_eq (r : α) (s : S) :
mk r s = r * ((to_units s)⁻¹ : units _) :=
quotient.sound $ by simp
@[elab_as_eliminator]
protected theorem induction_on {C : localization α S → Prop} (x : localization α S)
(ih : ∀ r s, C (mk r s : localization α S)) : C x :=
by rcases x with ⟨r, s⟩; exact ih r s
section
variables {β : Type v} [comm_ring β] {T : set β} [is_submonoid T] (f : α → β) [is_ring_hom f]
@[elab_with_expected_type]
def lift' (g : S → units β) (hg : ∀ s, (g s : β) = f s) (x : localization α S) : β :=
quotient.lift_on x (λ p, f p.1 * ((g p.2)⁻¹ : units β)) $ λ ⟨r₁, s₁⟩ ⟨r₂, s₂⟩ ⟨t, hts, ht⟩,
show f r₁ * ↑(g s₁)⁻¹ = f r₂ * ↑(g s₂)⁻¹, from
calc f r₁ * ↑(g s₁)⁻¹
= (f r₁ * g s₂ + ((g s₁ * f r₂ - g s₂ * f r₁) * g ⟨t, hts⟩) * ↑(g ⟨t, hts⟩)⁻¹)
* ↑(g s₁)⁻¹ * ↑(g s₂)⁻¹ :
by simp only [hg, subtype.coe_mk, (is_ring_hom.map_mul f).symm, (is_ring_hom.map_sub f).symm,
ht, is_ring_hom.map_zero f, zero_mul, add_zero];
rw [is_ring_hom.map_mul f, ← hg, mul_right_comm,
mul_assoc (f r₁), ← units.coe_mul, mul_inv_self];
rw [units.coe_one, mul_one]
... = f r₂ * ↑(g s₂)⁻¹ :
by rw [mul_assoc, mul_assoc, ← units.coe_mul, mul_inv_self, units.coe_one,
mul_one, mul_comm ↑(g s₂), add_sub_cancel'_right];
rw [mul_comm ↑(g s₁), ← mul_assoc, mul_assoc (f r₂), ← units.coe_mul,
mul_inv_self, units.coe_one, mul_one]
instance lift'.is_ring_hom (g : S → units β) (hg : ∀ s, (g s : β) = f s) :
is_ring_hom (localization.lift' f g hg) :=
{ map_one := have g 1 = 1, from units.ext (by rw hg; exact is_ring_hom.map_one f),
show f 1 * ↑(g 1)⁻¹ = 1, by rw [this, one_inv, units.coe_one, mul_one, is_ring_hom.map_one f],
map_mul := λ x y, localization.induction_on x $ λ r₁ s₁,
localization.induction_on y $ λ r₂ s₂,
have g (s₁ * s₂) = g s₁ * g s₂,
from units.ext (by simp only [hg, units.coe_mul]; exact is_ring_hom.map_mul f),
show _ * ↑(g (_ * _))⁻¹ = (_ * _) * (_ * _),
by simp only [subtype.coe_mk, mul_one, one_mul, subtype.coe_eta, this, mul_inv_rev];
rw [is_ring_hom.map_mul f, units.coe_mul, ← mul_assoc, ← mul_assoc];
simp only [mul_right_comm],
map_add := λ x y, localization.induction_on x $ λ r₁ s₁,
localization.induction_on y $ λ r₂ s₂,
have g (s₁ * s₂) = g s₁ * g s₂,
from units.ext (by simp only [hg, units.coe_mul]; exact is_ring_hom.map_mul f),
show _ * ↑(g (_ * _))⁻¹ = _ * _ + _ * _,
by simp only [subtype.coe_mk, mul_one, one_mul, subtype.coe_eta, this, mul_inv_rev];
simp only [is_ring_hom.map_mul f, is_ring_hom.map_add f, add_mul, (hg _).symm];
simp only [mul_assoc, mul_comm, mul_left_comm, (units.coe_mul _ _).symm];
rw [mul_inv_cancel_left, mul_left_comm, ← mul_assoc, mul_inv_cancel_right, add_comm] }
noncomputable def lift (h : ∀ s ∈ S, is_unit (f s)) :
localization α S → β :=
localization.lift' f (λ s, classical.some $ h s.1 s.2)
(λ s, by rw [← classical.some_spec (h s.1 s.2)]; refl)
instance lift.is_ring_hom (h : ∀ s ∈ S, is_unit (f s)) :
is_ring_hom (lift f h) :=
lift'.is_ring_hom _ _ _
-- This lemma does not apply with simp, since (mk r s) simplifies to (r * s⁻¹).
-- However, it could apply with dsimp.
@[simp, nolint simp_nf]
lemma lift'_mk (g : S → units β) (hg : ∀ s, (g s : β) = f s) (r : α) (s : S) :
lift' f g hg (mk r s) = f r * ↑(g s)⁻¹ := rfl
@[simp] lemma lift'_of (g : S → units β) (hg : ∀ s, (g s : β) = f s) (a : α) :
lift' f g hg (of a) = f a :=
have g 1 = 1, from units.ext_iff.2 $ by simp [hg, is_ring_hom.map_one f],
by simp [lift', quotient.lift_on_beta, of, mk, this]
@[simp] lemma lift'_coe (g : S → units β) (hg : ∀ s, (g s : β) = f s) (a : α) :
lift' f g hg a = f a := lift'_of _ _ _ _
@[simp] lemma lift_of (h : ∀ s ∈ S, is_unit (f s)) (a : α) :
lift f h (of a) = f a := lift'_of _ _ _ _
@[simp] lemma lift_coe (h : ∀ s ∈ S, is_unit (f s)) (a : α) :
lift f h a = f a := lift'_of _ _ _ _
@[simp] lemma lift'_comp_of (g : S → units β) (hg : ∀ s, (g s : β) = f s) :
lift' f g hg ∘ of = f := funext $ λ a, lift'_of _ _ _ a
@[simp] lemma lift_comp_of (h : ∀ s ∈ S, is_unit (f s)) :
lift f h ∘ of = f := lift'_comp_of _ _ _
@[simp] lemma lift'_apply_coe (f : localization α S → β) [is_ring_hom f]
(g : S → units β) (hg : ∀ s, (g s : β) = f s) :
lift' (λ a : α, f a) g hg = f :=
have g = (λ s, (units.map' f : units (localization α S) → units β) (to_units s)),
from funext $ λ x, units.ext $ (hg x).symm ▸ rfl,
funext $ λ x, localization.induction_on x
(λ r s, by subst this; rw [lift'_mk, ← (units.map' f).map_inv, units.coe_map'];
simp [is_ring_hom.map_mul f])
@[simp] lemma lift_apply_coe (f : localization α S → β) [is_ring_hom f] :
lift (λ a : α, f a)
(λ s hs, is_unit.map' f (is_unit_unit (to_units ⟨s, hs⟩))) = f :=
by rw [lift, lift'_apply_coe]
/-- Function extensionality for localisations:
two functions are equal if they agree on elements that are coercions.-/
protected lemma funext (f g : localization α S → β) [is_ring_hom f] [is_ring_hom g]
(h : ∀ a : α, f a = g a) : f = g :=
begin
rw [← lift_apply_coe f, ← lift_apply_coe g],
congr' 1,
exact funext h
end
variables {α S T}
def map (hf : ∀ s ∈ S, f s ∈ T) : localization α S → localization β T :=
lift' (of ∘ f) (to_units ∘ subtype.map f hf) (λ s, rfl)
instance map.is_ring_hom (hf : ∀ s ∈ S, f s ∈ T) : is_ring_hom (map f hf) :=
lift'.is_ring_hom _ _ _
@[simp] lemma map_of (hf : ∀ s ∈ S, f s ∈ T) (a : α) :
map f hf (of a) = of (f a) := lift'_of _ _ _ _
@[simp] lemma map_coe (hf : ∀ s ∈ S, f s ∈ T) (a : α) :
map f hf a = (f a) := lift'_of _ _ _ _
@[simp] lemma map_comp_of (hf : ∀ s ∈ S, f s ∈ T) :
map f hf ∘ of = of ∘ f := funext $ λ a, map_of _ _ _
@[simp] lemma map_id : map id (λ s (hs : s ∈ S), hs) = id :=
localization.funext _ _ $ map_coe _ _
lemma map_comp_map {γ : Type*} [comm_ring γ] (hf : ∀ s ∈ S, f s ∈ T) (U : set γ)
[is_submonoid U] (g : β → γ) [is_ring_hom g] (hg : ∀ t ∈ T, g t ∈ U) :
map g hg ∘ map f hf = map (λ x, g (f x)) (λ s hs, hg _ (hf _ hs)) :=
localization.funext _ _ $ by simp
lemma map_map {γ : Type*} [comm_ring γ] (hf : ∀ s ∈ S, f s ∈ T) (U : set γ)
[is_submonoid U] (g : β → γ) [is_ring_hom g] (hg : ∀ t ∈ T, g t ∈ U) (x) :
map g hg (map f hf x) = map (λ x, g (f x)) (λ s hs, hg _ (hf _ hs)) x :=
congr_fun (map_comp_map _ _ _ _ _) x
def equiv_of_equiv (h₁ : α ≃+* β) (h₂ : h₁ '' S = T) :
localization α S ≃+* localization β T :=
{ to_fun := map h₁ $ λ s hs, h₂ ▸ set.mem_image_of_mem _ hs,
inv_fun := map h₁.symm $ λ t ht,
by simp [h₁.image_eq_preimage, set.preimage, set.ext_iff, *] at *,
left_inv := λ _, by simp only [map_map, h₁.symm_apply_apply]; erw map_id; refl,
right_inv := λ _, by simp only [map_map, h₁.apply_symm_apply]; erw map_id; refl,
map_mul' := λ _ _, is_ring_hom.map_mul _,
map_add' := λ _ _, is_ring_hom.map_add _ }
end
section away
variables {β : Type v} [comm_ring β] (f : α → β) [is_ring_hom f]
@[reducible] def away (x : α) := localization α (powers x)
@[simp] def away.inv_self (x : α) : away x :=
mk 1 ⟨x, 1, pow_one x⟩
@[elab_with_expected_type]
noncomputable def away.lift {x : α} (hfx : is_unit (f x)) : away x → β :=
localization.lift' f (λ s, classical.some hfx ^ classical.some s.2) $ λ s,
by rw [units.coe_pow, ← classical.some_spec hfx,
← is_semiring_hom.map_pow f, classical.some_spec s.2]; refl
instance away.lift.is_ring_hom {x : α} (hfx : is_unit (f x)) :
is_ring_hom (localization.away.lift f hfx) :=
lift'.is_ring_hom _ _ _
@[simp] lemma away.lift_of {x : α} (hfx : is_unit (f x)) (a : α) :
away.lift f hfx (of a) = f a := lift'_of _ _ _ _
@[simp] lemma away.lift_coe {x : α} (hfx : is_unit (f x)) (a : α) :
away.lift f hfx a = f a := lift'_of _ _ _ _
@[simp] lemma away.lift_comp_of {x : α} (hfx : is_unit (f x)) :
away.lift f hfx ∘ of = f := lift'_comp_of _ _ _
noncomputable def away_to_away_right (x y : α) : away x → away (x * y) :=
localization.away.lift coe $
is_unit_of_mul_eq_one x (y * away.inv_self (x * y)) $
by rw [away.inv_self, coe_mul_mk, coe_mul_mk, mul_one, mk_self]
instance away_to_away_right.is_ring_hom (x y : α) :
is_ring_hom (away_to_away_right x y) :=
away.lift.is_ring_hom _ _
end away
section at_prime
variables (P : ideal α) [hp : ideal.is_prime P]
include hp
instance prime.is_submonoid :
is_submonoid (-P : set α) :=
{ one_mem := P.ne_top_iff_one.1 hp.1,
mul_mem := λ x y hnx hny hxy, or.cases_on (hp.2 hxy) hnx hny }
@[reducible] def at_prime := localization α (-P)
instance at_prime.local_ring : local_ring (at_prime P) :=
local_of_nonunits_ideal
(λ hze,
let ⟨t, hts, ht⟩ := quotient.exact hze in
hts $ have htz : t = 0, by simpa using ht,
suffices (0:α) ∈ P, by rwa htz,
P.zero_mem)
(begin
rintro ⟨⟨r₁, s₁, hs₁⟩⟩ ⟨⟨r₂, s₂, hs₂⟩⟩ hx hy hu,
rcases is_unit_iff_exists_inv.1 hu with ⟨⟨⟨r₃, s₃, hs₃⟩⟩, hz⟩,
rcases quotient.exact hz with ⟨t, hts, ht⟩,
simp at ht,
have : ∀ {r s hs}, (⟦⟨r, s, hs⟩⟧ : at_prime P) ∈ nonunits (at_prime P) → r ∈ P,
{ haveI := classical.dec,
exact λ r s hs, not_imp_comm.1 (λ nr,
is_unit_iff_exists_inv.2 ⟨⟦⟨s, r, nr⟩⟧,
quotient.sound $ r_of_eq $ by simp [mul_comm]⟩) },
have hr₃ := (hp.mem_or_mem_of_mul_eq_zero ht).resolve_right hts,
have := (ideal.add_mem_iff_left _ _).1 hr₃,
{ exact not_or (mt hp.mem_or_mem (not_or hs₁ hs₂)) hs₃ (hp.mem_or_mem this) },
{ exact P.neg_mem (P.mul_mem_right
(P.add_mem (P.mul_mem_left (this hy)) (P.mul_mem_left (this hx)))) }
end)
end at_prime
variable (α)
def non_zero_divisors : set α := {x | ∀ z, z * x = 0 → z = 0}
instance non_zero_divisors.is_submonoid : is_submonoid (non_zero_divisors α) :=
{ one_mem := λ z hz, by rwa mul_one at hz,
mul_mem := λ x₁ x₂ hx₁ hx₂ z hz,
have z * x₁ * x₂ = 0, by rwa mul_assoc,
hx₁ z $ hx₂ (z * x₁) this }
@[simp] lemma non_zero_divisors_one_val : (1 : non_zero_divisors α).val = 1 := rfl
/-- The field of fractions of an integral domain.-/
@[reducible] def fraction_ring := localization α (non_zero_divisors α)
namespace fraction_ring
open function
variables {β : Type u} [integral_domain β] [decidable_eq β]
lemma eq_zero_of_ne_zero_of_mul_eq_zero {x y : β} :
x ≠ 0 → y * x = 0 → y = 0 :=
λ hnx hxy, or.resolve_right (eq_zero_or_eq_zero_of_mul_eq_zero hxy) hnx
lemma mem_non_zero_divisors_iff_ne_zero {x : β} :
x ∈ non_zero_divisors β ↔ x ≠ 0 :=
⟨λ hm hz, zero_ne_one (hm 1 $ by rw [hz, one_mul]).symm,
λ hnx z, eq_zero_of_ne_zero_of_mul_eq_zero hnx⟩
variable (β)
def inv_aux (x : β × (non_zero_divisors β)) : fraction_ring β :=
if h : x.1 = 0 then 0 else ⟦⟨x.2, x.1, mem_non_zero_divisors_iff_ne_zero.mpr h⟩⟧
instance : has_inv (fraction_ring β) :=
⟨quotient.lift (inv_aux β) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩ ⟨t, hts, ht⟩,
begin
have hrs : s₁ * r₂ = 0 + s₂ * r₁,
from sub_eq_iff_eq_add.1 (hts _ ht),
by_cases hr₁ : r₁ = 0;
by_cases hr₂ : r₂ = 0;
simp [hr₁, hr₂] at hrs;
simp only [inv_aux, hr₁, hr₂, dif_pos, dif_neg, not_false_iff, subtype.coe_mk, quotient.eq],
{ exfalso,
exact mem_non_zero_divisors_iff_ne_zero.mp hs₁ hrs },
{ exfalso,
exact mem_non_zero_divisors_iff_ne_zero.mp hs₂ hrs },
{ apply r_of_eq,
simpa [mul_comm] using hrs.symm }
end⟩
lemma mk_inv {r s} :
(mk r s : fraction_ring β)⁻¹ =
if h : r = 0 then 0 else ⟦⟨s, r, mem_non_zero_divisors_iff_ne_zero.mpr h⟩⟧ := rfl
lemma mk_inv' :
∀ (x : β × (non_zero_divisors β)), (⟦x⟧⁻¹ : fraction_ring β) =
if h : x.1 = 0 then 0 else ⟦⟨x.2.val, x.1, mem_non_zero_divisors_iff_ne_zero.mpr h⟩⟧
| ⟨r,s,hs⟩ := rfl
instance : decidable_eq (fraction_ring β) :=
@quotient.decidable_eq (β × non_zero_divisors β) (localization.setoid β (non_zero_divisors β)) $
λ ⟨r₁, s₁, hs₁⟩ ⟨r₂, s₂, hs₂⟩, show decidable (∃ t ∈ non_zero_divisors β, (s₁ * r₂ - s₂ * r₁) * t = 0),
from decidable_of_iff (s₁ * r₂ - s₂ * r₁ = 0)
⟨λ H, ⟨1, λ y, (mul_one y).symm ▸ id, H.symm ▸ zero_mul _⟩,
λ ⟨t, ht1, ht2⟩, or.resolve_right (mul_eq_zero.1 ht2) $ λ ht,
one_ne_zero (ht1 1 ((one_mul t).symm ▸ ht))⟩
instance : field (fraction_ring β) :=
by refine
{ inv := has_inv.inv,
zero_ne_one := λ hzo,
let ⟨t, hts, ht⟩ := quotient.exact hzo in
zero_ne_one (by simpa using hts _ ht : 0 = 1),
mul_inv_cancel := quotient.ind _,
inv_zero := dif_pos rfl,
.. localization.comm_ring };
{ intros x hnx,
rcases x with ⟨x, z, hz⟩,
have : x ≠ 0,
from assume hx, hnx (quotient.sound $ r_of_eq $ by simp [hx]),
simp only [has_inv.inv, inv_aux, quotient.lift_beta, dif_neg this],
exact (quotient.sound $ r_of_eq $ by simp [mul_comm]) }
@[simp, nolint simp_nf] -- takes a crazy amount of time simplify lhs
lemma mk_eq_div {r s} : (mk r s : fraction_ring β) = (r / s : fraction_ring β) :=
show _ = _ * dite (s.1 = 0) _ _, by rw [dif_neg (mem_non_zero_divisors_iff_ne_zero.mp s.2)];
exact localization.mk_eq_mul_mk_one _ _
variables {β}
@[simp] lemma mk_eq_div' (x : β × (non_zero_divisors β)) :
(⟦x⟧ : fraction_ring β) = ((x.1) / ((x.2).val) : fraction_ring β) :=
by erw ← mk_eq_div; cases x; refl
lemma eq_zero_of (x : β) (h : (of x : fraction_ring β) = 0) : x = 0 :=
begin
rcases quotient.exact h with ⟨t, ht, ht'⟩,
simpa [mem_non_zero_divisors_iff_ne_zero.mp ht] using ht'
end
lemma of.injective : function.injective (of : β → fraction_ring β) :=
(is_add_group_hom.injective_iff _).mpr eq_zero_of
section map
open function is_ring_hom
variables {A : Type u} [integral_domain A] [decidable_eq A]
variables {B : Type v} [integral_domain B] [decidable_eq B]
variables (f : A → B) [is_ring_hom f]
def map (hf : injective f) : fraction_ring A → fraction_ring B :=
localization.map f $ λ s h,
by rw [mem_non_zero_divisors_iff_ne_zero, ← map_zero f, ne.def, hf.eq_iff];
exact mem_non_zero_divisors_iff_ne_zero.1 h
@[simp] lemma map_of (hf : injective f) (a : A) : map f hf (of a) = of (f a) :=
localization.map_of _ _ _
@[simp] lemma map_coe (hf : injective f) (a : A) : map f hf a = f a :=
localization.map_coe _ _ _
@[simp] lemma map_comp_of (hf : injective f) :
map f hf ∘ (of : A → fraction_ring A) = (of : B → fraction_ring B) ∘ f :=
localization.map_comp_of _ _
instance map.is_ring_hom (hf : injective f) : is_ring_hom (map f hf) :=
localization.map.is_ring_hom _ _
def equiv_of_equiv (h : A ≃+* B) : fraction_ring A ≃+* fraction_ring B :=
localization.equiv_of_equiv h
begin
ext b,
rw [h.image_eq_preimage, set.preimage, set.mem_set_of_eq,
mem_non_zero_divisors_iff_ne_zero, mem_non_zero_divisors_iff_ne_zero, ne.def],
exact h.symm.map_ne_zero_iff
end
end map
end fraction_ring
section ideals
theorem map_comap (J : ideal (localization α S)) :
ideal.map (ring_hom.of coe) (ideal.comap (ring_hom.of (coe : α → localization α S)) J) = J :=
le_antisymm (ideal.map_le_iff_le_comap.2 (le_refl _)) $ λ x,
localization.induction_on x $ λ r s hJ, (submodule.mem_coe _).2 $
mul_one r ▸ coe_mul_mk r 1 s ▸ (ideal.mul_mem_right _ $ ideal.mem_map_of_mem $
have _ := @ideal.mul_mem_left (localization α S) _ _ s _ hJ,
by rwa [coe_coe, coe_mul_mk, mk_mul_cancel_left] at this)
def le_order_embedding :
((≤) : ideal (localization α S) → ideal (localization α S) → Prop) ≼o
((≤) : ideal α → ideal α → Prop) :=
{ to_fun := λ J, ideal.comap (ring_hom.of coe) J,
inj := function.injective_of_left_inverse (map_comap α),
ord := λ J₁ J₂, ⟨ideal.comap_mono, λ hJ,
map_comap α J₁ ▸ map_comap α J₂ ▸ ideal.map_mono hJ⟩ }
end ideals
section module
/-! ### `module` section
Localizations form an algebra over `α` induced by the embedding `coe : α → localization α S`.
-/
set_option class.instance_max_depth 50
variables (α S)
instance : algebra α (localization α S) := algebra.of_ring_hom coe (is_ring_hom.of_semiring coe)
lemma of_smul (c x : α) : (of (c • x) : localization α S) = c • of x :=
by { simp, refl }
lemma coe_smul (c x : α) : (coe (c • x) : localization α S) = c • coe x :=
of_smul α S c x
lemma coe_mul_eq_smul (c : α) (x : localization α S) : coe c * x = c • x :=
rfl
lemma mul_coe_eq_smul (c : α) (x : localization α S) : x * coe c = c • x :=
mul_comm x (coe c)
/-- The embedding `coe : α → localization α S` induces a linear map. -/
def lin_coe : α →ₗ[α] localization α S := ⟨coe, coe_add α S, coe_smul α S⟩
@[simp] lemma lin_coe_apply (a : α) : lin_coe α S a = coe a := rfl
instance coe_submodules : has_coe (ideal α) (submodule α (localization α S)) :=
⟨submodule.map (lin_coe _ _)⟩
@[simp] lemma of_id (a : α) : (algebra.of_id α (localization α S) : α → localization α S) a = ↑a :=
rfl
end module
section is_integer
/-- `a : localization α S` is an integer if it is an element of the original ring `α` -/
def is_integer (S : set α) [is_submonoid S] (a : localization α S) : Prop :=
a ∈ set.range (coe : α → localization α S)
lemma is_integer_coe (a : α) : is_integer α S a :=
⟨a, rfl⟩
lemma is_integer_add {a b} (ha : is_integer α S a) (hb : is_integer α S b) :
is_integer α S (a + b) :=
begin
rcases ha with ⟨a', ha⟩,
rcases hb with ⟨b', hb⟩,
use a' + b',
rw [coe_add, ha, hb]
end
lemma is_integer_mul {a b} (ha : is_integer α S a) (hb : is_integer α S b) :
is_integer α S (a * b) :=
begin
rcases ha with ⟨a', ha⟩,
rcases hb with ⟨b', hb⟩,
use a' * b',
rw [coe_mul, ha, hb]
end
set_option class.instance_max_depth 50
lemma is_integer_smul {a : α} {b} (hb : is_integer α S b) :
is_integer α S (a • b) :=
begin
rcases hb with ⟨b', hb⟩,
use a * b',
rw [←hb, ←coe_smul, smul_eq_mul]
end
end is_integer
end localization
|
f766ceb6fb0698f058152ea1d75f66c50e299f7c | 4727251e0cd73359b15b664c3170e5d754078599 | /src/data/rat/floor.lean | 8bff93de4016e27c401b872b46974d09bc31fab4 | [
"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,320 | 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, Kevin Kappelmann
-/
import algebra.order.floor
import tactic.field_simp
/-!
# Floor Function for Rational Numbers
## Summary
We define the `floor` function and the `floor_ring` instance on `ℚ`. Some technical lemmas relating
`floor` to integer division and modulo arithmetic are derived as well as some simple inequalities.
## Tags
rat, rationals, ℚ, floor
-/
open int
namespace rat
/-- `floor q` is the largest integer `z` such that `z ≤ q` -/
protected def floor : ℚ → ℤ
| ⟨n, d, h, c⟩ := n / d
protected theorem le_floor {z : ℤ} : ∀ {r : ℚ}, z ≤ rat.floor r ↔ (z : ℚ) ≤ r
| ⟨n, d, h, c⟩ := begin
simp [rat.floor],
rw [num_denom'],
have h' := int.coe_nat_lt.2 h,
conv { to_rhs,
rw [coe_int_eq_mk, rat.le_def zero_lt_one h', mul_one] },
exact int.le_div_iff_mul_le h'
end
instance : floor_ring ℚ :=
floor_ring.of_floor ℚ rat.floor $ λ a z, rat.le_floor.symm
protected lemma floor_def {q : ℚ} : ⌊q⌋ = q.num / q.denom := by { cases q, refl }
lemma floor_int_div_nat_eq_div {n : ℤ} {d : ℕ} : ⌊(↑n : ℚ) / (↑d : ℚ)⌋ = n / (↑d : ℤ) :=
begin
rw [rat.floor_def],
cases decidable.em (d = 0) with d_eq_zero d_ne_zero,
{ simp [d_eq_zero] },
{ set q := (n : ℚ) / d with q_eq,
obtain ⟨c, n_eq_c_mul_num, d_eq_c_mul_denom⟩ : ∃ c, n = c * q.num ∧ (d : ℤ) = c * q.denom, by
{ rw q_eq,
exact_mod_cast (@rat.exists_eq_mul_div_num_and_eq_mul_div_denom n d
(by exact_mod_cast d_ne_zero)) },
suffices : q.num / q.denom = c * q.num / (c * q.denom),
by rwa [n_eq_c_mul_num, d_eq_c_mul_denom],
suffices : c > 0, by solve_by_elim [int.mul_div_mul_of_pos],
have q_denom_mul_c_pos : (0 : ℤ) < q.denom * c, by
{ have : (d : ℤ) > 0, by exact_mod_cast (pos_iff_ne_zero.elim_right d_ne_zero),
rwa [d_eq_c_mul_denom, mul_comm] at this },
suffices : (0 : ℤ) ≤ q.denom, from pos_of_mul_pos_left q_denom_mul_c_pos this,
exact_mod_cast (le_of_lt q.pos) }
end
end rat
lemma int.mod_nat_eq_sub_mul_floor_rat_div {n : ℤ} {d : ℕ} : n % d = n - d * ⌊(n : ℚ) / d⌋ :=
by rw [(eq_sub_of_add_eq $ int.mod_add_div n d), rat.floor_int_div_nat_eq_div]
lemma nat.coprime_sub_mul_floor_rat_div_of_coprime {n d : ℕ} (n_coprime_d : n.coprime d) :
((n : ℤ) - d * ⌊(n : ℚ)/ d⌋).nat_abs.coprime d :=
begin
have : (n : ℤ) % d = n - d * ⌊(n : ℚ)/ d⌋, from int.mod_nat_eq_sub_mul_floor_rat_div,
rw ←this,
have : d.coprime n, from n_coprime_d.symm,
rwa [nat.coprime, nat.gcd_rec] at this
end
namespace rat
lemma num_lt_succ_floor_mul_denom (q : ℚ) : q.num < (⌊q⌋ + 1) * q.denom :=
begin
suffices : (q.num : ℚ) < (⌊q⌋ + 1) * q.denom, by exact_mod_cast this,
suffices : (q.num : ℚ) < (q - fract q + 1) * q.denom, by
{ have : (⌊q⌋ : ℚ) = q - fract q, from (eq_sub_of_add_eq $ floor_add_fract q),
rwa this },
suffices : (q.num : ℚ) < q.num + (1 - fract q) * q.denom, by
{ have : (q - fract q + 1) * q.denom = q.num + (1 - fract q) * q.denom, calc
(q - fract q + 1) * q.denom = (q + (1 - fract q)) * q.denom : by ring
... = q * q.denom + (1 - fract q) * q.denom : by rw add_mul
... = q.num + (1 - fract q) * q.denom : by simp,
rwa this },
suffices : 0 < (1 - fract q) * q.denom, by { rw ←sub_lt_iff_lt_add', simpa },
have : 0 < 1 - fract q, by
{ have : fract q < 1, from fract_lt_one q,
have : 0 + fract q < 1, by simp [this],
rwa lt_sub_iff_add_lt },
exact mul_pos this (by exact_mod_cast q.pos)
end
lemma fract_inv_num_lt_num_of_pos {q : ℚ} (q_pos : 0 < q): (fract q⁻¹).num < q.num :=
begin
-- we know that the numerator must be positive
have q_num_pos : 0 < q.num, from rat.num_pos_iff_pos.elim_right q_pos,
-- we will work with the absolute value of the numerator, which is equal to the numerator
have q_num_abs_eq_q_num : (q.num.nat_abs : ℤ) = q.num, from
(int.nat_abs_of_nonneg q_num_pos.le),
set q_inv := (q.denom : ℚ) / q.num with q_inv_def,
have q_inv_eq : q⁻¹ = q_inv, from rat.inv_def',
suffices : (q_inv - ⌊q_inv⌋).num < q.num, by rwa q_inv_eq,
suffices : ((q.denom - q.num * ⌊q_inv⌋ : ℚ) / q.num).num < q.num, by
field_simp [this, (ne_of_gt q_num_pos)],
suffices : (q.denom : ℤ) - q.num * ⌊q_inv⌋ < q.num, by
{ -- use that `q.num` and `q.denom` are coprime to show that the numerator stays unreduced
have : ((q.denom - q.num * ⌊q_inv⌋ : ℚ) / q.num).num = q.denom - q.num * ⌊q_inv⌋, by
{ suffices : ((q.denom : ℤ) - q.num * ⌊q_inv⌋).nat_abs.coprime q.num.nat_abs, by
exact_mod_cast (rat.num_div_eq_of_coprime q_num_pos this),
have : (q.num.nat_abs : ℚ) = (q.num : ℚ), by exact_mod_cast q_num_abs_eq_q_num,
have tmp := nat.coprime_sub_mul_floor_rat_div_of_coprime q.cop.symm,
simpa only [this, q_num_abs_eq_q_num] using tmp },
rwa this },
-- to show the claim, start with the following inequality
have q_inv_num_denom_ineq : q⁻¹.num - ⌊q⁻¹⌋ * q⁻¹.denom < q⁻¹.denom, by
{ have : q⁻¹.num < (⌊q⁻¹⌋ + 1) * q⁻¹.denom, from rat.num_lt_succ_floor_mul_denom q⁻¹,
have : q⁻¹.num < ⌊q⁻¹⌋ * q⁻¹.denom + q⁻¹.denom, by rwa [right_distrib, one_mul] at this,
rwa [←sub_lt_iff_lt_add'] at this },
-- use that `q.num` and `q.denom` are coprime to show that q_inv is the unreduced reciprocal
-- of `q`
have : q_inv.num = q.denom ∧ q_inv.denom = q.num.nat_abs, by
{ have coprime_q_denom_q_num : q.denom.coprime q.num.nat_abs, from q.cop.symm,
have : int.nat_abs q.denom = q.denom, by simp,
rw ←this at coprime_q_denom_q_num,
rw q_inv_def,
split,
{ exact_mod_cast (rat.num_div_eq_of_coprime q_num_pos coprime_q_denom_q_num) },
{ suffices : (((q.denom : ℚ) / q.num).denom : ℤ) = q.num.nat_abs, by exact_mod_cast this,
rw q_num_abs_eq_q_num,
exact_mod_cast (rat.denom_div_eq_of_coprime q_num_pos coprime_q_denom_q_num) } },
rwa [q_inv_eq, this.left, this.right, q_num_abs_eq_q_num, mul_comm] at q_inv_num_denom_ineq
end
end rat
|
3605ee8c15c00bf4263350c0f1a05a6be50cad12 | 964a8bb66c2eb89b920fe65d0ec2f77eab0d5f65 | /src/hanoiwidget.lean | 1873f0a9ae75693045fbcd4b1bc859ef7df730d9 | [
"MIT"
] | permissive | SnobbyDragon/leanhanoi | 9990a7b586f1d843d39c3c7aab7ac0b5eefbe653 | a42811ce5ab55e0451880a8c111c75b082936d39 | refs/heads/master | 1,675,735,182,689 | 1,609,387,288,000 | 1,609,387,288,000 | 284,167,126 | 8 | 1 | MIT | 1,609,262,129,000 | 1,596,247,349,000 | Lean | UTF-8 | Lean | false | false | 6,003 | lean | import hanoi
import hanoitactics
open hanoi hanoi.tower
open widget widget.html widget.attr
open tactic hanoitactics
variable {α:Type}
-- DUOLINGO COLORS ARE AESTHETIC <3
-- https://design.duolingo.com/identity/color
inductive color : Type
| cardinal
| bee
| beetle
| fox
| humpback
| macaw
| wolf
| white
open color
instance : has_to_string color :=
⟨ λ c, match c with
| cardinal := "#ff4b4b"
| bee := "#ffc800"
| beetle := "#ce82ff"
| fox := "#ff9600"
| humpback := "#2b70c9"
| macaw := "#1cb0f6"
| wolf := "#777777"
| white := "#ffffff"
end
⟩
meta def pole_html (disks currDisks : ℕ) (c : color) : html α :=
h "div" [
style [
("z-index", "-1"),
("background-color", to_string c),
("width", "5px"),
("height", to_string (disks*10 + 10) ++ "px"),
("margin-bottom", "-" ++ to_string (currDisks*10) ++ "px")
]
] []
#html pole_html 3 0 wolf
meta def disk_html (size disks currDisks : ℕ) (c : color) (selected : bool) : html α :=
h "div" [
key (to_string c),
style ([
("transition", "transform 0.2s ease-in-out"),
("background-color", to_string c),
("width", to_string (size*20) ++ "px"),
("height", "10px")
] ++ (if selected then [("transform", "translate(0px,-" ++ to_string ((disks - currDisks)*10 + 30) ++ "px)")] else []))
] []
def disk_color1 : ℕ → color
| 1 := cardinal
| 2 := bee
| 3 := beetle
| 4 := fox
| 5 := humpback
| 6 := macaw
| _ := wolf -- no more than 6 disks D:
def disk_color2 : ℕ → color
| 1 := fox
| 2 := humpback
| 3 := macaw
| 4 := cardinal
| 5 := bee
| 6 := beetle
| _ := wolf
meta def disks_html (disks : ℕ) (currDisks : list ℕ) (selected : bool) : list (html α) :=
currDisks.map_with_index (λ i currDisk, disk_html currDisk disks currDisks.length (disk_color1 currDisk) (selected ∧ i=0))
-- #html disk_html 1 cardinal
-- #html disk_html 2 bee
-- #html disk_html 3 beetle
-- #html disk_html 4 fox
-- #html disk_html 5 humpback
-- #html disk_html 6 macaw
meta def tower_html (disks : ℕ) (currDisks : list ℕ) (selected : bool) : html unit :=
h "div" [
style [
("display", "flex"),
("flex-direction", "column"),
("align-items", "center"),
("justify-items", "center"),
("width", to_string (disks*20 + 20) ++ "px"),
("height", to_string (disks*10 + 20) ++ "px") -- room between towers and buttons
],
on_click (λ x, ())
] ([pole_html disks currDisks.length wolf] ++ (disks_html disks currDisks selected))
-- #html tower_html 3 [1,2,3]
meta def towers_html (t : position) (selected : option tower) : html tower :=
h "div" [
style [
("display", "flex"),
("flex-direction", "row"),
("align-items", "flex-end"),
("height", to_string ((num_disks t)*10 + 60) ++ "px") -- make room at top for disk animation
]
] [
html.map_action (λ _, tower.a) $ tower_html (num_disks t) t.A (some tower.a = selected),
html.map_action (λ _, tower.b) $ tower_html (num_disks t) t.B (some tower.b = selected),
html.map_action (λ _, tower.c) $ tower_html (num_disks t) t.C (some tower.c = selected)
]
-- #html towers_html (position.mk [1,2,3] [] [])
-- #html towers_html (position.mk [2,3] [] [1])
-- #html towers_html (position.mk [3] [2] [1])
-- #html towers_html (position.mk [3] [1,2] [])
-- #html towers_html (position.mk [] [1,2] [3])
-- #html towers_html (position.mk [1] [2] [3])
-- #html towers_html (position.mk [1] [] [2,3])
-- #html towers_html (position.mk [] [] [1,2,3])
-- hanoi graph
-- Kevin showed me this cool thing https://twitter.com/stevenstrogatz/status/1340626057628688384?s=20
-- triangle from https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_shapes_triangle-up
meta def uptriangle_html (pole : ℕ) (trig text : color) : html empty :=
h "div" [
style [
("width", "0px"),
("height", "0px"),
("border-left","25px solid transparent"),
("border-right", "25px solid transparent"),
("border-bottom", "50px solid " ++ to_string trig)
]
] [
h "p" [
style [
("position", "relative"),
("color", to_string text),
("font-size", "24px"),
("text-align", "center"),
("top", "18px"),
("right", "6.5px")
]
] [to_string pole]
]
#html uptriangle_html 1 humpback white
#html uptriangle_html 2 bee wolf
#html uptriangle_html 3 beetle white
meta structure hanoi_state :=
(moves : list (tower × tower))
(initpos : position)
(pos : position)
(select : option tower)
meta inductive hanoi_action
| click_tower (t : tower)
| commit
| reset
meta def hanoi_view : hanoi_state → list (html hanoi_action)
| s := [
html.map_action hanoi_action.click_tower $ towers_html s.pos s.select,
button "commit" (hanoi_action.commit),
button "reset" (hanoi_action.reset)
]
meta def hanoi_update : hanoi_state → hanoi_action → hanoi_state × option widget.effects
| s (hanoi_action.click_tower t) :=
match s.select with
| none := ({select := some t, ..s}, none)
| (some x) := ({select := none, pos := move s.pos x t, moves := s.moves ++ [(x,t)], ..s}, none)
end
| s (hanoi_action.commit) :=
(s, some [effect.insert_text $ string.join $ s.moves.map (λ ⟨x,y⟩, "md " ++ to_string x ++ " " ++ to_string y ++ ", ")]) /- [todo] -/
| s (hanoi_action.reset) := ({moves:= [], pos:= s.initpos, ..s}, none)
meta def hanoi_init : unit → tactic hanoi_state
| () := do
position ← get_position,
return {pos := position, initpos := position, moves := [], select := none}
-- meta def hanoi_widget : tactic (list (html empty)) :=
-- do { position ← get_position,
-- return [towers_html position]
-- } <|> return [widget_override.goals_accomplished_message]
meta def hanoi_component : tc unit empty :=
component.ignore_action
$ component.with_effects (λ _ e, e)
$ tc.mk_simple hanoi_action hanoi_state hanoi_init (λ _ s a, pure $ hanoi_update s a) (λ _ s, pure $ hanoi_view s)
meta def hanoi_save_info (p : pos) : tactic unit :=
do tactic.save_widget p (widget.tc.to_component hanoi_component)
|
c46352c014da24a93bfd447b462b261d63eee8dc | a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7 | /src/order/filter/basic.lean | 3b337674e6f9d0d59e17dc50df7786d315cfb103 | [
"Apache-2.0"
] | permissive | kmill/mathlib | ea5a007b67ae4e9e18dd50d31d8aa60f650425ee | 1a419a9fea7b959317eddd556e1bb9639f4dcc05 | refs/heads/master | 1,668,578,197,719 | 1,593,629,163,000 | 1,593,629,163,000 | 276,482,939 | 0 | 0 | null | 1,593,637,960,000 | 1,593,637,959,000 | null | UTF-8 | Lean | false | false | 91,159 | lean | /-
Copyright (c) 2017 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Jeremy Avigad
-/
import order.zorn
import order.copy
import data.set.finite
/-!
# Theory of filters on sets
## Main definitions
* `filter` : filters on a set;
* `at_top`, `at_bot`, `cofinite`, `principal` : specific filters;
* `map`, `comap`, `prod` : operations on filters;
* `tendsto` : limit with respect to filters;
* `eventually` : `f.eventually p` means `{x | p x} ∈ f`;
* `frequently` : `f.frequently p` means `{x | ¬p x} ∉ f`.
* `filter_upwards [h₁, ..., hₙ]` : takes a list of proofs `hᵢ : sᵢ ∈ f`, and replaces a goal `s ∈ f`
with `∀ x, x ∈ s₁ → ... → x ∈ sₙ → x ∈ s`;
Filters on a type `X` are sets of sets of `X` satisfying three conditions. They are mostly used to
abstract two related kinds of ideas:
* *limits*, including finite or infinite limits of sequences, finite or infinite limits of functions
at a point or at infinity, etc...
* *things happening eventually*, including things happening for large enough `n : ℕ`, or near enough
a point `x`, or for close enough pairs of points, or things happening almost everywhere in the
sense of measure theory. Dually, filters can also express the idea of *things happening often*:
for arbitrarily large `n`, or at a point in any neighborhood of given a point etc...
In this file, we define the type `filter X` of filters on `X`, and endow it with a complete lattice
structure. This structure is lifted from the lattice structure on `set (set X)` using the Galois
insertion which maps a filter to its elements in one direction, and an arbitrary set of sets to
the smallest filter containing it in the other direction.
We also prove `filter` is a monadic functor, with a push-forward operation
`filter.map` and a pull-back operation `filter.comap` that form a Galois connections for the
order on filters.
Finally we describe a product operation `filter X → filter Y → filter (X × Y)`.
The examples of filters appearing in the description of the two motivating ideas are:
* `(at_top : filter ℕ)` : made of sets of `ℕ` containing `{n | n ≥ N}` for some `N`
* `𝓝 x` : made of neighborhoods of `x` in a topological space (defined in topology.basic)
* `𝓤 X` : made of entourages of a uniform space (those space are generalizations of metric spaces
defined in topology.uniform_space.basic)
* `μ.a_e` : made of sets whose complement has zero measure with respect to `μ` (defined in
measure_theory.measure_space)
The general notion of limit of a map with respect to filters on the source and target types
is `filter.tendsto`. It is defined in terms of the order and the push-forward operation.
The predicate "happening eventually" is `filter.eventually`, and "happening often" is
`filter.frequently`, whose definitions are immediate after `filter` is defined (but they come
rather late in this file in order to immediately relate them to the lattice structure).
For instance, anticipating on topology.basic, the statement: "if a sequence `u` converges to
some `x` and `u n` belongs to a set `M` for `n` large enough then `x` is in the closure of
`M`" is formalized as: `tendsto u at_top (𝓝 x) → (∀ᶠ n in at_top, u n ∈ M) → x ∈ closure M`,
which is a special case of `mem_closure_of_tendsto` from topology.basic.
## Notations
* `∀ᶠ x in f, p x` : `f.eventually p`;
* `∃ᶠ x in f, p x` : `f.frequently p`;
* `f =ᶠ[l] g` : `∀ᶠ x in l, f x = g x`;
* `f ×ᶠ g` : `filter.prod f g`, localized in `filter`;
* `𝓟 s` : `principal s`, localized in `filter`.
## References
* [N. Bourbaki, *General Topology*][bourbaki1966]
Important note: Bourbaki requires that a filter on `X` cannot contain all sets of `X`, which
we do *not* require. This gives `filter X` better formal properties, in particular a bottom element
`⊥` for its lattice structure, at the cost of including the assumption
`f ≠ ⊥` in a number of lemmas and definitions.
-/
open set
universes u v w x y
open_locale classical
/-- A filter `F` on a type `α` is a collection of sets of `α` which contains the whole `α`,
is upwards-closed, and is stable under intersection. We do not forbid this collection to be
all sets of `α`. -/
structure filter (α : Type*) :=
(sets : set (set α))
(univ_sets : set.univ ∈ sets)
(sets_of_superset {x y} : x ∈ sets → x ⊆ y → y ∈ sets)
(inter_sets {x y} : x ∈ sets → y ∈ sets → x ∩ y ∈ sets)
/-- If `F` is a filter on `α`, and `U` a subset of `α` then we can write `U ∈ F` as on paper. -/
@[reducible]
instance {α : Type*}: has_mem (set α) (filter α) := ⟨λ U F, U ∈ F.sets⟩
namespace filter
variables {α : Type u} {f g : filter α} {s t : set α}
lemma filter_eq : ∀{f g : filter α}, f.sets = g.sets → f = g
| ⟨a, _, _, _⟩ ⟨._, _, _, _⟩ rfl := rfl
lemma filter_eq_iff : f = g ↔ f.sets = g.sets :=
⟨congr_arg _, filter_eq⟩
protected lemma ext_iff : f = g ↔ ∀ s, s ∈ f ↔ s ∈ g :=
by rw [filter_eq_iff, ext_iff]
@[ext]
protected lemma ext : (∀ s, s ∈ f ↔ s ∈ g) → f = g :=
filter.ext_iff.2
lemma univ_mem_sets : univ ∈ f :=
f.univ_sets
lemma mem_sets_of_superset : ∀{x y : set α}, x ∈ f → x ⊆ y → y ∈ f :=
f.sets_of_superset
lemma inter_mem_sets : ∀{s t}, s ∈ f → t ∈ f → s ∩ t ∈ f :=
f.inter_sets
lemma univ_mem_sets' (h : ∀ a, a ∈ s) : s ∈ f :=
mem_sets_of_superset univ_mem_sets (assume x _, h x)
lemma mp_sets (hs : s ∈ f) (h : {x | x ∈ s → x ∈ t} ∈ f) : t ∈ f :=
mem_sets_of_superset (inter_mem_sets hs h) $ assume x ⟨h₁, h₂⟩, h₂ h₁
lemma congr_sets (h : {x | x ∈ s ↔ x ∈ t} ∈ f) : s ∈ f ↔ t ∈ f :=
⟨λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mp)),
λ hs, mp_sets hs (mem_sets_of_superset h (λ x, iff.mpr))⟩
lemma Inter_mem_sets {β : Type v} {s : β → set α} {is : set β} (hf : finite is) :
(∀i∈is, s i ∈ f) → (⋂i∈is, s i) ∈ f :=
finite.induction_on hf
(assume hs, by simp only [univ_mem_sets, mem_empty_eq, Inter_neg, Inter_univ, not_false_iff])
(assume i is _ hf hi hs,
have h₁ : s i ∈ f, from hs i (by simp),
have h₂ : (⋂x∈is, s x) ∈ f, from hi $ assume a ha, hs _ $ by simp only [ha, mem_insert_iff, or_true],
by simp [inter_mem_sets h₁ h₂])
lemma sInter_mem_sets_of_finite {s : set (set α)} (hfin : finite s) (h_in : ∀ U ∈ s, U ∈ f) :
⋂₀ s ∈ f :=
by { rw sInter_eq_bInter, exact Inter_mem_sets hfin h_in }
lemma Inter_mem_sets_of_fintype {β : Type v} {s : β → set α} [fintype β] (h : ∀i, s i ∈ f) :
(⋂i, s i) ∈ f :=
by simpa using Inter_mem_sets finite_univ (λi hi, h i)
lemma exists_sets_subset_iff : (∃t ∈ f, t ⊆ s) ↔ s ∈ f :=
⟨assume ⟨t, ht, ts⟩, mem_sets_of_superset ht ts, assume hs, ⟨s, hs, subset.refl _⟩⟩
lemma monotone_mem_sets {f : filter α} : monotone (λs, s ∈ f) :=
assume s t hst h, mem_sets_of_superset h hst
end filter
namespace tactic.interactive
open tactic interactive
/-- `filter_upwards [h1, ⋯, hn]` replaces a goal of the form `s ∈ f`
and terms `h1 : t1 ∈ f, ⋯, hn : tn ∈ f` with `∀x, x ∈ t1 → ⋯ → x ∈ tn → x ∈ s`.
`filter_upwards [h1, ⋯, hn] e` is a short form for `{ filter_upwards [h1, ⋯, hn], exact e }`.
-/
meta def filter_upwards
(s : parse types.pexpr_list)
(e' : parse $ optional types.texpr) : tactic unit :=
do
s.reverse.mmap (λ e, eapplyc `filter.mp_sets >> eapply e),
eapplyc `filter.univ_mem_sets',
match e' with
| some e := interactive.exact e
| none := skip
end
end tactic.interactive
namespace filter
variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x}
section principal
/-- The principal filter of `s` is the collection of all supersets of `s`. -/
def principal (s : set α) : filter α :=
{ sets := {t | s ⊆ t},
univ_sets := subset_univ s,
sets_of_superset := assume x y hx hy, subset.trans hx hy,
inter_sets := assume x y, subset_inter }
localized "notation `𝓟` := filter.principal" in filter
instance : inhabited (filter α) :=
⟨𝓟 ∅⟩
@[simp] lemma mem_principal_sets {s t : set α} : s ∈ 𝓟 t ↔ t ⊆ s := iff.rfl
lemma mem_principal_self (s : set α) : s ∈ 𝓟 s := subset.refl _
end principal
open_locale filter
section join
/-- The join of a filter of filters is defined by the relation `s ∈ join f ↔ {t | s ∈ t} ∈ f`. -/
def join (f : filter (filter α)) : filter α :=
{ sets := {s | {t : filter α | s ∈ t} ∈ f},
univ_sets := by simp only [univ_mem_sets, mem_set_of_eq]; exact univ_mem_sets,
sets_of_superset := assume x y hx xy,
mem_sets_of_superset hx $ assume f h, mem_sets_of_superset h xy,
inter_sets := assume x y hx hy,
mem_sets_of_superset (inter_mem_sets hx hy) $ assume f ⟨h₁, h₂⟩, inter_mem_sets h₁ h₂ }
@[simp] lemma mem_join_sets {s : set α} {f : filter (filter α)} :
s ∈ join f ↔ {t | s ∈ t} ∈ f := iff.rfl
end join
section lattice
instance : partial_order (filter α) :=
{ le := λf g, ∀ ⦃U : set α⦄, U ∈ g → U ∈ f,
le_antisymm := assume a b h₁ h₂, filter_eq $ subset.antisymm h₂ h₁,
le_refl := assume a, subset.refl _,
le_trans := assume a b c h₁ h₂, subset.trans h₂ h₁ }
theorem le_def {f g : filter α} : f ≤ g ↔ ∀ x ∈ g, x ∈ f := iff.rfl
/-- `generate_sets g s`: `s` is in the filter closure of `g`. -/
inductive generate_sets (g : set (set α)) : set α → Prop
| basic {s : set α} : s ∈ g → generate_sets s
| univ : generate_sets univ
| superset {s t : set α} : generate_sets s → s ⊆ t → generate_sets t
| inter {s t : set α} : generate_sets s → generate_sets t → generate_sets (s ∩ t)
/-- `generate g` is the smallest filter containing the sets `g`. -/
def generate (g : set (set α)) : filter α :=
{ sets := generate_sets g,
univ_sets := generate_sets.univ,
sets_of_superset := assume x y, generate_sets.superset,
inter_sets := assume s t, generate_sets.inter }
lemma sets_iff_generate {s : set (set α)} {f : filter α} : f ≤ filter.generate s ↔ s ⊆ f.sets :=
iff.intro
(assume h u hu, h $ generate_sets.basic $ hu)
(assume h u hu, hu.rec_on h univ_mem_sets
(assume x y _ hxy hx, mem_sets_of_superset hx hxy)
(assume x y _ _ hx hy, inter_mem_sets hx hy))
lemma mem_generate_iff (s : set $ set α) {U : set α} : U ∈ generate s ↔ ∃ t ⊆ s, finite t ∧ ⋂₀ t ⊆ U :=
begin
split ; intro h,
{ induction h with V V_in V W V_in hVW hV V W V_in W_in hV hW,
{ use {V},
simp [V_in] },
{ use ∅,
simp [subset.refl, univ] },
{ rcases hV with ⟨t, hts, htfin, hinter⟩,
exact ⟨t, hts, htfin, subset.trans hinter hVW⟩ },
{ rcases hV with ⟨t, hts, htfin, htinter⟩,
rcases hW with ⟨z, hzs, hzfin, hzinter⟩,
refine ⟨t ∪ z, union_subset hts hzs, htfin.union hzfin, _⟩,
rw sInter_union,
exact inter_subset_inter htinter hzinter } },
{ rcases h with ⟨t, ts, tfin, h⟩,
apply generate_sets.superset _ h,
revert ts,
apply finite.induction_on tfin,
{ intro h,
rw sInter_empty,
exact generate_sets.univ },
{ intros V r hV rfin hinter h,
cases insert_subset.mp h with V_in r_sub,
rw [insert_eq V r, sInter_union],
apply generate_sets.inter _ (hinter r_sub),
rw sInter_singleton,
exact generate_sets.basic V_in } },
end
/-- `mk_of_closure s hs` constructs a filter on `α` whose elements set is exactly
`s : set (set α)`, provided one gives the assumption `hs : (generate s).sets = s`. -/
protected def mk_of_closure (s : set (set α)) (hs : (generate s).sets = s) : filter α :=
{ sets := s,
univ_sets := hs ▸ (univ_mem_sets : univ ∈ generate s),
sets_of_superset := assume x y, hs ▸ (mem_sets_of_superset : x ∈ generate s → x ⊆ y → y ∈ generate s),
inter_sets := assume x y, hs ▸ (inter_mem_sets : x ∈ generate s → y ∈ generate s → x ∩ y ∈ generate s) }
lemma mk_of_closure_sets {s : set (set α)} {hs : (generate s).sets = s} :
filter.mk_of_closure s hs = generate s :=
filter.ext $ assume u,
show u ∈ (filter.mk_of_closure s hs).sets ↔ u ∈ (generate s).sets, from hs.symm ▸ iff.rfl
/-- Galois insertion from sets of sets into filters. -/
def gi_generate (α : Type*) :
@galois_insertion (set (set α)) (order_dual (filter α)) _ _ filter.generate filter.sets :=
{ gc := assume s f, sets_iff_generate,
le_l_u := assume f u h, generate_sets.basic h,
choice := λs hs, filter.mk_of_closure s (le_antisymm hs $ sets_iff_generate.1 $ le_refl _),
choice_eq := assume s hs, mk_of_closure_sets }
/-- The infimum of filters is the filter generated by intersections
of elements of the two filters. -/
instance : has_inf (filter α) := ⟨λf g : filter α,
{ sets := {s | ∃ (a ∈ f) (b ∈ g), a ∩ b ⊆ s },
univ_sets := ⟨_, univ_mem_sets, _, univ_mem_sets, inter_subset_left _ _⟩,
sets_of_superset := assume x y ⟨a, ha, b, hb, h⟩ xy, ⟨a, ha, b, hb, subset.trans h xy⟩,
inter_sets := assume x y ⟨a, ha, b, hb, hx⟩ ⟨c, hc, d, hd, hy⟩,
⟨_, inter_mem_sets ha hc, _, inter_mem_sets hb hd,
calc a ∩ c ∩ (b ∩ d) = (a ∩ b) ∩ (c ∩ d) : by ac_refl
... ⊆ x ∩ y : inter_subset_inter hx hy⟩ }⟩
@[simp] lemma mem_inf_sets {f g : filter α} {s : set α} :
s ∈ f ⊓ g ↔ ∃t₁∈f, ∃t₂∈g, t₁ ∩ t₂ ⊆ s := iff.rfl
lemma mem_inf_sets_of_left {f g : filter α} {s : set α} (h : s ∈ f) : s ∈ f ⊓ g :=
⟨s, h, univ, univ_mem_sets, inter_subset_left _ _⟩
lemma mem_inf_sets_of_right {f g : filter α} {s : set α} (h : s ∈ g) : s ∈ f ⊓ g :=
⟨univ, univ_mem_sets, s, h, inter_subset_right _ _⟩
lemma inter_mem_inf_sets {α : Type u} {f g : filter α} {s t : set α}
(hs : s ∈ f) (ht : t ∈ g) : s ∩ t ∈ f ⊓ g :=
inter_mem_sets (mem_inf_sets_of_left hs) (mem_inf_sets_of_right ht)
instance : has_top (filter α) :=
⟨{ sets := {s | ∀x, x ∈ s},
univ_sets := assume x, mem_univ x,
sets_of_superset := assume x y hx hxy a, hxy (hx a),
inter_sets := assume x y hx hy a, mem_inter (hx _) (hy _) }⟩
lemma mem_top_sets_iff_forall {s : set α} : s ∈ (⊤ : filter α) ↔ (∀x, x ∈ s) :=
iff.rfl
@[simp] lemma mem_top_sets {s : set α} : s ∈ (⊤ : filter α) ↔ s = univ :=
by rw [mem_top_sets_iff_forall, eq_univ_iff_forall]
section complete_lattice
/- We lift the complete lattice along the Galois connection `generate` / `sets`. Unfortunately,
we want to have different definitional equalities for the lattice operations. So we define them
upfront and change the lattice operations for the complete lattice instance. -/
private def original_complete_lattice : complete_lattice (filter α) :=
@order_dual.complete_lattice _ (gi_generate α).lift_complete_lattice
local attribute [instance] original_complete_lattice
instance : complete_lattice (filter α) := original_complete_lattice.copy
/- le -/ filter.partial_order.le rfl
/- top -/ (filter.has_top).1
(top_unique $ assume s hs, by have := univ_mem_sets ; finish)
/- bot -/ _ rfl
/- sup -/ _ rfl
/- inf -/ (filter.has_inf).1
begin
ext f g : 2,
exact le_antisymm
(le_inf (assume s, mem_inf_sets_of_left) (assume s, mem_inf_sets_of_right))
(assume s ⟨a, ha, b, hb, hs⟩, show s ∈ complete_lattice.inf f g, from
mem_sets_of_superset (inter_mem_sets
(@inf_le_left (filter α) _ _ _ _ ha)
(@inf_le_right (filter α) _ _ _ _ hb)) hs)
end
/- Sup -/ (join ∘ 𝓟) (by ext s x; exact (@mem_bInter_iff _ _ s filter.sets x).symm)
/- Inf -/ _ rfl
end complete_lattice
lemma bot_sets_eq : (⊥ : filter α).sets = univ := rfl
lemma sup_sets_eq {f g : filter α} : (f ⊔ g).sets = f.sets ∩ g.sets :=
(gi_generate α).gc.u_inf
lemma Sup_sets_eq {s : set (filter α)} : (Sup s).sets = (⋂f∈s, (f:filter α).sets) :=
(gi_generate α).gc.u_Inf
lemma supr_sets_eq {f : ι → filter α} : (supr f).sets = (⋂i, (f i).sets) :=
(gi_generate α).gc.u_infi
lemma generate_empty : filter.generate ∅ = (⊤ : filter α) :=
(gi_generate α).gc.l_bot
lemma generate_univ : filter.generate univ = (⊥ : filter α) :=
mk_of_closure_sets.symm
lemma generate_union {s t : set (set α)} :
filter.generate (s ∪ t) = filter.generate s ⊓ filter.generate t :=
(gi_generate α).gc.l_sup
lemma generate_Union {s : ι → set (set α)} :
filter.generate (⋃ i, s i) = (⨅ i, filter.generate (s i)) :=
(gi_generate α).gc.l_supr
@[simp] lemma mem_bot_sets {s : set α} : s ∈ (⊥ : filter α) :=
trivial
@[simp] lemma mem_sup_sets {f g : filter α} {s : set α} :
s ∈ f ⊔ g ↔ s ∈ f ∧ s ∈ g :=
iff.rfl
@[simp] lemma mem_Sup_sets {x : set α} {s : set (filter α)} :
x ∈ Sup s ↔ (∀f∈s, x ∈ (f:filter α)) :=
iff.rfl
@[simp] lemma mem_supr_sets {x : set α} {f : ι → filter α} :
x ∈ supr f ↔ (∀i, x ∈ f i) :=
by simp only [supr_sets_eq, iff_self, mem_Inter]
lemma infi_eq_generate (s : ι → filter α) : infi s = generate (⋃ i, (s i).sets) :=
show generate _ = generate _, from congr_arg _ supr_range
lemma mem_infi_iff {ι} {s : ι → filter α} {U : set α} : (U ∈ ⨅ i, s i) ↔
∃ I : set ι, finite I ∧ ∃ V : {i | i ∈ I} → set α, (∀ i, V i ∈ s i) ∧ (⋂ i, V i) ⊆ U :=
begin
rw [infi_eq_generate, mem_generate_iff],
split,
{ rintro ⟨t, tsub, tfin, tinter⟩,
rcases eq_finite_Union_of_finite_subset_Union tfin tsub with ⟨I, Ifin, σ, σfin, σsub, rfl⟩,
rw sInter_Union at tinter,
let V := λ i, ⋂₀ σ i,
have V_in : ∀ i, V i ∈ s i,
{ rintro ⟨i, i_in⟩,
apply sInter_mem_sets_of_finite (σfin _),
apply σsub },
exact ⟨I, Ifin, V, V_in, tinter⟩ },
{ rintro ⟨I, Ifin, V, V_in, h⟩,
refine ⟨range V, _, _, h⟩,
{ rintro _ ⟨i, rfl⟩,
rw mem_Union,
use [i, V_in i] },
{ haveI : fintype {i : ι | i ∈ I} := finite.fintype Ifin,
exact finite_range _ } },
end
@[simp] lemma le_principal_iff {s : set α} {f : filter α} : f ≤ 𝓟 s ↔ s ∈ f :=
show (∀{t}, s ⊆ t → t ∈ f) ↔ s ∈ f,
from ⟨assume h, h (subset.refl s), assume hs t ht, mem_sets_of_superset hs ht⟩
lemma principal_mono {s t : set α} : 𝓟 s ≤ 𝓟 t ↔ s ⊆ t :=
by simp only [le_principal_iff, iff_self, mem_principal_sets]
lemma monotone_principal : monotone (𝓟 : set α → filter α) :=
λ _ _, principal_mono.2
@[simp] lemma principal_eq_iff_eq {s t : set α} : 𝓟 s = 𝓟 t ↔ s = t :=
by simp only [le_antisymm_iff, le_principal_iff, mem_principal_sets]; refl
@[simp] lemma join_principal_eq_Sup {s : set (filter α)} : join (𝓟 s) = Sup s := rfl
lemma principal_univ : 𝓟 (univ : set α) = ⊤ :=
top_unique $ by simp only [le_principal_iff, mem_top_sets, eq_self_iff_true]
lemma principal_empty : 𝓟 (∅ : set α) = ⊥ :=
bot_unique $ assume s _, empty_subset _
/-! ### Lattice equations -/
lemma empty_in_sets_eq_bot {f : filter α} : ∅ ∈ f ↔ f = ⊥ :=
⟨assume h, bot_unique $ assume s _, mem_sets_of_superset h (empty_subset s),
assume : f = ⊥, this.symm ▸ mem_bot_sets⟩
lemma nonempty_of_mem_sets {f : filter α} (hf : f ≠ ⊥) {s : set α} (hs : s ∈ f) :
s.nonempty :=
s.eq_empty_or_nonempty.elim (λ h, absurd hs (h.symm ▸ mt empty_in_sets_eq_bot.mp hf)) id
lemma nonempty_of_ne_bot {f : filter α} (hf : f ≠ ⊥) : nonempty α :=
nonempty_of_exists $ nonempty_of_mem_sets hf univ_mem_sets
lemma filter_eq_bot_of_not_nonempty {f : filter α} (ne : ¬ nonempty α) : f = ⊥ :=
empty_in_sets_eq_bot.mp $ univ_mem_sets' $ assume x, false.elim (ne ⟨x⟩)
lemma forall_sets_nonempty_iff_ne_bot {f : filter α} :
(∀ (s : set α), s ∈ f → s.nonempty) ↔ f ≠ ⊥ :=
⟨λ h hf, empty_not_nonempty (h ∅ $ hf.symm ▸ mem_bot_sets), nonempty_of_mem_sets⟩
lemma mem_sets_of_eq_bot {f : filter α} {s : set α} (h : f ⊓ 𝓟 sᶜ = ⊥) : s ∈ f :=
have ∅ ∈ f ⊓ 𝓟 sᶜ, from h.symm ▸ mem_bot_sets,
let ⟨s₁, hs₁, s₂, (hs₂ : sᶜ ⊆ s₂), (hs : s₁ ∩ s₂ ⊆ ∅)⟩ := this in
by filter_upwards [hs₁] assume a ha, classical.by_contradiction $ assume ha', hs ⟨ha, hs₂ ha'⟩
lemma inf_ne_bot_iff {f g : filter α} :
f ⊓ g ≠ ⊥ ↔ ∀ {U V}, U ∈ f → V ∈ g → set.nonempty (U ∩ V) :=
begin
rw ← forall_sets_nonempty_iff_ne_bot,
simp_rw mem_inf_sets,
split ; intro h,
{ intros U V U_in V_in,
exact h (U ∩ V) ⟨U, U_in, V, V_in, subset.refl _⟩ },
{ rintros S ⟨U, U_in, V, V_in, hUV⟩,
cases h U_in V_in with a ha,
use [a, hUV ha] }
end
lemma inf_principal_ne_bot_iff (f : filter α) (s : set α) :
f ⊓ 𝓟 s ≠ ⊥ ↔ ∀ U ∈ f, (U ∩ s).nonempty :=
begin
rw inf_ne_bot_iff,
apply forall_congr,
intros U,
split,
{ intros h U_in,
exact h U_in (mem_principal_self s) },
{ intros h V U_in V_in,
rw mem_principal_sets at V_in,
cases h U_in with x hx,
exact ⟨x, hx.1, V_in hx.2⟩ },
end
lemma inf_eq_bot_iff {f g : filter α} :
f ⊓ g = ⊥ ↔ ∃ U V, (U ∈ f) ∧ (V ∈ g) ∧ U ∩ V = ∅ :=
begin
rw ← not_iff_not,
simp only [not_exists, not_and, ← ne.def, inf_ne_bot_iff, ne_empty_iff_nonempty]
end
protected lemma disjoint_iff {f g : filter α} :
disjoint f g ↔ ∃ U V, (U ∈ f) ∧ (V ∈ g) ∧ U ∩ V = ∅ :=
disjoint_iff.trans inf_eq_bot_iff
lemma eq_Inf_of_mem_sets_iff_exists_mem {S : set (filter α)} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ f ∈ S, s ∈ f) : l = Inf S :=
le_antisymm (le_Inf $ λ f hf s hs, h.2 ⟨f, hf, hs⟩)
(λ s hs, let ⟨f, hf, hs⟩ := h.1 hs in (Inf_le hf : Inf S ≤ f) hs)
lemma eq_infi_of_mem_sets_iff_exists_mem {f : ι → filter α} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ i, s ∈ f i) :
l = infi f :=
eq_Inf_of_mem_sets_iff_exists_mem $ λ s, h.trans exists_range_iff.symm
lemma eq_binfi_of_mem_sets_iff_exists_mem {f : ι → filter α} {p : ι → Prop} {l : filter α}
(h : ∀ {s}, s ∈ l ↔ ∃ i (_ : p i), s ∈ f i) :
l = ⨅ i (_ : p i), f i :=
begin
rw [infi_subtype'],
apply eq_infi_of_mem_sets_iff_exists_mem,
intro s,
exact h.trans ⟨λ ⟨i, pi, si⟩, ⟨⟨i, pi⟩, si⟩, λ ⟨⟨i, pi⟩, si⟩, ⟨i, pi, si⟩⟩
end
lemma infi_sets_eq {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) :
(infi f).sets = (⋃ i, (f i).sets) :=
let ⟨i⟩ := ne, u := { filter .
sets := (⋃ i, (f i).sets),
univ_sets := by simp only [mem_Union]; exact ⟨i, univ_mem_sets⟩,
sets_of_superset := by simp only [mem_Union, exists_imp_distrib];
intros x y i hx hxy; exact ⟨i, mem_sets_of_superset hx hxy⟩,
inter_sets :=
begin
simp only [mem_Union, exists_imp_distrib],
assume x y a hx b hy,
rcases h a b with ⟨c, ha, hb⟩,
exact ⟨c, inter_mem_sets (ha hx) (hb hy)⟩
end } in
have u = infi f, from eq_infi_of_mem_sets_iff_exists_mem (λ s, by simp only [mem_Union]),
congr_arg filter.sets this.symm
lemma mem_infi {f : ι → filter α} (h : directed (≥) f) (ne : nonempty ι) (s) :
s ∈ infi f ↔ ∃ i, s ∈ f i :=
by simp only [infi_sets_eq h ne, mem_Union]
@[nolint ge_or_gt] -- Intentional use of `≥`
lemma binfi_sets_eq {f : β → filter α} {s : set β}
(h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) :
(⨅ i∈s, f i).sets = (⋃ i ∈ s, (f i).sets) :=
let ⟨i, hi⟩ := ne in
calc (⨅ i ∈ s, f i).sets = (⨅ t : {t // t ∈ s}, (f t.val)).sets : by rw [infi_subtype]; refl
... = (⨆ t : {t // t ∈ s}, (f t.val).sets) : infi_sets_eq
(assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end)
⟨⟨i, hi⟩⟩
... = (⨆ t ∈ {t | t ∈ s}, (f t).sets) : by rw [supr_subtype]; refl
@[nolint ge_or_gt] -- Intentional use of `≥`
lemma mem_binfi {f : β → filter α} {s : set β}
(h : directed_on (f ⁻¹'o (≥)) s) (ne : s.nonempty) {t : set α} :
t ∈ (⨅ i∈s, f i) ↔ ∃ i ∈ s, t ∈ f i :=
by simp only [binfi_sets_eq h ne, mem_bUnion_iff]
lemma infi_sets_eq_finite (f : ι → filter α) :
(⨅i, f i).sets = (⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets) :=
begin
rw [infi_eq_infi_finset, infi_sets_eq],
exact (directed_of_sup $ λs₁ s₂ hs, infi_le_infi $ λi, infi_le_infi_const $ λh, hs h),
apply_instance
end
lemma mem_infi_finite {f : ι → filter α} (s) :
s ∈ infi f ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets :=
show s ∈ (infi f).sets ↔ s ∈ ⋃t:finset (plift ι), (⨅i∈t, f (plift.down i)).sets,
by rw infi_sets_eq_finite
@[simp] lemma sup_join {f₁ f₂ : filter (filter α)} : (join f₁ ⊔ join f₂) = join (f₁ ⊔ f₂) :=
filter_eq $ set.ext $ assume x,
by simp only [supr_sets_eq, join, mem_sup_sets, iff_self, mem_set_of_eq]
@[simp] lemma supr_join {ι : Sort w} {f : ι → filter (filter α)} :
(⨆x, join (f x)) = join (⨆x, f x) :=
filter_eq $ set.ext $ assume x,
by simp only [supr_sets_eq, join, iff_self, mem_Inter, mem_set_of_eq]
instance : bounded_distrib_lattice (filter α) :=
{ le_sup_inf :=
begin
assume x y z s,
simp only [and_assoc, mem_inf_sets, mem_sup_sets, exists_prop, exists_imp_distrib, and_imp],
intros hs t₁ ht₁ t₂ ht₂ hts,
exact ⟨s ∪ t₁,
x.sets_of_superset hs $ subset_union_left _ _,
y.sets_of_superset ht₁ $ subset_union_right _ _,
s ∪ t₂,
x.sets_of_superset hs $ subset_union_left _ _,
z.sets_of_superset ht₂ $ subset_union_right _ _,
subset.trans (@le_sup_inf (set α) _ _ _ _) (union_subset (subset.refl _) hts)⟩
end,
..filter.complete_lattice }
/- the complementary version with ⨆i, f ⊓ g i does not hold! -/
lemma infi_sup_eq {f : filter α} {g : ι → filter α} : (⨅ x, f ⊔ g x) = f ⊔ infi g :=
begin
refine le_antisymm _ (le_infi $ assume i, sup_le_sup_left (infi_le _ _) _),
rintros t ⟨h₁, h₂⟩,
rw [infi_sets_eq_finite] at h₂,
simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at h₂,
rcases h₂ with ⟨s, hs⟩,
suffices : (⨅i, f ⊔ g i) ≤ f ⊔ s.inf (λi, g i.down), { exact this ⟨h₁, hs⟩ },
refine finset.induction_on s _ _,
{ exact le_sup_right_of_le le_top },
{ rintros ⟨i⟩ s his ih,
rw [finset.inf_insert, sup_inf_left],
exact le_inf (infi_le _ _) ih }
end
lemma mem_infi_sets_finset {s : finset α} {f : α → filter β} :
∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⋂a∈s, p a) ⊆ t) :=
show ∀t, t ∈ (⨅a∈s, f a) ↔ (∃p:α → set β, (∀a∈s, p a ∈ f a) ∧ (⨅a∈s, p a) ≤ t),
begin
simp only [(finset.inf_eq_infi _ _).symm],
refine finset.induction_on s _ _,
{ simp only [finset.not_mem_empty, false_implies_iff, finset.inf_empty, top_le_iff,
imp_true_iff, mem_top_sets, true_and, exists_const],
intros; refl },
{ intros a s has ih t,
simp only [ih, finset.forall_mem_insert, finset.inf_insert, mem_inf_sets,
exists_prop, iff_iff_implies_and_implies, exists_imp_distrib, and_imp, and_assoc] {contextual := tt},
split,
{ intros t₁ ht₁ t₂ p hp ht₂ ht,
existsi function.update p a t₁,
have : ∀a'∈s, function.update p a t₁ a' = p a',
from assume a' ha',
have a' ≠ a, from assume h, has $ h ▸ ha',
function.update_noteq this _ _,
have eq : s.inf (λj, function.update p a t₁ j) = s.inf (λj, p j) :=
finset.inf_congr rfl this,
simp only [this, ht₁, hp, function.update_same, true_and, imp_true_iff, eq] {contextual := tt},
exact subset.trans (inter_subset_inter (subset.refl _) ht₂) ht },
assume p hpa hp ht,
exact ⟨p a, hpa, (s.inf p), ⟨⟨p, hp, le_refl _⟩, ht⟩⟩ }
end
/-- If `f : ι → filter α` is directed, `ι` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`.
See also `infi_ne_bot_of_directed` for a version assuming `nonempty α` instead of `nonempty ι`. -/
lemma infi_ne_bot_of_directed' {f : ι → filter α} (hn : nonempty ι)
(hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥) : (infi f) ≠ ⊥ :=
begin
intro h,
have he: ∅ ∈ (infi f), from h.symm ▸ (mem_bot_sets : ∅ ∈ (⊥ : filter α)),
obtain ⟨i, hi⟩ : ∃i, ∅ ∈ f i,
from (mem_infi hd hn ∅).1 he,
exact hb i (empty_in_sets_eq_bot.1 hi)
end
/-- If `f : ι → filter α` is directed, `α` is not empty, and `∀ i, f i ≠ ⊥`, then `infi f ≠ ⊥`.
See also `infi_ne_bot_of_directed'` for a version assuming `nonempty ι` instead of `nonempty α`. -/
lemma infi_ne_bot_of_directed {f : ι → filter α}
(hn : nonempty α) (hd : directed (≥) f) (hb : ∀i, f i ≠ ⊥) : (infi f) ≠ ⊥ :=
if hι : nonempty ι then infi_ne_bot_of_directed' hι hd hb else
assume h : infi f = ⊥,
have univ ⊆ (∅ : set α),
begin
rw [←principal_mono, principal_univ, principal_empty, ←h],
exact (le_infi $ assume i, false.elim $ hι ⟨i⟩)
end,
let ⟨x⟩ := hn in this (mem_univ x)
lemma infi_ne_bot_iff_of_directed' {f : ι → filter α}
(hn : nonempty ι) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) :=
⟨assume ne_bot i, ne_bot_of_le_ne_bot ne_bot (infi_le _ i),
infi_ne_bot_of_directed' hn hd⟩
lemma infi_ne_bot_iff_of_directed {f : ι → filter α}
(hn : nonempty α) (hd : directed (≥) f) : (infi f) ≠ ⊥ ↔ (∀i, f i ≠ ⊥) :=
⟨assume ne_bot i, ne_bot_of_le_ne_bot ne_bot (infi_le _ i),
infi_ne_bot_of_directed hn hd⟩
lemma mem_infi_sets {f : ι → filter α} (i : ι) : ∀{s}, s ∈ f i → s ∈ ⨅i, f i :=
show (⨅i, f i) ≤ f i, from infi_le _ _
@[elab_as_eliminator]
lemma infi_sets_induct {f : ι → filter α} {s : set α} (hs : s ∈ infi f) {p : set α → Prop}
(uni : p univ)
(ins : ∀{i s₁ s₂}, s₁ ∈ f i → p s₂ → p (s₁ ∩ s₂))
(upw : ∀{s₁ s₂}, s₁ ⊆ s₂ → p s₁ → p s₂) : p s :=
begin
rw [mem_infi_finite] at hs,
simp only [mem_Union, (finset.inf_eq_infi _ _).symm] at hs,
rcases hs with ⟨is, his⟩,
revert s,
refine finset.induction_on is _ _,
{ assume s hs, rwa [mem_top_sets.1 hs] },
{ rintros ⟨i⟩ js his ih s hs,
rw [finset.inf_insert, mem_inf_sets] at hs,
rcases hs with ⟨s₁, hs₁, s₂, hs₂, hs⟩,
exact upw hs (ins hs₁ (ih hs₂)) }
end
/- principal equations -/
@[simp] lemma inf_principal {s t : set α} : 𝓟 s ⊓ 𝓟 t = 𝓟 (s ∩ t) :=
le_antisymm
(by simp; exact ⟨s, subset.refl s, t, subset.refl t, by simp⟩)
(by simp [le_inf_iff, inter_subset_left, inter_subset_right])
@[simp] lemma sup_principal {s t : set α} : 𝓟 s ⊔ 𝓟 t = 𝓟 (s ∪ t) :=
filter_eq $ set.ext $
by simp only [union_subset_iff, union_subset_iff, mem_sup_sets, forall_const, iff_self, mem_principal_sets]
@[simp] lemma supr_principal {ι : Sort w} {s : ι → set α} : (⨆x, 𝓟 (s x)) = 𝓟 (⋃i, s i) :=
filter_eq $ set.ext $ assume x, by simp only [supr_sets_eq, mem_principal_sets, mem_Inter];
exact (@supr_le_iff (set α) _ _ _ _).symm
@[simp] lemma principal_eq_bot_iff {s : set α} : 𝓟 s = ⊥ ↔ s = ∅ :=
empty_in_sets_eq_bot.symm.trans $ mem_principal_sets.trans subset_empty_iff
lemma principal_ne_bot_iff {s : set α} : 𝓟 s ≠ ⊥ ↔ s.nonempty :=
(not_congr principal_eq_bot_iff).trans ne_empty_iff_nonempty
lemma is_compl_principal (s : set α) : is_compl (𝓟 s) (𝓟 sᶜ) :=
⟨by simp only [inf_principal, inter_compl_self, principal_empty, le_refl],
by simp only [sup_principal, union_compl_self, principal_univ, le_refl]⟩
lemma inf_principal_eq_bot {f : filter α} {s : set α} (hs : sᶜ ∈ f) : f ⊓ 𝓟 s = ⊥ :=
empty_in_sets_eq_bot.mp ⟨_, hs, s, mem_principal_self s, assume x ⟨h₁, h₂⟩, h₁ h₂⟩
theorem mem_inf_principal (f : filter α) (s t : set α) :
s ∈ f ⊓ 𝓟 t ↔ {x | x ∈ t → x ∈ s} ∈ f :=
begin
simp only [← le_principal_iff, (is_compl_principal s).le_left_iff, disjoint, inf_assoc,
inf_principal, imp_iff_not_or],
rw [← disjoint, ← (is_compl_principal (t ∩ sᶜ)).le_right_iff, compl_inter, compl_compl],
refl
end
lemma mem_iff_inf_principal_compl {f : filter α} {V : set α} :
V ∈ f ↔ f ⊓ 𝓟 Vᶜ = ⊥ :=
begin
rw inf_eq_bot_iff,
split,
{ intro h,
use [V, Vᶜ],
simp [h, subset.refl] },
{ rintros ⟨U, W, U_in, W_in, UW⟩,
rw [mem_principal_sets, compl_subset_comm] at W_in,
apply mem_sets_of_superset U_in,
intros x x_in,
apply W_in,
intro H,
have : x ∈ U ∩ W := ⟨x_in, H⟩,
rwa UW at this },
end
lemma le_iff_forall_inf_principal_compl {f g : filter α} :
f ≤ g ↔ ∀ V ∈ g, f ⊓ 𝓟 Vᶜ = ⊥ :=
begin
change (∀ V ∈ g, V ∈ f) ↔ _,
simp_rw [mem_iff_inf_principal_compl],
end
lemma principal_le_iff {s : set α} {f : filter α} :
𝓟 s ≤ f ↔ ∀ V ∈ f, s ⊆ V :=
begin
change (∀ V, V ∈ f → V ∈ _) ↔ _,
simp_rw mem_principal_sets,
end
@[simp] lemma infi_principal_finset {ι : Type w} (s : finset ι) (f : ι → set α) :
(⨅i∈s, 𝓟 (f i)) = 𝓟 (⋂i∈s, f i) :=
begin
ext t,
simp [mem_infi_sets_finset],
split,
{ rintros ⟨p, hp, ht⟩,
calc (⋂ (i : ι) (H : i ∈ s), f i) ≤ (⋂ (i : ι) (H : i ∈ s), p i) :
infi_le_infi (λi, infi_le_infi (λhi, mem_principal_sets.1 (hp i hi)))
... ≤ t : ht },
{ assume h,
exact ⟨f, λi hi, subset.refl _, h⟩ }
end
@[simp] lemma infi_principal_fintype {ι : Type w} [fintype ι] (f : ι → set α) :
(⨅i, 𝓟 (f i)) = 𝓟 (⋂i, f i) :=
by simpa using infi_principal_finset finset.univ f
end lattice
/-! ### Eventually -/
/-- `f.eventually p` or `∀ᶠ x in f, p x` mean that `{x | p x} ∈ f`. E.g., `∀ᶠ x in at_top, p x`
means that `p` holds true for sufficiently large `x`. -/
protected def eventually (p : α → Prop) (f : filter α) : Prop := {x | p x} ∈ f
notation `∀ᶠ` binders ` in ` f `, ` r:(scoped p, filter.eventually p f) := r
lemma eventually_iff {f : filter α} {P : α → Prop} : (∀ᶠ x in f, P x) ↔ {x | P x} ∈ f :=
iff.rfl
protected lemma ext' {f₁ f₂ : filter α}
(h : ∀ p : α → Prop, (∀ᶠ x in f₁, p x) ↔ (∀ᶠ x in f₂, p x)) :
f₁ = f₂ :=
filter.ext h
lemma eventually.filter_mono {f₁ f₂ : filter α} (h : f₁ ≤ f₂) {p : α → Prop}
(hp : ∀ᶠ x in f₂, p x) :
∀ᶠ x in f₁, p x :=
h hp
lemma eventually_of_mem {f : filter α} {P : α → Prop} {U : set α} (hU : U ∈ f) (h : ∀ x ∈ U, P x) :
∀ᶠ x in f, P x :=
mem_sets_of_superset hU h
protected lemma eventually.and {p q : α → Prop} {f : filter α} :
f.eventually p → f.eventually q → ∀ᶠ x in f, p x ∧ q x :=
inter_mem_sets
@[simp]
lemma eventually_true (f : filter α) : ∀ᶠ x in f, true := univ_mem_sets
lemma eventually_of_forall {p : α → Prop} (f : filter α) (hp : ∀ x, p x) :
∀ᶠ x in f, p x :=
univ_mem_sets' hp
@[simp] lemma eventually_false_iff_eq_bot {f : filter α} :
(∀ᶠ x in f, false) ↔ f = ⊥ :=
empty_in_sets_eq_bot
@[simp] lemma eventually_const {f : filter α} (hf : f ≠ ⊥) {p : Prop} :
(∀ᶠ x in f, p) ↔ p :=
classical.by_cases (λ h : p, by simp [h]) (λ h, by simp [h, hf])
lemma eventually.mp {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x)
(hq : ∀ᶠ x in f, p x → q x) :
∀ᶠ x in f, q x :=
mp_sets hp hq
lemma eventually.mono {p q : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x)
(hq : ∀ x, p x → q x) :
∀ᶠ x in f, q x :=
hp.mp (f.eventually_of_forall hq)
@[simp] lemma eventually_and {p q : α → Prop} {f : filter α} :
(∀ᶠ x in f, p x ∧ q x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in f, q x) :=
⟨λ h, ⟨h.mono $ λ _, and.left, h.mono $ λ _, and.right⟩, λ h, h.1.and h.2⟩
lemma eventually.congr {f : filter α} {p q : α → Prop} (h' : ∀ᶠ x in f, p x)
(h : ∀ᶠ x in f, p x ↔ q x) : ∀ᶠ x in f, q x :=
h'.mp (h.mono $ λ x hx, hx.mp)
lemma eventually_congr {f : filter α} {p q : α → Prop} (h : ∀ᶠ x in f, p x ↔ q x) :
(∀ᶠ x in f, p x) ↔ (∀ᶠ x in f, q x) :=
⟨λ hp, hp.congr h, λ hq, hq.congr $ by simpa only [iff.comm] using h⟩
@[simp] lemma eventually_or_distrib_left {f : filter α} {p : Prop} {q : α → Prop} :
(∀ᶠ x in f, p ∨ q x) ↔ (p ∨ ∀ᶠ x in f, q x) :=
classical.by_cases (λ h : p, by simp [h]) (λ h, by simp [h])
@[simp] lemma eventually_or_distrib_right {f : filter α} {p : α → Prop} {q : Prop} :
(∀ᶠ x in f, p x ∨ q) ↔ ((∀ᶠ x in f, p x) ∨ q) :=
by simp only [or_comm _ q, eventually_or_distrib_left]
@[simp] lemma eventually_imp_distrib_left {f : filter α} {p : Prop} {q : α → Prop} :
(∀ᶠ x in f, p → q x) ↔ (p → ∀ᶠ x in f, q x) :=
by simp only [imp_iff_not_or, eventually_or_distrib_left]
@[simp]
lemma eventually_bot {p : α → Prop} : ∀ᶠ x in ⊥, p x := ⟨⟩
@[simp]
lemma eventually_top {p : α → Prop} : (∀ᶠ x in ⊤, p x) ↔ (∀ x, p x) :=
iff.rfl
lemma eventually_sup {p : α → Prop} {f g : filter α} :
(∀ᶠ x in f ⊔ g, p x) ↔ (∀ᶠ x in f, p x) ∧ (∀ᶠ x in g, p x) :=
iff.rfl
@[simp]
lemma eventually_Sup {p : α → Prop} {fs : set (filter α)} :
(∀ᶠ x in Sup fs, p x) ↔ (∀ f ∈ fs, ∀ᶠ x in f, p x) :=
iff.rfl
@[simp]
lemma eventually_supr {p : α → Prop} {fs : β → filter α} :
(∀ᶠ x in (⨆ b, fs b), p x) ↔ (∀ b, ∀ᶠ x in fs b, p x) :=
mem_supr_sets
@[simp]
lemma eventually_principal {a : set α} {p : α → Prop} :
(∀ᶠ x in 𝓟 a, p x) ↔ (∀ x ∈ a, p x) :=
iff.rfl
/-! ### Frequently -/
/-- `f.frequently p` or `∃ᶠ x in f, p x` mean that `{x | ¬p x} ∉ f`. E.g., `∃ᶠ x in at_top, p x`
means that there exist arbitrarily large `x` for which `p` holds true. -/
protected def frequently (p : α → Prop) (f : filter α) : Prop := ¬∀ᶠ x in f, ¬p x
notation `∃ᶠ` binders ` in ` f `, ` r:(scoped p, filter.frequently p f) := r
lemma eventually.frequently {f : filter α} (hf : f ≠ ⊥) {p : α → Prop} (h : ∀ᶠ x in f, p x) :
∃ᶠ x in f, p x :=
begin
assume h',
have := h.and h',
simp only [and_not_self, eventually_false_iff_eq_bot] at this,
exact hf this
end
lemma frequently_of_forall {f : filter α} (hf : f ≠ ⊥) {p : α → Prop} (h : ∀ x, p x) :
∃ᶠ x in f, p x :=
eventually.frequently hf (f.eventually_of_forall h)
lemma frequently.mp {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x)
(hpq : ∀ᶠ x in f, p x → q x) :
∃ᶠ x in f, q x :=
mt (λ hq, hq.mp $ hpq.mono $ λ x, mt) h
lemma frequently.mono {p q : α → Prop} {f : filter α} (h : ∃ᶠ x in f, p x)
(hpq : ∀ x, p x → q x) :
∃ᶠ x in f, q x :=
h.mp (f.eventually_of_forall hpq)
lemma frequently.and_eventually {p q : α → Prop} {f : filter α}
(hp : ∃ᶠ x in f, p x) (hq : ∀ᶠ x in f, q x) :
∃ᶠ x in f, p x ∧ q x :=
begin
refine mt (λ h, hq.mp $ h.mono _) hp,
assume x hpq hq hp,
exact hpq ⟨hp, hq⟩
end
lemma frequently.exists {p : α → Prop} {f : filter α} (hp : ∃ᶠ x in f, p x) : ∃ x, p x :=
begin
by_contradiction H,
replace H : ∀ᶠ x in f, ¬ p x, from f.eventually_of_forall (not_exists.1 H),
exact hp H
end
lemma eventually.exists {p : α → Prop} {f : filter α} (hp : ∀ᶠ x in f, p x) (hf : f ≠ ⊥) :
∃ x, p x :=
(hp.frequently hf).exists
lemma frequently_iff_forall_eventually_exists_and {p : α → Prop} {f : filter α} :
(∃ᶠ x in f, p x) ↔ ∀ {q : α → Prop}, (∀ᶠ x in f, q x) → ∃ x, p x ∧ q x :=
⟨assume hp q hq, (hp.and_eventually hq).exists,
assume H hp, by simpa only [and_not_self, exists_false] using H hp⟩
lemma frequently_iff {f : filter α} {P : α → Prop} :
(∃ᶠ x in f, P x) ↔ ∀ {U}, U ∈ f → ∃ x ∈ U, P x :=
begin
rw frequently_iff_forall_eventually_exists_and,
split ; intro h,
{ intros U U_in,
simpa [exists_prop, and_comm] using h U_in },
{ intros H H',
simpa [and_comm] using h H' },
end
@[simp] lemma not_eventually {p : α → Prop} {f : filter α} :
(¬ ∀ᶠ x in f, p x) ↔ (∃ᶠ x in f, ¬ p x) :=
by simp [filter.frequently]
@[simp] lemma not_frequently {p : α → Prop} {f : filter α} :
(¬ ∃ᶠ x in f, p x) ↔ (∀ᶠ x in f, ¬ p x) :=
by simp only [filter.frequently, not_not]
@[simp] lemma frequently_true_iff_ne_bot (f : filter α) : (∃ᶠ x in f, true) ↔ f ≠ ⊥ :=
by simp [filter.frequently, -not_eventually, eventually_false_iff_eq_bot]
@[simp] lemma frequently_false (f : filter α) : ¬ ∃ᶠ x in f, false := by simp
@[simp] lemma frequently_const {f : filter α} (hf : f ≠ ⊥) {p : Prop} :
(∃ᶠ x in f, p) ↔ p :=
classical.by_cases (λ h : p, by simp [*]) (λ h, by simp [*])
@[simp] lemma frequently_or_distrib {f : filter α} {p q : α → Prop} :
(∃ᶠ x in f, p x ∨ q x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in f, q x) :=
by simp only [filter.frequently, ← not_and_distrib, not_or_distrib, eventually_and]
lemma frequently_or_distrib_left {f : filter α} (hf : f ≠ ⊥) {p : Prop} {q : α → Prop} :
(∃ᶠ x in f, p ∨ q x) ↔ (p ∨ ∃ᶠ x in f, q x) :=
by simp [hf]
lemma frequently_or_distrib_right {f : filter α} (hf : f ≠ ⊥) {p : α → Prop} {q : Prop} :
(∃ᶠ x in f, p x ∨ q) ↔ (∃ᶠ x in f, p x) ∨ q :=
by simp [hf]
@[simp] lemma frequently_imp_distrib {f : filter α} {p q : α → Prop} :
(∃ᶠ x in f, p x → q x) ↔ ((∀ᶠ x in f, p x) → ∃ᶠ x in f, q x) :=
by simp [imp_iff_not_or, not_eventually, frequently_or_distrib]
lemma frequently_imp_distrib_left {f : filter α} (hf : f ≠ ⊥) {p : Prop} {q : α → Prop} :
(∃ᶠ x in f, p → q x) ↔ (p → ∃ᶠ x in f, q x) :=
by simp [hf]
lemma frequently_imp_distrib_right {f : filter α} (hf : f ≠ ⊥) {p : α → Prop} {q : Prop} :
(∃ᶠ x in f, p x → q) ↔ ((∀ᶠ x in f, p x) → q) :=
by simp [hf]
@[simp] lemma eventually_imp_distrib_right {f : filter α} {p : α → Prop} {q : Prop} :
(∀ᶠ x in f, p x → q) ↔ ((∃ᶠ x in f, p x) → q) :=
by simp only [imp_iff_not_or, eventually_or_distrib_right, not_frequently]
@[simp] lemma frequently_bot {p : α → Prop} : ¬ ∃ᶠ x in ⊥, p x := by simp
@[simp]
lemma frequently_top {p : α → Prop} : (∃ᶠ x in ⊤, p x) ↔ (∃ x, p x) :=
by simp [filter.frequently]
lemma inf_ne_bot_iff_frequently_left {f g : filter α} :
f ⊓ g ≠ ⊥ ↔ ∀ {p : α → Prop}, (∀ᶠ x in f, p x) → ∃ᶠ x in g, p x :=
begin
rw filter.inf_ne_bot_iff,
split ; intro h,
{ intros U U_in H,
rcases h U_in H with ⟨x, hx, hx'⟩,
exact hx' hx},
{ intros U V U_in V_in,
classical,
by_contra H,
exact h U_in (mem_sets_of_superset V_in $ λ v v_in v_in', H ⟨v, v_in', v_in⟩) }
end
lemma inf_ne_bot_iff_frequently_right {f g : filter α} :
f ⊓ g ≠ ⊥ ↔ ∀ {p : α → Prop}, (∀ᶠ x in g, p x) → ∃ᶠ x in f, p x :=
by { rw inf_comm, exact filter.inf_ne_bot_iff_frequently_left }
@[simp]
lemma frequently_principal {a : set α} {p : α → Prop} :
(∃ᶠ x in 𝓟 a, p x) ↔ (∃ x ∈ a, p x) :=
by simp [filter.frequently, not_forall]
lemma frequently_sup {p : α → Prop} {f g : filter α} :
(∃ᶠ x in f ⊔ g, p x) ↔ (∃ᶠ x in f, p x) ∨ (∃ᶠ x in g, p x) :=
by simp only [filter.frequently, eventually_sup, not_and_distrib]
@[simp]
lemma frequently_Sup {p : α → Prop} {fs : set (filter α)} :
(∃ᶠ x in Sup fs, p x) ↔ (∃ f ∈ fs, ∃ᶠ x in f, p x) :=
by simp [filter.frequently, -not_eventually, not_forall]
@[simp]
lemma frequently_supr {p : α → Prop} {fs : β → filter α} :
(∃ᶠ x in (⨆ b, fs b), p x) ↔ (∃ b, ∃ᶠ x in fs b, p x) :=
by simp [filter.frequently, -not_eventually, not_forall]
/-!
### Relation “eventually equal”
-/
/-- Two functions `f` and `g` are *eventually equal* along a filter `l` if the set of `x` such that
`f x = g x` belongs to `l`. -/
def eventually_eq (l : filter α) (f g : α → β) : Prop := ∀ᶠ x in l, f x = g x
notation f ` =ᶠ[`l`] ` g := eventually_eq l f g
lemma eventually_eq.rw {l : filter α} {f g : α → β} (h : f =ᶠ[l] g) (p : α → β → Prop)
(hf : ∀ᶠ x in l, p x (f x)) :
∀ᶠ x in l, p x (g x) :=
hf.congr $ h.mono $ λ x hx, hx ▸ iff.rfl
@[refl] lemma eventually_eq.refl (l : filter α) (f : α → β) :
f =ᶠ[l] f :=
eventually_of_forall l $ λ x, rfl
@[symm] lemma eventually_eq.symm {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) :
g =ᶠ[l] f :=
H.mono $ λ _, eq.symm
@[trans] lemma eventually_eq.trans {f g h : α → β} {l : filter α}
(H₁ : f =ᶠ[l] g) (H₂ : g =ᶠ[l] h) :
f =ᶠ[l] h :=
H₂.rw (λ x y, f x = y) H₁
lemma eventually_eq.fun_comp {f g : α → β} {l : filter α} (H : f =ᶠ[l] g) (h : β → γ) :
(h ∘ f) =ᶠ[l] (h ∘ g) :=
H.mono $ λ x hx, congr_arg h hx
lemma eventually_eq.comp₂ {δ} {f f' : α → β} {g g' : α → γ} {l} (Hf : f =ᶠ[l] f') (h : β → γ → δ)
(Hg : g =ᶠ[l] g') :
(λ x, h (f x) (g x)) =ᶠ[l] (λ x, h (f' x) (g' x)) :=
Hf.mp $ Hg.mono $ by { intros, simp only * }
@[to_additive]
lemma eventually_eq.mul [has_mul β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g)
(h' : f' =ᶠ[l] g') :
((λ x, f x * f' x) =ᶠ[l] (λ x, g x * g' x)) :=
h.comp₂ (*) h'
@[to_additive]
lemma eventually_eq.inv [has_inv β] {f g : α → β} {l : filter α} (h : f =ᶠ[l] g) :
((λ x, (f x)⁻¹) =ᶠ[l] (λ x, (g x)⁻¹)) :=
h.fun_comp has_inv.inv
lemma eventually_eq.div [group_with_zero β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g)
(h' : f' =ᶠ[l] g') :
((λ x, f x / f' x) =ᶠ[l] (λ x, g x / g' x)) :=
h.mul h'.inv
lemma eventually_eq.sub [add_group β] {f f' g g' : α → β} {l : filter α} (h : f =ᶠ[l] g)
(h' : f' =ᶠ[l] g') :
((λ x, f x - f' x) =ᶠ[l] (λ x, g x - g' x)) :=
h.add h'.neg
/-! ### Push-forwards, pull-backs, and the monad structure -/
section map
/-- The forward map of a filter -/
def map (m : α → β) (f : filter α) : filter β :=
{ sets := preimage m ⁻¹' f.sets,
univ_sets := univ_mem_sets,
sets_of_superset := assume s t hs st, mem_sets_of_superset hs $ preimage_mono st,
inter_sets := assume s t hs ht, inter_mem_sets hs ht }
@[simp] lemma map_principal {s : set α} {f : α → β} :
map f (𝓟 s) = 𝓟 (set.image f s) :=
filter_eq $ set.ext $ assume a, image_subset_iff.symm
variables {f : filter α} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] lemma eventually_map {P : β → Prop} :
(∀ᶠ b in map m f, P b) ↔ ∀ᶠ a in f, P (m a) :=
iff.rfl
@[simp] lemma frequently_map {P : β → Prop} :
(∃ᶠ b in map m f, P b) ↔ ∃ᶠ a in f, P (m a) :=
iff.rfl
@[simp] lemma mem_map : t ∈ map m f ↔ {x | m x ∈ t} ∈ f := iff.rfl
lemma image_mem_map (hs : s ∈ f) : m '' s ∈ map m f :=
f.sets_of_superset hs $ subset_preimage_image m s
lemma range_mem_map : range m ∈ map m f :=
by rw ←image_univ; exact image_mem_map univ_mem_sets
lemma mem_map_sets_iff : t ∈ map m f ↔ (∃s∈f, m '' s ⊆ t) :=
iff.intro
(assume ht, ⟨set.preimage m t, ht, image_preimage_subset _ _⟩)
(assume ⟨s, hs, ht⟩, mem_sets_of_superset (image_mem_map hs) ht)
@[simp] lemma map_id : filter.map id f = f :=
filter_eq $ rfl
@[simp] lemma map_compose : filter.map m' ∘ filter.map m = filter.map (m' ∘ m) :=
funext $ assume _, filter_eq $ rfl
@[simp] lemma map_map : filter.map m' (filter.map m f) = filter.map (m' ∘ m) f :=
congr_fun (@@filter.map_compose m m') f
/-- If functions `m₁` and `m₂` are eventually equal at a filter `f`, then
they map this filter to the same filter. -/
lemma map_congr {m₁ m₂ : α → β} {f : filter α} (h : m₁ =ᶠ[f] m₂) :
map m₁ f = map m₂ f :=
filter.ext' $ λ p,
by { simp only [eventually_map], exact eventually_congr (h.mono $ λ x hx, hx ▸ iff.rfl) }
end map
section comap
/-- The inverse map of a filter -/
def comap (m : α → β) (f : filter β) : filter α :=
{ sets := { s | ∃t∈ f, m ⁻¹' t ⊆ s },
univ_sets := ⟨univ, univ_mem_sets, by simp only [subset_univ, preimage_univ]⟩,
sets_of_superset := assume a b ⟨a', ha', ma'a⟩ ab,
⟨a', ha', subset.trans ma'a ab⟩,
inter_sets := assume a b ⟨a', ha₁, ha₂⟩ ⟨b', hb₁, hb₂⟩,
⟨a' ∩ b', inter_mem_sets ha₁ hb₁, inter_subset_inter ha₂ hb₂⟩ }
@[simp] lemma eventually_comap {f : filter β} {φ : α → β} {P : α → Prop} :
(∀ᶠ a in comap φ f, P a) ↔ ∀ᶠ b in f, ∀ a, φ a = b → P a :=
begin
split ; intro h,
{ rcases h with ⟨t, t_in, ht⟩,
apply mem_sets_of_superset t_in,
rintros y y_in _ rfl,
apply ht y_in },
{ exact ⟨_, h, λ _ x_in, x_in _ rfl⟩ }
end
@[simp] lemma frequently_comap {f : filter β} {φ : α → β} {P : α → Prop} :
(∃ᶠ a in comap φ f, P a) ↔ ∃ᶠ b in f, ∃ a, φ a = b ∧ P a :=
begin
classical,
erw [← not_iff_not, not_not, not_not, filter.eventually_comap],
simp only [not_exists, not_and],
end
end comap
/-- The monadic bind operation on filter is defined the usual way in terms of `map` and `join`.
Unfortunately, this `bind` does not result in the expected applicative. See `filter.seq` for the
applicative instance. -/
def bind (f : filter α) (m : α → filter β) : filter β := join (map m f)
/-- The applicative sequentiation operation. This is not induced by the bind operation. -/
def seq (f : filter (α → β)) (g : filter α) : filter β :=
⟨{ s | ∃u∈ f, ∃t∈ g, (∀m∈u, ∀x∈t, (m : α → β) x ∈ s) },
⟨univ, univ_mem_sets, univ, univ_mem_sets, by simp only [forall_prop_of_true, mem_univ, forall_true_iff]⟩,
assume s₀ s₁ ⟨t₀, t₁, h₀, h₁, h⟩ hst, ⟨t₀, t₁, h₀, h₁, assume x hx y hy, hst $ h _ hx _ hy⟩,
assume s₀ s₁ ⟨t₀, ht₀, t₁, ht₁, ht⟩ ⟨u₀, hu₀, u₁, hu₁, hu⟩,
⟨t₀ ∩ u₀, inter_mem_sets ht₀ hu₀, t₁ ∩ u₁, inter_mem_sets ht₁ hu₁,
assume x ⟨hx₀, hx₁⟩ x ⟨hy₀, hy₁⟩, ⟨ht _ hx₀ _ hy₀, hu _ hx₁ _ hy₁⟩⟩⟩
/-- `pure x` is the set of sets that contain `x`. It is equal to `𝓟 {x}` but
with this definition we have `s ∈ pure a` defeq `a ∈ s`. -/
instance : has_pure filter :=
⟨λ (α : Type u) x,
{ sets := {s | x ∈ s},
inter_sets := λ s t, and.intro,
sets_of_superset := λ s t hs hst, hst hs,
univ_sets := trivial }⟩
instance : has_bind filter := ⟨@filter.bind⟩
instance : has_seq filter := ⟨@filter.seq⟩
instance : functor filter := { map := @filter.map }
lemma pure_sets (a : α) : (pure a : filter α).sets = {s | a ∈ s} := rfl
@[simp] lemma mem_pure_sets {a : α} {s : set α} : s ∈ (pure a : filter α) ↔ a ∈ s := iff.rfl
lemma pure_eq_principal (a : α) : (pure a : filter α) = 𝓟 {a} :=
filter.ext $ λ s, by simp only [mem_pure_sets, mem_principal_sets, singleton_subset_iff]
@[simp] lemma map_pure (f : α → β) (a : α) : map f (pure a) = pure (f a) :=
filter.ext $ λ s, iff.rfl
@[simp] lemma join_pure (f : filter α) : join (pure f) = f := filter.ext $ λ s, iff.rfl
@[simp] lemma pure_bind (a : α) (m : α → filter β) :
bind (pure a) m = m a :=
by simp only [has_bind.bind, bind, map_pure, join_pure]
section
-- this section needs to be before applicative, otherwise the wrong instance will be chosen
/-- The monad structure on filters. -/
protected def monad : monad filter := { map := @filter.map }
local attribute [instance] filter.monad
protected lemma is_lawful_monad : is_lawful_monad filter :=
{ id_map := assume α f, filter_eq rfl,
pure_bind := assume α β, pure_bind,
bind_assoc := assume α β γ f m₁ m₂, filter_eq rfl,
bind_pure_comp_eq_map := assume α β f x, filter.ext $ λ s,
by simp only [has_bind.bind, bind, functor.map, mem_map, mem_join_sets, mem_set_of_eq,
function.comp, mem_pure_sets] }
end
instance : applicative filter := { map := @filter.map, seq := @filter.seq }
instance : alternative filter :=
{ failure := λα, ⊥,
orelse := λα x y, x ⊔ y }
@[simp] lemma map_def {α β} (m : α → β) (f : filter α) : m <$> f = map m f := rfl
@[simp] lemma bind_def {α β} (f : filter α) (m : α → filter β) : f >>= m = bind f m := rfl
/- map and comap equations -/
section map
variables {f f₁ f₂ : filter α} {g g₁ g₂ : filter β} {m : α → β} {m' : β → γ} {s : set α} {t : set β}
@[simp] theorem mem_comap_sets : s ∈ comap m g ↔ ∃t∈ g, m ⁻¹' t ⊆ s := iff.rfl
theorem preimage_mem_comap (ht : t ∈ g) : m ⁻¹' t ∈ comap m g :=
⟨t, ht, subset.refl _⟩
lemma comap_id : comap id f = f :=
le_antisymm (assume s, preimage_mem_comap) (assume s ⟨t, ht, hst⟩, mem_sets_of_superset ht hst)
lemma comap_const_of_not_mem {x : α} {f : filter α} {V : set α} (hV : V ∈ f) (hx : x ∉ V) :
comap (λ y : α, x) f = ⊥ :=
begin
ext W,
suffices : ∃ t ∈ f, (λ (y : α), x) ⁻¹' t ⊆ W, by simpa,
use [V, hV],
simp [preimage_const_of_not_mem hx],
end
lemma comap_const_of_mem {x : α} {f : filter α} (h : ∀ V ∈ f, x ∈ V) : comap (λ y : α, x) f = ⊤ :=
begin
ext W,
suffices : (∃ (t : set α), t ∈ f.sets ∧ (λ (y : α), x) ⁻¹' t ⊆ W) ↔ W = univ,
by simpa,
split,
{ rintros ⟨V, V_in, hW⟩,
simpa [preimage_const_of_mem (h V V_in), univ_subset_iff] using hW },
{ rintro rfl,
use univ,
simp [univ_mem_sets] },
end
lemma comap_comap_comp {m : γ → β} {n : β → α} : comap m (comap n f) = comap (n ∘ m) f :=
le_antisymm
(assume c ⟨b, hb, (h : preimage (n ∘ m) b ⊆ c)⟩, ⟨preimage n b, preimage_mem_comap hb, h⟩)
(assume c ⟨b, ⟨a, ha, (h₁ : preimage n a ⊆ b)⟩, (h₂ : preimage m b ⊆ c)⟩,
⟨a, ha, show preimage m (preimage n a) ⊆ c, from subset.trans (preimage_mono h₁) h₂⟩)
@[simp] theorem comap_principal {t : set β} : comap m (𝓟 t) = 𝓟 (m ⁻¹' t) :=
filter_eq $ set.ext $ assume s,
⟨assume ⟨u, (hu : t ⊆ u), (b : preimage m u ⊆ s)⟩, subset.trans (preimage_mono hu) b,
assume : preimage m t ⊆ s, ⟨t, subset.refl t, this⟩⟩
lemma map_le_iff_le_comap : map m f ≤ g ↔ f ≤ comap m g :=
⟨assume h s ⟨t, ht, hts⟩, mem_sets_of_superset (h ht) hts, assume h s ht, h ⟨_, ht, subset.refl _⟩⟩
lemma gc_map_comap (m : α → β) : galois_connection (map m) (comap m) :=
assume f g, map_le_iff_le_comap
lemma map_mono : monotone (map m) := (gc_map_comap m).monotone_l
lemma comap_mono : monotone (comap m) := (gc_map_comap m).monotone_u
@[simp] lemma map_bot : map m ⊥ = ⊥ := (gc_map_comap m).l_bot
@[simp] lemma map_sup : map m (f₁ ⊔ f₂) = map m f₁ ⊔ map m f₂ := (gc_map_comap m).l_sup
@[simp] lemma map_supr {f : ι → filter α} : map m (⨆i, f i) = (⨆i, map m (f i)) :=
(gc_map_comap m).l_supr
@[simp] lemma comap_top : comap m ⊤ = ⊤ := (gc_map_comap m).u_top
@[simp] lemma comap_inf : comap m (g₁ ⊓ g₂) = comap m g₁ ⊓ comap m g₂ := (gc_map_comap m).u_inf
@[simp] lemma comap_infi {f : ι → filter β} : comap m (⨅i, f i) = (⨅i, comap m (f i)) :=
(gc_map_comap m).u_infi
lemma le_comap_top (f : α → β) (l : filter α) : l ≤ comap f ⊤ :=
by rw [comap_top]; exact le_top
lemma map_comap_le : map m (comap m g) ≤ g := (gc_map_comap m).l_u_le _
lemma le_comap_map : f ≤ comap m (map m f) := (gc_map_comap m).le_u_l _
@[simp] lemma comap_bot : comap m ⊥ = ⊥ :=
bot_unique $ assume s _, ⟨∅, by simp only [mem_bot_sets], by simp only [empty_subset, preimage_empty]⟩
lemma comap_supr {ι} {f : ι → filter β} {m : α → β} :
comap m (supr f) = (⨆i, comap m (f i)) :=
le_antisymm
(assume s hs,
have ∀i, ∃t, t ∈ f i ∧ m ⁻¹' t ⊆ s, by simpa only [mem_comap_sets, exists_prop, mem_supr_sets] using mem_supr_sets.1 hs,
let ⟨t, ht⟩ := classical.axiom_of_choice this in
⟨⋃i, t i, mem_supr_sets.2 $ assume i, (f i).sets_of_superset (ht i).1 (subset_Union _ _),
begin
rw [preimage_Union, Union_subset_iff],
assume i,
exact (ht i).2
end⟩)
(supr_le $ assume i, comap_mono $ le_supr _ _)
lemma comap_Sup {s : set (filter β)} {m : α → β} : comap m (Sup s) = (⨆f∈s, comap m f) :=
by simp only [Sup_eq_supr, comap_supr, eq_self_iff_true]
lemma comap_sup : comap m (g₁ ⊔ g₂) = comap m g₁ ⊔ comap m g₂ :=
le_antisymm
(assume s ⟨⟨t₁, ht₁, hs₁⟩, ⟨t₂, ht₂, hs₂⟩⟩,
⟨t₁ ∪ t₂,
⟨g₁.sets_of_superset ht₁ (subset_union_left _ _), g₂.sets_of_superset ht₂ (subset_union_right _ _)⟩,
union_subset hs₁ hs₂⟩)
((@comap_mono _ _ m).le_map_sup _ _)
lemma map_comap {f : filter β} {m : α → β} (hf : range m ∈ f) : (f.comap m).map m = f :=
le_antisymm
map_comap_le
(assume t' ⟨t, ht, sub⟩, by filter_upwards [ht, hf]; rintros x hxt ⟨y, rfl⟩; exact sub hxt)
lemma comap_map {f : filter α} {m : α → β} (h : ∀ x y, m x = m y → x = y) :
comap m (map m f) = f :=
have ∀s, preimage m (image m s) = s,
from assume s, preimage_image_eq s h,
le_antisymm
(assume s hs, ⟨
image m s,
f.sets_of_superset hs $ by simp only [this, subset.refl],
by simp only [this, subset.refl]⟩)
le_comap_map
lemma le_of_map_le_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f ≤ map m g) : f ≤ g :=
assume t ht, by filter_upwards [hsf, h $ image_mem_map (inter_mem_sets hsg ht)]
assume a has ⟨b, ⟨hbs, hb⟩, h⟩,
have b = a, from hm _ hbs _ has h,
this ▸ hb
lemma le_of_map_le_map_inj_iff {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y) :
map m f ≤ map m g ↔ f ≤ g :=
iff.intro (le_of_map_le_map_inj' hsf hsg hm) (λ h, map_mono h)
lemma eq_of_map_eq_map_inj' {f g : filter α} {m : α → β} {s : set α}
(hsf : s ∈ f) (hsg : s ∈ g) (hm : ∀x∈s, ∀y∈s, m x = m y → x = y)
(h : map m f = map m g) : f = g :=
le_antisymm
(le_of_map_le_map_inj' hsf hsg hm $ le_of_eq h)
(le_of_map_le_map_inj' hsg hsf hm $ le_of_eq h.symm)
lemma map_inj {f g : filter α} {m : α → β} (hm : ∀ x y, m x = m y → x = y) (h : map m f = map m g) :
f = g :=
have comap m (map m f) = comap m (map m g), by rw h,
by rwa [comap_map hm, comap_map hm] at this
theorem le_map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l)
(hf : ∀ y ∈ u, ∃ x, f x = y) :
l ≤ map f (comap f l) :=
assume s ⟨t, tl, ht⟩,
have t ∩ u ⊆ s, from
assume x ⟨xt, xu⟩,
exists.elim (hf x xu) $ λ a faeq,
by { rw ←faeq, apply ht, change f a ∈ t, rw faeq, exact xt },
mem_sets_of_superset (inter_mem_sets tl ul) this
theorem map_comap_of_surjective' {f : α → β} {l : filter β} {u : set β} (ul : u ∈ l)
(hf : ∀ y ∈ u, ∃ x, f x = y) :
map f (comap f l) = l :=
le_antisymm map_comap_le (le_map_comap_of_surjective' ul hf)
theorem le_map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) :
l ≤ map f (comap f l) :=
le_map_comap_of_surjective' univ_mem_sets (λ y _, hf y)
theorem map_comap_of_surjective {f : α → β} (hf : function.surjective f) (l : filter β) :
map f (comap f l) = l :=
le_antisymm map_comap_le (le_map_comap_of_surjective hf l)
lemma subtype_coe_map_comap (s : set α) (f : filter α) :
map (coe : s → α) (comap (coe : s → α) f) = f ⊓ 𝓟 s :=
begin
apply le_antisymm,
{ rw [map_le_iff_le_comap, comap_inf, comap_principal],
have : (coe : s → α) ⁻¹' s = univ, by { ext x, simp },
rw [this, principal_univ],
simp [le_refl _] },
{ intros V V_in,
rcases V_in with ⟨W, W_in, H⟩,
rw mem_inf_sets,
use [W, W_in, s, mem_principal_self s],
erw [← image_subset_iff, subtype.image_preimage_coe] at H,
exact H }
end
lemma subtype_coe_map_comap_prod (s : set α) (f : filter (α × α)) :
map (coe : s × s → α × α) (comap (coe : s × s → α × α) f) = f ⊓ 𝓟 (s.prod s) :=
let φ (x : s × s) : s.prod s := ⟨⟨x.1.1, x.2.1⟩, ⟨x.1.2, x.2.2⟩⟩ in
begin
rw show (coe : s × s → α × α) = coe ∘ φ, by ext x; cases x; refl,
rw [← filter.map_map, ← filter.comap_comap_comp],
rw map_comap_of_surjective,
exact subtype_coe_map_comap _ _,
exact λ ⟨⟨a, b⟩, ⟨ha, hb⟩⟩, ⟨⟨⟨a, ha⟩, ⟨b, hb⟩⟩, rfl⟩
end
lemma comap_ne_bot {f : filter β} {m : α → β} (hm : ∀t∈ f, ∃a, m a ∈ t) :
comap m f ≠ ⊥ :=
forall_sets_nonempty_iff_ne_bot.mp $ assume s ⟨t, ht, t_s⟩,
set.nonempty.mono t_s (hm t ht)
lemma comap_ne_bot_of_range_mem {f : filter β} {m : α → β}
(hf : f ≠ ⊥) (hm : range m ∈ f) : comap m f ≠ ⊥ :=
comap_ne_bot $ assume t ht,
let ⟨_, ha, a, rfl⟩ := nonempty_of_mem_sets hf (inter_mem_sets ht hm)
in ⟨a, ha⟩
lemma comap_inf_principal_ne_bot_of_image_mem {f : filter β} {m : α → β}
(hf : f ≠ ⊥) {s : set α} (hs : m '' s ∈ f) : (comap m f ⊓ 𝓟 s) ≠ ⊥ :=
begin
refine compl_compl s ▸ mt mem_sets_of_eq_bot _,
rintros ⟨t, ht, hts⟩,
rcases nonempty_of_mem_sets hf (inter_mem_sets hs ht) with ⟨_, ⟨x, hxs, rfl⟩, hxt⟩,
exact absurd hxs (hts hxt)
end
lemma comap_ne_bot_of_surj {f : filter β} {m : α → β}
(hf : f ≠ ⊥) (hm : function.surjective m) : comap m f ≠ ⊥ :=
comap_ne_bot_of_range_mem hf $ univ_mem_sets' hm
lemma comap_ne_bot_of_image_mem {f : filter β} {m : α → β} (hf : f ≠ ⊥)
{s : set α} (hs : m '' s ∈ f) : comap m f ≠ ⊥ :=
ne_bot_of_le_ne_bot (comap_inf_principal_ne_bot_of_image_mem hf hs) inf_le_left
@[simp] lemma map_eq_bot_iff : map m f = ⊥ ↔ f = ⊥ :=
⟨by rw [←empty_in_sets_eq_bot, ←empty_in_sets_eq_bot]; exact id,
assume h, by simp only [h, eq_self_iff_true, map_bot]⟩
lemma map_ne_bot (hf : f ≠ ⊥) : map m f ≠ ⊥ :=
assume h, hf $ by rwa [map_eq_bot_iff] at h
lemma map_ne_bot_iff (f : α → β) {F : filter α} : map f F ≠ ⊥ ↔ F ≠ ⊥ :=
by rw [not_iff_not, map_eq_bot_iff]
lemma sInter_comap_sets (f : α → β) (F : filter β) :
⋂₀(comap f F).sets = ⋂ U ∈ F, f ⁻¹' U :=
begin
ext x,
suffices : (∀ (A : set α) (B : set β), B ∈ F → f ⁻¹' B ⊆ A → x ∈ A) ↔
∀ (B : set β), B ∈ F → f x ∈ B,
by simp only [mem_sInter, mem_Inter, mem_comap_sets, this, and_imp, mem_comap_sets, exists_prop, mem_sInter,
iff_self, mem_Inter, mem_preimage, exists_imp_distrib],
split,
{ intros h U U_in,
simpa only [set.subset.refl, forall_prop_of_true, mem_preimage] using h (f ⁻¹' U) U U_in },
{ intros h V U U_in f_U_V,
exact f_U_V (h U U_in) },
end
end map
-- this is a generic rule for monotone functions:
lemma map_infi_le {f : ι → filter α} {m : α → β} :
map m (infi f) ≤ (⨅ i, map m (f i)) :=
le_infi $ assume i, map_mono $ infi_le _ _
lemma map_infi_eq {f : ι → filter α} {m : α → β} (hf : directed (≥) f) (hι : nonempty ι) :
map m (infi f) = (⨅ i, map m (f i)) :=
le_antisymm
map_infi_le
(assume s (hs : preimage m s ∈ infi f),
have ∃i, preimage m s ∈ f i,
by simp only [infi_sets_eq hf hι, mem_Union] at hs; assumption,
let ⟨i, hi⟩ := this in
have (⨅ i, map m (f i)) ≤ 𝓟 s, from
infi_le_of_le i $ by simp only [le_principal_iff, mem_map]; assumption,
by simp only [filter.le_principal_iff] at this; assumption)
lemma map_binfi_eq {ι : Type w} {f : ι → filter α} {m : α → β} {p : ι → Prop}
(h : directed_on (f ⁻¹'o (≥)) {x | p x}) (ne : ∃i, p i) :
map m (⨅i (h : p i), f i) = (⨅i (h: p i), map m (f i)) :=
let ⟨i, hi⟩ := ne in
calc map m (⨅i (h : p i), f i) = map m (⨅i:subtype p, f i.val) : by simp only [infi_subtype, eq_self_iff_true]
... = (⨅i:subtype p, map m (f i.val)) : map_infi_eq
(assume ⟨x, hx⟩ ⟨y, hy⟩, match h x hx y hy with ⟨z, h₁, h₂, h₃⟩ := ⟨⟨z, h₁⟩, h₂, h₃⟩ end)
⟨⟨i, hi⟩⟩
... = (⨅i (h : p i), map m (f i)) : by simp only [infi_subtype, eq_self_iff_true]
lemma map_inf_le {f g : filter α} {m : α → β} : map m (f ⊓ g) ≤ map m f ⊓ map m g :=
(@map_mono _ _ m).map_inf_le f g
lemma map_inf' {f g : filter α} {m : α → β} {t : set α} (htf : t ∈ f) (htg : t ∈ g)
(h : ∀x∈t, ∀y∈t, m x = m y → x = y) : map m (f ⊓ g) = map m f ⊓ map m g :=
begin
refine le_antisymm map_inf_le (assume s hs, _),
simp only [map, mem_inf_sets, exists_prop, mem_map, mem_preimage, mem_inf_sets] at hs ⊢,
rcases hs with ⟨t₁, h₁, t₂, h₂, hs⟩,
refine ⟨m '' (t₁ ∩ t), _, m '' (t₂ ∩ t), _, _⟩,
{ filter_upwards [h₁, htf] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ filter_upwards [h₂, htg] assume a h₁ h₂, mem_image_of_mem _ ⟨h₁, h₂⟩ },
{ rw [image_inter_on],
{ refine image_subset_iff.2 _,
exact λ x ⟨⟨h₁, _⟩, h₂, _⟩, hs ⟨h₁, h₂⟩ },
{ exact λ x ⟨_, hx⟩ y ⟨_, hy⟩, h x hx y hy } }
end
lemma map_inf {f g : filter α} {m : α → β} (h : function.injective m) :
map m (f ⊓ g) = map m f ⊓ map m g :=
map_inf' univ_mem_sets univ_mem_sets (assume x _ y _ hxy, h hxy)
lemma map_eq_comap_of_inverse {f : filter α} {m : α → β} {n : β → α}
(h₁ : m ∘ n = id) (h₂ : n ∘ m = id) : map m f = comap n f :=
le_antisymm
(assume b ⟨a, ha, (h : preimage n a ⊆ b)⟩, f.sets_of_superset ha $
calc a = preimage (n ∘ m) a : by simp only [h₂, preimage_id, eq_self_iff_true]
... ⊆ preimage m b : preimage_mono h)
(assume b (hb : preimage m b ∈ f),
⟨preimage m b, hb, show preimage (m ∘ n) b ⊆ b, by simp only [h₁]; apply subset.refl⟩)
lemma map_swap_eq_comap_swap {f : filter (α × β)} : prod.swap <$> f = comap prod.swap f :=
map_eq_comap_of_inverse prod.swap_swap_eq prod.swap_swap_eq
lemma le_map {f : filter α} {m : α → β} {g : filter β} (h : ∀s∈ f, m '' s ∈ g) :
g ≤ f.map m :=
assume s hs, mem_sets_of_superset (h _ hs) $ image_preimage_subset _ _
protected lemma push_pull (f : α → β) (F : filter α) (G : filter β) :
map f (F ⊓ comap f G) = map f F ⊓ G :=
begin
apply le_antisymm,
{ calc map f (F ⊓ comap f G) ≤ map f F ⊓ (map f $ comap f G) : map_inf_le
... ≤ map f F ⊓ G : inf_le_inf_left (map f F) map_comap_le },
{ rintros U ⟨V, V_in, W, ⟨Z, Z_in, hZ⟩, h⟩,
rw ← image_subset_iff at h,
use [f '' V, image_mem_map V_in, Z, Z_in],
refine subset.trans _ h,
have : f '' (V ∩ f ⁻¹' Z) ⊆ f '' (V ∩ W),
from image_subset _ (inter_subset_inter_right _ ‹_›),
rwa set.push_pull at this }
end
protected lemma push_pull' (f : α → β) (F : filter α) (G : filter β) :
map f (comap f G ⊓ F) = G ⊓ map f F :=
by simp only [filter.push_pull, inf_comm]
section applicative
lemma singleton_mem_pure_sets {a : α} : {a} ∈ (pure a : filter α) :=
mem_singleton a
lemma pure_injective : function.injective (pure : α → filter α) :=
assume a b hab, (filter.ext_iff.1 hab {x | a = x}).1 rfl
@[simp] lemma pure_ne_bot {α : Type u} {a : α} : pure a ≠ (⊥ : filter α) :=
mt empty_in_sets_eq_bot.2 $ not_mem_empty a
@[simp] lemma le_pure_iff {f : filter α} {a : α} : f ≤ pure a ↔ {a} ∈ f :=
⟨λ h, h singleton_mem_pure_sets,
λ h s hs, mem_sets_of_superset h $ singleton_subset_iff.2 hs⟩
lemma mem_seq_sets_def {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, ∀x∈u, ∀y∈t, (x : α → β) y ∈ s) :=
iff.rfl
lemma mem_seq_sets_iff {f : filter (α → β)} {g : filter α} {s : set β} :
s ∈ f.seq g ↔ (∃u ∈ f, ∃t ∈ g, set.seq u t ⊆ s) :=
by simp only [mem_seq_sets_def, seq_subset, exists_prop, iff_self]
lemma mem_map_seq_iff {f : filter α} {g : filter β} {m : α → β → γ} {s : set γ} :
s ∈ (f.map m).seq g ↔ (∃t u, t ∈ g ∧ u ∈ f ∧ ∀x∈u, ∀y∈t, m x y ∈ s) :=
iff.intro
(assume ⟨t, ht, s, hs, hts⟩, ⟨s, m ⁻¹' t, hs, ht, assume a, hts _⟩)
(assume ⟨t, s, ht, hs, hts⟩, ⟨m '' s, image_mem_map hs, t, ht, assume f ⟨a, has, eq⟩, eq ▸ hts _ has⟩)
lemma seq_mem_seq_sets {f : filter (α → β)} {g : filter α} {s : set (α → β)} {t : set α}
(hs : s ∈ f) (ht : t ∈ g) : s.seq t ∈ f.seq g :=
⟨s, hs, t, ht, assume f hf a ha, ⟨f, hf, a, ha, rfl⟩⟩
lemma le_seq {f : filter (α → β)} {g : filter α} {h : filter β}
(hh : ∀t ∈ f, ∀u ∈ g, set.seq t u ∈ h) : h ≤ seq f g :=
assume s ⟨t, ht, u, hu, hs⟩, mem_sets_of_superset (hh _ ht _ hu) $
assume b ⟨m, hm, a, ha, eq⟩, eq ▸ hs _ hm _ ha
lemma seq_mono {f₁ f₂ : filter (α → β)} {g₁ g₂ : filter α}
(hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.seq g₁ ≤ f₂.seq g₂ :=
le_seq $ assume s hs t ht, seq_mem_seq_sets (hf hs) (hg ht)
@[simp] lemma pure_seq_eq_map (g : α → β) (f : filter α) : seq (pure g) f = f.map g :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← singleton_seq, apply seq_mem_seq_sets _ hs,
exact singleton_mem_pure_sets },
{ refine sets_of_superset (map g f) (image_mem_map ht) _,
rintros b ⟨a, ha, rfl⟩, exact ⟨g, hs, a, ha, rfl⟩ }
end
@[simp] lemma seq_pure (f : filter (α → β)) (a : α) : seq f (pure a) = map (λg:α → β, g a) f :=
begin
refine le_antisymm (le_map $ assume s hs, _) (le_seq $ assume s hs t ht, _),
{ rw ← seq_singleton,
exact seq_mem_seq_sets hs singleton_mem_pure_sets },
{ refine sets_of_superset (map (λg:α→β, g a) f) (image_mem_map hs) _,
rintros b ⟨g, hg, rfl⟩, exact ⟨g, hg, a, ht, rfl⟩ }
end
@[simp] lemma seq_assoc (x : filter α) (g : filter (α → β)) (h : filter (β → γ)) :
seq h (seq g x) = seq (seq (map (∘) h) g) x :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_seq_sets_iff.1 hs with ⟨u, hu, v, hv, hs⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hu⟩,
refine mem_sets_of_superset _
(set.seq_mono (subset.trans (set.seq_mono hu (subset.refl _)) hs) (subset.refl _)),
rw ← set.seq_seq,
exact seq_mem_seq_sets hw (seq_mem_seq_sets hv ht) },
{ rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
refine mem_sets_of_superset _ (set.seq_mono (subset.refl _) ht),
rw set.seq_seq,
exact seq_mem_seq_sets (seq_mem_seq_sets (image_mem_map hs) hu) hv }
end
lemma prod_map_seq_comm (f : filter α) (g : filter β) :
(map prod.mk f).seq g = seq (map (λb a, (a, b)) g) f :=
begin
refine le_antisymm (le_seq $ assume s hs t ht, _) (le_seq $ assume s hs t ht, _),
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw ← set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu },
{ rcases mem_map_sets_iff.1 hs with ⟨u, hu, hs⟩,
refine mem_sets_of_superset _ (set.seq_mono hs (subset.refl _)),
rw set.prod_image_seq_comm,
exact seq_mem_seq_sets (image_mem_map ht) hu }
end
instance : is_lawful_functor (filter : Type u → Type u) :=
{ id_map := assume α f, map_id,
comp_map := assume α β γ f g a, map_map.symm }
instance : is_lawful_applicative (filter : Type u → Type u) :=
{ pure_seq_eq_map := assume α β, pure_seq_eq_map,
map_pure := assume α β, map_pure,
seq_pure := assume α β, seq_pure,
seq_assoc := assume α β γ, seq_assoc }
instance : is_comm_applicative (filter : Type u → Type u) :=
⟨assume α β f g, prod_map_seq_comm f g⟩
lemma {l} seq_eq_filter_seq {α β : Type l} (f : filter (α → β)) (g : filter α) :
f <*> g = seq f g := rfl
end applicative
/- bind equations -/
section bind
@[simp] lemma mem_bind_sets {s : set β} {f : filter α} {m : α → filter β} :
s ∈ bind f m ↔ ∃t ∈ f, ∀x ∈ t, s ∈ m x :=
calc s ∈ bind f m ↔ {a | s ∈ m a} ∈ f : by simp only [bind, mem_map, iff_self, mem_join_sets, mem_set_of_eq]
... ↔ (∃t ∈ f, t ⊆ {a | s ∈ m a}) : exists_sets_subset_iff.symm
... ↔ (∃t ∈ f, ∀x ∈ t, s ∈ m x) : iff.rfl
lemma bind_mono {f : filter α} {g h : α → filter β} (h₁ : {a | g a ≤ h a} ∈ f) :
bind f g ≤ bind f h :=
assume x h₂, show (_ ∈ f), by filter_upwards [h₁, h₂] assume s gh' h', gh' h'
lemma bind_sup {f g : filter α} {h : α → filter β} :
bind (f ⊔ g) h = bind f h ⊔ bind g h :=
by simp only [bind, sup_join, map_sup, eq_self_iff_true]
lemma bind_mono2 {f g : filter α} {h : α → filter β} (h₁ : f ≤ g) :
bind f h ≤ bind g h :=
assume s h', h₁ h'
lemma principal_bind {s : set α} {f : α → filter β} :
(bind (𝓟 s) f) = (⨆x ∈ s, f x) :=
show join (map f (𝓟 s)) = (⨆x ∈ s, f x),
by simp only [Sup_image, join_principal_eq_Sup, map_principal, eq_self_iff_true]
end bind
section list_traverse
/- This is a separate section in order to open `list`, but mostly because of universe
equality requirements in `traverse` -/
open list
lemma sequence_mono :
∀(as bs : list (filter α)), forall₂ (≤) as bs → sequence as ≤ sequence bs
| [] [] forall₂.nil := le_refl _
| (a::as) (b::bs) (forall₂.cons h hs) := seq_mono (map_mono h) (sequence_mono as bs hs)
variables {α' β' γ' : Type u} {f : β' → filter α'} {s : γ' → set α'}
lemma mem_traverse_sets :
∀(fs : list β') (us : list γ'),
forall₂ (λb c, s c ∈ f b) fs us → traverse s us ∈ traverse f fs
| [] [] forall₂.nil := mem_pure_sets.2 $ mem_singleton _
| (f::fs) (u::us) (forall₂.cons h hs) := seq_mem_seq_sets (image_mem_map h) (mem_traverse_sets fs us hs)
lemma mem_traverse_sets_iff (fs : list β') (t : set (list α')) :
t ∈ traverse f fs ↔
(∃us:list (set α'), forall₂ (λb (s : set α'), s ∈ f b) fs us ∧ sequence us ⊆ t) :=
begin
split,
{ induction fs generalizing t,
case nil { simp only [sequence, mem_pure_sets, imp_self, forall₂_nil_left_iff,
exists_eq_left, set.pure_def, singleton_subset_iff, traverse_nil] },
case cons : b fs ih t {
assume ht,
rcases mem_seq_sets_iff.1 ht with ⟨u, hu, v, hv, ht⟩,
rcases mem_map_sets_iff.1 hu with ⟨w, hw, hwu⟩,
rcases ih v hv with ⟨us, hus, hu⟩,
exact ⟨w :: us, forall₂.cons hw hus, subset.trans (set.seq_mono hwu hu) ht⟩ } },
{ rintros ⟨us, hus, hs⟩,
exact mem_sets_of_superset (mem_traverse_sets _ _ hus) hs }
end
end list_traverse
/-! ### Limits -/
/-- `tendsto` is the generic "limit of a function" predicate.
`tendsto f l₁ l₂` asserts that for every `l₂` neighborhood `a`,
the `f`-preimage of `a` is an `l₁` neighborhood. -/
def tendsto (f : α → β) (l₁ : filter α) (l₂ : filter β) := l₁.map f ≤ l₂
lemma tendsto_def {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ ∀ s ∈ l₂, f ⁻¹' s ∈ l₁ := iff.rfl
lemma tendsto.eventually {f : α → β} {l₁ : filter α} {l₂ : filter β} {p : β → Prop}
(hf : tendsto f l₁ l₂) (h : ∀ᶠ y in l₂, p y) :
∀ᶠ x in l₁, p (f x) :=
hf h
lemma eventually_eq_of_left_inv_of_right_inv {f : α → β} {g₁ g₂ : β → α} {fa : filter α}
{fb : filter β} (hleft : ∀ᶠ x in fa, g₁ (f x) = x) (hright : ∀ᶠ y in fb, f (g₂ y) = y)
(htendsto : tendsto g₂ fb fa) :
g₁ =ᶠ[fb] g₂ :=
(htendsto.eventually hleft).mp $ hright.mono $ λ y hr hl, (congr_arg g₁ hr.symm).trans hl
lemma tendsto_iff_comap {f : α → β} {l₁ : filter α} {l₂ : filter β} :
tendsto f l₁ l₂ ↔ l₁ ≤ l₂.comap f :=
map_le_iff_le_comap
lemma tendsto_congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(hl : f₁ =ᶠ[l₁] f₂) :
tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ :=
by rw [tendsto, tendsto, map_congr hl]
lemma tendsto.congr' {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(hl : f₁ =ᶠ[l₁] f₂) (h : tendsto f₁ l₁ l₂) : tendsto f₂ l₁ l₂ :=
(tendsto_congr' hl).1 h
theorem tendsto_congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ ↔ tendsto f₂ l₁ l₂ :=
tendsto_congr' (univ_mem_sets' h)
theorem tendsto.congr {f₁ f₂ : α → β} {l₁ : filter α} {l₂ : filter β}
(h : ∀ x, f₁ x = f₂ x) : tendsto f₁ l₁ l₂ → tendsto f₂ l₁ l₂ :=
(tendsto_congr h).1
lemma tendsto_id' {x y : filter α} : x ≤ y → tendsto id x y :=
by simp only [tendsto, map_id, forall_true_iff] {contextual := tt}
lemma tendsto_id {x : filter α} : tendsto id x x := tendsto_id' $ le_refl x
lemma tendsto.comp {f : α → β} {g : β → γ} {x : filter α} {y : filter β} {z : filter γ}
(hg : tendsto g y z) (hf : tendsto f x y) : tendsto (g ∘ f) x z :=
calc map (g ∘ f) x = map g (map f x) : by rw [map_map]
... ≤ map g y : map_mono hf
... ≤ z : hg
lemma tendsto_le_left {f : α → β} {x y : filter α} {z : filter β}
(h : y ≤ x) : tendsto f x z → tendsto f y z :=
le_trans (map_mono h)
lemma tendsto_le_right {f : α → β} {x : filter α} {y z : filter β}
(h₁ : y ≤ z) (h₂ : tendsto f x y) : tendsto f x z :=
le_trans h₂ h₁
lemma tendsto.ne_bot {f : α → β} {x : filter α} {y : filter β} (h : tendsto f x y) (hx : x ≠ ⊥) :
y ≠ ⊥ :=
ne_bot_of_le_ne_bot (map_ne_bot hx) h
lemma tendsto_map {f : α → β} {x : filter α} : tendsto f x (map f x) := le_refl (map f x)
lemma tendsto_map' {f : β → γ} {g : α → β} {x : filter α} {y : filter γ}
(h : tendsto (f ∘ g) x y) : tendsto f (map g x) y :=
by rwa [tendsto, map_map]
lemma tendsto_map'_iff {f : β → γ} {g : α → β} {x : filter α} {y : filter γ} :
tendsto f (map g x) y ↔ tendsto (f ∘ g) x y :=
by rw [tendsto, map_map]; refl
lemma tendsto_comap {f : α → β} {x : filter β} : tendsto f (comap f x) x :=
map_comap_le
lemma tendsto_comap_iff {f : α → β} {g : β → γ} {a : filter α} {c : filter γ} :
tendsto f a (c.comap g) ↔ tendsto (g ∘ f) a c :=
⟨assume h, tendsto_comap.comp h, assume h, map_le_iff_le_comap.mp $ by rwa [map_map]⟩
lemma tendsto_comap'_iff {m : α → β} {f : filter α} {g : filter β} {i : γ → α}
(h : range i ∈ f) : tendsto (m ∘ i) (comap i f) g ↔ tendsto m f g :=
by rw [tendsto, ← map_compose]; simp only [(∘), map_comap h, tendsto]
lemma comap_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : ψ ∘ φ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : comap φ g = f :=
begin
refine le_antisymm (le_trans (comap_mono $ map_le_iff_le_comap.1 hψ) _) (map_le_iff_le_comap.1 hφ),
rw [comap_comap_comp, eq, comap_id],
exact le_refl _
end
lemma map_eq_of_inverse {f : filter α} {g : filter β} {φ : α → β} (ψ : β → α)
(eq : φ ∘ ψ = id) (hφ : tendsto φ f g) (hψ : tendsto ψ g f) : map φ f = g :=
begin
refine le_antisymm hφ (le_trans _ (map_mono hψ)),
rw [map_map, eq, map_id],
exact le_refl _
end
lemma tendsto_inf {f : α → β} {x : filter α} {y₁ y₂ : filter β} :
tendsto f x (y₁ ⊓ y₂) ↔ tendsto f x y₁ ∧ tendsto f x y₂ :=
by simp only [tendsto, le_inf_iff, iff_self]
lemma tendsto_inf_left {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₁ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_left) h
lemma tendsto_inf_right {f : α → β} {x₁ x₂ : filter α} {y : filter β}
(h : tendsto f x₂ y) : tendsto f (x₁ ⊓ x₂) y :=
le_trans (map_mono inf_le_right) h
lemma tendsto.inf {f : α → β} {x₁ x₂ : filter α} {y₁ y₂ : filter β}
(h₁ : tendsto f x₁ y₁) (h₂ : tendsto f x₂ y₂) : tendsto f (x₁ ⊓ x₂) (y₁ ⊓ y₂) :=
tendsto_inf.2 ⟨tendsto_inf_left h₁, tendsto_inf_right h₂⟩
lemma tendsto_infi {f : α → β} {x : filter α} {y : ι → filter β} :
tendsto f x (⨅i, y i) ↔ ∀i, tendsto f x (y i) :=
by simp only [tendsto, iff_self, le_infi_iff]
lemma tendsto_infi' {f : α → β} {x : ι → filter α} {y : filter β} (i : ι) :
tendsto f (x i) y → tendsto f (⨅i, x i) y :=
tendsto_le_left (infi_le _ _)
lemma tendsto_principal {f : α → β} {l : filter α} {s : set β} :
tendsto f l (𝓟 s) ↔ ∀ᶠ a in l, f a ∈ s :=
by simp only [tendsto, le_principal_iff, mem_map, iff_self, filter.eventually]
lemma tendsto_principal_principal {f : α → β} {s : set α} {t : set β} :
tendsto f (𝓟 s) (𝓟 t) ↔ ∀a∈s, f a ∈ t :=
by simp only [tendsto, image_subset_iff, le_principal_iff, map_principal, mem_principal_sets]; refl
lemma tendsto_pure {f : α → β} {a : filter α} {b : β} :
tendsto f a (pure b) ↔ ∀ᶠ x in a, f x = b :=
by simp only [tendsto, le_pure_iff, mem_map, mem_singleton_iff, filter.eventually]
lemma tendsto_pure_pure (f : α → β) (a : α) :
tendsto f (pure a) (pure (f a)) :=
tendsto_pure.2 rfl
lemma tendsto_const_pure {a : filter α} {b : β} : tendsto (λx, b) a (pure b) :=
tendsto_pure.2 $ univ_mem_sets' $ λ _, rfl
/-- If two filters are disjoint, then a function cannot tend to both of them along a non-trivial
filter. -/
lemma tendsto.not_tendsto {f : α → β} {a : filter α} {b₁ b₂ : filter β} (hf : tendsto f a b₁)
(ha : a ≠ ⊥) (hb : disjoint b₁ b₂) :
¬ tendsto f a b₂ :=
λ hf', (tendsto_inf.2 ⟨hf, hf'⟩).ne_bot ha hb.eq_bot
lemma tendsto_if {l₁ : filter α} {l₂ : filter β}
{f g : α → β} {p : α → Prop} [decidable_pred p]
(h₀ : tendsto f (l₁ ⊓ 𝓟 p) l₂)
(h₁ : tendsto g (l₁ ⊓ 𝓟 { x | ¬ p x }) l₂) :
tendsto (λ x, if p x then f x else g x) l₁ l₂ :=
begin
revert h₀ h₁, simp only [tendsto_def, mem_inf_principal],
intros h₀ h₁ s hs,
apply mem_sets_of_superset (inter_mem_sets (h₀ s hs) (h₁ s hs)),
rintros x ⟨hp₀, hp₁⟩, simp only [mem_preimage],
by_cases h : p x,
{ rw if_pos h, exact hp₀ h },
rw if_neg h, exact hp₁ h
end
/-! ### Products of filters -/
section prod
variables {s : set α} {t : set β} {f : filter α} {g : filter β}
/- The product filter cannot be defined using the monad structure on filters. For example:
F := do {x ← seq, y ← top, return (x, y)}
hence:
s ∈ F ↔ ∃n, [n..∞] × univ ⊆ s
G := do {y ← top, x ← seq, return (x, y)}
hence:
s ∈ G ↔ ∀i:ℕ, ∃n, [n..∞] × {i} ⊆ s
Now ⋃i, [i..∞] × {i} is in G but not in F.
As product filter we want to have F as result.
-/
/-- Product of filters. This is the filter generated by cartesian products
of elements of the component filters. -/
protected def prod (f : filter α) (g : filter β) : filter (α × β) :=
f.comap prod.fst ⊓ g.comap prod.snd
localized "infix ` ×ᶠ `:60 := filter.prod" in filter
lemma prod_mem_prod {s : set α} {t : set β} {f : filter α} {g : filter β}
(hs : s ∈ f) (ht : t ∈ g) : set.prod s t ∈ f ×ᶠ g :=
inter_mem_inf_sets (preimage_mem_comap hs) (preimage_mem_comap ht)
lemma mem_prod_iff {s : set (α×β)} {f : filter α} {g : filter β} :
s ∈ f ×ᶠ g ↔ (∃ t₁ ∈ f, ∃ t₂ ∈ g, set.prod t₁ t₂ ⊆ s) :=
begin
simp only [filter.prod],
split,
exact assume ⟨t₁, ⟨s₁, hs₁, hts₁⟩, t₂, ⟨s₂, hs₂, hts₂⟩, h⟩,
⟨s₁, hs₁, s₂, hs₂, subset.trans (inter_subset_inter hts₁ hts₂) h⟩,
exact assume ⟨t₁, ht₁, t₂, ht₂, h⟩,
⟨prod.fst ⁻¹' t₁, ⟨t₁, ht₁, subset.refl _⟩, prod.snd ⁻¹' t₂, ⟨t₂, ht₂, subset.refl _⟩, h⟩
end
lemma comap_prod (f : α → β × γ) (b : filter β) (c : filter γ) :
comap f (b ×ᶠ c) = (comap (prod.fst ∘ f) b) ⊓ (comap (prod.snd ∘ f) c) :=
by erw [comap_inf, filter.comap_comap_comp, filter.comap_comap_comp]
lemma eventually_prod_iff {p : α × β → Prop} {f : filter α} {g : filter β} :
(∀ᶠ x in f ×ᶠ g, p x) ↔ ∃ (pa : α → Prop) (ha : ∀ᶠ x in f, pa x)
(pb : β → Prop) (hb : ∀ᶠ y in g, pb y), ∀ {x}, pa x → ∀ {y}, pb y → p (x, y) :=
by simpa only [set.prod_subset_iff] using @mem_prod_iff α β p f g
lemma tendsto_fst {f : filter α} {g : filter β} : tendsto prod.fst (f ×ᶠ g) f :=
tendsto_inf_left tendsto_comap
lemma tendsto_snd {f : filter α} {g : filter β} : tendsto prod.snd (f ×ᶠ g) g :=
tendsto_inf_right tendsto_comap
lemma tendsto.prod_mk {f : filter α} {g : filter β} {h : filter γ} {m₁ : α → β} {m₂ : α → γ}
(h₁ : tendsto m₁ f g) (h₂ : tendsto m₂ f h) : tendsto (λx, (m₁ x, m₂ x)) f (g ×ᶠ h) :=
tendsto_inf.2 ⟨tendsto_comap_iff.2 h₁, tendsto_comap_iff.2 h₂⟩
lemma eventually.prod_inl {la : filter α} {p : α → Prop} (h : ∀ᶠ x in la, p x) (lb : filter β) :
∀ᶠ x in la ×ᶠ lb, p (x : α × β).1 :=
tendsto_fst.eventually h
lemma eventually.prod_inr {lb : filter β} {p : β → Prop} (h : ∀ᶠ x in lb, p x) (la : filter α) :
∀ᶠ x in la ×ᶠ lb, p (x : α × β).2 :=
tendsto_snd.eventually h
lemma eventually.prod_mk {la : filter α} {pa : α → Prop} (ha : ∀ᶠ x in la, pa x)
{lb : filter β} {pb : β → Prop} (hb : ∀ᶠ y in lb, pb y) :
∀ᶠ p in la ×ᶠ lb, pa (p : α × β).1 ∧ pb p.2 :=
(ha.prod_inl lb).and (hb.prod_inr la)
lemma eventually.curry {la : filter α} {lb : filter β} {p : α × β → Prop}
(h : ∀ᶠ x in la.prod lb, p x) :
∀ᶠ x in la, ∀ᶠ y in lb, p (x, y) :=
begin
rcases eventually_prod_iff.1 h with ⟨pa, ha, pb, hb, h⟩,
exact ha.mono (λ a ha, hb.mono $ λ b hb, h ha hb)
end
lemma prod_infi_left {f : ι → filter α} {g : filter β} (i : ι) :
(⨅i, f i) ×ᶠ g = (⨅i, (f i) ×ᶠ g) :=
by rw [filter.prod, comap_infi, infi_inf i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_infi_right {f : filter α} {g : ι → filter β} (i : ι) :
f ×ᶠ (⨅i, g i) = (⨅i, f ×ᶠ (g i)) :=
by rw [filter.prod, comap_infi, inf_infi i]; simp only [filter.prod, eq_self_iff_true]
lemma prod_mono {f₁ f₂ : filter α} {g₁ g₂ : filter β} (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) :
f₁ ×ᶠ g₁ ≤ f₂ ×ᶠ g₂ :=
inf_le_inf (comap_mono hf) (comap_mono hg)
lemma prod_comap_comap_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : β₁ → α₁} {m₂ : β₂ → α₂} :
(comap m₁ f₁) ×ᶠ (comap m₂ f₂) = comap (λp:β₁×β₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) :=
by simp only [filter.prod, comap_comap_comp, eq_self_iff_true, comap_inf]
lemma prod_comm' : f ×ᶠ g = comap (prod.swap) (g ×ᶠ f) :=
by simp only [filter.prod, comap_comap_comp, (∘), inf_comm, prod.fst_swap,
eq_self_iff_true, prod.snd_swap, comap_inf]
lemma prod_comm : f ×ᶠ g = map (λp:β×α, (p.2, p.1)) (g ×ᶠ f) :=
by rw [prod_comm', ← map_swap_eq_comap_swap]; refl
lemma prod_map_map_eq {α₁ : Type u} {α₂ : Type v} {β₁ : Type w} {β₂ : Type x}
{f₁ : filter α₁} {f₂ : filter α₂} {m₁ : α₁ → β₁} {m₂ : α₂ → β₂} :
(map m₁ f₁) ×ᶠ (map m₂ f₂) = map (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) (f₁ ×ᶠ f₂) :=
le_antisymm
(assume s hs,
let ⟨s₁, hs₁, s₂, hs₂, h⟩ := mem_prod_iff.mp hs in
filter.sets_of_superset _ (prod_mem_prod (image_mem_map hs₁) (image_mem_map hs₂)) $
calc set.prod (m₁ '' s₁) (m₂ '' s₂) = (λp:α₁×α₂, (m₁ p.1, m₂ p.2)) '' set.prod s₁ s₂ :
set.prod_image_image_eq
... ⊆ _ : by rwa [image_subset_iff])
((tendsto.comp (le_refl _) tendsto_fst).prod_mk (tendsto.comp (le_refl _) tendsto_snd))
lemma tendsto.prod_map {δ : Type*} {f : α → γ} {g : β → δ} {a : filter α} {b : filter β}
{c : filter γ} {d : filter δ} (hf : tendsto f a c) (hg : tendsto g b d) :
tendsto (prod.map f g) (a ×ᶠ b) (c ×ᶠ d) :=
begin
erw [tendsto, ← prod_map_map_eq],
exact filter.prod_mono hf hg,
end
lemma map_prod (m : α × β → γ) (f : filter α) (g : filter β) :
map m (f.prod g) = (f.map (λa b, m (a, b))).seq g :=
begin
simp [filter.ext_iff, mem_prod_iff, mem_map_seq_iff],
assume s,
split,
exact assume ⟨t, ht, s, hs, h⟩, ⟨s, hs, t, ht, assume x hx y hy, @h ⟨x, y⟩ ⟨hx, hy⟩⟩,
exact assume ⟨s, hs, t, ht, h⟩, ⟨t, ht, s, hs, assume ⟨x, y⟩ ⟨hx, hy⟩, h x hx y hy⟩
end
lemma prod_eq {f : filter α} {g : filter β} : f.prod g = (f.map prod.mk).seq g :=
have h : _ := map_prod id f g, by rwa [map_id] at h
lemma prod_inf_prod {f₁ f₂ : filter α} {g₁ g₂ : filter β} :
(f₁ ×ᶠ g₁) ⊓ (f₂ ×ᶠ g₂) = (f₁ ⊓ f₂) ×ᶠ (g₁ ⊓ g₂) :=
by simp only [filter.prod, comap_inf, inf_comm, inf_assoc, inf_left_comm]
@[simp] lemma prod_bot {f : filter α} : f ×ᶠ (⊥ : filter β) = ⊥ := by simp [filter.prod]
@[simp] lemma bot_prod {g : filter β} : (⊥ : filter α) ×ᶠ g = ⊥ := by simp [filter.prod]
@[simp] lemma prod_principal_principal {s : set α} {t : set β} :
(𝓟 s) ×ᶠ (𝓟 t) = 𝓟 (set.prod s t) :=
by simp only [filter.prod, comap_principal, principal_eq_iff_eq, comap_principal, inf_principal]; refl
@[simp] lemma prod_pure_pure {a : α} {b : β} : (pure a) ×ᶠ (pure b) = pure (a, b) :=
by simp [pure_eq_principal]
lemma prod_eq_bot {f : filter α} {g : filter β} : f ×ᶠ g = ⊥ ↔ (f = ⊥ ∨ g = ⊥) :=
begin
split,
{ assume h,
rcases mem_prod_iff.1 (empty_in_sets_eq_bot.2 h) with ⟨s, hs, t, ht, hst⟩,
rw [subset_empty_iff, set.prod_eq_empty_iff] at hst,
cases hst with s_eq t_eq,
{ left, exact empty_in_sets_eq_bot.1 (s_eq ▸ hs) },
{ right, exact empty_in_sets_eq_bot.1 (t_eq ▸ ht) } },
{ rintros (rfl | rfl),
exact bot_prod,
exact prod_bot }
end
lemma prod_ne_bot {f : filter α} {g : filter β} : f ×ᶠ g ≠ ⊥ ↔ (f ≠ ⊥ ∧ g ≠ ⊥) :=
by rw [(≠), prod_eq_bot, not_or_distrib]
lemma tendsto_prod_iff {f : α × β → γ} {x : filter α} {y : filter β} {z : filter γ} :
filter.tendsto f (x ×ᶠ y) z ↔
∀ W ∈ z, ∃ U ∈ x, ∃ V ∈ y, ∀ x y, x ∈ U → y ∈ V → f (x, y) ∈ W :=
by simp only [tendsto_def, mem_prod_iff, prod_sub_preimage_iff, exists_prop, iff_self]
end prod
end filter
|
b08124a9900e193e661eef8f408708a590690942 | 4b846d8dabdc64e7ea03552bad8f7fa74763fc67 | /library/tools/super/prover.lean | ccdc899bb87e45e1db04d3b7cd948aec75a990d0 | [
"Apache-2.0"
] | permissive | pacchiano/lean | 9324b33f3ac3b5c5647285160f9f6ea8d0d767dc | fdadada3a970377a6df8afcd629a6f2eab6e84e8 | refs/heads/master | 1,611,357,380,399 | 1,489,870,101,000 | 1,489,870,101,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,684 | lean | /-
Copyright (c) 2016 Gabriel Ebner. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Gabriel Ebner
-/
import .clause .prover_state
import .misc_preprocessing
import .selection
import .trim
-- default inferences
-- 0
import .clausifier
-- 10
import .demod
import .inhabited
import .datatypes
-- 20
import .subsumption
-- 30
import .splitting
-- 40
import .factoring
import .resolution
import .superposition
import .equality
import .simp
import .defs
open monad tactic expr
declare_trace super
namespace super
meta def trace_clauses : prover unit :=
do state ← state_t.read, trace state
meta def run_prover_loop
(literal_selection : selection_strategy)
(clause_selection : clause_selection_strategy)
(preprocessing_rules : list (prover unit))
(inference_rules : list inference)
: ℕ → prover (option expr) | i := do
sequence' preprocessing_rules,
new ← take_newly_derived, for' new register_as_passive,
when (is_trace_enabled_for `super) $ for' new $ λn,
tactic.trace { n with c := { (n^.c) with proof := const (mk_simple_name "derived") [] } },
needs_sat_run ← flip monad.lift state_t.read (λst, st^.needs_sat_run),
if needs_sat_run then do
res ← do_sat_run,
match res with
| some proof := return (some proof)
| none := do
model ← flip monad.lift state_t.read (λst, st^.current_model),
when (is_trace_enabled_for `super) (do
pp_model ← pp (model^.to_list^.map (λlit, if lit.2 = tt then lit.1 else ```(not %%lit.1))),
trace $ to_fmt "sat model: " ++ pp_model),
run_prover_loop i
end
else do
passive ← get_passive,
if rb_map.size passive = 0 then return none else do
given_name ← clause_selection i,
given ← option.to_monad (rb_map.find passive given_name),
-- trace_clauses,
remove_passive given_name,
given ← literal_selection given,
when (is_trace_enabled_for `super) (do
fmt ← pp given, trace (to_fmt "given: " ++ fmt)),
add_active given,
seq_inferences inference_rules given,
run_prover_loop (i+1)
meta def default_preprocessing : list (prover unit) :=
[
clausify_pre,
clause_normalize_pre,
factor_dup_lits_pre,
remove_duplicates_pre,
refl_r_pre,
diff_constr_eq_l_pre,
tautology_removal_pre,
subsumption_interreduction_pre,
forward_subsumption_pre,
return ()
]
end super
open super
meta def super (sos_lemmas : list expr) : tactic unit := with_trim $ do
as_refutation, local_false ← target,
clauses ← clauses_of_context,
sos_clauses ← monad.for sos_lemmas (clause.of_proof local_false),
initial_state ← prover_state.initial local_false (clauses ++ sos_clauses),
inf_names ← attribute.get_instances `super.inf,
infs ← for inf_names $ λn, eval_expr inf_decl (const n []),
infs ← return $ list.map inf_decl.inf $ list.sort_on inf_decl.prio infs,
res ← run_prover_loop selection21 (age_weight_clause_selection 3 4)
default_preprocessing infs
0 initial_state,
match res with
| (some empty_clause, st) := apply empty_clause
| (none, saturation) := do sat_fmt ← pp saturation,
fail $ to_fmt "saturation:" ++ format.line ++ sat_fmt
end
namespace tactic.interactive
open lean.parser
open interactive
open interactive.types
meta def with_lemmas (ls : parse $ many ident) : tactic unit := monad.for' ls $ λl, do
p ← mk_const l,
t ← infer_type p,
n ← get_unused_name p^.get_app_fn^.const_name none,
tactic.assertv n t p
meta def super (extra_clause_names : parse $ many ident)
(extra_lemma_names : parse with_ident_list) : tactic unit := do
with_lemmas extra_clause_names,
extra_lemmas ← monad.for extra_lemma_names mk_const,
_root_.super extra_lemmas
end tactic.interactive
|
e312578350d3fe8bb9513c6f0e10257d2b028701 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/analysis/convex/integral.lean | 4e86117765799dd401775fd4268284bcf2a1e00c | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 7,704 | lean | /-
Copyright (c) 2020 Yury G. Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury G. Kudryashov
-/
import analysis.convex.basic
import measure_theory.set_integral
/-!
# Jensen's inequality for integrals
In this file we prove four theorems:
* `convex.smul_integral_mem`: if `μ` is a non-zero finite measure on `α`, `s` is a convex closed set
in `E`, and `f` is an integrable function sending `μ`-a.e. points to `s`, then the average value
of `f` belongs to `s`: `(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s`. See also `convex.center_mass_mem`
for a finite sum version of this lemma.
* `convex.integral_mem`: if `μ` is a probability measure on `α`, `s` is a convex closed set in `E`,
and `f` is an integrable function sending `μ`-a.e. points to `s`, then the expected value of `f`
belongs to `s`: `∫ x, f x ∂μ ∈ s`. See also `convex.sum_mem` for a finite sum version of this
lemma.
* `convex_on.map_smul_integral_le`: Jensen's inequality: if a function `g : E → ℝ` is convex and
continuous on a convex closed set `s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is
a function sending `μ`-a.e. points to `s`, then the value of `g` at the average value of `f` is
less than or equal to the average value of `g ∘ f` provided that both `f` and `g ∘ f` are
integrable. See also `convex.map_center_mass_le` for a finite sum version of this lemma.
* `convex_on.map_integral_le`: Jensen's inequality: if a function `g : E → ℝ` is convex and
continuous on a convex closed set `s`, `μ` is a probability measure on `α`, and `f : α → E` is a
function sending `μ`-a.e. points to `s`, then the value of `g` at the expected value of `f` is
less than or equal to the expected value of `g ∘ f` provided that both `f` and `g ∘ f` are
integrable. See also `convex.map_sum_le` for a finite sum version of this lemma.
## Tags
convex, integral, center mass, Jensen's inequality
-/
open measure_theory set filter
open_locale topological_space big_operators
variables {α E : Type*} [measurable_space α] {μ : measure α}
[normed_group E] [normed_space ℝ E] [complete_space E]
[topological_space.second_countable_topology E] [measurable_space E] [borel_space E]
private lemma convex.smul_integral_mem_of_measurable
[finite_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s)
(hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) (hfm : measurable f) :
(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s :=
begin
unfreezingI { rcases eq_empty_or_nonempty s with rfl|⟨y₀, h₀⟩ },
{ refine (hμ _).elim, simpa using hfs },
rw ← hsc.closure_eq at hfs,
have hc : integrable (λ _, y₀) μ := integrable_const _,
set F : ℕ → simple_func α E := simple_func.approx_on f hfm s y₀ h₀,
have : tendsto (λ n, (F n).integral μ) at_top (𝓝 $ ∫ x, f x ∂μ),
{ simp only [simple_func.integral_eq_integral _
(simple_func.integrable_approx_on hfm hfi h₀ hc _)],
exact tendsto_integral_of_L1 _ hfi
(eventually_of_forall $ simple_func.integrable_approx_on hfm hfi h₀ hc)
(simple_func.tendsto_approx_on_L1_edist hfm h₀ hfs (hfi.sub hc).2) },
refine hsc.mem_of_tendsto (tendsto_const_nhds.smul this) (eventually_of_forall $ λ n, _),
have : ∑ y in (F n).range, (μ ((F n) ⁻¹' {y})).to_real = (μ univ).to_real,
by rw [← (F n).sum_range_measure_preimage_singleton, @ennreal.to_real_sum _ _
(λ y, μ ((F n) ⁻¹' {y})) (λ _ _, (measure_lt_top _ _))],
rw [← this, simple_func.integral],
refine hs.center_mass_mem (λ _ _, ennreal.to_real_nonneg) _ _,
{ rw [this, ennreal.to_real_pos_iff, pos_iff_ne_zero, ne.def, measure.measure_univ_eq_zero],
exact ⟨hμ, measure_ne_top _ _⟩ },
{ simp only [simple_func.mem_range],
rintros _ ⟨x, rfl⟩,
exact simple_func.approx_on_mem hfm h₀ n x }
end
/-- If `μ` is a non-zero finite measure on `α`, `s` is a convex closed set in `E`, and `f` is an
integrable function sending `μ`-a.e. points to `s`, then the average value of `f` belongs to `s`:
`(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s`. See also `convex.center_mass_mem` for a finite sum version
of this lemma. -/
lemma convex.smul_integral_mem
[finite_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s)
(hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) :
(μ univ).to_real⁻¹ • ∫ x, f x ∂μ ∈ s :=
begin
have : ∀ᵐ (x : α) ∂μ, hfi.ae_measurable.mk f x ∈ s,
{ filter_upwards [hfs, hfi.ae_measurable.ae_eq_mk],
assume a ha h,
rwa ← h },
convert convex.smul_integral_mem_of_measurable hs hsc hμ this
(hfi.congr hfi.ae_measurable.ae_eq_mk) (hfi.ae_measurable.measurable_mk) using 2,
apply integral_congr_ae,
exact hfi.ae_measurable.ae_eq_mk
end
/-- If `μ` is a probability measure on `α`, `s` is a convex closed set in `E`, and `f` is an
integrable function sending `μ`-a.e. points to `s`, then the expected value of `f` belongs to `s`:
`∫ x, f x ∂μ ∈ s`. See also `convex.sum_mem` for a finite sum version of this lemma. -/
lemma convex.integral_mem [probability_measure μ] {s : set E} (hs : convex s) (hsc : is_closed s)
{f : α → E} (hf : ∀ᵐ x ∂μ, f x ∈ s) (hfi : integrable f μ) :
∫ x, f x ∂μ ∈ s :=
by simpa [measure_univ] using hs.smul_integral_mem hsc (probability_measure.ne_zero μ) hf hfi
/-- Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set
`s`, `μ` is a finite non-zero measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points
to `s`, then the value of `g` at the average value of `f` is less than or equal to the average value
of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_center_mass_le`
for a finite sum version of this lemma. -/
lemma convex_on.map_smul_integral_le [finite_measure μ] {s : set E} {g : E → ℝ} (hg : convex_on s g)
(hgc : continuous_on g s) (hsc : is_closed s) (hμ : μ ≠ 0) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s)
(hfi : integrable f μ) (hgi : integrable (g ∘ f) μ) :
g ((μ univ).to_real⁻¹ • ∫ x, f x ∂μ) ≤ (μ univ).to_real⁻¹ • ∫ x, g (f x) ∂μ :=
begin
set t := {p : E × ℝ | p.1 ∈ s ∧ g p.1 ≤ p.2},
have ht_conv : convex t := hg.convex_epigraph,
have ht_closed : is_closed t :=
(hsc.preimage continuous_fst).is_closed_le (hgc.comp continuous_on_fst (subset.refl _))
continuous_on_snd,
have ht_mem : ∀ᵐ x ∂μ, (f x, g (f x)) ∈ t := hfs.mono (λ x hx, ⟨hx, le_rfl⟩),
simpa [integral_pair hfi hgi]
using (ht_conv.smul_integral_mem ht_closed hμ ht_mem (hfi.prod_mk hgi)).2
end
/-- Jensen's inequality: if a function `g : E → ℝ` is convex and continuous on a convex closed set
`s`, `μ` is a probability measure on `α`, and `f : α → E` is a function sending `μ`-a.e. points to
`s`, then the value of `g` at the expected value of `f` is less than or equal to the expected value
of `g ∘ f` provided that both `f` and `g ∘ f` are integrable. See also `convex.map_sum_le` for a
finite sum version of this lemma. -/
lemma convex_on.map_integral_le [probability_measure μ] {s : set E} {g : E → ℝ} (hg : convex_on s g)
(hgc : continuous_on g s) (hsc : is_closed s) {f : α → E} (hfs : ∀ᵐ x ∂μ, f x ∈ s)
(hfi : integrable f μ) (hgi : integrable (g ∘ f) μ) :
g (∫ x, f x ∂μ) ≤ ∫ x, g (f x) ∂μ :=
by simpa [measure_univ]
using hg.map_smul_integral_le hgc hsc (probability_measure.ne_zero μ) hfs hfi hgi
|
d80a34b01e4c7e721f5237ab02af67e7bec22112 | 618003631150032a5676f229d13a079ac875ff77 | /test/ring_exp.lean | ef75c3755dabfb4c1935edb83805bc6295bbc3c6 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 6,306 | lean | import tactic.ring_exp
import algebra.group_with_zero_power
universes u
section addition
/-!
### `addition` section
Test associativity and commutativity of `(+)`.
-/
example (a b : ℚ) : a = a := by ring_exp
example (a b : ℚ) : a + b = a + b := by ring_exp
example (a b : ℚ) : b + a = a + b := by ring_exp
example (a b : ℤ) : a + b + b = b + (a + b) := by ring_exp
example (a b c : ℕ) : a + b + b + (c + c) = c + (b + c) + (a + b) := by ring_exp
end addition
section numerals
/-!
### `numerals` section
Test that numerals behave like rational numbers.
-/
example (a : ℕ) : a + 5 + 5 = 0 + a + 10 := by ring_exp
example (a : ℤ) : a + 5 + 5 = 0 + a + 10 := by ring_exp
example (a : ℚ) : (1/2) * a + (1/2) * a = a := by ring_exp
end numerals
section multiplication
/-!
### `multiplication section`
Test that multiplication is associative and commutative.
Also test distributivity of `(+)` and `(*)`.
-/
example (a : ℕ) : 0 = a * 0 := by ring_exp_eq
example (a : ℕ) : a = a * 1 := by ring_exp
example (a : ℕ) : a + a = a * 2 := by ring_exp
example (a b : ℤ) : a * b = b * a := by ring_exp
example (a b : ℕ) : a * 4 * b + a = a * (4 * b + 1) := by ring_exp
end multiplication
section exponentiation
/-!
### `exponentiation` section
Test that exponentiation has the correct distributivity properties.
-/
example : 0 ^ 1 = 0 := by ring_exp
example : 0 ^ 2 = 0 := by ring_exp
example (a : ℕ) : a ^ 0 = 1 := by ring_exp
example (a : ℕ) : a ^ 1 = a := by ring_exp
example (a : ℕ) : a ^ 2 = a * a := by ring_exp
example (a b : ℕ) : a ^ b = a ^ b := by ring_exp
example (a b : ℕ) : a ^ (b + 1) = a * a ^ b := by ring_exp
example (n : ℕ) (a m : ℕ) : a * a^n * m = a^(n+1) * m := by ring_exp
example (n : ℕ) (a m : ℕ) : m * a^n * a = a^(n+1) * m := by ring_exp
example (n : ℕ) (a m : ℤ) : a * a^n * m = a^(n+1) * m := by ring_exp
example (n : ℕ) (m : ℤ) : 2 * 2^n * m = 2^(n+1) * m := by ring_exp
example (n : ℕ) (m : ℤ) : 2^(n+1) * m = 2 * 2^n * m := by ring_exp
example (n m : ℕ) (a : ℤ) : (a ^ n)^m = a^(n * m) := by ring_exp
example (n m : ℕ) (a : ℤ) : a^(n^0) = a^1 := by ring_exp
example (n : ℕ) : 0^(n + 1) = 0 := by ring_exp
example {α} [comm_ring α] (x : α) (k : ℕ) : x ^ (k + 2) = x * x * x^k := by ring_exp
example {α} [comm_ring α] (k : ℕ) (x y z : α) :
x * (z * (x - y)) + (x * (y * y ^ k) - y * (y * y ^ k)) = (z * x + y * y ^ k) * (x - y)
:= by ring_exp
-- We can represent a large exponent `n` more efficiently than just `n` multiplications:
example (a b : ℚ) : (a * b) ^ 1000000 = (b * a) ^ 1000000 := by ring_exp
end exponentiation
section power_of_sum
/-!
### `power_of_sum` section
Test that raising a sum to a power behaves like repeated multiplication,
if needed.
-/
example (a b : ℤ) : (a + b)^2 = a^2 + b^2 + a * b + b * a := by ring_exp
example (a b : ℤ) (n : ℕ) : (a + b)^(n + 2) = (a^2 + b^2 + a * b + b * a) * (a + b)^n := by ring_exp
end power_of_sum
section negation
/-!
### `negation` section
Test that negation and subtraction satisfy the expected properties,
also in semirings such as `ℕ`.
-/
example {α} [comm_ring α] (a : α) : a - a = 0 := by ring_exp_eq
example (a : ℤ) : a - a = 0 := by ring_exp
example (a : ℤ) : a + - a = 0 := by ring_exp
example (a : ℤ) : - a = (-1) * a := by ring_exp
example (a b : ℕ) : a - b + a + a = a - b + 2 * a := by ring_exp -- Here, (a - b) is treated as an atom.
example (n : ℕ) : n + 1 - 1 = n := by ring_exp! -- But we can force a bit of evaluation anyway.
end negation
constant f {α} : α → α
section complicated
/-!
### `complicated` section
Test that complicated, real-life expressions also get normalized.
-/
example {α : Type} [linear_ordered_field α] (x : α) :
2 * x + 1 * 1 - (2 * f (x + 1 / 2) + 2 * 1) + (1 * 1 - (2 * x - 2 * f (x + 1 / 2))) = 0
:= by ring_exp_eq
example {α : Type u} [linear_ordered_field α] (x : α) :
f (x + 1 / 2) ^ 1 * -2 + (f (x + 1 / 2) ^ 1 * 2 + 0) = 0
:= by ring_exp_eq
example (x y : ℕ) : x + id y = y + id x := by ring_exp!
-- Here, we check that `n - s` is not treated as `n + additive_inverse s`,
-- if `s` doesn't have an additive inverse.
example (B s n : ℕ) : B * (f s * ((n - s) * f (n - s - 1))) = B * (n - s) * (f s * f (n - s - 1)) :=
by ring_exp
-- This is a somewhat subtle case: `-c/b` is parsed as `(-c)/b`,
-- so we can't simply treat both sides of the division as atoms.
-- Instead, we follow the `ring` tactic in interpreting `-c / b` as `-c * b⁻¹`,
-- with only `b⁻¹` an atom.
example {α} [linear_ordered_field α] (a b c : α) : a*(-c/b)*(-c/b) = a*((c/b)*(c/b)) := by ring_exp
-- test that `field_simp` works fine with powers and `ring_exp`.
example (x y : ℚ) (n : ℕ) (hx : x ≠ 0) (hy : y ≠ 0) :
1/ (2/(x / y))^(2 * n) + y / y^(n+1) - (x/y)^n * (x/(2 * y))^n / 2 ^n = 1/y^n :=
begin
simp [sub_eq_add_neg],
field_simp [hx, hy],
ring_exp
end
end complicated
section conv
/-!
### `conv` section
Test that `ring_exp` works inside of `conv`, both with and without `!`.
-/
example (n : ℕ) : (2^n * 2 + 1)^10 = (2^(n+1) + 1)^10 :=
begin
conv_rhs
{ congr,
ring_exp, },
conv_lhs
{ congr,
ring_exp, },
end
example (x y : ℤ) : x + id y - y + id x = x * 2 := begin
conv_lhs { ring_exp!, },
end
end conv
section benchmark
/-!
### `benchmark` section
The `ring_exp` tactic shouldn't be too slow.
-/
-- This last example was copied from `data/polynomial.lean`, because it timed out.
-- After some optimization, it doesn't.
variables {α : Type} [comm_ring α]
def pow_sub_pow_factor (x y : α) : Π {i : ℕ},{z // x^i - y^i = z*(x - y)}
| 0 := ⟨0, by simp⟩
| 1 := ⟨1, by simp⟩
| (k+2) :=
begin
cases @pow_sub_pow_factor (k+1) with z hz,
existsi z*x + y^(k+1),
rw [_root_.pow_succ x, _root_.pow_succ y, ←sub_add_sub_cancel (x*x^(k+1)) (x*y^(k+1)),
←mul_sub x, hz],
ring_exp_eq
end
-- Another benchmark: bound the growth of the complexity somewhat.
example {α} [comm_semiring α] (x : α) : (x + 1) ^ 4 = (1 + x) ^ 4 := by try_for 5000 {ring_exp}
example {α} [comm_semiring α] (x : α) : (x + 1) ^ 6 = (1 + x) ^ 6 := by try_for 10000 {ring_exp}
example {α} [comm_semiring α] (x : α) : (x + 1) ^ 8 = (1 + x) ^ 8 := by try_for 15000 {ring_exp}
end benchmark
|
5bb16fb0bbcbf58712f4b406422b29e2accea97d | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/data/list/pairwise.lean | a2099352effb5f8c2eea117a8edb9f68a17297b6 | [
"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 | 17,377 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
import data.list.sublists
/-!
# Pairwise relations on a list
This file provides basic results about `list.pairwise` and `list.pw_filter` (definitions are in
`data.list.defs`).
`pairwise r [a 0, ..., a (n - 1)]` means `∀ i j, i < j → r (a i) (a j)`. For example,
`pairwise (≠) l` means that all elements of `l` are distinct, and `pairwise (<) l` means that `l`
is strictly increasing.
`pw_filter r l` is the list obtained by iteratively adding each element of `l` that doesn't break
the pairwiseness of the list we have so far. It thus yields `l'` a maximal sublist of `l` such that
`pairwise r l'`.
## Tags
sorted, nodup
-/
open nat function
namespace list
variables {α β : Type*} {R : α → α → Prop}
mk_iff_of_inductive_prop list.pairwise list.pairwise_iff
/-! ### Pairwise -/
theorem rel_of_pairwise_cons {a : α} {l : list α}
(p : pairwise R (a :: l)) : ∀ {a'}, a' ∈ l → R a a' :=
(pairwise_cons.1 p).1
theorem pairwise_of_pairwise_cons {a : α} {l : list α}
(p : pairwise R (a :: l)) : pairwise R l :=
(pairwise_cons.1 p).2
theorem pairwise.tail : ∀ {l : list α} (p : pairwise R l), pairwise R l.tail
| [] h := h
| (a :: l) h := pairwise_of_pairwise_cons h
theorem pairwise.imp_of_mem {S : α → α → Prop} {l : list α}
(H : ∀ {a b}, a ∈ l → b ∈ l → R a b → S a b) (p : pairwise R l) : pairwise S l :=
begin
induction p with a l r p IH generalizing H; constructor,
{ exact ball.imp_right
(λ x h, H (mem_cons_self _ _) (mem_cons_of_mem _ h)) r },
{ exact IH (λ a b m m', H
(mem_cons_of_mem _ m) (mem_cons_of_mem _ m')) }
end
theorem pairwise.imp {S : α → α → Prop}
(H : ∀ a b, R a b → S a b) {l : list α} : pairwise R l → pairwise S l :=
pairwise.imp_of_mem (λ a b _ _, H a b)
theorem pairwise.and {S : α → α → Prop} {l : list α} :
pairwise (λ a b, R a b ∧ S a b) l ↔ pairwise R l ∧ pairwise S l :=
⟨λ h, ⟨h.imp (λ a b h, h.1), h.imp (λ a b h, h.2)⟩,
λ ⟨hR, hS⟩, begin
clear_, induction hR with a l R1 R2 IH;
simp only [pairwise.nil, pairwise_cons] at *,
exact ⟨λ b bl, ⟨R1 b bl, hS.1 b bl⟩, IH hS.2⟩
end⟩
theorem pairwise.imp₂ {S : α → α → Prop} {T : α → α → Prop}
(H : ∀ a b, R a b → S a b → T a b) {l : list α}
(hR : pairwise R l) (hS : pairwise S l) : pairwise T l :=
(pairwise.and.2 ⟨hR, hS⟩).imp $ λ a b, and.rec (H a b)
theorem pairwise.iff_of_mem {S : α → α → Prop} {l : list α}
(H : ∀ {a b}, a ∈ l → b ∈ l → (R a b ↔ S a b)) : pairwise R l ↔ pairwise S l :=
⟨pairwise.imp_of_mem (λ a b m m', (H m m').1),
pairwise.imp_of_mem (λ a b m m', (H m m').2)⟩
theorem pairwise.iff {S : α → α → Prop}
(H : ∀ a b, R a b ↔ S a b) {l : list α} : pairwise R l ↔ pairwise S l :=
pairwise.iff_of_mem (λ a b _ _, H a b)
theorem pairwise_of_forall {l : list α} (H : ∀ x y, R x y) : pairwise R l :=
by induction l; [exact pairwise.nil,
simp only [*, pairwise_cons, forall_2_true_iff, and_true]]
theorem pairwise.and_mem {l : list α} :
pairwise R l ↔ pairwise (λ x y, x ∈ l ∧ y ∈ l ∧ R x y) l :=
pairwise.iff_of_mem (by simp only [true_and, iff_self, forall_2_true_iff] {contextual := tt})
theorem pairwise.imp_mem {l : list α} :
pairwise R l ↔ pairwise (λ x y, x ∈ l → y ∈ l → R x y) l :=
pairwise.iff_of_mem
(by simp only [forall_prop_of_true, iff_self, forall_2_true_iff] {contextual := tt})
theorem pairwise_of_sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → pairwise R l₂ → pairwise R l₁
| ._ ._ sublist.slnil h := h
| ._ ._ (sublist.cons l₁ l₂ a s) (pairwise.cons i n) := pairwise_of_sublist s n
| ._ ._ (sublist.cons2 l₁ l₂ a s) (pairwise.cons i n) :=
(pairwise_of_sublist s n).cons (ball.imp_left s.subset i)
theorem forall_of_forall_of_pairwise (H : symmetric R)
{l : list α} (H₁ : ∀ x ∈ l, R x x) (H₂ : pairwise R l) :
∀ (x ∈ l) (y ∈ l), R x y :=
begin
induction l with a l IH, { exact forall_mem_nil _ },
cases forall_mem_cons.1 H₁ with H₁₁ H₁₂,
cases pairwise_cons.1 H₂ with H₂₁ H₂₂,
rintro x (rfl | hx) y (rfl | hy),
exacts [H₁₁, H₂₁ _ hy, H (H₂₁ _ hx), IH H₁₂ H₂₂ _ hx _ hy]
end
lemma forall_of_pairwise (H : symmetric R) {l : list α}
(hl : pairwise R l) : (∀a∈l, ∀b∈l, a ≠ b → R a b) :=
forall_of_forall_of_pairwise
(λ a b h hne, H (h hne.symm))
(λ _ _ h, (h rfl).elim)
(pairwise.imp (λ _ _ h _, h) hl)
theorem pairwise_singleton (R) (a : α) : pairwise R [a] :=
by simp only [pairwise_cons, mem_singleton, forall_prop_of_false (not_mem_nil _), forall_true_iff,
pairwise.nil, and_true]
theorem pairwise_pair {a b : α} : pairwise R [a, b] ↔ R a b :=
by simp only [pairwise_cons, mem_singleton, forall_eq, forall_prop_of_false (not_mem_nil _),
forall_true_iff, pairwise.nil, and_true]
theorem pairwise_append {l₁ l₂ : list α} : pairwise R (l₁++l₂) ↔
pairwise R l₁ ∧ pairwise R l₂ ∧ ∀ x ∈ l₁, ∀ y ∈ l₂, R x y :=
by induction l₁ with x l₁ IH; [simp only [list.pairwise.nil, forall_prop_of_false (not_mem_nil _),
forall_true_iff, and_true, true_and, nil_append],
simp only [cons_append, pairwise_cons, forall_mem_append, IH, forall_mem_cons, forall_and_distrib,
and_assoc, and.left_comm]]
theorem pairwise_append_comm (s : symmetric R) {l₁ l₂ : list α} :
pairwise R (l₁++l₂) ↔ pairwise R (l₂++l₁) :=
have ∀ l₁ l₂ : list α,
(∀ (x : α), x ∈ l₁ → ∀ (y : α), y ∈ l₂ → R x y) →
(∀ (x : α), x ∈ l₂ → ∀ (y : α), y ∈ l₁ → R x y),
from λ l₁ l₂ a x xm y ym, s (a y ym x xm),
by simp only [pairwise_append, and.left_comm]; rw iff.intro (this l₁ l₂) (this l₂ l₁)
theorem pairwise_middle (s : symmetric R) {a : α} {l₁ l₂ : list α} :
pairwise R (l₁ ++ a :: l₂) ↔ pairwise R (a :: (l₁++l₂)) :=
show pairwise R (l₁ ++ ([a] ++ l₂)) ↔ pairwise R ([a] ++ l₁ ++ l₂),
by rw [← append_assoc, pairwise_append, @pairwise_append _ _ ([a] ++ l₁), pairwise_append_comm s];
simp only [mem_append, or_comm]
theorem pairwise_map (f : β → α) :
∀ {l : list β}, pairwise R (map f l) ↔ pairwise (λ a b : β, R (f a) (f b)) l
| [] := by simp only [map, pairwise.nil]
| (b :: l) :=
have (∀ a b', b' ∈ l → f b' = a → R (f b) a) ↔ ∀ (b' : β), b' ∈ l → R (f b) (f b'), from
forall_swap.trans $ forall_congr $ λ a, forall_swap.trans $ by simp only [forall_eq'],
by simp only [map, pairwise_cons, mem_map, exists_imp_distrib, and_imp, this, pairwise_map]
theorem pairwise_of_pairwise_map {S : β → β → Prop} (f : α → β)
(H : ∀ a b : α, S (f a) (f b) → R a b) {l : list α}
(p : pairwise S (map f l)) : pairwise R l :=
((pairwise_map f).1 p).imp H
theorem pairwise_map_of_pairwise {S : β → β → Prop} (f : α → β)
(H : ∀ a b : α, R a b → S (f a) (f b)) {l : list α}
(p : pairwise R l) : pairwise S (map f l) :=
(pairwise_map f).2 $ p.imp H
theorem pairwise_filter_map (f : β → option α) {l : list β} :
pairwise R (filter_map f l) ↔ pairwise (λ a a' : β, ∀ (b ∈ f a) (b' ∈ f a'), R b b') l :=
let S (a a' : β) := ∀ (b ∈ f a) (b' ∈ f a'), R b b' in
begin
simp only [option.mem_def], induction l with a l IH,
{ simp only [filter_map, pairwise.nil] },
cases e : f a with b,
{ rw [filter_map_cons_none _ _ e, IH, pairwise_cons],
simp only [e, forall_prop_of_false not_false, forall_3_true_iff, true_and] },
rw [filter_map_cons_some _ _ _ e],
simp only [pairwise_cons, mem_filter_map, exists_imp_distrib, and_imp, IH, e, forall_eq'],
show (∀ (a' : α) (x : β), x ∈ l → f x = some a' → R b a') ∧ pairwise S l ↔
(∀ (a' : β), a' ∈ l → ∀ (b' : α), f a' = some b' → R b b') ∧ pairwise S l,
from and_congr ⟨λ h b mb a ma, h a b mb ma, λ h a b mb ma, h b mb a ma⟩ iff.rfl
end
theorem pairwise_filter_map_of_pairwise {S : β → β → Prop} (f : α → option β)
(H : ∀ (a a' : α), R a a' → ∀ (b ∈ f a) (b' ∈ f a'), S b b') {l : list α}
(p : pairwise R l) : pairwise S (filter_map f l) :=
(pairwise_filter_map _).2 $ p.imp H
theorem pairwise_filter (p : α → Prop) [decidable_pred p] {l : list α} :
pairwise R (filter p l) ↔ pairwise (λ x y, p x → p y → R x y) l :=
begin
rw [← filter_map_eq_filter, pairwise_filter_map],
apply pairwise.iff, intros, simp only [option.mem_def, option.guard_eq_some, and_imp, forall_eq'],
end
theorem pairwise_filter_of_pairwise (p : α → Prop) [decidable_pred p] {l : list α}
: pairwise R l → pairwise R (filter p l) :=
pairwise_of_sublist (filter_sublist _)
theorem pairwise_pmap {p : β → Prop} {f : Π b, p b → α} {l : list β} (h : ∀ x ∈ l, p x) :
pairwise R (l.pmap f h) ↔
pairwise (λ b₁ b₂, ∀ (h₁ : p b₁) (h₂ : p b₂), R (f b₁ h₁) (f b₂ h₂)) l :=
begin
induction l with a l ihl, { simp },
obtain ⟨ha, hl⟩ : p a ∧ ∀ b, b ∈ l → p b, by simpa using h,
simp only [ihl hl, pairwise_cons, bex_imp_distrib, pmap, and.congr_left_iff, mem_pmap],
refine λ _, ⟨λ H b hb hpa hpb, H _ _ hb rfl, _⟩,
rintro H _ b hb rfl,
exact H b hb _ _
end
theorem pairwise.pmap {l : list α} (hl : pairwise R l)
{p : α → Prop} {f : Π a, p a → β} (h : ∀ x ∈ l, p x) {S : β → β → Prop}
(hS : ∀ ⦃x⦄ (hx : p x) ⦃y⦄ (hy : p y), R x y → S (f x hx) (f y hy)) :
pairwise S (l.pmap f h) :=
begin
refine (pairwise_pmap h).2 (pairwise.imp_of_mem _ hl),
intros, apply hS, assumption
end
theorem pairwise_join {L : list (list α)} : pairwise R (join L) ↔
(∀ l ∈ L, pairwise R l) ∧ pairwise (λ l₁ l₂, ∀ (x ∈ l₁) (y ∈ l₂), R x y) L :=
begin
induction L with l L IH,
{simp only [join, pairwise.nil, forall_prop_of_false (not_mem_nil _), forall_const, and_self]},
have : (∀ (x : α), x ∈ l → ∀ (y : α) (x_1 : list α), x_1 ∈ L → y ∈ x_1 → R x y) ↔
∀ (a' : list α), a' ∈ L → ∀ (x : α), x ∈ l → ∀ (y : α), y ∈ a' → R x y :=
⟨λ h a b c d e, h c d e a b, λ h c d e a b, h a b c d e⟩,
simp only [join, pairwise_append, IH, mem_join, exists_imp_distrib, and_imp, this,
forall_mem_cons, pairwise_cons],
simp only [and_assoc, and_comm, and.left_comm],
end
@[simp] theorem pairwise_reverse : ∀ {R} {l : list α},
pairwise R (reverse l) ↔ pairwise (λ x y, R y x) l :=
suffices ∀ {R l}, @pairwise α R l → pairwise (λ x y, R y x) (reverse l),
from λ R l, ⟨λ p, reverse_reverse l ▸ this p, this⟩,
λ R l p, by induction p with a l h p IH;
[apply pairwise.nil, simpa only [reverse_cons, pairwise_append, IH,
pairwise_cons, forall_prop_of_false (not_mem_nil _), forall_true_iff,
pairwise.nil, mem_reverse, mem_singleton, forall_eq, true_and] using h]
lemma pairwise.set_pairwise_on {l : list α} (h : pairwise R l) (hr : symmetric R) :
set.pairwise_on {x | x ∈ l} R :=
begin
induction h with hd tl imp h IH,
{ simp },
{ intros x hx y hy hxy,
simp only [mem_cons_iff, set.mem_set_of_eq] at hx hy,
rcases hx with rfl|hx;
rcases hy with rfl|hy,
{ contradiction },
{ exact imp y hy },
{ exact hr (imp x hx) },
{ exact IH x hx y hy hxy } }
end
lemma pairwise_of_reflexive_on_dupl_of_forall_ne [decidable_eq α] {l : list α} {r : α → α → Prop}
(hr : ∀ a, 1 < count a l → r a a)
(h : ∀ (a ∈ l) (b ∈ l), a ≠ b → r a b) : l.pairwise r :=
begin
induction l with hd tl IH,
{ simp },
{ rw list.pairwise_cons,
split,
{ intros x hx,
by_cases H : hd = x,
{ rw H,
refine hr _ _,
simpa [count_cons, H, nat.succ_lt_succ_iff, count_pos] using hx },
{ exact h hd (mem_cons_self _ _) x (mem_cons_of_mem _ hx) H } },
{ refine IH _ _,
{ intros x hx,
refine hr _ _,
rw count_cons,
split_ifs,
{ exact hx.trans (nat.lt_succ_self _) },
{ exact hx } },
{ intros x hx y hy,
exact h x (mem_cons_of_mem _ hx) y (mem_cons_of_mem _ hy) } } }
end
lemma pairwise_of_reflexive_of_forall_ne {l : list α} {r : α → α → Prop}
(hr : reflexive r) (h : ∀ (a ∈ l) (b ∈ l), a ≠ b → r a b) : l.pairwise r :=
begin
classical,
refine pairwise_of_reflexive_on_dupl_of_forall_ne _ h,
exact λ _ _, hr _
end
theorem pairwise_iff_nth_le {R} : ∀ {l : list α},
pairwise R l ↔ ∀ i j (h₁ : j < length l) (h₂ : i < j),
R (nth_le l i (lt_trans h₂ h₁)) (nth_le l j h₁)
| [] := by simp only [pairwise.nil, true_iff]; exact λ i j h, (not_lt_zero j).elim h
| (a :: l) := begin
rw [pairwise_cons, pairwise_iff_nth_le],
refine ⟨λ H i j h₁ h₂, _, λ H, ⟨λ a' m, _,
λ i j h₁ h₂, H _ _ (succ_lt_succ h₁) (succ_lt_succ h₂)⟩⟩,
{ cases j with j, {exact (not_lt_zero _).elim h₂},
cases i with i,
{ exact H.1 _ (nth_le_mem l _ _) },
{ exact H.2 _ _ (lt_of_succ_lt_succ h₁) (lt_of_succ_lt_succ h₂) } },
{ rcases nth_le_of_mem m with ⟨n, h, rfl⟩,
exact H _ _ (succ_lt_succ h) (succ_pos _) }
end
theorem pairwise_sublists' {R} : ∀ {l : list α}, pairwise R l →
pairwise (lex (swap R)) (sublists' l)
| _ pairwise.nil := pairwise_singleton _ _
| _ (@pairwise.cons _ _ a l H₁ H₂) :=
begin
simp only [sublists'_cons, pairwise_append, pairwise_map, mem_sublists', mem_map,
exists_imp_distrib, and_imp],
have IH := pairwise_sublists' H₂,
refine ⟨IH, IH.imp (λ l₁ l₂, lex.cons), _⟩,
intros l₁ sl₁ x l₂ sl₂ e, subst e,
cases l₁ with b l₁, {constructor},
exact lex.rel (H₁ _ $ sl₁.subset $ mem_cons_self _ _)
end
theorem pairwise_sublists {R} {l : list α} (H : pairwise R l) :
pairwise (λ l₁ l₂, lex R (reverse l₁) (reverse l₂)) (sublists l) :=
by have := pairwise_sublists' (pairwise_reverse.2 H);
rwa [sublists'_reverse, pairwise_map] at this
/-! ### Pairwise filtering -/
variable [decidable_rel R]
@[simp] theorem pw_filter_nil : pw_filter R [] = [] := rfl
@[simp] theorem pw_filter_cons_of_pos {a : α} {l : list α} (h : ∀ b ∈ pw_filter R l, R a b) :
pw_filter R (a :: l) = a :: pw_filter R l := if_pos h
@[simp] theorem pw_filter_cons_of_neg {a : α} {l : list α} (h : ¬ ∀ b ∈ pw_filter R l, R a b) :
pw_filter R (a :: l) = pw_filter R l := if_neg h
theorem pw_filter_map (f : β → α) :
Π (l : list β), pw_filter R (map f l) = map f (pw_filter (λ x y, R (f x) (f y)) l)
| [] := rfl
| (x :: xs) :=
if h : ∀ b ∈ pw_filter R (map f xs), R (f x) b
then have h' : ∀ (b : β), b ∈ pw_filter (λ (x y : β), R (f x) (f y)) xs → R (f x) (f b),
from λ b hb, h _ (by rw [pw_filter_map]; apply mem_map_of_mem _ hb),
by rw [map,pw_filter_cons_of_pos h,pw_filter_cons_of_pos h',pw_filter_map,map]
else have h' : ¬∀ (b : β), b ∈ pw_filter (λ (x y : β), R (f x) (f y)) xs → R (f x) (f b),
from λ hh, h $ λ a ha,
by { rw [pw_filter_map,mem_map] at ha, rcases ha with ⟨b,hb₀,hb₁⟩,
subst a, exact hh _ hb₀, },
by rw [map,pw_filter_cons_of_neg h,pw_filter_cons_of_neg h',pw_filter_map]
theorem pw_filter_sublist : ∀ (l : list α), pw_filter R l <+ l
| [] := nil_sublist _
| (x :: l) := begin
by_cases (∀ y ∈ pw_filter R l, R x y),
{ rw [pw_filter_cons_of_pos h],
exact cons_sublist_cons _ (pw_filter_sublist l) },
{ rw [pw_filter_cons_of_neg h],
exact sublist_cons_of_sublist _ (pw_filter_sublist l) },
end
theorem pw_filter_subset (l : list α) : pw_filter R l ⊆ l :=
(pw_filter_sublist _).subset
theorem pairwise_pw_filter : ∀ (l : list α), pairwise R (pw_filter R l)
| [] := pairwise.nil
| (x :: l) := begin
by_cases (∀ y ∈ pw_filter R l, R x y),
{ rw [pw_filter_cons_of_pos h],
exact pairwise_cons.2 ⟨h, pairwise_pw_filter l⟩ },
{ rw [pw_filter_cons_of_neg h],
exact pairwise_pw_filter l },
end
theorem pw_filter_eq_self {l : list α} : pw_filter R l = l ↔ pairwise R l :=
⟨λ e, e ▸ pairwise_pw_filter l, λ p, begin
induction l with x l IH, {refl},
cases pairwise_cons.1 p with al p,
rw [pw_filter_cons_of_pos (ball.imp_left (pw_filter_subset l) al), IH p],
end⟩
@[simp] theorem pw_filter_idempotent {l : list α} :
pw_filter R (pw_filter R l) = pw_filter R l :=
pw_filter_eq_self.mpr (pairwise_pw_filter l)
theorem forall_mem_pw_filter (neg_trans : ∀ {x y z}, R x z → R x y ∨ R y z)
(a : α) (l : list α) : (∀ b ∈ pw_filter R l, R a b) ↔ (∀ b ∈ l, R a b) :=
⟨begin
induction l with x l IH, { exact λ _ _, false.elim },
simp only [forall_mem_cons],
by_cases (∀ y ∈ pw_filter R l, R x y); dsimp at h,
{ simp only [pw_filter_cons_of_pos h, forall_mem_cons, and_imp],
exact λ r H, ⟨r, IH H⟩ },
{ rw [pw_filter_cons_of_neg h],
refine λ H, ⟨_, IH H⟩,
cases e : find (λ y, ¬ R x y) (pw_filter R l) with k,
{ refine h.elim (ball.imp_right _ (find_eq_none.1 e)),
exact λ y _, not_not.1 },
{ have := find_some e,
exact (neg_trans (H k (find_mem e))).resolve_right this } }
end, ball.imp_left (pw_filter_subset l)⟩
end list
|
c3bb46657fd720662b12e464efa2c857bdc6e796 | 302c785c90d40ad3d6be43d33bc6a558354cc2cf | /src/data/finsupp/basic.lean | d48fa2a03b1e67b64c6a66cc843419962c821cc6 | [
"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 | 96,078 | 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, Scott Morrison
-/
import algebra.group.pi
import algebra.big_operators.order
import algebra.module.basic
import algebra.module.pi
import group_theory.submonoid.basic
import data.fintype.card
import data.finset.preimage
import data.multiset.antidiagonal
import data.indicator_function
/-!
# Type of functions with finite support
For any type `α` and a type `M` with zero, we define the type `finsupp α M` (notation: `α →₀ M`)
of finitely supported functions from `α` to `M`, i.e. the functions which are zero everywhere
on `α` except on a finite set.
Functions with finite support are used (at least) in the following parts of the library:
* `monoid_algebra R M` and `add_monoid_algebra R M` are defined as `M →₀ R`;
* polynomials and multivariate polynomials are defined as `add_monoid_algebra`s, hence they use
`finsupp` under the hood;
* the linear combination of a family of vectors `v i` with coefficients `f i` (as used, e.g., to
define linearly independent family `linear_independent`) is defined as a map
`finsupp.total : (ι → M) → (ι →₀ R) →ₗ[R] M`.
Some other constructions are naturally equivalent to `α →₀ M` with some `α` and `M` but are defined
in a different way in the library:
* `multiset α ≃+ α →₀ ℕ`;
* `free_abelian_group α ≃+ α →₀ ℤ`.
Most of the theory assumes that the range is a commutative additive monoid. This gives us the big
sum operator as a powerful way to construct `finsupp` elements.
Many constructions based on `α →₀ M` use `semireducible` type tags to avoid reusing unwanted type
instances. E.g., `monoid_algebra`, `add_monoid_algebra`, and types based on these two have
non-pointwise multiplication.
## Notations
This file adds `α →₀ M` as a global notation for `finsupp α M`. We also use the following convention
for `Type*` variables in this file
* `α`, `β`, `γ`: types with no additional structure that appear as the first argument to `finsupp`
somewhere in the statement;
* `ι` : an auxiliary index type;
* `M`, `M'`, `N`, `P`: types with `has_zero` or `(add_)(comm_)monoid` structure; `M` is also used
for a (semi)module over a (semi)ring.
* `G`, `H`: groups (commutative or not, multiplicative or additive);
* `R`, `S`: (semi)rings.
## TODO
* This file is currently ~2K lines long, so possibly it should be splitted into smaller chunks;
* Add the list of definitions and important lemmas to the module docstring.
## Implementation notes
This file is a `noncomputable theory` and uses classical logic throughout.
## Notation
This file defines `α →₀ β` as notation for `finsupp α β`.
-/
noncomputable theory
open_locale classical big_operators
open finset
variables {α β γ ι M M' N P G H R S : Type*}
/-- `finsupp α M`, denoted `α →₀ M`, is the type of functions `f : α → M` such that
`f x = 0` for all but finitely many `x`. -/
structure finsupp (α : Type*) (M : Type*) [has_zero M] :=
(support : finset α)
(to_fun : α → M)
(mem_support_to_fun : ∀a, a ∈ support ↔ to_fun a ≠ 0)
infixr ` →₀ `:25 := finsupp
namespace finsupp
/-! ### Basic declarations about `finsupp` -/
section basic
variable [has_zero M]
instance : has_coe_to_fun (α →₀ M) := ⟨λ _, α → M, to_fun⟩
@[simp] lemma coe_mk (f : α → M) (s : finset α) (h : ∀ a, a ∈ s ↔ f a ≠ 0) :
⇑(⟨s, f, h⟩ : α →₀ M) = f := rfl
instance : has_zero (α →₀ M) := ⟨⟨∅, 0, λ _, ⟨false.elim, λ H, H rfl⟩⟩⟩
@[simp] lemma coe_zero : ⇑(0 : α →₀ M) = 0 := rfl
lemma zero_apply {a : α} : (0 : α →₀ M) a = 0 := rfl
@[simp] lemma support_zero : (0 : α →₀ M).support = ∅ := rfl
instance : inhabited (α →₀ M) := ⟨0⟩
@[simp] lemma mem_support_iff {f : α →₀ M} : ∀{a:α}, a ∈ f.support ↔ f a ≠ 0 :=
f.mem_support_to_fun
@[simp, norm_cast] lemma fun_support_eq (f : α →₀ M) : function.support f = f.support :=
set.ext $ λ x, mem_support_iff.symm
lemma not_mem_support_iff {f : α →₀ M} {a} : a ∉ f.support ↔ f a = 0 :=
not_iff_comm.1 mem_support_iff.symm
lemma coe_fn_injective : @function.injective (α →₀ M) (α → M) coe_fn
| ⟨s, f, hf⟩ ⟨t, g, hg⟩ h :=
begin
change f = g at h, subst h,
have : s = t, { ext a, exact (hf a).trans (hg a).symm },
subst this
end
@[simp, norm_cast] lemma coe_fn_inj {f g : α →₀ M} : (f : α → M) = g ↔ f = g :=
coe_fn_injective.eq_iff
@[simp, norm_cast] lemma coe_eq_zero {f : α →₀ M} : (f : α → M) = 0 ↔ f = 0 :=
by rw [← coe_zero, coe_fn_inj]
@[ext] lemma ext {f g : α →₀ M} (h : ∀a, f a = g a) : f = g := coe_fn_injective (funext h)
lemma ext_iff {f g : α →₀ M} : f = g ↔ (∀a:α, f a = g a) :=
⟨by rintros rfl a; refl, ext⟩
lemma ext_iff' {f g : α →₀ M} : f = g ↔ f.support = g.support ∧ ∀ x ∈ f.support, f x = g x :=
⟨λ h, h ▸ ⟨rfl, λ _ _, rfl⟩, λ ⟨h₁, h₂⟩, ext $ λ a,
if h : a ∈ f.support then h₂ a h else
have hf : f a = 0, from not_mem_support_iff.1 h,
have hg : g a = 0, by rwa [h₁, not_mem_support_iff] at h,
by rw [hf, hg]⟩
@[simp] lemma support_eq_empty {f : α →₀ M} : f.support = ∅ ↔ f = 0 :=
by exact_mod_cast @function.support_eq_empty_iff _ _ _ f
lemma support_nonempty_iff {f : α →₀ M} : f.support.nonempty ↔ f ≠ 0 :=
by simp only [finsupp.support_eq_empty, finset.nonempty_iff_ne_empty, ne.def]
lemma nonzero_iff_exists {f : α →₀ M} : f ≠ 0 ↔ ∃ a : α, f a ≠ 0 :=
by simp [← finsupp.support_eq_empty, finset.eq_empty_iff_forall_not_mem]
lemma card_support_eq_zero {f : α →₀ M} : card f.support = 0 ↔ f = 0 :=
by simp
instance finsupp.decidable_eq [decidable_eq α] [decidable_eq M] : decidable_eq (α →₀ M) :=
assume f g, decidable_of_iff (f.support = g.support ∧ (∀a∈f.support, f a = g a)) ext_iff'.symm
lemma finite_support (f : α →₀ M) : set.finite (function.support f) :=
f.fun_support_eq.symm ▸ f.support.finite_to_set
lemma support_subset_iff {s : set α} {f : α →₀ M} :
↑f.support ⊆ s ↔ (∀a∉s, f a = 0) :=
by simp only [set.subset_def, mem_coe, mem_support_iff];
exact forall_congr (assume a, not_imp_comm)
/-- Given `fintype α`, `equiv_fun_on_fintype` is the `equiv` between `α →₀ β` and `α → β`.
(All functions on a finite type are finitely supported.) -/
def equiv_fun_on_fintype [fintype α] : (α →₀ M) ≃ (α → M) :=
⟨λf a, f a, λf, mk (finset.univ.filter $ λa, f a ≠ 0) f (by simp only [true_and, finset.mem_univ,
iff_self, finset.mem_filter, finset.filter_congr_decidable, forall_true_iff]),
begin intro f, ext a, refl end,
begin intro f, ext a, refl end⟩
end basic
/-! ### Declarations about `single` -/
section single
variables [has_zero M] {a a' : α} {b : M}
/-- `single a b` is the finitely supported function which has
value `b` at `a` and zero otherwise. -/
def single (a : α) (b : M) : α →₀ M :=
⟨if b = 0 then ∅ else {a}, λ a', if a = a' then b else 0, λ a', begin
by_cases hb : b = 0; by_cases a = a';
simp only [hb, h, if_pos, if_false, mem_singleton],
{ exact ⟨false.elim, λ H, H rfl⟩ },
{ exact ⟨false.elim, λ H, H rfl⟩ },
{ exact ⟨λ _, hb, λ _, rfl⟩ },
{ exact ⟨λ H _, h H.symm, λ H, (H rfl).elim⟩ }
end⟩
lemma single_apply : single a b a' = if a = a' then b else 0 :=
rfl
lemma single_eq_indicator : ⇑(single a b) = set.indicator {a} (λ _, b) :=
by { ext, simp [single_apply, set.indicator, @eq_comm _ a] }
@[simp] lemma single_eq_same : (single a b : α →₀ M) a = b :=
if_pos rfl
@[simp] lemma single_eq_of_ne (h : a ≠ a') : (single a b : α →₀ M) a' = 0 :=
if_neg h
lemma single_eq_update : ⇑(single a b) = function.update 0 a b :=
by rw [single_eq_indicator, ← set.piecewise_eq_indicator, set.piecewise_singleton]
lemma single_eq_pi_single : ⇑(single a b) = pi.single a b :=
single_eq_update
@[simp] lemma single_zero : (single a 0 : α →₀ M) = 0 :=
coe_fn_injective $ by simpa only [single_eq_update, coe_zero]
using function.update_eq_self a (0 : α → M)
lemma single_of_single_apply (a a' : α) (b : M) :
single a ((single a' b) a) = single a' (single a' b) a :=
begin
rw [single_apply, single_apply],
ext,
split_ifs,
{ rw h, },
{ rw [zero_apply, single_apply, if_t_t], },
end
lemma support_single_ne_zero (hb : b ≠ 0) : (single a b).support = {a} :=
if_neg hb
lemma support_single_subset : (single a b).support ⊆ {a} :=
show ite _ _ _ ⊆ _, by split_ifs; [exact empty_subset _, exact subset.refl _]
lemma single_apply_mem (x) : single a b x ∈ ({0, b} : set M) :=
by rcases em (a = x) with (rfl|hx); [simp, simp [single_eq_of_ne hx]]
lemma range_single_subset : set.range (single a b) ⊆ {0, b} :=
set.range_subset_iff.2 single_apply_mem
lemma single_injective (a : α) : function.injective (single a : M → α →₀ M) :=
assume b₁ b₂ eq,
have (single a b₁ : α →₀ M) a = (single a b₂ : α →₀ M) a, by rw eq,
by rwa [single_eq_same, single_eq_same] at this
lemma single_apply_eq_zero {a x : α} {b : M} : single a b x = 0 ↔ (x = a → b = 0) :=
by simp [single_eq_indicator]
lemma mem_support_single (a a' : α) (b : M) :
a ∈ (single a' b).support ↔ a = a' ∧ b ≠ 0 :=
by simp [single_apply_eq_zero, not_or_distrib]
lemma eq_single_iff {f : α →₀ M} {a b} : f = single a b ↔ f.support ⊆ {a} ∧ f a = b :=
begin
refine ⟨λ h, h.symm ▸ ⟨support_single_subset, single_eq_same⟩, _⟩,
rintro ⟨h, rfl⟩,
ext x,
by_cases hx : a = x; simp only [hx, single_eq_same, single_eq_of_ne, ne.def, not_false_iff],
exact not_mem_support_iff.1 (mt (λ hx, (mem_singleton.1 (h hx)).symm) hx)
end
lemma single_eq_single_iff (a₁ a₂ : α) (b₁ b₂ : M) :
single a₁ b₁ = single a₂ b₂ ↔ ((a₁ = a₂ ∧ b₁ = b₂) ∨ (b₁ = 0 ∧ b₂ = 0)) :=
begin
split,
{ assume eq,
by_cases a₁ = a₂,
{ refine or.inl ⟨h, _⟩,
rwa [h, (single_injective a₂).eq_iff] at eq },
{ rw [ext_iff] at eq,
have h₁ := eq a₁,
have h₂ := eq a₂,
simp only [single_eq_same, single_eq_of_ne h, single_eq_of_ne (ne.symm h)] at h₁ h₂,
exact or.inr ⟨h₁, h₂.symm⟩ } },
{ rintros (⟨rfl, rfl⟩ | ⟨rfl, rfl⟩),
{ refl },
{ rw [single_zero, single_zero] } }
end
lemma single_left_inj (h : b ≠ 0) :
single a b = single a' b ↔ a = a' :=
⟨λ H, by simpa only [h, single_eq_single_iff,
and_false, or_false, eq_self_iff_true, and_true] using H,
λ H, by rw [H]⟩
lemma support_single_ne_bot (i : α) (h : b ≠ 0) :
(single i b).support ≠ ⊥ :=
begin
have : i ∈ (single i b).support := by simpa using h,
intro H,
simpa [H]
end
lemma support_single_disjoint {b' : M} (hb : b ≠ 0) (hb' : b' ≠ 0) {i j : α} :
disjoint (single i b).support (single j b').support ↔ i ≠ j :=
by simpa [support_single_ne_zero, hb, hb'] using ne_comm
@[simp] lemma single_eq_zero : single a b = 0 ↔ b = 0 :=
by simp [ext_iff, single_eq_indicator]
lemma single_swap (a₁ a₂ : α) (b : M) : single a₁ b a₂ = single a₂ b a₁ :=
by simp only [single_apply]; ac_refl
instance [nonempty α] [nontrivial M] : nontrivial (α →₀ M) :=
begin
inhabit α,
rcases exists_ne (0 : M) with ⟨x, hx⟩,
exact nontrivial_of_ne (single (default α) x) 0 (mt single_eq_zero.1 hx)
end
lemma unique_single [unique α] (x : α →₀ M) : x = single (default α) (x (default α)) :=
ext $ unique.forall_iff.2 single_eq_same.symm
lemma unique_ext [unique α] {f g : α →₀ M} (h : f (default α) = g (default α)) : f = g :=
ext $ λ a, by rwa [unique.eq_default a]
lemma unique_ext_iff [unique α] {f g : α →₀ M} : f = g ↔ f (default α) = g (default α) :=
⟨λ h, h ▸ rfl, unique_ext⟩
@[simp] lemma unique_single_eq_iff [unique α] {b' : M} :
single a b = single a' b' ↔ b = b' :=
by rw [unique_ext_iff, unique.eq_default a, unique.eq_default a', single_eq_same, single_eq_same]
lemma support_eq_singleton {f : α →₀ M} {a : α} :
f.support = {a} ↔ f a ≠ 0 ∧ f = single a (f a) :=
⟨λ h, ⟨mem_support_iff.1 $ h.symm ▸ finset.mem_singleton_self a,
eq_single_iff.2 ⟨subset_of_eq h, rfl⟩⟩, λ h, h.2.symm ▸ support_single_ne_zero h.1⟩
lemma support_eq_singleton' {f : α →₀ M} {a : α} :
f.support = {a} ↔ ∃ b ≠ 0, f = single a b :=
⟨λ h, let h := support_eq_singleton.1 h in ⟨_, h.1, h.2⟩,
λ ⟨b, hb, hf⟩, hf.symm ▸ support_single_ne_zero hb⟩
lemma card_support_eq_one {f : α →₀ M} : card f.support = 1 ↔ ∃ a, f a ≠ 0 ∧ f = single a (f a) :=
by simp only [card_eq_one, support_eq_singleton]
lemma card_support_eq_one' {f : α →₀ M} : card f.support = 1 ↔ ∃ a (b ≠ 0), f = single a b :=
by simp only [card_eq_one, support_eq_singleton']
end single
/-! ### Declarations about `on_finset` -/
section on_finset
variables [has_zero M]
/-- `on_finset s f hf` is the finsupp function representing `f` restricted to the finset `s`.
The function needs to be `0` outside of `s`. Use this when the set needs to be filtered anyways,
otherwise a better set representation is often available. -/
def on_finset (s : finset α) (f : α → M) (hf : ∀a, f a ≠ 0 → a ∈ s) : α →₀ M :=
⟨s.filter (λa, f a ≠ 0), f, by simpa⟩
@[simp] lemma on_finset_apply {s : finset α} {f : α → M} {hf a} :
(on_finset s f hf : α →₀ M) a = f a :=
rfl
@[simp] lemma support_on_finset_subset {s : finset α} {f : α → M} {hf} :
(on_finset s f hf).support ⊆ s :=
filter_subset _ _
@[simp] lemma mem_support_on_finset
{s : finset α} {f : α → M} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) {a : α} :
a ∈ (finsupp.on_finset s f hf).support ↔ f a ≠ 0 :=
by rw [finsupp.mem_support_iff, finsupp.on_finset_apply]
lemma support_on_finset
{s : finset α} {f : α → M} (hf : ∀ (a : α), f a ≠ 0 → a ∈ s) :
(finsupp.on_finset s f hf).support = s.filter (λ a, f a ≠ 0) :=
rfl
end on_finset
section of_support_finite
variables [has_zero M]
/-- The natural `finsupp` induced by the function `f` given that it has finite support. -/
noncomputable def of_support_finite
(f : α → M) (hf : (function.support f).finite) : α →₀ M :=
{ support := hf.to_finset,
to_fun := f,
mem_support_to_fun := λ _, hf.mem_to_finset }
lemma of_support_finite_coe {f : α → M} {hf : (function.support f).finite} :
(of_support_finite f hf : α → M) = f := rfl
instance : can_lift (α → M) (α →₀ M) :=
{ coe := coe_fn,
cond := λ f, (function.support f).finite,
prf := λ f hf, ⟨of_support_finite f hf, rfl⟩ }
end of_support_finite
/-! ### Declarations about `map_range` -/
section map_range
variables [has_zero M] [has_zero N] [has_zero P]
/-- The composition of `f : M → N` and `g : α →₀ M` is
`map_range f hf g : α →₀ N`, well-defined when `f 0 = 0`.
This preserves the structure on `f`, and exists in various bundled forms for when `f` is itself
bundled:
* `finsupp.map_range.zero_hom`
* `finsupp.map_range.add_monoid_hom`
* `finsupp.map_range.add_equiv`
* `finsupp.map_range.linear_map`
* `finsupp.map_range.linear_equiv`
-/
def map_range (f : M → N) (hf : f 0 = 0) (g : α →₀ M) : α →₀ N :=
on_finset g.support (f ∘ g) $
assume a, by rw [mem_support_iff, not_imp_not]; exact λ H, (congr_arg f H).trans hf
@[simp] lemma map_range_apply {f : M → N} {hf : f 0 = 0} {g : α →₀ M} {a : α} :
map_range f hf g a = f (g a) :=
rfl
@[simp] lemma map_range_zero {f : M → N} {hf : f 0 = 0} : map_range f hf (0 : α →₀ M) = 0 :=
ext $ λ a, by simp only [hf, zero_apply, map_range_apply]
@[simp] lemma map_range_id (g : α →₀ M) : map_range id rfl g = g :=
ext $ λ _, rfl
lemma map_range_comp
(f : N → P) (hf : f 0 = 0) (f₂ : M → N) (hf₂ : f₂ 0 = 0) (h : (f ∘ f₂) 0 = 0) (g : α →₀ M) :
map_range (f ∘ f₂) h g = map_range f hf (map_range f₂ hf₂ g) :=
ext $ λ _, rfl
lemma support_map_range {f : M → N} {hf : f 0 = 0} {g : α →₀ M} :
(map_range f hf g).support ⊆ g.support :=
support_on_finset_subset
@[simp] lemma map_range_single {f : M → N} {hf : f 0 = 0} {a : α} {b : M} :
map_range f hf (single a b) = single a (f b) :=
ext $ λ a', show f (ite _ _ _) = ite _ _ _, by split_ifs; [refl, exact hf]
end map_range
/-! ### Declarations about `emb_domain` -/
section emb_domain
variables [has_zero M] [has_zero N]
/-- Given `f : α ↪ β` and `v : α →₀ M`, `emb_domain f v : β →₀ M`
is the finitely supported function whose value at `f a : β` is `v a`.
For a `b : β` outside the range of `f`, it is zero. -/
def emb_domain (f : α ↪ β) (v : α →₀ M) : β →₀ M :=
begin
refine ⟨v.support.map f, λa₂,
if h : a₂ ∈ v.support.map f then v (v.support.choose (λa₁, f a₁ = a₂) _) else 0, _⟩,
{ rcases finset.mem_map.1 h with ⟨a, ha, rfl⟩,
exact exists_unique.intro a ⟨ha, rfl⟩ (assume b ⟨_, hb⟩, f.injective hb) },
{ assume a₂,
split_ifs,
{ simp only [h, true_iff, ne.def],
rw [← not_mem_support_iff, not_not],
apply finset.choose_mem },
{ simp only [h, ne.def, ne_self_iff_false] } }
end
@[simp] lemma support_emb_domain (f : α ↪ β) (v : α →₀ M) :
(emb_domain f v).support = v.support.map f :=
rfl
@[simp] lemma emb_domain_zero (f : α ↪ β) : (emb_domain f 0 : β →₀ M) = 0 :=
rfl
@[simp] lemma emb_domain_apply (f : α ↪ β) (v : α →₀ M) (a : α) :
emb_domain f v (f a) = v a :=
begin
change dite _ _ _ = _,
split_ifs; rw [finset.mem_map' f] at h,
{ refine congr_arg (v : α → M) (f.inj' _),
exact finset.choose_property (λa₁, f a₁ = f a) _ _ },
{ exact (not_mem_support_iff.1 h).symm }
end
lemma emb_domain_notin_range (f : α ↪ β) (v : α →₀ M) (a : β) (h : a ∉ set.range f) :
emb_domain f v a = 0 :=
begin
refine dif_neg (mt (assume h, _) h),
rcases finset.mem_map.1 h with ⟨a, h, rfl⟩,
exact set.mem_range_self a
end
lemma emb_domain_injective (f : α ↪ β) :
function.injective (emb_domain f : (α →₀ M) → (β →₀ M)) :=
λ l₁ l₂ h, ext $ λ a, by simpa only [emb_domain_apply] using ext_iff.1 h (f a)
@[simp] lemma emb_domain_inj {f : α ↪ β} {l₁ l₂ : α →₀ M} :
emb_domain f l₁ = emb_domain f l₂ ↔ l₁ = l₂ :=
(emb_domain_injective f).eq_iff
@[simp] lemma emb_domain_eq_zero {f : α ↪ β} {l : α →₀ M} :
emb_domain f l = 0 ↔ l = 0 :=
(emb_domain_injective f).eq_iff' $ emb_domain_zero f
lemma emb_domain_map_range
(f : α ↪ β) (g : M → N) (p : α →₀ M) (hg : g 0 = 0) :
emb_domain f (map_range g hg p) = map_range g hg (emb_domain f p) :=
begin
ext a,
by_cases a ∈ set.range f,
{ rcases h with ⟨a', rfl⟩,
rw [map_range_apply, emb_domain_apply, emb_domain_apply, map_range_apply] },
{ rw [map_range_apply, emb_domain_notin_range, emb_domain_notin_range, ← hg]; assumption }
end
lemma single_of_emb_domain_single
(l : α →₀ M) (f : α ↪ β) (a : β) (b : M) (hb : b ≠ 0)
(h : l.emb_domain f = single a b) :
∃ x, l = single x b ∧ f x = a :=
begin
have h_map_support : finset.map f (l.support) = {a},
by rw [←support_emb_domain, h, support_single_ne_zero hb]; refl,
have ha : a ∈ finset.map f (l.support),
by simp only [h_map_support, finset.mem_singleton],
rcases finset.mem_map.1 ha with ⟨c, hc₁, hc₂⟩,
use c,
split,
{ ext d,
rw [← emb_domain_apply f l, h],
by_cases h_cases : c = d,
{ simp only [eq.symm h_cases, hc₂, single_eq_same] },
{ rw [single_apply, single_apply, if_neg, if_neg h_cases],
by_contra hfd,
exact h_cases (f.injective (hc₂.trans hfd)) } },
{ exact hc₂ }
end
end emb_domain
/-! ### Declarations about `zip_with` -/
section zip_with
variables [has_zero M] [has_zero N] [has_zero P]
/-- `zip_with f hf g₁ g₂` is the finitely supported function satisfying
`zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a)`, and it is well-defined when `f 0 0 = 0`. -/
def zip_with (f : M → N → P) (hf : f 0 0 = 0) (g₁ : α →₀ M) (g₂ : α →₀ N) : (α →₀ P) :=
on_finset (g₁.support ∪ g₂.support) (λa, f (g₁ a) (g₂ a)) $ λ a H,
begin
simp only [mem_union, mem_support_iff, ne], rw [← not_and_distrib],
rintro ⟨h₁, h₂⟩, rw [h₁, h₂] at H, exact H hf
end
@[simp] lemma zip_with_apply
{f : M → N → P} {hf : f 0 0 = 0} {g₁ : α →₀ M} {g₂ : α →₀ N} {a : α} :
zip_with f hf g₁ g₂ a = f (g₁ a) (g₂ a) :=
rfl
lemma support_zip_with [D : decidable_eq α] {f : M → N → P} {hf : f 0 0 = 0}
{g₁ : α →₀ M} {g₂ : α →₀ N} : (zip_with f hf g₁ g₂).support ⊆ g₁.support ∪ g₂.support :=
by rw subsingleton.elim D; exact support_on_finset_subset
end zip_with
/-! ### Declarations about `erase` -/
section erase
variables [has_zero M]
/-- `erase a f` is the finitely supported function equal to `f` except at `a` where it is equal to
`0`. -/
def erase (a : α) (f : α →₀ M) : α →₀ M :=
⟨f.support.erase a, (λa', if a' = a then 0 else f a'),
assume a', by rw [mem_erase, mem_support_iff]; split_ifs;
[exact ⟨λ H _, H.1 h, λ H, (H rfl).elim⟩,
exact and_iff_right h]⟩
@[simp] lemma support_erase {a : α} {f : α →₀ M} :
(f.erase a).support = f.support.erase a :=
rfl
@[simp] lemma erase_same {a : α} {f : α →₀ M} : (f.erase a) a = 0 :=
if_pos rfl
@[simp] lemma erase_ne {a a' : α} {f : α →₀ M} (h : a' ≠ a) : (f.erase a) a' = f a' :=
if_neg h
@[simp] lemma erase_single {a : α} {b : M} : (erase a (single a b)) = 0 :=
begin
ext s, by_cases hs : s = a,
{ rw [hs, erase_same], refl },
{ rw [erase_ne hs], exact single_eq_of_ne (ne.symm hs) }
end
lemma erase_single_ne {a a' : α} {b : M} (h : a ≠ a') : (erase a (single a' b)) = single a' b :=
begin
ext s, by_cases hs : s = a,
{ rw [hs, erase_same, single_eq_of_ne (h.symm)] },
{ rw [erase_ne hs] }
end
@[simp] lemma erase_zero (a : α) : erase a (0 : α →₀ M) = 0 :=
by rw [← support_eq_empty, support_erase, support_zero, erase_empty]
end erase
/-!
### Declarations about `sum` and `prod`
In most of this section, the domain `β` is assumed to be an `add_monoid`.
-/
section sum_prod
-- [to_additive sum] for finsupp.prod doesn't work, the equation lemmas are not generated
/-- `sum f g` is the sum of `g a (f a)` over the support of `f`. -/
def sum [has_zero M] [add_comm_monoid N] (f : α →₀ M) (g : α → M → N) : N :=
∑ a in f.support, g a (f a)
/-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/
@[to_additive]
def prod [has_zero M] [comm_monoid N] (f : α →₀ M) (g : α → M → N) : N :=
∏ a in f.support, g a (f a)
variables [has_zero M] [has_zero M'] [comm_monoid N]
@[to_additive]
lemma prod_of_support_subset (f : α →₀ M) {s : finset α}
(hs : f.support ⊆ s) (g : α → M → N) (h : ∀ i ∈ s, g i 0 = 1) :
f.prod g = ∏ x in s, g x (f x) :=
finset.prod_subset hs $ λ x hxs hx, h x hxs ▸ congr_arg (g x) $ not_mem_support_iff.1 hx
@[to_additive]
lemma prod_fintype [fintype α] (f : α →₀ M) (g : α → M → N) (h : ∀ i, g i 0 = 1) :
f.prod g = ∏ i, g i (f i) :=
f.prod_of_support_subset (subset_univ _) g (λ x _, h x)
@[simp, to_additive]
lemma prod_single_index {a : α} {b : M} {h : α → M → N} (h_zero : h a 0 = 1) :
(single a b).prod h = h a b :=
calc (single a b).prod h = ∏ x in {a}, h x (single a b x) :
prod_of_support_subset _ support_single_subset h $ λ x hx, (mem_singleton.1 hx).symm ▸ h_zero
... = h a b : by simp
@[to_additive]
lemma prod_map_range_index {f : M → M'} {hf : f 0 = 0} {g : α →₀ M} {h : α → M' → N}
(h0 : ∀a, h a 0 = 1) : (map_range f hf g).prod h = g.prod (λa b, h a (f b)) :=
finset.prod_subset support_map_range $ λ _ _ H,
by rw [not_mem_support_iff.1 H, h0]
@[simp, to_additive]
lemma prod_zero_index {h : α → M → N} : (0 : α →₀ M).prod h = 1 := rfl
@[to_additive]
lemma prod_comm (f : α →₀ M) (g : β →₀ M') (h : α → M → β → M' → N) :
f.prod (λ x v, g.prod (λ x' v', h x v x' v')) = g.prod (λ x' v', f.prod (λ x v, h x v x' v')) :=
finset.prod_comm
@[simp, to_additive]
lemma prod_ite_eq [decidable_eq α] (f : α →₀ M) (a : α) (b : α → M → N) :
f.prod (λ x v, ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 :=
by { dsimp [finsupp.prod], rw f.support.prod_ite_eq, }
@[simp] lemma sum_ite_self_eq
[decidable_eq α] {N : Type*} [add_comm_monoid N] (f : α →₀ N) (a : α) :
f.sum (λ x v, ite (a = x) v 0) = f a :=
by { convert f.sum_ite_eq a (λ x, id), simp [ite_eq_right_iff.2 eq.symm] }
/-- A restatement of `prod_ite_eq` with the equality test reversed. -/
@[simp, to_additive "A restatement of `sum_ite_eq` with the equality test reversed."]
lemma prod_ite_eq' [decidable_eq α] (f : α →₀ M) (a : α) (b : α → M → N) :
f.prod (λ x v, ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 :=
by { dsimp [finsupp.prod], rw f.support.prod_ite_eq', }
@[simp] lemma sum_ite_self_eq'
[decidable_eq α] {N : Type*} [add_comm_monoid N] (f : α →₀ N) (a : α) :
f.sum (λ x v, ite (x = a) v 0) = f a :=
by { convert f.sum_ite_eq' a (λ x, id), simp [ite_eq_right_iff.2 eq.symm] }
@[simp] lemma prod_pow [fintype α] (f : α →₀ ℕ) (g : α → N) :
f.prod (λ a b, g a ^ b) = ∏ a, g a ^ (f a) :=
f.prod_fintype _ $ λ a, pow_zero _
/-- If `g` maps a second argument of 0 to 1, then multiplying it over the
result of `on_finset` is the same as multiplying it over the original
`finset`. -/
@[to_additive "If `g` maps a second argument of 0 to 0, summing it over the
result of `on_finset` is the same as summing it over the original
`finset`."]
lemma on_finset_prod {s : finset α} {f : α → M} {g : α → M → N}
(hf : ∀a, f a ≠ 0 → a ∈ s) (hg : ∀ a, g a 0 = 1) :
(on_finset s f hf).prod g = ∏ a in s, g a (f a) :=
finset.prod_subset support_on_finset_subset $ by simp [*] { contextual := tt }
end sum_prod
/-!
### Additive monoid structure on `α →₀ M`
-/
section add_zero_class
variables [add_zero_class M]
instance : has_add (α →₀ M) := ⟨zip_with (+) (add_zero 0)⟩
@[simp] lemma coe_add (f g : α →₀ M) : ⇑(f + g) = f + g := rfl
lemma add_apply (g₁ g₂ : α →₀ M) (a : α) : (g₁ + g₂) a = g₁ a + g₂ a := rfl
lemma support_add [decidable_eq α] {g₁ g₂ : α →₀ M} :
(g₁ + g₂).support ⊆ g₁.support ∪ g₂.support :=
support_zip_with
lemma support_add_eq [decidable_eq α] {g₁ g₂ : α →₀ M} (h : disjoint g₁.support g₂.support) :
(g₁ + g₂).support = g₁.support ∪ g₂.support :=
le_antisymm support_zip_with $ assume a ha,
(finset.mem_union.1 ha).elim
(assume ha, have a ∉ g₂.support, from disjoint_left.1 h ha,
by simp only [mem_support_iff, not_not] at *;
simpa only [add_apply, this, add_zero])
(assume ha, have a ∉ g₁.support, from disjoint_right.1 h ha,
by simp only [mem_support_iff, not_not] at *;
simpa only [add_apply, this, zero_add])
@[simp] lemma single_add {a : α} {b₁ b₂ : M} : single a (b₁ + b₂) = single a b₁ + single a b₂ :=
ext $ assume a',
begin
by_cases h : a = a',
{ rw [h, add_apply, single_eq_same, single_eq_same, single_eq_same] },
{ rw [add_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h, zero_add] }
end
instance : add_zero_class (α →₀ M) :=
{ zero := 0,
add := (+),
zero_add := assume ⟨s, f, hf⟩, ext $ assume a, zero_add _,
add_zero := assume ⟨s, f, hf⟩, ext $ assume a, add_zero _ }
/-- `finsupp.single` as an `add_monoid_hom`.
See `finsupp.lsingle` for the stronger version as a linear map.
-/
@[simps] def single_add_hom (a : α) : M →+ α →₀ M :=
⟨single a, single_zero, λ _ _, single_add⟩
/-- Evaluation of a function `f : α →₀ M` at a point as an additive monoid homomorphism.
See `finsupp.lapply` for the stronger version as a linear map. -/
@[simps apply]
def apply_add_hom (a : α) : (α →₀ M) →+ M := ⟨λ g, g a, zero_apply, λ _ _, add_apply _ _ _⟩
lemma single_add_erase (a : α) (f : α →₀ M) : single a (f a) + f.erase a = f :=
ext $ λ a',
if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, add_zero]
else by simp only [add_apply, single_eq_of_ne h, zero_add, erase_ne (ne.symm h)]
lemma erase_add_single (a : α) (f : α →₀ M) : f.erase a + single a (f a) = f :=
ext $ λ a',
if h : a = a' then by subst h; simp only [add_apply, single_eq_same, erase_same, zero_add]
else by simp only [add_apply, single_eq_of_ne h, add_zero, erase_ne (ne.symm h)]
@[simp] lemma erase_add (a : α) (f f' : α →₀ M) : erase a (f + f') = erase a f + erase a f' :=
begin
ext s, by_cases hs : s = a,
{ rw [hs, add_apply, erase_same, erase_same, erase_same, add_zero] },
rw [add_apply, erase_ne hs, erase_ne hs, erase_ne hs, add_apply],
end
@[elab_as_eliminator]
protected theorem induction {p : (α →₀ M) → Prop} (f : α →₀ M)
(h0 : p 0) (ha : ∀a b (f : α →₀ M), a ∉ f.support → b ≠ 0 → p f → p (single a b + f)) :
p f :=
suffices ∀s (f : α →₀ M), f.support = s → p f, from this _ _ rfl,
assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $
assume a s has ih f hf,
suffices p (single a (f a) + f.erase a), by rwa [single_add_erase] at this,
begin
apply ha,
{ rw [support_erase, mem_erase], exact λ H, H.1 rfl },
{ rw [← mem_support_iff, hf], exact mem_insert_self _ _ },
{ apply ih _ _,
rw [support_erase, hf, finset.erase_insert has] }
end
lemma induction₂ {p : (α →₀ M) → Prop} (f : α →₀ M)
(h0 : p 0) (ha : ∀a b (f : α →₀ M), a ∉ f.support → b ≠ 0 → p f → p (f + single a b)) :
p f :=
suffices ∀s (f : α →₀ M), f.support = s → p f, from this _ _ rfl,
assume s, finset.induction_on s (λ f hf, by rwa [support_eq_empty.1 hf]) $
assume a s has ih f hf,
suffices p (f.erase a + single a (f a)), by rwa [erase_add_single] at this,
begin
apply ha,
{ rw [support_erase, mem_erase], exact λ H, H.1 rfl },
{ rw [← mem_support_iff, hf], exact mem_insert_self _ _ },
{ apply ih _ _,
rw [support_erase, hf, finset.erase_insert has] }
end
lemma induction_linear {p : (α →₀ M) → Prop} (f : α →₀ M)
(h0 : p 0) (hadd : ∀ f g : α →₀ M, p f → p g → p (f + g)) (hsingle : ∀ a b, p (single a b)) :
p f :=
induction₂ f h0 (λ a b f _ _ w, hadd _ _ w (hsingle _ _))
@[simp] lemma add_closure_Union_range_single :
add_submonoid.closure (⋃ a : α, set.range (single a : M → α →₀ M)) = ⊤ :=
top_unique $ λ x hx, finsupp.induction x (add_submonoid.zero_mem _) $
λ a b f ha hb hf, add_submonoid.add_mem _
(add_submonoid.subset_closure $ set.mem_Union.2 ⟨a, set.mem_range_self _⟩) hf
/-- If two additive homomorphisms from `α →₀ M` are equal on each `single a b`, then
they are equal. -/
lemma add_hom_ext [add_zero_class N] ⦃f g : (α →₀ M) →+ N⦄
(H : ∀ x y, f (single x y) = g (single x y)) :
f = g :=
begin
refine add_monoid_hom.eq_of_eq_on_mdense add_closure_Union_range_single (λ f hf, _),
simp only [set.mem_Union, set.mem_range] at hf,
rcases hf with ⟨x, y, rfl⟩,
apply H
end
/-- If two additive homomorphisms from `α →₀ M` are equal on each `single a b`, then
they are equal.
We formulate this using equality of `add_monoid_hom`s so that `ext` tactic can apply a type-specific
extensionality lemma after this one. E.g., if the fiber `M` is `ℕ` or `ℤ`, then it suffices to
verify `f (single a 1) = g (single a 1)`. -/
@[ext] lemma add_hom_ext' [add_zero_class N] ⦃f g : (α →₀ M) →+ N⦄
(H : ∀ x, f.comp (single_add_hom x) = g.comp (single_add_hom x)) :
f = g :=
add_hom_ext $ λ x, add_monoid_hom.congr_fun (H x)
lemma mul_hom_ext [mul_one_class N] ⦃f g : multiplicative (α →₀ M) →* N⦄
(H : ∀ x y, f (multiplicative.of_add $ single x y) = g (multiplicative.of_add $ single x y)) :
f = g :=
monoid_hom.ext $ add_monoid_hom.congr_fun $
@add_hom_ext α M (additive N) _ _ f.to_additive'' g.to_additive'' H
@[ext] lemma mul_hom_ext' [mul_one_class N] {f g : multiplicative (α →₀ M) →* N}
(H : ∀ x, f.comp (single_add_hom x).to_multiplicative =
g.comp (single_add_hom x).to_multiplicative) :
f = g :=
mul_hom_ext $ λ x, monoid_hom.congr_fun (H x)
lemma map_range_add [add_zero_class N]
{f : M → N} {hf : f 0 = 0} (hf' : ∀ x y, f (x + y) = f x + f y) (v₁ v₂ : α →₀ M) :
map_range f hf (v₁ + v₂) = map_range f hf v₁ + map_range f hf v₂ :=
ext $ λ a, by simp only [hf', add_apply, map_range_apply]
end add_zero_class
section add_monoid
variables [add_monoid M]
instance : add_monoid (α →₀ M) :=
{ add_monoid .
zero := 0,
add := (+),
add_assoc := assume ⟨s, f, hf⟩ ⟨t, g, hg⟩ ⟨u, h, hh⟩, ext $ assume a, add_assoc _ _ _,
.. finsupp.add_zero_class }
end add_monoid
end finsupp
@[to_additive]
lemma mul_equiv.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P]
(h : N ≃* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) :=
h.map_prod _ _
@[to_additive]
lemma monoid_hom.map_finsupp_prod [has_zero M] [comm_monoid N] [comm_monoid P]
(h : N →* P) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod (λ a b, h (g a b)) :=
h.map_prod _ _
lemma ring_hom.map_finsupp_sum [has_zero M] [semiring R] [semiring S]
(h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.sum g) = f.sum (λ a b, h (g a b)) :=
h.map_sum _ _
lemma ring_hom.map_finsupp_prod [has_zero M] [comm_semiring R] [comm_semiring S]
(h : R →+* S) (f : α →₀ M) (g : α → M → R) : h (f.prod g) = f.prod (λ a b, h (g a b)) :=
h.map_prod _ _
@[to_additive]
lemma monoid_hom.coe_finsupp_prod [has_zero β] [monoid N] [comm_monoid P]
(f : α →₀ β) (g : α → β → N →* P) :
⇑(f.prod g) = f.prod (λ i fi, g i fi) :=
monoid_hom.coe_prod _ _
@[simp, to_additive]
lemma monoid_hom.finsupp_prod_apply [has_zero β] [monoid N] [comm_monoid P]
(f : α →₀ β) (g : α → β → N →* P) (x : N) :
f.prod g x = f.prod (λ i fi, g i fi x) :=
monoid_hom.finset_prod_apply _ _ _
namespace finsupp
section nat_sub
instance nat_sub : has_sub (α →₀ ℕ) := ⟨zip_with (λ m n, m - n) (nat.sub_zero 0)⟩
@[simp] lemma coe_nat_sub (g₁ g₂ : α →₀ ℕ) : ⇑(g₁ - g₂) = g₁ - g₂ := rfl
lemma nat_sub_apply (g₁ g₂ : α →₀ ℕ) (a : α) : (g₁ - g₂) a = g₁ a - g₂ a := rfl
@[simp] lemma single_sub {a : α} {n₁ n₂ : ℕ} : single a (n₁ - n₂) = single a n₁ - single a n₂ :=
begin
ext f,
by_cases h : (a = f),
{ rw [h, nat_sub_apply, single_eq_same, single_eq_same, single_eq_same] },
rw [nat_sub_apply, single_eq_of_ne h, single_eq_of_ne h, single_eq_of_ne h]
end
-- These next two lemmas are used in developing
-- the partial derivative on `mv_polynomial`.
lemma sub_single_one_add {a : α} {u u' : α →₀ ℕ} (h : u a ≠ 0) :
u - single a 1 + u' = u + u' - single a 1 :=
begin
ext b,
rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply],
by_cases h : a = b,
{ rw [←h, single_eq_same], cases (u a), { contradiction }, { simp }, },
{ simp [h], }
end
lemma add_sub_single_one {a : α} {u u' : α →₀ ℕ} (h : u' a ≠ 0) :
u + (u' - single a 1) = u + u' - single a 1 :=
begin
ext b,
rw [add_apply, nat_sub_apply, nat_sub_apply, add_apply],
by_cases h : a = b,
{ rw [←h, single_eq_same], cases (u' a), { contradiction }, { simp }, },
{ simp [h], }
end
@[simp] lemma nat_zero_sub (f : α →₀ ℕ) : 0 - f = 0 := ext $ λ x, nat.zero_sub _
end nat_sub
instance [add_comm_monoid M] : add_comm_monoid (α →₀ M) :=
{ add_comm := assume ⟨s, f, _⟩ ⟨t, g, _⟩, ext $ assume a, add_comm _ _,
.. finsupp.add_monoid }
instance [add_group G] : has_sub (α →₀ G) := ⟨zip_with has_sub.sub (sub_zero _)⟩
instance [add_group G] : add_group (α →₀ G) :=
{ neg := map_range (has_neg.neg) neg_zero,
sub := has_sub.sub,
sub_eq_add_neg := λ x y, ext (λ i, sub_eq_add_neg _ _),
add_left_neg := assume ⟨s, f, _⟩, ext $ assume x, add_left_neg _,
.. finsupp.add_monoid }
instance [add_comm_group G] : add_comm_group (α →₀ G) :=
{ add_comm := add_comm, ..finsupp.add_group }
lemma single_multiset_sum [add_comm_monoid M] (s : multiset M) (a : α) :
single a s.sum = (s.map (single a)).sum :=
multiset.induction_on s single_zero $ λ a s ih,
by rw [multiset.sum_cons, single_add, ih, multiset.map_cons, multiset.sum_cons]
lemma single_finset_sum [add_comm_monoid M] (s : finset ι) (f : ι → M) (a : α) :
single a (∑ b in s, f b) = ∑ b in s, single a (f b) :=
begin
transitivity,
apply single_multiset_sum,
rw [multiset.map_map],
refl
end
lemma single_sum [has_zero M] [add_comm_monoid N] (s : ι →₀ M) (f : ι → M → N) (a : α) :
single a (s.sum f) = s.sum (λd c, single a (f d c)) :=
single_finset_sum _ _ _
@[to_additive]
lemma prod_neg_index [add_group G] [comm_monoid M] {g : α →₀ G} {h : α → G → M}
(h0 : ∀a, h a 0 = 1) :
(-g).prod h = g.prod (λa b, h a (- b)) :=
prod_map_range_index h0
@[simp] lemma coe_neg [add_group G] (g : α →₀ G) : ⇑(-g) = -g := rfl
lemma neg_apply [add_group G] (g : α →₀ G) (a : α) : (- g) a = - g a := rfl
@[simp] lemma coe_sub [add_group G] (g₁ g₂ : α →₀ G) : ⇑(g₁ - g₂) = g₁ - g₂ := rfl
lemma sub_apply [add_group G] (g₁ g₂ : α →₀ G) (a : α) : (g₁ - g₂) a = g₁ a - g₂ a := rfl
@[simp] lemma support_neg [add_group G] {f : α →₀ G} : support (-f) = support f :=
finset.subset.antisymm
support_map_range
(calc support f = support (- (- f)) : congr_arg support (neg_neg _).symm
... ⊆ support (- f) : support_map_range)
@[simp] lemma sum_apply [has_zero M] [add_comm_monoid N]
{f : α →₀ M} {g : α → M → β →₀ N} {a₂ : β} :
(f.sum g) a₂ = f.sum (λa₁ b, g a₁ b a₂) :=
(apply_add_hom a₂ : (β →₀ N) →+ _).map_sum _ _
lemma support_sum [decidable_eq β] [has_zero M] [add_comm_monoid N]
{f : α →₀ M} {g : α → M → (β →₀ N)} :
(f.sum g).support ⊆ f.support.bUnion (λa, (g a (f a)).support) :=
have ∀ c, f.sum (λ a b, g a b c) ≠ 0 → (∃ a, f a ≠ 0 ∧ ¬ (g a (f a)) c = 0),
from assume a₁ h,
let ⟨a, ha, ne⟩ := finset.exists_ne_zero_of_sum_ne_zero h in
⟨a, mem_support_iff.mp ha, ne⟩,
by simpa only [finset.subset_iff, mem_support_iff, finset.mem_bUnion, sum_apply, exists_prop]
@[simp] lemma sum_zero [has_zero M] [add_comm_monoid N] {f : α →₀ M} :
f.sum (λa b, (0 : N)) = 0 :=
finset.sum_const_zero
@[simp, to_additive]
lemma prod_mul [has_zero M] [comm_monoid N] {f : α →₀ M} {h₁ h₂ : α → M → N} :
f.prod (λa b, h₁ a b * h₂ a b) = f.prod h₁ * f.prod h₂ :=
finset.prod_mul_distrib
@[simp, to_additive]
lemma prod_inv [has_zero M] [comm_group G] {f : α →₀ M}
{h : α → M → G} : f.prod (λa b, (h a b)⁻¹) = (f.prod h)⁻¹ :=
(((monoid_hom.id G)⁻¹).map_prod _ _).symm
@[simp] lemma sum_sub [has_zero M] [add_comm_group G] {f : α →₀ M}
{h₁ h₂ : α → M → G} :
f.sum (λa b, h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ :=
finset.sum_sub_distrib
@[to_additive]
lemma prod_add_index [add_comm_monoid M] [comm_monoid N] {f g : α →₀ M}
{h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) :
(f + g).prod h = f.prod h * g.prod h :=
have hf : f.prod h = ∏ a in f.support ∪ g.support, h a (f a),
from f.prod_of_support_subset (subset_union_left _ _) _ $ λ a ha, h_zero a,
have hg : g.prod h = ∏ a in f.support ∪ g.support, h a (g a),
from g.prod_of_support_subset (subset_union_right _ _) _ $ λ a ha, h_zero a,
have hfg : (f + g).prod h = ∏ a in f.support ∪ g.support, h a ((f + g) a),
from (f + g).prod_of_support_subset support_add _ $ λ a ha, h_zero a,
by simp only [*, add_apply, prod_mul_distrib]
@[simp]
lemma sum_add_index' [add_comm_monoid M] [add_comm_monoid N] {f g : α →₀ M} (h : α → M →+ N) :
(f + g).sum (λ x, h x) = f.sum (λ x, h x) + g.sum (λ x, h x) :=
sum_add_index (λ a, (h a).map_zero) (λ a, (h a).map_add)
@[simp]
lemma prod_add_index' [add_comm_monoid M] [comm_monoid N] {f g : α →₀ M}
(h : α → multiplicative M →* N) :
(f + g).prod (λ a b, h a (multiplicative.of_add b)) =
f.prod (λ a b, h a (multiplicative.of_add b)) * g.prod (λ a b, h a (multiplicative.of_add b)) :=
prod_add_index (λ a, (h a).map_one) (λ a, (h a).map_mul)
/-- The canonical isomorphism between families of additive monoid homomorphisms `α → (M →+ N)`
and monoid homomorphisms `(α →₀ M) →+ N`. -/
def lift_add_hom [add_comm_monoid M] [add_comm_monoid N] : (α → M →+ N) ≃+ ((α →₀ M) →+ N) :=
{ to_fun := λ F,
{ to_fun := λ f, f.sum (λ x, F x),
map_zero' := finset.sum_empty,
map_add' := λ _ _, sum_add_index (λ x, (F x).map_zero) (λ x, (F x).map_add) },
inv_fun := λ F x, F.comp $ single_add_hom x,
left_inv := λ F, by { ext, simp },
right_inv := λ F, by { ext, simp },
map_add' := λ F G, by { ext, simp } }
@[simp] lemma lift_add_hom_apply [add_comm_monoid M] [add_comm_monoid N]
(F : α → M →+ N) (f : α →₀ M) :
lift_add_hom F f = f.sum (λ x, F x) :=
rfl
@[simp] lemma lift_add_hom_symm_apply [add_comm_monoid M] [add_comm_monoid N]
(F : (α →₀ M) →+ N) (x : α) :
lift_add_hom.symm F x = F.comp (single_add_hom x) :=
rfl
lemma lift_add_hom_symm_apply_apply [add_comm_monoid M] [add_comm_monoid N]
(F : (α →₀ M) →+ N) (x : α) (y : M) :
lift_add_hom.symm F x y = F (single x y) :=
rfl
@[simp] lemma lift_add_hom_single_add_hom [add_comm_monoid M] :
lift_add_hom (single_add_hom : α → M →+ α →₀ M) = add_monoid_hom.id _ :=
lift_add_hom.to_equiv.apply_eq_iff_eq_symm_apply.2 rfl
@[simp] lemma sum_single [add_comm_monoid M] (f : α →₀ M) :
f.sum single = f :=
add_monoid_hom.congr_fun lift_add_hom_single_add_hom f
@[simp] lemma lift_add_hom_apply_single [add_comm_monoid M] [add_comm_monoid N]
(f : α → M →+ N) (a : α) (b : M) :
lift_add_hom f (single a b) = f a b :=
sum_single_index (f a).map_zero
@[simp] lemma lift_add_hom_comp_single [add_comm_monoid M] [add_comm_monoid N] (f : α → M →+ N)
(a : α) :
(lift_add_hom f).comp (single_add_hom a) = f a :=
add_monoid_hom.ext $ λ b, lift_add_hom_apply_single f a b
lemma comp_lift_add_hom [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P]
(g : N →+ P) (f : α → M →+ N) :
g.comp (lift_add_hom f) = lift_add_hom (λ a, g.comp (f a)) :=
lift_add_hom.symm_apply_eq.1 $ funext $ λ a,
by rw [lift_add_hom_symm_apply, add_monoid_hom.comp_assoc, lift_add_hom_comp_single]
lemma sum_sub_index [add_comm_group β] [add_comm_group γ] {f g : α →₀ β}
{h : α → β → γ} (h_sub : ∀a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) :
(f - g).sum h = f.sum h - g.sum h :=
(lift_add_hom (λ a, add_monoid_hom.of_map_sub (h a) (h_sub a))).map_sub f g
@[to_additive]
lemma prod_emb_domain [has_zero M] [comm_monoid N] {v : α →₀ M} {f : α ↪ β} {g : β → M → N} :
(v.emb_domain f).prod g = v.prod (λ a b, g (f a) b) :=
begin
rw [prod, prod, support_emb_domain, finset.prod_map],
simp_rw emb_domain_apply,
end
@[to_additive]
lemma prod_finset_sum_index [add_comm_monoid M] [comm_monoid N]
{s : finset ι} {g : ι → α →₀ M}
{h : α → M → N} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) :
∏ i in s, (g i).prod h = (∑ i in s, g i).prod h :=
finset.induction_on s rfl $ λ a s has ih,
by rw [prod_insert has, ih, sum_insert has, prod_add_index h_zero h_add]
@[to_additive]
lemma prod_sum_index
[add_comm_monoid M] [add_comm_monoid N] [comm_monoid P]
{f : α →₀ M} {g : α → M → β →₀ N}
{h : β → N → P} (h_zero : ∀a, h a 0 = 1) (h_add : ∀a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) :
(f.sum g).prod h = f.prod (λa b, (g a b).prod h) :=
(prod_finset_sum_index h_zero h_add).symm
lemma multiset_sum_sum_index
[add_comm_monoid M] [add_comm_monoid N]
(f : multiset (α →₀ M)) (h : α → M → N)
(h₀ : ∀a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : M), h a (b₁ + b₂) = h a b₁ + h a b₂) :
(f.sum.sum h) = (f.map $ λg:α →₀ M, g.sum h).sum :=
multiset.induction_on f rfl $ assume a s ih,
by rw [multiset.sum_cons, multiset.map_cons, multiset.sum_cons, sum_add_index h₀ h₁, ih]
lemma support_sum_eq_bUnion {α : Type*} {ι : Type*} {M : Type*} [add_comm_monoid M]
{g : ι → α →₀ M} (s : finset ι) (h : ∀ i₁ i₂, i₁ ≠ i₂ → disjoint (g i₁).support (g i₂).support) :
(∑ i in s, g i).support = s.bUnion (λ i, (g i).support) :=
begin
apply finset.induction_on s,
{ simp },
{ intros i s hi,
simp only [hi, sum_insert, not_false_iff, bUnion_insert],
intro hs,
rw [finsupp.support_add_eq, hs],
rw [hs],
intros x hx,
simp only [mem_bUnion, exists_prop, inf_eq_inter, ne.def, mem_inter] at hx,
obtain ⟨hxi, j, hj, hxj⟩ := hx,
have hn : i ≠ j := λ H, hi (H.symm ▸ hj),
apply h _ _ hn,
simp [hxi, hxj] }
end
lemma multiset_map_sum [has_zero M] {f : α →₀ M} {m : β → γ} {h : α → M → multiset β} :
multiset.map m (f.sum h) = f.sum (λa b, (h a b).map m) :=
(f.support.sum_hom _).symm
lemma multiset_sum_sum [has_zero M] [add_comm_monoid N] {f : α →₀ M} {h : α → M → multiset N} :
multiset.sum (f.sum h) = f.sum (λa b, multiset.sum (h a b)) :=
(f.support.sum_hom multiset.sum).symm
section map_range
section zero_hom
variables [has_zero M] [has_zero N] [has_zero P]
/-- Composition with a fixed zero-preserving homomorphism is itself an zero-preserving homomorphism
on functions. -/
@[simps]
def map_range.zero_hom (f : zero_hom M N) : zero_hom (α →₀ M) (α →₀ N) :=
{ to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)),
map_zero' := map_range_zero }
@[simp]
lemma map_range.zero_hom_id :
map_range.zero_hom (zero_hom.id M) = zero_hom.id (α →₀ M) := zero_hom.ext map_range_id
lemma map_range.zero_hom_comp (f : zero_hom N P) (f₂ : zero_hom M N) :
(map_range.zero_hom (f.comp f₂) : zero_hom (α →₀ _) _) =
(map_range.zero_hom f).comp (map_range.zero_hom f₂) :=
zero_hom.ext $ map_range_comp _ _ _ _ _
end zero_hom
section add_monoid_hom
variables [add_comm_monoid M] [add_comm_monoid N] [add_comm_monoid P]
/--
Composition with a fixed additive homomorphism is itself an additive homomorphism on functions.
-/
@[simps]
def map_range.add_monoid_hom (f : M →+ N) : (α →₀ M) →+ (α →₀ N) :=
{ to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)),
map_zero' := map_range_zero,
map_add' := λ a b, map_range_add f.map_add _ _ }
@[simp]
lemma map_range.add_monoid_hom_id :
map_range.add_monoid_hom (add_monoid_hom.id M) = add_monoid_hom.id (α →₀ M) :=
add_monoid_hom.ext map_range_id
lemma map_range.add_monoid_hom_comp (f : N →+ P) (f₂ : M →+ N) :
(map_range.add_monoid_hom (f.comp f₂) : (α →₀ _) →+ _) =
(map_range.add_monoid_hom f).comp (map_range.add_monoid_hom f₂) :=
add_monoid_hom.ext $ map_range_comp _ _ _ _ _
lemma map_range_multiset_sum (f : M →+ N) (m : multiset (α →₀ M)) :
map_range f f.map_zero m.sum = (m.map $ λx, map_range f f.map_zero x).sum :=
(map_range.add_monoid_hom f : (α →₀ _) →+ _).map_multiset_sum _
lemma map_range_finset_sum (f : M →+ N) (s : finset ι) (g : ι → (α →₀ M)) :
map_range f f.map_zero (∑ x in s, g x) = ∑ x in s, map_range f f.map_zero (g x) :=
(map_range.add_monoid_hom f : (α →₀ _) →+ _).map_sum _ _
/-- `finsupp.map_range.add_monoid_hom` as an equiv. -/
@[simps apply]
def map_range.add_equiv (f : M ≃+ N) : (α →₀ M) ≃+ (α →₀ N) :=
{ to_fun := (map_range f f.map_zero : (α →₀ M) → (α →₀ N)),
inv_fun := (map_range f.symm f.symm.map_zero : (α →₀ N) → (α →₀ M)),
left_inv := λ x, begin
rw ←map_range_comp _ _ _ _; simp_rw add_equiv.symm_comp_self,
{ exact map_range_id _ },
{ refl },
end,
right_inv := λ x, begin
rw ←map_range_comp _ _ _ _; simp_rw add_equiv.self_comp_symm,
{ exact map_range_id _ },
{ refl },
end,
..(map_range.add_monoid_hom f.to_add_monoid_hom) }
@[simp]
lemma map_range.add_equiv_refl :
map_range.add_equiv (add_equiv.refl M) = add_equiv.refl (α →₀ M) :=
add_equiv.ext map_range_id
lemma map_range.add_equiv_trans (f : M ≃+ N) (f₂ : N ≃+ P) :
(map_range.add_equiv (f.trans f₂) : (α →₀ _) ≃+ _) =
(map_range.add_equiv f).trans (map_range.add_equiv f₂) :=
add_equiv.ext $ map_range_comp _ _ _ _ _
lemma map_range.add_equiv_symm (f : M ≃+ N) :
((map_range.add_equiv f).symm : (α →₀ _) ≃+ _) = map_range.add_equiv f.symm :=
add_equiv.ext $ λ x, rfl
end add_monoid_hom
end map_range
/-! ### Declarations about `map_domain` -/
section map_domain
variables [add_comm_monoid M] {v v₁ v₂ : α →₀ M}
/-- Given `f : α → β` and `v : α →₀ M`, `map_domain f v : β →₀ M`
is the finitely supported function whose value at `a : β` is the sum
of `v x` over all `x` such that `f x = a`. -/
def map_domain (f : α → β) (v : α →₀ M) : β →₀ M :=
v.sum $ λa, single (f a)
lemma map_domain_apply {f : α → β} (hf : function.injective f) (x : α →₀ M) (a : α) :
map_domain f x (f a) = x a :=
begin
rw [map_domain, sum_apply, sum, finset.sum_eq_single a, single_eq_same],
{ assume b _ hba, exact single_eq_of_ne (hf.ne hba) },
{ assume h, rw [not_mem_support_iff.1 h, single_zero, zero_apply] }
end
lemma map_domain_notin_range {f : α → β} (x : α →₀ M) (a : β) (h : a ∉ set.range f) :
map_domain f x a = 0 :=
begin
rw [map_domain, sum_apply, sum],
exact finset.sum_eq_zero
(assume a' h', single_eq_of_ne $ assume eq, h $ eq ▸ set.mem_range_self _)
end
@[simp]
lemma map_domain_id : map_domain id v = v :=
sum_single _
lemma map_domain_comp {f : α → β} {g : β → γ} :
map_domain (g ∘ f) v = map_domain g (map_domain f v) :=
begin
refine ((sum_sum_index _ _).trans _).symm,
{ intros, exact single_zero },
{ intros, exact single_add },
refine sum_congr rfl (λ _ _, sum_single_index _),
{ exact single_zero }
end
@[simp]
lemma map_domain_single {f : α → β} {a : α} {b : M} : map_domain f (single a b) = single (f a) b :=
sum_single_index single_zero
@[simp] lemma map_domain_zero {f : α → β} : map_domain f (0 : α →₀ M) = (0 : β →₀ M) :=
sum_zero_index
lemma map_domain_congr {f g : α → β} (h : ∀x∈v.support, f x = g x) :
v.map_domain f = v.map_domain g :=
finset.sum_congr rfl $ λ _ H, by simp only [h _ H]
lemma map_domain_add {f : α → β} : map_domain f (v₁ + v₂) = map_domain f v₁ + map_domain f v₂ :=
sum_add_index (λ _, single_zero) (λ _ _ _, single_add)
@[simp] lemma map_domain_equiv_apply {f : α ≃ β} (x : α →₀ M) (a : β) :
map_domain f x a = x (f.symm a) :=
begin
conv_lhs { rw ←f.apply_symm_apply a },
exact map_domain_apply f.injective _ _,
end
/-- `finsupp.map_domain` is an `add_monoid_hom`. -/
@[simps]
def map_domain.add_monoid_hom (f : α → β) : (α →₀ M) →+ (β →₀ M) :=
{ to_fun := map_domain f,
map_zero' := map_domain_zero,
map_add' := λ _ _, map_domain_add}
@[simp]
lemma map_domain.add_monoid_hom_id : map_domain.add_monoid_hom id = add_monoid_hom.id (α →₀ M) :=
add_monoid_hom.ext $ λ _, map_domain_id
lemma map_domain.add_monoid_hom_comp (f : β → γ) (g : α → β) :
(map_domain.add_monoid_hom (f ∘ g) : (α →₀ M) →+ (γ →₀ M)) =
(map_domain.add_monoid_hom f).comp (map_domain.add_monoid_hom g) :=
add_monoid_hom.ext $ λ _, map_domain_comp
lemma map_domain_finset_sum {f : α → β} {s : finset ι} {v : ι → α →₀ M} :
map_domain f (∑ i in s, v i) = ∑ i in s, map_domain f (v i) :=
(map_domain.add_monoid_hom f : (α →₀ M) →+ β →₀ M).map_sum _ _
lemma map_domain_sum [has_zero N] {f : α → β} {s : α →₀ N} {v : α → N → α →₀ M} :
map_domain f (s.sum v) = s.sum (λa b, map_domain f (v a b)) :=
(map_domain.add_monoid_hom f : (α →₀ M) →+ β →₀ M).map_finsupp_sum _ _
lemma map_domain_support [decidable_eq β] {f : α → β} {s : α →₀ M} :
(s.map_domain f).support ⊆ s.support.image f :=
finset.subset.trans support_sum $
finset.subset.trans (finset.bUnion_mono $ assume a ha, support_single_subset) $
by rw [finset.bUnion_singleton]; exact subset.refl _
@[to_additive]
lemma prod_map_domain_index [comm_monoid N] {f : α → β} {s : α →₀ M}
{h : β → M → N} (h_zero : ∀b, h b 0 = 1) (h_add : ∀b m₁ m₂, h b (m₁ + m₂) = h b m₁ * h b m₂) :
(map_domain f s).prod h = s.prod (λa m, h (f a) m) :=
(prod_sum_index h_zero h_add).trans $ prod_congr rfl $ λ _ _, prod_single_index (h_zero _)
/--
A version of `sum_map_domain_index` that takes a bundled `add_monoid_hom`,
rather than separate linearity hypotheses.
-/
-- Note that in `prod_map_domain_index`, `M` is still an additive monoid,
-- so there is no analogous version in terms of `monoid_hom`.
@[simp]
lemma sum_map_domain_index_add_monoid_hom [add_comm_monoid N] {f : α → β}
{s : α →₀ M} (h : β → M →+ N) :
(map_domain f s).sum (λ b m, h b m) = s.sum (λ a m, h (f a) m) :=
@sum_map_domain_index _ _ _ _ _ _ _ _
(λ b m, h b m)
(λ b, (h b).map_zero)
(λ b m₁ m₂, (h b).map_add _ _)
lemma emb_domain_eq_map_domain (f : α ↪ β) (v : α →₀ M) :
emb_domain f v = map_domain f v :=
begin
ext a,
by_cases a ∈ set.range f,
{ rcases h with ⟨a, rfl⟩,
rw [map_domain_apply f.injective, emb_domain_apply] },
{ rw [map_domain_notin_range, emb_domain_notin_range]; assumption }
end
@[to_additive]
lemma prod_map_domain_index_inj [comm_monoid N] {f : α → β} {s : α →₀ M}
{h : β → M → N} (hf : function.injective f) :
(s.map_domain f).prod h = s.prod (λa b, h (f a) b) :=
by rw [←function.embedding.coe_fn_mk f hf, ←emb_domain_eq_map_domain, prod_emb_domain]
lemma map_domain_injective {f : α → β} (hf : function.injective f) :
function.injective (map_domain f : (α →₀ M) → (β →₀ M)) :=
begin
assume v₁ v₂ eq, ext a,
have : map_domain f v₁ (f a) = map_domain f v₂ (f a), { rw eq },
rwa [map_domain_apply hf, map_domain_apply hf] at this,
end
lemma map_domain.add_monoid_hom_comp_map_range [add_comm_monoid N] (f : α → β) (g : M →+ N) :
(map_domain.add_monoid_hom f).comp (map_range.add_monoid_hom g) =
(map_range.add_monoid_hom g).comp (map_domain.add_monoid_hom f) :=
by { ext, simp }
/-- When `g` preserves addition, `map_range` and `map_domain` commute. -/
lemma map_domain_map_range [add_comm_monoid N] (f : α → β) (v : α →₀ M) (g : M → N)
(h0 : g 0 = 0) (hadd : ∀ x y, g (x + y) = g x + g y) :
map_domain f (map_range g h0 v) = map_range g h0 (map_domain f v) :=
let g' : M →+ N := { to_fun := g, map_zero' := h0, map_add' := hadd} in
add_monoid_hom.congr_fun (map_domain.add_monoid_hom_comp_map_range f g') v
end map_domain
/-! ### Declarations about `comap_domain` -/
section comap_domain
/-- Given `f : α → β`, `l : β →₀ M` and a proof `hf` that `f` is injective on
the preimage of `l.support`, `comap_domain f l hf` is the finitely supported function
from `α` to `M` given by composing `l` with `f`. -/
def comap_domain [has_zero M] (f : α → β) (l : β →₀ M) (hf : set.inj_on f (f ⁻¹' ↑l.support)) :
α →₀ M :=
{ support := l.support.preimage f hf,
to_fun := (λ a, l (f a)),
mem_support_to_fun :=
begin
intros a,
simp only [finset.mem_def.symm, finset.mem_preimage],
exact l.mem_support_to_fun (f a),
end }
@[simp]
lemma comap_domain_apply [has_zero M] (f : α → β) (l : β →₀ M)
(hf : set.inj_on f (f ⁻¹' ↑l.support)) (a : α) :
comap_domain f l hf a = l (f a) :=
rfl
lemma sum_comap_domain [has_zero M] [add_comm_monoid N]
(f : α → β) (l : β →₀ M) (g : β → M → N)
(hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) :
(comap_domain f l hf.inj_on).sum (g ∘ f) = l.sum g :=
begin
simp only [sum, comap_domain_apply, (∘)],
simp [comap_domain, finset.sum_preimage_of_bij f _ _ (λ x, g x (l x))],
end
lemma eq_zero_of_comap_domain_eq_zero [add_comm_monoid M]
(f : α → β) (l : β →₀ M) (hf : set.bij_on f (f ⁻¹' ↑l.support) ↑l.support) :
comap_domain f l hf.inj_on = 0 → l = 0 :=
begin
rw [← support_eq_empty, ← support_eq_empty, comap_domain],
simp only [finset.ext_iff, finset.not_mem_empty, iff_false, mem_preimage],
assume h a ha,
cases hf.2.2 ha with b hb,
exact h b (hb.2.symm ▸ ha)
end
lemma map_domain_comap_domain [add_comm_monoid M] (f : α → β) (l : β →₀ M)
(hf : function.injective f) (hl : ↑l.support ⊆ set.range f):
map_domain f (comap_domain f l (hf.inj_on _)) = l :=
begin
ext a,
by_cases h_cases: a ∈ set.range f,
{ rcases set.mem_range.1 h_cases with ⟨b, hb⟩,
rw [hb.symm, map_domain_apply hf, comap_domain_apply] },
{ rw map_domain_notin_range _ _ h_cases,
by_contra h_contr,
apply h_cases (hl $ finset.mem_coe.2 $ mem_support_iff.2 $ λ h, h_contr h.symm) }
end
end comap_domain
/-! ### Declarations about `filter` -/
section filter
section has_zero
variables [has_zero M] (p : α → Prop) (f : α →₀ M)
/-- `filter p f` is the function which is `f a` if `p a` is true and 0 otherwise. -/
def filter (p : α → Prop) (f : α →₀ M) : α →₀ M :=
{ to_fun := λ a, if p a then f a else 0,
support := f.support.filter (λ a, p a),
mem_support_to_fun := λ a, by split_ifs; { simp only [h, mem_filter, mem_support_iff], tauto } }
lemma filter_apply (a : α) [D : decidable (p a)] : f.filter p a = if p a then f a else 0 :=
by rw subsingleton.elim D; refl
lemma filter_eq_indicator : ⇑(f.filter p) = set.indicator {x | p x} f := rfl
@[simp] lemma filter_apply_pos {a : α} (h : p a) : f.filter p a = f a :=
if_pos h
@[simp] lemma filter_apply_neg {a : α} (h : ¬ p a) : f.filter p a = 0 :=
if_neg h
@[simp] lemma support_filter [D : decidable_pred p] : (f.filter p).support = f.support.filter p :=
by rw subsingleton.elim D; refl
lemma filter_zero : (0 : α →₀ M).filter p = 0 :=
by rw [← support_eq_empty, support_filter, support_zero, finset.filter_empty]
@[simp] lemma filter_single_of_pos
{a : α} {b : M} (h : p a) : (single a b).filter p = single a b :=
coe_fn_injective $ by simp [filter_eq_indicator, set.subset_def, mem_support_single, h]
@[simp] lemma filter_single_of_neg
{a : α} {b : M} (h : ¬ p a) : (single a b).filter p = 0 :=
ext $ by simp [filter_eq_indicator, single_apply_eq_zero, @imp.swap (p _), h]
end has_zero
lemma filter_pos_add_filter_neg [add_zero_class M] (f : α →₀ M) (p : α → Prop) :
f.filter p + f.filter (λa, ¬ p a) = f :=
coe_fn_injective $ set.indicator_self_add_compl {x | p x} f
end filter
/-! ### Declarations about `frange` -/
section frange
variables [has_zero M]
/-- `frange f` is the image of `f` on the support of `f`. -/
def frange (f : α →₀ M) : finset M := finset.image f f.support
theorem mem_frange {f : α →₀ M} {y : M} :
y ∈ f.frange ↔ y ≠ 0 ∧ ∃ x, f x = y :=
finset.mem_image.trans
⟨λ ⟨x, hx1, hx2⟩, ⟨hx2 ▸ mem_support_iff.1 hx1, x, hx2⟩,
λ ⟨hy, x, hx⟩, ⟨x, mem_support_iff.2 (hx.symm ▸ hy), hx⟩⟩
theorem zero_not_mem_frange {f : α →₀ M} : (0:M) ∉ f.frange :=
λ H, (mem_frange.1 H).1 rfl
theorem frange_single {x : α} {y : M} : frange (single x y) ⊆ {y} :=
λ r hr, let ⟨t, ht1, ht2⟩ := mem_frange.1 hr in ht2 ▸
(by rw single_apply at ht2 ⊢; split_ifs at ht2 ⊢; [exact finset.mem_singleton_self _, cc])
end frange
/-! ### Declarations about `subtype_domain` -/
section subtype_domain
section zero
variables [has_zero M] {p : α → Prop}
/-- `subtype_domain p f` is the restriction of the finitely supported function
`f` to the subtype `p`. -/
def subtype_domain (p : α → Prop) (f : α →₀ M) : (subtype p →₀ M) :=
⟨f.support.subtype p, f ∘ coe, λ a, by simp only [mem_subtype, mem_support_iff]⟩
@[simp] lemma support_subtype_domain [D : decidable_pred p] {f : α →₀ M} :
(subtype_domain p f).support = f.support.subtype p :=
by rw subsingleton.elim D; refl
@[simp] lemma subtype_domain_apply {a : subtype p} {v : α →₀ M} :
(subtype_domain p v) a = v (a.val) :=
rfl
@[simp] lemma subtype_domain_zero : subtype_domain p (0 : α →₀ M) = 0 :=
rfl
lemma subtype_domain_eq_zero_iff' {f : α →₀ M} :
f.subtype_domain p = 0 ↔ ∀ x, p x → f x = 0 :=
by simp_rw [← support_eq_empty, support_subtype_domain, subtype_eq_empty, not_mem_support_iff]
lemma subtype_domain_eq_zero_iff {f : α →₀ M} (hf : ∀ x ∈ f.support , p x) :
f.subtype_domain p = 0 ↔ f = 0 :=
subtype_domain_eq_zero_iff'.trans ⟨λ H, ext $ λ x,
if hx : p x then H x hx else not_mem_support_iff.1 $ mt (hf x) hx, λ H x _, by simp [H]⟩
@[to_additive]
lemma prod_subtype_domain_index [comm_monoid N] {v : α →₀ M}
{h : α → M → N} (hp : ∀x∈v.support, p x) :
(v.subtype_domain p).prod (λa b, h a b) = v.prod h :=
prod_bij (λp _, p.val)
(λ _, mem_subtype.1)
(λ _ _, rfl)
(λ _ _ _ _, subtype.eq)
(λ b hb, ⟨⟨b, hp b hb⟩, mem_subtype.2 hb, rfl⟩)
end zero
section add_zero_class
variables [add_zero_class M] {p : α → Prop} {v v' : α →₀ M}
@[simp] lemma subtype_domain_add {v v' : α →₀ M} :
(v + v').subtype_domain p = v.subtype_domain p + v'.subtype_domain p :=
ext $ λ _, rfl
instance subtype_domain.is_add_monoid_hom :
is_add_monoid_hom (subtype_domain p : (α →₀ M) → subtype p →₀ M) :=
{ map_add := λ _ _, subtype_domain_add, map_zero := subtype_domain_zero }
/-- `finsupp.filter` as an `add_monoid_hom`. -/
def filter_add_hom (p : α → Prop) : (α →₀ M) →+ (α →₀ M) :=
{ to_fun := filter p,
map_zero' := filter_zero p,
map_add' := λ f g, coe_fn_injective $ set.indicator_add {x | p x} f g }
@[simp] lemma filter_add {v v' : α →₀ M} : (v + v').filter p = v.filter p + v'.filter p :=
(filter_add_hom p).map_add v v'
end add_zero_class
section comm_monoid
variables [add_comm_monoid M] {p : α → Prop}
lemma subtype_domain_sum {s : finset ι} {h : ι → α →₀ M} :
(∑ c in s, h c).subtype_domain p = ∑ c in s, (h c).subtype_domain p :=
eq.symm (s.sum_hom _)
lemma subtype_domain_finsupp_sum [has_zero N] {s : β →₀ N} {h : β → N → α →₀ M} :
(s.sum h).subtype_domain p = s.sum (λc d, (h c d).subtype_domain p) :=
subtype_domain_sum
lemma filter_sum (s : finset ι) (f : ι → α →₀ M) :
(∑ a in s, f a).filter p = ∑ a in s, filter p (f a) :=
(filter_add_hom p : (α →₀ M) →+ _).map_sum f s
lemma filter_eq_sum (p : α → Prop) [D : decidable_pred p] (f : α →₀ M) :
f.filter p = ∑ i in f.support.filter p, single i (f i) :=
(f.filter p).sum_single.symm.trans $ finset.sum_congr (by rw subsingleton.elim D; refl) $
λ x hx, by rw [filter_apply_pos _ _ (mem_filter.1 hx).2]
end comm_monoid
section group
variables [add_group G] {p : α → Prop} {v v' : α →₀ G}
@[simp] lemma subtype_domain_neg : (- v).subtype_domain p = - v.subtype_domain p :=
ext $ λ _, rfl
@[simp] lemma subtype_domain_sub :
(v - v').subtype_domain p = v.subtype_domain p - v'.subtype_domain p :=
ext $ λ _, rfl
end group
end subtype_domain
/-! ### Declarations relating `finsupp` to `multiset` -/
section multiset
/-- Given `f : α →₀ ℕ`, `f.to_multiset` is the multiset with multiplicities given by the values of
`f` on the elements of `α`. We define this function as an `add_equiv`. -/
def to_multiset : (α →₀ ℕ) ≃+ multiset α :=
{ to_fun := λ f, f.sum (λa n, n •ℕ {a}),
inv_fun := λ s, ⟨s.to_finset, λ a, s.count a, λ a, by simp⟩,
left_inv := λ f, ext $ λ a,
suffices (if f a = 0 then 0 else f a) = f a,
by simpa [finsupp.sum, multiset.count_sum', multiset.count_cons],
by split_ifs with h; [rw h, refl],
right_inv := λ s, by simp [finsupp.sum],
map_add' := λ f g, sum_add_index (λ a, zero_nsmul _) (λ a, add_nsmul _) }
lemma to_multiset_zero : (0 : α →₀ ℕ).to_multiset = 0 :=
rfl
lemma to_multiset_add (m n : α →₀ ℕ) :
(m + n).to_multiset = m.to_multiset + n.to_multiset :=
to_multiset.map_add m n
lemma to_multiset_apply (f : α →₀ ℕ) : f.to_multiset = f.sum (λ a n, n •ℕ {a}) := rfl
@[simp] lemma to_multiset_single (a : α) (n : ℕ) : to_multiset (single a n) = n •ℕ {a} :=
by rw [to_multiset_apply, sum_single_index]; apply zero_nsmul
lemma to_multiset_sum {ι : Type*} {f : ι → α →₀ ℕ} (s : finset ι) :
finsupp.to_multiset (∑ i in s, f i) = ∑ i in s, finsupp.to_multiset (f i) :=
begin
apply finset.induction_on s,
{ simp },
{ intros i s hi,
simp [hi] }
end
lemma to_multiset_sum_single {ι : Type*} (s : finset ι) (n : ℕ) :
finsupp.to_multiset (∑ i in s, single i n) = n •ℕ s.val :=
by simp_rw [to_multiset_sum, finsupp.to_multiset_single, multiset.singleton_eq_singleton,
sum_nsmul, sum_multiset_singleton]
lemma card_to_multiset (f : α →₀ ℕ) : f.to_multiset.card = f.sum (λa, id) :=
by simp [to_multiset_apply, add_monoid_hom.map_finsupp_sum, function.id_def]
lemma to_multiset_map (f : α →₀ ℕ) (g : α → β) :
f.to_multiset.map g = (f.map_domain g).to_multiset :=
begin
refine f.induction _ _,
{ rw [to_multiset_zero, multiset.map_zero, map_domain_zero, to_multiset_zero] },
{ assume a n f _ _ ih,
rw [to_multiset_add, multiset.map_add, ih, map_domain_add, map_domain_single,
to_multiset_single, to_multiset_add, to_multiset_single,
is_add_monoid_hom.map_nsmul (multiset.map g)],
refl }
end
@[simp] lemma prod_to_multiset [comm_monoid M] (f : M →₀ ℕ) :
f.to_multiset.prod = f.prod (λa n, a ^ n) :=
begin
refine f.induction _ _,
{ rw [to_multiset_zero, multiset.prod_zero, finsupp.prod_zero_index] },
{ assume a n f _ _ ih,
rw [to_multiset_add, multiset.prod_add, ih, to_multiset_single, finsupp.prod_add_index,
finsupp.prod_single_index, multiset.prod_nsmul, multiset.singleton_eq_singleton,
multiset.prod_singleton],
{ exact pow_zero a },
{ exact pow_zero },
{ exact pow_add } }
end
@[simp] lemma to_finset_to_multiset [decidable_eq α] (f : α →₀ ℕ) :
f.to_multiset.to_finset = f.support :=
begin
refine f.induction _ _,
{ rw [to_multiset_zero, multiset.to_finset_zero, support_zero] },
{ assume a n f ha hn ih,
rw [to_multiset_add, multiset.to_finset_add, ih, to_multiset_single, support_add_eq,
support_single_ne_zero hn, multiset.to_finset_nsmul _ _ hn,
multiset.singleton_eq_singleton, multiset.to_finset_cons, multiset.to_finset_zero],
refl,
refine disjoint.mono_left support_single_subset _,
rwa [finset.singleton_disjoint] }
end
@[simp] lemma count_to_multiset [decidable_eq α] (f : α →₀ ℕ) (a : α) :
f.to_multiset.count a = f a :=
calc f.to_multiset.count a = f.sum (λx n, (n •ℕ {x} : multiset α).count a) :
(f.support.sum_hom $ multiset.count a).symm
... = f.sum (λx n, n * ({x} : multiset α).count a) : by simp only [multiset.count_nsmul]
... = f.sum (λx n, n * (x ::ₘ 0 : multiset α).count a) : rfl
... = f a * (a ::ₘ 0 : multiset α).count a : sum_eq_single _
(λ a' _ H, by simp only [multiset.count_cons_of_ne (ne.symm H), multiset.count_zero, mul_zero])
(λ H, by simp only [not_mem_support_iff.1 H, zero_mul])
... = f a : by simp only [multiset.count_singleton, mul_one]
lemma mem_support_multiset_sum [add_comm_monoid M]
{s : multiset (α →₀ M)} (a : α) :
a ∈ s.sum.support → ∃f∈s, a ∈ (f : α →₀ M).support :=
multiset.induction_on s false.elim
begin
assume f s ih ha,
by_cases a ∈ f.support,
{ exact ⟨f, multiset.mem_cons_self _ _, h⟩ },
{ simp only [multiset.sum_cons, mem_support_iff, add_apply,
not_mem_support_iff.1 h, zero_add] at ha,
rcases ih (mem_support_iff.2 ha) with ⟨f', h₀, h₁⟩,
exact ⟨f', multiset.mem_cons_of_mem h₀, h₁⟩ }
end
lemma mem_support_finset_sum [add_comm_monoid M]
{s : finset ι} {h : ι → α →₀ M} (a : α) (ha : a ∈ (∑ c in s, h c).support) :
∃ c ∈ s, a ∈ (h c).support :=
let ⟨f, hf, hfa⟩ := mem_support_multiset_sum a ha in
let ⟨c, hc, eq⟩ := multiset.mem_map.1 hf in
⟨c, hc, eq.symm ▸ hfa⟩
@[simp] lemma mem_to_multiset (f : α →₀ ℕ) (i : α) :
i ∈ f.to_multiset ↔ i ∈ f.support :=
by rw [← multiset.count_ne_zero, finsupp.count_to_multiset, finsupp.mem_support_iff]
end multiset
/-! ### Declarations about `curry` and `uncurry` -/
section curry_uncurry
variables [add_comm_monoid M] [add_comm_monoid N]
/-- Given a finitely supported function `f` from a product type `α × β` to `γ`,
`curry f` is the "curried" finitely supported function from `α` to the type of
finitely supported functions from `β` to `γ`. -/
protected def curry (f : (α × β) →₀ M) : α →₀ (β →₀ M) :=
f.sum $ λp c, single p.1 (single p.2 c)
lemma sum_curry_index (f : (α × β) →₀ M) (g : α → β → M → N)
(hg₀ : ∀ a b, g a b 0 = 0) (hg₁ : ∀a b c₀ c₁, g a b (c₀ + c₁) = g a b c₀ + g a b c₁) :
f.curry.sum (λa f, f.sum (g a)) = f.sum (λp c, g p.1 p.2 c) :=
begin
rw [finsupp.curry],
transitivity,
{ exact sum_sum_index (assume a, sum_zero_index)
(assume a b₀ b₁, sum_add_index (assume a, hg₀ _ _) (assume c d₀ d₁, hg₁ _ _ _ _)) },
congr, funext p c,
transitivity,
{ exact sum_single_index sum_zero_index },
exact sum_single_index (hg₀ _ _)
end
/-- Given a finitely supported function `f` from `α` to the type of
finitely supported functions from `β` to `M`,
`uncurry f` is the "uncurried" finitely supported function from `α × β` to `M`. -/
protected def uncurry (f : α →₀ (β →₀ M)) : (α × β) →₀ M :=
f.sum $ λa g, g.sum $ λb c, single (a, b) c
/-- `finsupp_prod_equiv` defines the `equiv` between `((α × β) →₀ M)` and `(α →₀ (β →₀ M))` given by
currying and uncurrying. -/
def finsupp_prod_equiv : ((α × β) →₀ M) ≃ (α →₀ (β →₀ M)) :=
by refine ⟨finsupp.curry, finsupp.uncurry, λ f, _, λ f, _⟩; simp only [
finsupp.curry, finsupp.uncurry, sum_sum_index, sum_zero_index, sum_add_index,
sum_single_index, single_zero, single_add, eq_self_iff_true, forall_true_iff,
forall_3_true_iff, prod.mk.eta, (single_sum _ _ _).symm, sum_single]
lemma filter_curry (f : α × β →₀ M) (p : α → Prop) :
(f.filter (λa:α×β, p a.1)).curry = f.curry.filter p :=
begin
rw [finsupp.curry, finsupp.curry, finsupp.sum, finsupp.sum, filter_sum, support_filter,
sum_filter],
refine finset.sum_congr rfl _,
rintros ⟨a₁, a₂⟩ ha,
dsimp only,
split_ifs,
{ rw [filter_apply_pos, filter_single_of_pos]; exact h },
{ rwa [filter_single_of_neg] }
end
lemma support_curry [decidable_eq α] (f : α × β →₀ M) :
f.curry.support ⊆ f.support.image prod.fst :=
begin
rw ← finset.bUnion_singleton,
refine finset.subset.trans support_sum _,
refine finset.bUnion_mono (assume a _, support_single_subset)
end
end curry_uncurry
section sum
/-- `finsupp.sum_elim f g` maps `inl x` to `f x` and `inr y` to `g y`. -/
def sum_elim {α β γ : Type*} [has_zero γ]
(f : α →₀ γ) (g : β →₀ γ) : α ⊕ β →₀ γ :=
on_finset
((f.support.map ⟨_, sum.inl_injective⟩) ∪ g.support.map ⟨_, sum.inr_injective⟩)
(sum.elim f g)
(λ ab h, by { cases ab with a b; simp only [sum.elim_inl, sum.elim_inr] at h; simpa })
@[simp] lemma coe_sum_elim {α β γ : Type*} [has_zero γ]
(f : α →₀ γ) (g : β →₀ γ) : ⇑(sum_elim f g) = sum.elim f g := rfl
lemma sum_elim_apply {α β γ : Type*} [has_zero γ]
(f : α →₀ γ) (g : β →₀ γ) (x : α ⊕ β) : sum_elim f g x = sum.elim f g x := rfl
lemma sum_elim_inl {α β γ : Type*} [has_zero γ]
(f : α →₀ γ) (g : β →₀ γ) (x : α) : sum_elim f g (sum.inl x) = f x := rfl
lemma sum_elim_inr {α β γ : Type*} [has_zero γ]
(f : α →₀ γ) (g : β →₀ γ) (x : β) : sum_elim f g (sum.inr x) = g x := rfl
/-- The equivalence between `(α ⊕ β) →₀ γ` and `(α →₀ γ) × (β →₀ γ)`.
This is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/
@[simps apply symm_apply]
def sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ] :
((α ⊕ β) →₀ γ) ≃ (α →₀ γ) × (β →₀ γ) :=
{ to_fun := λ f,
⟨f.comap_domain sum.inl (sum.inl_injective.inj_on _),
f.comap_domain sum.inr (sum.inr_injective.inj_on _)⟩,
inv_fun := λ fg, sum_elim fg.1 fg.2,
left_inv := λ f, by { ext ab, cases ab with a b; simp },
right_inv := λ fg, by { ext; simp } }
lemma fst_sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ]
(f : (α ⊕ β) →₀ γ) (x : α) :
(sum_finsupp_equiv_prod_finsupp f).1 x = f (sum.inl x) :=
rfl
lemma snd_sum_finsupp_equiv_prod_finsupp {α β γ : Type*} [has_zero γ]
(f : (α ⊕ β) →₀ γ) (y : β) :
(sum_finsupp_equiv_prod_finsupp f).2 y = f (sum.inr y) :=
rfl
lemma sum_finsupp_equiv_prod_finsupp_symm_inl {α β γ : Type*} [has_zero γ]
(fg : (α →₀ γ) × (β →₀ γ)) (x : α) :
(sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x :=
rfl
lemma sum_finsupp_equiv_prod_finsupp_symm_inr {α β γ : Type*} [has_zero γ]
(fg : (α →₀ γ) × (β →₀ γ)) (y : β) :
(sum_finsupp_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y :=
rfl
variables [add_monoid M]
/-- The additive equivalence between `(α ⊕ β) →₀ M` and `(α →₀ M) × (β →₀ M)`.
This is the `finsupp` version of `equiv.sum_arrow_equiv_prod_arrow`. -/
@[simps apply symm_apply] def sum_finsupp_add_equiv_prod_finsupp {α β : Type*} :
((α ⊕ β) →₀ M) ≃+ (α →₀ M) × (β →₀ M) :=
{ map_add' :=
by { intros, ext;
simp only [equiv.to_fun_as_coe, prod.fst_add, prod.snd_add, add_apply,
snd_sum_finsupp_equiv_prod_finsupp, fst_sum_finsupp_equiv_prod_finsupp] },
.. sum_finsupp_equiv_prod_finsupp }
lemma fst_sum_finsupp_add_equiv_prod_finsupp {α β : Type*}
(f : (α ⊕ β) →₀ M) (x : α) :
(sum_finsupp_add_equiv_prod_finsupp f).1 x = f (sum.inl x) :=
rfl
lemma snd_sum_finsupp_add_equiv_prod_finsupp {α β : Type*}
(f : (α ⊕ β) →₀ M) (y : β) :
(sum_finsupp_add_equiv_prod_finsupp f).2 y = f (sum.inr y) :=
rfl
lemma sum_finsupp_add_equiv_prod_finsupp_symm_inl {α β : Type*}
(fg : (α →₀ M) × (β →₀ M)) (x : α) :
(sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inl x) = fg.1 x :=
rfl
lemma sum_finsupp_add_equiv_prod_finsupp_symm_inr {α β : Type*}
(fg : (α →₀ M) × (β →₀ M)) (y : β) :
(sum_finsupp_add_equiv_prod_finsupp.symm fg) (sum.inr y) = fg.2 y :=
rfl
end sum
section
variables [group G] [mul_action G α] [add_comm_monoid M]
/--
Scalar multiplication by a group element g,
given by precomposition with the action of g⁻¹ on the domain.
-/
def comap_has_scalar : has_scalar G (α →₀ M) :=
{ smul := λ g f, f.comap_domain (λ a, g⁻¹ • a)
(λ a a' m m' h, by simpa [←mul_smul] using (congr_arg (λ a, g • a) h)) }
local attribute [instance] comap_has_scalar
/--
Scalar multiplication by a group element,
given by precomposition with the action of g⁻¹ on the domain,
is multiplicative in g.
-/
def comap_mul_action : mul_action G (α →₀ M) :=
{ one_smul := λ f, by { ext, dsimp [(•)], simp, },
mul_smul := λ g g' f, by { ext, dsimp [(•)], simp [mul_smul], }, }
local attribute [instance] comap_mul_action
/--
Scalar multiplication by a group element,
given by precomposition with the action of g⁻¹ on the domain,
is additive in the second argument.
-/
def comap_distrib_mul_action :
distrib_mul_action G (α →₀ M) :=
{ smul_zero := λ g, by { ext, dsimp [(•)], simp, },
smul_add := λ g f f', by { ext, dsimp [(•)], simp, }, }
/--
Scalar multiplication by a group element on finitely supported functions on a group,
given by precomposition with the action of g⁻¹. -/
def comap_distrib_mul_action_self :
distrib_mul_action G (G →₀ M) :=
@finsupp.comap_distrib_mul_action G M G _ (monoid.to_mul_action G) _
@[simp]
lemma comap_smul_single (g : G) (a : α) (b : M) :
g • single a b = single (g • a) b :=
begin
ext a',
dsimp [(•)],
by_cases h : g • a = a',
{ subst h, simp [←mul_smul], },
{ simp [single_eq_of_ne h], rw [single_eq_of_ne],
rintro rfl, simpa [←mul_smul] using h, }
end
@[simp]
lemma comap_smul_apply (g : G) (f : α →₀ M) (a : α) :
(g • f) a = f (g⁻¹ • a) := rfl
end
section
instance [semiring R] [add_comm_monoid M] [semimodule R M] : has_scalar R (α →₀ M) :=
⟨λa v, v.map_range ((•) a) (smul_zero _)⟩
/-!
Throughout this section, some `semiring` arguments are specified with `{}` instead of `[]`.
See note [implicit instance arguments].
-/
@[simp] lemma coe_smul {_ : semiring R} [add_comm_monoid M] [semimodule R M]
(b : R) (v : α →₀ M) : ⇑(b • v) = b • v := rfl
lemma smul_apply {_ : semiring R} [add_comm_monoid M] [semimodule R M]
(b : R) (v : α →₀ M) (a : α) : (b • v) a = b • (v a) := rfl
variables (α M)
instance [semiring R] [add_comm_monoid M] [semimodule R M] : semimodule R (α →₀ M) :=
{ smul := (•),
smul_add := λ a x y, ext $ λ _, smul_add _ _ _,
add_smul := λ a x y, ext $ λ _, add_smul _ _ _,
one_smul := λ x, ext $ λ _, one_smul _ _,
mul_smul := λ r s x, ext $ λ _, mul_smul _ _ _,
zero_smul := λ x, ext $ λ _, zero_smul _ _,
smul_zero := λ x, ext $ λ _, smul_zero _ }
instance [semiring R] [semiring S] [add_comm_monoid M] [semimodule R M] [semimodule S M]
[has_scalar R S] [is_scalar_tower R S M] :
is_scalar_tower R S (α →₀ M) :=
{ smul_assoc := λ r s a, ext $ λ _, smul_assoc _ _ _ }
instance [semiring R] [semiring S] [add_comm_monoid M] [semimodule R M] [semimodule S M]
[smul_comm_class R S M] :
smul_comm_class R S (α →₀ M) :=
{ smul_comm := λ r s a, ext $ λ _, smul_comm _ _ _ }
variables {α M} {R}
lemma support_smul {_ : semiring R} [add_comm_monoid M] [semimodule R M] {b : R} {g : α →₀ M} :
(b • g).support ⊆ g.support :=
λ a, by simp only [smul_apply, mem_support_iff, ne.def]; exact mt (λ h, h.symm ▸ smul_zero _)
section
variables {p : α → Prop}
@[simp] lemma filter_smul {_ : semiring R} [add_comm_monoid M] [semimodule R M]
{b : R} {v : α →₀ M} : (b • v).filter p = b • v.filter p :=
coe_fn_injective $ set.indicator_smul {x | p x} b v
end
lemma map_domain_smul {_ : semiring R} [add_comm_monoid M] [semimodule R M]
{f : α → β} (b : R) (v : α →₀ M) : map_domain f (b • v) = b • map_domain f v :=
begin
change map_domain f (map_range _ _ _) = map_range _ _ _,
apply finsupp.induction v, { simp only [map_domain_zero, map_range_zero] },
intros a b v' hv₁ hv₂ IH,
rw [map_range_add, map_domain_add, IH, map_domain_add, map_range_add,
map_range_single, map_domain_single, map_domain_single, map_range_single];
apply smul_add
end
@[simp] lemma smul_single {_ : semiring R} [add_comm_monoid M] [semimodule R M]
(c : R) (a : α) (b : M) : c • finsupp.single a b = finsupp.single a (c • b) :=
map_range_single
@[simp] lemma smul_single' {_ : semiring R}
(c : R) (a : α) (b : R) : c • finsupp.single a b = finsupp.single a (c * b) :=
smul_single _ _ _
lemma map_range_smul {_ : semiring R} [add_comm_monoid M] [semimodule R M]
[add_comm_monoid N] [semimodule R N]
{f : M → N} {hf : f 0 = 0} (c : R) (v : α →₀ M) (hsmul : ∀ x, f (c • x) = c • f x) :
map_range f hf (c • v) = c • map_range f hf v :=
begin
erw ←map_range_comp,
have : (f ∘ (•) c) = ((•) c ∘ f) := funext hsmul,
simp_rw this,
apply map_range_comp,
rw [function.comp_apply, smul_zero, hf],
end
lemma smul_single_one [semiring R] (a : α) (b : R) : b • single a 1 = single a b :=
by rw [smul_single, smul_eq_mul, mul_one]
end
lemma sum_smul_index [semiring R] [add_comm_monoid M] {g : α →₀ R} {b : R} {h : α → R → M}
(h0 : ∀i, h i 0 = 0) : (b • g).sum h = g.sum (λi a, h i (b * a)) :=
finsupp.sum_map_range_index h0
lemma sum_smul_index' [semiring R] [add_comm_monoid M] [semimodule R M] [add_comm_monoid N]
{g : α →₀ M} {b : R} {h : α → M → N} (h0 : ∀i, h i 0 = 0) :
(b • g).sum h = g.sum (λi c, h i (b • c)) :=
finsupp.sum_map_range_index h0
/-- A version of `finsupp.sum_smul_index'` for bundled additive maps. -/
lemma sum_smul_index_add_monoid_hom
[semiring R] [add_comm_monoid M] [add_comm_monoid N] [semimodule R M]
{g : α →₀ M} {b : R} {h : α → M →+ N} :
(b • g).sum (λ a, h a) = g.sum (λ i c, h i (b • c)) :=
sum_map_range_index (λ i, (h i).map_zero)
instance [semiring R] [add_comm_monoid M] [semimodule R M] {ι : Type*}
[no_zero_smul_divisors R M] : no_zero_smul_divisors R (ι →₀ M) :=
⟨λ c f h, or_iff_not_imp_left.mpr (λ hc, finsupp.ext
(λ i, (smul_eq_zero.mp (finsupp.ext_iff.mp h i)).resolve_left hc))⟩
section
variables [semiring R] [semiring S]
lemma sum_mul (b : S) (s : α →₀ R) {f : α → R → S} :
(s.sum f) * b = s.sum (λ a c, (f a c) * b) :=
by simp only [finsupp.sum, finset.sum_mul]
lemma mul_sum (b : S) (s : α →₀ R) {f : α → R → S} :
b * (s.sum f) = s.sum (λ a c, b * (f a c)) :=
by simp only [finsupp.sum, finset.mul_sum]
instance unique_of_right [subsingleton R] : unique (α →₀ R) :=
{ uniq := λ l, ext $ λ i, subsingleton.elim _ _,
.. finsupp.inhabited }
end
/-- Given an `add_comm_monoid M` and `s : set α`, `restrict_support_equiv s M` is the `equiv`
between the subtype of finitely supported functions with support contained in `s` and
the type of finitely supported functions from `s`. -/
def restrict_support_equiv (s : set α) (M : Type*) [add_comm_monoid M] :
{f : α →₀ M // ↑f.support ⊆ s } ≃ (s →₀ M) :=
begin
refine ⟨λf, subtype_domain (λx, x ∈ s) f.1, λ f, ⟨f.map_domain subtype.val, _⟩, _, _⟩,
{ refine set.subset.trans (finset.coe_subset.2 map_domain_support) _,
rw [finset.coe_image, set.image_subset_iff],
exact assume x hx, x.2 },
{ rintros ⟨f, hf⟩,
apply subtype.eq,
ext a,
dsimp only,
refine classical.by_cases (assume h : a ∈ set.range (subtype.val : s → α), _) (assume h, _),
{ rcases h with ⟨x, rfl⟩,
rw [map_domain_apply subtype.val_injective, subtype_domain_apply] },
{ convert map_domain_notin_range _ _ h,
rw [← not_mem_support_iff],
refine mt _ h,
exact assume ha, ⟨⟨a, hf ha⟩, rfl⟩ } },
{ assume f,
ext ⟨a, ha⟩,
dsimp only,
rw [subtype_domain_apply, map_domain_apply subtype.val_injective] }
end
/-- Given `add_comm_monoid M` and `e : α ≃ β`, `dom_congr e` is the corresponding `equiv` between
`α →₀ M` and `β →₀ M`. -/
@[simps apply]
protected def dom_congr [add_comm_monoid M] (e : α ≃ β) : (α →₀ M) ≃+ (β →₀ M) :=
{ to_fun := map_domain e,
inv_fun := map_domain e.symm,
left_inv := begin
assume v,
simp only [map_domain_comp.symm, (∘), equiv.symm_apply_apply],
exact map_domain_id
end,
right_inv := begin
assume v,
simp only [map_domain_comp.symm, (∘), equiv.apply_symm_apply],
exact map_domain_id
end,
map_add' := λ a b, map_domain_add, }
@[simp] lemma dom_congr_refl [add_comm_monoid M] :
finsupp.dom_congr (equiv.refl α) = add_equiv.refl (α →₀ M) :=
add_equiv.ext $ λ _, map_domain_id
@[simp] lemma dom_congr_symm [add_comm_monoid M] (e : α ≃ β) :
(finsupp.dom_congr e).symm = (finsupp.dom_congr e.symm : (β →₀ M) ≃+ (α →₀ M)):=
add_equiv.ext $ λ _, rfl
@[simp] lemma dom_congr_trans [add_comm_monoid M] (e : α ≃ β) (f : β ≃ γ) :
(finsupp.dom_congr e).trans (finsupp.dom_congr f) =
(finsupp.dom_congr (e.trans f) : (α →₀ M) ≃+ _) :=
add_equiv.ext $ λ _, map_domain_comp.symm
end finsupp
namespace finsupp
/-! ### Declarations about sigma types -/
section sigma
variables {αs : ι → Type*} [has_zero M] (l : (Σ i, αs i) →₀ M)
/-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `M` and
an index element `i : ι`, `split l i` is the `i`th component of `l`,
a finitely supported function from `as i` to `M`. -/
def split (i : ι) : αs i →₀ M :=
l.comap_domain (sigma.mk i) (λ x1 x2 _ _ hx, heq_iff_eq.1 (sigma.mk.inj hx).2)
lemma split_apply (i : ι) (x : αs i) : split l i x = l ⟨i, x⟩ :=
begin
dunfold split,
rw comap_domain_apply
end
/-- Given `l`, a finitely supported function from the sigma type `Σ (i : ι), αs i` to `β`,
`split_support l` is the finset of indices in `ι` that appear in the support of `l`. -/
def split_support : finset ι := l.support.image sigma.fst
lemma mem_split_support_iff_nonzero (i : ι) :
i ∈ split_support l ↔ split l i ≠ 0 :=
begin
rw [split_support, mem_image, ne.def, ← support_eq_empty, ← ne.def,
← finset.nonempty_iff_ne_empty, split, comap_domain, finset.nonempty],
simp only [exists_prop, finset.mem_preimage, exists_and_distrib_right, exists_eq_right,
mem_support_iff, sigma.exists, ne.def]
end
/-- Given `l`, a finitely supported function from the sigma type `Σ i, αs i` to `β` and
an `ι`-indexed family `g` of functions from `(αs i →₀ β)` to `γ`, `split_comp` defines a
finitely supported function from the index type `ι` to `γ` given by composing `g i` with
`split l i`. -/
def split_comp [has_zero N] (g : Π i, (αs i →₀ M) → N)
(hg : ∀ i x, x = 0 ↔ g i x = 0) : ι →₀ N :=
{ support := split_support l,
to_fun := λ i, g i (split l i),
mem_support_to_fun :=
begin
intros i,
rw [mem_split_support_iff_nonzero, not_iff_not, hg],
end }
lemma sigma_support : l.support = l.split_support.sigma (λ i, (l.split i).support) :=
by simp only [finset.ext_iff, split_support, split, comap_domain, mem_image,
mem_preimage, sigma.forall, mem_sigma]; tauto
lemma sigma_sum [add_comm_monoid N] (f : (Σ (i : ι), αs i) → M → N) :
l.sum f = ∑ i in split_support l, (split l i).sum (λ (a : αs i) b, f ⟨i, a⟩ b) :=
by simp only [sum, sigma_support, sum_sigma, split_apply]
end sigma
end finsupp
/-! ### Declarations relating `multiset` to `finsupp` -/
namespace multiset
/-- Given a multiset `s`, `s.to_finsupp` returns the finitely supported function on `ℕ` given by
the multiplicities of the elements of `s`. -/
def to_finsupp : multiset α ≃+ (α →₀ ℕ) := finsupp.to_multiset.symm
@[simp] lemma to_finsupp_support [D : decidable_eq α] (s : multiset α) :
s.to_finsupp.support = s.to_finset :=
by rw subsingleton.elim D; refl
@[simp] lemma to_finsupp_apply [D : decidable_eq α] (s : multiset α) (a : α) :
to_finsupp s a = s.count a :=
by rw subsingleton.elim D; refl
lemma to_finsupp_zero : to_finsupp (0 : multiset α) = 0 := add_equiv.map_zero _
lemma to_finsupp_add (s t : multiset α) :
to_finsupp (s + t) = to_finsupp s + to_finsupp t :=
to_finsupp.map_add s t
@[simp] lemma to_finsupp_singleton (a : α) : to_finsupp (a ::ₘ 0) = finsupp.single a 1 :=
finsupp.to_multiset.symm_apply_eq.2 $ by simp
@[simp] lemma to_finsupp_to_multiset (s : multiset α) :
s.to_finsupp.to_multiset = s :=
finsupp.to_multiset.apply_symm_apply s
lemma to_finsupp_eq_iff {s : multiset α} {f : α →₀ ℕ} : s.to_finsupp = f ↔ s = f.to_multiset :=
finsupp.to_multiset.symm_apply_eq
end multiset
@[simp] lemma finsupp.to_multiset_to_finsupp (f : α →₀ ℕ) :
f.to_multiset.to_finsupp = f :=
finsupp.to_multiset.symm_apply_apply f
/-! ### Declarations about order(ed) instances on `finsupp` -/
namespace finsupp
instance [preorder M] [has_zero M] : preorder (α →₀ M) :=
{ le := λ f g, ∀ s, f s ≤ g s,
le_refl := λ f s, le_refl _,
le_trans := λ f g h Hfg Hgh s, le_trans (Hfg s) (Hgh s) }
instance [partial_order M] [has_zero M] : partial_order (α →₀ M) :=
{ le_antisymm := λ f g hfg hgf, ext $ λ s, le_antisymm (hfg s) (hgf s),
.. finsupp.preorder }
instance [ordered_cancel_add_comm_monoid M] : add_left_cancel_semigroup (α →₀ M) :=
{ add_left_cancel := λ a b c h, ext $ λ s,
by { rw ext_iff at h, exact add_left_cancel (h s) },
.. finsupp.add_monoid }
instance [ordered_cancel_add_comm_monoid M] : add_right_cancel_semigroup (α →₀ M) :=
{ add_right_cancel := λ a b c h, ext $ λ s,
by { rw ext_iff at h, exact add_right_cancel (h s) },
.. finsupp.add_monoid }
instance [ordered_cancel_add_comm_monoid M] : ordered_cancel_add_comm_monoid (α →₀ M) :=
{ add_le_add_left := λ a b h c s, add_le_add_left (h s) (c s),
le_of_add_le_add_left := λ a b c h s, le_of_add_le_add_left (h s),
.. finsupp.add_comm_monoid, .. finsupp.partial_order,
.. finsupp.add_left_cancel_semigroup, .. finsupp.add_right_cancel_semigroup }
lemma le_def [preorder M] [has_zero M] {f g : α →₀ M} : f ≤ g ↔ ∀ x, f x ≤ g x := iff.rfl
lemma le_iff [canonically_ordered_add_monoid M] (f g : α →₀ M) :
f ≤ g ↔ ∀ s ∈ f.support, f s ≤ g s :=
⟨λ h s hs, h s,
λ h s, if H : s ∈ f.support then h s H else (not_mem_support_iff.1 H).symm ▸ zero_le (g s)⟩
@[simp] lemma add_eq_zero_iff [canonically_ordered_add_monoid M] (f g : α →₀ M) :
f + g = 0 ↔ f = 0 ∧ g = 0 :=
by simp [ext_iff, forall_and_distrib]
/-- `finsupp.to_multiset` as an order isomorphism. -/
def order_iso_multiset : (α →₀ ℕ) ≃o multiset α :=
{ to_equiv := to_multiset.to_equiv,
map_rel_iff' := λ f g, by simp [multiset.le_iff_count, le_def] }
@[simp] lemma coe_order_iso_multiset : ⇑(@order_iso_multiset α) = to_multiset := rfl
@[simp] lemma coe_order_iso_multiset_symm :
⇑(@order_iso_multiset α).symm = multiset.to_finsupp := rfl
lemma to_multiset_strict_mono : strict_mono (@to_multiset α) :=
order_iso_multiset.strict_mono
lemma sum_id_lt_of_lt (m n : α →₀ ℕ) (h : m < n) :
m.sum (λ _, id) < n.sum (λ _, id) :=
begin
rw [← card_to_multiset, ← card_to_multiset],
apply multiset.card_lt_of_lt,
exact to_multiset_strict_mono h
end
variable (α)
/-- The order on `σ →₀ ℕ` is well-founded.-/
lemma lt_wf : well_founded (@has_lt.lt (α →₀ ℕ) _) :=
subrelation.wf (sum_id_lt_of_lt) $ inv_image.wf _ nat.lt_wf
instance decidable_le : decidable_rel (@has_le.le (α →₀ ℕ) _) :=
λ m n, by rw le_iff; apply_instance
variable {α}
@[simp] lemma nat_add_sub_cancel (f g : α →₀ ℕ) : f + g - g = f :=
ext $ λ a, nat.add_sub_cancel _ _
@[simp] lemma nat_add_sub_cancel_left (f g : α →₀ ℕ) : f + g - f = g :=
ext $ λ a, nat.add_sub_cancel_left _ _
lemma nat_add_sub_of_le {f g : α →₀ ℕ} (h : f ≤ g) : f + (g - f) = g :=
ext $ λ a, nat.add_sub_of_le (h a)
lemma nat_sub_add_cancel {f g : α →₀ ℕ} (h : f ≤ g) : g - f + f = g :=
ext $ λ a, nat.sub_add_cancel (h a)
instance : canonically_ordered_add_monoid (α →₀ ℕ) :=
{ bot := 0,
bot_le := λ f s, zero_le (f s),
le_iff_exists_add := λ f g, ⟨λ H, ⟨g - f, (nat_add_sub_of_le H).symm⟩,
λ ⟨c, hc⟩, hc.symm ▸ λ x, by simp⟩,
.. (infer_instance : ordered_add_comm_monoid (α →₀ ℕ)) }
/-- The `finsupp` counterpart of `multiset.antidiagonal`: the antidiagonal of
`s : α →₀ ℕ` consists of all pairs `(t₁, t₂) : (α →₀ ℕ) × (α →₀ ℕ)` such that `t₁ + t₂ = s`.
The finitely supported function `antidiagonal s` is equal to the multiplicities of these pairs. -/
def antidiagonal (f : α →₀ ℕ) : ((α →₀ ℕ) × (α →₀ ℕ)) →₀ ℕ :=
(f.to_multiset.antidiagonal.map (prod.map multiset.to_finsupp multiset.to_finsupp)).to_finsupp
@[simp] lemma mem_antidiagonal_support {f : α →₀ ℕ} {p : (α →₀ ℕ) × (α →₀ ℕ)} :
p ∈ (antidiagonal f).support ↔ p.1 + p.2 = f :=
begin
rcases p with ⟨p₁, p₂⟩,
simp [antidiagonal, ← and.assoc, ← finsupp.to_multiset.apply_eq_iff_eq]
end
lemma swap_mem_antidiagonal_support {n : α →₀ ℕ} {f : (α →₀ ℕ) × (α →₀ ℕ)} :
f.swap ∈ (antidiagonal n).support ↔ f ∈ (antidiagonal n).support :=
by simp only [mem_antidiagonal_support, add_comm, prod.swap]
lemma antidiagonal_support_filter_fst_eq (f g : α →₀ ℕ)
[D : Π (p : (α →₀ ℕ) × (α →₀ ℕ)), decidable (p.1 = g)] :
(antidiagonal f).support.filter (λ p, p.1 = g) = if g ≤ f then {(g, f - g)} else ∅ :=
begin
ext ⟨a, b⟩,
suffices : a = g → (a + b = f ↔ g ≤ f ∧ b = f - g),
{ simpa [apply_ite ((∈) (a, b)), ← and.assoc, @and.right_comm _ (a = _), and.congr_left_iff] },
unfreezingI {rintro rfl}, split,
{ rintro rfl, exact ⟨le_add_right le_rfl, (nat_add_sub_cancel_left _ _).symm⟩ },
{ rintro ⟨h, rfl⟩, exact nat_add_sub_of_le h }
end
lemma antidiagonal_support_filter_snd_eq (f g : α →₀ ℕ)
[D : Π (p : (α →₀ ℕ) × (α →₀ ℕ)), decidable (p.2 = g)] :
(antidiagonal f).support.filter (λ p, p.2 = g) = if g ≤ f then {(f - g, g)} else ∅ :=
begin
ext ⟨a, b⟩,
suffices : b = g → (a + b = f ↔ g ≤ f ∧ a = f - g),
{ simpa [apply_ite ((∈) (a, b)), ← and.assoc, and.congr_left_iff] },
unfreezingI {rintro rfl}, split,
{ rintro rfl, exact ⟨le_add_left le_rfl, (nat_add_sub_cancel _ _).symm⟩ },
{ rintro ⟨h, rfl⟩, exact nat_sub_add_cancel h }
end
@[simp] lemma antidiagonal_zero : antidiagonal (0 : α →₀ ℕ) = single (0,0) 1 :=
by rw [← multiset.to_finsupp_singleton]; refl
@[to_additive]
lemma prod_antidiagonal_support_swap {M : Type*} [comm_monoid M] (n : α →₀ ℕ)
(f : (α →₀ ℕ) → (α →₀ ℕ) → M) :
∏ p in (antidiagonal n).support, f p.1 p.2 = ∏ p in (antidiagonal n).support, f p.2 p.1 :=
finset.prod_bij (λ p hp, p.swap) (λ p, swap_mem_antidiagonal_support.2) (λ p hp, rfl)
(λ p₁ p₂ _ _ h, prod.swap_injective h)
(λ p hp, ⟨p.swap, swap_mem_antidiagonal_support.2 hp, p.swap_swap.symm⟩)
/-- The set `{m : α →₀ ℕ | m ≤ n}` as a `finset`. -/
def Iic_finset (n : α →₀ ℕ) : finset (α →₀ ℕ) :=
(antidiagonal n).support.image prod.fst
@[simp] lemma mem_Iic_finset {m n : α →₀ ℕ} : m ∈ Iic_finset n ↔ m ≤ n :=
by simp [Iic_finset, le_iff_exists_add, eq_comm]
@[simp] lemma coe_Iic_finset (n : α →₀ ℕ) : ↑(Iic_finset n) = set.Iic n :=
by { ext, simp }
/-- Let `n : α →₀ ℕ` be a finitely supported function.
The set of `m : α →₀ ℕ` that are coordinatewise less than or equal to `n`,
is a finite set. -/
lemma finite_le_nat (n : α →₀ ℕ) : set.finite {m | m ≤ n} :=
by simpa using (Iic_finset n).finite_to_set
/-- Let `n : α →₀ ℕ` be a finitely supported function.
The set of `m : α →₀ ℕ` that are coordinatewise less than or equal to `n`,
but not equal to `n` everywhere, is a finite set. -/
lemma finite_lt_nat (n : α →₀ ℕ) : set.finite {m | m < n} :=
(finite_le_nat n).subset $ λ m, le_of_lt
end finsupp
namespace multiset
lemma to_finsuppstrict_mono : strict_mono (@to_finsupp α) :=
finsupp.order_iso_multiset.symm.strict_mono
end multiset
|
80b60b38b0305eb84c996cdd946766e79a5d5791 | d406927ab5617694ec9ea7001f101b7c9e3d9702 | /src/topology/instances/nnreal.lean | 99e88c45e71755fdbb5244e67f6d0447c476f099 | [
"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 | 9,800 | lean | /-
Copyright (c) 2018 Johan Commelin. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johan Commelin
-/
import topology.algebra.infinite_sum
import topology.algebra.group_with_zero
/-!
# Topology on `ℝ≥0`
The natural topology on `ℝ≥0` (the one induced from `ℝ`), and a basic API.
## Main definitions
Instances for the following typeclasses are defined:
* `topological_space ℝ≥0`
* `topological_semiring ℝ≥0`
* `second_countable_topology ℝ≥0`
* `order_topology ℝ≥0`
* `has_continuous_sub ℝ≥0`
* `has_continuous_inv₀ ℝ≥0` (continuity of `x⁻¹` away from `0`)
* `has_continuous_smul ℝ≥0 α` (whenever `α` has a continuous `mul_action ℝ α`)
Everything is inherited from the corresponding structures on the reals.
## Main statements
Various mathematically trivial lemmas are proved about the compatibility
of limits and sums in `ℝ≥0` and `ℝ`. For example
* `tendsto_coe {f : filter α} {m : α → ℝ≥0} {x : ℝ≥0} :
tendsto (λa, (m a : ℝ)) f (𝓝 (x : ℝ)) ↔ tendsto m f (𝓝 x)`
says that the limit of a filter along a map to `ℝ≥0` is the same in `ℝ` and `ℝ≥0`, and
* `coe_tsum {f : α → ℝ≥0} : ((∑'a, f a) : ℝ) = (∑'a, (f a : ℝ))`
says that says that a sum of elements in `ℝ≥0` is the same in `ℝ` and `ℝ≥0`.
Similarly, some mathematically trivial lemmas about infinite sums are proved,
a few of which rely on the fact that subtraction is continuous.
-/
noncomputable theory
open set topological_space metric filter
open_locale topological_space
namespace nnreal
open_locale nnreal big_operators filter
instance : topological_space ℝ≥0 := infer_instance -- short-circuit type class inference
instance : topological_semiring ℝ≥0 :=
{ continuous_mul :=
(continuous_subtype_val.fst'.mul continuous_subtype_val.snd').subtype_mk _,
continuous_add :=
(continuous_subtype_val.fst'.add continuous_subtype_val.snd').subtype_mk _ }
instance : second_countable_topology ℝ≥0 :=
topological_space.subtype.second_countable_topology _ _
instance : order_topology ℝ≥0 := @order_topology_of_ord_connected _ _ _ _ (Ici 0) _
section coe
variable {α : Type*}
open filter finset
lemma _root_.continuous_real_to_nnreal : continuous real.to_nnreal :=
(continuous_id.max continuous_const).subtype_mk _
lemma continuous_coe : continuous (coe : ℝ≥0 → ℝ) :=
continuous_subtype_val
/-- Embedding of `ℝ≥0` to `ℝ` as a bundled continuous map. -/
@[simps { fully_applied := ff }] def _root_.continuous_map.coe_nnreal_real : C(ℝ≥0, ℝ) :=
⟨coe, continuous_coe⟩
instance continuous_map.can_lift {X : Type*} [topological_space X] :
can_lift C(X, ℝ) C(X, ℝ≥0) continuous_map.coe_nnreal_real.comp (λ f, ∀ x, 0 ≤ f x) :=
{ prf := λ f hf, ⟨⟨λ x, ⟨f x, hf x⟩, f.2.subtype_mk _⟩, fun_like.ext' rfl⟩ }
@[simp, norm_cast] lemma tendsto_coe {f : filter α} {m : α → ℝ≥0} {x : ℝ≥0} :
tendsto (λa, (m a : ℝ)) f (𝓝 (x : ℝ)) ↔ tendsto m f (𝓝 x) :=
tendsto_subtype_rng.symm
lemma tendsto_coe' {f : filter α} [ne_bot f] {m : α → ℝ≥0} {x : ℝ} :
tendsto (λ a, m a : α → ℝ) f (𝓝 x) ↔ ∃ hx : 0 ≤ x, tendsto m f (𝓝 ⟨x, hx⟩) :=
⟨λ h, ⟨ge_of_tendsto' h (λ c, (m c).2), tendsto_coe.1 h⟩, λ ⟨hx, hm⟩, tendsto_coe.2 hm⟩
@[simp] lemma map_coe_at_top : map (coe : ℝ≥0 → ℝ) at_top = at_top :=
map_coe_Ici_at_top 0
lemma comap_coe_at_top : comap (coe : ℝ≥0 → ℝ) at_top = at_top :=
(at_top_Ici_eq 0).symm
@[simp, norm_cast] lemma tendsto_coe_at_top {f : filter α} {m : α → ℝ≥0} :
tendsto (λ a, (m a : ℝ)) f at_top ↔ tendsto m f at_top :=
tendsto_Ici_at_top.symm
lemma _root_.tendsto_real_to_nnreal {f : filter α} {m : α → ℝ} {x : ℝ} (h : tendsto m f (𝓝 x)) :
tendsto (λa, real.to_nnreal (m a)) f (𝓝 (real.to_nnreal x)) :=
(continuous_real_to_nnreal.tendsto _).comp h
lemma _root_.tendsto_real_to_nnreal_at_top : tendsto real.to_nnreal at_top at_top :=
begin
rw ← tendsto_coe_at_top,
apply tendsto_id.congr' _,
filter_upwards [Ici_mem_at_top (0 : ℝ)] with x hx,
simp only [max_eq_left (set.mem_Ici.1 hx), id.def, real.coe_to_nnreal'],
end
lemma nhds_zero : 𝓝 (0 : ℝ≥0) = ⨅a ≠ 0, 𝓟 (Iio a) :=
nhds_bot_order.trans $ by simp [bot_lt_iff_ne_bot]
lemma nhds_zero_basis : (𝓝 (0 : ℝ≥0)).has_basis (λ a : ℝ≥0, 0 < a) (λ a, Iio a) :=
nhds_bot_basis
instance : has_continuous_sub ℝ≥0 :=
⟨((continuous_coe.fst'.sub continuous_coe.snd').max continuous_const).subtype_mk _⟩
instance : has_continuous_inv₀ ℝ≥0 :=
⟨λ x hx, tendsto_coe.1 $ (real.tendsto_inv $ nnreal.coe_ne_zero.2 hx).comp
continuous_coe.continuous_at⟩
instance [topological_space α] [mul_action ℝ α] [has_continuous_smul ℝ α] :
has_continuous_smul ℝ≥0 α :=
{ continuous_smul := (continuous_induced_dom.comp continuous_fst).smul continuous_snd }
@[norm_cast] lemma has_sum_coe {f : α → ℝ≥0} {r : ℝ≥0} :
has_sum (λa, (f a : ℝ)) (r : ℝ) ↔ has_sum f r :=
by simp only [has_sum, coe_sum.symm, tendsto_coe]
lemma has_sum_real_to_nnreal_of_nonneg {f : α → ℝ} (hf_nonneg : ∀ n, 0 ≤ f n) (hf : summable f) :
has_sum (λ n, real.to_nnreal (f n)) (real.to_nnreal (∑' n, f n)) :=
begin
have h_sum : (λ s, ∑ b in s, real.to_nnreal (f b)) = λ s, real.to_nnreal (∑ b in s, f b),
from funext (λ _, (real.to_nnreal_sum_of_nonneg (λ n _, hf_nonneg n)).symm),
simp_rw [has_sum, h_sum],
exact tendsto_real_to_nnreal hf.has_sum,
end
@[norm_cast] lemma summable_coe {f : α → ℝ≥0} : summable (λa, (f a : ℝ)) ↔ summable f :=
begin
split,
exact assume ⟨a, ha⟩, ⟨⟨a, has_sum_le (λa, (f a).2) has_sum_zero ha⟩, has_sum_coe.1 ha⟩,
exact assume ⟨a, ha⟩, ⟨a.1, has_sum_coe.2 ha⟩
end
lemma summable_coe_of_nonneg {f : α → ℝ} (hf₁ : ∀ n, 0 ≤ f n) :
@summable (ℝ≥0) _ _ _ (λ n, ⟨f n, hf₁ n⟩) ↔ summable f :=
begin
lift f to α → ℝ≥0 using hf₁ with f rfl hf₁,
simp only [summable_coe, subtype.coe_eta]
end
open_locale classical
@[norm_cast] lemma coe_tsum {f : α → ℝ≥0} : ↑∑'a, f a = ∑'a, (f a : ℝ) :=
if hf : summable f
then (eq.symm $ (has_sum_coe.2 $ hf.has_sum).tsum_eq)
else by simp [tsum, hf, mt summable_coe.1 hf]
lemma coe_tsum_of_nonneg {f : α → ℝ} (hf₁ : ∀ n, 0 ≤ f n) :
(⟨∑' n, f n, tsum_nonneg hf₁⟩ : ℝ≥0) = (∑' n, ⟨f n, hf₁ n⟩ : ℝ≥0) :=
begin
lift f to α → ℝ≥0 using hf₁ with f rfl hf₁,
simp_rw [← nnreal.coe_tsum, subtype.coe_eta]
end
lemma tsum_mul_left (a : ℝ≥0) (f : α → ℝ≥0) : ∑' x, a * f x = a * ∑' x, f x :=
nnreal.eq $ by simp only [coe_tsum, nnreal.coe_mul, tsum_mul_left]
lemma tsum_mul_right (f : α → ℝ≥0) (a : ℝ≥0) : (∑' x, f x * a) = (∑' x, f x) * a :=
nnreal.eq $ by simp only [coe_tsum, nnreal.coe_mul, tsum_mul_right]
lemma summable_comp_injective {β : Type*} {f : α → ℝ≥0} (hf : summable f)
{i : β → α} (hi : function.injective i) :
summable (f ∘ i) :=
nnreal.summable_coe.1 $
show summable ((coe ∘ f) ∘ i), from (nnreal.summable_coe.2 hf).comp_injective hi
lemma summable_nat_add (f : ℕ → ℝ≥0) (hf : summable f) (k : ℕ) : summable (λ i, f (i + k)) :=
summable_comp_injective hf $ add_left_injective k
lemma summable_nat_add_iff {f : ℕ → ℝ≥0} (k : ℕ) : summable (λ i, f (i + k)) ↔ summable f :=
begin
rw [← summable_coe, ← summable_coe],
exact @summable_nat_add_iff ℝ _ _ _ (λ i, (f i : ℝ)) k,
end
lemma has_sum_nat_add_iff {f : ℕ → ℝ≥0} (k : ℕ) {a : ℝ≥0} :
has_sum (λ n, f (n + k)) a ↔ has_sum f (a + ∑ i in range k, f i) :=
by simp [← has_sum_coe, coe_sum, nnreal.coe_add, ← has_sum_nat_add_iff k]
lemma sum_add_tsum_nat_add {f : ℕ → ℝ≥0} (k : ℕ) (hf : summable f) :
∑' i, f i = (∑ i in range k, f i) + ∑' i, f (i + k) :=
by rw [←nnreal.coe_eq, coe_tsum, nnreal.coe_add, coe_sum, coe_tsum,
sum_add_tsum_nat_add k (nnreal.summable_coe.2 hf)]
lemma infi_real_pos_eq_infi_nnreal_pos [complete_lattice α] {f : ℝ → α} :
(⨅ (n : ℝ) (h : 0 < n), f n) = (⨅ (n : ℝ≥0) (h : 0 < n), f n) :=
le_antisymm (infi_mono' $ λ r, ⟨r, le_rfl⟩) (infi₂_mono' $ λ r hr, ⟨⟨r, hr.le⟩, hr, le_rfl⟩)
end coe
lemma tendsto_cofinite_zero_of_summable {α} {f : α → ℝ≥0} (hf : summable f) :
tendsto f cofinite (𝓝 0) :=
begin
have h_f_coe : f = λ n, real.to_nnreal (f n : ℝ), from funext (λ n, real.to_nnreal_coe.symm),
rw [h_f_coe, ← @real.to_nnreal_coe 0],
exact tendsto_real_to_nnreal ((summable_coe.mpr hf).tendsto_cofinite_zero),
end
lemma tendsto_at_top_zero_of_summable {f : ℕ → ℝ≥0} (hf : summable f) :
tendsto f at_top (𝓝 0) :=
by { rw ←nat.cofinite_eq_at_top, exact tendsto_cofinite_zero_of_summable hf }
/-- The sum over the complement of a finset tends to `0` when the finset grows to cover the whole
space. This does not need a summability assumption, as otherwise all sums are zero. -/
lemma tendsto_tsum_compl_at_top_zero {α : Type*} (f : α → ℝ≥0) :
tendsto (λ (s : finset α), ∑' b : {x // x ∉ s}, f b) at_top (𝓝 0) :=
begin
simp_rw [← tendsto_coe, coe_tsum, nnreal.coe_zero],
exact tendsto_tsum_compl_at_top_zero (λ (a : α), (f a : ℝ))
end
/-- `x ↦ x ^ n` as an order isomorphism of `ℝ≥0`. -/
def pow_order_iso (n : ℕ) (hn : n ≠ 0) : ℝ≥0 ≃o ℝ≥0 :=
strict_mono.order_iso_of_surjective (λ x, x ^ n)
(λ x y h, strict_mono_on_pow hn.bot_lt (zero_le x) (zero_le y) h) $
(continuous_id.pow _).surjective (tendsto_pow_at_top hn) $
by simpa [order_bot.at_bot_eq, pos_iff_ne_zero]
end nnreal
|
a1b933cb88bada9d2ff25f3c730297602f39db80 | d6df40070de62348d40ffe5d02f9c5e18250e863 | /src/apartness_logic.lean | 309117d8cb03cd8c92c37cb35221c1c4dd1e21f4 | [] | no_license | owen-fool/Elementary-Apartness-Spaces | f1ce08f065092c21c7d179cac5f7527f4fea7e46 | 139029bae2c9f40322ee4c75f8f5299b5bf4f2d5 | refs/heads/master | 1,670,498,775,650 | 1,597,845,090,000 | 1,597,845,090,000 | 293,635,048 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,188 | lean | import tactic
import apartness_spaces
open point_set_apartness
open apartness
lemma napart_of_near (X : Type) (w : point_set_apartness_space X) (A : set X) :
∀ x, near X x A → ¬ apart X x A :=
begin
intros x h h',
specialize h A h',
cases h with y h,
cases h with yh Ah,
dsimp at *,
apply apart_to_nin X y A Ah yh,
end
lemma set_prop_in_iff_prop (X : Type) (p : Prop) (x : X) : x ∈ {i : X | p} ↔ p :=
begin
simp,
end
lemma set_prop_nin_iff_not (X : Type) (p : Prop) (x : X) : x ∉ {i : X | p} ↔ ¬ p :=
begin
split,
{
intros h ph,
apply h,
exact ph,
},
{
intros ph h,
apply ph,
exact h,
}
end
lemma doub_neg_of_napart_near_equiv :
(∀ A x, real_near x A ↔ ¬ real_apart x A) → (∀ p : Prop, ¬ ¬ p → p) :=
begin
intros h p,
specialize h {x | p},
intro ph,
have hapart : ∀ x : ℝ, ¬ real_apart x {x : ℝ | p},
{
intros x hx,
apply ph,
rw ← set_prop_nin_iff_not ℝ p x,
exact hx,
},
have hnear : ∀ x : ℝ, real_near x {i : ℝ | p},
{
intro x,
exact (h x).2 (hapart x),
},
specialize hnear 0 {1} (by finish),
cases hnear with y hy,
cases hy with hy1 hy2,
exact (set_prop_in_iff_prop ℝ p y).1 hy1,
end |
66b993b3bbcbff24beeb8814bffcd41267cb4e4d | 491068d2ad28831e7dade8d6dff871c3e49d9431 | /library/data/examples/vector.lean | 84a2848561a4a9912450017974c4d120d14daa14 | [
"Apache-2.0"
] | permissive | davidmueller13/lean | 65a3ed141b4088cd0a268e4de80eb6778b21a0e9 | c626e2e3c6f3771e07c32e82ee5b9e030de5b050 | refs/heads/master | 1,611,278,313,401 | 1,444,021,177,000 | 1,444,021,177,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 13,167 | lean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Floris van Doorn, Leonardo de Moura
This file demonstrates how to encode vectors using indexed inductive families.
In standard library we do not use this approach.
-/
import data.nat data.list data.fin
open nat prod fin
inductive vector (A : Type) : nat → Type :=
| nil {} : vector A zero
| cons : Π {n}, A → vector A n → vector A (succ n)
namespace vector
notation a :: b := cons a b
notation `[` l:(foldr `, ` (h t, cons h t) nil `]`) := l
variables {A B C : Type}
protected definition is_inhabited [instance] [h : inhabited A] : ∀ (n : nat), inhabited (vector A n)
| 0 := inhabited.mk []
| (n+1) := inhabited.mk (inhabited.value h :: inhabited.value (is_inhabited n))
theorem vector0_eq_nil : ∀ (v : vector A 0), v = []
| [] := rfl
definition head : Π {n : nat}, vector A (succ n) → A
| n (a::v) := a
definition tail : Π {n : nat}, vector A (succ n) → vector A n
| n (a::v) := v
theorem head_cons {n : nat} (h : A) (t : vector A n) : head (h :: t) = h :=
rfl
theorem tail_cons {n : nat} (h : A) (t : vector A n) : tail (h :: t) = t :=
rfl
theorem eta : ∀ {n : nat} (v : vector A (succ n)), head v :: tail v = v
| n (a::v) := rfl
definition last : Π {n : nat}, vector A (succ n) → A
| last [a] := a
| last (a::v) := last v
theorem last_singleton (a : A) : last [a] = a :=
rfl
theorem last_cons {n : nat} (a : A) (v : vector A (succ n)) : last (a :: v) = last v :=
rfl
definition const : Π (n : nat), A → vector A n
| 0 a := []
| (succ n) a := a :: const n a
theorem head_const (n : nat) (a : A) : head (const (succ n) a) = a :=
rfl
theorem last_const : ∀ (n : nat) (a : A), last (const (succ n) a) = a
| 0 a := rfl
| (n+1) a := last_const n a
definition nth : Π {n : nat}, vector A n → fin n → A
| ⌞0⌟ [] i := elim0 i
| ⌞n+1⌟ (a :: v) (mk 0 _) := a
| ⌞n+1⌟ (a :: v) (mk (succ i) h) := nth v (mk_pred i h)
lemma nth_zero {n : nat} (a : A) (v : vector A n) (h : 0 < succ n) : nth (a::v) (mk 0 h) = a :=
rfl
lemma nth_succ {n : nat} (a : A) (v : vector A n) (i : nat) (h : succ i < succ n)
: nth (a::v) (mk (succ i) h) = nth v (mk_pred i h) :=
rfl
definition tabulate : Π {n : nat}, (fin n → A) → vector A n
| 0 f := []
| (n+1) f := f (@zero n) :: tabulate (λ i : fin n, f (succ i))
theorem nth_tabulate : ∀ {n : nat} (f : fin n → A) (i : fin n), nth (tabulate f) i = f i
| 0 f i := elim0 i
| (n+1) f (mk 0 h) := by reflexivity
| (n+1) f (mk (succ i) h) :=
begin
change nth (f (@zero n) :: tabulate (λ i : fin n, f (succ i))) (mk (succ i) h) = f (mk (succ i) h),
rewrite nth_succ,
rewrite nth_tabulate
end
definition map (f : A → B) : Π {n : nat}, vector A n → vector B n
| map [] := []
| map (a::v) := f a :: map v
theorem map_nil (f : A → B) : map f [] = [] :=
rfl
theorem map_cons {n : nat} (f : A → B) (h : A) (t : vector A n) : map f (h :: t) = f h :: map f t :=
rfl
theorem nth_map (f : A → B) : ∀ {n : nat} (v : vector A n) (i : fin n), nth (map f v) i = f (nth v i)
| 0 v i := elim0 i
| (succ n) (a :: t) (mk 0 h) := by reflexivity
| (succ n) (a :: t) (mk (succ i) h) := by rewrite [map_cons, *nth_succ, nth_map]
section
open function
theorem map_id : ∀ {n : nat} (v : vector A n), map id v = v
| 0 [] := rfl
| (succ n) (x::xs) := by rewrite [map_cons, map_id]
theorem map_map (g : B → C) (f : A → B) : ∀ {n :nat} (v : vector A n), map g (map f v) = map (g ∘ f) v
| 0 [] := rfl
| (succ n) (a :: l) :=
show (g ∘ f) a :: map g (map f l) = map (g ∘ f) (a :: l),
by rewrite (map_map l)
end
definition map2 (f : A → B → C) : Π {n : nat}, vector A n → vector B n → vector C n
| map2 [] [] := []
| map2 (a::va) (b::vb) := f a b :: map2 va vb
theorem map2_nil (f : A → B → C) : map2 f [] [] = [] :=
rfl
theorem map2_cons {n : nat} (f : A → B → C) (h₁ : A) (h₂ : B) (t₁ : vector A n) (t₂ : vector B n) :
map2 f (h₁ :: t₁) (h₂ :: t₂) = f h₁ h₂ :: map2 f t₁ t₂ :=
rfl
definition append : Π {n m : nat}, vector A n → vector A m → vector A (n ⊕ m)
| 0 m [] w := w
| (succ n) m (a::v) w := a :: (append v w)
theorem append_nil_left {n : nat} (v : vector A n) : append [] v = v :=
rfl
theorem append_cons {n m : nat} (h : A) (t : vector A n) (v : vector A m) :
append (h::t) v = h :: (append t v) :=
rfl
theorem map_append (f : A → B) : ∀ {n m : nat} (v : vector A n) (w : vector A m), map f (append v w) = append (map f v) (map f w)
| 0 m [] w := rfl
| (n+1) m (h :: t) w :=
begin
change (f h :: map f (append t w) = f h :: append (map f t) (map f w)),
rewrite map_append
end
definition unzip : Π {n : nat}, vector (A × B) n → vector A n × vector B n
| unzip [] := ([], [])
| unzip ((a, b) :: v) := (a :: pr₁ (unzip v), b :: pr₂ (unzip v))
theorem unzip_nil : unzip (@nil (A × B)) = ([], []) :=
rfl
theorem unzip_cons {n : nat} (a : A) (b : B) (v : vector (A × B) n) :
unzip ((a, b) :: v) = (a :: pr₁ (unzip v), b :: pr₂ (unzip v)) :=
rfl
definition zip : Π {n : nat}, vector A n → vector B n → vector (A × B) n
| zip [] [] := []
| zip (a::va) (b::vb) := ((a, b) :: zip va vb)
theorem zip_nil_nil : zip (@nil A) (@nil B) = nil :=
rfl
theorem zip_cons_cons {n : nat} (a : A) (b : B) (va : vector A n) (vb : vector B n) :
zip (a::va) (b::vb) = ((a, b) :: zip va vb) :=
rfl
theorem unzip_zip : ∀ {n : nat} (v₁ : vector A n) (v₂ : vector B n), unzip (zip v₁ v₂) = (v₁, v₂)
| 0 [] [] := rfl
| (n+1) (a::va) (b::vb) := calc
unzip (zip (a :: va) (b :: vb))
= (a :: pr₁ (unzip (zip va vb)), b :: pr₂ (unzip (zip va vb))) : rfl
... = (a :: pr₁ (va, vb), b :: pr₂ (va, vb)) : by rewrite unzip_zip
... = (a :: va, b :: vb) : rfl
theorem zip_unzip : ∀ {n : nat} (v : vector (A × B) n), zip (pr₁ (unzip v)) (pr₂ (unzip v)) = v
| 0 [] := rfl
| (n+1) ((a, b) :: v) := calc
zip (pr₁ (unzip ((a, b) :: v))) (pr₂ (unzip ((a, b) :: v)))
= (a, b) :: zip (pr₁ (unzip v)) (pr₂ (unzip v)) : rfl
... = (a, b) :: v : by rewrite zip_unzip
/- Concat -/
definition concat : Π {n : nat}, vector A n → A → vector A (succ n)
| concat [] a := [a]
| concat (b::v) a := b :: concat v a
theorem concat_nil (a : A) : concat [] a = [a] :=
rfl
theorem concat_cons {n : nat} (b : A) (v : vector A n) (a : A) : concat (b :: v) a = b :: concat v a :=
rfl
theorem last_concat : ∀ {n : nat} (v : vector A n) (a : A), last (concat v a) = a
| 0 [] a := rfl
| (n+1) (b::v) a := calc
last (concat (b::v) a) = last (concat v a) : rfl
... = a : last_concat v a
/- Reverse -/
definition reverse : Π {n : nat}, vector A n → vector A n
| 0 [] := []
| (n+1) (x :: xs) := concat (reverse xs) x
theorem reverse_concat : Π {n : nat} (xs : vector A n) (a : A), reverse (concat xs a) = a :: reverse xs
| 0 [] a := rfl
| (n+1) (x :: xs) a :=
begin
change (concat (reverse (concat xs a)) x = a :: reverse (x :: xs)),
rewrite reverse_concat
end
theorem reverse_reverse : Π {n : nat} (xs : vector A n), reverse (reverse xs) = xs
| 0 [] := rfl
| (n+1) (x :: xs) :=
begin
change (reverse (concat (reverse xs) x) = x :: xs),
rewrite [reverse_concat, reverse_reverse]
end
/- list <-> vector -/
definition of_list : Π (l : list A), vector A (list.length l)
| list.nil := []
| (list.cons a l) := a :: (of_list l)
definition to_list : Π {n : nat}, vector A n → list A
| 0 [] := list.nil
| (n+1) (a :: vs) := list.cons a (to_list vs)
theorem to_list_of_list : ∀ (l : list A), to_list (of_list l) = l
| list.nil := rfl
| (list.cons a l) :=
begin
change (list.cons a (to_list (of_list l)) = list.cons a l),
rewrite to_list_of_list
end
theorem to_list_nil : to_list [] = (list.nil : list A) :=
rfl
theorem length_to_list : ∀ {n : nat} (v : vector A n), list.length (to_list v) = n
| 0 [] := rfl
| (n+1) (a :: vs) :=
begin
change (succ (list.length (to_list vs)) = succ n),
rewrite length_to_list
end
theorem heq_of_list_eq : ∀ {n m} {v₁ : vector A n} {v₂ : vector A m}, to_list v₁ = to_list v₂ → n = m → v₁ == v₂
| 0 0 [] [] h₁ h₂ := !heq.refl
| 0 (m+1) [] (y::ys) h₁ h₂ := by contradiction
| (n+1) 0 (x::xs) [] h₁ h₂ := by contradiction
| (n+1) (m+1) (x::xs) (y::ys) h₁ h₂ :=
assert e₁ : n = m, from succ.inj h₂,
assert e₂ : x = y, begin unfold to_list at h₁, injection h₁, assumption end,
have to_list xs = to_list ys, begin unfold to_list at h₁, injection h₁, assumption end,
assert xs == ys, from heq_of_list_eq this e₁,
assert y :: xs == y :: ys, begin clear heq_of_list_eq h₁ h₂, revert xs ys this, induction e₁, intro xs ys h, rewrite [heq.to_eq h] end,
show x :: xs == y :: ys, by rewrite e₂; exact this
theorem list_eq_of_heq {n m} {v₁ : vector A n} {v₂ : vector A m} : v₁ == v₂ → n = m → to_list v₁ = to_list v₂ :=
begin
intro h₁ h₂, revert v₁ v₂ h₁,
subst n, intro v₁ v₂ h₁, rewrite [heq.to_eq h₁]
end
theorem of_list_to_list {n : nat} (v : vector A n) : of_list (to_list v) == v :=
begin
apply heq_of_list_eq, rewrite to_list_of_list, rewrite length_to_list
end
theorem to_list_append : ∀ {n m : nat} (v₁ : vector A n) (v₂ : vector A m), to_list (append v₁ v₂) = list.append (to_list v₁) (to_list v₂)
| 0 m [] ys := rfl
| (succ n) m (x::xs) ys := begin unfold append, unfold to_list at {1,2}, krewrite [to_list_append xs ys] end
theorem to_list_map (f : A → B) : ∀ {n : nat} (v : vector A n), to_list (map f v) = list.map f (to_list v)
| 0 [] := rfl
| (succ n) (x::xs) := begin unfold [map, to_list], rewrite to_list_map end
theorem to_list_concat : ∀ {n : nat} (v : vector A n) (a : A), to_list (concat v a) = list.concat a (to_list v)
| 0 [] a := rfl
| (succ n) (x::xs) a := begin unfold [concat, to_list], rewrite to_list_concat end
theorem to_list_reverse : ∀ {n : nat} (v : vector A n), to_list (reverse v) = list.reverse (to_list v)
| 0 [] := rfl
| (succ n) (x::xs) := begin unfold [reverse], rewrite [to_list_concat, to_list_reverse] end
theorem append_nil_right {n : nat} (v : vector A n) : append v [] == v :=
begin
apply heq_of_list_eq,
rewrite [to_list_append, to_list_nil, list.append_nil_right],
rewrite [-add_eq_addl]
end
theorem append.assoc {n₁ n₂ n₃ : nat} (v₁ : vector A n₁) (v₂ : vector A n₂) (v₃ : vector A n₃) : append v₁ (append v₂ v₃) == append (append v₁ v₂) v₃ :=
begin
apply heq_of_list_eq,
rewrite [*to_list_append, list.append.assoc],
rewrite [-*add_eq_addl, add.assoc]
end
theorem reverse_append {n m : nat} (v : vector A n) (w : vector A m) : reverse (append v w) == append (reverse w) (reverse v) :=
begin
apply heq_of_list_eq,
rewrite [to_list_reverse, to_list_append, list.reverse_append, to_list_append, *to_list_reverse],
rewrite [-*add_eq_addl, add.comm]
end
theorem concat_eq_append {n : nat} (v : vector A n) (a : A) : concat v a == append v [a] :=
begin
apply heq_of_list_eq,
rewrite [to_list_concat, to_list_append, list.concat_eq_append],
rewrite [-add_eq_addl]
end
/- decidable equality -/
open decidable
definition decidable_eq [H : decidable_eq A] : ∀ {n : nat} (v₁ v₂ : vector A n), decidable (v₁ = v₂)
| ⌞0⌟ [] [] := by left; reflexivity
| ⌞n+1⌟ (a::v₁) (b::v₂) :=
match H a b with
| inl Hab :=
match decidable_eq v₁ v₂ with
| inl He := by left; congruence; repeat assumption
| inr Hn := by right; intro h; injection h; contradiction
end
| inr Hnab := by right; intro h; injection h; contradiction
end
section
open equiv function
definition vector_equiv_of_equiv {A B : Type} : A ≃ B → ∀ n, vector A n ≃ vector B n
| (mk f g l r) n :=
mk (map f) (map g)
begin intros, rewrite [map_map, id_of_left_inverse l, map_id], reflexivity end
begin intros, rewrite [map_map, id_of_righ_inverse r, map_id], reflexivity end
end
end vector
|
068b3791fcaef78b539566e74eccb3cfc8ea69a1 | 4b846d8dabdc64e7ea03552bad8f7fa74763fc67 | /library/init/meta/interactive.lean | 0e8b16880da94d2887facda41dd28e3cbf101117 | [
"Apache-2.0"
] | permissive | pacchiano/lean | 9324b33f3ac3b5c5647285160f9f6ea8d0d767dc | fdadada3a970377a6df8afcd629a6f2eab6e84e8 | refs/heads/master | 1,611,357,380,399 | 1,489,870,101,000 | 1,489,870,101,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,221 | 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.meta.tactic init.meta.rewrite_tactic init.meta.simp_tactic
import init.meta.smt.congruence_closure init.category.combinators
import init.meta.lean.parser init.meta.quote
open lean
open lean.parser
local postfix `?`:9001 := optional
local postfix *:9001 := many
namespace interactive
/-- (parse p) as the parameter type of an interactive tactic will instruct the Lean parser
to run `p` when parsing the parameter and to pass the parsed value as an argument
to the tactic. -/
@[reducible] meta def parse {α : Type} [has_quote α] (p : parser α) : Type := α
namespace types
variables {α β : Type}
-- optimized pretty printer
meta def brackets (l r : string) (p : parser α) := tk l *> p <* tk r
meta def list_of (p : parser α) := brackets "[" "]" $ sep_by "," p
/-- A 'tactic expression', which uses right-binding power 2 so that it is terminated by
'<|>' (rbp 2), ';' (rbp 1), and ',' (rbp 0). It should be used for any (potentially)
trailing expression parameters. -/
meta def texpr := qexpr 2
/-- Parse an identifier or a '_' -/
meta def ident_ : parser name := ident <|> tk "_" *> return `_
meta def using_ident := (tk "using" *> ident)?
meta def with_ident_list := (tk "with" *> ident_*) <|> return []
meta def without_ident_list := (tk "without" *> ident*) <|> return []
meta def location := (tk "at" *> ident*) <|> return []
meta def qexpr_list := list_of (qexpr 0)
meta def opt_qexpr_list := qexpr_list <|> return []
meta def qexpr_list_or_texpr := qexpr_list <|> return <$> texpr
end types
open expr format tactic types
private meta def maybe_paren : list format → format
| [] := ""
| [f] := f
| fs := paren (join fs)
private meta def unfold (e : expr) : tactic expr :=
do (expr.const f_name f_lvls) ← return e^.get_app_fn | failed,
env ← get_env,
decl ← env^.get f_name,
new_f ← decl^.instantiate_value_univ_params f_lvls,
head_beta (expr.mk_app new_f e^.get_app_args)
private meta def parser_desc_aux : expr → tactic (list format)
| ```(ident) := return ["id"]
| ```(ident_) := return ["id"]
| ```(qexpr) := return ["expr"]
| ```(tk %%c) := return <$> to_fmt <$> eval_expr string c
| ```(return ._) := return []
| ```(._ <$> %%p) := parser_desc_aux p
| ```(%%p₁ <*> %%p₂) := do
f₁ ← parser_desc_aux p₁,
f₂ ← parser_desc_aux p₂,
return $ f₁ ++ [" "] ++ f₂
| ```(%%p₁ <* %%p₂) := do
f₁ ← parser_desc_aux p₁,
f₂ ← parser_desc_aux p₂,
return $ f₁ ++ [" "] ++ f₂
| ```(%%p₁ *> %%p₂) := do
f₁ ← parser_desc_aux p₁,
f₂ ← parser_desc_aux p₂,
return $ f₁ ++ [" "] ++ f₂
| ```(many %%p) := do
f ← parser_desc_aux p,
return [maybe_paren f ++ "*"]
| ```(optional %%p) := do
f ← parser_desc_aux p,
return [maybe_paren f ++ "?"]
| ```(sep_by %%sep %%p) := do
f ← parser_desc_aux p,
fs ← eval_expr string sep,
return [maybe_paren f ++ to_fmt fs, " ..."]
| ```(%%p₁ <|> %%p₂) := do
f₁ ← parser_desc_aux p₁,
f₂ ← parser_desc_aux p₂,
return $ if list.empty f₁ then [maybe_paren f₂ ++ "?"] else
if list.empty f₂ then [maybe_paren f₁ ++ "?"] else
[paren $ join $ f₁ ++ [to_fmt " | "] ++ f₂]
| ```(brackets %%l %%r %%p) := do
f ← parser_desc_aux p,
l ← eval_expr string l,
r ← eval_expr string r,
-- much better than the naive [l, " ", f, " ", r]
return [to_fmt l ++ join f ++ to_fmt r]
| e := do
e' ← (do e' ← unfold e,
guard $ e' ≠ e,
return e') <|>
(do f ← pp e,
fail $ to_fmt "don't know how to pretty print " ++ f),
parser_desc_aux e'
meta def param_desc : expr → tactic format
| ```(parse %%p) := join <$> parser_desc_aux p
| ```(opt_param %%t ._) := (++ "?") <$> pp t
| e := if is_constant e ∧ (const_name e)^.components^.ilast ∈ [`itactic, `irtactic]
then return $ to_fmt "{ tactic }"
else paren <$> pp e
end interactive
namespace tactic
meta def report_resolve_name_failure {α : Type} (e : expr) (n : name) : tactic α :=
if e^.is_choice_macro
then fail ("failed to resolve name '" ++ to_string n ++ "', it is overloaded")
else fail ("failed to resolve name '" ++ to_string n ++ "', unexpected result")
/- allows metavars and report errors -/
meta def i_to_expr (q : pexpr) : tactic expr :=
to_expr q tt tt
/- doesn't allows metavars and report errors -/
meta def i_to_expr_strict (q : pexpr) : tactic expr :=
to_expr q ff tt
namespace interactive
open interactive interactive.types expr
/-
itactic: parse a nested "interactive" tactic. That is, parse
`{` tactic `}`
-/
meta def itactic : Type :=
tactic unit
/-
irtactic: parse a nested "interactive" tactic. That is, parse
`{` tactic `}`
It is similar to itactic, but the interactive mode will report errors
when any of the nested tactics fail. This is good for tactics such as async and abstract.
-/
meta def irtactic : Type :=
tactic unit
/--
This tactic applies to a goal that is either a Pi/forall or starts with a let binder.
If the current goal is a Pi/forall `∀ x:T, U` (resp `let x:=t in U`) then intro puts `x:T` (resp `x:=t`) in the local context. The new subgoal target is `U`.
If the goal is an arrow `T → U`, then it puts in the local context either `h:T`, and the new goal target is `U`.
If the goal is neither a Pi/forall nor starting with a let definition,
the tactic `intro` applies the tactic `whnf` until the tactic `intro` can be applied or the goal is not `head-reducible`.
-/
meta def intro : parse ident_? → tactic unit
| none := intro1 >> skip
| (some h) := tactic.intro h >> skip
/--
Similar to `intro` tactic. The tactic `intros` will keep introducing new hypotheses until the goal target is not a Pi/forall or let binder.
The variant `intros h_1 ... h_n` introduces `n` new hypotheses using the given identifiers to name them.
-/
meta def intros : parse ident_* → tactic unit
| [] := tactic.intros >> skip
| hs := intro_lst hs >> skip
/--
The tactic `rename h₁ h₂` renames hypothesis `h₁` into `h₂` in the current local context.
-/
meta def rename : parse ident → parse ident → tactic unit :=
tactic.rename
/--
This tactic applies to any goal.
The argument term is a term well-formed in the local context of the main goal.
The tactic apply tries to match the current goal against the conclusion of the type of term.
If it succeeds, then the tactic returns as many subgoals as the number of non-dependent premises
that have not been fixed by type inference or type class resolution.
The tactic `apply` uses higher-order pattern matching, type class resolution, and
first-order unification with dependent types.
-/
meta def apply (q : parse texpr) : tactic unit :=
i_to_expr q >>= tactic.apply
/--
Similar to the `apply` tactic, but it also creates subgoals for dependent premises
that have not been fixed by type inference or type class resolution.
-/
meta def fapply (q : parse texpr) : tactic unit :=
i_to_expr q >>= tactic.fapply
/--
This tactic tries to close the main goal `... |- U` using type class resolution.
It succeeds if it generates a term of type `U` using type class resolution.
-/
meta def apply_instance : tactic unit :=
tactic.apply_instance
/--
This tactic applies to any goal. It behaves like `exact` with a big difference:
the user can leave some holes `_` in the term.
`refine` will generate as many subgoals as there are holes in the term.
Note that some holes may be implicit.
The type of holes must be either synthesized by the system or declared by
an explicit type ascription like (e.g., `(_ : nat → Prop)`).
-/
meta def refine (q : parse texpr) : tactic unit :=
tactic.refine q tt
/--
This tactic looks in the local context for an hypothesis which type is equal to the goal target.
If it is the case, the subgoal is proved. Otherwise, it fails.
-/
meta def assumption : tactic unit :=
tactic.assumption
/--
This tactic applies to any goal. `change U` replaces the main goal target `T` with `U`
providing that `U` is well-formed with respect to the main goal local context,
and `T` and `U` are definitionally equal.
-/
meta def change (q : parse texpr) : tactic unit :=
i_to_expr q >>= tactic.change
/--
This tactic applies to any goal. It gives directly the exact proof
term of the goal. Let `T` be our goal, let `p` be a term of type `U` then
`exact p` succeeds iff `T` and `U` are definitionally equal.
-/
meta def exact (q : parse texpr) : tactic unit :=
do tgt : expr ← target,
i_to_expr_strict ``(%%q : %%tgt) >>= tactic.exact
private meta def get_locals : list name → tactic (list expr)
| [] := return []
| (n::ns) := do h ← get_local n, hs ← get_locals ns, return (h::hs)
/--
`revert h₁ ... hₙ` applies to any goal with hypotheses `h₁` ... `hₙ`.
It moves the hypotheses and its dependencies to the goal target.
This tactic is the inverse of `intro`.
-/
meta def revert (ids : parse ident*) : tactic unit :=
do hs ← get_locals ids, revert_lst hs, skip
/- Return (some a) iff p is of the form (- a) -/
private meta def is_neg (p : pexpr) : option pexpr :=
/- Remark: we use the low-level to_raw_expr and of_raw_expr to be able to
pattern match pre-terms. This is a low-level trick (aka hack). -/
match pexpr.to_raw_expr p with
| (app fn arg) :=
match fn^.erase_annotations with
| (const `neg _) := some (pexpr.of_raw_expr arg)
| _ := none
end
| _ := none
end
private meta def resolve_name' (n : name) : tactic expr :=
do {
e ← resolve_name n,
match e with
| expr.const n _ := mk_const n -- create metavars for universe levels
| _ := return e
end
}
/- Version of to_expr that tries to bypass the elaborator if `p` is just a constant or local constant.
This is not an optimization, by skipping the elaborator we make sure that no unwanted resolution is used.
Example: the elaborator will force any unassigned ?A that must have be an instance of (has_one ?A) to nat.
Remark: another benefit is that auxiliary temporary metavariables do not appear in error messages. -/
private meta def to_expr' (p : pexpr) : tactic expr :=
let e := pexpr.to_raw_expr p in
match e with
| (const c []) := do new_e ← resolve_name' c, save_type_info new_e e, return new_e
| (local_const c _ _ _) := do new_e ← resolve_name' c, save_type_info new_e e, return new_e
| _ := i_to_expr p
end
private meta def maybe_save_info : option pos → tactic unit
| (some p) := save_info p
| none := skip
private meta def symm_expr := bool × expr × option pos
private meta def to_symm_expr_list : list pexpr → tactic (list symm_expr)
| [] := return []
| (p::ps) :=
match is_neg p with
| some a := do r ← to_expr' a, rs ← to_symm_expr_list ps, return ((tt, r, pexpr.pos p) :: rs)
| none := do r ← to_expr' p, rs ← to_symm_expr_list ps, return ((ff, r, pexpr.pos p) :: rs)
end
private meta def rw_goal : transparency → list symm_expr → tactic unit
| m [] := return ()
| m ((symm, e, pos)::es) := maybe_save_info pos >> rewrite_core m tt tt occurrences.all symm e >> rw_goal m es
private meta def rw_hyp : transparency → list symm_expr → name → tactic unit
| m [] hname := return ()
| m ((symm, e, pos)::es) hname :=
do h ← get_local hname,
maybe_save_info pos,
rewrite_at_core m tt tt occurrences.all symm e h,
rw_hyp m es hname
private meta def rw_hyps : transparency → list symm_expr → list name → tactic unit
| m es [] := return ()
| m es (h::hs) := rw_hyp m es h >> rw_hyps m es hs
private meta def rw_core (m : transparency) (hs : parse qexpr_list_or_texpr) (loc : parse location) : tactic unit :=
do hlist ← to_symm_expr_list hs,
match loc with
| [] := rw_goal m hlist >> try (reflexivity reducible)
| hs := rw_hyps m hlist hs >> try (reflexivity reducible)
end
meta def rewrite : parse qexpr_list_or_texpr → parse location → tactic unit :=
rw_core reducible
meta def rw : parse qexpr_list_or_texpr → parse location → tactic unit :=
rewrite
/- rewrite followed by assumption -/
meta def rwa (q : parse qexpr_list_or_texpr) (l : parse location) : tactic unit :=
rewrite q l >> try assumption
meta def erewrite : parse qexpr_list_or_texpr → parse location → tactic unit :=
rw_core semireducible
meta def erw : parse qexpr_list_or_texpr → parse location → tactic unit :=
erewrite
private meta def get_type_name (e : expr) : tactic name :=
do e_type ← infer_type e >>= whnf,
(const I ls) ← return $ get_app_fn e_type,
return I
meta def induction (p : parse texpr) (rec_name : parse using_ident) (ids : parse with_ident_list) : tactic unit :=
do e ← i_to_expr p, tactic.induction e ids rec_name, return ()
meta def cases (p : parse texpr) (ids : parse with_ident_list) : tactic unit :=
do e ← i_to_expr p,
tactic.cases e ids
meta def destruct (p : parse texpr) : tactic unit :=
i_to_expr p >>= tactic.destruct
meta def generalize (p : parse qexpr) (x : parse ident) : tactic unit :=
do e ← i_to_expr p,
tactic.generalize e x
meta def trivial : tactic unit :=
tactic.triv <|> tactic.reflexivity <|> tactic.contradiction <|> fail "trivial tactic failed"
/-- Closes the main goal using sorry. -/
meta def admit : tactic unit := tactic.admit
/--
This tactic applies to any goal. The contradiction tactic attempts to find in the current local context an hypothesis that is equivalent to
an empty inductive type (e.g. `false`), a hypothesis of the form `c_1 ... = c_2 ...` where `c_1` and `c_2` are distinct constructors,
or two contradictory hypotheses.
-/
meta def contradiction : tactic unit :=
tactic.contradiction
meta def repeat : itactic → tactic unit :=
tactic.repeat
meta def try : itactic → tactic unit :=
tactic.try
meta def solve1 : itactic → tactic unit :=
tactic.solve1
meta def abstract (id : parse ident? ) (tac : irtactic) : tactic unit :=
tactic.abstract tac id
meta def all_goals : itactic → tactic unit :=
tactic.all_goals
meta def any_goals : itactic → tactic unit :=
tactic.any_goals
meta def focus (tac : irtactic) : tactic unit :=
tactic.focus [tac]
/--
This tactic applies to any goal. `assert h : T` adds a new hypothesis of name `h` and type `T` to the current goal and opens a new subgoal with target `T`.
The new subgoal becomes the main goal.
-/
meta def assert (h : parse ident) (q : parse $ tk ":" *> texpr) : tactic unit :=
do e ← i_to_expr_strict q,
tactic.assert h e
meta def define (h : parse ident) (q : parse $ tk ":" *> texpr) : tactic unit :=
do e ← i_to_expr_strict q,
tactic.define h e
/--
This tactic applies to any goal. `assertv h : T := p` adds a new hypothesis of name `h` and type `T` to the current goal if `p` a term of type `T`.
-/
meta def assertv (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : tactic unit :=
do t ← i_to_expr_strict q₁,
v ← i_to_expr_strict ``(%%q₂ : %%t),
tactic.assertv h t v
meta def definev (h : parse ident) (q₁ : parse $ tk ":" *> texpr) (q₂ : parse $ tk ":=" *> texpr) : tactic unit :=
do t ← i_to_expr_strict q₁,
v ← i_to_expr_strict ``(%%q₂ : %%t),
tactic.definev h t v
meta def note (h : parse ident) (q : parse $ tk ":=" *> texpr) : tactic unit :=
do p ← i_to_expr_strict q,
tactic.note h p
meta def pose (h : parse ident) (q : parse $ tk ":=" *> texpr) : tactic unit :=
do p ← i_to_expr_strict q,
tactic.pose h p
/--
This tactic displays the current state in the tracing buffer.
-/
meta def trace_state : tactic unit :=
tactic.trace_state
/--
`trace a` displays `a` in the tracing buffer.
-/
meta def trace {α : Type} [has_to_tactic_format α] (a : α) : tactic unit :=
tactic.trace a
meta def existsi (e : parse texpr) : tactic unit :=
i_to_expr e >>= tactic.existsi
/--
This tactic applies to a goal such that its conclusion is an inductive type (say `I`).
It tries to apply each constructor of `I` until it succeeds.
-/
meta def constructor : tactic unit :=
tactic.constructor
meta def left : tactic unit :=
tactic.left
meta def right : tactic unit :=
tactic.right
meta def split : tactic unit :=
tactic.split
meta def exfalso : tactic unit :=
tactic.exfalso
/--
The injection tactic is based on the fact that constructors of inductive datatypes are injections.
That means that if `c` is a constructor of an inductive datatype,
and if `(c t₁)` and `(c t₂)` are two terms that are equal then `t₁` and `t₂` are equal too.
If `q` is a proof of a statement of conclusion `t₁ = t₂`,
then injection applies injectivity to derive the equality of all arguments of `t₁` and `t₂` placed in the same positions.
For example, from `(a::b) = (c::d)` we derive `a=c` and `b=d`.
To use this tactic `t₁` and `t₂` should be constructor applications of the same constructor.
Given `h : a::b = c::d`, the tactic `injection h` adds to new hypothesis with types `a = c` and `b = d`
to the main goal. The tactic `injection h with h₁ h₂` uses the names `h₁` an `h₂` to name the new
hypotheses.
-/
meta def injection (q : parse texpr) (hs : parse with_ident_list) : tactic unit :=
do e ← i_to_expr q, tactic.injection_with e hs
private meta def add_simps : simp_lemmas → list name → tactic simp_lemmas
| s [] := return s
| s (n::ns) := do s' ← s^.add_simp n, add_simps s' ns
private meta def report_invalid_simp_lemma {α : Type} (n : name): tactic α :=
fail ("invalid simplification lemma '" ++ to_string n ++ "' (use command 'set_option trace.simp_lemmas true' for more details)")
private meta def simp_lemmas.resolve_and_add (s : simp_lemmas) (n : name) (ref : expr) : tactic simp_lemmas :=
do
e ← resolve_name n,
match e with
| expr.const n _ :=
(do b ← is_valid_simp_lemma_cnst reducible n, guard b, save_const_type_info n ref, s^.add_simp n)
<|>
(do eqns ← get_eqn_lemmas_for tt n, guard (eqns^.length > 0), save_const_type_info n ref, add_simps s eqns)
<|>
report_invalid_simp_lemma n
| _ :=
(do b ← is_valid_simp_lemma reducible e, guard b, try (save_type_info e ref), s^.add e)
<|>
report_invalid_simp_lemma n
end
private meta def simp_lemmas.add_pexpr (s : simp_lemmas) (p : pexpr) : tactic simp_lemmas :=
let e := pexpr.to_raw_expr p in
match e with
| (const c []) := simp_lemmas.resolve_and_add s c e
| (local_const c _ _ _) := simp_lemmas.resolve_and_add s c e
| _ := do new_e ← i_to_expr p, s^.add new_e
end
private meta def simp_lemmas.append_pexprs : simp_lemmas → list pexpr → tactic simp_lemmas
| s [] := return s
| s (l::ls) := do new_s ← simp_lemmas.add_pexpr s l, simp_lemmas.append_pexprs new_s ls
private meta def mk_simp_set (attr_names : list name) (hs : list pexpr) (ex : list name) : tactic simp_lemmas :=
do s₀ ← join_user_simp_lemmas attr_names,
s₁ ← simp_lemmas.append_pexprs s₀ hs,
return $ simp_lemmas.erase s₁ ex
private meta def simp_goal (cfg : simp_config) : simp_lemmas → tactic unit
| s := do
(new_target, Heq) ← target >>= simplify_core cfg s `eq,
tactic.assert `Htarget new_target, swap,
Ht ← get_local `Htarget,
mk_eq_mpr Heq Ht >>= tactic.exact
private meta def simp_hyp (cfg : simp_config) (s : simp_lemmas) (h_name : name) : tactic unit :=
do h ← get_local h_name,
htype ← infer_type h,
(new_htype, eqpr) ← simplify_core cfg s `eq htype,
tactic.assert (expr.local_pp_name h) new_htype,
mk_eq_mp eqpr h >>= tactic.exact,
try $ tactic.clear h
private meta def simp_hyps (cfg : simp_config) : simp_lemmas → list name → tactic unit
| s [] := skip
| s (h::hs) := simp_hyp cfg s h >> simp_hyps s hs
private meta def simp_core (cfg : simp_config) (ctx : list expr) (hs : list pexpr) (attr_names : list name) (ids : list name) (loc : list name) : tactic unit :=
do s ← mk_simp_set attr_names hs ids,
s ← s^.append ctx,
match loc : _ → tactic unit with
| [] := simp_goal cfg s
| _ := simp_hyps cfg s loc
end,
try tactic.triv, try (tactic.reflexivity reducible)
/--
This tactic uses lemmas and hypotheses to simplify the main goal target or non-dependent hypotheses.
It has many variants.
- `simp` simplifies the main goal target using lemmas tagged with the attribute `[simp]`.
- `simp [h_1, ..., h_n]` simplifies the main goal target using the lemmas tagged with the attribute `[simp]` and the given `h_i`s.
The `h_i`'s are terms. If a `h_i` is a definition `f`, then the equational lemmas associated with `f` are used.
This is a convenient way to "unfold" `f`.
- `simp without id_1 ... id_n` simplifies the main goal target using the lemmas tagged with the attribute `[simp]`,
but removes the ones named `id_i`s.
- `simp at h` simplifies the non dependent hypothesis `h : T`. The tactic fails if the target or another hypothesis depends on `h`.
- `simp with attr` simplifies the main goal target using the lemmas tagged with the attribute `[attr]`.
-/
meta def simp (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) (loc : parse location)
(cfg : simp_config := {}) : tactic unit :=
simp_core cfg [] hs attr_names ids loc
/--
Similar to the `simp` tactic, but adds all applicable hypotheses as simplification rules.
-/
meta def simp_using_hs (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list)
(cfg : simp_config := {}) : tactic unit :=
do ctx ← collect_ctx_simps,
simp_core cfg ctx hs attr_names ids []
meta def simph (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list)
(cfg : simp_config := {}) : tactic unit :=
simp_using_hs hs attr_names ids cfg
meta def simp_intros (ids : parse ident_*) (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list)
(wo_ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit :=
do s ← mk_simp_set attr_names hs wo_ids,
match ids with
| [] := simp_intros_using s cfg
| ns := simp_intro_lst_using ns s cfg
end,
try triv >> try (reflexivity reducible)
meta def simph_intros (ids : parse ident_*) (hs : parse opt_qexpr_list) (attr_names : parse with_ident_list)
(wo_ids : parse without_ident_list) (cfg : simp_config := {}) : tactic unit :=
do s ← mk_simp_set attr_names hs wo_ids,
match ids with
| [] := simph_intros_using s cfg
| ns := simph_intro_lst_using ns s cfg
end,
try triv >> try (reflexivity reducible)
private meta def dsimp_hyps (s : simp_lemmas) : list name → tactic unit
| [] := skip
| (h::hs) := get_local h >>= dsimp_at_core s
meta def dsimp (es : parse opt_qexpr_list) (attr_names : parse with_ident_list) (ids : parse without_ident_list) : parse location → tactic unit
| [] := do s ← mk_simp_set attr_names es ids, tactic.dsimp_core s
| hs := do s ← mk_simp_set attr_names es ids, dsimp_hyps s hs
/--
This tactic applies to a goal that has the form `t ~ u` where `~` is a reflexive relation.
That is, a relation which has a reflexivity lemma tagged with the attribute `[refl]`.
The tactic checks whether `t` and `u` are definitionally equal and then solves the goal.
-/
meta def reflexivity : tactic unit :=
tactic.reflexivity
/--
Shorter name for the tactic `reflexivity`.
-/
meta def refl : tactic unit :=
tactic.reflexivity
meta def symmetry : tactic unit :=
tactic.symmetry
meta def transitivity : tactic unit :=
tactic.transitivity
meta def ac_reflexivity : tactic unit :=
tactic.ac_refl
meta def ac_refl : tactic unit :=
tactic.ac_refl
meta def cc : tactic unit :=
tactic.cc
meta def subst (q : parse texpr) : tactic unit :=
i_to_expr q >>= tactic.subst >> try (tactic.reflexivity reducible)
meta def clear : parse ident* → tactic unit :=
tactic.clear_lst
private meta def to_qualified_name_core : name → list name → tactic name
| n [] := fail $ "unknown declaration '" ++ to_string n ++ "'"
| n (ns::nss) := do
curr ← return $ ns ++ n,
env ← get_env,
if env^.contains curr then return curr
else to_qualified_name_core n nss
private meta def to_qualified_name (n : name) : tactic name :=
do env ← get_env,
if env^.contains n then return n
else do
ns ← open_namespaces,
to_qualified_name_core n ns
private meta def to_qualified_names : list name → tactic (list name)
| [] := return []
| (c::cs) := do new_c ← to_qualified_name c, new_cs ← to_qualified_names cs, return (new_c::new_cs)
private meta def dunfold_hyps : list name → list name → tactic unit
| cs [] := skip
| cs (h::hs) := get_local h >>= dunfold_at cs >> dunfold_hyps cs hs
meta def dunfold : parse ident* → parse location → tactic unit
| cs [] := do new_cs ← to_qualified_names cs, tactic.dunfold new_cs
| cs hs := do new_cs ← to_qualified_names cs, dunfold_hyps new_cs hs
/- TODO(Leo): add support for non-refl lemmas -/
meta def unfold : parse ident* → parse location → tactic unit :=
dunfold
private meta def dunfold_hyps_occs : name → occurrences → list name → tactic unit
| c occs [] := skip
| c occs (h::hs) := get_local h >>= dunfold_core_at occs [c] >> dunfold_hyps_occs c occs hs
meta def dunfold_occs : parse ident → parse location → list nat → tactic unit
| c [] ps := do new_c ← to_qualified_name c, tactic.dunfold_occs_of ps new_c
| c hs ps := do new_c ← to_qualified_name c, dunfold_hyps_occs new_c (occurrences.pos ps) hs
/- TODO(Leo): add support for non-refl lemmas -/
meta def unfold_occs : parse ident → parse location → list nat → tactic unit :=
dunfold_occs
private meta def delta_hyps : list name → list name → tactic unit
| cs [] := skip
| cs (h::hs) := get_local h >>= delta_at cs >> dunfold_hyps cs hs
meta def delta : parse ident* → parse location → tactic unit
| cs [] := do new_cs ← to_qualified_names cs, tactic.delta new_cs
| cs hs := do new_cs ← to_qualified_names cs, delta_hyps new_cs hs
end interactive
end tactic
|
84a1db2ea151d603f1730a87c699d1ee391c3686 | 6432ea7a083ff6ba21ea17af9ee47b9c371760f7 | /src/lake/Lake/Config/WorkspaceConfig.lean | 98e2f0ce18edf7a6646b51498ab0d0344e5b0f41 | [
"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 | 591 | lean | /-
Copyright (c) 2021 Mac Malone. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mac Malone
-/
open System
namespace Lake
/-- The default setting for a `WorkspaceConfig`'s `packagesDir` option. -/
def defaultPackagesDir : FilePath := "lake-packages"
/-- A `Workspace`'s declarative configuration. -/
structure WorkspaceConfig where
/--
The directory to which Lake should download remote dependencies.
Defaults to `defaultPackagesDir` (i.e., `lake-packages`).
-/
packagesDir : Option FilePath := none
deriving Inhabited, Repr
|
7cda51b9e11c59da118ea3bbcc0b1e2bdba62fb7 | 076f5040b63237c6dd928c6401329ed5adcb0e44 | /instructor-notes/2019.10.29.props_and_proofs/2019.10.29.props_and_proofs.lean | 8a986ea0a45652c24636d197e7134bd7019a6e1d | [] | no_license | kevinsullivan/uva-cs-dm-f19 | 0f123689cf6cb078f263950b18382a7086bf30be | 09a950752884bd7ade4be33e9e89a2c4b1927167 | refs/heads/master | 1,594,771,841,541 | 1,575,853,850,000 | 1,575,853,850,000 | 205,433,890 | 4 | 9 | null | 1,571,592,121,000 | 1,567,188,539,000 | Lean | UTF-8 | Lean | false | false | 5,310 | lean | /-
Key differences: Predicate vs propositional logic.
(1) In propositional logic, variables range over
Boolean truth values. A variable is either Boolean
true or Boolean false. In predicate logic, variables
can range over values of many types. A variable can
represent a Boolean truth value, but also a natural
number, a string, a person, a chemical, or whatever.
Predicate logic thus has far greater expressiveness
than propositional logic.
(2) Predicate logic has quantifiers: forall (∀) and
exists (∃). These allow one to express the ideas
that some proposition is true of, every, or of at
least one, object of some kind.
(3) Predicate logic has predicates, which you can
think of as propositions with parameters, such that
the application of a predicate to argument values
of its parameter types gives rise to a specific
proposition about those specific argument values.
In general, some of these propositions will be true
and some won't be.
Here's an example of a proposition, in predicate
logic, that illustrates all three points. It
asserts, in precise mathematical terms, that
every natural number is equal to itself. Even
in this simple example, we see that predicate
logic includes (1) quantifiers, (2) variables
that can range over domains other than bool,
and (3) predicates. Here eq is a 2-argument
predicate that, when applied to two values,
yields the proposition that the first one
equals the second. THe more common notation
for eq a b is a = b.
-/
def every_nat_eq_itself := ∀ (n : ℕ), eq n n
-- (_ = _) is an infix notation for (eq _ _)
def every_nat_eq_itself' := ∀ (n : ℕ), n = n
/-
This example illustrates all three points. First,
the logical variable, n, ranges over all values of
type nat. Second, the proposition has a universal
quantifier. Third, in the context of the "binding"
of n to some arbitrary but specific natural number
by the ∀, the overall proposition then asserts that
the proposition n = n is true, using a two-argument
predicate, eq, applied to n and n as arguments, to
state this proposition.
-/
/-
It will be important for you to learn both how
to translate predicate logical expressions into
natural language (here, English), and to express
concepts stated in English in predicate logic.
Let's start with some expressions in predicate
logic. We imagine a world in which there are
people and a relation between people that we
will called "likes _ _", where the proposition
"likes p q" asserts that person p likes person
q.
What, then, do the following proposition mean,
translating from predicate logic to English?
∀ (p : Person), ∃ (q : Person), likes p q
∀ (p : Person), ∃ (q : Person), ¬ likes p q
∃ (p : Person), ∀ (q : Person), likes p q
∃ (p : Person), ∀ (q : Person), likes q p
∃ (p : Person), ∀ (q : Person), ¬ likes q p
(∀ (p : Person), ∃ (q : Person), ¬ likes p q) →
(∃ (p : Person), ∀ (q : Person), ¬ likes q p)
(∃ (p : Person), ∀ (q : Person), ¬ likes q p) →
(∀ (p : Person), ∃ (q : Person), ¬ likes p q)
-/
/-
One of the last two propositions is valid.
Which one?
In predicate logic, we can no longer evaluate
the validity of a proposition using truth
tables. What we need will instead is a proof.
A proof is an object that we will accept as
compelling evidence for the truth of a given
proposition.
It will also be important for you to learn how
to express proofs in both natural language and
formally, in terms of the "inference rules" of
what we call "natural deduction."
Let's go for a natural language proof of this:
(∃ (p : Person), ∀ (q : Person), ¬ likes q p) →
(∀ (p : Person), ∃ (q : Person), ¬ likes p q)
What is says in plain English is that if there
is a person no one likes, then everyone dislikes
somebody.
To prove such an implication we will assume
that there is a proof of the premise (and
that the premise is therefore true), and we
will then show that given this assumption we
can construct a proof of the conclusion.
Step 1: To prove the proposition, we assume
(∃ (p : Person), ∀ (q : Person), ¬ likes q p).
Given (in the context of) this assumption, it
remains to be proved that (∀ (p : Person),
∃ (q : Person), ¬ likes p q).
Step 2: We've assumed that there is someone
no one likes. We give that someone (whoever it
is) a name: let's say, w. What we know about
w is that ∀ (q : Person), ¬ likes q w. That
is, we know everyone dislikes w in particular.
Step 3: To prove our remaining goal, that
(∀ (p : Person), ∃ (q : Person), ¬ likes p q),
for each person p, we select w as a "witness"
for q: a person that we expect will make the
remaining proposition true. All that remains
to be proved now is that ¬ likes p w.
Step 4: To prove this, we just apply the fact
from step 2 that ∀ (q : Person), ¬ likes q w,
i.e., that *everyone* dislikes w, to conclude
that p, in particular, dislikes w.
We've thus shown that if there is someone
everyone dislikes then everyone dislikes
someone. For every (∀) person, p, it is
enough to point to w to show there is (∃)
someone that p dislikes. While p might
dislike other people, we can be sure that
he or she dislikes w because w is someone
whom everyone dislikes.
QED. [Latin for Quod Est Demonstratum. Thus
it is shown/proved/demonstrated.]
-/
|
9b9c891c9f1cd787e2d8ee7589eb474e4798b470 | 7c2dd01406c42053207061adb11703dc7ce0b5e5 | /src/solutions/07_first_negations.lean | 7a59be61644424ab38a8b6981d7700923228fce6 | [
"Apache-2.0"
] | permissive | leanprover-community/tutorials | 50ec79564cbf2ad1afd1ac43d8ee3c592c2883a8 | 79a6872a755c4ae0c2aca57e1adfdac38b1d8bb1 | refs/heads/master | 1,687,466,144,386 | 1,672,061,276,000 | 1,672,061,276,000 | 189,169,918 | 186 | 81 | Apache-2.0 | 1,686,350,300,000 | 1,559,113,678,000 | Lean | UTF-8 | Lean | false | false | 8,746 | lean | import tuto_lib
import data.int.parity
/-
Negations, proof by contradiction and contraposition.
This file introduces the logical rules and tactics related to negation:
exfalso, by_contradiction, contrapose, by_cases and push_neg.
There is a special statement denoted by `false` which, by definition,
has no proof.
So `false` implies everything. Indeed `false → P` means any proof of
`false` could be turned into a proof of P.
This fact is known by its latin name
"ex falso quod libet" (from false follows whatever you want).
Hence Lean's tactic to invoke this is called `exfalso`.
-/
example : false → 0 = 1 :=
begin
intro h,
exfalso,
exact h,
end
/-
The preceding example suggests that this definition of `false` isn't very useful.
But actually it allows us to define the negation of a statement P as
"P implies false" that we can read as "if P were true, we would get
a contradiction". Lean denotes this by `¬ P`.
One can prove that (¬ P) ↔ (P ↔ false). But in practice we directly
use the definition of `¬ P`.
-/
example {x : ℝ} : ¬ x < x :=
begin
intro hyp,
rw lt_iff_le_and_ne at hyp,
cases hyp with hyp_inf hyp_non,
clear hyp_inf, -- we won't use that one, so let's discard it
change x = x → false at hyp_non, -- Lean doesn't need this psychological line
apply hyp_non,
refl,
end
open int
-- 0045
example (n : ℤ) (h_even : even n) (h_not_even : ¬ even n) : 0 = 1 :=
begin
-- sorry
exfalso,
exact h_not_even h_even,
-- sorry
end
-- 0046
example (P Q : Prop) (h₁ : P ∨ Q) (h₂ : ¬ (P ∧ Q)) : ¬ P ↔ Q :=
begin
-- sorry
split,
{ intro hnP,
cases h₁ with hP hQ,
{ exfalso,
exact hnP hP, },
{ exact hQ }, },
{ intros hQ hP,
exact h₂ ⟨hP, hQ⟩ },
-- sorry
end
/-
The definition of negation easily implies that, for every statement P,
P → ¬ ¬ P
The excluded middle axiom, which asserts P ∨ ¬ P allows us to
prove the converse implication.
Together those two implications form the principle of double negation elimination.
not_not {P : Prop} : (¬ ¬ P) ↔ P
The implication `¬ ¬ P → P` is the basis for proofs by contradiction:
in order to prove P, it suffices to prove ¬¬ P, ie `¬ P → false`.
Of course there is no need to keep explaining all this. The tactic
`by_contradiction Hyp` will transform any goal P into `false` and
add Hyp : ¬ P to the local context.
Let's return to a proof from the 5th file: uniqueness of limits for a sequence.
This cannot be proved without using some version of the excluded middle
axiom. We used it secretely in
eq_of_abs_sub_le_all (x y : ℝ) : (∀ ε > 0, |x - y| ≤ ε) → x = y
(we'll prove a variation on this lemma below).
In the proof below, we also take the opportunity to introduce the `let` tactic
which creates a local definition. If needed, it can be unfolded using `dsimp` which
takes a list of definitions to unfold. For instance after using `let N₀ := max N N'`,
you could write `dsimp [N₀] at h` to replace `N₀` by its definition in some
local assumption `h`.
-/
example (u : ℕ → ℝ) (l l' : ℝ) : seq_limit u l → seq_limit u l' → l = l' :=
begin
intros hl hl',
by_contradiction H,
change l ≠ l' at H, -- Lean does not need this line
have ineg : |l-l'| > 0,
exact abs_pos.mpr (sub_ne_zero_of_ne H), -- details about that line are not important
cases hl ( |l-l'|/4 ) (by linarith) with N hN,
cases hl' ( |l-l'|/4 ) (by linarith) with N' hN',
let N₀ := max N N',
specialize hN N₀ (le_max_left _ _),
specialize hN' N₀ (le_max_right _ _),
have clef : |l-l'| < |l-l'|,
calc
|l - l'| = |(l-u N₀) + (u N₀ -l')| : by ring_nf
... ≤ |l - u N₀| + |u N₀ - l'| : by apply abs_add
... = |u N₀ - l| + |u N₀ - l'| : by rw abs_sub_comm
... < |l-l'| : by linarith,
linarith, -- linarith can also find simple numerical contradictions
end
/-
Another incarnation of the excluded middle axiom is the principle of
contraposition: in order to prove P ⇒ Q, it suffices to prove
non Q ⇒ non P.
-/
-- Using a proof by contradiction, let's prove the contraposition principle
-- 0047
example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=
begin
-- sorry
intro hP,
by_contradiction hnQ,
exact h hnQ hP,
-- sorry
end
/-
Again Lean doesn't need this principle explained to it. We can use the
`contrapose` tactic.
-/
example (P Q : Prop) (h : ¬ Q → ¬ P) : P → Q :=
begin
contrapose,
exact h,
end
/-
In the next exercise, we'll use
odd n : ∃ k, n = 2*k + 1
int.odd_iff_not_even {n : ℤ} : odd n ↔ ¬ even n
-/
-- 0048
example (n : ℤ) : even (n^2) ↔ even n :=
begin
-- sorry
split,
{ contrapose,
rw ← int.odd_iff_not_even,
rw ← int.odd_iff_not_even,
rintro ⟨k, rfl⟩,
use 2*k*(k+1),
ring },
{ rintro ⟨k, rfl⟩,
use 2*k^2,
ring },
-- sorry
end
/-
As a last step on our law of the excluded middle tour, let's notice that, especially
in pure logic exercises, it can sometimes be useful to use the
excluded middle axiom in its original form:
classical.em : ∀ P, P ∨ ¬ P
Instead of applying this lemma and then using the `cases` tactic, we
have the shortcut
by_cases h : P,
combining both steps to create two proof branches: one assuming
h : P, and the other assuming h : ¬ P
For instance, let's prove a reformulation of this implication relation,
which is sometimes used as a definition in other logical foundations,
especially those based on truth tables (hence very strongly using
excluded middle from the very beginning).
-/
variables (P Q : Prop)
example : (P → Q) ↔ (¬ P ∨ Q) :=
begin
split,
{ intro h,
by_cases hP : P,
{ right,
exact h hP },
{ left,
exact hP } },
{ intros h hP,
cases h with hnP hQ,
{ exfalso,
exact hnP hP },
{ exact hQ } },
end
-- 0049
example : ¬ (P ∧ Q) ↔ ¬ P ∨ ¬ Q :=
begin
-- sorry
split,
{ intro h,
by_cases hP : P,
{ right,
intro hQ,
exact h ⟨hP, hQ⟩ },
{ left,
exact hP } },
{ rintros h ⟨hP, hQ⟩,
cases h with hnP hnQ,
{ exact hnP hP },
{ exact hnQ hQ } },
-- sorry
end
/-
It is crucial to understand negation of quantifiers.
Let's do it by hand for a little while.
In the first exercise, only the definition of negation is needed.
-/
-- 0050
example (n : ℤ) : ¬ (∃ k, n = 2*k) ↔ ∀ k, n ≠ 2*k :=
begin
-- sorry
split,
{ intros hyp k hk,
exact hyp ⟨k, hk⟩ },
{ rintros hyp ⟨k, rfl⟩,
exact hyp k rfl },
-- sorry
end
/-
Contrary to negation of the existential quantifier, negation of the
universal quantifier requires excluded middle for the first implication.
In order to prove this, we can use either
* a double proof by contradiction
* a contraposition, not_not : (¬ ¬ P) ↔ P) and a proof by contradiction.
-/
def even_fun (f : ℝ → ℝ) := ∀ x, f (-x) = f x
-- 0051
example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=
begin
-- sorry
split,
{ contrapose,
intro h,
rw not_not,
intro x,
by_contradiction H,
apply h,
use x,
/- Alternative version
intro h,
by_contradiction H,
apply h,
intro x,
by_contradiction H',
apply H,
use x, -/ },
{ rintros ⟨x, hx⟩ h',
exact hx (h' x) },
-- sorry
end
/-
Of course we can't keep repeating the above proofs, especially the second one.
So we use the `push_neg` tactic.
-/
example : ¬ even_fun (λ x, 2*x) :=
begin
unfold even_fun, -- Here unfolding is important because push_neg won't do it.
push_neg,
use 42,
linarith,
end
-- 0052
example (f : ℝ → ℝ) : ¬ even_fun f ↔ ∃ x, f (-x) ≠ f x :=
begin
-- sorry
unfold even_fun,
push_neg,
-- sorry
end
def bounded_above (f : ℝ → ℝ) := ∃ M, ∀ x, f x ≤ M
example : ¬ bounded_above (λ x, x) :=
begin
unfold bounded_above,
push_neg,
intro M,
use M + 1,
linarith,
end
-- Let's contrapose
-- 0053
example (x : ℝ) : (∀ ε > 0, x ≤ ε) → x ≤ 0 :=
begin
-- sorry
contrapose,
push_neg,
intro h,
use x/2,
split ; linarith,
-- sorry
end
/-
The "contrapose, push_neg" combo is so common that we can abreviate it to
`contrapose!`
Let's use this trick, together with:
eq_or_lt_of_le : a ≤ b → a = b ∨ a < b
-/
-- 0054
example (f : ℝ → ℝ) : (∀ x y, x < y → f x < f y) ↔ (∀ x y, (x ≤ y ↔ f x ≤ f y)) :=
begin
-- sorry
split,
{ intros hf x y,
split,
{ intros hxy,
cases eq_or_lt_of_le hxy with hxy hxy,
{ rw hxy },
{ linarith [hf x y hxy]} },
{ contrapose!,
apply hf } },
{ intros hf x y,
contrapose!,
intro h,
rwa hf, }
-- sorry
end
|
7fdf1bef3bed7febbb20e39f745755060fdd07c5 | fe25de614feb5587799621c41487aaee0d083b08 | /tests/compiler/partial.lean | 43ac3fb675368ef981c32854e69d79d6fbe16470 | [
"Apache-2.0"
] | permissive | pollend/lean4 | e8469c2f5fb8779b773618c3267883cf21fb9fac | c913886938c4b3b83238a3f99673c6c5a9cec270 | refs/heads/master | 1,687,973,251,481 | 1,628,039,739,000 | 1,628,039,739,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 373 | lean |
set_option pp.explicit true
set_option pp.binderTypes false
-- set_option trace.compiler.boxed true
partial def contains : String → Char → Nat → Bool
| s, c, i =>
if s.atEnd i then false
else if s.get i == c then true
else contains s c (s.next i)
def main : IO Unit :=
let s1 := "hello";
IO.println (contains s1 'a' 0) *>
IO.println (contains s1 'o' 0)
|
7d77f61bb996730779f2b3e62f184d52229e780f | 69d4931b605e11ca61881fc4f66db50a0a875e39 | /src/data/list/range.lean | b1d33856f0b23c8ed6416c886773a50c90b4a6a4 | [
"Apache-2.0"
] | permissive | abentkamp/mathlib | d9a75d291ec09f4637b0f30cc3880ffb07549ee5 | 5360e476391508e092b5a1e5210bd0ed22dc0755 | refs/heads/master | 1,682,382,954,948 | 1,622,106,077,000 | 1,622,106,077,000 | 149,285,665 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 10,104 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Kenny Lau, Scott Morrison
-/
import data.list.chain
import data.list.nodup
import data.list.of_fn
import data.list.zip
open nat
namespace list
/- iota and range(') -/
universe u
variables {α : Type u}
@[simp] theorem length_range' : ∀ (s n : ℕ), length (range' s n) = n
| s 0 := rfl
| s (n+1) := congr_arg succ (length_range' _ _)
@[simp] theorem range'_eq_nil {s n : ℕ} : range' s n = [] ↔ n = 0 :=
by rw [← length_eq_zero, length_range']
@[simp] theorem mem_range' {m : ℕ} : ∀ {s n : ℕ}, m ∈ range' s n ↔ s ≤ m ∧ m < s + n
| s 0 := (false_iff _).2 $ λ ⟨H1, H2⟩, not_le_of_lt H2 H1
| s (succ n) :=
have m = s → m < s + n + 1,
from λ e, e ▸ lt_succ_of_le (le_add_right _ _),
have l : m = s ∨ s + 1 ≤ m ↔ s ≤ m,
by simpa only [eq_comm] using (@decidable.le_iff_eq_or_lt _ _ _ s m).symm,
(mem_cons_iff _ _ _).trans $ by simp only [mem_range',
or_and_distrib_left, or_iff_right_of_imp this, l, add_right_comm]; refl
theorem map_add_range' (a) : ∀ s n : ℕ, map ((+) a) (range' s n) = range' (a + s) n
| s 0 := rfl
| s (n+1) := congr_arg (cons _) (map_add_range' (s+1) n)
theorem map_sub_range' (a) :
∀ (s n : ℕ) (h : a ≤ s), map (λ x, x - a) (range' s n) = range' (s - a) n
| s 0 _ := rfl
| s (n+1) h :=
begin
convert congr_arg (cons (s-a)) (map_sub_range' (s+1) n (nat.le_succ_of_le h)),
rw nat.succ_sub h,
refl,
end
theorem chain_succ_range' : ∀ s n : ℕ, chain (λ a b, b = succ a) s (range' (s+1) n)
| s 0 := chain.nil
| s (n+1) := (chain_succ_range' (s+1) n).cons rfl
theorem chain_lt_range' (s n : ℕ) : chain (<) s (range' (s+1) n) :=
(chain_succ_range' s n).imp (λ a b e, e.symm ▸ lt_succ_self _)
theorem pairwise_lt_range' : ∀ s n : ℕ, pairwise (<) (range' s n)
| s 0 := pairwise.nil
| s (n+1) := (chain_iff_pairwise (by exact λ a b c, lt_trans)).1 (chain_lt_range' s n)
theorem nodup_range' (s n : ℕ) : nodup (range' s n) :=
(pairwise_lt_range' s n).imp (λ a b, ne_of_lt)
@[simp] theorem range'_append : ∀ s m n : ℕ, range' s m ++ range' (s+m) n = range' s (n+m)
| s 0 n := rfl
| s (m+1) n := show s :: (range' (s+1) m ++ range' (s+m+1) n) = s :: range' (s+1) (n+m),
by rw [add_right_comm, range'_append]
theorem range'_sublist_right {s m n : ℕ} : range' s m <+ range' s n ↔ m ≤ n :=
⟨λ h, by simpa only [length_range'] using length_le_of_sublist h,
λ h, by rw [← nat.sub_add_cancel h, ← range'_append]; apply sublist_append_left⟩
theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤ n :=
⟨λ h, le_of_not_lt $ λ hn, lt_irrefl (s+n) $
(mem_range'.1 $ h $ mem_range'.2 ⟨le_add_right _ _, nat.add_lt_add_left hn s⟩).2,
λ h, (range'_sublist_right.2 h).subset⟩
theorem nth_range' : ∀ s {m n : ℕ}, m < n → nth (range' s n) m = some (s + m)
| s 0 (n+1) _ := rfl
| s (m+1) (n+1) h := (nth_range' (s+1) (lt_of_add_lt_add_right h)).trans $
by rw add_right_comm; refl
@[simp] lemma nth_le_range' {n m} (i) (H : i < (range' n m).length) :
nth_le (range' n m) i H = n + i :=
option.some.inj $ by rw [←nth_le_nth _, nth_range' _ (by simpa using H)]
theorem range'_concat (s n : ℕ) : range' s (n + 1) = range' s n ++ [s+n] :=
by rw add_comm n 1; exact (range'_append s n 1).symm
theorem range_core_range' : ∀ s n : ℕ, range_core s (range' s n) = range' 0 (n + s)
| 0 n := rfl
| (s+1) n := by rw [show n+(s+1) = n+1+s, from add_right_comm n s 1];
exact range_core_range' s (n+1)
theorem range_eq_range' (n : ℕ) : range n = range' 0 n :=
(range_core_range' n 0).trans $ by rw zero_add
theorem range_succ_eq_map (n : ℕ) : range (n + 1) = 0 :: map succ (range n) :=
by rw [range_eq_range', range_eq_range', range',
add_comm, ← map_add_range'];
congr; exact funext one_add
theorem range'_eq_map_range (s n : ℕ) : range' s n = map ((+) s) (range n) :=
by rw [range_eq_range', map_add_range']; refl
@[simp] theorem length_range (n : ℕ) : length (range n) = n :=
by simp only [range_eq_range', length_range']
@[simp] theorem range_eq_nil {n : ℕ} : range n = [] ↔ n = 0 :=
by rw [← length_eq_zero, length_range]
theorem pairwise_lt_range (n : ℕ) : pairwise (<) (range n) :=
by simp only [range_eq_range', pairwise_lt_range']
theorem nodup_range (n : ℕ) : nodup (range n) :=
by simp only [range_eq_range', nodup_range']
theorem range_sublist {m n : ℕ} : range m <+ range n ↔ m ≤ n :=
by simp only [range_eq_range', range'_sublist_right]
theorem range_subset {m n : ℕ} : range m ⊆ range n ↔ m ≤ n :=
by simp only [range_eq_range', range'_subset_right]
@[simp] theorem mem_range {m n : ℕ} : m ∈ range n ↔ m < n :=
by simp only [range_eq_range', mem_range', nat.zero_le, true_and, zero_add]
@[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n :=
mt mem_range.1 $ lt_irrefl _
@[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) :=
by simp only [succ_pos', lt_add_iff_pos_right, mem_range]
theorem nth_range {m n : ℕ} (h : m < n) : nth (range n) m = some m :=
by simp only [range_eq_range', nth_range' _ h, zero_add]
theorem range_succ (n : ℕ) : range (succ n) = range n ++ [n] :=
by simp only [range_eq_range', range'_concat, zero_add]
@[simp] lemma range_zero : range 0 = [] := rfl
theorem iota_eq_reverse_range' : ∀ n : ℕ, iota n = reverse (range' 1 n)
| 0 := rfl
| (n+1) := by simp only [iota, range'_concat, iota_eq_reverse_range' n,
reverse_append, add_comm]; refl
@[simp] theorem length_iota (n : ℕ) : length (iota n) = n :=
by simp only [iota_eq_reverse_range', length_reverse, length_range']
theorem pairwise_gt_iota (n : ℕ) : pairwise (>) (iota n) :=
by simp only [iota_eq_reverse_range', pairwise_reverse, pairwise_lt_range']
theorem nodup_iota (n : ℕ) : nodup (iota n) :=
by simp only [iota_eq_reverse_range', nodup_reverse, nodup_range']
theorem mem_iota {m n : ℕ} : m ∈ iota n ↔ 1 ≤ m ∧ m ≤ n :=
by simp only [iota_eq_reverse_range', mem_reverse, mem_range', add_comm, lt_succ_iff]
theorem reverse_range' : ∀ s n : ℕ,
reverse (range' s n) = map (λ i, s + n - 1 - i) (range n)
| s 0 := rfl
| s (n+1) := by rw [range'_concat, reverse_append, range_succ_eq_map];
simpa only [show s + (n + 1) - 1 = s + n, from rfl, (∘),
λ a i, show a - 1 - i = a - succ i, from pred_sub _ _,
reverse_singleton, map_cons, nat.sub_zero, cons_append,
nil_append, eq_self_iff_true, true_and, map_map]
using reverse_range' s n
/-- All elements of `fin n`, from `0` to `n-1`. -/
def fin_range (n : ℕ) : list (fin n) :=
(range n).pmap fin.mk (λ _, list.mem_range.1)
@[simp] lemma fin_range_zero : fin_range 0 = [] := rfl
@[simp] lemma mem_fin_range {n : ℕ} (a : fin n) : a ∈ fin_range n :=
mem_pmap.2 ⟨a.1, mem_range.2 a.2, fin.eta _ _⟩
lemma nodup_fin_range (n : ℕ) : (fin_range n).nodup :=
nodup_pmap (λ _ _ _ _, fin.veq_of_eq) (nodup_range _)
@[simp] lemma length_fin_range (n : ℕ) : (fin_range n).length = n :=
by rw [fin_range, length_pmap, length_range]
@[simp] lemma fin_range_eq_nil {n : ℕ} : fin_range n = [] ↔ n = 0 :=
by rw [← length_eq_zero, length_fin_range]
@[to_additive]
theorem prod_range_succ {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) :
((range n.succ).map f).prod = ((range n).map f).prod * f n :=
by rw [range_succ, map_append, map_singleton,
prod_append, prod_cons, prod_nil, mul_one]
/-- A variant of `prod_range_succ` which pulls off the first
term in the product rather than the last.-/
@[to_additive "A variant of `sum_range_succ` which pulls off the first term in the sum
rather than the last."]
theorem prod_range_succ' {α : Type u} [monoid α] (f : ℕ → α) (n : ℕ) :
((range n.succ).map f).prod = f 0 * ((range n).map (λ i, f (succ i))).prod :=
nat.rec_on n
(show 1 * f 0 = f 0 * 1, by rw [one_mul, mul_one])
(λ _ hd, by rw [list.prod_range_succ, hd, mul_assoc, ←list.prod_range_succ])
@[simp] theorem enum_from_map_fst : ∀ n (l : list α),
map prod.fst (enum_from n l) = range' n l.length
| n [] := rfl
| n (a :: l) := congr_arg (cons _) (enum_from_map_fst _ _)
@[simp] theorem enum_map_fst (l : list α) :
map prod.fst (enum l) = range l.length :=
by simp only [enum, enum_from_map_fst, range_eq_range']
lemma enum_eq_zip_range (l : list α) :
l.enum = (range l.length).zip l :=
zip_of_prod (enum_map_fst _) (enum_map_snd _)
@[simp] lemma unzip_enum_eq_prod (l : list α) :
l.enum.unzip = (range l.length, l) :=
by simp only [enum_eq_zip_range, unzip_zip, length_range]
lemma enum_from_eq_zip_range' (l : list α) {n : ℕ} :
l.enum_from n = (range' n l.length).zip l :=
zip_of_prod (enum_from_map_fst _ _) (enum_from_map_snd _ _)
@[simp] lemma unzip_enum_from_eq_prod (l : list α) {n : ℕ} :
(l.enum_from n).unzip = (range' n l.length, l) :=
by simp only [enum_from_eq_zip_range', unzip_zip, length_range']
@[simp] lemma nth_le_range {n} (i) (H : i < (range n).length) :
nth_le (range n) i H = i :=
option.some.inj $ by rw [← nth_le_nth _, nth_range (by simpa using H)]
@[simp] lemma nth_le_fin_range {n : ℕ} {i : ℕ} (h) :
(fin_range n).nth_le i h = ⟨i, length_fin_range n ▸ h⟩ :=
by simp only [fin_range, nth_le_range, nth_le_pmap, fin.mk_eq_subtype_mk]
theorem of_fn_eq_pmap {α n} {f : fin n → α} :
of_fn f = pmap (λ i hi, f ⟨i, hi⟩) (range n) (λ _, mem_range.1) :=
by rw [pmap_eq_map_attach]; from ext_le (by simp)
(λ i hi1 hi2, by { simp at hi1, simp [nth_le_of_fn f ⟨i, hi1⟩, -subtype.val_eq_coe] })
theorem of_fn_id (n) : of_fn id = fin_range n := of_fn_eq_pmap
theorem of_fn_eq_map {α n} {f : fin n → α} :
of_fn f = (fin_range n).map f :=
by rw [← of_fn_id, map_of_fn, function.right_id]
theorem nodup_of_fn {α n} {f : fin n → α} (hf : function.injective f) :
nodup (of_fn f) :=
by rw of_fn_eq_pmap; from nodup_pmap
(λ _ _ _ _ H, fin.veq_of_eq $ hf H) (nodup_range n)
end list
|
041609b60e74b5ae81c3d661d60ec7cd0f52923f | aa3f8992ef7806974bc1ffd468baa0c79f4d6643 | /tests/lean/error_full_names.lean | 77ea0165266f7da280db220e62b46bb7c469e4bc | [
"Apache-2.0"
] | permissive | codyroux/lean | 7f8dff750722c5382bdd0a9a9275dc4bb2c58dd3 | 0cca265db19f7296531e339192e9b9bae4a31f8b | refs/heads/master | 1,610,909,964,159 | 1,407,084,399,000 | 1,416,857,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 187 | lean | import data.nat.basic
namespace foo
open nat
inductive nat : Type := zero, foosucc : nat → nat
check 0 + nat.zero --error
end foo
namespace foo
check nat.succ nat.zero --error
end foo
|
589f9f936ce72097a458ced1548e7f16efaadfa5 | fa02ed5a3c9c0adee3c26887a16855e7841c668b | /src/category_theory/limits/yoneda.lean | ba12a84f77f679ec4ec8415930d73332c6e4dc21 | [
"Apache-2.0"
] | permissive | jjgarzella/mathlib | 96a345378c4e0bf26cf604aed84f90329e4896a2 | 395d8716c3ad03747059d482090e2bb97db612c8 | refs/heads/master | 1,686,480,124,379 | 1,625,163,323,000 | 1,625,163,323,000 | 281,190,421 | 2 | 0 | Apache-2.0 | 1,595,268,170,000 | 1,595,268,169,000 | null | UTF-8 | Lean | false | false | 4,595 | lean | /-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison, Bhavik Mehta
-/
import category_theory.limits.functor_category
/-!
# Limit properties relating to the (co)yoneda embedding.
We calculate the colimit of `Y ↦ (X ⟶ Y)`, which is just `punit`.
(This is used in characterising cofinal functors.)
We also show the (co)yoneda embeddings preserve limits and jointly reflect them.
-/
open opposite
open category_theory
open category_theory.limits
universes v u
namespace category_theory
namespace coyoneda
variables {C : Type v} [small_category C]
/--
The colimit cocone over `coyoneda.obj X`, with cocone point `punit`.
-/
@[simps]
def colimit_cocone (X : Cᵒᵖ) : cocone (coyoneda.obj X) :=
{ X := punit,
ι := { app := by tidy, } }
/--
The proposed colimit cocone over `coyoneda.obj X` is a colimit cocone.
-/
@[simps]
def colimit_cocone_is_colimit (X : Cᵒᵖ) : is_colimit (colimit_cocone X) :=
{ desc := λ s x, s.ι.app (unop X) (𝟙 _),
fac' := λ s Y, by { ext f, convert congr_fun (s.w f).symm (𝟙 (unop X)), simp, },
uniq' := λ s m w, by { ext ⟨⟩, rw ← w, simp, } }
instance (X : Cᵒᵖ) : has_colimit (coyoneda.obj X) :=
has_colimit.mk { cocone := _, is_colimit := colimit_cocone_is_colimit X }
/--
The colimit of `coyoneda.obj X` is isomorphic to `punit`.
-/
noncomputable
def colimit_coyoneda_iso (X : Cᵒᵖ) : colimit (coyoneda.obj X) ≅ punit :=
colimit.iso_colimit_cocone { cocone := _, is_colimit := colimit_cocone_is_colimit X }
end coyoneda
variables {C : Type u} [category.{v} C]
open limits
/-- The yoneda embedding `yoneda.obj X : Cᵒᵖ ⥤ Type v` for `X : C` preserves limits. -/
instance yoneda_preserves_limits (X : C) : preserves_limits (yoneda.obj X) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ K,
{ preserves := λ c t,
{ lift := λ s x, quiver.hom.unop (t.lift ⟨op X, λ j, (s.π.app j x).op, λ j₁ j₂ α, _⟩),
fac' := λ s j, funext $ λ x, quiver.hom.op_inj (t.fac _ _),
uniq' := λ s m w, funext $ λ x,
begin
refine quiver.hom.op_inj (t.uniq ⟨op X, _, _⟩ _ (λ j, _)),
{ dsimp, simp [← s.w α] }, -- See library note [dsimp, simp]
{ exact quiver.hom.unop_inj (congr_fun (w j) x) },
end } } } }
/-- The coyoneda embedding `coyoneda.obj X : C ⥤ Type v` for `X : Cᵒᵖ` preserves limits. -/
instance coyoneda_preserves_limits (X : Cᵒᵖ) : preserves_limits (coyoneda.obj X) :=
{ preserves_limits_of_shape := λ J 𝒥, by exactI
{ preserves_limit := λ K,
{ preserves := λ c t,
{ lift := λ s x, t.lift ⟨unop X, λ j, s.π.app j x, λ j₁ j₂ α, by { dsimp, simp [← s.w α]}⟩,
-- See library note [dsimp, simp]
fac' := λ s j, funext $ λ x, t.fac _ _,
uniq' := λ s m w, funext $ λ x,
begin
refine (t.uniq ⟨unop X, _⟩ _ (λ j, _)),
exact congr_fun (w j) x,
end } } } }
/-- The yoneda embeddings jointly reflect limits. -/
def yoneda_jointly_reflects_limits (J : Type v) [small_category J] (K : J ⥤ Cᵒᵖ) (c : cone K)
(t : Π (X : C), is_limit ((yoneda.obj X).map_cone c)) : is_limit c :=
let s' : Π (s : cone K), cone (K ⋙ yoneda.obj s.X.unop) :=
λ s, ⟨punit, λ j _, (s.π.app j).unop, λ j₁ j₂ α, funext $ λ _, quiver.hom.op_inj (s.w α).symm⟩
in
{ lift := λ s, ((t s.X.unop).lift (s' s) punit.star).op,
fac' := λ s j, quiver.hom.unop_inj (congr_fun ((t s.X.unop).fac (s' s) j) punit.star),
uniq' := λ s m w,
begin
apply quiver.hom.unop_inj,
suffices : (λ (x : punit), m.unop) = (t s.X.unop).lift (s' s),
{ apply congr_fun this punit.star },
apply (t _).uniq (s' s) _ (λ j, _),
ext,
exact quiver.hom.op_inj (w j),
end }
/-- The coyoneda embeddings jointly reflect limits. -/
def coyoneda_jointly_reflects_limits (J : Type v) [small_category J] (K : J ⥤ C) (c : cone K)
(t : Π (X : Cᵒᵖ), is_limit ((coyoneda.obj X).map_cone c)) : is_limit c :=
let s' : Π (s : cone K), cone (K ⋙ coyoneda.obj (op s.X)) :=
λ s, ⟨punit, λ j _, s.π.app j, λ j₁ j₂ α, funext $ λ _, (s.w α).symm⟩
in
{ lift := λ s, (t (op s.X)).lift (s' s) punit.star,
fac' := λ s j, congr_fun ((t _).fac (s' s) j) punit.star,
uniq' := λ s m w,
begin
suffices : (λ (x : punit), m) = (t _).lift (s' s),
{ apply congr_fun this punit.star },
apply (t _).uniq (s' s) _ (λ j, _),
ext,
exact (w j),
end }
end category_theory
|
efed5c9f942e1051b8af14d829dc7b9f2872daad | b00eb947a9c4141624aa8919e94ce6dcd249ed70 | /src/Lean/PrettyPrinter/Delaborator/Builtins.lean | 85771f7e96a64d28ed1b20639b5717babc6ea062 | [
"Apache-2.0"
] | permissive | gebner/lean4-old | a4129a041af2d4d12afb3a8d4deedabde727719b | ee51cdfaf63ee313c914d83264f91f414a0e3b6e | refs/heads/master | 1,683,628,606,745 | 1,622,651,300,000 | 1,622,654,405,000 | 142,608,821 | 1 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 26,603 | lean | /-
Copyright (c) 2020 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich
-/
import Lean.PrettyPrinter.Delaborator.Basic
import Lean.Parser
namespace Lean.PrettyPrinter.Delaborator
open Lean.Meta
open Lean.Parser.Term
@[builtinDelab fvar]
def delabFVar : Delab := do
let Expr.fvar id _ ← getExpr | unreachable!
try
let l ← getLocalDecl id
pure $ mkIdent l.userName
catch _ =>
-- loose free variable, use internal name
pure $ mkIdent id
-- loose bound variable, use pseudo syntax
@[builtinDelab bvar]
def delabBVar : Delab := do
let Expr.bvar idx _ ← getExpr | unreachable!
pure $ mkIdent $ Name.mkSimple $ "#" ++ toString idx
@[builtinDelab mvar]
def delabMVar : Delab := do
let Expr.mvar n _ ← getExpr | unreachable!
let mvarDecl ← getMVarDecl n
let n :=
match mvarDecl.userName with
| Name.anonymous => n.replacePrefix `_uniq `m
| n => n
`(?$(mkIdent n))
@[builtinDelab sort]
def delabSort : Delab := do
let Expr.sort l _ ← getExpr | unreachable!
match l with
| Level.zero _ => `(Prop)
| Level.succ (Level.zero _) _ => `(Type)
| _ => match l.dec with
| some l' => `(Type $(Level.quote l' max_prec))
| none => `(Sort $(Level.quote l max_prec))
-- find shorter names for constants, in reverse to Lean.Elab.ResolveName
private def unresolveQualifiedName (ns : Name) (c : Name) : DelabM Name := do
let c' := c.replacePrefix ns Name.anonymous;
let env ← getEnv
guard $ c' != c && !c'.isAnonymous && (!c'.isAtomic || !isProtected env c)
pure c'
private def unresolveUsingNamespace (c : Name) : Name → DelabM Name
| ns@(Name.str p _ _) => unresolveQualifiedName ns c <|> unresolveUsingNamespace c p
| _ => failure
private def unresolveOpenDecls (c : Name) : List OpenDecl → DelabM Name
| [] => failure
| OpenDecl.simple ns exs :: openDecls =>
let c' := c.replacePrefix ns Name.anonymous
if c' != c && exs.elem c' then unresolveOpenDecls c openDecls
else
unresolveQualifiedName ns c <|> unresolveOpenDecls c openDecls
| OpenDecl.explicit openedId resolvedId :: openDecls =>
guard (c == resolvedId) *> pure openedId <|> unresolveOpenDecls c openDecls
-- NOTE: not a registered delaborator, as `const` is never called (see [delab] description)
def delabConst : Delab := do
let Expr.const c ls _ ← getExpr | unreachable!
let c₀ := c
let mut c ← if (← getPPOption getPPFullNames) then pure c else
let ctx ← read
let env ← getEnv
let as := getRevAliases env c
-- might want to use a more clever heuristic such as selecting the shortest alias...
let c := as.headD c
unresolveUsingNamespace c ctx.currNamespace <|> unresolveOpenDecls c ctx.openDecls <|> pure c
unless (← getPPOption getPPPrivateNames) do
c := (privateToUserName? c).getD c
let ppUnivs ← getPPOption getPPUniverses
if ls.isEmpty || !ppUnivs then
if (← getLCtx).usesUserName c then
-- `c` is also a local declaration
if c == c₀ then
-- `c` is the fully qualified named. So, we append the `_root_` prefix
c := `_root_ ++ c
else
c := c₀
return mkIdent c
else
`($(mkIdent c).{$[$(ls.toArray.map quote)],*})
inductive ParamKind where
| explicit
-- combines implicit params, optParams, and autoParams
| implicit (defVal : Option Expr)
/-- Return array with n-th element set to kind of n-th parameter of `e`. -/
def getParamKinds (e : Expr) : MetaM (Array ParamKind) := do
let t ← inferType e
forallTelescopeReducing t fun params _ =>
params.mapM fun param => do
let l ← getLocalDecl param.fvarId!
match l.type.getOptParamDefault? with
| some val => pure $ ParamKind.implicit val
| _ =>
if l.type.isAutoParam || !l.binderInfo.isExplicit then
pure $ ParamKind.implicit none
else
pure ParamKind.explicit
@[builtinDelab app]
def delabAppExplicit : Delab := do
let (fnStx, argStxs) ← withAppFnArgs
(do
let fn ← getExpr
let stx ← if fn.isConst then delabConst else delab
let paramKinds ← liftM <| getParamKinds fn <|> pure #[]
let stx ← if paramKinds.any (fun | ParamKind.explicit => false | _ => true) then `(@$stx) else pure stx
pure (stx, #[]))
(fun ⟨fnStx, argStxs⟩ => do
let argStx ← delab
pure (fnStx, argStxs.push argStx))
Syntax.mkApp fnStx argStxs
@[builtinDelab app]
def delabAppImplicit : Delab := whenNotPPOption getPPExplicit do
let (fnStx, _, argStxs) ← withAppFnArgs
(do
let fn ← getExpr
let stx ← if fn.isConst then delabConst else delab
let paramKinds ← liftM (getParamKinds fn <|> pure #[])
pure (stx, paramKinds.toList, #[]))
(fun (fnStx, paramKinds, argStxs) => do
let arg ← getExpr;
let implicit : Bool := match paramKinds with -- TODO: check why we need `: Bool` here
| [ParamKind.implicit (some v)] => !v.hasLooseBVars && v == arg
| ParamKind.implicit none :: _ => true
| _ => false
if implicit then
pure (fnStx, paramKinds.tailD [], argStxs)
else do
let argStx ← delab
pure (fnStx, paramKinds.tailD [], argStxs.push argStx))
Syntax.mkApp fnStx argStxs
@[builtinDelab app]
def delabAppWithUnexpander : Delab := whenPPOption getPPNotation do
let Expr.const c _ _ ← pure (← getExpr).getAppFn | failure
let stx ← delabAppImplicit
match stx with
| `($cPP:ident $args*) => do go c stx
| `($cPP:ident) => do go c stx
| _ => pure stx
where
go c stx := do
let some (f::_) ← pure <| (appUnexpanderAttribute.ext.getState (← getEnv)).table.find? c
| pure stx
let EStateM.Result.ok stx _ ← f stx |>.run ()
| pure stx
pure stx
/-- State for `delabAppMatch` and helpers. -/
structure AppMatchState where
info : MatcherInfo
matcherTy : Expr
params : Array Expr := #[]
hasMotive : Bool := false
discrs : Array Syntax := #[]
varNames : Array (Array Name) := #[]
rhss : Array Syntax := #[]
-- additional arguments applied to the result of the `match` expression
moreArgs : Array Syntax := #[]
/--
Extract arguments of motive applications from the matcher type.
For the example below: `#[#[`([])], #[`(a::as)]]` -/
private partial def delabPatterns (st : AppMatchState) : DelabM (Array (Array Syntax)) :=
withReader (fun ctx => { ctx with inPattern := true }) do
let ty ← instantiateForall st.matcherTy st.params
forallTelescope ty fun params _ => do
-- skip motive and discriminators
let alts := Array.ofSubarray $ params[1 + st.discrs.size:]
alts.mapIdxM fun idx alt => do
let ty ← inferType alt
withReader ({ · with expr := ty }) $
usingNames st.varNames[idx] do
withAppFnArgs (pure #[]) (fun pats => do pure $ pats.push (← delab))
where
usingNames {α} (varNames : Array Name) (x : DelabM α) : DelabM α :=
usingNamesAux 0 varNames x
usingNamesAux {α} (i : Nat) (varNames : Array Name) (x : DelabM α) : DelabM α :=
if i < varNames.size then
withBindingBody varNames[i] <| usingNamesAux (i+1) varNames x
else
x
/-- Skip `numParams` binders, and execute `x varNames` where `varNames` contains the new binder names. -/
private partial def skippingBinders {α} (numParams : Nat) (x : Array Name → DelabM α) : DelabM α :=
loop numParams #[]
where
loop : Nat → Array Name → DelabM α
| 0, varNames => x varNames
| n+1, varNames => do
let rec visitLambda : DelabM α := do
let varName ← (← getExpr).bindingName!.eraseMacroScopes
-- Pattern variables cannot shadow each other
if varNames.contains varName then
let varName := (← getLCtx).getUnusedName varName
withBindingBody varName do
loop n (varNames.push varName)
else
withBindingBodyUnusedName fun id => do
loop n (varNames.push id.getId)
let e ← getExpr
if e.isLambda then
visitLambda
else
-- eta expand `e`
let e ← forallTelescopeReducing (← inferType e) fun xs _ => do
if xs.size == 1 && (← inferType xs[0]).isConstOf ``Unit then
-- `e` might be a thunk create by the dependent pattern matching compiler, and `xs[0]` may not even be a pattern variable.
-- If it is a pattern variable, it doesn't look too bad to use `()` instead of the pattern variable.
-- If it becomes a problem in the future, we should modify the dependent pattern matching compiler, and make sure
-- it adds an annotation to distinguish these two cases.
mkLambdaFVars xs (mkApp e (mkConst ``Unit.unit))
else
mkLambdaFVars xs (mkAppN e xs)
withReader (fun ctx => { ctx with expr := e }) visitLambda
/--
Delaborate applications of "matchers" such as
```
List.map.match_1 : {α : Type _} →
(motive : List α → Sort _) →
(x : List α) → (Unit → motive List.nil) → ((a : α) → (as : List α) → motive (a :: as)) → motive x
```
-/
@[builtinDelab app]
def delabAppMatch : Delab := whenPPOption getPPNotation do
-- incrementally fill `AppMatchState` from arguments
let st ← withAppFnArgs
(do
let (Expr.const c us _) ← getExpr | failure
let (some info) ← getMatcherInfo? c | failure
{ matcherTy := (← getConstInfo c).instantiateTypeLevelParams us, info := info : AppMatchState })
(fun st => do
if st.params.size < st.info.numParams then
pure { st with params := st.params.push (← getExpr) }
else if !st.hasMotive then
-- discard motive argument
pure { st with hasMotive := true }
else if st.discrs.size < st.info.numDiscrs then
pure { st with discrs := st.discrs.push (← delab) }
else if st.rhss.size < st.info.altNumParams.size then
/- We save the variables names here to be able to implement safe_shadowing.
The pattern delaboration must use the names saved here. -/
let (varNames, rhs) ← skippingBinders st.info.altNumParams[st.rhss.size] fun varNames => do
let rhs ← delab
return (varNames, rhs)
pure { st with rhss := st.rhss.push rhs, varNames := st.varNames.push varNames }
else
pure { st with moreArgs := st.moreArgs.push (← delab) })
if st.discrs.size < st.info.numDiscrs || st.rhss.size < st.info.altNumParams.size then
-- underapplied
failure
match st.discrs, st.rhss with
| #[discr], #[] =>
let stx ← `(nomatch $discr)
Syntax.mkApp stx st.moreArgs
| _, #[] => failure
| _, _ =>
let pats ← delabPatterns st
let stx ← `(match $[$st.discrs:term],* with $[| $pats,* => $st.rhss]*)
Syntax.mkApp stx st.moreArgs
@[builtinDelab mdata]
def delabMData : Delab := do
if let some _ := Lean.Meta.Match.inaccessible? (← getExpr) then
let s ← withMDataExpr delab
if (← read).inPattern then
`(.($s)) -- We only include the inaccessible annotation when we are delaborating patterns
else
return s
else
-- only interpret `pp.` values by default
let Expr.mdata m _ _ ← getExpr | unreachable!
let mut posOpts := (← read).optionsPerPos
let pos := (← read).pos
for (k, v) in m do
if (`pp).isPrefixOf k then
let opts := posOpts.find? pos |>.getD {}
posOpts := posOpts.insert pos (opts.insert k v)
withReader ({ · with optionsPerPos := posOpts }) do
withMDataExpr delab
/--
Check for a `Syntax.ident` of the given name anywhere in the tree.
This is usually a bad idea since it does not check for shadowing bindings,
but in the delaborator we assume that bindings are never shadowed.
-/
partial def hasIdent (id : Name) : Syntax → Bool
| Syntax.ident _ _ id' _ => id == id'
| Syntax.node _ args => args.any (hasIdent id)
| _ => false
/--
Return `true` iff current binder should be merged with the nested
binder, if any, into a single binder group:
* both binders must have same binder info and domain
* they cannot be inst-implicit (`[a b : A]` is not valid syntax)
* `pp.binder_types` must be the same value for both terms
* prefer `fun a b` over `fun (a b)`
-/
private def shouldGroupWithNext : DelabM Bool := do
let e ← getExpr
let ppEType ← getPPOption getPPBinderTypes;
let go (e' : Expr) := do
let ppE'Type ← withBindingBody `_ $ getPPOption getPPBinderTypes
pure $ e.binderInfo == e'.binderInfo &&
e.bindingDomain! == e'.bindingDomain! &&
e'.binderInfo != BinderInfo.instImplicit &&
ppEType == ppE'Type &&
(e'.binderInfo != BinderInfo.default || ppE'Type)
match e with
| Expr.lam _ _ e'@(Expr.lam _ _ _ _) _ => go e'
| Expr.forallE _ _ e'@(Expr.forallE _ _ _ _) _ => go e'
| _ => pure false
private partial def delabBinders (delabGroup : Array Syntax → Syntax → Delab) : optParam (Array Syntax) #[] → Delab
-- Accumulate names (`Syntax.ident`s with position information) of the current, unfinished
-- binder group `(d e ...)` as determined by `shouldGroupWithNext`. We cannot do grouping
-- inside-out, on the Syntax level, because it depends on comparing the Expr binder types.
| curNames => do
if (← shouldGroupWithNext) then
-- group with nested binder => recurse immediately
withBindingBodyUnusedName fun stxN => delabBinders delabGroup (curNames.push stxN)
else
-- don't group => delab body and prepend current binder group
let (stx, stxN) ← withBindingBodyUnusedName fun stxN => do (← delab, stxN)
delabGroup (curNames.push stxN) stx
@[builtinDelab lam]
def delabLam : Delab :=
delabBinders fun curNames stxBody => do
let e ← getExpr
let stxT ← withBindingDomain delab
let ppTypes ← getPPOption getPPBinderTypes
let expl ← getPPOption getPPExplicit
-- leave lambda implicit if possible
let blockImplicitLambda := expl ||
e.binderInfo == BinderInfo.default ||
Elab.Term.blockImplicitLambda stxBody ||
curNames.any (fun n => hasIdent n.getId stxBody);
if !blockImplicitLambda then
pure stxBody
else
let group ← match e.binderInfo, ppTypes with
| BinderInfo.default, true =>
-- "default" binder group is the only one that expects binder names
-- as a term, i.e. a single `Syntax.ident` or an application thereof
let stxCurNames ←
if curNames.size > 1 then
`($(curNames.get! 0) $(curNames.eraseIdx 0)*)
else
pure $ curNames.get! 0;
`(funBinder| ($stxCurNames : $stxT))
| BinderInfo.default, false => pure curNames.back -- here `curNames.size == 1`
| BinderInfo.implicit, true => `(funBinder| {$curNames* : $stxT})
| BinderInfo.implicit, false => `(funBinder| {$curNames*})
| BinderInfo.instImplicit, _ => `(funBinder| [$curNames.back : $stxT]) -- here `curNames.size == 1`
| _ , _ => unreachable!;
match stxBody with
| `(fun $binderGroups* => $stxBody) => `(fun $group $binderGroups* => $stxBody)
| _ => `(fun $group => $stxBody)
@[builtinDelab forallE]
def delabForall : Delab :=
delabBinders fun curNames stxBody => do
let e ← getExpr
let prop ← try isProp e catch _ => false
let stxT ← withBindingDomain delab
let group ← match e.binderInfo with
| BinderInfo.implicit => `(bracketedBinderF|{$curNames* : $stxT})
-- here `curNames.size == 1`
| BinderInfo.instImplicit => `(bracketedBinderF|[$curNames.back : $stxT])
| _ =>
-- heuristic: use non-dependent arrows only if possible for whole group to avoid
-- noisy mix like `(α : Type) → Type → (γ : Type) → ...`.
let dependent := curNames.any $ fun n => hasIdent n.getId stxBody
-- NOTE: non-dependent arrows are available only for the default binder info
if dependent then
if prop && !(← getPPOption getPPBinderTypes) then
return ← `(∀ $curNames:ident*, $stxBody)
else
`(bracketedBinderF|($curNames* : $stxT))
else
return ← curNames.foldrM (fun _ stxBody => `($stxT → $stxBody)) stxBody
if prop then
match stxBody with
| `(∀ $groups*, $stxBody) => `(∀ $group $groups*, $stxBody)
| _ => `(∀ $group, $stxBody)
else
`($group:bracketedBinder → $stxBody)
@[builtinDelab letE]
def delabLetE : Delab := do
let Expr.letE n t v b _ ← getExpr | unreachable!
let n ← getUnusedName n b
let stxT ← descend t 0 delab
let stxV ← descend v 1 delab
let stxB ← withLetDecl n t v fun fvar =>
let b := b.instantiate1 fvar
descend b 2 delab
`(let $(mkIdent n) : $stxT := $stxV; $stxB)
@[builtinDelab lit]
def delabLit : Delab := do
let Expr.lit l _ ← getExpr | unreachable!
match l with
| Literal.natVal n => pure $ quote n
| Literal.strVal s => pure $ quote s
-- `@OfNat.ofNat _ n _` ~> `n`
@[builtinDelab app.OfNat.ofNat]
def delabOfNat : Delab := whenPPOption getPPCoercions do
let (Expr.app (Expr.app _ (Expr.lit (Literal.natVal n) _) _) _ _) ← getExpr | failure
return quote n
-- `@OfDecimal.ofDecimal _ _ m s e` ~> `m*10^(sign * e)` where `sign == 1` if `s = false` and `sign = -1` if `s = true`
@[builtinDelab app.OfScientific.ofScientific]
def delabOfScientific : Delab := whenPPOption getPPCoercions do
let expr ← getExpr
guard <| expr.getAppNumArgs == 5
let Expr.lit (Literal.natVal m) _ ← pure (expr.getArg! 2) | failure
let Expr.lit (Literal.natVal e) _ ← pure (expr.getArg! 4) | failure
let s ← match expr.getArg! 3 with
| Expr.const `Bool.true _ _ => pure true
| Expr.const `Bool.false _ _ => pure false
| _ => failure
let str := toString m
if s && e == str.length then
return Syntax.mkScientificLit ("0." ++ str)
else if s && e < str.length then
let mStr := str.extract 0 (str.length - e)
let eStr := str.extract (str.length - e) str.length
return Syntax.mkScientificLit (mStr ++ "." ++ eStr)
else
return Syntax.mkScientificLit (str ++ "e" ++ (if s then "-" else "") ++ toString e)
/--
Delaborate a projection primitive. These do not usually occur in
user code, but are pretty-printed when e.g. `#print`ing a projection
function.
-/
@[builtinDelab proj]
def delabProj : Delab := do
let Expr.proj _ idx _ _ ← getExpr | unreachable!
let e ← withProj delab
-- not perfectly authentic: elaborates to the `idx`-th named projection
-- function (e.g. `e.1` is `Prod.fst e`), which unfolds to the actual
-- `proj`.
let idx := Syntax.mkLit fieldIdxKind (toString (idx + 1));
`($(e).$idx:fieldIdx)
/-- Delaborate a call to a projection function such as `Prod.fst`. -/
@[builtinDelab app]
def delabProjectionApp : Delab := whenPPOption getPPStructureProjections $ do
let e@(Expr.app fn _ _) ← getExpr | failure
let Expr.const c@(Name.str _ f _) _ _ ← pure fn.getAppFn | failure
let env ← getEnv
let some info ← pure $ env.getProjectionFnInfo? c | failure
-- can't use with classes since the instance parameter is implicit
guard $ !info.fromClass
-- projection function should be fully applied (#struct params + 1 instance parameter)
-- TODO: support over-application
guard $ e.getAppNumArgs == info.nparams + 1
-- If pp.explicit is true, and the structure has parameters, we should not
-- use field notation because we will not be able to see the parameters.
let expl ← getPPOption getPPExplicit
guard $ !expl || info.nparams == 0
let appStx ← withAppArg delab
`($(appStx).$(mkIdent f):ident)
@[builtinDelab app]
def delabStructureInstance : Delab := whenPPOption getPPStructureInstances do
let env ← getEnv
let e ← getExpr
let some s ← pure $ e.isConstructorApp? env | failure
guard $ isStructure env s.induct;
/- If implicit arguments should be shown, and the structure has parameters, we should not
pretty print using { ... }, because we will not be able to see the parameters. -/
let explicit ← getPPOption getPPExplicit
guard !(explicit && s.numParams > 0)
let fieldNames := getStructureFields env s.induct
let (_, fields) ← withAppFnArgs (pure (0, #[])) fun ⟨idx, fields⟩ => do
if idx < s.numParams then
pure (idx + 1, fields)
else
let val ← delab
let field ← `(structInstField|$(mkIdent <| fieldNames.get! (idx - s.numParams)):ident := $val)
pure (idx + 1, fields.push field)
let lastField := fields[fields.size - 1]
let fields := fields.pop
let ty ←
if (← getPPOption getPPStructureInstanceType) then
let ty ← inferType e
-- `ty` is not actually part of `e`, but since `e` must be an application or constant, we know that
-- index 2 is unused.
pure <| some (← descend ty 2 delab)
else pure <| none
`({ $[$fields, ]* $lastField $[: $ty]? })
@[builtinDelab app.Prod.mk]
def delabTuple : Delab := whenPPOption getPPNotation do
let e ← getExpr
guard $ e.getAppNumArgs == 4
let a ← withAppFn $ withAppArg delab
let b ← withAppArg delab
match b with
| `(($b, $bs,*)) => `(($a, $b, $bs,*))
| _ => `(($a, $b))
-- abbrev coe {α : Sort u} {β : Sort v} (a : α) [CoeT α a β] : β
@[builtinDelab app.coe]
def delabCoe : Delab := whenPPOption getPPCoercions do
let e ← getExpr
guard $ e.getAppNumArgs >= 4
-- delab as application, then discard function
let stx ← delabAppImplicit
match stx with
| `($fn $arg) => arg
| `($fn $args*) => `($(args.get! 0) $(args.eraseIdx 0)*)
| _ => failure
-- abbrev coeFun {α : Sort u} {γ : α → Sort v} (a : α) [CoeFun α γ] : γ a
@[builtinDelab app.coeFun]
def delabCoeFun : Delab := delabCoe
@[builtinDelab app.List.nil]
def delabNil : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 1
`([])
@[builtinDelab app.List.cons]
def delabConsList : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 3
let x ← withAppFn (withAppArg delab)
match (← withAppArg delab) with
| `([]) => `([$x])
| `([$xs,*]) => `([$x, $xs,*])
| _ => failure
@[builtinDelab app.List.toArray]
def delabListToArray : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 2
match (← withAppArg delab) with
| `([$xs,*]) => `(#[$xs,*])
| _ => failure
@[builtinDelab app.ite]
def delabIte : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 5
let c ← withAppFn $ withAppFn $ withAppFn $ withAppArg delab
let t ← withAppFn $ withAppArg delab
let e ← withAppArg delab
`(if $c then $t else $e)
@[builtinDelab app.dite]
def delabDIte : Delab := whenPPOption getPPNotation do
guard $ (← getExpr).getAppNumArgs == 5
let c ← withAppFn $ withAppFn $ withAppFn $ withAppArg delab
let (t, h) ← withAppFn $ withAppArg $ delabBranch none
let (e, _) ← withAppArg $ delabBranch h
`(if $(mkIdent h):ident : $c then $t else $e)
where
delabBranch (h? : Option Name) : DelabM (Syntax × Name) := do
let e ← getExpr
guard e.isLambda
let h ← match h? with
| some h => return (← withBindingBody h delab, h)
| none => withBindingBodyUnusedName fun h => do
return (← delab, h.getId)
@[builtinDelab app.namedPattern]
def delabNamedPattern : Delab := do
guard $ (← getExpr).getAppNumArgs == 3
let x ← withAppFn $ withAppArg delab
let p ← withAppArg delab
guard x.isIdent
`($x:ident@$p:term)
partial def delabDoElems : DelabM (List Syntax) := do
let e ← getExpr
if e.isAppOfArity `Bind.bind 6 then
-- Bind.bind.{u, v} : {m : Type u → Type v} → [self : Bind m] → {α β : Type u} → m α → (α → m β) → m β
let ma ← withAppFn $ withAppArg delab
withAppArg do
match (← getExpr) with
| Expr.lam _ _ body _ =>
withBindingBodyUnusedName fun n => do
if body.hasLooseBVars then
prependAndRec `(doElem|let $n:term ← $ma)
else
prependAndRec `(doElem|$ma:term)
| _ => failure
else if e.isLet then
let Expr.letE n t v b _ ← getExpr | unreachable!
let n ← getUnusedName n b
let stxT ← descend t 0 delab
let stxV ← descend v 1 delab
withLetDecl n t v fun fvar =>
let b := b.instantiate1 fvar
descend b 2 $
prependAndRec `(doElem|let $(mkIdent n) : $stxT := $stxV)
else
let stx ← delab
[←`(doElem|$stx:term)]
where
prependAndRec x : DelabM _ := List.cons <$> x <*> delabDoElems
@[builtinDelab app.Bind.bind]
def delabDo : Delab := whenPPOption getPPNotation do
guard <| (← getExpr).isAppOfArity `Bind.bind 6
let elems ← delabDoElems
let items ← elems.toArray.mapM (`(doSeqItem|$(·):doElem))
`(do $items:doSeqItem*)
@[builtinDelab app.sorryAx]
def delabSorryAx : Delab := whenPPOption getPPNotation do
guard <| (← getExpr).isAppOfArity ``sorryAx 2
`(sorry)
@[builtinDelab app.Eq.ndrec]
def delabEqNDRec : Delab := whenPPOption getPPNotation do
guard <| (← getExpr).getAppNumArgs == 6
-- Eq.ndrec.{u1, u2} : {α : Sort u2} → {a : α} → {motive : α → Sort u1} → (m : motive a) → {b : α} → (h : a = b) → motive b
let m ← withAppFn <| withAppFn <| withAppArg delab
let h ← withAppArg delab
`($h ▸ $m)
@[builtinDelab app.Eq.rec]
def delabEqRec : Delab :=
-- relevant signature parts as in `Eq.ndrec`
delabEqNDRec
def reifyName : Expr → DelabM Name
| Expr.const ``Lean.Name.anonymous .. => Name.anonymous
| Expr.app (Expr.app (Expr.const ``Lean.Name.mkStr ..) n _) (Expr.lit (Literal.strVal s) _) _ => do
(← reifyName n).mkStr s
| Expr.app (Expr.app (Expr.const ``Lean.Name.mkNum ..) n _) (Expr.lit (Literal.natVal i) _) _ => do
(← reifyName n).mkNum i
| _ => failure
@[builtinDelab app.Lean.Name.mkStr]
def delabNameMkStr : Delab := whenPPOption getPPNotation do
let n ← reifyName (← getExpr)
-- not guaranteed to be a syntactically valid name, but usually more helpful than the explicit version
mkNode ``Lean.Parser.Term.quotedName #[Syntax.mkNameLit s!"`{n}"]
@[builtinDelab app.Lean.Name.mkNum]
def delabNameMkNum : Delab := delabNameMkStr
end Lean.PrettyPrinter.Delaborator
|
db19c69c7198f1ca4f93883128e24c785d453b54 | 1dd482be3f611941db7801003235dc84147ec60a | /src/data/finmap.lean | e7c2f086b29a98d8addfdc2b1cc197f6c0cf70ce | [
"Apache-2.0"
] | permissive | sanderdahmen/mathlib | 479039302bd66434bb5672c2a4cecf8d69981458 | 8f0eae75cd2d8b7a083cf935666fcce4565df076 | refs/heads/master | 1,587,491,322,775 | 1,549,672,060,000 | 1,549,672,060,000 | 169,748,224 | 0 | 0 | Apache-2.0 | 1,549,636,694,000 | 1,549,636,694,000 | null | UTF-8 | Lean | false | false | 7,488 | lean | /-
Copyright (c) 2018 Sean Leather. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sean Leather, Mario Carneiro
Finite maps over `multiset`.
-/
import data.list.alist data.finset data.pfun
universes u v w
open list
variables {α : Type u} {β : α → Type v}
namespace multiset
/-- `nodupkeys s` means that `s` has no duplicate keys. -/
def nodupkeys (s : multiset (sigma β)) : Prop :=
quot.lift_on s list.nodupkeys (λ s t p, propext $ perm_nodupkeys p)
@[simp] theorem coe_nodupkeys {l : list (sigma β)} : @nodupkeys α β l ↔ l.nodupkeys := iff.rfl
end multiset
/-- `finmap β` is the type of finite maps over a multiset. It is effectively
a quotient of `alist β` by permutation of the underlying list. -/
structure finmap (β : α → Type v) : Type (max u v) :=
(entries : multiset (sigma β))
(nodupkeys : entries.nodupkeys)
/-- The quotient map from `alist` to `finmap`. -/
def alist.to_finmap (s : alist β) : finmap β := ⟨s.entries, s.nodupkeys⟩
local notation `⟦`:max a `⟧`:0 := alist.to_finmap a
theorem alist.to_finmap_eq {s₁ s₂ : alist β} :
⟦s₁⟧ = ⟦s₂⟧ ↔ s₁.entries ~ s₂.entries :=
by cases s₁; cases s₂; simp [alist.to_finmap]
@[simp] theorem alist.to_finmap_entries (s : alist β) : ⟦s⟧.entries = s.entries := rfl
namespace finmap
open alist
/-- Lift a permutation-respecting function on `alist` to `finmap`. -/
@[elab_as_eliminator] def lift_on
{γ} (s : finmap β) (f : alist β → γ)
(H : ∀ a b : alist β, a.entries ~ b.entries → f a = f b) : γ :=
begin
refine (quotient.lift_on s.1 (λ l, (⟨_, λ nd, f ⟨l, nd⟩⟩ : roption γ))
(λ l₁ l₂ p, roption.ext' (perm_nodupkeys p) _) : roption γ).get _,
{ exact λ h₁ h₂, H _ _ (by exact p) },
{ have := s.nodupkeys, rcases s.entries with ⟨l⟩, exact id }
end
@[simp] theorem lift_on_to_finmap {γ} (s : alist β) (f : alist β → γ) (H) :
lift_on ⟦s⟧ f H = f s := by cases s; refl
@[elab_as_eliminator] theorem induction_on
{C : finmap β → Prop} (s : finmap β) (H : ∀ (a : alist β), C ⟦a⟧) : C s :=
by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩
@[extensionality] theorem ext : ∀ {s t : finmap β}, s.entries = t.entries → s = t
| ⟨l₁, h₁⟩ ⟨l₂, h₂⟩ H := by congr'
/-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/
instance : has_mem α (finmap β) := ⟨λ a s, ∃ b : β a, sigma.mk a b ∈ s.entries⟩
theorem mem_def {a : α} {s : finmap β} :
a ∈ s ↔ ∃ b : β a, sigma.mk a b ∈ s.entries := iff.rfl
@[simp] theorem mem_to_finmap {a : α} {s : alist β} :
a ∈ ⟦s⟧ ↔ a ∈ s := iff.rfl
/-- The set of keys of a finite map. -/
def keys (s : finmap β) : finset α :=
⟨s.entries.map sigma.fst, induction_on s $ λ s, s.keys_nodup⟩
@[simp] theorem keys_val (s : alist β) : (keys ⟦s⟧).val = s.keys := rfl
@[simp] theorem keys_ext {s₁ s₂ : alist β} :
keys ⟦s₁⟧ = keys ⟦s₂⟧ ↔ s₁.keys ~ s₂.keys :=
by simp [keys, alist.keys]
theorem mem_keys {a : α} {s : finmap β} : a ∈ s.keys ↔ a ∈ s :=
induction_on s $ λ s, mem_keys
/-- The empty map. -/
instance : has_emptyc (finmap β) := ⟨⟨0, nodupkeys_nil⟩⟩
@[simp] theorem empty_to_finmap (s : alist β) :
(⟦∅⟧ : finmap β) = ∅ := rfl
theorem not_mem_empty_entries {s : sigma β} : s ∉ (∅ : finmap β).entries :=
multiset.not_mem_zero _
theorem not_mem_empty {a : α} : a ∉ (∅ : finmap β) :=
λ ⟨b, h⟩, not_mem_empty_entries h
@[simp] theorem keys_empty : (∅ : finmap β).keys = ∅ := rfl
/-- The singleton map. -/
def singleton (a : α) (b : β a) : finmap β :=
⟨⟨a, b⟩::0, nodupkeys_singleton _⟩
@[simp] theorem keys_singleton (a : α) (b : β a) :
(singleton a b).keys = finset.singleton a := rfl
variables [decidable_eq α]
/-- Look up the value associated to a key in a map. -/
def lookup (a : α) (s : finmap β) : option (β a) :=
lift_on s (lookup a) (λ s t, perm_lookup)
@[simp] theorem lookup_to_finmap (a : α) (s : alist β) :
lookup a ⟦s⟧ = s.lookup a := rfl
theorem lookup_is_some {a : α} {s : finmap β} :
(s.lookup a).is_some ↔ a ∈ s :=
induction_on s $ λ s, alist.lookup_is_some
instance (a : α) (s : finmap β) : decidable (a ∈ s) :=
decidable_of_iff _ lookup_is_some
/-- Insert a key-value pair into a finite map.
If the key is already present it does nothing. -/
def insert (a : α) (b : β a) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦insert a b t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_insert p
@[simp] theorem insert_to_finmap (a : α) (b : β a) (s : alist β) :
insert a b ⟦s⟧ = ⟦s.insert a b⟧ := by simp [insert]
@[simp] theorem insert_of_pos {a : α} {b : β a} {s : finmap β} : a ∈ s →
insert a b s = s :=
induction_on s $ λ ⟨s, nd⟩ h, congr_arg to_finmap $
insert_of_pos (mem_to_finmap.2 h)
theorem insert_entries_of_neg {a : α} {b : β a} {s : finmap β} : a ∉ s →
(insert a b s).entries = ⟨a, b⟩ :: s.entries :=
induction_on s $ λ s h,
by simp [insert_entries_of_neg (mt mem_to_finmap.1 h)]
@[simp] theorem mem_insert {a a' : α} {b : β a} {s : finmap β} :
a' ∈ insert a b s ↔ a' = a ∨ a' ∈ s :=
induction_on s $ by simp
/-- Replace a key with a given value in a finite map.
If the key is not present it does nothing. -/
def replace (a : α) (b : β a) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦replace a b t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_replace p
@[simp] theorem replace_to_finmap (a : α) (b : β a) (s : alist β) :
replace a b ⟦s⟧ = ⟦s.replace a b⟧ := by simp [replace]
@[simp] theorem keys_replace (a : α) (b : β a) (s : finmap β) :
(replace a b s).keys = s.keys :=
induction_on s $ λ s, by simp
@[simp] theorem mem_replace {a a' : α} {b : β a} {s : finmap β} :
a' ∈ replace a b s ↔ a' ∈ s :=
induction_on s $ λ s, by simp
/-- Fold a commutative function over the key-value pairs in the map -/
def foldl {δ : Type w} (f : δ → Π a, β a → δ)
(H : ∀ d a₁ b₁ a₂ b₂, f (f d a₁ b₁) a₂ b₂ = f (f d a₂ b₂) a₁ b₁)
(d : δ) (m : finmap β) : δ :=
m.entries.foldl (λ d s, f d s.1 s.2) (λ d s t, H _ _ _ _ _) d
/-- Erase a key from the map. If the key is not present it does nothing. -/
def erase (a : α) (s : finmap β) : finmap β :=
lift_on s (λ t, ⟦erase a t⟧) $
λ s₁ s₂ p, to_finmap_eq.2 $ perm_erase p
@[simp] theorem erase_to_finmap (a : α) (s : alist β) :
erase a ⟦s⟧ = ⟦s.erase a⟧ := by simp [erase]
@[simp] theorem keys_erase_to_finset (a : α) (s : alist β) :
keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a :=
by simp [finset.erase, keys, alist.erase, list.kerase_map_fst]
@[simp] theorem keys_erase (a : α) (s : finmap β) :
(erase a s).keys = s.keys.erase a :=
induction_on s $ λ s, by simp
@[simp] theorem mem_erase {a a' : α} {s : finmap β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s :=
induction_on s $ λ s, by simp
/-- Erase a key from the map, and return the corresponding value, if found. -/
def extract (a : α) (s : finmap β) : option (β a) × finmap β :=
lift_on s (λ t, prod.map id to_finmap (extract a t)) $
λ s₁ s₂ p, by simp [perm_lookup p, to_finmap_eq, perm_erase p]
@[simp] theorem extract_eq_lookup_erase (a : α) (s : finmap β) :
extract a s = (lookup a s, erase a s) :=
induction_on s $ λ s, by simp [extract]
end finmap
|
efc005e3dda7def9c04339aca5f3bf858a6969e4 | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/data/nat/totient.lean | b1fccbdf88a80f1ca9441ebe4a87b9c483881a5b | [
"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 | 14,625 | 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 algebra.char_p.two
import data.nat.factorization.basic
import data.nat.periodic
import data.zmod.basic
/-!
# Euler's totient function
This file defines [Euler's totient function](https://en.wikipedia.org/wiki/Euler's_totient_function)
`nat.totient n` which counts the number of naturals less than `n` that are coprime with `n`.
We prove the divisor sum formula, namely that `n` equals `φ` summed over the divisors of `n`. See
`sum_totient`. We also prove two lemmas to help compute totients, namely `totient_mul` and
`totient_prime_pow`.
-/
open finset
open_locale big_operators
namespace nat
/-- Euler's totient function. This counts the number of naturals strictly less than `n` which are
coprime with `n`. -/
def totient (n : ℕ) : ℕ := ((range n).filter n.coprime).card
localized "notation (name := nat.totient) `φ` := nat.totient" in nat
@[simp] theorem totient_zero : φ 0 = 0 := rfl
@[simp] theorem totient_one : φ 1 = 1 :=
by simp [totient]
lemma totient_eq_card_coprime (n : ℕ) : φ n = ((range n).filter n.coprime).card := rfl
lemma totient_le (n : ℕ) : φ n ≤ n :=
((range n).card_filter_le _).trans_eq (card_range n)
lemma totient_lt (n : ℕ) (hn : 1 < n) : φ n < n :=
(card_lt_card (filter_ssubset.2 ⟨0, by simp [hn.ne', pos_of_gt hn]⟩)).trans_eq (card_range n)
lemma totient_pos : ∀ {n : ℕ}, 0 < n → 0 < φ n
| 0 := dec_trivial
| 1 := by simp [totient]
| (n+2) := λ h, card_pos.2 ⟨1, mem_filter.2 ⟨mem_range.2 dec_trivial, coprime_one_right _⟩⟩
lemma filter_coprime_Ico_eq_totient (a n : ℕ) :
((Ico n (n+a)).filter (coprime a)).card = totient a :=
begin
rw [totient, filter_Ico_card_eq_of_periodic, count_eq_card_filter_range],
exact periodic_coprime a,
end
lemma Ico_filter_coprime_le {a : ℕ} (k n : ℕ) (a_pos : 0 < a) :
((Ico k (k + n)).filter (coprime a)).card ≤ totient a * (n / a + 1) :=
begin
conv_lhs { rw ←nat.mod_add_div n a },
induction n / a with i ih,
{ rw ←filter_coprime_Ico_eq_totient a k,
simp only [add_zero, mul_one, mul_zero, le_of_lt (mod_lt n a_pos)],
mono,
refine monotone_filter_left a.coprime _,
simp only [finset.le_eq_subset],
exact Ico_subset_Ico rfl.le (add_le_add_left (le_of_lt (mod_lt n a_pos)) k), },
simp only [mul_succ],
simp_rw ←add_assoc at ih ⊢,
calc (filter a.coprime (Ico k (k + n % a + a * i + a))).card
= (filter a.coprime (Ico k (k + n % a + a * i)
∪ Ico (k + n % a + a * i) (k + n % a + a * i + a))).card :
begin
congr,
rw Ico_union_Ico_eq_Ico,
rw add_assoc,
exact le_self_add,
exact le_self_add,
end
... ≤ (filter a.coprime (Ico k (k + n % a + a * i))).card + a.totient :
begin
rw [filter_union, ←filter_coprime_Ico_eq_totient a (k + n % a + a * i)],
apply card_union_le,
end
... ≤ a.totient * i + a.totient + a.totient : add_le_add_right ih (totient a),
end
open zmod
/-- Note this takes an explicit `fintype ((zmod n)ˣ)` argument to avoid trouble with instance
diamonds. -/
@[simp] lemma _root_.zmod.card_units_eq_totient (n : ℕ) [ne_zero n] [fintype ((zmod n)ˣ)] :
fintype.card ((zmod n)ˣ) = φ n :=
calc fintype.card ((zmod n)ˣ) = fintype.card {x : zmod n // x.val.coprime n} :
fintype.card_congr zmod.units_equiv_coprime
... = φ n :
begin
unfreezingI { obtain ⟨m, rfl⟩ : ∃ m, n = m + 1 := exists_eq_succ_of_ne_zero ne_zero.out },
simp only [totient, finset.card_eq_sum_ones, fintype.card_subtype, finset.sum_filter,
← fin.sum_univ_eq_sum_range, @nat.coprime_comm (m + 1)],
refl
end
lemma totient_even {n : ℕ} (hn : 2 < n) : even n.totient :=
begin
haveI : fact (1 < n) := ⟨one_lt_two.trans hn⟩,
haveI : ne_zero n := ne_zero.of_gt hn,
suffices : 2 = order_of (-1 : (zmod n)ˣ),
{ rw [← zmod.card_units_eq_totient, even_iff_two_dvd, this], exact order_of_dvd_card_univ },
rw [←order_of_units, units.coe_neg_one, order_of_neg_one, ring_char.eq (zmod n) n, if_neg hn.ne'],
end
lemma totient_mul {m n : ℕ} (h : m.coprime n) : φ (m * n) = φ m * φ n :=
if hmn0 : m * n = 0
then by cases nat.mul_eq_zero.1 hmn0 with h h;
simp only [totient_zero, mul_zero, zero_mul, h]
else
begin
haveI : ne_zero (m * n) := ⟨hmn0⟩,
haveI : ne_zero m := ⟨left_ne_zero_of_mul hmn0⟩,
haveI : ne_zero n := ⟨right_ne_zero_of_mul hmn0⟩,
simp only [← zmod.card_units_eq_totient],
rw [fintype.card_congr (units.map_equiv (zmod.chinese_remainder h).to_mul_equiv).to_equiv,
fintype.card_congr (@mul_equiv.prod_units (zmod m) (zmod n) _ _).to_equiv,
fintype.card_prod]
end
/-- For `d ∣ n`, the totient of `n/d` equals the number of values `k < n` such that `gcd n k = d` -/
lemma totient_div_of_dvd {n d : ℕ} (hnd : d ∣ n) :
φ (n/d) = (filter (λ (k : ℕ), n.gcd k = d) (range n)).card :=
begin
rcases d.eq_zero_or_pos with rfl | hd0, { simp [eq_zero_of_zero_dvd hnd] },
rcases hnd with ⟨x, rfl⟩,
rw nat.mul_div_cancel_left x hd0,
apply finset.card_congr (λ k _, d * k),
{ simp only [mem_filter, mem_range, and_imp, coprime],
refine λ a ha1 ha2, ⟨(mul_lt_mul_left hd0).2 ha1, _⟩,
rw [gcd_mul_left, ha2, mul_one] },
{ simp [hd0.ne'] },
{ simp only [mem_filter, mem_range, exists_prop, and_imp],
refine λ b hb1 hb2, _,
have : d ∣ b, { rw ←hb2, apply gcd_dvd_right },
rcases this with ⟨q, rfl⟩,
refine ⟨q, ⟨⟨(mul_lt_mul_left hd0).1 hb1, _⟩, rfl⟩⟩,
rwa [gcd_mul_left, mul_right_eq_self_iff hd0] at hb2 },
end
lemma sum_totient (n : ℕ) : n.divisors.sum φ = n :=
begin
rcases n.eq_zero_or_pos with rfl | hn, { simp },
rw ←sum_div_divisors n φ,
have : n = ∑ (d : ℕ) in n.divisors, (filter (λ (k : ℕ), n.gcd k = d) (range n)).card,
{ nth_rewrite_lhs 0 ←card_range n,
refine card_eq_sum_card_fiberwise (λ x hx, mem_divisors.2 ⟨_, hn.ne'⟩),
apply gcd_dvd_left },
nth_rewrite_rhs 0 this,
exact sum_congr rfl (λ x hx, totient_div_of_dvd (dvd_of_mem_divisors hx)),
end
lemma sum_totient' (n : ℕ) : ∑ m in (range n.succ).filter (∣ n), φ m = n :=
begin
convert sum_totient _ using 1,
simp only [nat.divisors, sum_filter, range_eq_Ico],
rw sum_eq_sum_Ico_succ_bot; simp
end
/-- When `p` is prime, then the totient of `p ^ (n + 1)` is `p ^ n * (p - 1)` -/
lemma totient_prime_pow_succ {p : ℕ} (hp : p.prime) (n : ℕ) :
φ (p ^ (n + 1)) = p ^ n * (p - 1) :=
calc φ (p ^ (n + 1))
= ((range (p ^ (n + 1))).filter (coprime (p ^ (n + 1)))).card :
totient_eq_card_coprime _
... = (range (p ^ (n + 1)) \ ((range (p ^ n)).image (* p))).card :
congr_arg card begin
rw [sdiff_eq_filter],
apply filter_congr,
simp only [mem_range, mem_filter, coprime_pow_left_iff n.succ_pos,
mem_image, not_exists, hp.coprime_iff_not_dvd],
intros a ha,
split,
{ rintros hap b _ rfl,
exact hap (dvd_mul_left _ _) },
{ rintros h ⟨b, rfl⟩,
rw [pow_succ] at ha,
exact h b (lt_of_mul_lt_mul_left ha (zero_le _)) (mul_comm _ _) }
end
... = _ :
have h1 : set.inj_on (* p) (range (p ^ n)),
from λ x _ y _, (nat.mul_left_inj hp.pos).1,
have h2 : (range (p ^ n)).image (* p) ⊆ range (p ^ (n + 1)),
from λ a, begin
simp only [mem_image, mem_range, exists_imp_distrib],
rintros b h rfl,
rw [pow_succ'],
exact (mul_lt_mul_right hp.pos).2 h
end,
begin
rw [card_sdiff h2, card_image_of_inj_on h1, card_range,
card_range, ← one_mul (p ^ n), pow_succ, ← tsub_mul,
one_mul, mul_comm]
end
/-- When `p` is prime, then the totient of `p ^ n` is `p ^ (n - 1) * (p - 1)` -/
lemma totient_prime_pow {p : ℕ} (hp : p.prime) {n : ℕ} (hn : 0 < n) :
φ (p ^ n) = p ^ (n - 1) * (p - 1) :=
by rcases exists_eq_succ_of_ne_zero (pos_iff_ne_zero.1 hn) with ⟨m, rfl⟩;
exact totient_prime_pow_succ hp _
lemma totient_prime {p : ℕ} (hp : p.prime) : φ p = p - 1 :=
by rw [← pow_one p, totient_prime_pow hp]; simp
lemma totient_eq_iff_prime {p : ℕ} (hp : 0 < p) : p.totient = p - 1 ↔ p.prime :=
begin
refine ⟨λ h, _, totient_prime⟩,
replace hp : 1 < p,
{ apply lt_of_le_of_ne,
{ rwa succ_le_iff },
{ rintro rfl,
rw [totient_one, tsub_self] at h,
exact one_ne_zero h } },
rw [totient_eq_card_coprime, range_eq_Ico, ←Ico_insert_succ_left hp.le, finset.filter_insert,
if_neg (not_coprime_of_dvd_of_dvd hp (dvd_refl p) (dvd_zero p)), ←nat.card_Ico 1 p] at h,
refine p.prime_of_coprime hp (λ n hn hnz, finset.filter_card_eq h n $ finset.mem_Ico.mpr ⟨_, hn⟩),
rwa [succ_le_iff, pos_iff_ne_zero],
end
lemma card_units_zmod_lt_sub_one {p : ℕ} (hp : 1 < p) [fintype ((zmod p)ˣ)] :
fintype.card ((zmod p)ˣ) ≤ p - 1 :=
begin
haveI : ne_zero p := ⟨(pos_of_gt hp).ne'⟩,
rw zmod.card_units_eq_totient p,
exact nat.le_pred_of_lt (nat.totient_lt p hp),
end
lemma prime_iff_card_units (p : ℕ) [fintype ((zmod p)ˣ)] :
p.prime ↔ fintype.card ((zmod p)ˣ) = p - 1 :=
begin
casesI eq_zero_or_ne_zero p with hp hp,
{ substI hp,
simp only [zmod, not_prime_zero, false_iff, zero_tsub],
-- the substI created an non-defeq but subsingleton instance diamond; resolve it
suffices : fintype.card ℤˣ ≠ 0, { convert this },
simp },
rw [zmod.card_units_eq_totient, nat.totient_eq_iff_prime $ ne_zero.pos p],
end
@[simp] lemma totient_two : φ 2 = 1 :=
(totient_prime prime_two).trans rfl
lemma totient_eq_one_iff : ∀ {n : ℕ}, n.totient = 1 ↔ n = 1 ∨ n = 2
| 0 := by simp
| 1 := by simp
| 2 := by simp
| (n+3) :=
begin
have : 3 ≤ n + 3 := le_add_self,
simp only [succ_succ_ne_one, false_or],
exact ⟨λ h, not_even_one.elim $ h ▸ totient_even this, by rintro ⟨⟩⟩,
end
/-! ### Euler's product formula for the totient function
We prove several different statements of this formula. -/
/-- Euler's product formula for the totient function. -/
theorem totient_eq_prod_factorization {n : ℕ} (hn : n ≠ 0) :
φ n = n.factorization.prod (λ p k, p ^ (k - 1) * (p - 1)) :=
begin
rw multiplicative_factorization φ @totient_mul totient_one hn,
apply finsupp.prod_congr (λ p hp, _),
have h := zero_lt_iff.mpr (finsupp.mem_support_iff.mp hp),
rw [totient_prime_pow (prime_of_mem_factorization hp) h],
end
/-- Euler's product formula for the totient function. -/
theorem totient_mul_prod_factors (n : ℕ) :
φ n * ∏ p in n.factors.to_finset, p = n * ∏ p in n.factors.to_finset, (p - 1) :=
begin
by_cases hn : n = 0, { simp [hn] },
rw totient_eq_prod_factorization hn,
nth_rewrite 2 ←factorization_prod_pow_eq_self hn,
simp only [←prod_factorization_eq_prod_factors, ←finsupp.prod_mul],
refine finsupp.prod_congr (λ p hp, _),
rw [finsupp.mem_support_iff, ← zero_lt_iff] at hp,
rw [mul_comm, ←mul_assoc, ←pow_succ, nat.sub_add_cancel hp],
end
/-- Euler's product formula for the totient function. -/
theorem totient_eq_div_factors_mul (n : ℕ) :
φ n = n / (∏ p in n.factors.to_finset, p) * (∏ p in n.factors.to_finset, (p - 1)) :=
begin
rw [← mul_div_left n.totient, totient_mul_prod_factors, mul_comm,
nat.mul_div_assoc _ (prod_prime_factors_dvd n), mul_comm],
simpa [prod_factorization_eq_prod_factors] using prod_pos (λ p, pos_of_mem_factorization),
end
/-- Euler's product formula for the totient function. -/
theorem totient_eq_mul_prod_factors (n : ℕ) :
(φ n : ℚ) = n * ∏ p in n.factors.to_finset, (1 - p⁻¹) :=
begin
by_cases hn : n = 0, { simp [hn] },
have hn' : (n : ℚ) ≠ 0, { simp [hn] },
have hpQ : ∏ p in n.factors.to_finset, (p : ℚ) ≠ 0,
{ rw [←cast_prod, cast_ne_zero, ←zero_lt_iff, ←prod_factorization_eq_prod_factors],
exact prod_pos (λ p hp, pos_of_mem_factorization hp) },
simp only [totient_eq_div_factors_mul n, prod_prime_factors_dvd n, cast_mul, cast_prod,
cast_div_char_zero, mul_comm_div, mul_right_inj' hn', div_eq_iff hpQ, ←prod_mul_distrib],
refine prod_congr rfl (λ p hp, _),
have hp := pos_of_mem_factors (list.mem_to_finset.mp hp),
have hp' : (p : ℚ) ≠ 0 := cast_ne_zero.mpr hp.ne.symm,
rw [sub_mul, one_mul, mul_comm, mul_inv_cancel hp', cast_pred hp],
end
lemma totient_gcd_mul_totient_mul (a b : ℕ) : φ (a.gcd b) * φ (a * b) = φ a * φ b * (a.gcd b) :=
begin
have shuffle : ∀ a1 a2 b1 b2 c1 c2 : ℕ, b1 ∣ a1 → b2 ∣ a2 →
(a1/b1 * c1) * (a2/b2 * c2) = (a1*a2)/(b1*b2) * (c1*c2),
{ intros a1 a2 b1 b2 c1 c2 h1 h2,
calc
(a1/b1 * c1) * (a2/b2 * c2) = ((a1/b1) * (a2/b2)) * (c1*c2) : by apply mul_mul_mul_comm
... = (a1*a2)/(b1*b2) * (c1*c2) : by { congr' 1, exact div_mul_div_comm h1 h2 } },
simp only [totient_eq_div_factors_mul],
rw [shuffle, shuffle],
rotate, repeat { apply prod_prime_factors_dvd },
{ simp only [prod_factors_gcd_mul_prod_factors_mul],
rw [eq_comm, mul_comm, ←mul_assoc, ←nat.mul_div_assoc],
exact mul_dvd_mul (prod_prime_factors_dvd a) (prod_prime_factors_dvd b) }
end
lemma totient_super_multiplicative (a b : ℕ) : φ a * φ b ≤ φ (a * b) :=
begin
let d := a.gcd b,
rcases (zero_le a).eq_or_lt with rfl | ha0, { simp },
have hd0 : 0 < d, from nat.gcd_pos_of_pos_left _ ha0,
rw [←mul_le_mul_right hd0, ←totient_gcd_mul_totient_mul a b, mul_comm],
apply mul_le_mul_left' (nat.totient_le d),
end
lemma totient_dvd_of_dvd {a b : ℕ} (h : a ∣ b) : φ a ∣ φ b :=
begin
rcases eq_or_ne a 0 with rfl | ha0, { simp [zero_dvd_iff.1 h] },
rcases eq_or_ne b 0 with rfl | hb0, { simp },
have hab' : a.factorization.support ⊆ b.factorization.support,
{ intro p,
simp only [support_factorization, list.mem_to_finset],
apply factors_subset_of_dvd h hb0 },
rw [totient_eq_prod_factorization ha0, totient_eq_prod_factorization hb0],
refine finsupp.prod_dvd_prod_of_subset_of_dvd hab' (λ p hp, mul_dvd_mul _ dvd_rfl),
exact pow_dvd_pow p (tsub_le_tsub_right ((factorization_le_iff_dvd ha0 hb0).2 h p) 1),
end
lemma totient_mul_of_prime_of_dvd {p n : ℕ} (hp : p.prime) (h : p ∣ n) :
(p * n).totient = p * n.totient :=
begin
have h1 := totient_gcd_mul_totient_mul p n,
rw [(gcd_eq_left h), mul_assoc] at h1,
simpa [(totient_pos hp.pos).ne', mul_comm] using h1,
end
lemma totient_mul_of_prime_of_not_dvd {p n : ℕ} (hp : p.prime) (h : ¬ p ∣ n) :
(p * n).totient = (p - 1) * n.totient :=
begin
rw [totient_mul _, totient_prime hp],
simpa [h] using coprime_or_dvd_of_prime hp n,
end
end nat
|
c776ea99fbe475dbdeff4317430467e82671d4cb | ecdf4e083eb363cd3a0d6880399f86e2cd7f5adb | /src/group_theory/euclidean_lattice.lean | 3a5aadf01994a40cede0d6619d7f788ac7e6e532 | [] | no_license | fpvandoorn/formalabstracts | 29aa71772da418f18994c38379e2192a6ef361f7 | cea2f9f96d89ee1187d1b01e33f22305cdfe4d59 | refs/heads/master | 1,609,476,761,601 | 1,558,130,287,000 | 1,558,130,287,000 | 97,261,457 | 0 | 2 | null | 1,550,879,230,000 | 1,500,056,313,000 | Lean | UTF-8 | Lean | false | false | 6,953 | lean | import .basic data.real.basic linear_algebra.basic ..data.dvector linear_algebra.basis
local notation h :: t := dvector.cons h t
local notation `[` l:(foldr `, ` (h t, dvector.cons h t) dvector.nil `]`) := l
/- In case we have to work with order-theoretic lattices later,
we'll use the term "Euclidean lattice" -/
/-- A Euclidean lattice of dimension n is a subset of ℝ^n which satisfies the following property: every element is a ℤ-linear combination of a basis for ℝ^n -/
def euclidean_space (n) := dvector ℝ n
instance euclidean_space_add_comm_group {n} : add_comm_group $ euclidean_space n :=
{ add := by {induction n, exact λ x y, [],
intros x y, cases x, cases y,
refine (_::(n_ih x_xs y_xs)), exact x_x + y_x},
add_assoc := λ _ _ _, omitted,
zero := by {induction n, exact [], exact (0::n_ih)},
zero_add := λ _, omitted,
add_zero := λ _, omitted,
neg := by {induction n, exact λ _, [], intro x, cases x, exact (-x_x :: n_ih x_xs)},
add_left_neg := λ _, omitted,
add_comm := omitted}
noncomputable instance euclidean_space_vector_space {n} : vector_space ℝ $ euclidean_space n :=
{ smul := by {induction n, exact λ _ _, [], intros r x, cases x, exact (r * x_x :: n_ih r x_xs)},
smul_add := omitted,
add_smul := omitted,
mul_smul := omitted,
one_smul := by omitted,
zero_smul := omitted,
smul_zero := omitted}
def inner_product : ∀ {n}, euclidean_space n → euclidean_space n → ℝ
| 0 _ _ := 0
| (n+1) (x::xs) (y::ys) := x*y + inner_product xs ys
def is_linear {n} (f : euclidean_space n → ℝ) :=
(∀ x y, f(x+y) = f x + f y) ∧ ∀ (r : ℝ) x, f(r • x) = r • (f x)
def is_multilinear {n k} (f : dvector (euclidean_space (n)) k → ℝ) : Prop :=
begin
induction k with k ih, exact ∀ x, f x = 0,
exact ∀ k' (xs : euclidean_space n) (xss : dvector (euclidean_space n) k),
is_linear $ λ xs, f $ dvector.insert xs k' xss
end
def is_alternating {n k} (f : dvector (euclidean_space (n)) k → ℝ) :=
∀ i j (h₁ : i < k) (h₂ : j < k) (xs : dvector (euclidean_space n) (k)),
xs.nth i h₁ = xs.nth j h₂ → f xs = 0
/-- The canonical inclusion from ℝ^n → ℝ^(n+1) given by inserting 0 at the end of all vectors -/
def euclidean_space_canonical_inclusion {n} : euclidean_space n → euclidean_space (n+1) :=
λ xs, by {induction xs, exact [0], exact xs_x :: xs_ih}
def identity_matrix : ∀(n), dvector (euclidean_space n) n
| 0 := []
| (n+1) := ((@identity_matrix n).map (λ xs, euclidean_space_canonical_inclusion xs)).concat $
(0 : euclidean_space n).concat 1
lemma identity_matrix_1 : identity_matrix 1 = [[1]] := by refl
lemma identity_matrix_2 : identity_matrix 2 = [[1,0],[0,1]] := by refl
def sends_identity_to_1 {n} (f : dvector (euclidean_space (n)) n → ℝ) : Prop :=
f (identity_matrix _) = 1
/-- The determinant is the unique alternating multilinear function which sends the identity
matrix to 1-/
def determinant_spec {n} : ∃! (f : dvector (euclidean_space n) n → ℝ), is_multilinear f ∧ is_alternating f ∧ sends_identity_to_1 f := omitted
noncomputable def determinant {n} : dvector (euclidean_space n) n → ℝ := (classical.indefinite_description _ determinant_spec).val
local notation `⟪`:90 x `,` y `⟫`:0 := inner_product x y
local notation `ℝ^^`:50 n:0 := euclidean_space n
instance ℤ_module_euclidean_space {n} : module ℤ (ℝ^^n) :=
{ smul := λ z x, by {induction x, exact [], exact z * x_x :: x_ih},
smul_add := omitted,
add_smul := omitted,
mul_smul := omitted,
one_smul := omitted,
zero_smul := omitted,
smul_zero := omitted }
/- An x : ℝ^^n is in the integral span of B if it can be written as a ℤ-linear combination of elements of B -/
def in_integral_span {n} (B : set ℝ^^n) : (ℝ^^n) → Prop :=
λ x, x ∈ submodule.span ℤ B
def is_euclidean_lattice {n : ℕ} (Λ : set ℝ^^n) := ∃ B : set ℝ^^n, is_basis ℝ B ∧ ∀ x ∈ Λ, in_integral_span B x
def euclidean_lattice (n : ℕ) := {Λ : set (ℝ^^n) // is_euclidean_lattice Λ}
def is_integer : set ℝ := (λ x : ℤ, x) '' set.univ
def is_even_integer : set ℝ := (λ x : ℤ, x) '' (λ z, ∃ w, 2 * w = z)
/-- A Euclidean lattice is even if for every x : Λ, ||x||^2 is an even integer -/
def even {n} (Λ : euclidean_lattice n) : Prop := ∀ x ∈ Λ.val, is_even_integer ⟪x ,x⟫
/-- A Euclidean lattice is unimodular if it has a basis with determinant 1 -/
def unimodular {n} (Λ : euclidean_lattice n) : Prop := ∃ B : (dvector (ℝ^^n) n), is_basis ℝ (B.to_set) ∧ ∀ x ∈ Λ.val, in_integral_span (B.to_set) x ∧ determinant B = 1
/-- The Leech lattice satisfies the property that the norm square of all of its nonzero
elements is greater than 4 -/
def nonzero_lengths_gt_2 {n} (Λ : euclidean_lattice n) : Prop :=
∀ x : ℝ^^n, x ∈ Λ.val → x ≠ 0 → ⟪x,x⟫ > 4
def GL (n) := linear_map.general_linear_group ℝ (ℝ^^n)
noncomputable instance GL_monoid (n) : monoid ((ℝ^^n) →ₗ[ℝ] ℝ^^n) :=
{ mul := λ f g, linear_map.comp f g,
mul_assoc := λ _, omitted,
one := { to_fun := id,
add := omitted,
smul := omitted },
one_mul := omitted,
mul_one := omitted}
noncomputable instance GL_mul (n) : has_mul $ GL n := ⟨λ f g, { val := linear_map.comp f.val g.val,
inv := linear_map.comp f.inv g.inv,
val_inv := omitted,
inv_val := omitted}⟩
noncomputable instance GL_inv (n) : has_inv $ GL n :=
⟨by {intro σ, cases σ,
exact { val := σ_inv,
inv := σ_val,
val_inv := σ_inv_val,
inv_val := σ_val_inv }}⟩
/-- An automorphism of an n-dimensional Euclidean lattice is a map in GL(n) which permutes Λ -/
def is_lattice_automorphism {n} {Λ : euclidean_lattice n} (σ : GL n) : Prop :=
set.bij_on (λ x : ℝ^^n, by cases σ; exact σ_val.to_fun x : (ℝ^^n) → (ℝ^^n)) Λ.val Λ.val
/- Source: https://groupprops.subwiki.org/wiki/Leech_lattice -/
/-- The Leech lattice is the unique even unimodular lattice Λ_24 in 24 dimensions
such that the length of every non-zero vector in Λ_24 is strictly greater than 2.
It is unique up to isomorphism, so any Λ_24 witnessing this existential assertion
will suffice.
-/
def leech_lattice_spec : ∃ Λ_24 : euclidean_lattice 24, even Λ_24 ∧ unimodular Λ_24 ∧ nonzero_lengths_gt_2 Λ_24 := omitted
noncomputable def Λ_24 := (classical.indefinite_description _ leech_lattice_spec).val
noncomputable def lattice_Aut {n} (Λ : euclidean_lattice n): Group :=
{ α := {σ // @is_lattice_automorphism _ Λ σ},
str := { mul := λ f g, ⟨f * g, omitted⟩,
mul_assoc := omitted,
one := { val := { val := { to_fun := id,
add := omitted,
smul := omitted },
inv := { to_fun := id,
add := omitted,
smul := omitted },
val_inv := omitted,
inv_val := omitted },
property := omitted },
one_mul := omitted,
mul_one := omitted,
inv := λ σ, ⟨σ.val ⁻¹, omitted⟩,
mul_left_inv := omitted }}
|
aa3bdb30c7aed9ed78d5244abeac2630e1af4f84 | a7dd8b83f933e72c40845fd168dde330f050b1c9 | /test/rcases.lean | 6c60ad1ebf60f139c47717b9bebd3bfb51ae4f93 | [
"Apache-2.0"
] | permissive | NeilStrickland/mathlib | 10420e92ee5cb7aba1163c9a01dea2f04652ed67 | 3efbd6f6dff0fb9b0946849b43b39948560a1ffe | refs/heads/master | 1,589,043,046,346 | 1,558,938,706,000 | 1,558,938,706,000 | 181,285,984 | 0 | 0 | Apache-2.0 | 1,568,941,848,000 | 1,555,233,833,000 | Lean | UTF-8 | Lean | false | false | 1,336 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import tactic.rcases
universe u
variables {α β γ : Type u}
example (x : α × β × γ) : true :=
begin
rcases x with ⟨a, b, c⟩,
{ guard_hyp a := α,
guard_hyp b := β,
guard_hyp c := γ,
trivial }
end
example (x : α × β × γ) : true :=
begin
rcases x with ⟨a, ⟨b, c⟩⟩,
{ guard_hyp a := α,
guard_hyp b := β,
guard_hyp c := γ,
trivial }
end
example (x : (α × β) × γ) : true :=
begin
rcases x with ⟨⟨a, b⟩, c⟩,
{ guard_hyp a := α,
guard_hyp b := β,
guard_hyp c := γ,
trivial }
end
example (x : inhabited α × option β ⊕ γ) : true :=
begin
rcases x with ⟨⟨a⟩, _ | b⟩ | c,
{ guard_hyp a := α, trivial },
{ guard_hyp a := α, guard_hyp b := β, trivial },
{ guard_hyp c := γ, trivial }
end
example (x y : ℕ) (h : x = y) : true :=
begin
rcases x with _|⟨⟩|z,
{ guard_hyp h := nat.zero = y, trivial },
{ guard_hyp h := nat.succ nat.zero = y, trivial },
{ guard_hyp z := ℕ,
guard_hyp h := z.succ.succ = y, trivial },
end
-- from equiv.sum_empty
example (s : α ⊕ empty) : true :=
begin
rcases s with _ | ⟨⟨⟩⟩,
{ guard_hyp s := α, trivial }
end
|
208b46b85c41a172ee9ee67149f08efc980b1e61 | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/functor/flat.lean | c03d26266d8e10c534dd39a18d089ee156a202a1 | [
"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 | 16,429 | lean | /-
Copyright (c) 2021 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import category_theory.limits.filtered_colimit_commutes_finite_limit
import category_theory.limits.preserves.functor_category
import category_theory.limits.bicones
import category_theory.limits.comma
import category_theory.limits.preserves.finite
import category_theory.limits.shapes.finite_limits
/-!
# Representably flat functors
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
We define representably flat functors as functors such that the category of structured arrows
over `X` is cofiltered for each `X`. This concept is also known as flat functors as in [Elephant]
Remark C2.3.7, and this name is suggested by Mike Shulman in
https://golem.ph.utexas.edu/category/2011/06/flat_functors_and_morphisms_of.html to avoid
confusion with other notions of flatness.
This definition is equivalent to left exact functors (functors that preserves finite limits) when
`C` has all finite limits.
## Main results
* `flat_of_preserves_finite_limits`: If `F : C ⥤ D` preserves finite limits and `C` has all finite
limits, then `F` is flat.
* `preserves_finite_limits_of_flat`: If `F : C ⥤ D` is flat, then it preserves all finite limits.
* `preserves_finite_limits_iff_flat`: If `C` has all finite limits,
then `F` is flat iff `F` is left_exact.
* `Lan_preserves_finite_limits_of_flat`: If `F : C ⥤ D` is a flat functor between small categories,
then the functor `Lan F.op` between presheaves of sets preserves all finite limits.
* `flat_iff_Lan_flat`: If `C`, `D` are small and `C` has all finite limits, then `F` is flat iff
`Lan F.op : (Cᵒᵖ ⥤ Type*) ⥤ (Dᵒᵖ ⥤ Type*)` is flat.
* `preserves_finite_limits_iff_Lan_preserves_finite_limits`: If `C`, `D` are small and `C` has all
finite limits, then `F` preserves finite limits iff `Lan F.op : (Cᵒᵖ ⥤ Type*) ⥤ (Dᵒᵖ ⥤ Type*)`
does.
-/
universes w v₁ v₂ v₃ u₁ u₂ u₃
open category_theory
open category_theory.limits
open opposite
namespace category_theory
namespace structured_arrow_cone
open structured_arrow
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₁} D]
variables {J : Type w} [small_category J]
variables {K : J ⥤ C} (F : C ⥤ D) (c : cone K)
/--
Given a cone `c : cone K` and a map `f : X ⟶ c.X`, we can construct a cone of structured
arrows over `X` with `f` as the cone point. This is the underlying diagram.
-/
@[simps]
def to_diagram : J ⥤ structured_arrow c.X K :=
{ obj := λ j, structured_arrow.mk (c.π.app j),
map := λ j k g, structured_arrow.hom_mk g (by simpa) }
/-- Given a diagram of `structured_arrow X F`s, we may obtain a cone with cone point `X`. -/
@[simps]
def diagram_to_cone {X : D} (G : J ⥤ structured_arrow X F) : cone (G ⋙ proj X F ⋙ F) :=
{ X := X, π := { app := λ j, (G.obj j).hom } }
/--
Given a cone `c : cone K` and a map `f : X ⟶ F.obj c.X`, we can construct a cone of structured
arrows over `X` with `f` as the cone point.
-/
@[simps]
def to_cone {X : D} (f : X ⟶ F.obj c.X) :
cone (to_diagram (F.map_cone c) ⋙ map f ⋙ pre _ K F) :=
{ X := mk f, π := { app := λ j, hom_mk (c.π.app j) rfl,
naturality' := λ j k g, by { ext, dsimp, simp } } }
end structured_arrow_cone
section representably_flat
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D]
variables {E : Type u₃} [category.{v₃} E]
/--
A functor `F : C ⥤ D` is representably-flat functor if the comma category `(X/F)`
is cofiltered for each `X : C`.
-/
class representably_flat (F : C ⥤ D) : Prop :=
(cofiltered : ∀ (X : D), is_cofiltered (structured_arrow X F))
attribute [instance] representably_flat.cofiltered
local attribute [instance] is_cofiltered.nonempty
instance representably_flat.id : representably_flat (𝟭 C) :=
begin
constructor,
intro X,
haveI : nonempty (structured_arrow X (𝟭 C)) := ⟨structured_arrow.mk (𝟙 _)⟩,
rsufficesI : is_cofiltered_or_empty (structured_arrow X (𝟭 C)),
{ constructor },
constructor,
{ intros Y Z,
use structured_arrow.mk (𝟙 _),
use structured_arrow.hom_mk Y.hom (by erw [functor.id_map, category.id_comp]),
use structured_arrow.hom_mk Z.hom (by erw [functor.id_map, category.id_comp]) },
{ intros Y Z f g,
use structured_arrow.mk (𝟙 _),
use structured_arrow.hom_mk Y.hom (by erw [functor.id_map, category.id_comp]),
ext,
transitivity Z.hom; simp }
end
instance representably_flat.comp (F : C ⥤ D) (G : D ⥤ E)
[representably_flat F] [representably_flat G] : representably_flat (F ⋙ G) :=
begin
constructor,
intro X,
haveI : nonempty (structured_arrow X (F ⋙ G)),
{ have f₁ : structured_arrow X G := nonempty.some infer_instance,
have f₂ : structured_arrow f₁.right F := nonempty.some infer_instance,
exact ⟨structured_arrow.mk (f₁.hom ≫ G.map f₂.hom)⟩ },
rsufficesI : is_cofiltered_or_empty (structured_arrow X (F ⋙ G)),
{ constructor },
constructor,
{ intros Y Z,
let W := @is_cofiltered.min (structured_arrow X G) _ _
(structured_arrow.mk Y.hom) (structured_arrow.mk Z.hom),
let Y' : W ⟶ _ := is_cofiltered.min_to_left _ _,
let Z' : W ⟶ _ := is_cofiltered.min_to_right _ _,
let W' := @is_cofiltered.min (structured_arrow W.right F) _ _
(structured_arrow.mk Y'.right) (structured_arrow.mk Z'.right),
let Y'' : W' ⟶ _ := is_cofiltered.min_to_left _ _,
let Z'' : W' ⟶ _ := is_cofiltered.min_to_right _ _,
use structured_arrow.mk (W.hom ≫ G.map W'.hom),
use structured_arrow.hom_mk Y''.right (by simp [← G.map_comp]),
use structured_arrow.hom_mk Z''.right (by simp [← G.map_comp]) },
{ intros Y Z f g,
let W := @is_cofiltered.eq (structured_arrow X G) _ _
(structured_arrow.mk Y.hom) (structured_arrow.mk Z.hom)
(structured_arrow.hom_mk (F.map f.right) (structured_arrow.w f))
(structured_arrow.hom_mk (F.map g.right) (structured_arrow.w g)),
let h : W ⟶ _ := is_cofiltered.eq_hom _ _,
let h_cond : h ≫ _ = h ≫ _ := is_cofiltered.eq_condition _ _,
let W' := @is_cofiltered.eq (structured_arrow W.right F) _ _
(structured_arrow.mk h.right) (structured_arrow.mk (h.right ≫ F.map f.right))
(structured_arrow.hom_mk f.right rfl)
(structured_arrow.hom_mk g.right (congr_arg comma_morphism.right h_cond).symm),
let h' : W' ⟶ _ := is_cofiltered.eq_hom _ _,
let h'_cond : h' ≫ _ = h' ≫ _ := is_cofiltered.eq_condition _ _,
use structured_arrow.mk (W.hom ≫ G.map W'.hom),
use structured_arrow.hom_mk h'.right (by simp [← G.map_comp]),
ext,
exact (congr_arg comma_morphism.right h'_cond : _) }
end
end representably_flat
section has_limit
variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₁} D]
local attribute [instance] has_finite_limits_of_has_finite_limits_of_size
lemma cofiltered_of_has_finite_limits [has_finite_limits C] : is_cofiltered C :=
{ cone_objs := λ A B, ⟨limits.prod A B, limits.prod.fst, limits.prod.snd, trivial⟩,
cone_maps := λ A B f g, ⟨equalizer f g, equalizer.ι f g, equalizer.condition f g⟩,
nonempty := ⟨⊤_ C⟩ }
lemma flat_of_preserves_finite_limits [has_finite_limits C] (F : C ⥤ D)
[preserves_finite_limits F] : representably_flat F := ⟨λ X,
begin
haveI : has_finite_limits (structured_arrow X F) :=
begin
apply has_finite_limits_of_has_finite_limits_of_size.{v₁} (structured_arrow X F),
intros J sJ fJ, resetI, constructor
end,
exact cofiltered_of_has_finite_limits
end⟩
namespace preserves_finite_limits_of_flat
open structured_arrow
open structured_arrow_cone
variables {J : Type v₁} [small_category J] [fin_category J] {K : J ⥤ C}
variables (F : C ⥤ D) [representably_flat F] {c : cone K} (hc : is_limit c) (s : cone (K ⋙ F))
include hc
/--
(Implementation).
Given a limit cone `c : cone K` and a cone `s : cone (K ⋙ F)` with `F` representably flat,
`s` can factor through `F.map_cone c`.
-/
noncomputable def lift : s.X ⟶ F.obj c.X :=
let s' := is_cofiltered.cone (to_diagram s ⋙ structured_arrow.pre _ K F) in
s'.X.hom ≫ (F.map $ hc.lift $
(cones.postcompose ({ app := λ X, 𝟙 _, naturality' := by simp }
: (to_diagram s ⋙ pre s.X K F) ⋙ proj s.X F ⟶ K)).obj $
(structured_arrow.proj s.X F).map_cone s')
lemma fac (x : J) : lift F hc s ≫ (F.map_cone c).π.app x = s.π.app x :=
by simpa [lift, ←functor.map_comp]
local attribute [simp] eq_to_hom_map
lemma uniq {K : J ⥤ C} {c : cone K} (hc : is_limit c)
(s : cone (K ⋙ F)) (f₁ f₂ : s.X ⟶ F.obj c.X)
(h₁ : ∀ (j : J), f₁ ≫ (F.map_cone c).π.app j = s.π.app j)
(h₂ : ∀ (j : J), f₂ ≫ (F.map_cone c).π.app j = s.π.app j) : f₁ = f₂ :=
begin
-- We can make two cones over the diagram of `s` via `f₁` and `f₂`.
let α₁ : to_diagram (F.map_cone c) ⋙ map f₁ ⟶ to_diagram s :=
{ app := λ X, eq_to_hom (by simp [←h₁]), naturality' := λ _ _ _, by { ext, simp } },
let α₂ : to_diagram (F.map_cone c) ⋙ map f₂ ⟶ to_diagram s :=
{ app := λ X, eq_to_hom (by simp [←h₂]), naturality' := λ _ _ _, by { ext, simp } },
let c₁ : cone (to_diagram s ⋙ pre s.X K F) :=
(cones.postcompose (whisker_right α₁ (pre s.X K F) : _)).obj (to_cone F c f₁),
let c₂ : cone (to_diagram s ⋙ pre s.X K F) :=
(cones.postcompose (whisker_right α₂ (pre s.X K F) : _)).obj (to_cone F c f₂),
-- The two cones can then be combined and we may obtain a cone over the two cones since
-- `structured_arrow s.X F` is cofiltered.
let c₀ := is_cofiltered.cone (bicone_mk _ c₁ c₂),
let g₁ : c₀.X ⟶ c₁.X := c₀.π.app (bicone.left),
let g₂ : c₀.X ⟶ c₂.X := c₀.π.app (bicone.right),
-- Then `g₁.right` and `g₂.right` are two maps from the same cone into the `c`.
have : ∀ (j : J), g₁.right ≫ c.π.app j = g₂.right ≫ c.π.app j,
{ intro j,
injection c₀.π.naturality (bicone_hom.left j) with _ e₁,
injection c₀.π.naturality (bicone_hom.right j) with _ e₂,
simpa using e₁.symm.trans e₂ },
have : c.extend g₁.right = c.extend g₂.right,
{ unfold cone.extend, congr' 1, ext x, apply this },
-- And thus they are equal as `c` is the limit.
have : g₁.right = g₂.right,
calc g₁.right = hc.lift (c.extend g₁.right) : by { apply hc.uniq (c.extend _), tidy }
... = hc.lift (c.extend g₂.right) : by { congr, exact this }
... = g₂.right : by { symmetry, apply hc.uniq (c.extend _), tidy },
-- Finally, since `fᵢ` factors through `F(gᵢ)`, the result follows.
calc f₁ = 𝟙 _ ≫ f₁ : by simp
... = c₀.X.hom ≫ F.map g₁.right : g₁.w
... = c₀.X.hom ≫ F.map g₂.right : by rw this
... = 𝟙 _ ≫ f₂ : g₂.w.symm
... = f₂ : by simp
end
end preserves_finite_limits_of_flat
/-- Representably flat functors preserve finite limits. -/
noncomputable
def preserves_finite_limits_of_flat (F : C ⥤ D) [representably_flat F] :
preserves_finite_limits F :=
begin
apply preserves_finite_limits_of_preserves_finite_limits_of_size,
intros J _ _, constructor,
intros K, constructor,
intros c hc,
exactI { lift := preserves_finite_limits_of_flat.lift F hc,
fac' := preserves_finite_limits_of_flat.fac F hc,
uniq' := λ s m h, by
{ apply preserves_finite_limits_of_flat.uniq F hc,
exact h,
exact preserves_finite_limits_of_flat.fac F hc s } }
end
/--
If `C` is finitely cocomplete, then `F : C ⥤ D` is representably flat iff it preserves
finite limits.
-/
noncomputable
def preserves_finite_limits_iff_flat [has_finite_limits C] (F : C ⥤ D) :
representably_flat F ≃ preserves_finite_limits F :=
{ to_fun := λ _, by exactI preserves_finite_limits_of_flat F,
inv_fun := λ _, by exactI flat_of_preserves_finite_limits F,
left_inv := λ _, proof_irrel _ _,
right_inv := λ x, by { cases x, unfold preserves_finite_limits_of_flat,
dunfold preserves_finite_limits_of_preserves_finite_limits_of_size, congr } }
end has_limit
section small_category
variables {C D : Type u₁} [small_category C] [small_category D] (E : Type u₂) [category.{u₁} E]
/--
(Implementation)
The evaluation of `Lan F` at `X` is the colimit over the costructured arrows over `X`.
-/
noncomputable
def Lan_evaluation_iso_colim (F : C ⥤ D) (X : D)
[∀ (X : D), has_colimits_of_shape (costructured_arrow F X) E] :
Lan F ⋙ (evaluation D E).obj X ≅
((whiskering_left _ _ E).obj (costructured_arrow.proj F X)) ⋙ colim :=
nat_iso.of_components (λ G, colim.map_iso (iso.refl _))
begin
intros G H i,
ext,
simp only [functor.comp_map, colimit.ι_desc_assoc, functor.map_iso_refl, evaluation_obj_map,
whiskering_left_obj_map, category.comp_id, Lan_map_app, category.assoc],
erw [colimit.ι_pre_assoc (Lan.diagram F H X) (costructured_arrow.map j.hom),
category.id_comp, category.comp_id, colimit.ι_map],
rcases j with ⟨j_left, ⟨⟨⟩⟩, j_hom⟩,
congr,
rw [costructured_arrow.map_mk, category.id_comp, costructured_arrow.mk]
end
variables [concrete_category.{u₁} E] [has_limits E] [has_colimits E]
variables [reflects_limits (forget E)] [preserves_filtered_colimits (forget E)]
variables [preserves_limits (forget E)]
/--
If `F : C ⥤ D` is a representably flat functor between small categories, then the functor
`Lan F.op` that takes presheaves over `C` to presheaves over `D` preserves finite limits.
-/
noncomputable
instance Lan_preserves_finite_limits_of_flat (F : C ⥤ D) [representably_flat F] :
preserves_finite_limits (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ E)) :=
begin
apply preserves_finite_limits_of_preserves_finite_limits_of_size.{u₁},
intros J _ _, resetI,
apply preserves_limits_of_shape_of_evaluation (Lan F.op : (Cᵒᵖ ⥤ E) ⥤ (Dᵒᵖ ⥤ E)) J,
intro K,
haveI : is_filtered (costructured_arrow F.op K) :=
is_filtered.of_equivalence (structured_arrow_op_equivalence F (unop K)),
exact preserves_limits_of_shape_of_nat_iso (Lan_evaluation_iso_colim _ _ _).symm,
end
instance Lan_flat_of_flat (F : C ⥤ D) [representably_flat F] :
representably_flat (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ E)) := flat_of_preserves_finite_limits _
variable [has_finite_limits C]
noncomputable
instance Lan_preserves_finite_limits_of_preserves_finite_limits (F : C ⥤ D)
[preserves_finite_limits F] : preserves_finite_limits (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ E)) :=
begin
haveI := flat_of_preserves_finite_limits F,
apply_instance
end
lemma flat_iff_Lan_flat (F : C ⥤ D) :
representably_flat F ↔ representably_flat (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ Type u₁)) :=
⟨λ H, by exactI infer_instance, λ H,
begin
resetI,
haveI := preserves_finite_limits_of_flat (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ Type u₁)),
haveI : preserves_finite_limits F :=
begin
apply preserves_finite_limits_of_preserves_finite_limits_of_size.{u₁},
intros, resetI, apply preserves_limit_of_Lan_preserves_limit
end,
apply flat_of_preserves_finite_limits
end⟩
/--
If `C` is finitely complete, then `F : C ⥤ D` preserves finite limits iff
`Lan F.op : (Cᵒᵖ ⥤ Type*) ⥤ (Dᵒᵖ ⥤ Type*)` preserves finite limits.
-/
noncomputable
def preserves_finite_limits_iff_Lan_preserves_finite_limits (F : C ⥤ D) :
preserves_finite_limits F ≃ preserves_finite_limits (Lan F.op : _ ⥤ (Dᵒᵖ ⥤ Type u₁)) :=
{ to_fun := λ _, by exactI infer_instance,
inv_fun := λ _,
begin
apply preserves_finite_limits_of_preserves_finite_limits_of_size.{u₁},
intros, resetI, apply preserves_limit_of_Lan_preserves_limit
end,
left_inv := λ x,
begin
cases x, unfold preserves_finite_limits_of_flat,
dunfold preserves_finite_limits_of_preserves_finite_limits_of_size, congr
end,
right_inv := λ x,
begin
cases x,
unfold preserves_finite_limits_of_flat,
congr,
unfold category_theory.Lan_preserves_finite_limits_of_preserves_finite_limits
category_theory.Lan_preserves_finite_limits_of_flat,
dunfold preserves_finite_limits_of_preserves_finite_limits_of_size, congr
end }
end small_category
end category_theory
|
216beb958ffc73fac2a1230c76be30cc4d89221b | 4727251e0cd73359b15b664c3170e5d754078599 | /src/analysis/convex/complex.lean | d22d1c1b96700e4da5f44bb729ff63213fe21f79 | [
"Apache-2.0"
] | permissive | Vierkantor/mathlib | 0ea59ac32a3a43c93c44d70f441c4ee810ccceca | 83bc3b9ce9b13910b57bda6b56222495ebd31c2f | refs/heads/master | 1,658,323,012,449 | 1,652,256,003,000 | 1,652,256,003,000 | 209,296,341 | 0 | 1 | Apache-2.0 | 1,568,807,655,000 | 1,568,807,655,000 | null | UTF-8 | Lean | false | false | 1,595 | lean | /-
Copyright (c) 2019 Yury Kudriashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudriashov, Yaël Dillies
-/
import analysis.convex.basic
import data.complex.module
/-!
# Convexity of half spaces in ℂ
The open and closed half-spaces in ℂ given by an inequality on either the real or imaginary part
are all convex over ℝ.
-/
lemma convex_halfspace_re_lt (r : ℝ) : convex ℝ {c : ℂ | c.re < r} :=
convex_halfspace_lt (is_linear_map.mk complex.add_re complex.smul_re) _
lemma convex_halfspace_re_le (r : ℝ) : convex ℝ {c : ℂ | c.re ≤ r} :=
convex_halfspace_le (is_linear_map.mk complex.add_re complex.smul_re) _
lemma convex_halfspace_re_gt (r : ℝ) : convex ℝ {c : ℂ | r < c.re } :=
convex_halfspace_gt (is_linear_map.mk complex.add_re complex.smul_re) _
lemma convex_halfspace_re_ge (r : ℝ) : convex ℝ {c : ℂ | r ≤ c.re} :=
convex_halfspace_ge (is_linear_map.mk complex.add_re complex.smul_re) _
lemma convex_halfspace_im_lt (r : ℝ) : convex ℝ {c : ℂ | c.im < r} :=
convex_halfspace_lt (is_linear_map.mk complex.add_im complex.smul_im) _
lemma convex_halfspace_im_le (r : ℝ) : convex ℝ {c : ℂ | c.im ≤ r} :=
convex_halfspace_le (is_linear_map.mk complex.add_im complex.smul_im) _
lemma convex_halfspace_im_gt (r : ℝ) : convex ℝ {c : ℂ | r < c.im} :=
convex_halfspace_gt (is_linear_map.mk complex.add_im complex.smul_im) _
lemma convex_halfspace_im_ge (r : ℝ) : convex ℝ {c : ℂ | r ≤ c.im} :=
convex_halfspace_ge (is_linear_map.mk complex.add_im complex.smul_im) _
|
f9f6f75ff82f36aeab56d90a79293d56424661e0 | 5b273b8c05e2f73fb74340ce100ce261900a98cd | /series.lean | d76e54315b56ec755ce851c809f23c1b692cda96 | [] | no_license | ChrisHughes24/leanstuff1 | 2eba44bc48da6e544e07495b41e1703f81dc1c24 | cbcd788b8b1d07b20b2fff4482c870077a13d1c0 | refs/heads/master | 1,631,670,333,297 | 1,527,093,981,000 | 1,527,093,981,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 7,496 | lean | import data.nat.basic algebra.group data.real.cau_seq
open nat is_absolute_value
variables {α : Type*} {β : Type*}
def series [has_add α] (f : ℕ → α) : ℕ → α
| 0 := f 0
| (succ i) := series i + f (succ i)
def nat.sum [has_add α] (f : ℕ → α) (i j : ℕ) := series (λ k, f (k + i)) (j - i)
@[simp]
lemma series_zero [has_add α] (f : ℕ → α) : series f 0 = f 0 := by unfold series
lemma series_succ [has_add α] (f : ℕ → α) (i : ℕ) : series f (succ i) = series f i + f (succ i):= by unfold series
lemma series_eq_sum_zero [has_add α] (f : ℕ → α) (i : ℕ) : series f i = nat.sum f 0 i := by unfold nat.sum;simp
lemma series_succ₁ [add_comm_monoid α] (f : ℕ → α) (i : ℕ) : series f (succ i) = f 0 + series (λ i, f (succ i)) i := begin
induction i with i' hi,
simp!,simp!,rw ←hi,simp!,
end
lemma series_comm {α : Type*} [add_comm_monoid α] (f : ℕ → α) (n : ℕ) : series f n = series (λ i, f (n - i)) n := begin
induction n with n' hi,
simp!,simp!,rw hi,
have : (λ (i : ℕ), f (succ n' - i)) (succ n') = f (n' - n'),simp,
rw ←this,have : (λ (i : ℕ), f (succ n' - i)) (succ n') + series (λ (i : ℕ), f (succ n' - i)) n' = series (λ (i : ℕ), f (succ n' - i)) (succ n'),simp!,
rw this,
have : (λ i, f (n' - i)) = (λ i, f (succ n' - succ i)),
apply funext,assume i,rw succ_sub_succ,
rw this,clear this,
have : f (succ n') = (λ (i : ℕ), f (succ n' - i)) 0,simp,rw this,rw ←series_succ₁,
end
lemma series_neg [ring α] (f : ℕ → α) (n : ℕ) : -series f n = series (λ m, -f m) n := begin
induction n with n' hi, simp!,simp![hi],
end
lemma series_sub_series [ring α] (f : ℕ → α) {i j : ℕ} : i < j → series f j - series f i = nat.sum f (i + 1) j := begin
unfold nat.sum,assume ij,
induction i with i' hi,
cases j with j',exact absurd ij dec_trivial,
rw sub_eq_iff_eq_add',
exact series_succ₁ _ _,
rw [series_succ,sub_add_eq_sub_sub,hi (lt_of_succ_lt ij),sub_eq_iff_eq_add'],
have : (j - (i' + 1)) = succ (j - (succ i' + 1)),
rw [←nat.succ_sub ij,succ_sub_succ],
rw this,
have : f (succ i') = (λ (k : ℕ), f (k + (i' + 1))) 0,
simp,
rw this,simp[succ_add,add_succ],
rw series_succ₁,simp,
end
lemma series_const_zero [has_zero α] (i : ℕ): series (λ j, 0) i = 0 := begin
induction i with i' hi,simp,simpa [series_succ],
end
lemma series_add [add_comm_monoid α] (f g : ℕ → α) (n : ℕ) : series (λ i, f i + g i) n = series f n + series g n := begin
induction n with n' hi,simp[series_zero],simp[series_succ,hi],
end
lemma series_mul_left [semiring α] (f : ℕ → α) (a : α) (n : ℕ) : series (λ i, a * f i) n = a * series f n := begin
induction n with n' hi,simp[series_zero],simp[series_succ,hi,mul_add],
end
lemma series_mul_right [semiring α] (f : ℕ → α) (a : α) (n : ℕ) : series (λ i, f i * a) n = series f n * a:= begin
induction n with n' hi,simp[series_zero],simp[series_succ,hi,add_mul],
end
lemma series_le [add_comm_monoid α] {f g : ℕ → α} {n : ℕ} : (∀ i : ℕ, i ≤ n → f i = g i) → series f n = series g n := begin
assume h, induction n with n' hi,simp,exact h 0 (le_refl _),
simp[series_succ],rw [h (succ n') (le_refl _),hi (λ i h₁,h i (le_succ_of_le h₁))],
end
lemma abv_series_le_series_abv [discrete_linear_ordered_field α] [ring β] {f : ℕ → β}
{abv : β → α} [is_absolute_value abv] (n : ℕ) : abv (series f n) ≤ series (λ i, abv (f i)) n := begin
induction n with n' hi,
simp,simp[series_succ],
exact le_trans (abv_add _ _ _) (add_le_add_left hi _),
end
lemma series_mul_series [semiring α] (f g : ℕ → α) (n m : ℕ) : series f n * series g m = series (λ i, f i * series g m) n := begin
induction n with n' hi,
simp,simp[series_succ,mul_add,add_mul,hi],
end
lemma series_le_series [ordered_cancel_comm_monoid α] {f g : ℕ → α} {n : ℕ} : (∀ m ≤ n, f m ≤ g m) → series f n ≤ series g n := begin
assume h,induction n with n' hi,exact h 0 (le_refl _),
unfold series,exact add_le_add (hi (λ m hm, h m (le_succ_of_le hm))) (h _ (le_refl _)),
end
lemma series_congr [has_add α] {f g : ℕ → α} {i : ℕ} : (∀ j ≤ i, f j = g j) → series f i = series g i := begin
assume h,induction i with i' hi,exact h 0 (zero_le _),
unfold series,rw h _ (le_refl (succ i')),
rw hi (λ j ji, h j (le_succ_of_le ji)),
end
lemma series_nonneg [ordered_cancel_comm_monoid α] {f : ℕ → α} {n : ℕ} : (∀ m ≤ n, 0 ≤ f m) → 0 ≤ series f n := begin
induction n with n' hi,simp,assume h,exact h 0 (le_refl _),
assume h,unfold series,refine add_nonneg (hi (λ m hm, h m (le_succ_of_le hm))) (h _ (le_refl _)),
end
lemma series_series_diag_flip [add_comm_monoid α] (f : ℕ → ℕ → α) (n : ℕ) : series (λ i,
series (λ k, f k (i - k)) i) n = series (λ i, series (λ k, f i k) (n - i)) n := begin
have : ∀ m : ℕ, m ≤ n → series (λ (i : ℕ), series (λ k, f k (i - k)) (min m i)) n =
series (λ i, series (λ k, f i k) (n - i)) m,
assume m mn, induction m with m' hi,
simp[series_succ,series_zero,mul_add,max_eq_left (zero_le n)],
simp only [series_succ _ m'],rw ←hi (le_of_succ_le mn),clear hi,
induction n with n' hi,
simp[series_succ],exact absurd mn dec_trivial,cases n' with n₂,
simp [series_succ],rw [min_eq_left mn,series_succ,min_eq_left (le_of_succ_le mn)],
rw eq_zero_of_le_zero (le_of_succ_le_succ mn),simp,
cases lt_or_eq_of_le mn,
simp [series_succ _ (succ n₂),min_eq_left mn,hi (le_of_lt_succ h)],rw [←add_assoc,←add_assoc],
suffices : series (f (succ m')) (n₂ - m') + series (λ (k : ℕ), f k (succ (succ n₂) - k)) (succ m')
= series (f (succ m')) (succ n₂ - m') +
series (λ (k : ℕ), f k (succ (succ n₂) - k)) (min m' (succ (succ n₂))),
rw this,rw[min_eq_left (le_of_succ_le mn),series_succ,succ_sub_succ,succ_sub (le_of_succ_le_succ (le_of_lt_succ h)),series_succ],
rw [add_comm (series (λ (k : ℕ), f k (succ (succ n₂) - k)) m'),add_assoc],
rw ←h,simp[nat.sub_self],clear hi mn h,simp[series_succ,nat.sub_self],
suffices : series (λ (i : ℕ), series (λ (k : ℕ), f k (i - k)) (min (succ m') i)) m' = series (λ (i : ℕ), series (λ (k : ℕ), f k (i - k)) (min m' i)) m',
rw [this,min_eq_left (le_succ _)],clear n₂,
have h₁ : ∀ i ≤ m', (λ (i : ℕ), series (λ (k : ℕ), f k (i - k)) (min (succ m') i)) i = (λ (i : ℕ), series (λ (k : ℕ), f k (i - k)) (min m' i)) i,
assume i im,simp, rw [min_eq_right im,min_eq_right (le_succ_of_le im)],
rw series_congr h₁,
specialize this n (le_refl _),
rw ←this,refine series_congr _,assume i ni,rw min_eq_right ni,
end
lemma nat.sum_succ [has_add α] (f : ℕ → α) (i j : ℕ) : i ≤ j → nat.sum f i (succ j) = nat.sum f i j + f (succ j) := begin
assume ij,unfold nat.sum,rw [succ_sub ij,series_succ,←succ_sub ij,nat.sub_add_cancel (le_succ_of_le ij)],
end
lemma nat.sum_le_sum [ordered_cancel_comm_monoid α] {f g : ℕ → α} {i j : ℕ} : i ≤ j → (∀ k ≤ j, i ≤ k → f k ≤ g k) → nat.sum f i j ≤ nat.sum g i j := begin
assume ij h ,unfold nat.sum,
refine series_le_series _,
assume m hm,rw nat.le_sub_right_iff_add_le ij at hm,
exact h (m + i) hm (le_add_left _ _),
end
|
25c776347522add89fd76e704eb2d39ff000d596 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /tests/lean/343.lean | e3cfa3c8c391646e0df6b9d684c25f94ca183750 | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 617 | lean | structure CatIsh where
Obj : Type o
Hom : Obj → Obj → Type m
infixr:75 " ~> " => (CatIsh.Hom _)
structure FunctorIsh (C D : CatIsh) where
onObj : C.Obj → D.Obj
onHom : ∀ {s d : C.Obj}, (s ~> d) → (onObj s ~> onObj d)
def Catish : CatIsh :=
{
Obj := CatIsh
Hom := FunctorIsh
}
universe m o
unif_hint (mvar : CatIsh) where
Catish.{m,o} =?= mvar |- mvar.Obj =?= CatIsh.{m,o}
structure CtxSyntaxLayerParamsObj where
Ct : CatIsh
def CtxSyntaxLayerParams : CatIsh :=
{
Obj := CtxSyntaxLayerParamsObj
Hom := sorry
}
def CtxSyntaxLayerTy := CtxSyntaxLayerParams ~> Catish
|
a1636e0615b92c9bd2cbdd061490b695ebf43255 | d6124c8dbe5661dcc5b8c9da0a56fbf1f0480ad6 | /Papyrus/IR/InstructionKind.lean | fc3122e6a9e162ff5949d415174bfe24ef8db0db | [
"Apache-2.0"
] | permissive | xubaiw/lean4-papyrus | c3fbbf8ba162eb5f210155ae4e20feb2d32c8182 | 02e82973a5badda26fc0f9fd15b3d37e2eb309e0 | refs/heads/master | 1,691,425,756,824 | 1,632,122,825,000 | 1,632,123,075,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,196 | lean | import Papyrus.Internal.Enum
namespace Papyrus
open Internal
/-- Tags for all of the instruction types of LLVM (v12) IR. -/
sealed-enum InstructionKind : UInt8
-- terminator
| ret
| branch
| switch
| indirectBr
| invoke
| resume
| unreachable
| cleanupRet
| catchRet
| catchSwitch
| callBr
-- unary
| fneg
-- binary
| add
| fadd
| sub
| fsub
| mul
| fmul
| udiv
| sdiv
| fdiv
| urem
| srem
| frem
-- bitwise
| shl
| lshr
| ashr
| and
| or
| xor
-- memory
| alloca
| load
| store
| getElementPtr
| fence
| atomicCmpXchg
| atomicRMW
-- casts
| trunc
| zext
| sext
| fpToUI
| fpToSI
| uiToFP
| siToFP
| fpTrunc
| fpExt
| ptrToInt
| intToPtr
| bitcast
| addrSpaceCast
-- pad
| cleanupPad
| catchPad
-- other
| icmp
| fcmp
| phi
| call
| select
| userOp1
| userOp2
| vaarg
| extractElement
| insertElement
| shuffleVector
| extractValue
| insertValue
| landingPad
| freeze
deriving Inhabited, BEq, DecidableEq, Repr
namespace InstructionKind
def ofOpcode! (opcode : UInt32) : InstructionKind :=
let id := opcode - 1 |>.toUInt8
if h : id ≤ maxVal then
mk id h
else
panic! s!"unknown LLVM opcode {opcode}"
def toOpcode (self : InstructionKind) : UInt32 :=
self.val.toUInt32 + 1
|
56b7abf6cbe8efc5c851bce1aa91ec74dba2567f | a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91 | /tests/lean/run/meta_expr1.lean | 54985bd5f2c928fa47560652a7375306c6dc06c1 | [
"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,814 | lean | open unsigned list
meta_definition e1 := expr.app (expr.app (expr.const `f []) (expr.mk_var 1)) (expr.const `a [])
meta_definition e1' := expr.app (expr.app (expr.const `f []) (expr.mk_var 1)) (expr.const `a [])
meta_definition tst : e1 = e1' :=
rfl
vm_eval e1
vm_eval expr.fold e1 (0:nat) (λ e d n, n+1)
meta_definition l1 := expr.lam `a binder_info.default (expr.sort level.zero) (expr.mk_var 0)
meta_definition l2 := expr.lam `b binder_info.default (expr.sort level.zero) (expr.mk_var 0)
meta_definition l3 := expr.lam `a binder_info.default (expr.const `nat []) (expr.mk_var 0)
vm_eval l1
vm_eval l2
vm_eval l3
vm_eval decidable.to_bool (l1 = l2)
vm_eval decidable.to_bool (l1 =ₐ l2)
vm_eval expr.lex_lt (expr.const `a []) (expr.const `b [])
vm_eval expr.lt (expr.const `a []) (expr.const `b [])
meta_definition v1 := expr.app (expr.app (expr.const `f []) (expr.mk_var 0)) (expr.mk_var 1)
vm_eval v1
vm_eval expr.instantiate_var v1 (expr.const `a [])
vm_eval expr.instantiate_vars v1 [expr.const `a [], expr.const `b []]
meta_definition fv1 :=
expr.app
(expr.app (expr.const `f [])
(expr.local_const `a `a binder_info.default (expr.sort level.zero)))
(expr.local_const `b `b binder_info.default (expr.sort level.zero))
vm_eval fv1
vm_eval expr.abstract_local (expr.abstract_local fv1 `a) `b
vm_eval expr.abstract_locals fv1 [`a, `b]
vm_eval expr.abstract_locals fv1 [`b, `a]
vm_eval expr.lift_vars (expr.abstract_locals fv1 [`b, `a]) 1 1
vm_eval expr.has_local fv1
vm_eval expr.has_var fv1
vm_eval expr.has_var (expr.abstract_locals fv1 [`b, `a])
meta_definition foo : nat → expr
| 0 := expr.const `aa [level.zero, level.succ level.zero]
| (n+1) := foo n
/-
vm_eval match foo 10 with
| expr.const n ls := list.head (list.tail ls)
| _ := level.zero
end
-/
|
5da645ee6df1a98ac695764ab581db1a7b375faf | 57c233acf9386e610d99ed20ef139c5f97504ba3 | /src/category_theory/quotient.lean | 4bea125ea7b4d3698805c0f93cc9111f48aee2ad | [
"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 | 5,503 | lean | /-
Copyright (c) 2020 David Wärn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: David Wärn
-/
import category_theory.natural_isomorphism
import category_theory.equivalence
import category_theory.eq_to_hom
/-!
# Quotient category
Constructs the quotient of a category by an arbitrary family of relations on its hom-sets,
by introducing a type synonym for the objects, and identifying homs as necessary.
This is analogous to 'the quotient of a group by the normal closure of a subset', rather
than 'the quotient of a group by a normal subgroup'. When taking the quotient by a congruence
relation, `functor_map_eq_iff` says that no unnecessary identifications have been made.
-/
/-- A `hom_rel` on `C` consists of a relation on every hom-set. -/
@[derive inhabited]
def hom_rel (C) [quiver C] := Π ⦃X Y : C⦄, (X ⟶ Y) → (X ⟶ Y) → Prop
namespace category_theory
variables {C : Type*} [category C] (r : hom_rel C)
include r
/-- A `hom_rel` is a congruence when it's an equivalence on every hom-set, and it can be composed
from left and right. -/
class congruence : Prop :=
(is_equiv : ∀ {X Y}, is_equiv _ (@r X Y))
(comp_left : ∀ {X Y Z} (f : X ⟶ Y) {g g' : Y ⟶ Z}, r g g' → r (f ≫ g) (f ≫ g'))
(comp_right : ∀ {X Y Z} {f f' : X ⟶ Y} (g : Y ⟶ Z), r f f' → r (f ≫ g) (f' ≫ g))
attribute [instance] congruence.is_equiv
/-- A type synonym for `C`, thought of as the objects of the quotient category. -/
@[ext]
structure quotient := (as : C)
instance [inhabited C] : inhabited (quotient r) := ⟨ { as := default } ⟩
namespace quotient
/-- Generates the closure of a family of relations w.r.t. composition from left and right. -/
inductive comp_closure ⦃s t : C⦄ : (s ⟶ t) → (s ⟶ t) → Prop
| intro {a b} (f : s ⟶ a) (m₁ m₂ : a ⟶ b) (g : b ⟶ t) (h : r m₁ m₂) :
comp_closure (f ≫ m₁ ≫ g) (f ≫ m₂ ≫ g)
lemma comp_left {a b c : C} (f : a ⟶ b) : Π (g₁ g₂ : b ⟶ c) (h : comp_closure r g₁ g₂),
comp_closure r (f ≫ g₁) (f ≫ g₂)
| _ _ ⟨x, m₁, m₂, y, h⟩ := by simpa using comp_closure.intro (f ≫ x) m₁ m₂ y h
lemma comp_right {a b c : C} (g : b ⟶ c) : Π (f₁ f₂ : a ⟶ b) (h : comp_closure r f₁ f₂),
comp_closure r (f₁ ≫ g) (f₂ ≫ g)
| _ _ ⟨x, m₁, m₂, y, h⟩ := by simpa using comp_closure.intro x m₁ m₂ (y ≫ g) h
/-- Hom-sets of the quotient category. -/
def hom (s t : quotient r) := quot $ @comp_closure C _ r s.as t.as
instance (a : quotient r) : inhabited (hom r a a) := ⟨quot.mk _ (𝟙 a.as)⟩
/-- Composition in the quotient category. -/
def comp ⦃a b c : quotient r⦄ : hom r a b → hom r b c → hom r a c :=
λ hf hg, quot.lift_on hf ( λ f, quot.lift_on hg (λ g, quot.mk _ (f ≫ g))
(λ g₁ g₂ h, quot.sound $ comp_left r f g₁ g₂ h) )
(λ f₁ f₂ h, quot.induction_on hg $ λ g, quot.sound $ comp_right r g f₁ f₂ h)
@[simp]
lemma comp_mk {a b c : quotient r} (f : a.as ⟶ b.as) (g : b.as ⟶ c.as) :
comp r (quot.mk _ f) (quot.mk _ g) = quot.mk _ (f ≫ g) := rfl
instance category : category (quotient r) :=
{ hom := hom r,
id := λ a, quot.mk _ (𝟙 a.as),
comp := comp r }
/-- The functor from a category to its quotient. -/
@[simps]
def functor : C ⥤ quotient r :=
{ obj := λ a, { as := a },
map := λ _ _ f, quot.mk _ f }
noncomputable instance : full (functor r) :=
{ preimage := λ X Y f, quot.out f, }
instance : ess_surj (functor r) :=
{ mem_ess_image := λ Y, ⟨Y.as, ⟨eq_to_iso (by { ext, refl, })⟩⟩ }
protected lemma induction {P : Π {a b : quotient r}, (a ⟶ b) → Prop}
(h : ∀ {x y : C} (f : x ⟶ y), P ((functor r).map f)) :
∀ {a b : quotient r} (f : a ⟶ b), P f :=
by { rintros ⟨x⟩ ⟨y⟩ ⟨f⟩, exact h f, }
protected lemma sound {a b : C} {f₁ f₂ : a ⟶ b} (h : r f₁ f₂) :
(functor r).map f₁ = (functor r).map f₂ :=
by simpa using quot.sound (comp_closure.intro (𝟙 a) f₁ f₂ (𝟙 b) h)
lemma functor_map_eq_iff [congruence r] {X Y : C} (f f' : X ⟶ Y) :
(functor r).map f = (functor r).map f' ↔ r f f' :=
begin
split,
{ erw quot.eq,
intro h,
induction h with m m' hm,
{ cases hm, apply congruence.comp_left, apply congruence.comp_right, assumption, },
{ apply refl },
{ apply symm, assumption },
{ apply trans; assumption }, },
{ apply quotient.sound },
end
variables {D : Type*} [category D]
(F : C ⥤ D)
(H : ∀ (x y : C) (f₁ f₂ : x ⟶ y), r f₁ f₂ → F.map f₁ = F.map f₂)
include H
/-- The induced functor on the quotient category. -/
@[simps]
def lift : quotient r ⥤ D :=
{ obj := λ a, F.obj a.as,
map := λ a b hf, quot.lift_on hf (λ f, F.map f)
(by { rintros _ _ ⟨_, _, _, _, _, _, h⟩, simp [H _ _ _ _ h], }),
map_id' := λ a, F.map_id a.as,
map_comp' := by { rintros a b c ⟨f⟩ ⟨g⟩, exact F.map_comp f g, } }
/-- The original functor factors through the induced functor. -/
def lift.is_lift : (functor r) ⋙ lift r F H ≅ F :=
nat_iso.of_components (λ X, iso.refl _) (by tidy)
@[simp]
lemma lift.is_lift_hom (X : C) : (lift.is_lift r F H).hom.app X = 𝟙 (F.obj X) :=
rfl
@[simp]
lemma lift.is_lift_inv (X : C) : (lift.is_lift r F H).inv.app X = 𝟙 (F.obj X) :=
rfl
lemma lift_map_functor_map {X Y : C} (f : X ⟶ Y) :
(lift r F H).map ((functor r).map f) = F.map f :=
by { rw ←(nat_iso.naturality_1 (lift.is_lift r F H)), dsimp, simp, }
end quotient
end category_theory
|
eb5696c9ea9f2f8fa58971ca5c189704d0b50a4c | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/data/list/rotate.lean | 451bd4a8fef11efafa14964690d3d81e225ba0a2 | [
"Apache-2.0"
] | permissive | leanprover-community/mathlib | 56a2cadd17ac88caf4ece0a775932fa26327ba0e | 442a83d738cb208d3600056c489be16900ba701d | refs/heads/master | 1,693,584,102,358 | 1,693,471,902,000 | 1,693,471,902,000 | 97,922,418 | 1,595 | 352 | Apache-2.0 | 1,694,693,445,000 | 1,500,624,130,000 | Lean | UTF-8 | Lean | false | false | 22,798 | lean | /-
Copyright (c) 2019 Chris Hughes. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Hughes, Yakov Pechersky
-/
import data.list.perm
import data.list.range
/-!
# List rotation
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
This file proves basic results about `list.rotate`, the list rotation.
## Main declarations
* `is_rotated l₁ l₂`: States that `l₁` is a rotated version of `l₂`.
* `cyclic_permutations l`: The list of all cyclic permutants of `l`, up to the length of `l`.
## Tags
rotated, rotation, permutation, cycle
-/
universe u
variables {α : Type u}
open nat function
namespace list
lemma rotate_mod (l : list α) (n : ℕ) : l.rotate (n % l.length) = l.rotate n :=
by simp [rotate]
@[simp] lemma rotate_nil (n : ℕ) : ([] : list α).rotate n = [] := by simp [rotate]
@[simp] lemma rotate_zero (l : list α) : l.rotate 0 = l := by simp [rotate]
@[simp] lemma rotate'_nil (n : ℕ) : ([] : list α).rotate' n = [] := by cases n; refl
@[simp] lemma rotate'_zero (l : list α) : l.rotate' 0 = l := by cases l; refl
lemma rotate'_cons_succ (l : list α) (a : α) (n : ℕ) :
(a :: l : list α).rotate' n.succ = (l ++ [a]).rotate' n := by simp [rotate']
@[simp] lemma length_rotate' : ∀ (l : list α) (n : ℕ), (l.rotate' n).length = l.length
| [] n := rfl
| (a::l) 0 := rfl
| (a::l) (n+1) := by rw [list.rotate', length_rotate' (l ++ [a]) n]; simp
lemma rotate'_eq_drop_append_take : ∀ {l : list α} {n : ℕ}, n ≤ l.length →
l.rotate' n = l.drop n ++ l.take n
| [] n h := by simp [drop_append_of_le_length h]
| l 0 h := by simp [take_append_of_le_length h]
| (a::l) (n+1) h :=
have hnl : n ≤ l.length, from le_of_succ_le_succ h,
have hnl' : n ≤ (l ++ [a]).length,
by rw [length_append, length_cons, list.length, zero_add];
exact (le_of_succ_le h),
by rw [rotate'_cons_succ, rotate'_eq_drop_append_take hnl', drop, take,
drop_append_of_le_length hnl, take_append_of_le_length hnl];
simp
lemma rotate'_rotate' : ∀ (l : list α) (n m : ℕ), (l.rotate' n).rotate' m = l.rotate' (n + m)
| (a::l) 0 m := by simp
| [] n m := by simp
| (a::l) (n+1) m := by rw [rotate'_cons_succ, rotate'_rotate', add_right_comm, rotate'_cons_succ]
@[simp] lemma rotate'_length (l : list α) : rotate' l l.length = l :=
by rw rotate'_eq_drop_append_take le_rfl; simp
@[simp] lemma rotate'_length_mul (l : list α) : ∀ n : ℕ, l.rotate' (l.length * n) = l
| 0 := by simp
| (n+1) :=
calc l.rotate' (l.length * (n + 1)) =
(l.rotate' (l.length * n)).rotate' (l.rotate' (l.length * n)).length :
by simp [-rotate'_length, nat.mul_succ, rotate'_rotate']
... = l : by rw [rotate'_length, rotate'_length_mul]
lemma rotate'_mod (l : list α) (n : ℕ) : l.rotate' (n % l.length) = l.rotate' n :=
calc l.rotate' (n % l.length) = (l.rotate' (n % l.length)).rotate'
((l.rotate' (n % l.length)).length * (n / l.length)) : by rw rotate'_length_mul
... = l.rotate' n : by rw [rotate'_rotate', length_rotate', nat.mod_add_div]
lemma rotate_eq_rotate' (l : list α) (n : ℕ) : l.rotate n = l.rotate' n :=
if h : l.length = 0 then by simp [length_eq_zero, *] at *
else by
rw [← rotate'_mod, rotate'_eq_drop_append_take (le_of_lt (nat.mod_lt _ (nat.pos_of_ne_zero h)))];
simp [rotate]
lemma rotate_cons_succ (l : list α) (a : α) (n : ℕ) :
(a :: l : list α).rotate n.succ = (l ++ [a]).rotate n :=
by rw [rotate_eq_rotate', rotate_eq_rotate', rotate'_cons_succ]
@[simp] lemma mem_rotate : ∀ {l : list α} {a : α} {n : ℕ}, a ∈ l.rotate n ↔ a ∈ l
| [] _ n := by simp
| (a::l) _ 0 := by simp
| (a::l) _ (n+1) := by simp [rotate_cons_succ, mem_rotate, or.comm]
@[simp] lemma length_rotate (l : list α) (n : ℕ) : (l.rotate n).length = l.length :=
by rw [rotate_eq_rotate', length_rotate']
lemma rotate_replicate (a : α) (n : ℕ) (k : ℕ) : (replicate n a).rotate k = replicate n a :=
eq_replicate.2 ⟨by rw [length_rotate, length_replicate],
λ b hb, eq_of_mem_replicate $ mem_rotate.1 hb⟩
lemma rotate_eq_drop_append_take {l : list α} {n : ℕ} : n ≤ l.length →
l.rotate n = l.drop n ++ l.take n :=
by rw rotate_eq_rotate'; exact rotate'_eq_drop_append_take
lemma rotate_eq_drop_append_take_mod {l : list α} {n : ℕ} :
l.rotate n = l.drop (n % l.length) ++ l.take (n % l.length) :=
begin
cases l.length.zero_le.eq_or_lt with hl hl,
{ simp [eq_nil_of_length_eq_zero hl.symm ] },
rw [←rotate_eq_drop_append_take (n.mod_lt hl).le, rotate_mod]
end
@[simp] lemma rotate_append_length_eq (l l' : list α) : (l ++ l').rotate l.length = l' ++ l :=
begin
rw rotate_eq_rotate',
induction l generalizing l',
{ simp, },
{ simp [rotate', l_ih] },
end
lemma rotate_rotate (l : list α) (n m : ℕ) : (l.rotate n).rotate m = l.rotate (n + m) :=
by rw [rotate_eq_rotate', rotate_eq_rotate', rotate_eq_rotate', rotate'_rotate']
@[simp] lemma rotate_length (l : list α) : rotate l l.length = l :=
by rw [rotate_eq_rotate', rotate'_length]
@[simp] lemma rotate_length_mul (l : list α) (n : ℕ) : l.rotate (l.length * n) = l :=
by rw [rotate_eq_rotate', rotate'_length_mul]
lemma prod_rotate_eq_one_of_prod_eq_one [group α] : ∀ {l : list α} (hl : l.prod = 1) (n : ℕ),
(l.rotate n).prod = 1
| [] _ _ := by simp
| (a::l) hl n :=
have n % list.length (a :: l) ≤ list.length (a :: l), from le_of_lt (nat.mod_lt _ dec_trivial),
by rw ← list.take_append_drop (n % list.length (a :: l)) (a :: l) at hl;
rw [← rotate_mod, rotate_eq_drop_append_take this, list.prod_append, mul_eq_one_iff_inv_eq,
← one_mul (list.prod _)⁻¹, ← hl, list.prod_append, mul_assoc, mul_inv_self, mul_one]
lemma rotate_perm (l : list α) (n : ℕ) : l.rotate n ~ l :=
begin
rw rotate_eq_rotate',
induction n with n hn generalizing l,
{ simp },
{ cases l with hd tl,
{ simp },
{ rw rotate'_cons_succ,
exact (hn _).trans (perm_append_singleton _ _) } }
end
@[simp] lemma nodup_rotate {l : list α} {n : ℕ} : nodup (l.rotate n) ↔ nodup l :=
(rotate_perm l n).nodup_iff
@[simp] lemma rotate_eq_nil_iff {l : list α} {n : ℕ} : l.rotate n = [] ↔ l = [] :=
begin
induction n with n hn generalizing l,
{ simp },
{ cases l with hd tl,
{ simp },
{ simp [rotate_cons_succ, hn] } }
end
@[simp] lemma nil_eq_rotate_iff {l : list α} {n : ℕ} : [] = l.rotate n ↔ [] = l :=
by rw [eq_comm, rotate_eq_nil_iff, eq_comm]
@[simp] lemma rotate_singleton (x : α) (n : ℕ) :
[x].rotate n = [x] :=
rotate_replicate x 1 n
lemma zip_with_rotate_distrib {α β γ : Type*} (f : α → β → γ) (l : list α) (l' : list β) (n : ℕ)
(h : l.length = l'.length) :
(zip_with f l l').rotate n = zip_with f (l.rotate n) (l'.rotate n) :=
begin
rw [rotate_eq_drop_append_take_mod, rotate_eq_drop_append_take_mod,
rotate_eq_drop_append_take_mod, h, zip_with_append, ←zip_with_distrib_drop,
←zip_with_distrib_take, list.length_zip_with, h, min_self],
rw [length_drop, length_drop, h]
end
local attribute [simp] rotate_cons_succ
@[simp] lemma zip_with_rotate_one {β : Type*} (f : α → α → β) (x y : α) (l : list α) :
zip_with f (x :: y :: l) ((x :: y :: l).rotate 1) =
f x y :: zip_with f (y :: l) (l ++ [x]) :=
by simp
lemma nth_le_rotate_one (l : list α) (k : ℕ) (hk : k < (l.rotate 1).length) :
(l.rotate 1).nth_le k hk = l.nth_le ((k + 1) % l.length)
(mod_lt _ (length_rotate l 1 ▸ k.zero_le.trans_lt hk)) :=
begin
cases l with hd tl,
{ simp },
{ have : k ≤ tl.length,
{ refine nat.le_of_lt_succ _,
simpa using hk },
rcases this.eq_or_lt with rfl|hk',
{ simp [nth_le_append_right le_rfl] },
{ simpa [nth_le_append _ hk', length_cons, nat.mod_eq_of_lt (nat.succ_lt_succ hk')] } }
end
lemma nth_le_rotate (l : list α) (n k : ℕ) (hk : k < (l.rotate n).length) :
(l.rotate n).nth_le k hk = l.nth_le ((k + n) % l.length)
(mod_lt _ (length_rotate l n ▸ k.zero_le.trans_lt hk)) :=
begin
induction n with n hn generalizing l k,
{ have hk' : k < l.length := by simpa using hk,
simp [nat.mod_eq_of_lt hk'] },
{ simp [nat.succ_eq_add_one, ←rotate_rotate, nth_le_rotate_one, hn l, add_comm, add_left_comm] }
end
/-- A variant of `nth_le_rotate` useful for rewrites. -/
lemma nth_le_rotate' (l : list α) (n k : ℕ) (hk : k < l.length) :
(l.rotate n).nth_le ((l.length - n % l.length + k) % l.length)
((nat.mod_lt _ (k.zero_le.trans_lt hk)).trans_le (length_rotate _ _).ge) = l.nth_le k hk :=
begin
rw nth_le_rotate,
congr,
set m := l.length,
rw [mod_add_mod, add_assoc, add_left_comm, add_comm, add_mod, add_mod _ n],
cases (n % m).zero_le.eq_or_lt with hn hn,
{ simpa [←hn] using nat.mod_eq_of_lt hk },
{ have mpos : 0 < m := k.zero_le.trans_lt hk,
have hm : m - n % m < m := tsub_lt_self mpos hn,
have hn' : n % m < m := nat.mod_lt _ mpos,
simpa [mod_eq_of_lt hm, tsub_add_cancel_of_le hn'.le] using nat.mod_eq_of_lt hk }
end
lemma nth_rotate {l : list α} {n m : ℕ} (hml : m < l.length) :
(l.rotate n).nth m = l.nth ((m + n) % l.length) :=
begin
rw [nth_le_nth, nth_le_nth (nat.mod_lt _ _), nth_le_rotate],
rwa [length_rotate]
end
lemma head'_rotate {l : list α} {n : ℕ} (h : n < l.length) :
head' (l.rotate n) = l.nth n :=
by rw [← nth_zero, nth_rotate (n.zero_le.trans_lt h), zero_add, nat.mod_eq_of_lt h]
lemma rotate_eq_self_iff_eq_replicate [hα : nonempty α] :
∀ {l : list α}, (∀ n, l.rotate n = l) ↔ ∃ a, l = replicate l.length a
| [] := by simp
| (a :: l) := ⟨λ h, ⟨a, ext_le (length_replicate _ _).symm $ λ n h₁ h₂,
begin
inhabit α,
rw [nth_le_replicate, ← option.some_inj, ← nth_le_nth, ← head'_rotate h₁, h, head']
end⟩, λ ⟨b, hb⟩ n, by rw [hb, rotate_replicate]⟩
lemma rotate_one_eq_self_iff_eq_replicate [nonempty α] {l : list α} :
l.rotate 1 = l ↔ ∃ a : α, l = list.replicate l.length a :=
⟨λ h, rotate_eq_self_iff_eq_replicate.mp (λ n, nat.rec l.rotate_zero
(λ n hn, by rwa [nat.succ_eq_add_one, ←l.rotate_rotate, hn]) n),
λ h, rotate_eq_self_iff_eq_replicate.mpr h 1⟩
lemma rotate_injective (n : ℕ) : function.injective (λ l : list α, l.rotate n) :=
begin
rintro l₁ l₂ (h : l₁.rotate n = l₂.rotate n),
have : length l₁ = length l₂, by simpa only [length_rotate] using congr_arg length h,
refine ext_le this (λ k h₁ h₂, _),
rw [← nth_le_rotate' l₁ n, ← nth_le_rotate' l₂ n],
congr' 1; simp only [h, this]
end
@[simp] lemma rotate_eq_rotate {l l' : list α} {n : ℕ} :
l.rotate n = l'.rotate n ↔ l = l' :=
(rotate_injective n).eq_iff
lemma rotate_eq_iff {l l' : list α} {n : ℕ} :
l.rotate n = l' ↔ l = l'.rotate (l'.length - n % l'.length) :=
begin
rw [←@rotate_eq_rotate _ l _ n, rotate_rotate, ←rotate_mod l', add_mod],
cases l'.length.zero_le.eq_or_lt with hl hl,
{ rw [eq_nil_of_length_eq_zero hl.symm, rotate_nil, rotate_eq_nil_iff] },
{ cases (nat.zero_le (n % l'.length)).eq_or_lt with hn hn,
{ simp [←hn] },
{ rw [mod_eq_of_lt (tsub_lt_self hl hn), tsub_add_cancel_of_le, mod_self, rotate_zero],
exact (nat.mod_lt _ hl).le } }
end
@[simp] lemma rotate_eq_singleton_iff {l : list α} {n : ℕ} {x : α} : l.rotate n = [x] ↔ l = [x] :=
by rw [rotate_eq_iff, rotate_singleton]
@[simp] lemma singleton_eq_rotate_iff {l : list α} {n : ℕ} {x : α} : [x] = l.rotate n ↔ [x] = l :=
by rw [eq_comm, rotate_eq_singleton_iff, eq_comm]
lemma reverse_rotate (l : list α) (n : ℕ) :
(l.rotate n).reverse = l.reverse.rotate (l.length - (n % l.length)) :=
begin
rw [←length_reverse l, ←rotate_eq_iff],
induction n with n hn generalizing l,
{ simp },
{ cases l with hd tl,
{ simp },
{ rw [rotate_cons_succ, nat.succ_eq_add_one, ←rotate_rotate, hn],
simp } }
end
lemma rotate_reverse (l : list α) (n : ℕ) :
l.reverse.rotate n = (l.rotate (l.length - (n % l.length))).reverse :=
begin
rw [←reverse_reverse l],
simp_rw [reverse_rotate, reverse_reverse, rotate_eq_iff, rotate_rotate, length_rotate,
length_reverse],
rw [←length_reverse l],
set k := n % l.reverse.length with hk,
cases hk' : k with k',
{ simp [-length_reverse, ←rotate_rotate] },
{ cases l with x l,
{ simp },
{ have : k'.succ < (x :: l).length,
{ simp [←hk', hk, nat.mod_lt] },
rw [nat.mod_eq_of_lt, tsub_add_cancel_of_le, rotate_length],
{ exact tsub_le_self },
{ exact tsub_lt_self (by simp) nat.succ_pos' } } }
end
lemma map_rotate {β : Type*} (f : α → β) (l : list α) (n : ℕ) :
map f (l.rotate n) = (map f l).rotate n :=
begin
induction n with n hn IH generalizing l,
{ simp },
{ cases l with hd tl,
{ simp },
{ simp [hn] } }
end
theorem nodup.rotate_eq_self_iff {l : list α} (hl : l.nodup) {n : ℕ} :
l.rotate n = l ↔ n % l.length = 0 ∨ l = [] :=
begin
split,
{ intro h,
cases l.length.zero_le.eq_or_lt with hl' hl',
{ simp [←length_eq_zero, ←hl'] },
left,
rw nodup_iff_nth_le_inj at hl,
refine hl _ _ (mod_lt _ hl') hl' _,
rw ←nth_le_rotate' _ n,
simp_rw [h, tsub_add_cancel_of_le (mod_lt _ hl').le, mod_self] },
{ rintro (h|h),
{ rw [←rotate_mod, h],
exact rotate_zero l },
{ simp [h] } }
end
lemma nodup.rotate_congr {l : list α} (hl : l.nodup) (hn : l ≠ []) (i j : ℕ)
(h : l.rotate i = l.rotate j) : i % l.length = j % l.length :=
begin
have hi : i % l.length < l.length := mod_lt _ (length_pos_of_ne_nil hn),
have hj : j % l.length < l.length := mod_lt _ (length_pos_of_ne_nil hn),
refine (nodup_iff_nth_le_inj.mp hl) _ _ hi hj _,
rw [←nth_le_rotate' l i, ←nth_le_rotate' l j],
simp [tsub_add_cancel_of_le, hi.le, hj.le, h]
end
section is_rotated
variables (l l' : list α)
/-- `is_rotated l₁ l₂` or `l₁ ~r l₂` asserts that `l₁` and `l₂` are cyclic permutations
of each other. This is defined by claiming that `∃ n, l.rotate n = l'`. -/
def is_rotated : Prop := ∃ n, l.rotate n = l'
infixr ` ~r `:1000 := is_rotated
variables {l l'}
@[refl] lemma is_rotated.refl (l : list α) : l ~r l :=
⟨0, by simp⟩
@[symm] lemma is_rotated.symm (h : l ~r l') : l' ~r l :=
begin
obtain ⟨n, rfl⟩ := h,
cases l with hd tl,
{ simp },
{ use (hd :: tl).length * n - n,
rw [rotate_rotate, add_tsub_cancel_of_le, rotate_length_mul],
exact nat.le_mul_of_pos_left (by simp) }
end
lemma is_rotated_comm : l ~r l' ↔ l' ~r l :=
⟨is_rotated.symm, is_rotated.symm⟩
@[simp] protected lemma is_rotated.forall (l : list α) (n : ℕ) : l.rotate n ~r l :=
is_rotated.symm ⟨n, rfl⟩
@[trans] lemma is_rotated.trans : ∀ {l l' l'' : list α}, l ~r l' → l' ~r l'' → l ~r l''
| _ _ _ ⟨n, rfl⟩ ⟨m, rfl⟩ := ⟨n + m, by rw [rotate_rotate]⟩
lemma is_rotated.eqv : equivalence (@is_rotated α) :=
mk_equivalence _ is_rotated.refl (λ _ _, is_rotated.symm) (λ _ _ _, is_rotated.trans)
/-- The relation `list.is_rotated l l'` forms a `setoid` of cycles. -/
def is_rotated.setoid (α : Type*) : setoid (list α) :=
{ r := is_rotated, iseqv := is_rotated.eqv }
lemma is_rotated.perm (h : l ~r l') : l ~ l' :=
exists.elim h (λ _ hl, hl ▸ (rotate_perm _ _).symm)
lemma is_rotated.nodup_iff (h : l ~r l') : nodup l ↔ nodup l' :=
h.perm.nodup_iff
lemma is_rotated.mem_iff (h : l ~r l') {a : α} : a ∈ l ↔ a ∈ l' :=
h.perm.mem_iff
@[simp] lemma is_rotated_nil_iff : l ~r [] ↔ l = [] :=
⟨λ ⟨n, hn⟩, by simpa using hn, λ h, h ▸ by refl⟩
@[simp] lemma is_rotated_nil_iff' : [] ~r l ↔ [] = l :=
by rw [is_rotated_comm, is_rotated_nil_iff, eq_comm]
@[simp] lemma is_rotated_singleton_iff {x : α} : l ~r [x] ↔ l = [x] :=
⟨λ ⟨n, hn⟩, by simpa using hn, λ h, h ▸ by refl⟩
@[simp] lemma is_rotated_singleton_iff' {x : α} : [x] ~r l ↔ [x] = l :=
by rw [is_rotated_comm, is_rotated_singleton_iff, eq_comm]
lemma is_rotated_concat (hd : α) (tl : list α) :
(tl ++ [hd]) ~r (hd :: tl) :=
is_rotated.symm ⟨1, by simp⟩
lemma is_rotated_append : (l ++ l') ~r (l' ++ l) :=
⟨l.length, by simp⟩
lemma is_rotated.reverse (h : l ~r l') : l.reverse ~r l'.reverse :=
begin
obtain ⟨n, rfl⟩ := h,
exact ⟨_, (reverse_rotate _ _).symm⟩
end
lemma is_rotated_reverse_comm_iff :
l.reverse ~r l' ↔ l ~r l'.reverse :=
begin
split;
{ intro h,
simpa using h.reverse }
end
@[simp] lemma is_rotated_reverse_iff :
l.reverse ~r l'.reverse ↔ l ~r l' :=
by simp [is_rotated_reverse_comm_iff]
lemma is_rotated_iff_mod : l ~r l' ↔ ∃ n ≤ l.length, l.rotate n = l' :=
begin
refine ⟨λ h, _, λ ⟨n, _, h⟩, ⟨n, h⟩⟩,
obtain ⟨n, rfl⟩ := h,
cases l with hd tl,
{ simp },
{ refine ⟨n % (hd :: tl).length, _, rotate_mod _ _⟩,
refine (nat.mod_lt _ _).le,
simp }
end
lemma is_rotated_iff_mem_map_range : l ~r l' ↔ l' ∈ (list.range (l.length + 1)).map l.rotate :=
begin
simp_rw [mem_map, mem_range, is_rotated_iff_mod],
exact ⟨λ ⟨n, hn, h⟩, ⟨n, nat.lt_succ_of_le hn, h⟩, λ ⟨n, hn, h⟩, ⟨n, nat.le_of_lt_succ hn, h⟩⟩
end
@[congr] theorem is_rotated.map {β : Type*} {l₁ l₂ : list α} (h : l₁ ~r l₂) (f : α → β) :
map f l₁ ~r map f l₂ :=
begin
obtain ⟨n, rfl⟩ := h,
rw map_rotate,
use n
end
/-- List of all cyclic permutations of `l`.
The `cyclic_permutations` of a nonempty list `l` will always contain `list.length l` elements.
This implies that under certain conditions, there are duplicates in `list.cyclic_permutations l`.
The `n`th entry is equal to `l.rotate n`, proven in `list.nth_le_cyclic_permutations`.
The proof that every cyclic permutant of `l` is in the list is `list.mem_cyclic_permutations_iff`.
cyclic_permutations [1, 2, 3, 2, 4] =
[[1, 2, 3, 2, 4], [2, 3, 2, 4, 1], [3, 2, 4, 1, 2],
[2, 4, 1, 2, 3], [4, 1, 2, 3, 2]] -/
def cyclic_permutations : list α → list (list α)
| [] := [[]]
| l@(_ :: _) := init (zip_with (++) (tails l) (inits l))
@[simp] lemma cyclic_permutations_nil : cyclic_permutations ([] : list α) = [[]] := rfl
lemma cyclic_permutations_cons (x : α) (l : list α) :
cyclic_permutations (x :: l) = init (zip_with (++) (tails (x :: l)) (inits (x :: l))) := rfl
lemma cyclic_permutations_of_ne_nil (l : list α) (h : l ≠ []) :
cyclic_permutations l = init (zip_with (++) (tails l) (inits l)) :=
begin
obtain ⟨hd, tl, rfl⟩ := exists_cons_of_ne_nil h,
exact cyclic_permutations_cons _ _,
end
lemma length_cyclic_permutations_cons (x : α) (l : list α) :
length (cyclic_permutations (x :: l)) = length l + 1 :=
by simp [cyclic_permutations_of_ne_nil]
@[simp] lemma length_cyclic_permutations_of_ne_nil (l : list α) (h : l ≠ []) :
length (cyclic_permutations l) = length l :=
by simp [cyclic_permutations_of_ne_nil _ h]
@[simp] lemma nth_le_cyclic_permutations (l : list α) (n : ℕ)
(hn : n < length (cyclic_permutations l)) :
nth_le (cyclic_permutations l) n hn = l.rotate n :=
begin
obtain rfl | h := eq_or_ne l [],
{ simp },
{ rw length_cyclic_permutations_of_ne_nil _ h at hn,
simp [init_eq_take, cyclic_permutations_of_ne_nil _ h, nth_le_take',
rotate_eq_drop_append_take hn.le] }
end
lemma mem_cyclic_permutations_self (l : list α) :
l ∈ cyclic_permutations l :=
begin
cases l with x l,
{ simp },
{ rw mem_iff_nth_le,
refine ⟨0, by simp, _⟩,
simp }
end
lemma length_mem_cyclic_permutations (l : list α) (h : l' ∈ cyclic_permutations l) :
length l' = length l :=
begin
obtain ⟨k, hk, rfl⟩ := nth_le_of_mem h,
simp
end
@[simp] lemma mem_cyclic_permutations_iff {l l' : list α} :
l ∈ cyclic_permutations l' ↔ l ~r l' :=
begin
split,
{ intro h,
obtain ⟨k, hk, rfl⟩ := nth_le_of_mem h,
simp },
{ intro h,
obtain ⟨k, rfl⟩ := h.symm,
rw mem_iff_nth_le,
simp only [exists_prop, nth_le_cyclic_permutations],
cases l' with x l,
{ simp },
{ refine ⟨k % length (x :: l), _, rotate_mod _ _⟩,
simpa using nat.mod_lt _ (zero_lt_succ _) } }
end
@[simp] lemma cyclic_permutations_eq_nil_iff {l : list α} :
cyclic_permutations l = [[]] ↔ l = [] :=
begin
refine ⟨λ h, _, λ h, by simp [h]⟩,
rw [eq_comm, ←is_rotated_nil_iff', ←mem_cyclic_permutations_iff, h, mem_singleton]
end
@[simp] lemma cyclic_permutations_eq_singleton_iff {l : list α} {x : α} :
cyclic_permutations l = [[x]] ↔ l = [x] :=
begin
refine ⟨λ h, _, λ h, by simp [cyclic_permutations, h, init_eq_take]⟩,
rw [eq_comm, ←is_rotated_singleton_iff', ←mem_cyclic_permutations_iff, h, mem_singleton]
end
/-- If a `l : list α` is `nodup l`, then all of its cyclic permutants are distinct. -/
lemma nodup.cyclic_permutations {l : list α} (hn : nodup l) :
nodup (cyclic_permutations l) :=
begin
cases l with x l,
{ simp },
rw nodup_iff_nth_le_inj,
intros i j hi hj h,
simp only [length_cyclic_permutations_cons] at hi hj,
rw [←mod_eq_of_lt hi, ←mod_eq_of_lt hj, ←length_cons x l],
apply hn.rotate_congr,
{ simp },
{ simpa using h }
end
@[simp] lemma cyclic_permutations_rotate (l : list α) (k : ℕ) :
(l.rotate k).cyclic_permutations = l.cyclic_permutations.rotate k :=
begin
have : (l.rotate k).cyclic_permutations.length = length (l.cyclic_permutations.rotate k),
{ cases l,
{ simp },
{ rw length_cyclic_permutations_of_ne_nil;
simp } },
refine ext_le this (λ n hn hn', _),
rw [nth_le_cyclic_permutations, nth_le_rotate, nth_le_cyclic_permutations,
rotate_rotate, ←rotate_mod, add_comm],
cases l;
simp
end
lemma is_rotated.cyclic_permutations {l l' : list α} (h : l ~r l') :
l.cyclic_permutations ~r l'.cyclic_permutations :=
begin
obtain ⟨k, rfl⟩ := h,
exact ⟨k, by simp⟩
end
@[simp] lemma is_rotated_cyclic_permutations_iff {l l' : list α} :
l.cyclic_permutations ~r l'.cyclic_permutations ↔ l ~r l' :=
begin
by_cases hl : l = [],
{ simp [hl, eq_comm] },
have hl' : l.cyclic_permutations.length = l.length := length_cyclic_permutations_of_ne_nil _ hl,
refine ⟨λ h, _, is_rotated.cyclic_permutations⟩,
obtain ⟨k, hk⟩ := h,
refine ⟨k % l.length, _⟩,
have hk' : k % l.length < l.length := mod_lt _ (length_pos_of_ne_nil hl),
rw [←nth_le_cyclic_permutations _ _ (hk'.trans_le hl'.ge), ←nth_le_rotate' _ k],
simp [hk, hl', tsub_add_cancel_of_le hk'.le]
end
section decidable
variables [decidable_eq α]
instance is_rotated_decidable (l l' : list α) : decidable (l ~r l') :=
decidable_of_iff' _ is_rotated_iff_mem_map_range
instance {l l' : list α} : decidable (@setoid.r _ (is_rotated.setoid α) l l') :=
list.is_rotated_decidable _ _
end decidable
end is_rotated
end list
|
562e19737526cd463e51fda36ec303f43188f183 | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /tactic/linarith.lean | 0c090a8d265e781bb5eff937d103db921c131db1 | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 27,913 | lean | /-
Copyright (c) 2018 Robert Y. Lewis. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Robert Y. Lewis
A tactic for discharging linear arithmetic goals using Fourier-Motzkin elimination.
`linarith` is (in principle) complete for ℚ and ℝ. It is not complete for non-dense orders, i.e. ℤ.
@TODO: investigate storing comparisons in a list instead of a set, for possible efficiency gains
@TODO: perform slightly better on ℤ by strengthening t < 0 hyps to t + 1 ≤ 0
@TODO: alternative discharger to `ring`
@TODO: delay proofs of denominator normalization and nat casting until after contradiction is found
-/
import tactic.ring data.nat.gcd data.list.basic meta.rb_map
meta def nat.to_pexpr : ℕ → pexpr
| 0 := ``(0)
| 1 := ``(1)
| n := if n % 2 = 0 then ``(bit0 %%(nat.to_pexpr (n/2))) else ``(bit1 %%(nat.to_pexpr (n/2)))
open native
namespace linarith
section lemmas
lemma int.coe_nat_bit0 (n : ℕ) : (↑(bit0 n : ℕ) : ℤ) = bit0 (↑n : ℤ) := by simp [bit0]
lemma int.coe_nat_bit1 (n : ℕ) : (↑(bit1 n : ℕ) : ℤ) = bit1 (↑n : ℤ) := by simp [bit1, bit0]
lemma nat_eq_subst {n1 n2 : ℕ} {z1 z2 : ℤ} (hn : n1 = n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 = z2 :=
by simpa [eq.symm h1, eq.symm h2, int.coe_nat_eq_coe_nat_iff]
lemma nat_le_subst {n1 n2 : ℕ} {z1 z2 : ℤ} (hn : n1 ≤ n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 ≤ z2 :=
by simpa [eq.symm h1, eq.symm h2, int.coe_nat_le]
lemma nat_lt_subst {n1 n2 : ℕ} {z1 z2 : ℤ} (hn : n1 < n2) (h1 : ↑n1 = z1) (h2 : ↑n2 = z2) : z1 < z2 :=
by simpa [eq.symm h1, eq.symm h2, int.coe_nat_lt]
lemma eq_of_eq_of_eq {α} [ordered_semiring α] {a b : α} (ha : a = 0) (hb : b = 0) : a + b = 0 :=
by simp *
lemma le_of_eq_of_le {α} [ordered_semiring α] {a b : α} (ha : a = 0) (hb : b ≤ 0) : a + b ≤ 0 :=
by simp *
lemma lt_of_eq_of_lt {α} [ordered_semiring α] {a b : α} (ha : a = 0) (hb : b < 0) : a + b < 0 :=
by simp *
lemma le_of_le_of_eq {α} [ordered_semiring α] {a b : α} (ha : a ≤ 0) (hb : b = 0) : a + b ≤ 0 :=
by simp *
lemma lt_of_lt_of_eq {α} [ordered_semiring α] {a b : α} (ha : a < 0) (hb : b = 0) : a + b < 0 :=
by simp *
lemma mul_neg {α} [ordered_ring α] {a b : α} (ha : a < 0) (hb : b > 0) : b * a < 0 :=
have (-b)*a > 0, from mul_pos_of_neg_of_neg (neg_neg_of_pos hb) ha,
neg_of_neg_pos (by simpa)
lemma mul_nonpos {α} [ordered_ring α] {a b : α} (ha : a ≤ 0) (hb : b > 0) : b * a ≤ 0 :=
have (-b)*a ≥ 0, from mul_nonneg_of_nonpos_of_nonpos (le_of_lt (neg_neg_of_pos hb)) ha,
nonpos_of_neg_nonneg (by simp at this; exact this)
lemma mul_eq {α} [ordered_semiring α] {a b : α} (ha : a = 0) (hb : b > 0) : b * a = 0 :=
by simp *
lemma eq_of_not_lt_of_not_gt {α} [linear_order α] (a b : α) (h1 : ¬ a < b) (h2 : ¬ b < a) : a = b :=
le_antisymm (le_of_not_gt h2) (le_of_not_gt h1)
lemma add_subst {α} [ring α] {n e1 e2 t1 t2 : α} (h1 : n * e1 = t1) (h2 : n * e2 = t2) :
n * (e1 + e2) = t1 + t2 := by simp [left_distrib, *]
lemma sub_subst {α} [ring α] {n e1 e2 t1 t2 : α} (h1 : n * e1 = t1) (h2 : n * e2 = t2) :
n * (e1 - e2) = t1 - t2 := by simp [left_distrib, *]
lemma neg_subst {α} [ring α] {n e t : α} (h1 : n * e = t) : n * (-e) = -t := by simp *
private meta def apnn : tactic unit := `[norm_num]
lemma mul_subst {α} [comm_ring α] {n1 n2 k e1 e2 t1 t2 : α} (h1 : n1 * e1 = t1) (h2 : n2 * e2 = t2)
(h3 : n1*n2 = k . apnn) : k * (e1 * e2) = t1 * t2 :=
have h3 : n1 * n2 = k, from h3,
by rw [←h3, mul_comm n1, mul_assoc n2, ←mul_assoc n1, h1, ←mul_assoc n2, mul_comm n2, mul_assoc, h2] -- OUCH
lemma div_subst {α} [field α] {n1 n2 k e1 e2 t1 : α} (h1 : n1 * e1 = t1) (h2 : n2 / e2 = 1) (h3 : n1*n2 = k) :
k * (e1 / e2) = t1 :=
by rw [←h3, mul_assoc, mul_div_comm, h2, ←mul_assoc, h1, mul_comm, one_mul]
end lemmas
section datatypes
@[derive decidable_eq]
inductive ineq
| eq | le | lt
open ineq
def ineq.max : ineq → ineq → ineq
| eq a := a
| le a := a
| lt a := lt
def ineq.is_lt : ineq → ineq → bool
| eq le := tt
| eq lt := tt
| le lt := tt
| _ _ := ff
def ineq.to_string : ineq → string
| eq := "="
| le := "≤"
| lt := "<"
instance : has_to_string ineq := ⟨ineq.to_string⟩
/--
The main datatype for FM elimination.
Variables are represented by natural numbers, each of which has an integer coefficient.
Index 0 is reserved for constants, i.e. `coeffs.find 0` is the coefficient of 1.
The represented term is coeffs.keys.sum (λ i, coeffs.find i * Var[i]).
str determines the direction of the comparison -- is it < 0, ≤ 0, or = 0?
-/
meta structure comp :=
(str : ineq)
(coeffs : rb_map ℕ int)
meta instance : inhabited comp := ⟨⟨ineq.eq, mk_rb_map⟩⟩
meta inductive comp_source
| assump : ℕ → comp_source
| add : comp_source → comp_source → comp_source
| scale : ℕ → comp_source → comp_source
meta def comp_source.flatten : comp_source → rb_map ℕ ℕ
| (comp_source.assump n) := mk_rb_map.insert n 1
| (comp_source.add c1 c2) := (comp_source.flatten c1).add (comp_source.flatten c2)
| (comp_source.scale n c) := (comp_source.flatten c).map (λ v, v * n)
meta def comp_source.to_string : comp_source → string
| (comp_source.assump e) := to_string e
| (comp_source.add c1 c2) := comp_source.to_string c1 ++ " + " ++ comp_source.to_string c2
| (comp_source.scale n c) := to_string n ++ " * " ++ comp_source.to_string c
meta instance comp_source.has_to_format : has_to_format comp_source :=
⟨λ a, comp_source.to_string a⟩
meta structure pcomp :=
(c : comp)
(src : comp_source)
meta def map_lt (m1 m2 : rb_map ℕ int) : bool :=
list.lex (prod.lex (<) (<)) m1.to_list m2.to_list
-- make more efficient
meta def comp.lt (c1 c2 : comp) : bool :=
(c1.str.is_lt c2.str) || (c1.str = c2.str) && map_lt c1.coeffs c2.coeffs
meta instance comp.has_lt : has_lt comp := ⟨λ a b, comp.lt a b⟩
meta instance pcomp.has_lt : has_lt pcomp := ⟨λ p1 p2, p1.c < p2.c⟩
meta instance pcomp.has_lt_dec : decidable_rel ((<) : pcomp → pcomp → Prop) := by apply_instance
meta def comp.coeff_of (c : comp) (a : ℕ) : ℤ :=
c.coeffs.zfind a
meta def comp.scale (c : comp) (n : ℕ) : comp :=
{ c with coeffs := c.coeffs.map ((*) (n : ℤ)) }
meta def comp.add (c1 c2 : comp) : comp :=
⟨c1.str.max c2.str, c1.coeffs.add c2.coeffs⟩
meta def pcomp.scale (c : pcomp) (n : ℕ) : pcomp :=
⟨c.c.scale n, comp_source.scale n c.src⟩
meta def pcomp.add (c1 c2 : pcomp) : pcomp :=
⟨c1.c.add c2.c, comp_source.add c1.src c2.src⟩
meta instance pcomp.to_format : has_to_format pcomp :=
⟨λ p, to_fmt p.c.coeffs ++ to_string p.c.str ++ "0"⟩
meta instance comp.to_format : has_to_format comp :=
⟨λ p, to_fmt p.coeffs⟩
end datatypes
section fm_elim
/-- If c1 and c2 both contain variable a with opposite coefficients,
produces v1, v2, and c such that a has been cancelled in c := v1*c1 + v2*c2 -/
meta def elim_var (c1 c2 : comp) (a : ℕ) : option (ℕ × ℕ × comp) :=
let v1 := c1.coeff_of a,
v2 := c2.coeff_of a in
if v1 * v2 < 0 then
let vlcm := nat.lcm v1.nat_abs v2.nat_abs,
v1' := vlcm / v1.nat_abs,
v2' := vlcm / v2.nat_abs in
some ⟨v1', v2', comp.add (c1.scale v1') (c2.scale v2')⟩
else none
meta def pelim_var (p1 p2 : pcomp) (a : ℕ) : option pcomp :=
do (n1, n2, c) ← elim_var p1.c p2.c a,
return ⟨c, comp_source.add (p1.src.scale n1) (p2.src.scale n2)⟩
meta def comp.is_contr (c : comp) : bool := c.coeffs.empty ∧ c.str = ineq.lt
meta def pcomp.is_contr (p : pcomp) : bool := p.c.is_contr
meta def elim_with_set (a : ℕ) (p : pcomp) (comps : rb_set pcomp) : rb_set pcomp :=
if ¬ p.c.coeffs.contains a then mk_rb_set.insert p else
comps.fold mk_rb_set $ λ pc s,
match pelim_var p pc a with
| some pc := s.insert pc
| none := s
end
/--
The state for the elimination monad.
vars: the set of variables present in comps
comps: a set of comparisons
inputs: a set of pairs of exprs (t, pf), where t is a term and pf is a proof that t {<, ≤, =} 0,
indexed by ℕ.
has_false: stores a pcomp of 0 < 0 if one has been found
TODO: is it more efficient to store comps as a list, to avoid comparisons?
-/
meta structure linarith_structure :=
(vars : rb_set ℕ)
(comps : rb_set pcomp)
@[reducible] meta def linarith_monad :=
state_t linarith_structure (except_t pcomp id)
meta instance : monad linarith_monad := state_t.monad
meta instance : monad_except pcomp linarith_monad :=
state_t.monad_except pcomp
meta def get_vars : linarith_monad (rb_set ℕ) :=
linarith_structure.vars <$> get
meta def get_var_list : linarith_monad (list ℕ) :=
rb_set.to_list <$> get_vars
meta def get_comps : linarith_monad (rb_set pcomp) :=
linarith_structure.comps <$> get
meta def validate : linarith_monad unit :=
do ⟨_, comps⟩ ← get,
match comps.to_list.find (λ p : pcomp, p.is_contr) with
| none := return ()
| some c := throw c
end
meta def update (vars : rb_set ℕ) (comps : rb_set pcomp) : linarith_monad unit :=
state_t.put ⟨vars, comps⟩ >> validate
meta def monad.elim_var (a : ℕ) : linarith_monad unit :=
do vs ← get_vars,
when (vs.contains a) $
do comps ← get_comps,
let cs' := comps.fold mk_rb_set (λ p s, s.union (elim_with_set a p comps)),
update (vs.erase a) cs'
meta def elim_all_vars : linarith_monad unit :=
get_var_list >>= list.mmap' monad.elim_var
end fm_elim
section parse
open ineq tactic
meta def map_of_expr_mul_aux (c1 c2 : rb_map ℕ ℤ) : option (rb_map ℕ ℤ) :=
match c1.keys, c2.keys with
| [0], _ := some $ c2.scale (c1.zfind 0)
| _, [0] := some $ c1.scale (c2.zfind 0)
| _, _ := none
end
/--
Turns an expression into a map from ℕ to ℤ, for use in a comp object.
The expr_map ℕ argument identifies which expressions have already been assigned numbers.
Returns a new map.
-/
meta def map_of_expr : expr_map ℕ → expr → option (expr_map ℕ × rb_map ℕ ℤ)
| m `(%%e1 * %%e2) :=
do (m', comp1) ← map_of_expr m e1,
(m', comp2) ← map_of_expr m' e2,
mp ← map_of_expr_mul_aux comp1 comp2,
return (m', mp)
| m `(%%e1 + %%e2) :=
do (m', comp1) ← map_of_expr m e1,
(m', comp2) ← map_of_expr m' e2,
return (m', comp1.add comp2)
| m `(%%e1 - %%e2) :=
do (m', comp1) ← map_of_expr m e1,
(m', comp2) ← map_of_expr m' e2,
return (m', comp1.add (comp2.scale (-1)))
| m `(-%%e) := do (m', comp) ← map_of_expr m e, return (m', comp.scale (-1))
| m e :=
match e.to_int, m.find e with
| some 0, _ := return ⟨m, mk_rb_map⟩
| some z, _ := return ⟨m, mk_rb_map.insert 0 z⟩
| none, some k := return (m, mk_rb_map.insert k 1)
| none, none := let n := m.size + 1 in
return (m.insert e n, mk_rb_map.insert n 1)
end
meta def parse_into_comp_and_expr : expr → option (ineq × expr)
| `(%%e < 0) := (ineq.lt, e)
| `(%%e ≤ 0) := (ineq.le, e)
| `(%%e = 0) := (ineq.eq, e)
| _ := none
meta def to_comp (e : expr) (m : expr_map ℕ) : option (comp × expr_map ℕ) :=
do (iq, e) ← parse_into_comp_and_expr e,
(m', comp') ← map_of_expr m e,
return ⟨⟨iq, comp'⟩, m'⟩
meta def to_comp_fold : expr_map ℕ → list expr →
(list (option comp) × expr_map ℕ)
| m [] := ([], m)
| m (h::t) :=
match to_comp h m with
| some (c, m') := let (l, mp) := to_comp_fold m' t in (c::l, mp)
| none := let (l, mp) := to_comp_fold m t in (none::l, mp)
end
/--
Takes a list of proofs of props of the form t {<, ≤, =} 0, and creates a linarith_structure.
-/
meta def mk_linarith_structure (l : list expr) : tactic (linarith_structure × rb_map ℕ (expr × expr)) :=
do pftps ← l.mmap infer_type,
let (l', map) := to_comp_fold mk_rb_map pftps,
let lz := list.enum $ ((l.zip pftps).zip l').filter_map (λ ⟨a, b⟩, prod.mk a <$> b),
let prmap := rb_map.of_list $ lz.map (λ ⟨n, x⟩, (n, x.1)),
let vars : rb_set ℕ := rb_map.set_of_list $ list.range map.size.succ,
let pc : rb_set pcomp := rb_map.set_of_list $
lz.map (λ ⟨n, x⟩, ⟨x.2, comp_source.assump n⟩),
return (⟨vars, pc⟩, prmap)
meta def linarith_monad.run {α} (tac : linarith_monad α) (l : list expr) : tactic ((pcomp ⊕ α) × rb_map ℕ (expr × expr)) :=
do (struct, inputs) ← mk_linarith_structure l,
match (state_t.run (validate >> tac) struct).run with
| (except.ok (a, _)) := return (sum.inr a, inputs)
| (except.error contr) := return (sum.inl contr, inputs)
end
end parse
section prove
open ineq tactic
meta def get_rel_sides : expr → tactic (expr × expr)
| `(%%a < %%b) := return (a, b)
| `(%%a ≤ %%b) := return (a, b)
| `(%%a = %%b) := return (a, b)
| `(%%a ≥ %%b) := return (a, b)
| `(%%a > %%b) := return (a, b)
| _ := failed
meta def mul_expr (n : ℕ) (e : expr) : pexpr :=
if n = 1 then ``(%%e) else
``(%%(nat.to_pexpr n) * %%e)
meta def add_exprs_aux : pexpr → list pexpr → pexpr
| p [] := p
| p [a] := ``(%%p + %%a)
| p (h::t) := add_exprs_aux ``(%%p + %%h) t
meta def add_exprs : list pexpr → pexpr
| [] := ``(0)
| (h::t) := add_exprs_aux h t
meta def find_contr (m : rb_set pcomp) : option pcomp :=
m.keys.find (λ p, p.c.is_contr)
meta def ineq_const_mul_nm : ineq → name
| lt := ``mul_neg
| le := ``mul_nonpos
| eq := ``mul_eq
meta def ineq_const_nm : ineq → ineq → (name × ineq)
| eq eq := (``eq_of_eq_of_eq, eq)
| eq le := (``le_of_eq_of_le, le)
| eq lt := (``lt_of_eq_of_lt, lt)
| le eq := (``le_of_le_of_eq, le)
| le le := (`add_nonpos, le)
| le lt := (`add_neg_of_nonpos_of_neg, lt)
| lt eq := (``lt_of_lt_of_eq, lt)
| lt le := (`add_neg_of_neg_of_nonpos, lt)
| lt lt := (`add_neg, lt)
meta def mk_single_comp_zero_pf (c : ℕ) (h : expr) : tactic (ineq × expr) :=
do tp ← infer_type h,
some (iq, e) ← return $ parse_into_comp_and_expr tp,
if c = 0 then
do e' ← mk_app ``zero_mul [e], return (eq, e')
else if c = 1 then return (iq, h)
else
do nm ← resolve_name (ineq_const_mul_nm iq),
tp ← (prod.snd <$> (infer_type h >>= get_rel_sides)) >>= infer_type,
cpos ← to_expr ``((%%c.to_pexpr : %%tp) > 0),
(_, ex) ← solve_aux cpos `[norm_num, done],
-- e' ← mk_app (ineq_const_mul_nm iq) [h, ex], -- this takes many seconds longer in some examples! why?
e' ← to_expr ``(%%nm %%h %%ex) ff,
return (iq, e')
meta def mk_lt_zero_pf_aux (c : ineq) (pf npf : expr) (coeff : ℕ) : tactic (ineq × expr) :=
do (iq, h') ← mk_single_comp_zero_pf coeff npf,
let (nm, niq) := ineq_const_nm c iq,
n ← resolve_name nm,
e' ← to_expr ``(%%n %%pf %%h'),
return (niq, e')
/--
Takes a list of coefficients [c] and list of expressions, of equal length.
Each expression is a proof of a prop of the form t {<, ≤, =} 0.
Produces a proof that the sum of (c*t) {<, ≤, =} 0, where the comp is as strong as possible.
-/
meta def mk_lt_zero_pf : list ℕ → list expr → tactic expr
| _ [] := fail "no linear hypotheses found"
| [c] [h] := prod.snd <$> mk_single_comp_zero_pf c h
| (c::ct) (h::t) :=
do (iq, h') ← mk_single_comp_zero_pf c h,
prod.snd <$> (ct.zip t).mfoldl (λ pr ce, mk_lt_zero_pf_aux pr.1 pr.2 ce.2 ce.1) (iq, h')
| _ _ := fail "not enough args to mk_lt_zero_pf"
meta def term_of_ineq_prf (prf : expr) : tactic expr :=
do (lhs, _) ← infer_type prf >>= get_rel_sides,
return lhs
meta structure linarith_config :=
(discharger : tactic unit := `[ring])
(restrict_type : option Type := none)
(restrict_type_reflect : reflected restrict_type . apply_instance)
(exfalso : bool := tt)
meta def ineq_pf_tp (pf : expr) : tactic expr :=
do (_, z) ← infer_type pf >>= get_rel_sides,
infer_type z
meta def mk_neg_one_lt_zero_pf (tp : expr) : tactic expr :=
to_expr ``((neg_neg_of_pos zero_lt_one : -1 < (0 : %%tp)))
/--
Assumes e is a proof that t = 0. Creates a proof that -t = 0.
-/
meta def mk_neg_eq_zero_pf (e : expr) : tactic expr :=
to_expr ``(neg_eq_zero.mpr %%e)
meta def add_neg_eq_pfs : list expr → tactic (list expr)
| [] := return []
| (h::t) :=
do some (iq, tp) ← parse_into_comp_and_expr <$> infer_type h,
match iq with
| ineq.eq := do nep ← mk_neg_eq_zero_pf h, tl ← add_neg_eq_pfs t, return $ h::nep::tl
| _ := list.cons h <$> add_neg_eq_pfs t
end
/--
Takes a list of proofs of propositions of the form t {<, ≤, =} 0,
and tries to prove the goal `false`.
-/
meta def prove_false_by_linarith1 (cfg : linarith_config) : list expr → tactic unit
| [] := fail "no args to linarith"
| l@(h::t) :=
do extp ← match cfg.restrict_type with
| none := do (_, z) ← infer_type h >>= get_rel_sides, infer_type z
| some rtp := do
m ← mk_mvar,
unify `(some %%m : option Type) cfg.restrict_type_reflect,
return m
end,
hz ← mk_neg_one_lt_zero_pf extp,
l' ← if cfg.restrict_type.is_some then
l.mfilter (λ e, succeeds (ineq_pf_tp e >>= is_def_eq extp))
else return l,
l' ← add_neg_eq_pfs l',
(sum.inl contr, inputs) ← elim_all_vars.run (hz::l')
| fail "linarith failed to find a contradiction",
let coeffs := inputs.keys.map (λ k, (contr.src.flatten.ifind k)),
let pfs : list expr := inputs.keys.map (λ k, (inputs.ifind k).1),
let zip := (coeffs.zip pfs).filter (λ pr, pr.1 ≠ 0),
let (coeffs, pfs) := zip.unzip,
mls ← zip.mmap (λ pr, do e ← term_of_ineq_prf pr.2, return (mul_expr pr.1 e)),
sm ← to_expr $ add_exprs mls,
tgt ← to_expr ``(%%sm = 0),
(a, b) ← solve_aux tgt (cfg.discharger >> done),
pf ← mk_lt_zero_pf coeffs pfs,
pftp ← infer_type pf,
(_, nep, _) ← rewrite_core b pftp,
pf' ← mk_eq_mp nep pf,
mk_app `lt_irrefl [pf'] >>= exact
end prove
section normalize
open tactic
set_option eqn_compiler.max_steps 50000
meta def rearr_comp (prf : expr) : expr → tactic expr
| `(%%a ≤ 0) := return prf
| `(%%a < 0) := return prf
| `(%%a = 0) := return prf
| `(%%a ≥ 0) := to_expr ``(neg_nonpos.mpr %%prf)
| `(%%a > 0) := to_expr ``(neg_neg_of_pos %%prf) --mk_app ``neg_neg_of_pos [prf]
| `(0 ≥ %%a) := to_expr ``(show %%a ≤ 0, from %%prf)
| `(0 > %%a) := to_expr ``(show %%a < 0, from %%prf)
| `(0 = %%a) := to_expr ``(eq.symm %%prf)
| `(0 ≤ %%a) := to_expr ``(neg_nonpos.mpr %%prf)
| `(0 < %%a) := to_expr ``(neg_neg_of_pos %%prf)
| `(%%a ≤ %%b) := to_expr ``(sub_nonpos.mpr %%prf)
| `(%%a < %%b) := to_expr ``(sub_neg_of_lt %%prf) -- mk_app ``sub_neg_of_lt [prf]
| `(%%a = %%b) := to_expr ``(sub_eq_zero.mpr %%prf)
| `(%%a > %%b) := to_expr ``(sub_neg_of_lt %%prf) -- mk_app ``sub_neg_of_lt [prf]
| `(%%a ≥ %%b) := to_expr ``(sub_nonpos.mpr %%prf)
| _ := fail "couldn't rearrange comp"
meta def is_numeric : expr → option ℚ
| `(%%e1 + %%e2) := (+) <$> is_numeric e1 <*> is_numeric e2
| `(%%e1 - %%e2) := has_sub.sub <$> is_numeric e1 <*> is_numeric e2
| `(%%e1 * %%e2) := (*) <$> is_numeric e1 <*> is_numeric e2
| `(%%e1 / %%e2) := (/) <$> is_numeric e1 <*> is_numeric e2
| `(-%%e) := rat.neg <$> is_numeric e
| e := e.to_rat
inductive {u} tree (α : Type u) : Type u
| nil {} : tree
| node : α → tree → tree → tree
def tree.repr {α} [has_repr α] : tree α → string
| tree.nil := "nil"
| (tree.node a t1 t2) := "tree.node " ++ repr a ++ " (" ++ tree.repr t1 ++ ") (" ++ tree.repr t2 ++ ")"
instance {α} [has_repr α] : has_repr (tree α) := ⟨tree.repr⟩
meta def find_cancel_factor : expr → ℕ × tree ℕ
| `(%%e1 + %%e2) :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, lcm := v1.lcm v2 in
(lcm, tree.node lcm t1 t2)
| `(%%e1 - %%e2) :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, lcm := v1.lcm v2 in
(lcm, tree.node lcm t1 t2)
| `(%%e1 * %%e2) :=
let (v1, t1) := find_cancel_factor e1, (v2, t2) := find_cancel_factor e2, pd := v1*v2 in
(pd, tree.node pd t1 t2)
| `(%%e1 / %%e2) := --do q ← is_numeric e2, return q.num.nat_abs
match is_numeric e2 with
| some q := let (v1, t1) := find_cancel_factor e1, n := v1.lcm q.num.nat_abs in
(n, tree.node n t1 (tree.node q.num.nat_abs tree.nil tree.nil))
| none := (1, tree.node 1 tree.nil tree.nil)
end
| `(-%%e) := find_cancel_factor e
| _ := (1, tree.node 1 tree.nil tree.nil)
open tree
meta def mk_prod_prf : ℕ → tree ℕ → expr → tactic expr
| v (node _ lhs rhs) `(%%e1 + %%e2) :=
do v1 ← mk_prod_prf v lhs e1, v2 ← mk_prod_prf v rhs e2, mk_app ``add_subst [v1, v2]
| v (node _ lhs rhs) `(%%e1 - %%e2) :=
do v1 ← mk_prod_prf v lhs e1, v2 ← mk_prod_prf v rhs e2, mk_app ``sub_subst [v1, v2]
| v (node n lhs@(node ln _ _) rhs) `(%%e1 * %%e2) :=
do tp ← infer_type e1, v1 ← mk_prod_prf ln lhs e1, v2 ← mk_prod_prf (v/ln) rhs e2,
ln' ← tp.of_nat ln, vln' ← tp.of_nat (v/ln), v' ← tp.of_nat v,
ntp ← to_expr ``(%%ln' * %%vln' = %%v'),
(_, npf) ← solve_aux ntp `[norm_num, done],
mk_app ``mul_subst [v1, v2, npf]
| v (node n lhs rhs@(node rn _ _)) `(%%e1 / %%e2) :=
do tp ← infer_type e1, v1 ← mk_prod_prf (v/rn) lhs e1,
rn' ← tp.of_nat rn, vrn' ← tp.of_nat (v/rn), n' ← tp.of_nat n, v' ← tp.of_nat v,
ntp ← to_expr ``(%%rn' / %%e2 = 1),
(_, npf) ← solve_aux ntp `[norm_num, done],
ntp2 ← to_expr ``(%%vrn' * %%n' = %%v'),
(_, npf2) ← solve_aux ntp2 `[norm_num, done],
mk_app ``div_subst [v1, npf, npf2]
| v t `(-%%e) := do v' ← mk_prod_prf v t e, mk_app ``neg_subst [v']
| v _ e :=
do tp ← infer_type e,
v' ← tp.of_nat v,
e' ← to_expr ``(%%v' * %%e),
mk_app `eq.refl [e']
/--
e is a term with rational division. produces a natural number n and a proof that n*e = e',
where e' has no division.
-/
meta def kill_factors (e : expr) : tactic (ℕ × expr) :=
let (n, t) := find_cancel_factor e in
do e' ← mk_prod_prf n t e, return (n, e')
open expr
meta def expr_contains (n : name) : expr → bool
| (const nm _) := nm = n
| (lam _ _ _ bd) := expr_contains bd
| (pi _ _ _ bd) := expr_contains bd
| (app e1 e2) := expr_contains e1 || expr_contains e2
| _ := ff
lemma sub_into_lt {α} [ordered_semiring α] {a b : α} (he : a = b) (hl : a ≤ 0) : b ≤ 0 :=
by rwa he at hl
meta def norm_hyp_aux (h' lhs : expr) : tactic expr :=
do (v, lhs') ← kill_factors lhs,
(ih, h'') ← mk_single_comp_zero_pf v h',
(_, nep, _) ← infer_type h'' >>= rewrite_core lhs',
mk_eq_mp nep h''
meta def norm_hyp (h : expr) : tactic expr :=
do htp ← infer_type h,
h' ← rearr_comp h htp,
some (c, lhs) ← parse_into_comp_and_expr <$> infer_type h',
if expr_contains `has_div.div lhs then
norm_hyp_aux h' lhs
else return h'
meta def get_contr_lemma_name : expr → option name
| `(%%a < %%b) := return `lt_of_not_ge
| `(%%a ≤ %%b) := return `le_of_not_gt
| `(%%a = %%b) := return ``eq_of_not_lt_of_not_gt
| `(%%a ≥ %%b) := return `le_of_not_gt
| `(%%a > %%b) := return `lt_of_not_ge
| _ := none
-- assumes the input t is of type ℕ. Produces t' of type ℤ such that ↑t = t' and a proof of equality
meta def cast_expr (e : expr) : tactic (expr × expr) :=
do s ← [`int.coe_nat_add, `int.coe_nat_mul, `int.coe_nat_zero, `int.coe_nat_one,
``int.coe_nat_bit0, ``int.coe_nat_bit1].mfoldl simp_lemmas.add_simp simp_lemmas.mk,
ce ← to_expr ``(↑%%e : ℤ),
simplify s [] ce {fail_if_unchanged := ff}
meta def is_nat_int_coe : expr → option expr
| `((↑(%%n : ℕ) : ℤ)) := some n
| _ := none
meta def mk_coe_nat_nonneg_prf (e : expr) : tactic expr :=
mk_app `int.coe_nat_nonneg [e]
meta def get_nat_comps : expr → list expr
| `(%%a + %%b) := (get_nat_comps a).append (get_nat_comps b)
| `(%%a * %%b) := (get_nat_comps a).append (get_nat_comps b)
| e := match is_nat_int_coe e with
| some e' := [e']
| none := []
end
meta def mk_coe_nat_nonneg_prfs (e : expr) : tactic (list expr) :=
(get_nat_comps e).mmap mk_coe_nat_nonneg_prf
meta def mk_cast_eq_and_nonneg_prfs (pf a b : expr) (ln : name) : tactic (list expr) :=
do (a', prfa) ← cast_expr a,
(b', prfb) ← cast_expr b,
la ← mk_coe_nat_nonneg_prfs a',
lb ← mk_coe_nat_nonneg_prfs b',
pf' ← mk_app ln [pf, prfa, prfb],
return $ pf'::(la.append lb)
meta def mk_int_pfs_of_nat_pf (pf : expr) : tactic (list expr) :=
do tp ← infer_type pf,
match tp with
| `(%%a = %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_eq_subst
| `(%%a ≤ %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_le_subst
| `(%%a < %%b) := mk_cast_eq_and_nonneg_prfs pf a b ``nat_lt_subst
| `(%%a ≥ %%b) := mk_cast_eq_and_nonneg_prfs pf b a ``nat_le_subst
| `(%%a > %%b) := mk_cast_eq_and_nonneg_prfs pf b a ``nat_lt_subst
| _ := fail "mk_coe_comp_prf failed: proof is not an inequality"
end
meta def replace_nat_pfs : list expr → tactic (list expr)
| [] := return []
| (h::t) :=
(do (a, _) ← infer_type h >>= get_rel_sides,
infer_type a >>= unify `(ℕ),
ls ← mk_int_pfs_of_nat_pf h,
list.append ls <$> replace_nat_pfs t) <|> list.cons h <$> replace_nat_pfs t
/--
Takes a list of proofs of propositions.
Filters out the proofs of linear (in)equalities,
and tries to use them to prove `false`.
-/
meta def prove_false_by_linarith (cfg : linarith_config) (l : list expr) : tactic unit :=
do l' ← replace_nat_pfs l,
ls ← l'.mmap (λ h, (do s ← norm_hyp h, return (some s)) <|> return none),
prove_false_by_linarith1 cfg ls.reduce_option
end normalize
end linarith
section
open tactic linarith
open lean lean.parser interactive tactic interactive.types
local postfix `?`:9001 := optional
local postfix *:9001 := many
meta def linarith.interactive_aux (cfg : linarith_config) :
parse ident* → (parse (tk "using" *> pexpr_list)?) → tactic unit
| l (some pe) := pe.mmap (λ p, i_to_expr p >>= note_anon) >> linarith.interactive_aux l none
| [] none :=
do t ← target,
if t = `(false) then local_context >>= prove_false_by_linarith cfg
else match get_contr_lemma_name t with
| some nm := seq (applyc nm) (intro1 >> linarith.interactive_aux [] none)
| none := if cfg.exfalso then exfalso >> linarith.interactive_aux [] none
else fail "linarith failed: target type is not an inequality."
end
| ls none := (ls.mmap get_local) >>= prove_false_by_linarith cfg
/--
Tries to prove a goal of `false` by linear arithmetic on hypotheses.
If the goal is a linear (in)equality, tries to prove it by contradiction.
If the goal is not `false` or an inequality, applies `exfalso` and tries linarith on the
hypotheses.
`linarith` will use all relevant hypotheses in the local context.
`linarith h1 h2 h3` will only use hypotheses h1, h2, h3.
`linarith using [t1, t2, t3]` will add proof terms t1, t2, t3 to the local context.
Config options:
`linarith {exfalso := ff}` will fail on a goal that is neither an inequality nor `false`
`linarith {restrict_type := T}` will run only on hypotheses that are inequalities over `T`
`linarith {discharger := tac}` will use `tac` instead of `ring` for normalization.
Options: `ring2`, `ring SOP`, `simp`
-/
meta def tactic.interactive.linarith (ids : parse (many ident))
(using_hyps : parse (tk "using" *> pexpr_list)?) (cfg : linarith_config := {}) : tactic unit :=
linarith.interactive_aux cfg ids using_hyps
end |
7b6f4c12fbddc5774131bf5b2e49e4609bee8d71 | a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940 | /tests/lean/interactive/goTo.lean | ae5e6244ad19cc7b25b42fa962e29cbbe9540139 | [
"Apache-2.0"
] | permissive | WojciechKarpiel/lean4 | 7f89706b8e3c1f942b83a2c91a3a00b05da0e65b | f6e1314fa08293dea66a329e05b6c196a0189163 | refs/heads/master | 1,686,633,402,214 | 1,625,821,189,000 | 1,625,821,258,000 | 384,640,886 | 0 | 0 | Apache-2.0 | 1,625,903,617,000 | 1,625,903,026,000 | null | UTF-8 | Lean | false | false | 792 | lean | import Lean.Elab
structure Bar where
structure Foo where
foo₁ : Nat
foo₂ : Nat
bar : Bar
def mkFoo₁ : Foo := {
--v textDocument/definition
foo₁ := 1
--v textDocument/declaration
foo₂ := 2
--v textDocument/typeDefinition
bar := ⟨⟩
}
inductive HandWrittenStruct where
| mk (n : Nat)
def HandWrittenStruct.n := fun | mk n => n
--v textDocument/definition
def hws : HandWrittenStruct := {
--v textDocument/definition
n := 3
}
--v textDocument/declaration
def mkFoo₂ := mkFoo₁
syntax (name := elabTest) "test" : term
@[termElab elabTest] def elabElabTest : Lean.Elab.Term.TermElab := fun _ _ => do
let stx ← `(2)
Lean.Elab.Term.elabTerm stx none
--v textDocument/declaration
#check test
--^ textDocument/definition |
42af93a54a008c72671a5814d1896b721726834d | d1a52c3f208fa42c41df8278c3d280f075eb020c | /src/Lean/Compiler/IR/RC.lean | a715be72d56aa2f45f44507e0301f7a7a5deda8f | [
"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,792 | lean | /-
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
import Lean.Runtime
import Lean.Compiler.IR.CompilerM
import Lean.Compiler.IR.LiveVars
namespace Lean.IR.ExplicitRC
/- Insert explicit RC instructions. So, it assumes the input code does not contain `inc` nor `dec` instructions.
This transformation is applied before lower level optimizations
that introduce the instructions `release` and `set`
-/
structure VarInfo where
ref : Bool := true -- true if the variable may be a reference (aka pointer) at runtime
persistent : Bool := false -- true if the variable is statically known to be marked a Persistent at runtime
consume : Bool := false -- true if the variable RC must be "consumed"
deriving Inhabited
abbrev VarMap := Std.RBMap VarId VarInfo (fun x y => compare x.idx y.idx)
structure Context where
env : Environment
decls : Array Decl
varMap : VarMap := {}
jpLiveVarMap : JPLiveVarMap := {} -- map: join point => live variables
localCtx : LocalContext := {} -- we use it to store the join point declarations
def getDecl (ctx : Context) (fid : FunId) : Decl :=
match findEnvDecl' ctx.env fid ctx.decls with
| some decl => decl
| none => unreachable!
def getVarInfo (ctx : Context) (x : VarId) : VarInfo :=
match ctx.varMap.find? x with
| some info => info
| none => unreachable!
def getJPParams (ctx : Context) (j : JoinPointId) : Array Param :=
match ctx.localCtx.getJPParams j with
| some ps => ps
| none => unreachable!
def getJPLiveVars (ctx : Context) (j : JoinPointId) : LiveVarSet :=
match ctx.jpLiveVarMap.find? j with
| some s => s
| none => {}
def mustConsume (ctx : Context) (x : VarId) : Bool :=
let info := getVarInfo ctx x
info.ref && info.consume
@[inline] def addInc (ctx : Context) (x : VarId) (b : FnBody) (n := 1) : FnBody :=
let info := getVarInfo ctx x
if n == 0 then b else FnBody.inc x n true info.persistent b
@[inline] def addDec (ctx : Context) (x : VarId) (b : FnBody) : FnBody :=
let info := getVarInfo ctx x
FnBody.dec x 1 true info.persistent b
private def updateRefUsingCtorInfo (ctx : Context) (x : VarId) (c : CtorInfo) : Context :=
if c.isRef then
ctx
else
let m := ctx.varMap
{ ctx with
varMap := match m.find? x with
| some info => m.insert x { info with ref := false } -- I really want a Lenses library + notation
| none => m }
private def addDecForAlt (ctx : Context) (caseLiveVars altLiveVars : LiveVarSet) (b : FnBody) : FnBody :=
caseLiveVars.fold (init := b) fun b x =>
if !altLiveVars.contains x && mustConsume ctx x then addDec ctx x b else b
/- `isFirstOcc xs x i = true` if `xs[i]` is the first occurrence of `xs[i]` in `xs` -/
private def isFirstOcc (xs : Array Arg) (i : Nat) : Bool :=
let x := xs[i]
i.all fun j => xs[j] != x
/- Return true if `x` also occurs in `ys` in a position that is not consumed.
That is, it is also passed as a borrow reference. -/
@[specialize]
private def isBorrowParamAux (x : VarId) (ys : Array Arg) (consumeParamPred : Nat → Bool) : Bool :=
ys.size.any fun i =>
let y := ys[i]
match y with
| Arg.irrelevant => false
| Arg.var y => x == y && !consumeParamPred i
private def isBorrowParam (x : VarId) (ys : Array Arg) (ps : Array Param) : Bool :=
isBorrowParamAux x ys fun i => not ps[i].borrow
/-
Return `n`, the number of times `x` is consumed.
- `ys` is a sequence of instruction parameters where we search for `x`.
- `consumeParamPred i = true` if parameter `i` is consumed.
-/
@[specialize]
private def getNumConsumptions (x : VarId) (ys : Array Arg) (consumeParamPred : Nat → Bool) : Nat :=
ys.size.fold (init := 0) fun i n =>
let y := ys[i]
match y with
| Arg.irrelevant => n
| Arg.var y => if x == y && consumeParamPred i then n+1 else n
@[specialize]
private def addIncBeforeAux (ctx : Context) (xs : Array Arg) (consumeParamPred : Nat → Bool) (b : FnBody) (liveVarsAfter : LiveVarSet) : FnBody :=
xs.size.fold (init := b) fun i b =>
let x := xs[i]
match x with
| Arg.irrelevant => b
| Arg.var x =>
let info := getVarInfo ctx x
if !info.ref || !isFirstOcc xs i then b
else
let numConsuptions := getNumConsumptions x xs consumeParamPred -- number of times the argument is
let numIncs :=
if !info.consume || -- `x` is not a variable that must be consumed by the current procedure
liveVarsAfter.contains x || -- `x` is live after executing instruction
isBorrowParamAux x xs consumeParamPred -- `x` is used in a position that is passed as a borrow reference
then numConsuptions
else numConsuptions - 1
-- dbgTrace ("addInc " ++ toString x ++ " nconsumptions: " ++ toString numConsuptions ++ " incs: " ++ toString numIncs
-- ++ " consume: " ++ toString info.consume ++ " live: " ++ toString (liveVarsAfter.contains x)
-- ++ " borrowParam : " ++ toString (isBorrowParamAux x xs consumeParamPred)) $ fun _ =>
addInc ctx x b numIncs
private def addIncBefore (ctx : Context) (xs : Array Arg) (ps : Array Param) (b : FnBody) (liveVarsAfter : LiveVarSet) : FnBody :=
addIncBeforeAux ctx xs (fun i => not ps[i].borrow) b liveVarsAfter
/- See `addIncBeforeAux`/`addIncBefore` for the procedure that inserts `inc` operations before an application. -/
private def addDecAfterFullApp (ctx : Context) (xs : Array Arg) (ps : Array Param) (b : FnBody) (bLiveVars : LiveVarSet) : FnBody :=
xs.size.fold (init := b) fun i b =>
match xs[i] with
| Arg.irrelevant => b
| Arg.var x =>
/- We must add a `dec` if `x` must be consumed, it is alive after the application,
and it has been borrowed by the application.
Remark: `x` may occur multiple times in the application (e.g., `f x y x`).
This is why we check whether it is the first occurrence. -/
if mustConsume ctx x && isFirstOcc xs i && isBorrowParam x xs ps && !bLiveVars.contains x then
addDec ctx x b
else b
private def addIncBeforeConsumeAll (ctx : Context) (xs : Array Arg) (b : FnBody) (liveVarsAfter : LiveVarSet) : FnBody :=
addIncBeforeAux ctx xs (fun i => true) b liveVarsAfter
/- Add `dec` instructions for parameters that are references, are not alive in `b`, and are not borrow.
That is, we must make sure these parameters are consumed. -/
private def addDecForDeadParams (ctx : Context) (ps : Array Param) (b : FnBody) (bLiveVars : LiveVarSet) : FnBody :=
ps.foldl (init := b) fun b p =>
if !p.borrow && p.ty.isObj && !bLiveVars.contains p.x then addDec ctx p.x b else b
private def isPersistent : Expr → Bool
| Expr.fap c xs => xs.isEmpty -- all global constants are persistent objects
| _ => false
/- We do not need to consume the projection of a variable that is not consumed -/
private def consumeExpr (m : VarMap) : Expr → Bool
| Expr.proj i x => match m.find? x with
| some info => info.consume
| none => true
| other => true
/- Return true iff `v` at runtime is a scalar value stored in a tagged pointer.
We do not need RC operations for this kind of value. -/
private def isScalarBoxedInTaggedPtr (v : Expr) : Bool :=
match v with
| Expr.ctor c ys => c.size == 0 && c.ssize == 0 && c.usize == 0
| Expr.lit (LitVal.num n) => n ≤ maxSmallNat
| _ => false
private def updateVarInfo (ctx : Context) (x : VarId) (t : IRType) (v : Expr) : Context :=
{ ctx with
varMap := ctx.varMap.insert x {
ref := t.isObj && !isScalarBoxedInTaggedPtr v,
persistent := isPersistent v,
consume := consumeExpr ctx.varMap v
}
}
private def addDecIfNeeded (ctx : Context) (x : VarId) (b : FnBody) (bLiveVars : LiveVarSet) : FnBody :=
if mustConsume ctx x && !bLiveVars.contains x then addDec ctx x b else b
private def processVDecl (ctx : Context) (z : VarId) (t : IRType) (v : Expr) (b : FnBody) (bLiveVars : LiveVarSet) : FnBody × LiveVarSet :=
let b := match v with
| (Expr.ctor _ ys) => addIncBeforeConsumeAll ctx ys (FnBody.vdecl z t v b) bLiveVars
| (Expr.reuse _ _ _ ys) => addIncBeforeConsumeAll ctx ys (FnBody.vdecl z t v b) bLiveVars
| (Expr.proj _ x) =>
let b := addDecIfNeeded ctx x b bLiveVars
let b := if (getVarInfo ctx x).consume then addInc ctx z b else b
(FnBody.vdecl z t v b)
| (Expr.uproj _ x) => FnBody.vdecl z t v (addDecIfNeeded ctx x b bLiveVars)
| (Expr.sproj _ _ x) => FnBody.vdecl z t v (addDecIfNeeded ctx x b bLiveVars)
| (Expr.fap f ys) =>
-- dbgTrace ("processVDecl " ++ toString v) $ fun _ =>
let ps := (getDecl ctx f).params
let b := addDecAfterFullApp ctx ys ps b bLiveVars
let b := FnBody.vdecl z t v b
addIncBefore ctx ys ps b bLiveVars
| (Expr.pap _ ys) => addIncBeforeConsumeAll ctx ys (FnBody.vdecl z t v b) bLiveVars
| (Expr.ap x ys) =>
let ysx := ys.push (Arg.var x) -- TODO: avoid temporary array allocation
addIncBeforeConsumeAll ctx ysx (FnBody.vdecl z t v b) bLiveVars
| (Expr.unbox x) => FnBody.vdecl z t v (addDecIfNeeded ctx x b bLiveVars)
| other => FnBody.vdecl z t v b -- Expr.reset, Expr.box, Expr.lit are handled here
let liveVars := updateLiveVars v bLiveVars
let liveVars := liveVars.erase z
(b, liveVars)
def updateVarInfoWithParams (ctx : Context) (ps : Array Param) : Context :=
let m := ps.foldl (init := ctx.varMap) fun m p =>
m.insert p.x { ref := p.ty.isObj, consume := !p.borrow }
{ ctx with varMap := m }
partial def visitFnBody : FnBody → Context → (FnBody × LiveVarSet)
| FnBody.vdecl x t v b, ctx =>
let ctx := updateVarInfo ctx x t v
let (b, bLiveVars) := visitFnBody b ctx
processVDecl ctx x t v b bLiveVars
| FnBody.jdecl j xs v b, ctx =>
let ctxAtV := updateVarInfoWithParams ctx xs
let (v, vLiveVars) := visitFnBody v ctxAtV
let v := addDecForDeadParams ctxAtV xs v vLiveVars
let ctx := { ctx with
localCtx := ctx.localCtx.addJP j xs v
jpLiveVarMap := updateJPLiveVarMap j xs v ctx.jpLiveVarMap
}
let (b, bLiveVars) := visitFnBody b ctx
(FnBody.jdecl j xs v b, bLiveVars)
| FnBody.uset x i y b, ctx =>
let (b, s) := visitFnBody b ctx
-- We don't need to insert `y` since we only need to track live variables that are references at runtime
let s := s.insert x
(FnBody.uset x i y b, s)
| FnBody.sset x i o y t b, ctx =>
let (b, s) := visitFnBody b ctx
-- We don't need to insert `y` since we only need to track live variables that are references at runtime
let s := s.insert x
(FnBody.sset x i o y t b, s)
| FnBody.mdata m b, ctx =>
let (b, s) := visitFnBody b ctx
(FnBody.mdata m b, s)
| b@(FnBody.case tid x xType alts), ctx =>
let caseLiveVars := collectLiveVars b ctx.jpLiveVarMap
let alts := alts.map $ fun alt => match alt with
| Alt.ctor c b =>
let ctx := updateRefUsingCtorInfo ctx x c
let (b, altLiveVars) := visitFnBody b ctx
let b := addDecForAlt ctx caseLiveVars altLiveVars b
Alt.ctor c b
| Alt.default b =>
let (b, altLiveVars) := visitFnBody b ctx
let b := addDecForAlt ctx caseLiveVars altLiveVars b
Alt.default b
(FnBody.case tid x xType alts, caseLiveVars)
| b@(FnBody.ret x), ctx =>
match x with
| Arg.var x =>
let info := getVarInfo ctx x
if info.ref && !info.consume then (addInc ctx x b, mkLiveVarSet x) else (b, mkLiveVarSet x)
| _ => (b, {})
| b@(FnBody.jmp j xs), ctx =>
let jLiveVars := getJPLiveVars ctx j
let ps := getJPParams ctx j
let b := addIncBefore ctx xs ps b jLiveVars
let bLiveVars := collectLiveVars b ctx.jpLiveVarMap
(b, bLiveVars)
| FnBody.unreachable, _ => (FnBody.unreachable, {})
| other, ctx => (other, {}) -- unreachable if well-formed
partial def visitDecl (env : Environment) (decls : Array Decl) (d : Decl) : Decl :=
match d with
| Decl.fdecl (xs := xs) (body := b) .. =>
let ctx : Context := { env := env, decls := decls }
let ctx := updateVarInfoWithParams ctx xs
let (b, bLiveVars) := visitFnBody b ctx
let b := addDecForDeadParams ctx xs b bLiveVars
d.updateBody! b
| other => other
end ExplicitRC
def explicitRC (decls : Array Decl) : CompilerM (Array Decl) := do
let env ← getEnv
pure $ decls.map (ExplicitRC.visitDecl env decls)
end Lean.IR
|
39893433dc6be41126d4e92be62026d406b52394 | a6b711a4e8db20755026231f7ed529a9014b2b6d | /ZZ_IGNORE/S17/class/Exam2/Exam2.lean | 84be3a9b5dd32ca97feb768c8b78a0aa76c432ed | [] | no_license | chaseboettner/cs-dm-1 | b67d4a7e86f56bce59d2af115503769749d423b2 | 80b35f2957ffaa45b8b7a4479a3570a2d6eb4db0 | refs/heads/master | 1,585,367,603,488 | 1,536,235,675,000 | 1,536,235,675,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 3,094 | lean | /-
This is Exam2 for CS2101, Spring 2018 (Sullivan).
COLLABORATION POLICY: NO COLLABORATION ALLOWED.
This is an individual evaluation. You are not
allowed to communicate with *anyone* about this
exam, except the instructor, by any means, until
you have completed and submitted the exam and
anyone you're communicating with has as well. Do
not communicate about this exam with anyone not
in class until all students have completed and
submitted the exam. Cheating on this exam will
result in an Honor Committee referral. Don't do
it.
You MAY use all class materials, your own notes,
and material you find through on-line searches,
or in books, or other resources, on this exam.
Note: This exam document is written using Lean,
to take advantage of Lean's support for logical
notation. It does not test any concepts that we
have covered using Lean.
The problems add up to 100 points.
-/
/- ***********************************************
Problem #1 [30 pts]. One of the basic connectives
in propositional logic is called *equivalence*. If
P and Q are propositions, the proposition that P is
equivalent to Q is written as P ↔ Q. It can be read
as "P if and only if Q". Mathematicians shorten it
to "P iff Q."
What it means is nothing other than P → Q ∧ Q → P.
That is, P → Q and also Q → P.
In Dafny (not Lean!), the equivalence operator (a
binary Boolean operator) is written as <==>.
Sadly, our initial implementation of propositional
logic doesn't support this operator. You job is to
extend the implementation so that it does.
(A) Extend our syntax for propositional logic with
a new constructor, pEquiv, taking two propositions
as arguments.
(B) Extend our semantics for propositional logic
with a rule for evaluating propositions of this
form under a given interpretation.
(C) Just as the other logical connectives have
introduction and elimination rules in the natural
deduction system of logical reasoning, so does
the equivalence connective.
The introduction rules states that in a context
in which P → Q is true and Q → P is true then it
is valid to deduce P ↔ Q. Your job is to validate
this inference rule using the method of truth
tables. Extend consequence_test.dfy by adding a
check of this rule and confirming based on the
output of the program that it's a valid rule.
Similarly, ↔ has two elimination rules. From a
context in which P ↔ Q is true, you can deduce
by the iff left elimination rule that P → Q is
true, and from the right elimination rule that
Q → P is true. Represent and validate these two
rules as well by extending consequence_test.dfy
accordingly.
***********************************************
Problem #2 [70 points] Open and complete the
work required in the file Exam2.dfy.
************************************************
Submit the files syntax.dfy, evaluation.dfy,
consequence_test.dfy, any other files that you
had to change, and Exam2.dfy on Collab.
The exam is due before 9:30AM next Tuesday. Do
NOT submit it late! Late submissions, if taken
at all, will have 15 points off for being late.
-/ |
a95095d4a19f03699a0dc55efd89fee3b08bfad4 | 35677d2df3f081738fa6b08138e03ee36bc33cad | /src/tactic/rewrite.lean | a178bfbb34b46b80c23b5ad62a03ff5457d6394f | [
"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 | 7,665 | lean | /-
Copyright (c) 2018 Simon Hudon. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Simon Hudon
-/
import data.dlist tactic.core
namespace tactic
open expr list
meta def match_fn (fn : expr) : expr → tactic (expr × expr)
| (app (app fn' e₀) e₁) := unify fn fn' $> (e₀, e₁)
| _ := failed
meta def fill_args : expr → tactic (expr × list expr)
| (pi n bi d b) :=
do v ← mk_meta_var d,
(r, vs) ← fill_args (b.instantiate_var v),
return (r, v::vs)
| e := return (e, [])
meta def mk_assoc_pattern' (fn : expr) : expr → tactic (dlist expr)
| e :=
(do (e₀, e₁) ← match_fn fn e,
(++) <$> mk_assoc_pattern' e₀ <*> mk_assoc_pattern' e₁) <|>
pure (dlist.singleton e)
meta def mk_assoc_pattern (fn e : expr) : tactic (list expr) :=
dlist.to_list <$> mk_assoc_pattern' fn e
meta def mk_assoc (fn : expr) : list expr → tactic expr
| [] := failed
| [x] := pure x
| (x₀ :: x₁ :: xs) := mk_assoc (fn x₀ x₁ :: xs)
meta def chain_eq_trans : list expr → tactic expr
| [] := to_expr ``(rfl)
| [e] := pure e
| (e :: es) := chain_eq_trans es >>= mk_eq_trans e
meta def unify_prefix : list expr → list expr → tactic unit
| [] _ := pure ()
| _ [] := failed
| (x :: xs) (y :: ys) :=
unify x y >> unify_prefix xs ys
meta def match_assoc_pattern' (p : list expr) : list expr → tactic (list expr × list expr) | es :=
unify_prefix p es $> ([], es.drop p.length) <|>
match es with
| [] := failed
| (x :: xs) := prod.map (cons x) id <$> match_assoc_pattern' xs
end
meta def match_assoc_pattern (fn p e : expr) : tactic (list expr × list expr) :=
do p' ← mk_assoc_pattern fn p,
e' ← mk_assoc_pattern fn e,
match_assoc_pattern' p' e'
meta def mk_eq_proof (fn : expr) (e₀ e₁ : list expr) (p : expr) : tactic (expr × expr × expr) :=
do (l, r) ← infer_type p >>= match_eq,
if e₀.empty ∧ e₁.empty then pure (l, r, p)
else do
l' ← mk_assoc fn (e₀ ++ [l] ++ e₁),
r' ← mk_assoc fn (e₀ ++ [r] ++ e₁),
t ← infer_type l',
v ← mk_local_def `x t,
e ← mk_assoc fn (e₀ ++ [v] ++ e₁),
p ← mk_congr_arg (e.lambdas [v]) p,
p' ← mk_id_eq l' r' p,
return (l', r', p')
meta def assoc_root (fn assoc : expr) : expr → tactic (expr × expr) | e :=
(do (e₀, e₁) ← match_fn fn e,
(ea, eb) ← match_fn fn e₁,
let e' := fn (fn e₀ ea) eb,
p' ← mk_eq_symm (assoc e₀ ea eb),
(e'', p'') ← assoc_root e',
prod.mk e'' <$> mk_eq_trans p' p'') <|>
prod.mk e <$> mk_eq_refl e
meta def assoc_refl' (fn assoc : expr) : expr → expr → tactic expr
| l r := (is_def_eq l r >> mk_eq_refl l) <|> do
(l', l_p) ← assoc_root fn assoc l <|> fail "A",
(el₀, el₁) ← match_fn fn l' <|> fail "B",
(r', r_p) ← assoc_root fn assoc r <|> fail "C",
(er₀, er₁) ← match_fn fn r' <|> fail "D",
p₀ ← assoc_refl' el₀ er₀,
p₁ ← is_def_eq el₁ er₁ >> mk_eq_refl el₁,
f_eq ← mk_congr_arg fn p₀ <|> fail "G",
p' ← mk_congr f_eq p₁ <|> fail "H",
r_p' ← mk_eq_symm r_p,
chain_eq_trans [l_p, p', r_p']
meta def assoc_refl (fn : expr) : tactic unit :=
do (l, r) ← target >>= match_eq,
assoc ← mk_mapp ``is_associative.assoc [none, fn, none]
<|> fail format!"{fn} is not associative",
assoc_refl' fn assoc l r >>= tactic.exact
meta def flatten (fn assoc e : expr) : tactic (expr × expr) :=
do ls ← mk_assoc_pattern fn e,
e' ← mk_assoc fn ls,
p ← assoc_refl' fn assoc e e',
return (e', p)
meta def assoc_rewrite_intl (assoc h e : expr) : tactic (expr × expr) :=
do t ← infer_type h,
(lhs, rhs) ← match_eq t,
let fn := lhs.app_fn.app_fn,
(l, r) ← match_assoc_pattern fn lhs e,
(lhs', rhs', h') ← mk_eq_proof fn l r h,
e_p ← assoc_refl' fn assoc e lhs',
(rhs'', rhs_p) ← flatten fn assoc rhs',
final_p ← chain_eq_trans [e_p, h', rhs_p],
return (rhs'', final_p)
-- TODO(Simon): visit expressions built of `fn` nested inside other such expressions:
-- e.g.: x + f (a + b + c) + y should generate two rewrite candidates
meta def enum_assoc_subexpr' (fn : expr) : expr → tactic (dlist expr)
| e :=
dlist.singleton e <$ (match_fn fn e >> guard (¬ e.has_var)) <|>
expr.mfoldl (λ es e', (++ es) <$> enum_assoc_subexpr' e') dlist.empty e
meta def enum_assoc_subexpr (fn e : expr) : tactic (list expr) :=
dlist.to_list <$> enum_assoc_subexpr' fn e
meta def mk_assoc_instance (fn : expr) : tactic expr :=
do t ← mk_mapp ``is_associative [none, fn],
inst ← prod.snd <$> solve_aux t assumption <|>
(mk_instance t >>= assertv `_inst t) <|>
fail format!"{fn} is not associative",
mk_mapp ``is_associative.assoc [none, fn, inst]
meta def assoc_rewrite (h e : expr) (opt_assoc : option expr := none) :
tactic (expr × expr × list expr) :=
do (t, vs) ← infer_type h >>= fill_args,
(lhs, rhs) ← match_eq t,
let fn := lhs.app_fn.app_fn,
es ← enum_assoc_subexpr fn e,
assoc ← match opt_assoc with
| none := mk_assoc_instance fn
| (some assoc) := pure assoc
end,
(_, p) ← mfirst (assoc_rewrite_intl assoc $ h.mk_app vs) es,
(e', p', _) ← tactic.rewrite p e,
pure (e', p', vs)
meta def assoc_rewrite_target (h : expr) (opt_assoc : option expr := none) :
tactic unit :=
do tgt ← target,
(tgt', p, _) ← assoc_rewrite h tgt opt_assoc,
replace_target tgt' p
meta def assoc_rewrite_hyp (h hyp : expr) (opt_assoc : option expr := none) :
tactic expr :=
do tgt ← infer_type hyp,
(tgt', p, _) ← assoc_rewrite h tgt opt_assoc,
replace_hyp hyp tgt' p
namespace interactive
open lean.parser interactive interactive.types tactic
private meta def assoc_rw_goal (rs : list rw_rule) : tactic unit :=
rs.mmap' $ λ r, do
save_info r.pos,
eq_lemmas ← get_rule_eqn_lemmas r,
orelse'
(do e ← to_expr' r.rule, assoc_rewrite_target e)
(eq_lemmas.mfirst $ λ n, do e ← mk_const n, assoc_rewrite_target e)
(eq_lemmas.empty)
private meta def uses_hyp (e : expr) (h : expr) : bool :=
e.fold ff $ λ t _ r, r || (t = h)
private meta def assoc_rw_hyp : list rw_rule → expr → tactic unit
| [] hyp := skip
| (r::rs) hyp := do
save_info r.pos,
eq_lemmas ← get_rule_eqn_lemmas r,
orelse'
(do e ← to_expr' r.rule, when (¬ uses_hyp e hyp) $ assoc_rewrite_hyp e hyp >>= assoc_rw_hyp rs)
(eq_lemmas.mfirst $ λ n, do e ← mk_const n, assoc_rewrite_hyp e hyp >>= assoc_rw_hyp rs)
(eq_lemmas.empty)
private meta def assoc_rw_core (rs : parse rw_rules) (loca : parse location) : tactic unit :=
match loca with
| loc.wildcard := loca.try_apply (assoc_rw_hyp rs.rules) (assoc_rw_goal rs.rules)
| _ := loca.apply (assoc_rw_hyp rs.rules) (assoc_rw_goal rs.rules)
end >> try reflexivity
>> try (returnopt rs.end_pos >>= save_info)
/--
`assoc_rewrite [h₀,← h₁] at ⊢ h₂` behaves like `rewrite [h₀,← h₁] at ⊢ h₂`
with the exception that associativity is used implicitly to make rewriting
possible.
-/
meta def assoc_rewrite (q : parse rw_rules) (l : parse location) : tactic unit :=
propagate_tags (assoc_rw_core q l)
/-- synonym for `assoc_rewrite` -/
meta def assoc_rw (q : parse rw_rules) (l : parse location) : tactic unit :=
assoc_rewrite q l
add_tactic_doc
{ name := "assoc_rewrite",
category := doc_category.tactic,
decl_names := [`tactic.interactive.assoc_rewrite, `tactic.interactive.assoc_rw],
tags := ["rewrite"],
inherit_description_from := `tactic.interactive.assoc_rewrite }
end interactive
end tactic
|
46beb65d855754ae09a4f98c89954b9244bd0989 | 856e2e1615a12f95b551ed48fa5b03b245abba44 | /src/computability/encode_alphabet.lean | 1f82d76a91651d6f8fc833600bba31dc9d5f75a4 | [
"Apache-2.0"
] | permissive | pimsp/mathlib | 8b77e1ccfab21703ba8fbe65988c7de7765aa0e5 | 913318ca9d6979686996e8d9b5ebf7e74aae1c63 | refs/heads/master | 1,669,812,465,182 | 1,597,133,610,000 | 1,597,133,610,000 | 281,890,685 | 1 | 0 | null | 1,595,491,577,000 | 1,595,491,576,000 | null | UTF-8 | Lean | false | false | 5,563 | lean | import data.fintype.basic
import data.num.lemmas
import tactic
namespace encoding
structure encoding (α : Type) :=
(Γ : Type)
(encode : α → list Γ)
(decode : list Γ → option α)
(encodek : decode ∘ encode = option.some)
structure fin_encoding (α : Type) extends encoding α :=
(Γ_fin : fintype Γ)
--(encode_injective : function.injective encode)
#check fin_encoding
@[derive [inhabited,decidable_eq]]
inductive Γ₀₁ | bit0 | bit1
#check list.mem
def Γ₀₁_fin : fintype Γ₀₁ :=
{ elems := {Γ₀₁.bit0, Γ₀₁.bit1},
complete := λ x, begin cases x, left,trivial,right,left,trivial, end }
@[derive [decidable_eq]]
inductive Γ' | blank | bit0 | bit1 | bra | ket | comma
def Γ'_fin : fintype Γ' :=
{ elems := {Γ'.blank, Γ'.bit0, Γ'.bit1, Γ'.bra, Γ'.ket, Γ'.comma},
complete := λ x, begin cases x, left,trivial,right,left,trivial,right,right,left,trivial,right,right,right,left,trivial,right,right,right,right,left,trivial,right,right,right,right,right,left,trivial end }
def inclusion_Γ₀₁_Γ' : Γ₀₁ → Γ'
| Γ₀₁.bit0 := Γ'.bit0
| Γ₀₁.bit1 := Γ'.bit1
def section_Γ'_Γ₀₁ : Γ' → Γ₀₁
| Γ'.bit0 := Γ₀₁.bit0
| Γ'.bit1 := Γ₀₁.bit1
| _ := arbitrary Γ₀₁
lemma left_inverse_section_inclusion : function.left_inverse section_Γ'_Γ₀₁ inclusion_Γ₀₁_Γ' := begin intros x, cases x; trivial, end
def inclusion_Γ₀₁_Γ'_injective : function.injective inclusion_Γ₀₁_Γ' :=
begin tidy, cases a₁; cases a₂; simp; finish end
instance inhabited_Γ' : inhabited Γ' := ⟨Γ'.blank⟩
def encode_pos_num : pos_num → list Γ₀₁
| pos_num.one := [Γ₀₁.bit1]
| (pos_num.bit0 n) := Γ₀₁.bit0 :: encode_pos_num n
| (pos_num.bit1 n) := Γ₀₁.bit1 :: encode_pos_num n
def encode_num : num → list Γ₀₁
| num.zero := []
| (num.pos n) := encode_pos_num n
def encode_nat (n : ℕ) : list Γ₀₁ := encode_num n
def decode_pos_num : list Γ₀₁ → pos_num
| [Γ₀₁.bit1] := pos_num.one
| (Γ₀₁.bit0 :: l) := (pos_num.bit0 (decode_pos_num l))
| (Γ₀₁.bit1 :: l) := (pos_num.bit1 (decode_pos_num l))
| _ := pos_num.one
def decode_num : list Γ₀₁ → num
| [] := num.zero
| l := decode_pos_num l
namespace encodable
instance encodable_num : encodable num :=
{ encode := λ n, n,
decode := λ n, some n,
encodek := begin
intros a,
simp,
end}
end encodable
def decode_nat : list Γ₀₁ → nat := λ l, encodable.encode(decode_num(l))
def encode_pos_num_nonempty (n : pos_num) : (encode_pos_num n) ≠ [] :=
begin
cases n with m,
exact list.cons_ne_nil Γ₀₁.bit1 list.nil,
exact list.cons_ne_nil Γ₀₁.bit1 (encode_pos_num m),
exact list.cons_ne_nil Γ₀₁.bit0 (encode_pos_num n),
end
def encodek_pos_num : ∀ n, (decode_pos_num(encode_pos_num n) ) = n := begin
intros n,
induction n with m hm m hm,
trivial,
change decode_pos_num ((Γ₀₁.bit1) :: encode_pos_num m) = m.bit1,
have h := encode_pos_num_nonempty m,
calc decode_pos_num (Γ₀₁.bit1 :: encode_pos_num m)
= pos_num.bit1 (decode_pos_num (encode_pos_num m)) : begin cases (encode_pos_num m), exfalso, trivial,trivial, end
... = m.bit1 : by rw hm,
calc decode_pos_num (Γ₀₁.bit0 :: encode_pos_num m)
= pos_num.bit0 (decode_pos_num (encode_pos_num m)) : by trivial
... = m.bit0 : by rw hm,
end
def encodek_num : ∀ n, (decode_num(encode_num n) ) = n := begin
intros n,
cases n,
trivial,
change decode_num (encode_pos_num n) = num.pos n,
have h : encode_pos_num n ≠ [] := encode_pos_num_nonempty n,
have h' : decode_num (encode_pos_num n) = decode_pos_num (encode_pos_num n) := begin
cases encode_pos_num n; trivial,
end,
rw h',
rw (encodek_pos_num n),
simp only [pos_num.cast_to_num],
end
def encodek_nat : ∀ n, (decode_nat(encode_nat n) ) = n := begin
intros n,
change decode_nat (encode_num n) = n,
have h :decode_num (encode_num n) = n :=
begin
change decode_num (encode_num n) = n,
exact encodek_num ↑n,
end,
have h' : encodable.encode (decode_num (encode_num n)) = encodable.encode n :=
begin
rw h,
simp,
change (λ n, n : num → ℕ) ↑n = n,
simp,
end,
exact h',
end
def encoding_nat_Γ₀₁ : encoding ℕ :=
{ Γ := Γ₀₁,
encode := encode_nat,
decode := λ n, option.some (decode_nat n),
encodek := begin funext, simp, exact encodek_nat x end
}
def fin_encoding_nat_Γ₀₁ : fin_encoding ℕ :=
{ Γ_fin := Γ₀₁_fin,
..encoding_nat_Γ₀₁
}
def encoding_nat_Γ' : encoding ℕ :=
{ Γ := Γ',
encode := (list.map inclusion_Γ₀₁_Γ') ∘ encode_nat,
decode := option.some ∘ decode_nat ∘ (list.map section_Γ'_Γ₀₁),
encodek := begin
funext,
simp,
have h : section_Γ'_Γ₀₁ ∘ inclusion_Γ₀₁_Γ' = id := begin funext, simp, exact left_inverse_section_inclusion x end,
simp [h],
exact encodek_nat x,
end}
def fin_encoding_nat_Γ' : fin_encoding ℕ :=
{ Γ_fin := Γ'_fin,
..encoding_nat_Γ'
}
def encode_bool : bool → list Γ₀₁
| ff := [Γ₀₁.bit0]
| tt := [Γ₀₁.bit1]
def decode_bool : list Γ₀₁ → bool
| [Γ₀₁.bit0] := ff
| [Γ₀₁.bit1] := tt
| _ := arbitrary bool
def encodek_bool : ∀ b, (decode_bool(encode_bool b) ) = b := λ b,
begin
cases b; refl
end
def encoding_bool_Γ₀₁ : fin_encoding bool :=
{ Γ := Γ₀₁,
encode := encode_bool,
decode := option.some ∘ decode_bool,
encodek := begin funext, simp [encodek_bool x], end,
Γ_fin := Γ₀₁_fin }
end encoding
|
d1e2338273501b050c9096a54f844159c3d204e7 | cf39355caa609c0f33405126beee2739aa3cb77e | /tests/lean/ctx_error_msgs.lean | aef8c9811e5fa4915986c37c72c95393fb9c48f9 | [
"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 | 494 | lean | open tactic
example (f : nat) (a : nat) : true :=
by do
f ← get_local `f,
a ← get_local `a,
infer_type (expr.app f a) >>= trace,
constructor
example (a : nat) : true :=
by do
a ← get_local `a,
clear a,
infer_type a >>= trace,
constructor
example (a : nat) : true :=
by do
infer_type (expr.const `eq []) >>= trace,
constructor
example (a : nat) : true :=
by do
l ← return $ level.zero,
infer_type (expr.const `eq [l, l]) >>= trace,
constructor
|
eca3666542b778db9934dfea475efcdecd089c97 | 952248371e69ccae722eb20bfe6815d8641554a8 | /src/radicals.lean | 0a7ffe3505cb46838463e24df091296b903ab7de | [] | no_license | robertylewis/lean_polya | 5fd079031bf7114449d58d68ccd8c3bed9bcbc97 | 1da14d60a55ad6cd8af8017b1b64990fccb66ab7 | refs/heads/master | 1,647,212,226,179 | 1,558,108,354,000 | 1,558,108,354,000 | 89,933,264 | 1 | 2 | null | 1,560,964,118,000 | 1,493,650,551,000 | Lean | UTF-8 | Lean | false | false | 10,183 | lean | import rat_additions tactic.norm_num --comp_val
open tactic
lemma rat_pow_mul (a : ℚ) (e : ℤ) : rat.pow a (e + e) = rat.pow a e * rat.pow a e := sorry
@[simp]
lemma rat.pow_zero (b : ℚ) : rat.pow b 0 = 1 := sorry
attribute [simp] rat.pow_one
meta def pf_by_norm_num : tactic unit :=
do (lhs, rhs) ← target >>= match_eq,
(lhsv, lhspf) ← norm_num lhs,
(rhsv, rhspf) ← norm_num rhs,
to_expr ``(eq.trans %%lhspf (eq.symm %%rhspf)) >>= exact
lemma f1 (a) : rat.pow a ↑(0 : ℕ) = rat.pow a 0 := sorry
lemma f2 (a) : rat.pow a ↑(1 : ℕ) = rat.pow a 1 := sorry
lemma f3 (a b) : rat.pow a ↑(bit1 b : ℕ) = rat.pow a (bit1 ↑b) := sorry
lemma f4 (a b) : rat.pow a ↑(bit0 b : ℕ) = rat.pow a (bit0 ↑b) := sorry
meta def rat_pow_simp_lemmas : tactic simp_lemmas :=
to_simp_lemmas simp_lemmas.mk [`pow_bit0, `pow_bit1, `rat.pow_zero, `rat.pow_one, `f1, `f2, `f3, `f4]
meta def simp_rat_pow : tactic unit :=
rat_pow_simp_lemmas >>= simp_target
--`[simp only [pow_bit0, pow_bit1, rat.pow_zero, rat.pow_one, f1, f2, f3, f4]]
meta def prove_rat_pow : tactic unit :=
simp_rat_pow >> pf_by_norm_num
--example : rat.pow 5 3 = 125 := by prove_rat_pow
inductive approx_dir
| over | under | none
meta def approx_dir.to_expr : approx_dir → expr
| approx_dir.over := `(@ge rat _)
| approx_dir.under := `(@has_le.le rat _)
| approx_dir.none := `(@eq rat)
meta def correct_offset (A x prec : ℚ) (n : ℤ) : approx_dir → ℚ
| approx_dir.over := if rat.pow x n ≥ A then x else x + prec
| approx_dir.under := if rat.pow x n ≤ A then x else x - prec
| approx_dir.none := x
meta def round_to_denom (A : ℚ) (denom : ℕ) : ℚ :=
let num_q := (denom*A.num : ℤ) / A.denom in
rat.mk num_q denom
namespace rat
meta def nth_root_aux'' (A : ℚ) (n : ℤ) (prec : ℚ) (dir : approx_dir) : ℚ → ℚ
| guess :=
let delta_x := (1/(n : ℚ))*((A / rat.pow guess (n - 1)) - guess),
x := guess + delta_x in
/-correct_offset A-/ (if abs delta_x < prec then x else nth_root_aux'' x) /-prec n dir-/
meta def nth_root_aux' (A : ℚ) (n : ℤ) (prec : ℚ) (dir : approx_dir) (guess : ℚ) : ℚ :=
correct_offset A (nth_root_aux'' A n prec dir guess) prec n dir
meta def nth_root_aux (A : ℚ) (n : ℤ) (prec guess : ℚ) (dir : approx_dir := approx_dir.none) : ℚ :=
nth_root_aux' A n prec dir guess
/-meta def nth_root_approx (A : ℚ) (n : ℤ) (prec : ℚ) (dir : approx_dir := approx_dir.none) : ℚ :=
nth_root_aux A n prec (A / n) dir-/
meta def nth_root_approx_bin_aux (A : ℚ) (n : ℤ) (prec : ℚ) : ℕ → ℚ → ℚ
| steps guess :=
if steps = 0 then guess
else let v := rat.pow guess n in
if v < A then nth_root_approx_bin_aux (steps/2) (guess + steps*prec)
else if v > A then nth_root_approx_bin_aux (steps/2) (guess - steps*prec)
else guess
meta def nth_root_approx_bin (A : ℚ) (n : ℤ) (prec guess : ℚ) (dir : approx_dir := approx_dir.none) : ℚ :=
let v := nth_root_approx_bin_aux A n prec (ceil (A/4)).nat_abs guess in
correct_offset A v prec n dir
end rat
namespace int
/-def pow (base : ℤ) : ℤ → ℤ
| (of_nat n) := pow_nat base n
| -[1+n] :=
meta def nth_root_aux (A : ℤ) (n : ℤ) (prec : ℚ) : ℤ → ℤ
| guess :=
let delta_x := ((A / pow_int guess (n - 1)) - guess) / n,
x := guess + delta_x in
if abs delta_x < prec then x else nth_root_aux x
meta def nth_root_approx (A : ℚ) (n : ℤ) (prec : ℚ) : ℚ :=
nth_root_aux A n prec (A / n)-/
-- inl means success, inr means failed with
meta def nth_root_aux (A : ℤ) (n : ℕ) : ℤ → ℤ → ℤ⊕ℤ
| step guess :=
if step = 0 ∨ step = 1 then
if guess^n = A then sum.inl guess else if (guess+1)^n=A then sum.inl $ guess+1 else if (guess-1)^n=A then sum.inl $ guess-1 else sum.inr guess
else
if guess^n = A then sum.inl guess
else if guess^n < A then nth_root_aux ((step+1)/2) (guess+step)
else nth_root_aux ((step+1)/2) (guess-step)
meta def nth_root (A : ℤ) (n : ℕ) : ℤ⊕ℤ :=
nth_root_aux A n ((A+1)/4) ((A+1)/4)
meta def nth_root_o (A : ℤ) (n : ℕ) : option ℤ :=
match nth_root A n with
| sum.inl v := some v
| sum.inr _ := none
end
end int
/--- faster to skip first rat approx in non-1 denom case?
meta def nth_root_approx' (dir : approx_dir) : Π (A : ℚ) (n : ℕ) (prec : ℚ), ℚ | A n prec :=
if A.denom = 1 then
match int.nth_root A.num n with
| sum.inl v := v
| sum.inr v := rat.nth_root_aux A n prec (if v = 0 then prec else v) dir
end
else
let num_apr := int.nth_root A.num n,
den_apr := int.nth_root A.denom n in
match num_apr, den_apr with
| sum.inl vn, sum.inl vd := (vn : ℚ) / vd
| sum.inl vn, sum.inr vd := rat.nth_root_aux A n prec (vn / vd) dir --(vn / rat.nth_root_aux A.denom n prec vd) dir
| sum.inr vn, sum.inl vd := rat.nth_root_aux A n prec (vn / vd) dir --(rat.nth_root_aux A.num n prec vn / vd) dir
| sum.inr vn, sum.inr vd := rat.nth_root_aux A n prec (vn / vd) dir--(rat.nth_root_aux A.num n prec vn / rat.nth_root_aux A.denom n prec vn) dir
end
-- rat.nth_root_aux A n prec ((nth_root_approx' A.num n prec) / (nth_root_approx' A.denom n prec)) dir
-/
-- no rounding or direction fixing
meta def nth_root_approx''_a (dir : approx_dir) : Π (A : ℚ) (n : ℕ) (prec : ℚ), ℚ | A n prec :=
if A.denom = 1 then
match int.nth_root A.num n with
| sum.inl v := v
| sum.inr v := (rat.nth_root_aux'' A n prec dir (if v = 0 then prec else v)) --rat.nth_root_aux A n prec (if v = 0 then prec else v) dir
end
else
let num_apr := int.nth_root A.num n,
den_apr := int.nth_root A.denom n in
match num_apr, den_apr with
| sum.inl vn, sum.inl vd := (vn : ℚ) / vd
| sum.inl vn, sum.inr vd := rat.nth_root_aux'' A n prec dir (vn / vd) --(vn / rat.nth_root_aux A.denom n prec vd) dir
| sum.inr vn, sum.inl vd := rat.nth_root_aux'' A n prec dir (vn / vd) --(rat.nth_root_aux A.num n prec vn / vd) dir
| sum.inr vn, sum.inr vd := rat.nth_root_aux'' A n prec dir (vn / vd) --(rat.nth_root_aux A.num n prec vn / rat.nth_root_aux A.denom n prec vn) dir
end
meta def nth_root_approx (dir : approx_dir) (A : ℚ) (n : ℕ) (denom : ℕ) /-(prec : ℚ)-/ : ℚ :=
let av := nth_root_approx''_a dir A n (1/(denom : ℚ)) in
correct_offset A (round_to_denom av denom) (1/(denom : ℚ)) n dir
/-meta def nth_root_approx (A : ℚ) (n : ℕ) (prec : ℚ) (dir : approx_dir := approx_dir.none) : ℚ :=
nth_root_approx' dir A n prec
-/
open tactic
meta def prove_nth_root_approx (A : ℚ) (n : ℕ) (prec : ℕ) (dir : approx_dir) : tactic expr :=
let apprx := nth_root_approx dir A n prec in
do apprx_pow ← to_expr ``(rat.pow %%(reflect apprx) %%(reflect n)),
let tgt := dir.to_expr apprx_pow `(A) in
do (_, e) ← solve_aux tgt (trace_state >> simp_rat_pow >> trace_state >> (try `[norm_num]) >> done),
return e
--set_option pp.all true
#exit
local attribute [irreducible] rat.pow
example : (0 : ℚ) ≤ 5+1 :=
begin
simp
end
--example : (0 : ℚ) ≤ 500 := by reflexivity
#eval let v := nth_root_approx 10 2 0.5 in v*v
#eval nth_root_approx 2 2 10 approx_dir.under
#eval int.nth_root 2 2
#eval rat.nth_root_aux 2 2 100 0 approx_dir.over
#eval rat.nth_root_aux 2 4 100 0 approx_dir.over
example : (198 : ℚ) / 100 ≤ 2 :=
by norm_num --btrivial
run_cmd do prove_nth_root_approx 2 2 1000 approx_dir.under >>= infer_type >>= trace
/-do trace "hi",
let
tgt := dir.to_expr `(rat.pow apprx n) A in
failed
-/
#exit
set_option profiler true
run_cmd trace $int.nth_root 1934434936 3
run_cmd trace $ nth_root_approx 19344349361234579569400000000000 2 0.0000000000000000000005
run_cmd trace $ nth_root_approx 54354358908309423742342 2 0.0000000000000000000005
run_cmd trace $ nth_root_approx (54354358908309423742342 / 19344349361234579569400000000000) 2 0.0000000000000000000005
run_cmd trace $ nth_root_approx (100/81) 2 0.00005
example : true :=
by do
trace $ nth_root_approx 1934434936134579569400000000000 2 0.0000000000000000000005,
triv
--run_cmd trace $ int.nth_root 10 2
--run_cmd trace $ rat.nth_root_aux 10 2 0.5 4
#exit
meta def nearest_int (q : ℚ) : ℤ :=
if ↑q.ceil - q < 0.5 then q.ceil else q.floor
meta def find_integer_root (q : ℚ) (n : ℤ) : option ℚ :=
let n_o := nearest_int $ nth_root_approx q n 0.5 in
if rat.pow n_o n = q then some n_o else none
#exit
meta def small_factor_precomp : list ((int × rat) × (rat × expr)) :=
[
((2, 1), (1, `(by prove_rat_pow : rat.pow 1 2 = 1))),
((2, 4), (2, `(by prove_rat_pow : rat.pow 2 2 = 4))),
((2, 9), (3, `(by prove_rat_pow : rat.pow 3 2 = 9))),
((2, 16), (4, `(by prove_rat_pow : rat.pow 4 2 = 16))),
((2, 25), (5, `(by prove_rat_pow : rat.pow 5 2 = 25))),
((2, 36), (6, `(by prove_rat_pow : rat.pow 6 2 = 36))),
((2, 49), (7, `(by prove_rat_pow : rat.pow 7 2 = 49))),
((2, 64), (8, `(by prove_rat_pow : rat.pow 8 2 = 64))),
((2, 81), (9, `(by prove_rat_pow : rat.pow 9 2 = 81))),
((2, 100), (10, `(by prove_rat_pow : rat.pow 10 2 = 100))),
((3, 1), (1, `(by prove_rat_pow : rat.pow 1 3 = 1))),
((3, 8), (2, `(by prove_rat_pow : rat.pow 2 3 = 8))),
((3, 27), (3, `(by prove_rat_pow : rat.pow 3 3 = 27))),
((3, 64), (4, `(by prove_rat_pow : rat.pow 4 3 = 64))),
((3, 125), (5, `(by prove_rat_pow : rat.pow 5 3 = 125))),
((3, 216), (6, `(by prove_rat_pow : rat.pow 6 3 = 216))),
((3, 343), (7, `(by prove_rat_pow : rat.pow 7 3 = 343))),
((3, 512), (8, `(by prove_rat_pow : rat.pow 8 3 = 512))),
((3, 729), (9, `(by prove_rat_pow : rat.pow 9 3 = 729))),
((3, 1000), (10, `(by prove_rat_pow : rat.pow 10 3 = 1000))),
((4, 1), (1, `(by prove_rat_pow : rat.pow 1 4 = 1))),
((4, 16), (2, `(by prove_rat_pow : rat.pow 2 4 = 16))),
((4, 81), (3, `(by prove_rat_pow : rat.pow 3 4 = 81))),
((4, 256), (4, `(by prove_rat_pow : rat.pow 4 4 = 256))),
((4, 625), (5, `(by prove_rat_pow : rat.pow 5 4 = 625))),
((4, 1296), (6, `(by prove_rat_pow : rat.pow 6 4 = 1296))),
((4, 2401), (7, `(by prove_rat_pow : rat.pow 7 4 = 2401))),
((4, 4096), (8, `(by prove_rat_pow : rat.pow 8 4 = 4096))),
((4, 6561), (9, `(by prove_rat_pow : rat.pow 9 4 = 6561))),
((4, 10000), (10, `(by prove_rat_pow : rat.pow 10 4 = 10000)))
]
meta def small_factor_map : rb_map (ℤ × ℚ) (ℚ × expr) :=
rb_map.of_list small_factor_precomp
|
513d8c679767a70aa0e77927a4eacf6ecedefeeb | b82c5bb4c3b618c23ba67764bc3e93f4999a1a39 | /src/formal_ml/order_topological_basis.lean | 0f7a808ad030a1d8bce041c9c861378f9a8756a3 | [
"Apache-2.0"
] | permissive | nouretienne/formal-ml | 83c4261016955bf9bcb55bd32b4f2621b44163e0 | 40b6da3b6e875f47412d50c7cd97936cb5091a2b | refs/heads/master | 1,671,216,448,724 | 1,600,472,285,000 | 1,600,472,285,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 15,040 | lean | /-
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
import measure_theory.measurable_space
import measure_theory.measure_space
import measure_theory.outer_measure
import measure_theory.lebesgue_measure
import measure_theory.integration
import measure_theory.borel_space
import data.set.countable
--import formal_ml.set
import formal_ml.set
import formal_ml.topological_space
/-
Establishes a topological basis for a topological_space T and a type α where:
1. T is an order_topology
2. α has a decidable_linear_order
3. α has two distinct elements.
that Specifically, the goal of this file is to prove a topological
basis for the extended reals (ennreal, or [0,∞]).
A tricky part of a topological basis is that it must be closed under intersection. However, I
am not entirely clear as to the value of this. Specifically, one benefit of a basis is to prove
a function is continuous by considering the inverse of all sets. However, this adds a great
deal of complexity, because (1) it introduces a bunch of open sets one has to take the inverse
for and (2) it is sufficient to prove for sets of the form [0, a) and (a,∞] are open.
The value of a topological basis is that you can use it to prove that something is not
continuous. Thus, it is of particular importance to have a topological basis for ennreal,
because in this way you can show that multiplication is discontinuous.
Also, as far as I understand it, this theory can be lifted to an arbitrary order
topology. Specifically, if you have a linear order, then you should be able to prove this.
Some observations:
1. The work below tries too hard to prove that the basis does not contain the empty set.
This is harder than expected, because {b|b < 0} and {b|⊤ < b} are empty. However,
embracing the empty set makes the problem easy. Specifically:
Ioo a b ∩ Ioo c d = Ioo (max a c) (min b d)
Ioo a b ∩ Ioi c = Ioo (max a c) b
Ioo a b ∩ Iio c = Ioo a (min b c)
Iio a ∩ Ioi b = Ioo b a
Iio a ∩ Iio b = Iio (min a b)
Ioi a ∩ Ioi b = Ioi (max a b)
2. Also, the topological basis for the order topology is simply:
{U:set ennreal|∃ a b, U = (set.Ioo a b)}∪ {s | ∃a, s = {b | a < b} ∨ s = {b | b < a}}
There are introduction and elimination rules for the membership of Ioo, Iio, and Ioi.
-/
def order_topological_basis (α:Type*) [linear_order α]:set (set α) :=
{U:set α|∃ a b, U = (set.Ioo a b)}∪
{s | ∃a, s = {b | a < b} ∨ s = {b | b < a}}
lemma mem_order_topological_basis_intro_Ioo {α:Type*}
[linear_order α] (a b:α):
(set.Ioo a b) ∈ (order_topological_basis α) :=
begin
unfold order_topological_basis,
left,
apply exists.intro a,
apply exists.intro b,
refl,
end
lemma mem_order_topological_basis_intro_Ioi
{α:Type*} [linear_order α] (a:α):
set.Ioi a ∈ (order_topological_basis α) :=
begin
unfold order_topological_basis,
right,
apply exists.intro a,
left,
refl,
end
lemma mem_order_topological_basis_intro_Iio
{α:Type*} [linear_order α] (a:α):
set.Iio a ∈ (order_topological_basis α) :=
begin
unfold order_topological_basis,
right,
apply exists.intro a,
right,
refl,
end
def mem_order_topological_basis_def (α:Type*) [linear_order α] (U:set α):
U ∈ (order_topological_basis α) ↔
((∃ (a b:α), U=(set.Ioo a b))∨ (∃ c:α,(U=(set.Ioi c))∨ (U=(set.Iio c)))) :=
begin
refl,
end
lemma Ioo_inter_Ioo_eq_Ioo {α:Type*} [decidable_linear_order α] {a b c d:α}:
set.Ioo a b ∩ set.Ioo c d = set.Ioo (max a c) (min b d) :=
begin
unfold set.Ioo,
ext,split;intro A1,
{
simp at A1,
cases A1 with A2 A3,
cases A2 with A4 A5,
cases A3 with A6 A7,
simp,
apply (and.intro (and.intro A4 A6) (and.intro A5 A7)),
},
{
simp at A1,
cases A1 with A2 A3,
cases A2 with A4 A5,
cases A3 with A6 A7,
simp,
apply and.intro (and.intro A4 A6) (and.intro A5 A7),
},
end
lemma Ioo_inter_Ioi_eq_Ioo {α:Type*} [decidable_linear_order α] {a b c:α}:
set.Ioo a b ∩ set.Ioi c = set.Ioo (max a c) b :=
begin
unfold set.Ioo,
unfold set.Ioi,
ext,split;intro A1,
{
simp at A1,
cases A1 with A2 A3,
cases A2 with A4 A5,
simp,
apply and.intro (and.intro A4 A3) A5,
},
{
simp at A1,
cases A1 with A2 A3,
cases A2 with A4 A5,
simp,
apply and.intro (and.intro A4 A3) A5,
}
end
lemma Ioi_inter_Iio_eq_Ioo {α:Type*} [decidable_linear_order α] {a b:α}:
set.Ioi a ∩ set.Iio b = set.Ioo a b :=
begin
unfold set.Ioi,
unfold set.Ioo,
refl,
end
lemma Ioo_inter_Iio_eq_Ioo {α:Type*} [decidable_linear_order α] {a b c:α}:
set.Ioo a b ∩ set.Iio c = set.Ioo a (min b c) :=
begin
unfold set.Ioo,
unfold set.Iio,
ext,split;intro A1,
{
simp at A1,
cases A1 with A2 A3,
cases A2 with A4 A5,
simp,
apply and.intro A4 (and.intro A5 A3),
},
{
simp at A1,
simp,
apply and.intro (and.intro A1.left A1.right.left) A1.right.right,
}
end
lemma Iio_inter_Iio_eq_Iio {α:Type*} [decidable_linear_order α] {a b:α}:
set.Iio a ∩ set.Iio b = set.Iio (min a b) :=
begin
unfold set.Iio,
ext,split;intro A1,
{
simp at A1,
simp,
apply A1,
},
{
simp at A1,
simp,
apply A1,
}
end
lemma Ioi_inter_Ioi_eq_Ioi {α:Type*} [decidable_linear_order α] {a b:α}:
set.Ioi a ∩ set.Ioi b = set.Ioi (max a b) :=
begin
unfold set.Ioi,
ext,split;intro A1,
{
simp at A1,
simp,
apply A1,
},
{
simp at A1,
simp,
apply A1,
}
end
lemma inter_order_topological_basis (α:Type*) [decidable_linear_order α] {U V:set α}:
(U ∈ (order_topological_basis α)) →
(V ∈ (order_topological_basis α)) →
((U ∩ V) ∈ (order_topological_basis α)) :=
begin
intros A1 A2,
rw mem_order_topological_basis_def at A1,
rw mem_order_topological_basis_def at A2,
cases A1,
{
cases A1 with a A1,
cases A1 with b A1,
subst U,
cases A2,
{
cases A2 with c A2,
cases A2 with d A2,
subst V,
rw Ioo_inter_Ioo_eq_Ioo,
apply mem_order_topological_basis_intro_Ioo,
},
cases A2 with c A2,
cases A2,
{
subst V,
rw Ioo_inter_Ioi_eq_Ioo,
apply mem_order_topological_basis_intro_Ioo,
},
{
subst V,
rw Ioo_inter_Iio_eq_Ioo,
apply mem_order_topological_basis_intro_Ioo,
},
},
cases A1 with c A1,
cases A1,
{
subst U,
cases A2,
{
cases A2 with a A2,
cases A2 with b A2,
subst V,
rw set.inter_comm,
rw Ioo_inter_Ioi_eq_Ioo,
apply mem_order_topological_basis_intro_Ioo,
},
cases A2 with a A2,
cases A2,
{
subst V,
rw Ioi_inter_Ioi_eq_Ioi,
apply mem_order_topological_basis_intro_Ioi,
},
{
subst V,
rw Ioi_inter_Iio_eq_Ioo,
apply mem_order_topological_basis_intro_Ioo,
}
},
{
subst U,
cases A2,
{
cases A2 with a A2,
cases A2 with b A2,
subst V,
rw set.inter_comm,
rw Ioo_inter_Iio_eq_Ioo,
apply mem_order_topological_basis_intro_Ioo,
},
cases A2 with c A2,
cases A2,
{
subst V,
rw set.inter_comm,
rw Ioi_inter_Iio_eq_Ioo,
apply mem_order_topological_basis_intro_Ioo,
},
{
subst V,
rw Iio_inter_Iio_eq_Iio,
apply mem_order_topological_basis_intro_Iio,
},
},
end
/-
Note that the order topological basis is only a basis if there is at
least two distinct elements of the type. A singleton set only has the
universe and the empty set as open sets.
-/
lemma order_topological_basis_cover {α:Type*} [linear_order α] {a b:α}:
(a<b)→
⋃₀ (order_topological_basis α) = set.univ :=
begin
intro A0,
ext;split;intro A1,
{
simp,
},
{
clear A1,
simp,
have A2:(a < x ∨ x ≤ a) := lt_or_le a x,
cases A2,
{
apply exists.intro (set.Ioi a),
split,
apply mem_order_topological_basis_intro_Ioi,
apply A2,
},
{
apply exists.intro (set.Iio b),
split,
apply mem_order_topological_basis_intro_Iio,
apply lt_of_le_of_lt,
exact A2,
exact A0,
}
}
end
lemma generate_from_order_topological_basis {α:Type*} [decidable_linear_order α]
[T:topological_space α]
[X:order_topology α]:
T = @topological_space.generate_from α (order_topological_basis α) :=
begin
rw X.topology_eq_generate_intervals,
apply generate_from_eq_generate_from,
{
intros V A1,
apply topological_space.generate_open.basic,
simp at A1,
cases A1 with a A1,
cases A1,
{
subst V,
apply mem_order_topological_basis_intro_Ioi,
},
{
subst V,
apply mem_order_topological_basis_intro_Iio,
},
},
{
intros V A1,
rw mem_order_topological_basis_def at A1,
cases A1,
{
cases A1 with a A1,
cases A1 with b A1,
rw ← Ioi_inter_Iio_eq_Ioo at A1,
subst V,
apply topological_space.generate_open.inter;apply topological_space.generate_open.basic,
{
apply exists.intro a,
left,refl,
},
{
apply exists.intro b,
right,refl,
}
},
cases A1 with c A1,
apply topological_space.generate_open.basic,
cases A1;subst V;apply exists.intro c,
{
left,refl,
},
{
right,refl,
},
}
end
lemma is_topological_basis_order_topology {α:Type*} [decidable_linear_order α]
[T:topological_space α]
[X:order_topology α] {a b:α}:
(a < b)→
topological_space.is_topological_basis
(order_topological_basis α) :=
begin
intro A0,
unfold topological_space.is_topological_basis,
split,
{
intros t₁ A1 t₂ A2 x A3,
apply exists.intro (t₁ ∩ t₂),
split,
{
apply inter_order_topological_basis,
apply A1,
apply A2,
},
split,
{
exact A3,
},
{
apply set.subset.refl,
}
},
split,
{
apply order_topological_basis_cover A0,
},
{
apply generate_from_order_topological_basis,
}
end
lemma ennreal_is_topological_basis:
topological_space.is_topological_basis
(order_topological_basis ennreal) :=
begin
have A1:(0:ennreal) < (1:ennreal) := canonically_ordered_semiring.zero_lt_one,
apply is_topological_basis_order_topology A1,
end
/- In Munkres, Topology, Second Edition, page 84, they specify the basis of the order
topology as above (modulo the empty set). However, if there is a singleton type,
this is not the case.
-/
lemma not_is_topological_basis_singleton {α:Type*} [decidable_linear_order α]
[T:topological_space α]
[X:order_topology α]
[S:subsingleton α]
{a:α}:
¬ (topological_space.is_topological_basis
(order_topological_basis α)) :=
begin
intro A1,
unfold topological_space.is_topological_basis at A1,
cases A1 with A2 A3,
cases A3 with A4 A5,
have A6:a∈ set.univ := set.mem_univ a,
rw ← A4 at A6,
cases A6 with V A7,
cases A7 with A8 A9,
rw mem_order_topological_basis_def at A8,
cases A8,
{
cases A8 with b A8,
have A10:b=a := subsingleton.elim b a,
subst b,
cases A8 with c A8,
have A11:c=a := subsingleton.elim c a,
subst c,
subst V,
apply (@set.left_mem_Ioo α _ a a).mp,
exact A9,
},
cases A8 with c A8,
have A12:c=a := subsingleton.elim c a,
subst c,
cases A8,
{
subst V,
rw set.mem_Ioi at A9,
apply lt_irrefl a A9,
},
{
subst V,
rw set.mem_Iio at A9,
apply lt_irrefl a A9,
},
end
namespace order_topology_counterexample
inductive ot_singleton:Type
| ot_element
lemma ot_singleton_subsingletonh (a b:ot_singleton):a = b :=
begin
cases a,
cases b,
refl,
end
instance ot_singleton_subsingleton:subsingleton ot_singleton :=
subsingleton.intro ot_singleton_subsingletonh
def ot_singleton_le (a b:ot_singleton):Prop := true
def ot_singleton_lt (a b:ot_singleton):Prop := false
instance ot_singleton_has_lt:has_lt ot_singleton := {
lt := ot_singleton_lt,
}
instance ot_singleton_has_le:has_le ot_singleton := {
le := ot_singleton_le,
}
lemma ot_singleton_le_true (a b:ot_singleton): a ≤ b = true :=
begin
refl,
end
lemma ot_singleton_le_true2 (a b:ot_singleton): a ≤ b :=
begin
rw ot_singleton_le_true,
apply true.intro,
end
lemma ot_singleton_lt_false (a b:ot_singleton): a < b = false :=
begin
refl,
end
lemma ot_singleton_lt_false2 (a b:ot_singleton): ¬ (a < b) :=
begin
intro A1,
rw ot_singleton_lt_false at A1,
exact A1,
end
lemma ot_singleton_le_refl (a:ot_singleton):a ≤ a :=
begin
apply ot_singleton_le_true2,
end
lemma ot_singleton_le_trans (a b c:ot_singleton):a ≤ b → b ≤ c → a ≤ c :=
begin
intros A1 A2,
apply ot_singleton_le_true2,
end
lemma ot_singleton_le_antisymm (a b:ot_singleton):a ≤ b → b ≤ a → a = b :=
begin
intros A1 A2,
apply subsingleton.elim,
end
lemma ot_singleton_le_total (a b:ot_singleton):a ≤ b ∨ b ≤ a :=
or.inl (ot_singleton_le_true2 a b)
def ot_singleton_decidable_le:decidable_rel (ot_singleton_le) :=
begin
unfold decidable_rel,
intros a b,
apply decidable.is_true (ot_singleton_le_true2 a b),
end
def ot_singleton_decidable_lt:decidable_rel (ot_singleton_lt) :=
begin
unfold decidable_rel,
intros a b,
apply decidable.is_false (ot_singleton_lt_false2 a b),
end
lemma ot_singleton_lt_iff_le_not_le (a b:ot_singleton):a < b ↔ a ≤ b ∧ ¬b ≤ a :=
begin
split;intro A1,
{
exfalso,
apply ot_singleton_lt_false2 a b,
exact A1,
},
{
exfalso,
cases A1 with A2 A3,
apply A3,
apply ot_singleton_le_true2,
}
end
instance ot_singleton_decidable_linear_order:decidable_linear_order ot_singleton := {
le := ot_singleton_le,
lt := ot_singleton_lt,
le_refl := ot_singleton_le_refl,
le_trans := ot_singleton_le_trans,
le_antisymm := ot_singleton_le_antisymm,
lt_iff_le_not_le := ot_singleton_lt_iff_le_not_le,
le_total := ot_singleton_le_total,
decidable_le := ot_singleton_decidable_le,
decidable_lt := ot_singleton_decidable_lt,
}
lemma not_is_topological_basis_ot_singleton
[T:topological_space ot_singleton]
[X:order_topology ot_singleton]:
¬ (topological_space.is_topological_basis
(order_topological_basis ot_singleton)) :=
begin
apply not_is_topological_basis_singleton,
apply ot_singleton.ot_element,
end
end order_topology_counterexample
|
4fb5621e2021634d1338d62d1d8e9accf36eba88 | ad0c7d243dc1bd563419e2767ed42fb323d7beea | /data/real/cau_seq_completion.lean | 46a1675857fd6a6c3f2097f4d25e1fd8a62a4e4e | [
"Apache-2.0"
] | permissive | sebzim4500/mathlib | e0b5a63b1655f910dee30badf09bd7e191d3cf30 | 6997cafbd3a7325af5cb318561768c316ceb7757 | refs/heads/master | 1,585,549,958,618 | 1,538,221,723,000 | 1,538,221,723,000 | 150,869,076 | 0 | 0 | Apache-2.0 | 1,538,229,323,000 | 1,538,229,323,000 | null | UTF-8 | Lean | false | false | 4,986 | lean | /-
Copyright (c) 2018 Mario Carneiro. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Robert Y. Lewis
Generalizes the Cauchy completion of (ℚ, abs) to the completion of a
commutative ring with absolute value.
-/
import data.real.cau_seq
namespace cau_seq.completion
open cau_seq
section
parameters {α : Type*} [discrete_linear_ordered_field α]
parameters {β : Type*} [comm_ring β] {abv : β → α} [is_absolute_value abv]
def Cauchy := @quotient (cau_seq _ abv) cau_seq.equiv
def mk : cau_seq _ abv → Cauchy := quotient.mk
@[simp] theorem mk_eq_mk (f) : @eq Cauchy ⟦f⟧ (mk f) := rfl
theorem mk_eq {f g} : mk f = mk g ↔ f ≈ g := quotient.eq
def of_rat (x : β) : Cauchy := mk (const abv x)
instance : has_zero Cauchy := ⟨of_rat 0⟩
instance : has_one Cauchy := ⟨of_rat 1⟩
instance : inhabited Cauchy := ⟨0⟩
theorem of_rat_zero : of_rat 0 = 0 := rfl
theorem of_rat_one : of_rat 1 = 1 := rfl
@[simp] theorem mk_eq_zero {f} : mk f = 0 ↔ lim_zero f :=
by have : mk f = 0 ↔ lim_zero (f - 0) := quotient.eq;
rwa sub_zero at this
instance : has_add Cauchy :=
⟨λ x y, quotient.lift_on₂ x y (λ f g, mk (f + g)) $
λ f₁ g₁ f₂ g₂ hf hg, quotient.sound $
by simpa [(≈), setoid.r] using add_lim_zero hf hg⟩
@[simp] theorem mk_add (f g : cau_seq β abv) : mk f + mk g = mk (f + g) := rfl
instance : has_neg Cauchy :=
⟨λ x, quotient.lift_on x (λ f, mk (-f)) $
λ f₁ f₂ hf, quotient.sound $
by simpa [(≈), setoid.r] using neg_lim_zero hf⟩
@[simp] theorem mk_neg (f : cau_seq β abv) : -mk f = mk (-f) := rfl
instance : has_mul Cauchy :=
⟨λ x y, quotient.lift_on₂ x y (λ f g, mk (f * g)) $
λ f₁ g₁ f₂ g₂ hf hg, quotient.sound $
by simpa [(≈), setoid.r, mul_add, mul_comm] using
add_lim_zero (mul_lim_zero g₁ hf) (mul_lim_zero f₂ hg)⟩
@[simp] theorem mk_mul (f g : cau_seq β abv) : mk f * mk g = mk (f * g) := rfl
theorem of_rat_add (x y : β) : of_rat (x + y) = of_rat x + of_rat y :=
congr_arg mk (const_add _ _)
theorem of_rat_neg (x : β) : of_rat (-x) = -of_rat x :=
congr_arg mk (const_neg _)
theorem of_rat_mul (x y : β) : of_rat (x * y) = of_rat x * of_rat y :=
congr_arg mk (const_mul _ _)
private lemma zero_def : 0 = mk 0 := rfl
private lemma one_def : 1 = mk 1 := rfl
instance : comm_ring Cauchy :=
by refine { neg := has_neg.neg,
add := (+), zero := 0, mul := (*), one := 1, .. };
{ repeat {refine λ a, quotient.induction_on a (λ _, _)},
simp [zero_def, one_def, mul_left_comm, mul_comm, mul_add] }
theorem of_rat_sub (x y : β) : of_rat (x - y) = of_rat x - of_rat y :=
congr_arg mk (const_sub _ _)
end
local attribute [instance] classical.prop_decidable
section
parameters {α : Type*} [discrete_linear_ordered_field α]
parameters {β : Type*} [discrete_field β] {abv : β → α} [is_absolute_value abv]
local notation `Cauchy` := @Cauchy _ _ _ _ abv _
noncomputable instance : has_inv Cauchy :=
⟨λ x, quotient.lift_on x
(λ f, mk $ if h : lim_zero f then 0 else inv f h) $
λ f g fg, begin
have := lim_zero_congr fg,
by_cases hf : lim_zero f,
{ simp [hf, this.1 hf, setoid.refl] },
{ have hg := mt this.2 hf, simp [hf, hg],
have If : mk (inv f hf) * mk f = 1 := mk_eq.2 (inv_mul_cancel hf),
have Ig : mk (inv g hg) * mk g = 1 := mk_eq.2 (inv_mul_cancel hg),
rw [mk_eq.2 fg, ← Ig] at If,
rw mul_comm at Ig,
rw [← mul_one (mk (inv f hf)), ← Ig, ← mul_assoc, If,
mul_assoc, Ig, mul_one] }
end⟩
@[simp] theorem inv_zero : (0 : Cauchy)⁻¹ = 0 :=
congr_arg mk $ by rw dif_pos; [refl, exact zero_lim_zero]
@[simp] theorem inv_mk {f} (hf) : (@mk α _ β _ abv _ f)⁻¹ = mk (inv f hf) :=
congr_arg mk $ by rw dif_neg
lemma cau_seq_zero_ne_one : ¬ (0 : cau_seq _ abv) ≈ 1 := λ h,
have lim_zero (1 - 0), from setoid.symm h,
have lim_zero 1, by simpa,
one_ne_zero $ const_lim_zero.1 this
lemma zero_ne_one : (0 : Cauchy) ≠ 1 :=
λ h, cau_seq_zero_ne_one $ mk_eq.1 h
protected theorem inv_mul_cancel {x : Cauchy} : x ≠ 0 → x⁻¹ * x = 1 :=
quotient.induction_on x $ λ f hf, begin
simp at hf, simp [hf],
exact quotient.sound (cau_seq.inv_mul_cancel hf)
end
noncomputable def discrete_field : discrete_field Cauchy :=
{ inv := has_inv.inv,
inv_mul_cancel := @cau_seq.completion.inv_mul_cancel,
mul_inv_cancel := λ x x0, by rw [mul_comm, cau_seq.completion.inv_mul_cancel x0],
zero_ne_one := zero_ne_one,
inv_zero := inv_zero,
has_decidable_eq := by apply_instance,
..cau_seq.completion.comm_ring }
local attribute [instance] discrete_field
theorem of_rat_inv (x : β) : of_rat (x⁻¹) = ((of_rat x)⁻¹ : Cauchy) :=
congr_arg mk $ by split_ifs with h; try {simp [const_lim_zero.1 h]}; refl
theorem of_rat_div (x y : β) : of_rat (x / y) = (of_rat x / of_rat y : Cauchy) :=
by simp only [div_eq_inv_mul, of_rat_inv, of_rat_mul]
end
end cau_seq.completion |
aa023f4d41dbe7393a8eab91051650c64d66916f | 4727251e0cd73359b15b664c3170e5d754078599 | /src/measure_theory/function/ae_eq_fun.lean | 1227e854a462120af0d3c52b1ff19e46d3ac1af3 | [
"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 | 31,688 | lean | /-
Copyright (c) 2019 Johannes Hölzl, Zhouhang Zhou. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Zhouhang Zhou
-/
import measure_theory.integral.lebesgue
import order.filter.germ
import topology.continuous_function.algebra
import measure_theory.function.strongly_measurable
/-!
# Almost everywhere equal functions
We build a space of equivalence classes of functions, where two functions are treated as identical
if they are almost everywhere equal. We form the set of equivalence classes under the relation of
being almost everywhere equal, which is sometimes known as the `L⁰` space.
To use this space as a basis for the `L^p` spaces and for the Bochner integral, we consider
equivalence classes of strongly measurable functions (or, equivalently, of almost everywhere
strongly measurable functions.)
See `l1_space.lean` for `L¹` space.
## Notation
* `α →ₘ[μ] β` is the type of `L⁰` space, where `α` is a measurable space, `β` is a topological
space, and `μ` is a measure on `α`. `f : α →ₘ β` is a "function" in `L⁰`.
In comments, `[f]` is also used to denote an `L⁰` function.
`ₘ` can be typed as `\_m`. Sometimes it is shown as a box if font is missing.
## Main statements
* The linear structure of `L⁰` :
Addition and scalar multiplication are defined on `L⁰` in the natural way, i.e.,
`[f] + [g] := [f + g]`, `c • [f] := [c • f]`. So defined, `α →ₘ β` inherits the linear structure
of `β`. For example, if `β` is a module, then `α →ₘ β` is a module over the same ring.
See `mk_add_mk`, `neg_mk`, `mk_sub_mk`, `smul_mk`,
`add_to_fun`, `neg_to_fun`, `sub_to_fun`, `smul_to_fun`
* The order structure of `L⁰` :
`≤` can be defined in a similar way: `[f] ≤ [g]` if `f a ≤ g a` for almost all `a` in domain.
And `α →ₘ β` inherits the preorder and partial order of `β`.
TODO: Define `sup` and `inf` on `L⁰` so that it forms a lattice. It seems that `β` must be a
linear order, since otherwise `f ⊔ g` may not be a measurable function.
## Implementation notes
* `f.to_fun` : To find a representative of `f : α →ₘ β`, use the coercion `(f : α → β)`, which
is implemented as `f.to_fun`.
For each operation `op` in `L⁰`, there is a lemma called `coe_fn_op`,
characterizing, say, `(f op g : α → β)`.
* `ae_eq_fun.mk` : To constructs an `L⁰` function `α →ₘ β` from an almost everywhere strongly
measurable function `f : α → β`, use `ae_eq_fun.mk`
* `comp` : Use `comp g f` to get `[g ∘ f]` from `g : β → γ` and `[f] : α →ₘ γ` when `g` is
continuous. Use `comp_measurable` if `g` is only measurable (this requires the
target space to be second countable).
* `comp₂` : Use `comp₂ g f₁ f₂ to get `[λ a, g (f₁ a) (f₂ a)]`.
For example, `[f + g]` is `comp₂ (+)`
## Tags
function space, almost everywhere equal, `L⁰`, ae_eq_fun
-/
noncomputable theory
open_locale classical ennreal topological_space
open set filter topological_space ennreal emetric measure_theory function
variables {α β γ δ : Type*} [measurable_space α] {μ ν : measure α}
namespace measure_theory
section measurable_space
variables [topological_space β]
variable (β)
/-- The equivalence relation of being almost everywhere equal for almost everywhere strongly
measurable functions. -/
def measure.ae_eq_setoid (μ : measure α) : setoid { f : α → β // ae_strongly_measurable f μ } :=
⟨λ f g, (f : α → β) =ᵐ[μ] g, λ f, ae_eq_refl f, λ f g, ae_eq_symm, λ f g h, ae_eq_trans⟩
variable (α)
/-- The space of equivalence classes of almost everywhere strongly measurable functions, where two
strongly measurable functions are equivalent if they agree almost everywhere, i.e.,
they differ on a set of measure `0`. -/
def ae_eq_fun (μ : measure α) : Type* := quotient (μ.ae_eq_setoid β)
variables {α β}
notation α ` →ₘ[`:25 μ `] ` β := ae_eq_fun α β μ
end measurable_space
namespace ae_eq_fun
variables [topological_space β] [topological_space γ] [topological_space δ]
/-- Construct the equivalence class `[f]` of an almost everywhere measurable function `f`, based
on the equivalence relation of being almost everywhere equal. -/
def mk {β : Type*} [topological_space β]
(f : α → β) (hf : ae_strongly_measurable f μ) : α →ₘ[μ] β := quotient.mk' ⟨f, hf⟩
/-- A measurable representative of an `ae_eq_fun` [f] -/
instance : has_coe_to_fun (α →ₘ[μ] β) (λ _, α → β) :=
⟨λ f, ae_strongly_measurable.mk _ (quotient.out' f : {f : α → β // ae_strongly_measurable f μ}).2⟩
protected lemma strongly_measurable (f : α →ₘ[μ] β) : strongly_measurable f :=
ae_strongly_measurable.strongly_measurable_mk _
protected lemma ae_strongly_measurable (f : α →ₘ[μ] β) : ae_strongly_measurable f μ :=
f.strongly_measurable.ae_strongly_measurable
protected lemma measurable [metrizable_space β] [measurable_space β] [borel_space β]
(f : α →ₘ[μ] β) : measurable f :=
ae_strongly_measurable.measurable_mk _
protected lemma ae_measurable [metrizable_space β] [measurable_space β] [borel_space β]
(f : α →ₘ[μ] β) :
ae_measurable f μ :=
f.measurable.ae_measurable
@[simp] lemma quot_mk_eq_mk (f : α → β) (hf) :
(quot.mk (@setoid.r _ $ μ.ae_eq_setoid β) ⟨f, hf⟩ : α →ₘ[μ] β) = mk f hf :=
rfl
@[simp] lemma mk_eq_mk {f g : α → β} {hf hg} :
(mk f hf : α →ₘ[μ] β) = mk g hg ↔ f =ᵐ[μ] g :=
quotient.eq'
@[simp] lemma mk_coe_fn (f : α →ₘ[μ] β) : mk f f.ae_strongly_measurable = f :=
begin
conv_rhs { rw ← quotient.out_eq' f },
set g : {f : α → β // ae_strongly_measurable f μ} := quotient.out' f with hg,
have : g = ⟨g.1, g.2⟩ := subtype.eq rfl,
rw [this, ← mk, mk_eq_mk],
exact (ae_strongly_measurable.ae_eq_mk _).symm,
end
@[ext] lemma ext {f g : α →ₘ[μ] β} (h : f =ᵐ[μ] g) : f = g :=
by rwa [← f.mk_coe_fn, ← g.mk_coe_fn, mk_eq_mk]
lemma ext_iff {f g : α →ₘ[μ] β} : f = g ↔ f =ᵐ[μ] g :=
⟨λ h, by rw h, λ h, ext h⟩
lemma coe_fn_mk (f : α → β) (hf) : (mk f hf : α →ₘ[μ] β) =ᵐ[μ] f :=
begin
apply (ae_strongly_measurable.ae_eq_mk _).symm.trans,
exact @quotient.mk_out' _ (μ.ae_eq_setoid β) (⟨f, hf⟩ : {f // ae_strongly_measurable f μ})
end
@[elab_as_eliminator]
lemma induction_on (f : α →ₘ[μ] β) {p : (α →ₘ[μ] β) → Prop} (H : ∀ f hf, p (mk f hf)) : p f :=
quotient.induction_on' f $ subtype.forall.2 H
@[elab_as_eliminator]
lemma induction_on₂ {α' β' : Type*} [measurable_space α'] [topological_space β']
{μ' : measure α'}
(f : α →ₘ[μ] β) (f' : α' →ₘ[μ'] β') {p : (α →ₘ[μ] β) → (α' →ₘ[μ'] β') → Prop}
(H : ∀ f hf f' hf', p (mk f hf) (mk f' hf')) :
p f f' :=
induction_on f $ λ f hf, induction_on f' $ H f hf
@[elab_as_eliminator]
lemma induction_on₃ {α' β' : Type*} [measurable_space α'] [topological_space β']
{μ' : measure α'}
{α'' β'' : Type*} [measurable_space α''] [topological_space β'']
{μ'' : measure α''}
(f : α →ₘ[μ] β) (f' : α' →ₘ[μ'] β') (f'' : α'' →ₘ[μ''] β'')
{p : (α →ₘ[μ] β) → (α' →ₘ[μ'] β') → (α'' →ₘ[μ''] β'') → Prop}
(H : ∀ f hf f' hf' f'' hf'', p (mk f hf) (mk f' hf') (mk f'' hf'')) :
p f f' f'' :=
induction_on f $ λ f hf, induction_on₂ f' f'' $ H f hf
/-- Given a continuous function `g : β → γ`, and an almost everywhere equal function `[f] : α →ₘ β`,
return the equivalence class of `g ∘ f`, i.e., the almost everywhere equal function
`[g ∘ f] : α →ₘ γ`. -/
def comp (g : β → γ) (hg : continuous g) (f : α →ₘ[μ] β) : α →ₘ[μ] γ :=
quotient.lift_on' f (λ f, mk (g ∘ (f : α → β)) (hg.comp_ae_strongly_measurable f.2)) $
λ f f' H, mk_eq_mk.2 $ H.fun_comp g
@[simp] lemma comp_mk (g : β → γ) (hg : continuous g)
(f : α → β) (hf) :
comp g hg (mk f hf : α →ₘ[μ] β) = mk (g ∘ f) (hg.comp_ae_strongly_measurable hf) :=
rfl
lemma comp_eq_mk (g : β → γ) (hg : continuous g) (f : α →ₘ[μ] β) :
comp g hg f = mk (g ∘ f) (hg.comp_ae_strongly_measurable f.ae_strongly_measurable) :=
by rw [← comp_mk g hg f f.ae_strongly_measurable, mk_coe_fn]
lemma coe_fn_comp (g : β → γ) (hg : continuous g) (f : α →ₘ[μ] β) :
comp g hg f =ᵐ[μ] g ∘ f :=
by { rw [comp_eq_mk], apply coe_fn_mk }
section comp_measurable
variables [measurable_space β] [metrizable_space β] [borel_space β]
[measurable_space γ] [metrizable_space γ] [opens_measurable_space γ] [second_countable_topology γ]
/-- Given a measurable function `g : β → γ`, and an almost everywhere equal function `[f] : α →ₘ β`,
return the equivalence class of `g ∘ f`, i.e., the almost everywhere equal function
`[g ∘ f] : α →ₘ γ`. This requires that `γ` has a second countable topology. -/
def comp_measurable
(g : β → γ) (hg : measurable g) (f : α →ₘ[μ] β) : α →ₘ[μ] γ :=
quotient.lift_on' f (λ f', mk (g ∘ (f' : α → β))
(hg.comp_ae_measurable f'.2.ae_measurable).ae_strongly_measurable) $
λ f f' H, mk_eq_mk.2 $ H.fun_comp g
@[simp] lemma comp_measurable_mk (g : β → γ) (hg : measurable g)
(f : α → β) (hf : ae_strongly_measurable f μ) :
comp_measurable g hg (mk f hf : α →ₘ[μ] β) =
mk (g ∘ f) (hg.comp_ae_measurable hf.ae_measurable).ae_strongly_measurable :=
rfl
lemma comp_measurable_eq_mk (g : β → γ) (hg : measurable g) (f : α →ₘ[μ] β) :
comp_measurable g hg f =
mk (g ∘ f) (hg.comp_ae_measurable f.ae_measurable).ae_strongly_measurable :=
by rw [← comp_measurable_mk g hg f f.ae_strongly_measurable, mk_coe_fn]
lemma coe_fn_comp_measurable (g : β → γ) (hg : measurable g) (f : α →ₘ[μ] β) :
comp_measurable g hg f =ᵐ[μ] g ∘ f :=
by { rw [comp_measurable_eq_mk], apply coe_fn_mk }
end comp_measurable
/-- The class of `x ↦ (f x, g x)`. -/
def pair (f : α →ₘ[μ] β) (g : α →ₘ[μ] γ) : α →ₘ[μ] β × γ :=
quotient.lift_on₂' f g (λ f g, mk (λ x, (f.1 x, g.1 x)) (f.2.prod_mk g.2)) $
λ f g f' g' Hf Hg, mk_eq_mk.2 $ Hf.prod_mk Hg
@[simp] lemma pair_mk_mk (f : α → β) (hf) (g : α → γ) (hg) :
(mk f hf : α →ₘ[μ] β).pair (mk g hg) = mk (λ x, (f x, g x)) (hf.prod_mk hg) :=
rfl
lemma pair_eq_mk (f : α →ₘ[μ] β) (g : α →ₘ[μ] γ) :
f.pair g = mk (λ x, (f x, g x)) (f.ae_strongly_measurable.prod_mk g.ae_strongly_measurable) :=
by simp only [← pair_mk_mk, mk_coe_fn]
lemma coe_fn_pair (f : α →ₘ[μ] β) (g : α →ₘ[μ] γ) :
f.pair g =ᵐ[μ] (λ x, (f x, g x)) :=
by { rw pair_eq_mk, apply coe_fn_mk }
/-- Given a continuous function `g : β → γ → δ`, and almost everywhere equal functions
`[f₁] : α →ₘ β` and `[f₂] : α →ₘ γ`, return the equivalence class of the function
`λ a, g (f₁ a) (f₂ a)`, i.e., the almost everywhere equal function
`[λ a, g (f₁ a) (f₂ a)] : α →ₘ γ` -/
def comp₂ (g : β → γ → δ)
(hg : continuous (uncurry g)) (f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) : α →ₘ[μ] δ :=
comp _ hg (f₁.pair f₂)
@[simp] lemma comp₂_mk_mk
(g : β → γ → δ) (hg : continuous (uncurry g)) (f₁ : α → β) (f₂ : α → γ) (hf₁ hf₂) :
comp₂ g hg (mk f₁ hf₁ : α →ₘ[μ] β) (mk f₂ hf₂) =
mk (λ a, g (f₁ a) (f₂ a)) (hg.comp_ae_strongly_measurable (hf₁.prod_mk hf₂)) :=
rfl
lemma comp₂_eq_pair
(g : β → γ → δ) (hg : continuous (uncurry g)) (f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) :
comp₂ g hg f₁ f₂ = comp _ hg (f₁.pair f₂) :=
rfl
lemma comp₂_eq_mk
(g : β → γ → δ) (hg : continuous (uncurry g)) (f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) :
comp₂ g hg f₁ f₂ = mk (λ a, g (f₁ a) (f₂ a)) (hg.comp_ae_strongly_measurable
(f₁.ae_strongly_measurable.prod_mk f₂.ae_strongly_measurable)) :=
by rw [comp₂_eq_pair, pair_eq_mk, comp_mk]; refl
lemma coe_fn_comp₂
(g : β → γ → δ) (hg : continuous (uncurry g)) (f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) :
comp₂ g hg f₁ f₂ =ᵐ[μ] λ a, g (f₁ a) (f₂ a) :=
by { rw comp₂_eq_mk, apply coe_fn_mk }
section
variables [measurable_space β] [metrizable_space β] [borel_space β] [second_countable_topology β]
[measurable_space γ] [metrizable_space γ] [borel_space γ] [second_countable_topology γ]
[measurable_space δ] [metrizable_space δ] [opens_measurable_space δ] [second_countable_topology δ]
/-- Given a measurable function `g : β → γ → δ`, and almost everywhere equal functions
`[f₁] : α →ₘ β` and `[f₂] : α →ₘ γ`, return the equivalence class of the function
`λ a, g (f₁ a) (f₂ a)`, i.e., the almost everywhere equal function
`[λ a, g (f₁ a) (f₂ a)] : α →ₘ γ`. This requires `δ` to have second-countable topology. -/
def comp₂_measurable (g : β → γ → δ)
(hg : measurable (uncurry g)) (f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) : α →ₘ[μ] δ :=
comp_measurable _ hg (f₁.pair f₂)
@[simp] lemma comp₂_measurable_mk_mk
(g : β → γ → δ) (hg : measurable (uncurry g)) (f₁ : α → β) (f₂ : α → γ) (hf₁ hf₂) :
comp₂_measurable g hg (mk f₁ hf₁ : α →ₘ[μ] β) (mk f₂ hf₂) =
mk (λ a, g (f₁ a) (f₂ a))
(hg.comp_ae_measurable (hf₁.ae_measurable.prod_mk hf₂.ae_measurable)).ae_strongly_measurable :=
rfl
lemma comp₂_measurable_eq_pair
(g : β → γ → δ) (hg : measurable (uncurry g)) (f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) :
comp₂_measurable g hg f₁ f₂ = comp_measurable _ hg (f₁.pair f₂) :=
rfl
lemma comp₂_measurable_eq_mk
(g : β → γ → δ) (hg : measurable (uncurry g)) (f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) :
comp₂_measurable g hg f₁ f₂ = mk (λ a, g (f₁ a) (f₂ a)) (hg.comp_ae_measurable
(f₁.ae_measurable.prod_mk f₂.ae_measurable)).ae_strongly_measurable :=
by rw [comp₂_measurable_eq_pair, pair_eq_mk, comp_measurable_mk]; refl
lemma coe_fn_comp₂_measurable
(g : β → γ → δ) (hg : measurable (uncurry g)) (f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) :
comp₂_measurable g hg f₁ f₂ =ᵐ[μ] λ a, g (f₁ a) (f₂ a) :=
by { rw comp₂_measurable_eq_mk, apply coe_fn_mk }
end
/-- Interpret `f : α →ₘ[μ] β` as a germ at `μ.ae` forgetting that `f` is almost everywhere
strongly measurable. -/
def to_germ (f : α →ₘ[μ] β) : germ μ.ae β :=
quotient.lift_on' f (λ f, ((f : α → β) : germ μ.ae β)) $ λ f g H, germ.coe_eq.2 H
@[simp] lemma mk_to_germ (f : α → β) (hf) : (mk f hf : α →ₘ[μ] β).to_germ = f := rfl
lemma to_germ_eq (f : α →ₘ[μ] β) : f.to_germ = (f : α → β) :=
by rw [← mk_to_germ, mk_coe_fn]
lemma to_germ_injective : injective (to_germ : (α →ₘ[μ] β) → germ μ.ae β) :=
λ f g H, ext $ germ.coe_eq.1 $ by rwa [← to_germ_eq, ← to_germ_eq]
lemma comp_to_germ (g : β → γ) (hg : continuous g) (f : α →ₘ[μ] β) :
(comp g hg f).to_germ = f.to_germ.map g :=
induction_on f $ λ f hf, by simp
lemma comp_measurable_to_germ [measurable_space β] [borel_space β] [metrizable_space β]
[metrizable_space γ] [second_countable_topology γ] [measurable_space γ] [opens_measurable_space γ]
(g : β → γ) (hg : measurable g) (f : α →ₘ[μ] β) :
(comp_measurable g hg f).to_germ = f.to_germ.map g :=
induction_on f $ λ f hf, by simp
lemma comp₂_to_germ (g : β → γ → δ) (hg : continuous (uncurry g))
(f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) :
(comp₂ g hg f₁ f₂).to_germ = f₁.to_germ.map₂ g f₂.to_germ :=
induction_on₂ f₁ f₂ $ λ f₁ hf₁ f₂ hf₂, by simp
lemma comp₂_measurable_to_germ
[metrizable_space β] [second_countable_topology β] [measurable_space β] [borel_space β]
[metrizable_space γ] [second_countable_topology γ] [measurable_space γ] [borel_space γ]
[metrizable_space δ] [second_countable_topology δ] [measurable_space δ] [opens_measurable_space δ]
(g : β → γ → δ) (hg : measurable (uncurry g))
(f₁ : α →ₘ[μ] β) (f₂ : α →ₘ[μ] γ) :
(comp₂_measurable g hg f₁ f₂).to_germ = f₁.to_germ.map₂ g f₂.to_germ :=
induction_on₂ f₁ f₂ $ λ f₁ hf₁ f₂ hf₂, by simp
/-- Given a predicate `p` and an equivalence class `[f]`, return true if `p` holds of `f a`
for almost all `a` -/
def lift_pred (p : β → Prop) (f : α →ₘ[μ] β) : Prop := f.to_germ.lift_pred p
/-- Given a relation `r` and equivalence class `[f]` and `[g]`, return true if `r` holds of
`(f a, g a)` for almost all `a` -/
def lift_rel (r : β → γ → Prop) (f : α →ₘ[μ] β) (g : α →ₘ[μ] γ) : Prop :=
f.to_germ.lift_rel r g.to_germ
lemma lift_rel_mk_mk {r : β → γ → Prop} {f : α → β} {g : α → γ} {hf hg} :
lift_rel r (mk f hf : α →ₘ[μ] β) (mk g hg) ↔ ∀ᵐ a ∂μ, r (f a) (g a) :=
iff.rfl
lemma lift_rel_iff_coe_fn {r : β → γ → Prop} {f : α →ₘ[μ] β} {g : α →ₘ[μ] γ} :
lift_rel r f g ↔ ∀ᵐ a ∂μ, r (f a) (g a) :=
by rw [← lift_rel_mk_mk, mk_coe_fn, mk_coe_fn]
section order
instance [preorder β] : preorder (α →ₘ[μ] β) := preorder.lift to_germ
@[simp] lemma mk_le_mk [preorder β] {f g : α → β} (hf hg) :
(mk f hf : α →ₘ[μ] β) ≤ mk g hg ↔ f ≤ᵐ[μ] g :=
iff.rfl
@[simp, norm_cast] lemma coe_fn_le [preorder β] {f g : α →ₘ[μ] β} :
(f : α → β) ≤ᵐ[μ] g ↔ f ≤ g :=
lift_rel_iff_coe_fn.symm
instance [partial_order β] : partial_order (α →ₘ[μ] β) :=
partial_order.lift to_germ to_germ_injective
section lattice
section sup
variables [semilattice_sup β] [measurable_space β] [second_countable_topology β]
[metrizable_space β] [borel_space β] [has_measurable_sup₂ β]
instance : has_sup (α →ₘ[μ] β) :=
{ sup := λ f g, ae_eq_fun.comp₂_measurable (⊔) measurable_sup f g }
lemma coe_fn_sup (f g : α →ₘ[μ] β) : ⇑(f ⊔ g) =ᵐ[μ] λ x, f x ⊔ g x :=
coe_fn_comp₂_measurable _ _ _ _
protected lemma le_sup_left (f g : α →ₘ[μ] β) : f ≤ f ⊔ g :=
by { rw ← coe_fn_le, filter_upwards [coe_fn_sup f g] with _ ha, rw ha, exact le_sup_left, }
protected lemma le_sup_right (f g : α →ₘ[μ] β) : g ≤ f ⊔ g :=
by { rw ← coe_fn_le, filter_upwards [coe_fn_sup f g] with _ ha, rw ha, exact le_sup_right, }
protected lemma sup_le (f g f' : α →ₘ[μ] β) (hf : f ≤ f') (hg : g ≤ f') : f ⊔ g ≤ f' :=
begin
rw ← coe_fn_le at hf hg ⊢,
filter_upwards [hf, hg, coe_fn_sup f g] with _ haf hag ha_sup,
rw ha_sup,
exact sup_le haf hag,
end
end sup
section inf
variables [semilattice_inf β] [measurable_space β] [second_countable_topology β]
[metrizable_space β] [borel_space β] [has_measurable_inf₂ β]
instance : has_inf (α →ₘ[μ] β) :=
{ inf := λ f g, ae_eq_fun.comp₂_measurable (⊓) measurable_inf f g }
lemma coe_fn_inf (f g : α →ₘ[μ] β) : ⇑(f ⊓ g) =ᵐ[μ] λ x, f x ⊓ g x :=
coe_fn_comp₂_measurable _ _ _ _
protected lemma inf_le_left (f g : α →ₘ[μ] β) : f ⊓ g ≤ f :=
by { rw ← coe_fn_le, filter_upwards [coe_fn_inf f g] with _ ha, rw ha, exact inf_le_left, }
protected lemma inf_le_right (f g : α →ₘ[μ] β) : f ⊓ g ≤ g :=
by { rw ← coe_fn_le, filter_upwards [coe_fn_inf f g] with _ ha, rw ha, exact inf_le_right, }
protected lemma le_inf (f' f g : α →ₘ[μ] β) (hf : f' ≤ f) (hg : f' ≤ g) : f' ≤ f ⊓ g :=
begin
rw ← coe_fn_le at hf hg ⊢,
filter_upwards [hf, hg, coe_fn_inf f g] with _ haf hag ha_inf,
rw ha_inf,
exact le_inf haf hag,
end
end inf
instance [lattice β] [measurable_space β] [second_countable_topology β]
[metrizable_space β] [borel_space β]
[has_measurable_sup₂ β] [has_measurable_inf₂ β] : lattice (α →ₘ[μ] β) :=
{ sup := has_sup.sup,
le_sup_left := ae_eq_fun.le_sup_left,
le_sup_right := ae_eq_fun.le_sup_right,
sup_le := ae_eq_fun.sup_le,
inf := has_inf.inf,
inf_le_left := ae_eq_fun.inf_le_left,
inf_le_right := ae_eq_fun.inf_le_right,
le_inf := ae_eq_fun.le_inf,
..ae_eq_fun.partial_order}
end lattice
end order
variable (α)
/-- The equivalence class of a constant function: `[λ a:α, b]`, based on the equivalence relation of
being almost everywhere equal -/
def const (b : β) : α →ₘ[μ] β := mk (λ a:α, b) ae_strongly_measurable_const
lemma coe_fn_const (b : β) : (const α b : α →ₘ[μ] β) =ᵐ[μ] function.const α b :=
coe_fn_mk _ _
variable {α}
instance [inhabited β] : inhabited (α →ₘ[μ] β) := ⟨const α default⟩
@[to_additive] instance [has_one β] : has_one (α →ₘ[μ] β) := ⟨const α 1⟩
@[to_additive] lemma one_def [has_one β] :
(1 : α →ₘ[μ] β) = mk (λ a:α, 1) ae_strongly_measurable_const := rfl
@[to_additive] lemma coe_fn_one [has_one β] : ⇑(1 : α →ₘ[μ] β) =ᵐ[μ] 1 := coe_fn_const _ _
@[simp, to_additive] lemma one_to_germ [has_one β] : (1 : α →ₘ[μ] β).to_germ = 1 := rfl
-- Note we set up the scalar actions before the `monoid` structures in case we want to
-- try to override the `nsmul` or `zsmul` fields in future.
section has_scalar
variables {𝕜 𝕜' : Type*}
variables [has_scalar 𝕜 γ] [has_continuous_const_smul 𝕜 γ]
variables [has_scalar 𝕜' γ] [has_continuous_const_smul 𝕜' γ]
instance : has_scalar 𝕜 (α →ₘ[μ] γ) :=
⟨λ c f, comp ((•) c) (continuous_id.const_smul c) f⟩
@[simp] lemma smul_mk (c : 𝕜) (f : α → γ) (hf : ae_strongly_measurable f μ) :
c • (mk f hf : α →ₘ[μ] γ) = mk (c • f) (hf.const_smul _) :=
rfl
lemma coe_fn_smul (c : 𝕜) (f : α →ₘ[μ] γ) : ⇑(c • f) =ᵐ[μ] c • f := coe_fn_comp _ _ _
lemma smul_to_germ (c : 𝕜) (f : α →ₘ[μ] γ) : (c • f).to_germ = c • f.to_germ :=
comp_to_germ _ _ _
instance [smul_comm_class 𝕜 𝕜' γ] : smul_comm_class 𝕜 𝕜' (α →ₘ[μ] γ) :=
⟨λ a b f, induction_on f $ λ f hf, by simp_rw [smul_mk, smul_comm]⟩
instance [has_scalar 𝕜 𝕜'] [is_scalar_tower 𝕜 𝕜' γ] : is_scalar_tower 𝕜 𝕜' (α →ₘ[μ] γ) :=
⟨λ a b f, induction_on f $ λ f hf, by simp_rw [smul_mk, smul_assoc]⟩
instance [has_scalar 𝕜ᵐᵒᵖ γ] [is_central_scalar 𝕜 γ] : is_central_scalar 𝕜 (α →ₘ[μ] γ) :=
⟨λ a f, induction_on f $ λ f hf, by simp_rw [smul_mk, op_smul_eq_smul]⟩
end has_scalar
section has_mul
variables [has_mul γ] [has_continuous_mul γ]
@[to_additive]
instance : has_mul (α →ₘ[μ] γ) := ⟨comp₂ (*) continuous_mul⟩
@[simp, to_additive] lemma mk_mul_mk (f g : α → γ) (hf : ae_strongly_measurable f μ)
(hg : ae_strongly_measurable g μ) :
(mk f hf : α →ₘ[μ] γ) * (mk g hg) = mk (f * g) (hf.mul hg) :=
rfl
@[to_additive] lemma coe_fn_mul (f g : α →ₘ[μ] γ) : ⇑(f * g) =ᵐ[μ] f * g := coe_fn_comp₂ _ _ _ _
@[simp, to_additive] lemma mul_to_germ (f g : α →ₘ[μ] γ) :
(f * g).to_germ = f.to_germ * g.to_germ :=
comp₂_to_germ _ _ _ _
end has_mul
instance [add_monoid γ] [has_continuous_add γ] : add_monoid (α →ₘ[μ] γ) :=
to_germ_injective.add_monoid to_germ zero_to_germ add_to_germ (λ _ _, smul_to_germ _ _)
instance [add_comm_monoid γ] [has_continuous_add γ] : add_comm_monoid (α →ₘ[μ] γ) :=
to_germ_injective.add_comm_monoid to_germ zero_to_germ add_to_germ (λ _ _, smul_to_germ _ _)
section monoid
variables [monoid γ] [has_continuous_mul γ]
instance : has_pow (α →ₘ[μ] γ) ℕ := ⟨λ f n, comp _ (continuous_pow n) f⟩
@[simp] lemma mk_pow (f : α → γ) (hf) (n : ℕ) :
(mk f hf : α →ₘ[μ] γ) ^ n =
mk (f ^ n) ((_root_.continuous_pow n).comp_ae_strongly_measurable hf) :=
rfl
lemma coe_fn_pow (f : α →ₘ[μ] γ) (n : ℕ) : ⇑(f ^ n) =ᵐ[μ] f ^ n :=
coe_fn_comp _ _ _
@[simp] lemma pow_to_germ (f : α →ₘ[μ] γ) (n : ℕ) :
(f ^ n).to_germ = f.to_germ ^ n :=
comp_to_germ _ _ _
@[to_additive]
instance : monoid (α →ₘ[μ] γ) :=
to_germ_injective.monoid to_germ one_to_germ mul_to_germ pow_to_germ
/-- `ae_eq_fun.to_germ` as a `monoid_hom`. -/
@[to_additive "`ae_eq_fun.to_germ` as an `add_monoid_hom`.", simps]
def to_germ_monoid_hom : (α →ₘ[μ] γ) →* μ.ae.germ γ :=
{ to_fun := to_germ,
map_one' := one_to_germ,
map_mul' := mul_to_germ }
end monoid
@[to_additive]
instance [comm_monoid γ] [has_continuous_mul γ] : comm_monoid (α →ₘ[μ] γ) :=
to_germ_injective.comm_monoid to_germ one_to_germ mul_to_germ pow_to_germ
section group
variables [group γ] [topological_group γ]
section inv
@[to_additive] instance : has_inv (α →ₘ[μ] γ) := ⟨comp has_inv.inv continuous_inv⟩
@[simp, to_additive] lemma inv_mk (f : α → γ) (hf) : (mk f hf : α →ₘ[μ] γ)⁻¹ = mk f⁻¹ hf.inv := rfl
@[to_additive] lemma coe_fn_inv (f : α →ₘ[μ] γ) : ⇑(f⁻¹) =ᵐ[μ] f⁻¹ := coe_fn_comp _ _ _
@[to_additive] lemma inv_to_germ (f : α →ₘ[μ] γ) : (f⁻¹).to_germ = f.to_germ⁻¹ := comp_to_germ _ _ _
end inv
section div
@[to_additive] instance : has_div (α →ₘ[μ] γ) := ⟨comp₂ has_div.div continuous_div'⟩
@[simp, to_additive] lemma mk_div (f g : α → γ)
(hf : ae_strongly_measurable f μ) (hg : ae_strongly_measurable g μ) :
mk (f / g) (hf.div hg) = (mk f hf : α →ₘ[μ] γ) / (mk g hg) :=
rfl
@[to_additive] lemma coe_fn_div (f g : α →ₘ[μ] γ) : ⇑(f / g) =ᵐ[μ] f / g := coe_fn_comp₂ _ _ _ _
@[to_additive] lemma div_to_germ (f g : α →ₘ[μ] γ) : (f / g).to_germ = f.to_germ / g.to_germ :=
comp₂_to_germ _ _ _ _
end div
section zpow
instance has_int_pow : has_pow (α →ₘ[μ] γ) ℤ :=
⟨λ f n, comp _ (continuous_zpow n) f⟩
@[simp] lemma mk_zpow (f : α → γ) (hf) (n : ℤ) :
(mk f hf : α →ₘ[μ] γ) ^ n = mk (f ^ n) ((continuous_zpow n).comp_ae_strongly_measurable hf) :=
rfl
lemma coe_fn_zpow (f : α →ₘ[μ] γ) (n : ℤ) : ⇑(f ^ n) =ᵐ[μ] f ^ n :=
coe_fn_comp _ _ _
@[simp] lemma zpow_to_germ (f : α →ₘ[μ] γ) (n : ℤ) :
(f ^ n).to_germ = f.to_germ ^ n :=
comp_to_germ _ _ _
end zpow
end group
instance [add_group γ] [topological_add_group γ] :
add_group (α →ₘ[μ] γ) :=
to_germ_injective.add_group to_germ zero_to_germ add_to_germ neg_to_germ sub_to_germ
(λ _ _, smul_to_germ _ _) (λ _ _, smul_to_germ _ _)
instance [add_comm_group γ] [topological_add_group γ] :
add_comm_group (α →ₘ[μ] γ) :=
to_germ_injective.add_comm_group to_germ zero_to_germ add_to_germ neg_to_germ sub_to_germ
(λ _ _, smul_to_germ _ _) (λ _ _, smul_to_germ _ _)
@[to_additive]
instance [group γ] [topological_group γ] : group (α →ₘ[μ] γ) :=
to_germ_injective.group _ one_to_germ mul_to_germ inv_to_germ div_to_germ pow_to_germ zpow_to_germ
@[to_additive]
instance [comm_group γ] [topological_group γ] : comm_group (α →ₘ[μ] γ) :=
to_germ_injective.comm_group _
one_to_germ mul_to_germ inv_to_germ div_to_germ pow_to_germ zpow_to_germ
section module
variables {𝕜 : Type*}
instance [monoid 𝕜] [mul_action 𝕜 γ] [has_continuous_const_smul 𝕜 γ] :
mul_action 𝕜 (α →ₘ[μ] γ) :=
to_germ_injective.mul_action to_germ smul_to_germ
instance [monoid 𝕜] [add_monoid γ] [has_continuous_add γ]
[distrib_mul_action 𝕜 γ] [has_continuous_const_smul 𝕜 γ] :
distrib_mul_action 𝕜 (α →ₘ[μ] γ) :=
to_germ_injective.distrib_mul_action (to_germ_add_monoid_hom : (α →ₘ[μ] γ) →+ _)
(λ c : 𝕜, smul_to_germ c)
instance [semiring 𝕜] [add_comm_monoid γ] [has_continuous_add γ] [module 𝕜 γ]
[has_continuous_const_smul 𝕜 γ] :
module 𝕜 (α →ₘ[μ] γ) :=
to_germ_injective.module 𝕜 (to_germ_add_monoid_hom : (α →ₘ[μ] γ) →+ _) smul_to_germ
end module
open ennreal
/-- For `f : α → ℝ≥0∞`, define `∫ [f]` to be `∫ f` -/
def lintegral (f : α →ₘ[μ] ℝ≥0∞) : ℝ≥0∞ :=
quotient.lift_on' f (λ f, ∫⁻ a, (f : α → ℝ≥0∞) a ∂μ) (assume f g, lintegral_congr_ae)
@[simp] lemma lintegral_mk (f : α → ℝ≥0∞) (hf) :
(mk f hf : α →ₘ[μ] ℝ≥0∞).lintegral = ∫⁻ a, f a ∂μ := rfl
lemma lintegral_coe_fn (f : α →ₘ[μ] ℝ≥0∞) : ∫⁻ a, f a ∂μ = f.lintegral :=
by rw [← lintegral_mk, mk_coe_fn]
@[simp] lemma lintegral_zero : lintegral (0 : α →ₘ[μ] ℝ≥0∞) = 0 := lintegral_zero
@[simp] lemma lintegral_eq_zero_iff {f : α →ₘ[μ] ℝ≥0∞} : lintegral f = 0 ↔ f = 0 :=
induction_on f $ λ f hf, (lintegral_eq_zero_iff' hf.ae_measurable).trans mk_eq_mk.symm
lemma lintegral_add (f g : α →ₘ[μ] ℝ≥0∞) : lintegral (f + g) = lintegral f + lintegral g :=
induction_on₂ f g $ λ f hf g hg, by simp [lintegral_add' hf.ae_measurable hg.ae_measurable]
lemma lintegral_mono {f g : α →ₘ[μ] ℝ≥0∞} : f ≤ g → lintegral f ≤ lintegral g :=
induction_on₂ f g $ λ f hf g hg hfg, lintegral_mono_ae hfg
section pos_part
variables [linear_order γ] [order_closed_topology γ] [has_zero γ]
/-- Positive part of an `ae_eq_fun`. -/
def pos_part (f : α →ₘ[μ] γ) : α →ₘ[μ] γ :=
comp (λ x, max x 0) (continuous_id.max continuous_const) f
@[simp] lemma pos_part_mk (f : α → γ) (hf) :
pos_part (mk f hf : α →ₘ[μ] γ) =
mk (λ x, max (f x) 0) ((continuous_id.max continuous_const).comp_ae_strongly_measurable hf) :=
rfl
lemma coe_fn_pos_part (f : α →ₘ[μ] γ) : ⇑(pos_part f) =ᵐ[μ] (λ a, max (f a) 0) :=
coe_fn_comp _ _ _
end pos_part
end ae_eq_fun
end measure_theory
namespace continuous_map
open measure_theory
variables [topological_space α] [borel_space α] (μ)
variables [topological_space β] [second_countable_topology_either α β] [metrizable_space β]
/-- The equivalence class of `μ`-almost-everywhere measurable functions associated to a continuous
map. -/
def to_ae_eq_fun (f : C(α, β)) : α →ₘ[μ] β :=
ae_eq_fun.mk f f.continuous.ae_strongly_measurable
lemma coe_fn_to_ae_eq_fun (f : C(α, β)) : f.to_ae_eq_fun μ =ᵐ[μ] f :=
ae_eq_fun.coe_fn_mk f _
variables [group β] [topological_group β]
/-- The `mul_hom` from the group of continuous maps from `α` to `β` to the group of equivalence
classes of `μ`-almost-everywhere measurable functions. -/
@[to_additive "The `add_hom` from the group of continuous maps from `α` to `β` to the group of
equivalence classes of `μ`-almost-everywhere measurable functions."]
def to_ae_eq_fun_mul_hom : C(α, β) →* α →ₘ[μ] β :=
{ to_fun := continuous_map.to_ae_eq_fun μ,
map_one' := rfl,
map_mul' := λ f g, ae_eq_fun.mk_mul_mk _ _
f.continuous.ae_strongly_measurable g.continuous.ae_strongly_measurable }
variables {𝕜 : Type*} [semiring 𝕜]
variables [topological_space γ] [metrizable_space γ] [add_comm_group γ]
[module 𝕜 γ] [topological_add_group γ] [has_continuous_const_smul 𝕜 γ]
[second_countable_topology_either α γ]
/-- The linear map from the group of continuous maps from `α` to `β` to the group of equivalence
classes of `μ`-almost-everywhere measurable functions. -/
def to_ae_eq_fun_linear_map : C(α, γ) →ₗ[𝕜] α →ₘ[μ] γ :=
{ map_smul' := λ c f, ae_eq_fun.smul_mk c f f.continuous.ae_strongly_measurable,
.. to_ae_eq_fun_add_hom μ }
end continuous_map
|
28ecbac38a47a93445a2ca662b1bfd944894349b | 9dc8cecdf3c4634764a18254e94d43da07142918 | /src/algebra/group/conj.lean | 03e4dab4943475bb1c15c868cbbe0236bceef352 | [
"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 | 10,735 | lean | /-
Copyright (c) 2018 Patrick Massot. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Patrick Massot, Chris Hughes, Michael Howes
-/
import algebra.group.semiconj
import algebra.group_with_zero.basic
import algebra.hom.aut
import algebra.hom.group
import data.fintype.basic
/-!
# Conjugacy of group elements
See also `mul_aut.conj` and `quandle.conj`.
-/
universes u v
variables {α : Type u} {β : Type v}
section monoid
variables [monoid α] [monoid β]
/-- We say that `a` is conjugate to `b` if for some unit `c` we have `c * a * c⁻¹ = b`. -/
def is_conj (a b : α) := ∃ c : αˣ, semiconj_by ↑c a b
@[refl] lemma is_conj.refl (a : α) : is_conj a a :=
⟨1, semiconj_by.one_left a⟩
@[symm] lemma is_conj.symm {a b : α} : is_conj a b → is_conj b a
| ⟨c, hc⟩ := ⟨c⁻¹, hc.units_inv_symm_left⟩
lemma is_conj_comm {g h : α} : is_conj g h ↔ is_conj h g :=
⟨is_conj.symm, is_conj.symm⟩
@[trans] lemma is_conj.trans {a b c : α} : is_conj a b → is_conj b c → is_conj a c
| ⟨c₁, hc₁⟩ ⟨c₂, hc₂⟩ := ⟨c₂ * c₁, hc₂.mul_left hc₁⟩
@[simp] lemma is_conj_iff_eq {α : Type*} [comm_monoid α] {a b : α} : is_conj a b ↔ a = b :=
⟨λ ⟨c, hc⟩, begin
rw [semiconj_by, mul_comm, ← units.mul_inv_eq_iff_eq_mul, mul_assoc, c.mul_inv, mul_one] at hc,
exact hc,
end, λ h, by rw h⟩
protected lemma monoid_hom.map_is_conj (f : α →* β) {a b : α} : is_conj a b → is_conj (f a) (f b)
| ⟨c, hc⟩ := ⟨units.map f c, by rw [units.coe_map, semiconj_by, ← f.map_mul, hc.eq, f.map_mul]⟩
end monoid
section cancel_monoid
variables [cancel_monoid α]
-- These lemmas hold for `right_cancel_monoid` with the current proofs, but for the sake of
-- not duplicating code (these lemmas also hold for `left_cancel_monoids`) we leave these
-- not generalised.
@[simp] lemma is_conj_one_right {a : α} : is_conj 1 a ↔ a = 1 :=
⟨λ ⟨c, hc⟩, mul_right_cancel (hc.symm.trans ((mul_one _).trans (one_mul _).symm)), λ h, by rw [h]⟩
@[simp] lemma is_conj_one_left {a : α} : is_conj a 1 ↔ a = 1 :=
calc is_conj a 1 ↔ is_conj 1 a : ⟨is_conj.symm, is_conj.symm⟩
... ↔ a = 1 : is_conj_one_right
end cancel_monoid
section group
variables [group α]
@[simp] lemma is_conj_iff {a b : α} :
is_conj a b ↔ ∃ c : α, c * a * c⁻¹ = b :=
⟨λ ⟨c, hc⟩, ⟨c, mul_inv_eq_iff_eq_mul.2 hc⟩, λ ⟨c, hc⟩,
⟨⟨c, c⁻¹, mul_inv_self c, inv_mul_self c⟩, mul_inv_eq_iff_eq_mul.1 hc⟩⟩
@[simp] lemma conj_inv {a b : α} : (b * a * b⁻¹)⁻¹ = b * a⁻¹ * b⁻¹ :=
((mul_aut.conj b).map_inv a).symm
@[simp] lemma conj_mul {a b c : α} : (b * a * b⁻¹) * (b * c * b⁻¹) = b * (a * c) * b⁻¹ :=
((mul_aut.conj b).map_mul a c).symm
@[simp] lemma conj_pow {i : ℕ} {a b : α} : (a * b * a⁻¹) ^ i = a * (b ^ i) * a⁻¹ :=
begin
induction i with i hi,
{ simp },
{ simp [pow_succ, hi] }
end
@[simp] lemma conj_zpow {i : ℤ} {a b : α} : (a * b * a⁻¹) ^ i = a * (b ^ i) * a⁻¹ :=
begin
induction i,
{ simp },
{ simp [zpow_neg_succ_of_nat, conj_pow] }
end
lemma conj_injective {x : α} : function.injective (λ (g : α), x * g * x⁻¹) :=
(mul_aut.conj x).injective
end group
@[simp] lemma is_conj_iff₀ [group_with_zero α] {a b : α} :
is_conj a b ↔ ∃ c : α, c ≠ 0 ∧ c * a * c⁻¹ = b :=
⟨λ ⟨c, hc⟩, ⟨c, begin
rw [← units.coe_inv, units.mul_inv_eq_iff_eq_mul],
exact ⟨c.ne_zero, hc⟩,
end⟩, λ ⟨c, c0, hc⟩,
⟨units.mk0 c c0, begin
rw [semiconj_by, ← units.mul_inv_eq_iff_eq_mul, units.coe_inv, units.coe_mk0],
exact hc
end⟩⟩
namespace is_conj
/- This small quotient API is largely copied from the API of `associates`;
where possible, try to keep them in sync -/
/-- The setoid of the relation `is_conj` iff there is a unit `u` such that `u * x = y * u` -/
protected def setoid (α : Type*) [monoid α] : setoid α :=
{ r := is_conj, iseqv := ⟨is_conj.refl, λa b, is_conj.symm, λa b c, is_conj.trans⟩ }
end is_conj
local attribute [instance, priority 100] is_conj.setoid
/-- The quotient type of conjugacy classes of a group. -/
def conj_classes (α : Type*) [monoid α] : Type* :=
quotient (is_conj.setoid α)
namespace conj_classes
section monoid
variables [monoid α] [monoid β]
/-- The canonical quotient map from a monoid `α` into the `conj_classes` of `α` -/
protected def mk {α : Type*} [monoid α] (a : α) : conj_classes α :=
⟦a⟧
instance : inhabited (conj_classes α) := ⟨⟦1⟧⟩
theorem mk_eq_mk_iff_is_conj {a b : α} :
conj_classes.mk a = conj_classes.mk b ↔ is_conj a b :=
iff.intro quotient.exact quot.sound
theorem quotient_mk_eq_mk (a : α) : ⟦ a ⟧ = conj_classes.mk a := rfl
theorem quot_mk_eq_mk (a : α) : quot.mk setoid.r a = conj_classes.mk a := rfl
theorem forall_is_conj {p : conj_classes α → Prop} :
(∀a, p a) ↔ (∀a, p (conj_classes.mk a)) :=
iff.intro
(assume h a, h _)
(assume h a, quotient.induction_on a h)
theorem mk_surjective : function.surjective (@conj_classes.mk α _) :=
forall_is_conj.2 (λ a, ⟨a, rfl⟩)
instance : has_one (conj_classes α) := ⟨⟦ 1 ⟧⟩
theorem one_eq_mk_one : (1 : conj_classes α) = conj_classes.mk 1 := rfl
lemma exists_rep (a : conj_classes α) : ∃ a0 : α, conj_classes.mk a0 = a :=
quot.exists_rep a
/-- A `monoid_hom` maps conjugacy classes of one group to conjugacy classes of another. -/
def map (f : α →* β) : conj_classes α → conj_classes β :=
quotient.lift (conj_classes.mk ∘ f) (λ a b ab, mk_eq_mk_iff_is_conj.2 (f.map_is_conj ab))
lemma map_surjective {f : α →* β} (hf : function.surjective f) :
function.surjective (conj_classes.map f) :=
begin
intros b,
obtain ⟨b, rfl⟩ := conj_classes.mk_surjective b,
obtain ⟨a, rfl⟩ := hf b,
exact ⟨conj_classes.mk a, rfl⟩,
end
instance [fintype α] [decidable_rel (is_conj : α → α → Prop)] :
fintype (conj_classes α) :=
quotient.fintype (is_conj.setoid α)
/--
Certain instances trigger further searches when they are considered as candidate instances;
these instances should be assigned a priority lower than the default of 1000 (for example, 900).
The conditions for this rule are as follows:
* a class `C` has instances `instT : C T` and `instT' : C T'`
* types `T` and `T'` are both specializations of another type `S`
* the parameters supplied to `S` to produce `T` are not (fully) determined by `instT`,
instead they have to be found by instance search
If those conditions hold, the instance `instT` should be assigned lower priority.
For example, suppose the search for an instance of `decidable_eq (multiset α)` tries the
candidate instance `con.quotient.decidable_eq (c : con M) : decidable_eq c.quotient`.
Since `multiset` and `con.quotient` are both quotient types, unification will check
that the relations `list.perm` and `c.to_setoid.r` unify. However, `c.to_setoid` depends on
a `has_mul M` instance, so this unification triggers a search for `has_mul (list α)`;
this will traverse all subclasses of `has_mul` before failing.
On the other hand, the search for an instance of `decidable_eq (con.quotient c)` for `c : con M`
can quickly reject the candidate instance `multiset.has_decidable_eq` because the type of
`list.perm : list ?m_1 → list ?m_1 → Prop` does not unify with `M → M → Prop`.
Therefore, we should assign `con.quotient.decidable_eq` a lower priority because it fails slowly.
(In terms of the rules above, `C := decidable_eq`, `T := con.quotient`,
`instT := con.quotient.decidable_eq`, `T' := multiset`, `instT' := multiset.has_decidable_eq`,
and `S := quot`.)
If the type involved is a free variable (rather than an instantiation of some type `S`),
the instance priority should be even lower, see Note [lower instance priority].
-/
library_note "slow-failing instance priority"
@[priority 900] -- see Note [slow-failing instance priority]
instance [decidable_rel (is_conj : α → α → Prop)] : decidable_eq (conj_classes α) :=
quotient.decidable_eq
instance [decidable_eq α] [fintype α] : decidable_rel (is_conj : α → α → Prop) :=
λ a b, by { delta is_conj semiconj_by, apply_instance }
end monoid
section comm_monoid
variable [comm_monoid α]
lemma mk_injective : function.injective (@conj_classes.mk α _) :=
λ _ _, (mk_eq_mk_iff_is_conj.trans is_conj_iff_eq).1
lemma mk_bijective : function.bijective (@conj_classes.mk α _) :=
⟨mk_injective, mk_surjective⟩
/-- The bijection between a `comm_group` and its `conj_classes`. -/
def mk_equiv : α ≃ conj_classes α :=
⟨conj_classes.mk, quotient.lift id (λ (a : α) b, is_conj_iff_eq.1), quotient.lift_mk _ _,
begin
rw [function.right_inverse, function.left_inverse, forall_is_conj],
intro x,
rw [← quotient_mk_eq_mk, ← quotient_mk_eq_mk, quotient.lift_mk, id.def],
end⟩
end comm_monoid
end conj_classes
section monoid
variables [monoid α]
/-- Given an element `a`, `conjugates a` is the set of conjugates. -/
def conjugates_of (a : α) : set α := {b | is_conj a b}
lemma mem_conjugates_of_self {a : α} : a ∈ conjugates_of a := is_conj.refl _
lemma is_conj.conjugates_of_eq {a b : α} (ab : is_conj a b) :
conjugates_of a = conjugates_of b :=
set.ext (λ g, ⟨λ ag, (ab.symm).trans ag, λ bg, ab.trans bg⟩)
lemma is_conj_iff_conjugates_of_eq {a b : α} :
is_conj a b ↔ conjugates_of a = conjugates_of b :=
⟨is_conj.conjugates_of_eq, λ h, begin
have ha := mem_conjugates_of_self,
rwa ← h at ha,
end⟩
instance [fintype α] [decidable_rel (is_conj : α → α → Prop)] {a : α} : fintype (conjugates_of a) :=
@subtype.fintype _ _ (‹decidable_rel is_conj› a) _
end monoid
namespace conj_classes
variables [monoid α]
local attribute [instance] is_conj.setoid
/-- Given a conjugacy class `a`, `carrier a` is the set it represents. -/
def carrier : conj_classes α → set α :=
quotient.lift conjugates_of (λ (a : α) b ab, is_conj.conjugates_of_eq ab)
lemma mem_carrier_mk {a : α} : a ∈ carrier (conj_classes.mk a) := is_conj.refl _
lemma mem_carrier_iff_mk_eq {a : α} {b : conj_classes α} :
a ∈ carrier b ↔ conj_classes.mk a = b :=
begin
revert b,
rw forall_is_conj,
intro b,
rw [carrier, eq_comm, mk_eq_mk_iff_is_conj, ← quotient_mk_eq_mk, quotient.lift_mk],
refl,
end
lemma carrier_eq_preimage_mk {a : conj_classes α} :
a.carrier = conj_classes.mk ⁻¹' {a} :=
set.ext (λ x, mem_carrier_iff_mk_eq)
section fintype
variables [fintype α] [decidable_rel (is_conj : α → α → Prop)]
instance {x : conj_classes α} : fintype (carrier x) :=
quotient.rec_on_subsingleton x $ λ a, conjugates_of.fintype
end fintype
end conj_classes
|
1e31342321a3336b78e2a361f3a2f8b3910b4e69 | 1abd1ed12aa68b375cdef28959f39531c6e95b84 | /src/linear_algebra/general_linear_group.lean | 97b7a54a4dd6ce1ed6ebe19932fe84a277ebe41a | [
"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 | 5,783 | lean | /-
Copyright (c) 2021 Chris Birkbeck. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Chris Birkbeck
-/
import linear_algebra.matrix
import linear_algebra.matrix.nonsingular_inverse
import linear_algebra.special_linear_group
import linear_algebra.determinant
/-!
# The General Linear group $GL(n, R)$
This file defines the elements of the General Linear group `general_linear_group n R`,
consisting of all invertible `n` by `n` `R`-matrices.
## Main definitions
* `matrix.general_linear_group` is the type of matrices over R which are units in the matrix ring.
* `matrix.GL_pos` gives the subgroup of matrices with
positive determinant (over a linear ordered ring).
## Tags
matrix group, group, matrix inverse
-/
namespace matrix
universes u v
open_locale matrix
open linear_map
/-- `GL n R` is the group of `n` by `n` `R`-matrices with unit determinant.
Defined as a subtype of matrices-/
abbreviation general_linear_group (n : Type u) (R : Type v)
[decidable_eq n] [fintype n] [comm_ring R] : Type* := units (matrix n n R)
notation `GL` := general_linear_group
namespace general_linear_group
variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R]
/-- The determinant of a unit matrix is itself a unit. -/
@[simps]
def det : GL n R →* units R :=
{ to_fun := λ A,
{ val := (↑A : matrix n n R).det,
inv := (↑(A⁻¹) : matrix n n R).det,
val_inv := by rw [←det_mul, ←mul_eq_mul, A.mul_inv, det_one],
inv_val := by rw [←det_mul, ←mul_eq_mul, A.inv_mul, det_one]},
map_one' := units.ext det_one,
map_mul' := λ A B, units.ext $ det_mul _ _ }
/--The `GL n R` and `general_linear_group R n` groups are multiplicatively equivalent-/
def to_lin : (GL n R) ≃* (linear_map.general_linear_group R (n → R)) :=
units.map_equiv to_lin_alg_equiv'.to_mul_equiv
/--Given a matrix with invertible determinant we get an element of `GL n R`-/
def mk' (A : matrix n n R) (h : invertible (matrix.det A)) : GL n R :=
unit_of_det_invertible A
/--Given a matrix with unit determinant we get an element of `GL n R`-/
noncomputable def mk'' (A : matrix n n R) (h : is_unit (matrix.det A)) : GL n R :=
nonsing_inv_unit A h
instance coe_fun : has_coe_to_fun (GL n R) (λ _, n → n → R) :=
{ coe := λ A, A.val }
lemma ext_iff (A B : GL n R) : A = B ↔ (∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) :=
units.ext_iff.trans matrix.ext_iff.symm
/-- Not marked `@[ext]` as the `ext` tactic already solves this. -/
lemma ext ⦃A B : GL n R⦄ (h : ∀ i j, (A : matrix n n R) i j = (B : matrix n n R) i j) :
A = B :=
units.ext $ matrix.ext h
section coe_lemmas
variables (A B : GL n R)
@[simp] lemma coe_fn_eq_coe : ⇑A = (↑A : matrix n n R) := rfl
@[simp] lemma coe_mul : ↑(A * B) = (↑A : matrix n n R) ⬝ (↑B : matrix n n R) := rfl
@[simp] lemma coe_one : ↑(1 : GL n R) = (1 : matrix n n R) := rfl
lemma coe_inv : ↑(A⁻¹) = (↑A : matrix n n R)⁻¹ :=
begin
letI := A.invertible,
exact inv_eq_nonsing_inv_of_invertible (↑A : matrix n n R),
end
end coe_lemmas
end general_linear_group
namespace special_linear_group
variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R]
instance has_coe_to_general_linear_group : has_coe (special_linear_group n R) (GL n R) :=
⟨λ A, ⟨↑A, ↑(A⁻¹), congr_arg coe (mul_right_inv A), congr_arg coe (mul_left_inv A)⟩⟩
end special_linear_group
section
variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ]
section
variables (n R)
/-- This is the subgroup of `nxn` matrices with entries over a
linear ordered ring and positive determinant. -/
def GL_pos : subgroup (GL n R) :=
(units.pos_subgroup R).comap general_linear_group.det
end
@[simp] lemma mem_GL_pos (A : GL n R) : A ∈ GL_pos n R ↔ 0 < (A.det : R) := iff.rfl
end
section has_neg
variables {n : Type u} {R : Type v} [decidable_eq n] [fintype n] [linear_ordered_comm_ring R ]
[fact (even (fintype.card n))]
/-- Formal operation of negation on general linear group on even cardinality `n` given by negating
each element. -/
instance : has_neg (GL_pos n R) :=
⟨λ g,
⟨- g,
begin
simp only [mem_GL_pos, general_linear_group.coe_det_apply, units.coe_neg],
have := det_smul g (-1),
simp only [general_linear_group.coe_fn_eq_coe, one_smul, coe_fn_coe_base', neg_smul] at this,
rw this,
simp [nat.neg_one_pow_of_even (fact.out (even (fintype.card n)))],
have gdet := g.property,
simp only [mem_GL_pos, general_linear_group.coe_det_apply, subtype.val_eq_coe] at gdet,
exact gdet,
end⟩⟩
@[simp] lemma GL_pos_coe_neg (g : GL_pos n R) : ↑(- g) = - (↑g : matrix n n R) :=
rfl
@[simp]lemma GL_pos_neg_elt (g : GL_pos n R): ∀ i j, ( ↑(-g): matrix n n R) i j= - (g i j):=
begin
simp [coe_fn_coe_base'],
end
end has_neg
namespace special_linear_group
variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [linear_ordered_comm_ring R]
/-- `special_linear_group n R` embeds into `GL_pos n R` -/
def to_GL_pos : special_linear_group n R →* GL_pos n R :=
{ to_fun := λ A, ⟨(A : GL n R), show 0 < (↑A : matrix n n R).det, from A.prop.symm ▸ zero_lt_one⟩,
map_one' := subtype.ext $ units.ext $ rfl,
map_mul' := λ A₁ A₂, subtype.ext $ units.ext $ rfl }
instance : has_coe (special_linear_group n R) (GL_pos n R) := ⟨to_GL_pos⟩
lemma coe_eq_to_GL_pos : (coe : special_linear_group n R → GL_pos n R) = to_GL_pos := rfl
lemma to_GL_pos_injective :
function.injective (to_GL_pos : special_linear_group n R → GL_pos n R) :=
(show function.injective ((coe : GL_pos n R → matrix n n R) ∘ to_GL_pos),
from subtype.coe_injective).of_comp
end special_linear_group
end matrix
|
81f5395105618b043716ed718b82e5283aaf0e35 | c777c32c8e484e195053731103c5e52af26a25d1 | /src/linear_algebra/quadratic_form/basic.lean | 61d4c6bda127757f0b1566da43372cb31f255388 | [
"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 | 38,021 | lean | /-
Copyright (c) 2020 Anne Baanen. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Anne Baanen, Kexing Ying, Eric Wieser
-/
import algebra.invertible
import linear_algebra.matrix.determinant
import linear_algebra.matrix.bilinear_form
import linear_algebra.matrix.symmetric
/-!
# Quadratic forms
This file defines quadratic forms over a `R`-module `M`.
A quadratic form on a ring `R` is a map `Q : M → R` such that:
* `quadratic_form.map_smul`: `Q (a • x) = a * a * Q x`
* `quadratic_form.polar_add_left`, `quadratic_form.polar_add_right`,
`quadratic_form.polar_smul_left`, `quadratic_form.polar_smul_right`:
the map `quadratic_form.polar Q := λ x y, Q (x + y) - Q x - Q y` is bilinear.
This notion generalizes to semirings using the approach in [izhakian2016][] which requires that
there be a (possibly non-unique) companion bilinear form `B` such that
`∀ x y, Q (x + y) = Q x + Q y + B x y`. Over a ring, this `B` is precisely `quadratic_form.polar Q`.
To build a `quadratic_form` from the `polar` axioms, use `quadratic_form.of_polar`.
Quadratic forms come with a scalar multiplication, `(a • Q) x = Q (a • x) = a * a * Q x`,
and composition with linear maps `f`, `Q.comp f x = Q (f x)`.
## Main definitions
* `quadratic_form.of_polar`: a more familiar constructor that works on rings
* `quadratic_form.associated`: associated bilinear form
* `quadratic_form.pos_def`: positive definite quadratic forms
* `quadratic_form.anisotropic`: anisotropic quadratic forms
* `quadratic_form.discr`: discriminant of a quadratic form
## Main statements
* `quadratic_form.associated_left_inverse`,
* `quadratic_form.associated_right_inverse`: in a commutative ring where 2 has
an inverse, there is a correspondence between quadratic forms and symmetric
bilinear forms
* `bilin_form.exists_orthogonal_basis`: There exists an orthogonal basis with
respect to any nondegenerate, symmetric bilinear form `B`.
## Notation
In this file, the variable `R` is used when a `ring` structure is sufficient and
`R₁` is used when specifically a `comm_ring` is required. This allows us to keep
`[module R M]` and `[module R₁ M]` assumptions in the variables without
confusion between `*` from `ring` and `*` from `comm_ring`.
The variable `S` is used when `R` itself has a `•` action.
## References
* https://en.wikipedia.org/wiki/Quadratic_form
* https://en.wikipedia.org/wiki/Discriminant#Quadratic_forms
## Tags
quadratic form, homogeneous polynomial, quadratic polynomial
-/
universes u v w
variables {S : Type*}
variables {R R₁: Type*} {M : Type*}
open_locale big_operators
section polar
variables [ring R] [comm_ring R₁] [add_comm_group M]
namespace quadratic_form
/-- Up to a factor 2, `Q.polar` is the associated bilinear form for a quadratic form `Q`.
Source of this name: https://en.wikipedia.org/wiki/Quadratic_form#Generalization
-/
def polar (f : M → R) (x y : M) :=
f (x + y) - f x - f y
lemma polar_add (f g : M → R) (x y : M) :
polar (f + g) x y = polar f x y + polar g x y :=
by { simp only [polar, pi.add_apply], abel }
lemma polar_neg (f : M → R) (x y : M) :
polar (-f) x y = - polar f x y :=
by { simp only [polar, pi.neg_apply, sub_eq_add_neg, neg_add] }
lemma polar_smul [monoid S] [distrib_mul_action S R] (f : M → R) (s : S) (x y : M) :
polar (s • f) x y = s • polar f x y :=
by { simp only [polar, pi.smul_apply, smul_sub] }
lemma polar_comm (f : M → R) (x y : M) : polar f x y = polar f y x :=
by rw [polar, polar, add_comm, sub_sub, sub_sub, add_comm (f x) (f y)]
/-- Auxiliary lemma to express bilinearity of `quadratic_form.polar` without subtraction. -/
lemma polar_add_left_iff {f : M → R} {x x' y : M} :
polar f (x + x') y = polar f x y + polar f x' y ↔
f (x + x' + y) + (f x + f x' + f y) = f (x + x') + f (x' + y) + f (y + x) :=
begin
simp only [←add_assoc],
simp only [polar, sub_eq_iff_eq_add, eq_sub_iff_add_eq, sub_add_eq_add_sub, add_sub],
simp only [add_right_comm _ (f y) _, add_right_comm _ (f x') (f x)],
rw [add_comm y x, add_right_comm _ _ (f (x + y)), add_comm _ (f (x + y)),
add_right_comm (f (x + y)), add_left_inj],
end
lemma polar_comp {F : Type*} [ring S] [add_monoid_hom_class F R S] (f : M → R) (g : F) (x y : M) :
polar (g ∘ f) x y = g (polar f x y) :=
by simp only [polar, pi.smul_apply, function.comp_apply, map_sub]
end quadratic_form
end polar
/-- A quadratic form over a module.
For a more familiar constructor when `R` is a ring, see `quadratic_form.of_polar`. -/
structure quadratic_form (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] [module R M] :=
(to_fun : M → R)
(to_fun_smul : ∀ (a : R) (x : M), to_fun (a • x) = a * a * to_fun x)
(exists_companion' : ∃ B : bilin_form R M, ∀ x y, to_fun (x + y) = to_fun x + to_fun y + B x y)
namespace quadratic_form
section fun_like
variables [semiring R] [add_comm_monoid M] [module R M]
variables {Q Q' : quadratic_form R M}
instance fun_like : fun_like (quadratic_form R M) M (λ _, R) :=
{ coe := to_fun,
coe_injective' := λ x y h, by cases x; cases y; congr' }
/-- Helper instance for when there's too many metavariables to apply
`fun_like.has_coe_to_fun` directly. -/
instance : has_coe_to_fun (quadratic_form R M) (λ _, M → R) := ⟨to_fun⟩
variables (Q)
/-- The `simp` normal form for a quadratic form is `coe_fn`, not `to_fun`. -/
@[simp] lemma to_fun_eq_coe : Q.to_fun = ⇑Q := rfl
-- this must come after the coe_to_fun definition
initialize_simps_projections quadratic_form (to_fun → apply)
variables {Q}
@[ext] lemma ext (H : ∀ (x : M), Q x = Q' x) : Q = Q' := fun_like.ext _ _ H
lemma congr_fun (h : Q = Q') (x : M) : Q x = Q' x := fun_like.congr_fun h _
lemma ext_iff : Q = Q' ↔ (∀ x, Q x = Q' x) := fun_like.ext_iff
/-- Copy of a `quadratic_form` with a new `to_fun` equal to the old one. Useful to fix definitional
equalities. -/
protected def copy (Q : quadratic_form R M) (Q' : M → R) (h : Q' = ⇑Q) : quadratic_form R M :=
{ to_fun := Q',
to_fun_smul := h.symm ▸ Q.to_fun_smul,
exists_companion' := h.symm ▸ Q.exists_companion' }
@[simp]
lemma coe_copy (Q : quadratic_form R M) (Q' : M → R) (h : Q' = ⇑Q) : ⇑(Q.copy Q' h) = Q' := rfl
lemma copy_eq (Q : quadratic_form R M) (Q' : M → R) (h : Q' = ⇑Q) : Q.copy Q' h = Q :=
fun_like.ext' h
end fun_like
section semiring
variables [semiring R] [add_comm_monoid M] [module R M]
variables (Q : quadratic_form R M)
lemma map_smul (a : R) (x : M) : Q (a • x) = a * a * Q x := Q.to_fun_smul a x
lemma exists_companion : ∃ B : bilin_form R M, ∀ x y, Q (x + y) = Q x + Q y + B x y :=
Q.exists_companion'
lemma map_add_add_add_map (x y z : M) :
Q (x + y + z) + (Q x + Q y + Q z) = Q (x + y) + Q (y + z) + Q (z + x) :=
begin
obtain ⟨B, h⟩ := Q.exists_companion,
rw [add_comm z x],
simp [h],
abel,
end
lemma map_add_self (x : M) : Q (x + x) = 4 * Q x :=
by { rw [←one_smul R x, ←add_smul, map_smul], norm_num }
@[simp] lemma map_zero : Q 0 = 0 :=
by rw [←@zero_smul R _ _ _ _ (0 : M), map_smul, zero_mul, zero_mul]
instance zero_hom_class : zero_hom_class (quadratic_form R M) M R :=
{ map_zero := map_zero,
..quadratic_form.fun_like }
lemma map_smul_of_tower [comm_semiring S] [algebra S R] [module S M] [is_scalar_tower S R M]
(a : S) (x : M) :
Q (a • x) = (a * a) • Q x :=
by rw [←is_scalar_tower.algebra_map_smul R a x, map_smul, ←ring_hom.map_mul, algebra.smul_def]
end semiring
section ring
variables [ring R] [comm_ring R₁] [add_comm_group M]
variables [module R M] (Q : quadratic_form R M)
@[simp] lemma map_neg (x : M) : Q (-x) = Q x :=
by rw [←@neg_one_smul R _ _ _ _ x, map_smul, neg_one_mul, neg_neg, one_mul]
lemma map_sub (x y : M) : Q (x - y) = Q (y - x) :=
by rw [←neg_sub, map_neg]
@[simp]
lemma polar_zero_left (y : M) : polar Q 0 y = 0 :=
by simp only [polar, zero_add, quadratic_form.map_zero, sub_zero, sub_self]
@[simp]
lemma polar_add_left (x x' y : M) :
polar Q (x + x') y = polar Q x y + polar Q x' y :=
polar_add_left_iff.mpr $ Q.map_add_add_add_map x x' y
@[simp]
lemma polar_smul_left (a : R) (x y : M) :
polar Q (a • x) y = a * polar Q x y :=
begin
obtain ⟨B, h⟩ := Q.exists_companion,
simp_rw [polar, h, Q.map_smul, bilin_form.smul_left, sub_sub, add_sub_cancel'],
end
@[simp]
lemma polar_neg_left (x y : M) :
polar Q (-x) y = -polar Q x y :=
by rw [←neg_one_smul R x, polar_smul_left, neg_one_mul]
@[simp]
lemma polar_sub_left (x x' y : M) :
polar Q (x - x') y = polar Q x y - polar Q x' y :=
by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_left, polar_neg_left]
@[simp]
lemma polar_zero_right (y : M) : polar Q y 0 = 0 :=
by simp only [add_zero, polar, quadratic_form.map_zero, sub_self]
@[simp]
lemma polar_add_right (x y y' : M) :
polar Q x (y + y') = polar Q x y + polar Q x y' :=
by rw [polar_comm Q x, polar_comm Q x, polar_comm Q x, polar_add_left]
@[simp]
lemma polar_smul_right (a : R) (x y : M) :
polar Q x (a • y) = a * polar Q x y :=
by rw [polar_comm Q x, polar_comm Q x, polar_smul_left]
@[simp]
lemma polar_neg_right (x y : M) :
polar Q x (-y) = -polar Q x y :=
by rw [←neg_one_smul R y, polar_smul_right, neg_one_mul]
@[simp]
lemma polar_sub_right (x y y' : M) :
polar Q x (y - y') = polar Q x y - polar Q x y' :=
by rw [sub_eq_add_neg, sub_eq_add_neg, polar_add_right, polar_neg_right]
@[simp]
lemma polar_self (x : M) : polar Q x x = 2 * Q x :=
begin
rw [polar, map_add_self, sub_sub, sub_eq_iff_eq_add, ←two_mul, ←two_mul, ←mul_assoc],
norm_num
end
/-- `quadratic_form.polar` as a bilinear form -/
@[simps]
def polar_bilin : bilin_form R M :=
{ bilin := polar Q,
bilin_add_left := polar_add_left Q,
bilin_smul_left := polar_smul_left Q,
bilin_add_right := λ x y z, by simp_rw [polar_comm _ x, polar_add_left Q],
bilin_smul_right := λ r x y, by simp_rw [polar_comm _ x, polar_smul_left Q] }
variables [comm_semiring S] [algebra S R] [module S M] [is_scalar_tower S R M]
@[simp]
lemma polar_smul_left_of_tower (a : S) (x y : M) :
polar Q (a • x) y = a • polar Q x y :=
by rw [←is_scalar_tower.algebra_map_smul R a x, polar_smul_left, algebra.smul_def]
@[simp]
lemma polar_smul_right_of_tower (a : S) (x y : M) :
polar Q x (a • y) = a • polar Q x y :=
by rw [←is_scalar_tower.algebra_map_smul R a y, polar_smul_right, algebra.smul_def]
/-- An alternative constructor to `quadratic_form.mk`, for rings where `polar` can be used. -/
@[simps]
def of_polar (to_fun : M → R) (to_fun_smul : ∀ (a : R) (x : M), to_fun (a • x) = a * a * to_fun x)
(polar_add_left : ∀ (x x' y : M), polar to_fun (x + x') y = polar to_fun x y + polar to_fun x' y)
(polar_smul_left : ∀ (a : R) (x y : M), polar to_fun (a • x) y = a • polar to_fun x y) :
quadratic_form R M :=
{ to_fun := to_fun,
to_fun_smul := to_fun_smul,
exists_companion' := ⟨
{ bilin := polar to_fun,
bilin_add_left := polar_add_left,
bilin_smul_left := polar_smul_left,
bilin_add_right := λ x y z, by simp_rw [polar_comm _ x, polar_add_left],
bilin_smul_right := λ r x y, by simp_rw [polar_comm _ x, polar_smul_left, smul_eq_mul] },
λ x y, by rw [bilin_form.coe_fn_mk, polar, sub_sub, add_sub_cancel'_right]⟩ }
/-- In a ring the companion bilinear form is unique and equal to `quadratic_form.polar`. -/
lemma some_exists_companion : Q.exists_companion.some = polar_bilin Q :=
bilin_form.ext $ λ x y,
by rw [polar_bilin_apply, polar, Q.exists_companion.some_spec, sub_sub, add_sub_cancel']
end ring
section semiring_operators
variables [semiring R] [add_comm_monoid M] [module R M]
section has_smul
variables [monoid S] [distrib_mul_action S R] [smul_comm_class S R R]
/-- `quadratic_form R M` inherits the scalar action from any algebra over `R`.
When `R` is commutative, this provides an `R`-action via `algebra.id`. -/
instance : has_smul S (quadratic_form R M) :=
⟨ λ a Q,
{ to_fun := a • Q,
to_fun_smul := λ b x, by rw [pi.smul_apply, map_smul, pi.smul_apply, mul_smul_comm],
exists_companion' := let ⟨B, h⟩ := Q.exists_companion in ⟨a • B,
by simp [h]⟩ } ⟩
@[simp] lemma coe_fn_smul (a : S) (Q : quadratic_form R M) : ⇑(a • Q) = a • Q := rfl
@[simp] lemma smul_apply (a : S) (Q : quadratic_form R M) (x : M) :
(a • Q) x = a • Q x := rfl
end has_smul
instance : has_zero (quadratic_form R M) :=
⟨ { to_fun := λ x, 0,
to_fun_smul := λ a x, by simp only [mul_zero],
exists_companion' := ⟨0, λ x y, by simp only [add_zero, bilin_form.zero_apply]⟩ } ⟩
@[simp] lemma coe_fn_zero : ⇑(0 : quadratic_form R M) = 0 := rfl
@[simp] lemma zero_apply (x : M) : (0 : quadratic_form R M) x = 0 := rfl
instance : inhabited (quadratic_form R M) := ⟨0⟩
instance : has_add (quadratic_form R M) :=
⟨ λ Q Q',
{ to_fun := Q + Q',
to_fun_smul := λ a x,
by simp only [pi.add_apply, map_smul, mul_add],
exists_companion' :=
let ⟨B, h⟩ := Q.exists_companion, ⟨B', h'⟩ := Q'.exists_companion in
⟨B + B', λ x y, by simp_rw [pi.add_apply, h, h', bilin_form.add_apply, add_add_add_comm] ⟩ } ⟩
@[simp] lemma coe_fn_add (Q Q' : quadratic_form R M) : ⇑(Q + Q') = Q + Q' := rfl
@[simp] lemma add_apply (Q Q' : quadratic_form R M) (x : M) : (Q + Q') x = Q x + Q' x := rfl
instance : add_comm_monoid (quadratic_form R M) :=
fun_like.coe_injective.add_comm_monoid _ coe_fn_zero coe_fn_add (λ _ _, coe_fn_smul _ _)
/-- `@coe_fn (quadratic_form R M)` as an `add_monoid_hom`.
This API mirrors `add_monoid_hom.coe_fn`. -/
@[simps apply]
def coe_fn_add_monoid_hom : quadratic_form R M →+ (M → R) :=
{ to_fun := coe_fn, map_zero' := coe_fn_zero, map_add' := coe_fn_add }
/-- Evaluation on a particular element of the module `M` is an additive map over quadratic forms. -/
@[simps apply]
def eval_add_monoid_hom (m : M) : quadratic_form R M →+ R :=
(pi.eval_add_monoid_hom _ m).comp coe_fn_add_monoid_hom
section sum
@[simp] lemma coe_fn_sum {ι : Type*} (Q : ι → quadratic_form R M) (s : finset ι) :
⇑(∑ i in s, Q i) = ∑ i in s, Q i :=
(coe_fn_add_monoid_hom : _ →+ (M → R)).map_sum Q s
@[simp] lemma sum_apply {ι : Type*} (Q : ι → quadratic_form R M) (s : finset ι) (x : M) :
(∑ i in s, Q i) x = ∑ i in s, Q i x :=
(eval_add_monoid_hom x : _ →+ R).map_sum Q s
end sum
instance [monoid S] [distrib_mul_action S R] [smul_comm_class S R R] :
distrib_mul_action S (quadratic_form R M) :=
{ mul_smul := λ a b Q, ext (λ x, by simp only [smul_apply, mul_smul]),
one_smul := λ Q, ext (λ x, by simp only [quadratic_form.smul_apply, one_smul]),
smul_add := λ a Q Q', by { ext, simp only [add_apply, smul_apply, smul_add] },
smul_zero := λ a, by { ext, simp only [zero_apply, smul_apply, smul_zero] }, }
instance [semiring S] [module S R] [smul_comm_class S R R] : module S (quadratic_form R M) :=
{ zero_smul := λ Q, by { ext, simp only [zero_apply, smul_apply, zero_smul] },
add_smul := λ a b Q, by { ext, simp only [add_apply, smul_apply, add_smul] } }
end semiring_operators
section ring_operators
variables [ring R] [add_comm_group M] [module R M]
instance : has_neg (quadratic_form R M) :=
⟨ λ Q,
{ to_fun := -Q,
to_fun_smul := λ a x,
by simp only [pi.neg_apply, map_smul, mul_neg],
exists_companion' :=
let ⟨B, h⟩ := Q.exists_companion in
⟨-B, λ x y, by simp_rw [pi.neg_apply, h, bilin_form.neg_apply, neg_add] ⟩ } ⟩
@[simp] lemma coe_fn_neg (Q : quadratic_form R M) : ⇑(-Q) = -Q := rfl
@[simp] lemma neg_apply (Q : quadratic_form R M) (x : M) : (-Q) x = -Q x := rfl
instance : has_sub (quadratic_form R M) :=
⟨ λ Q Q', (Q + -Q').copy (Q - Q') (sub_eq_add_neg _ _) ⟩
@[simp] lemma coe_fn_sub (Q Q' : quadratic_form R M) : ⇑(Q - Q') = Q - Q' := rfl
@[simp] lemma sub_apply (Q Q' : quadratic_form R M) (x : M) : (Q - Q') x = Q x - Q' x := rfl
instance : add_comm_group (quadratic_form R M) :=
fun_like.coe_injective.add_comm_group _
coe_fn_zero coe_fn_add coe_fn_neg coe_fn_sub (λ _ _, coe_fn_smul _ _) (λ _ _, coe_fn_smul _ _)
end ring_operators
section comp
variables [semiring R] [add_comm_monoid M] [module R M]
variables {N : Type v} [add_comm_monoid N] [module R N]
/-- Compose the quadratic form with a linear function. -/
def comp (Q : quadratic_form R N) (f : M →ₗ[R] N) :
quadratic_form R M :=
{ to_fun := λ x, Q (f x),
to_fun_smul := λ a x, by simp only [map_smul, f.map_smul],
exists_companion' :=
let ⟨B, h⟩ := Q.exists_companion in
⟨B.comp f f, λ x y, by simp_rw [f.map_add, h, bilin_form.comp_apply]⟩ }
@[simp] lemma comp_apply (Q : quadratic_form R N) (f : M →ₗ[R] N) (x : M) :
(Q.comp f) x = Q (f x) := rfl
/-- Compose a quadratic form with a linear function on the left. -/
@[simps {simp_rhs := tt}]
def _root_.linear_map.comp_quadratic_form {S : Type*}
[comm_semiring S] [algebra S R] [module S M] [is_scalar_tower S R M]
(f : R →ₗ[S] S) (Q : quadratic_form R M) :
quadratic_form S M :=
{ to_fun := λ x, f (Q x),
to_fun_smul := λ b x, by rw [Q.map_smul_of_tower b x, f.map_smul, smul_eq_mul],
exists_companion' :=
let ⟨B, h⟩ := Q.exists_companion in
⟨f.comp_bilin_form B, λ x y, by simp_rw [h, f.map_add, linear_map.comp_bilin_form_apply]⟩ }
end comp
section comm_ring
variables [comm_semiring R] [add_comm_monoid M] [module R M]
/-- The product of linear forms is a quadratic form. -/
def lin_mul_lin (f g : M →ₗ[R] R) : quadratic_form R M :=
{ to_fun := f * g,
to_fun_smul := λ a x,
by { simp only [smul_eq_mul, ring_hom.id_apply, pi.mul_apply, linear_map.map_smulₛₗ], ring },
exists_companion' := ⟨
bilin_form.lin_mul_lin f g + bilin_form.lin_mul_lin g f, λ x y, by { simp, ring }⟩ }
@[simp]
lemma lin_mul_lin_apply (f g : M →ₗ[R] R) (x) : lin_mul_lin f g x = f x * g x := rfl
@[simp]
lemma add_lin_mul_lin (f g h : M →ₗ[R] R) :
lin_mul_lin (f + g) h = lin_mul_lin f h + lin_mul_lin g h :=
ext (λ x, add_mul _ _ _)
@[simp]
lemma lin_mul_lin_add (f g h : M →ₗ[R] R) :
lin_mul_lin f (g + h) = lin_mul_lin f g + lin_mul_lin f h :=
ext (λ x, mul_add _ _ _)
variables {N : Type v} [add_comm_monoid N] [module R N]
@[simp]
lemma lin_mul_lin_comp (f g : M →ₗ[R] R) (h : N →ₗ[R] M) :
(lin_mul_lin f g).comp h = lin_mul_lin (f.comp h) (g.comp h) :=
rfl
variables {n : Type*}
/-- `sq` is the quadratic form mapping the vector `x : R₁` to `x * x` -/
@[simps]
def sq : quadratic_form R R :=
lin_mul_lin linear_map.id linear_map.id
/-- `proj i j` is the quadratic form mapping the vector `x : n → R₁` to `x i * x j` -/
def proj (i j : n) : quadratic_form R (n → R) :=
lin_mul_lin (@linear_map.proj _ _ _ (λ _, R) _ _ i) (@linear_map.proj _ _ _ (λ _, R) _ _ j)
@[simp]
lemma proj_apply (i j : n) (x : n → R) : proj i j x = x i * x j := rfl
end comm_ring
end quadratic_form
/-!
### Associated bilinear forms
Over a commutative ring with an inverse of 2, the theory of quadratic forms is
basically identical to that of symmetric bilinear forms. The map from quadratic
forms to bilinear forms giving this identification is called the `associated`
quadratic form.
-/
namespace bilin_form
open quadratic_form
section semiring
variables [semiring R] [add_comm_monoid M] [module R M]
variables {B : bilin_form R M}
/-- A bilinear form gives a quadratic form by applying the argument twice. -/
def to_quadratic_form (B : bilin_form R M) : quadratic_form R M :=
{ to_fun := λ x, B x x,
to_fun_smul := λ a x, by simp only [mul_assoc, smul_right, smul_left],
exists_companion' :=
⟨B + bilin_form.flip_hom ℕ B, λ x y, by { simp [add_add_add_comm, add_comm] }⟩ }
@[simp] lemma to_quadratic_form_apply (B : bilin_form R M) (x : M) :
B.to_quadratic_form x = B x x :=
rfl
section
variables (R M)
@[simp] lemma to_quadratic_form_zero : (0 : bilin_form R M).to_quadratic_form = 0 := rfl
end
@[simp] lemma to_quadratic_form_add (B₁ B₂ : bilin_form R M) :
(B₁ + B₂).to_quadratic_form = B₁.to_quadratic_form + B₂.to_quadratic_form := rfl
@[simp] lemma to_quadratic_form_smul [monoid S] [distrib_mul_action S R] [smul_comm_class S R R]
(a : S) (B : bilin_form R M) :
(a • B).to_quadratic_form = a • B.to_quadratic_form := rfl
section
variables (R M)
/-- `bilin_form.to_quadratic_form` as an additive homomorphism -/
@[simps] def to_quadratic_form_add_monoid_hom : bilin_form R M →+ quadratic_form R M :=
{ to_fun := to_quadratic_form,
map_zero' := to_quadratic_form_zero _ _,
map_add' := to_quadratic_form_add }
end
@[simp] lemma to_quadratic_form_list_sum (B : list (bilin_form R M)) :
B.sum.to_quadratic_form = (B.map to_quadratic_form).sum :=
map_list_sum (to_quadratic_form_add_monoid_hom R M) B
@[simp] lemma to_quadratic_form_multiset_sum (B : multiset (bilin_form R M)) :
B.sum.to_quadratic_form = (B.map to_quadratic_form).sum :=
map_multiset_sum (to_quadratic_form_add_monoid_hom R M) B
@[simp] lemma to_quadratic_form_sum {ι : Type*} (s : finset ι) (B : ι → bilin_form R M) :
(∑ i in s, B i).to_quadratic_form = ∑ i in s, (B i).to_quadratic_form :=
map_sum (to_quadratic_form_add_monoid_hom R M) B s
@[simp] lemma to_quadratic_form_eq_zero {B : bilin_form R M} :
B.to_quadratic_form = 0 ↔ B.is_alt :=
quadratic_form.ext_iff
end semiring
section ring
variables [ring R] [add_comm_group M] [module R M]
variables {B : bilin_form R M}
lemma polar_to_quadratic_form (x y : M) : polar (λ x, B x x) x y = B x y + B y x :=
by { simp only [add_assoc, add_sub_cancel', add_right, polar, add_left_inj, add_neg_cancel_left,
add_left, sub_eq_add_neg _ (B y y), add_comm (B y x) _] }
@[simp] lemma to_quadratic_form_neg (B : bilin_form R M) :
(-B).to_quadratic_form = -B.to_quadratic_form := rfl
@[simp] lemma to_quadratic_form_sub (B₁ B₂ : bilin_form R M) :
(B₁ - B₂).to_quadratic_form = B₁.to_quadratic_form - B₂.to_quadratic_form := rfl
end ring
end bilin_form
namespace quadratic_form
open bilin_form
section associated_hom
variables [ring R] [comm_ring R₁] [add_comm_group M] [module R M] [module R₁ M]
variables (S) [comm_semiring S] [algebra S R]
variables [invertible (2 : R)] {B₁ : bilin_form R M}
/-- `associated_hom` is the map that sends a quadratic form on a module `M` over `R` to its
associated symmetric bilinear form. As provided here, this has the structure of an `S`-linear map
where `S` is a commutative subring of `R`.
Over a commutative ring, use `associated`, which gives an `R`-linear map. Over a general ring with
no nontrivial distinguished commutative subring, use `associated'`, which gives an additive
homomorphism (or more precisely a `ℤ`-linear map.) -/
def associated_hom : quadratic_form R M →ₗ[S] bilin_form R M :=
{ to_fun := λ Q,
((•) : submonoid.center R → bilin_form R M → bilin_form R M)
(⟨⅟2, λ x, (commute.one_right x).bit0_right.inv_of_right⟩) Q.polar_bilin,
map_add' := λ Q Q', by { ext, simp only [bilin_form.add_apply, bilin_form.smul_apply, coe_fn_mk,
polar_bilin_apply, polar_add, coe_fn_add, smul_add] },
map_smul' := λ s Q, by { ext, simp only [ring_hom.id_apply, polar_smul, smul_comm s,
polar_bilin_apply, coe_fn_mk, coe_fn_smul, bilin_form.smul_apply] } }
variables (Q : quadratic_form R M) (S)
@[simp] lemma associated_apply (x y : M) :
associated_hom S Q x y = ⅟2 * (Q (x + y) - Q x - Q y) := rfl
lemma associated_is_symm : (associated_hom S Q).is_symm :=
λ x y, by simp only [associated_apply, add_comm, add_left_comm, sub_eq_add_neg]
@[simp] lemma associated_comp {N : Type v} [add_comm_group N] [module R N] (f : N →ₗ[R] M) :
associated_hom S (Q.comp f) = (associated_hom S Q).comp f f :=
by { ext, simp only [quadratic_form.comp_apply, bilin_form.comp_apply, associated_apply,
linear_map.map_add] }
lemma associated_to_quadratic_form (B : bilin_form R M) (x y : M) :
associated_hom S B.to_quadratic_form x y = ⅟2 * (B x y + B y x) :=
by simp only [associated_apply, ← polar_to_quadratic_form, polar, to_quadratic_form_apply]
lemma associated_left_inverse (h : B₁.is_symm) :
associated_hom S (B₁.to_quadratic_form) = B₁ :=
bilin_form.ext $ λ x y,
by rw [associated_to_quadratic_form, is_symm.eq h x y, ←two_mul, ←mul_assoc, inv_of_mul_self,
one_mul]
lemma to_quadratic_form_associated : (associated_hom S Q).to_quadratic_form = Q :=
quadratic_form.ext $ λ x,
calc (associated_hom S Q).to_quadratic_form x
= ⅟2 * (Q x + Q x) : by simp only [add_assoc, add_sub_cancel', one_mul,
to_quadratic_form_apply, add_mul, associated_apply, map_add_self, bit0]
... = Q x : by rw [← two_mul (Q x), ←mul_assoc, inv_of_mul_self, one_mul]
-- note: usually `right_inverse` lemmas are named the other way around, but this is consistent
-- with historical naming in this file.
lemma associated_right_inverse :
function.right_inverse (associated_hom S)
(bilin_form.to_quadratic_form : _ → quadratic_form R M) :=
λ Q, to_quadratic_form_associated S Q
lemma associated_eq_self_apply (x : M) : associated_hom S Q x x = Q x :=
begin
rw [associated_apply, map_add_self],
suffices : (⅟2) * (2 * Q x) = Q x,
{ convert this,
simp only [bit0, add_mul, one_mul],
abel },
simp only [← mul_assoc, one_mul, inv_of_mul_self],
end
/-- `associated'` is the `ℤ`-linear map that sends a quadratic form on a module `M` over `R` to its
associated symmetric bilinear form. -/
abbreviation associated' : quadratic_form R M →ₗ[ℤ] bilin_form R M :=
associated_hom ℤ
/-- Symmetric bilinear forms can be lifted to quadratic forms -/
instance can_lift :
can_lift (bilin_form R M) (quadratic_form R M) (associated_hom ℕ) bilin_form.is_symm :=
{ prf := λ B hB, ⟨B.to_quadratic_form, associated_left_inverse _ hB⟩ }
/-- There exists a non-null vector with respect to any quadratic form `Q` whose associated
bilinear form is non-zero, i.e. there exists `x` such that `Q x ≠ 0`. -/
lemma exists_quadratic_form_ne_zero {Q : quadratic_form R M} (hB₁ : Q.associated' ≠ 0) :
∃ x, Q x ≠ 0 :=
begin
rw ←not_forall,
intro h,
apply hB₁,
rw [(quadratic_form.ext h : Q = 0), linear_map.map_zero],
end
end associated_hom
section associated
variables [comm_ring R₁] [add_comm_group M] [module R₁ M]
variables [invertible (2 : R₁)]
-- Note: When possible, rather than writing lemmas about `associated`, write a lemma applying to
-- the more general `associated_hom` and place it in the previous section.
/-- `associated` is the linear map that sends a quadratic form over a commutative ring to its
associated symmetric bilinear form. -/
abbreviation associated : quadratic_form R₁ M →ₗ[R₁] bilin_form R₁ M :=
associated_hom R₁
@[simp] lemma associated_lin_mul_lin (f g : M →ₗ[R₁] R₁) :
(lin_mul_lin f g).associated =
⅟(2 : R₁) • (bilin_form.lin_mul_lin f g + bilin_form.lin_mul_lin g f) :=
by { ext, simp only [smul_add, algebra.id.smul_eq_mul, bilin_form.lin_mul_lin_apply,
quadratic_form.lin_mul_lin_apply, bilin_form.smul_apply, associated_apply, bilin_form.add_apply,
linear_map.map_add], ring }
end associated
section anisotropic
section semiring
variables [semiring R] [add_comm_monoid M] [module R M]
/-- An anisotropic quadratic form is zero only on zero vectors. -/
def anisotropic (Q : quadratic_form R M) : Prop := ∀ x, Q x = 0 → x = 0
lemma not_anisotropic_iff_exists (Q : quadratic_form R M) :
¬anisotropic Q ↔ ∃ x ≠ 0, Q x = 0 :=
by simp only [anisotropic, not_forall, exists_prop, and_comm]
lemma anisotropic.eq_zero_iff {Q : quadratic_form R M} (h : anisotropic Q) {x : M} :
Q x = 0 ↔ x = 0 :=
⟨h x, λ h, h.symm ▸ map_zero Q⟩
end semiring
section ring
variables [ring R] [add_comm_group M] [module R M]
/-- The associated bilinear form of an anisotropic quadratic form is nondegenerate. -/
lemma nondegenerate_of_anisotropic [invertible (2 : R)] (Q : quadratic_form R M)
(hB : Q.anisotropic) : Q.associated'.nondegenerate :=
begin
intros x hx,
refine hB _ _,
rw ← hx x,
exact (associated_eq_self_apply _ _ x).symm,
end
end ring
end anisotropic
section pos_def
variables {R₂ : Type u} [ordered_ring R₂] [add_comm_monoid M] [module R₂ M]
variables {Q₂ : quadratic_form R₂ M}
/-- A positive definite quadratic form is positive on nonzero vectors. -/
def pos_def (Q₂ : quadratic_form R₂ M) : Prop := ∀ x ≠ 0, 0 < Q₂ x
lemma pos_def.smul {R} [linear_ordered_comm_ring R] [module R M]
{Q : quadratic_form R M} (h : pos_def Q) {a : R} (a_pos : 0 < a) : pos_def (a • Q) :=
λ x hx, mul_pos a_pos (h x hx)
variables {n : Type*}
lemma pos_def.nonneg {Q : quadratic_form R₂ M} (hQ : pos_def Q) (x : M) :
0 ≤ Q x :=
(eq_or_ne x 0).elim (λ h, h.symm ▸ (map_zero Q).symm.le) (λ h, (hQ _ h).le)
lemma pos_def.anisotropic {Q : quadratic_form R₂ M} (hQ : Q.pos_def) : Q.anisotropic :=
λ x hQx, classical.by_contradiction $ λ hx, lt_irrefl (0 : R₂) $ begin
have := hQ _ hx,
rw hQx at this,
exact this,
end
lemma pos_def_of_nonneg {Q : quadratic_form R₂ M} (h : ∀ x, 0 ≤ Q x) (h0 : Q.anisotropic) :
pos_def Q :=
λ x hx, lt_of_le_of_ne (h x) (ne.symm $ λ hQx, hx $ h0 _ hQx)
lemma pos_def_iff_nonneg {Q : quadratic_form R₂ M} :
pos_def Q ↔ (∀ x, 0 ≤ Q x) ∧ Q.anisotropic :=
⟨λ h, ⟨h.nonneg, h.anisotropic⟩, λ ⟨n, a⟩, pos_def_of_nonneg n a⟩
lemma pos_def.add (Q Q' : quadratic_form R₂ M) (hQ : pos_def Q) (hQ' : pos_def Q') :
pos_def (Q + Q') :=
λ x hx, add_pos (hQ x hx) (hQ' x hx)
lemma lin_mul_lin_self_pos_def {R} [linear_ordered_comm_ring R] [module R M]
(f : M →ₗ[R] R) (hf : linear_map.ker f = ⊥) :
pos_def (lin_mul_lin f f) :=
λ x hx, mul_self_pos.2 (λ h, hx $ linear_map.ker_eq_bot'.mp hf _ h)
end pos_def
end quadratic_form
section
/-!
### Quadratic forms and matrices
Connect quadratic forms and matrices, in order to explicitly compute with them.
The convention is twos out, so there might be a factor 2⁻¹ in the entries of the
matrix.
The determinant of the matrix is the discriminant of the quadratic form.
-/
variables {n : Type w} [fintype n] [decidable_eq n]
variables [comm_ring R₁] [add_comm_monoid M] [module R₁ M]
/-- `M.to_quadratic_form` is the map `λ x, col x ⬝ M ⬝ row x` as a quadratic form. -/
def matrix.to_quadratic_form' (M : matrix n n R₁) :
quadratic_form R₁ (n → R₁) :=
M.to_bilin'.to_quadratic_form
variables [invertible (2 : R₁)]
/-- A matrix representation of the quadratic form. -/
def quadratic_form.to_matrix' (Q : quadratic_form R₁ (n → R₁)) : matrix n n R₁ :=
Q.associated.to_matrix'
open quadratic_form
lemma quadratic_form.to_matrix'_smul (a : R₁) (Q : quadratic_form R₁ (n → R₁)) :
(a • Q).to_matrix' = a • Q.to_matrix' :=
by simp only [to_matrix', linear_equiv.map_smul, linear_map.map_smul]
lemma quadratic_form.is_symm_to_matrix' (Q : quadratic_form R₁ (n → R₁)) :
Q.to_matrix'.is_symm :=
begin
ext i j,
rw [to_matrix', bilin_form.to_matrix'_apply, bilin_form.to_matrix'_apply, associated_is_symm]
end
end
namespace quadratic_form
variables {n : Type w} [fintype n]
variables [comm_ring R₁] [decidable_eq n] [invertible (2 : R₁)]
variables {m : Type w} [decidable_eq m] [fintype m]
open_locale matrix
@[simp]
lemma to_matrix'_comp (Q : quadratic_form R₁ (m → R₁)) (f : (n → R₁) →ₗ[R₁] (m → R₁)) :
(Q.comp f).to_matrix' = f.to_matrix'ᵀ ⬝ Q.to_matrix' ⬝ f.to_matrix' :=
by { ext, simp only [quadratic_form.associated_comp, bilin_form.to_matrix'_comp, to_matrix'] }
section discriminant
variables {Q : quadratic_form R₁ (n → R₁)}
/-- The discriminant of a quadratic form generalizes the discriminant of a quadratic polynomial. -/
def discr (Q : quadratic_form R₁ (n → R₁)) : R₁ := Q.to_matrix'.det
lemma discr_smul (a : R₁) : (a • Q).discr = a ^ fintype.card n * Q.discr :=
by simp only [discr, to_matrix'_smul, matrix.det_smul]
lemma discr_comp (f : (n → R₁) →ₗ[R₁] (n → R₁)) :
(Q.comp f).discr = f.to_matrix'.det * f.to_matrix'.det * Q.discr :=
by simp only [matrix.det_transpose, mul_left_comm, quadratic_form.to_matrix'_comp, mul_comm,
matrix.det_mul, discr]
end discriminant
end quadratic_form
namespace quadratic_form
end quadratic_form
namespace bilin_form
section semiring
variables [semiring R] [add_comm_monoid M] [module R M]
/-- A bilinear form is nondegenerate if the quadratic form it is associated with is anisotropic. -/
lemma nondegenerate_of_anisotropic
{B : bilin_form R M} (hB : B.to_quadratic_form.anisotropic) : B.nondegenerate :=
λ x hx, hB _ (hx x)
end semiring
variables [ring R] [add_comm_group M] [module R M]
/-- There exists a non-null vector with respect to any symmetric, nonzero bilinear form `B`
on a module `M` over a ring `R` with invertible `2`, i.e. there exists some
`x : M` such that `B x x ≠ 0`. -/
lemma exists_bilin_form_self_ne_zero [htwo : invertible (2 : R)]
{B : bilin_form R M} (hB₁ : B ≠ 0) (hB₂ : B.is_symm) :
∃ x, ¬ B.is_ortho x x :=
begin
lift B to quadratic_form R M using hB₂ with Q,
obtain ⟨x, hx⟩ := quadratic_form.exists_quadratic_form_ne_zero hB₁,
exact ⟨x, λ h, hx (Q.associated_eq_self_apply ℕ x ▸ h)⟩,
end
open finite_dimensional
variables {V : Type u} {K : Type v} [field K] [add_comm_group V] [module K V]
variable [finite_dimensional K V]
/-- Given a symmetric bilinear form `B` on some vector space `V` over a field `K`
in which `2` is invertible, there exists an orthogonal basis with respect to `B`. -/
lemma exists_orthogonal_basis [hK : invertible (2 : K)]
{B : bilin_form K V} (hB₂ : B.is_symm) :
∃ (v : basis (fin (finrank K V)) K V), B.is_Ortho v :=
begin
unfreezingI { induction hd : finrank K V with d ih generalizing V },
{ exact ⟨basis_of_finrank_zero hd, λ _ _ _, zero_left _⟩ },
haveI := finrank_pos_iff.1 (hd.symm ▸ nat.succ_pos d : 0 < finrank K V),
-- either the bilinear form is trivial or we can pick a non-null `x`
obtain rfl | hB₁ := eq_or_ne B 0,
{ let b := finite_dimensional.fin_basis K V,
rw hd at b,
refine ⟨b, λ i j hij, rfl⟩, },
obtain ⟨x, hx⟩ := exists_bilin_form_self_ne_zero hB₁ hB₂,
rw [← submodule.finrank_add_eq_of_is_compl (is_compl_span_singleton_orthogonal hx).symm,
finrank_span_singleton (ne_zero_of_not_is_ortho_self x hx)] at hd,
let B' := B.restrict (B.orthogonal $ K ∙ x),
obtain ⟨v', hv₁⟩ := ih (B.restrict_symm hB₂ _ : B'.is_symm) (nat.succ.inj hd),
-- concatenate `x` with the basis obtained by induction
let b := basis.mk_fin_cons x v'
(begin
rintros c y hy hc,
rw add_eq_zero_iff_neg_eq at hc,
rw [← hc, submodule.neg_mem_iff] at hy,
have := (is_compl_span_singleton_orthogonal hx).disjoint,
rw submodule.disjoint_def at this,
have := this (c • x) (submodule.smul_mem _ _ $ submodule.mem_span_singleton_self _) hy,
exact (smul_eq_zero.1 this).resolve_right (λ h, hx $ h.symm ▸ zero_left _),
end)
(begin
intro y,
refine ⟨-B x y/B x x, λ z hz, _⟩,
obtain ⟨c, rfl⟩ := submodule.mem_span_singleton.1 hz,
rw [is_ortho, smul_left, add_right, smul_right, div_mul_cancel _ hx, add_neg_self, mul_zero],
end),
refine ⟨b, _⟩,
{ rw basis.coe_mk_fin_cons,
intros j i,
refine fin.cases _ (λ i, _) i; refine fin.cases _ (λ j, _) j; intro hij;
simp only [function.on_fun, fin.cons_zero, fin.cons_succ, function.comp_apply],
{ exact (hij rfl).elim },
{ rw [is_ortho, hB₂],
exact (v' j).prop _ (submodule.mem_span_singleton_self x) },
{ exact (v' i).prop _ (submodule.mem_span_singleton_self x) },
{ exact hv₁ (ne_of_apply_ne _ hij), }, }
end
end bilin_form
namespace quadratic_form
open finset bilin_form
variables {M₁ : Type*} [semiring R] [comm_semiring R₁] [add_comm_monoid M] [add_comm_monoid M₁]
variables [module R M] [module R M₁]
variables {ι : Type*} [fintype ι] {v : basis ι R M}
/-- Given a quadratic form `Q` and a basis, `basis_repr` is the basis representation of `Q`. -/
noncomputable def basis_repr (Q : quadratic_form R M) (v : basis ι R M) :
quadratic_form R (ι → R) :=
Q.comp v.equiv_fun.symm
@[simp]
lemma basis_repr_apply (Q : quadratic_form R M) (w : ι → R) :
Q.basis_repr v w = Q (∑ i : ι, w i • v i) :=
by { rw ← v.equiv_fun_symm_apply, refl }
section
variables (R₁)
/-- The weighted sum of squares with respect to some weight as a quadratic form.
The weights are applied using `•`; typically this definition is used either with `S = R₁` or
`[algebra S R₁]`, although this is stated more generally. -/
def weighted_sum_squares [monoid S] [distrib_mul_action S R₁]
[smul_comm_class S R₁ R₁]
(w : ι → S) : quadratic_form R₁ (ι → R₁) :=
∑ i : ι, w i • proj i i
end
@[simp]
lemma weighted_sum_squares_apply [monoid S] [distrib_mul_action S R₁] [smul_comm_class S R₁ R₁]
(w : ι → S) (v : ι → R₁) :
weighted_sum_squares R₁ w v = ∑ i : ι, w i • (v i * v i) :=
quadratic_form.sum_apply _ _ _
/-- On an orthogonal basis, the basis representation of `Q` is just a sum of squares. -/
lemma basis_repr_eq_of_is_Ortho
{R₁ M} [comm_ring R₁] [add_comm_group M] [module R₁ M] [invertible (2 : R₁)]
(Q : quadratic_form R₁ M) (v : basis ι R₁ M) (hv₂ : (associated Q).is_Ortho v) :
Q.basis_repr v = weighted_sum_squares _ (λ i, Q (v i)) :=
begin
ext w,
rw [basis_repr_apply, ←@associated_eq_self_apply R₁, sum_left, weighted_sum_squares_apply],
refine sum_congr rfl (λ j hj, _),
rw [←@associated_eq_self_apply R₁, sum_right, sum_eq_single_of_mem j hj],
{ rw [smul_left, smul_right, smul_eq_mul], ring },
{ intros i _ hij,
rw [smul_left, smul_right,
show associated_hom R₁ Q (v j) (v i) = 0, from hv₂ hij.symm,
mul_zero, mul_zero] },
end
end quadratic_form
|
f9c3dcd353396eaacd2229b64a77b4448cb88ad6 | 827a8a5c2041b1d7f55e128581f583dfbd65ecf6 | /snippets.hlean | 2b63a870d36b1b2f9e060192a44e63ff379ff003 | [
"Apache-2.0"
] | permissive | fpvandoorn/leansnippets | 6af0499f6f3fd2c07e4b580734d77b67574e7c27 | 601bafbe07e9534af76f60994d6bdf741996ef93 | refs/heads/master | 1,590,063,910,882 | 1,545,093,878,000 | 1,545,093,878,000 | 36,044,957 | 2 | 2 | null | 1,442,619,708,000 | 1,432,256,875,000 | Lean | UTF-8 | Lean | false | false | 28,146 | hlean | import types.nat.hott cubical.cubeover homotopy.join homotopy.circle ..Spectral.homotopy.smash
/---------------------------------------------------------------------------------------------------
Show that type quotient preserves equivalence.
Can this be done without univalence?
---------------------------------------------------------------------------------------------------/
namespace quotient
open equiv eq
universe variables u v
variables {A B : Type.{u}} {R : A → A → Type.{v}} {S : B → B → Type.{v}}
definition quotient_equiv (f : A ≃ B) (g : Π(a a' : A), R a a' ≃ S (f a) (f a'))
: quotient R ≃ quotient S :=
begin
revert S g, eapply (equiv.rec_on_ua_idp f), esimp, intro S g,
have H : R = S,
by intros, apply eq_of_homotopy, intro a, apply eq_of_homotopy, intro a',
eapply (equiv.rec_on_ua_idp (g a a')), reflexivity,
cases H, reflexivity
end
end quotient
/---------------------------------------------------------------------------------------------------
Test of pushouts: show that the pushout of the diagram
1 <- 0 -> 1
is equivalent to 2.
---------------------------------------------------------------------------------------------------/
namespace pushout
open equiv is_equiv unit bool
private definition unit_of_empty (u : empty) : unit := star
example : pushout unit_of_empty unit_of_empty ≃ bool :=
begin
fapply equiv.MK,
{ intro x, induction x with u u c,
exact ff,
exact tt,
cases c},
{ intro b, cases b,
exact (!inl ⋆),
exact (!inr ⋆)},
{ intro b, cases b,
reflexivity,
reflexivity},
{ intro x, induction x with u u c,
cases u, reflexivity,
cases u, reflexivity,
cases c},
end
end pushout
/---------------------------------------------------------------------------------------------------
(Part of) encode-decode proof to characterize the equality type of the natural numbers.
This is not needed in the library, since we use Hedberg's Theorem to show that the natural numbers
is a set
---------------------------------------------------------------------------------------------------/
namespace nathide
open unit eq is_trunc nat
protected definition code (n m : ℕ) : Type₀ :=
begin
revert m,
induction n with n IH,
{ intro m, cases m,
{ exact unit},
{ exact empty}},
{ intro m, cases m with m,
{ exact empty},
{ exact IH m}},
end
protected definition encode {n m : ℕ} (p : n = m) : nat.code n m :=
begin
revert m p,
induction n with n IH,
{ intro m p, cases m,
{ exact star},
{ contradiction}},
{ intro m p, cases m with m,
{ contradiction},
{ rewrite [↑nat.code], apply IH, injection p, assumption}},
end
protected definition decode {n m : ℕ} (q : nat.code n m) : n = m :=
begin
revert m q,
induction n with n IH,
{ intro m q, cases m,
{ reflexivity},
{ contradiction}},
{ intro m q, cases m with m,
{ contradiction},
{ exact ap succ (!IH q)}},
end
definition is_prop_code (n m : ℕ) : is_prop (nat.code n m) :=
begin
apply is_prop.mk, revert m,
induction n with n IH,
{ intro m p q, cases m,
{ cases p, cases q, reflexivity},
{ contradiction}},
{ intro m p q, cases m with m,
{ contradiction},
{ exact IH m p q}},
end
local attribute is_prop_code [instance]
end nathide
/---------------------------------------------------------------------------------------------------
(Part of) encode-decode proof to characterize < on nat, to show that it is a mere proposition.
In types.nat.hott there is a simpler proof
---------------------------------------------------------------------------------------------------/
namespace nat
namespace lt
open unit is_trunc algebra
protected definition code (n m : ℕ) : Type₀ :=
begin
revert m,
induction n with n IH,
{ intro m, cases m,
{ exact empty},
{ exact unit}},
{ intro m, cases m with m,
{ exact empty},
{ exact IH m}},
end
protected definition encode {n m : ℕ} (p : n < m) : lt.code n m :=
begin
revert m p,
induction n with n IH,
{ intro m p, cases m,
{ exfalso, exact !lt.irrefl p},
{ exact star}},
{ intro m p, cases m with m,
{ exfalso, apply !not_lt_zero p},
{ rewrite [↑lt.code], apply IH, apply lt_of_succ_lt_succ p}},
end
protected definition decode {n m : ℕ} (q : lt.code n m) : n < m :=
begin
revert m q,
induction n with n IH,
{ intro m q, cases m,
{ contradiction},
{ apply zero_lt_succ}},
{ intro m q, cases m with m,
{ contradiction},
{ exact succ_lt_succ (!IH q)}},
end
definition is_prop_code (n m : ℕ) : is_prop (lt.code n m) :=
begin
apply is_prop.mk, revert m,
induction n with n IH,
{ intro m p q, cases m,
{ contradiction},
{ cases p, cases q, reflexivity}},
{ intro m p q, cases m with m,
{ contradiction},
{ exact IH m p q}},
end
local attribute is_prop_code [instance]
end lt
end nat
/---------------------------------------------------------------------------------------------------
Alternative recursor for inequality on ℕ
---------------------------------------------------------------------------------------------------/
namespace nat
open eq is_trunc
inductive le2 : ℕ → ℕ → Type :=
| zero_le2 : Πk, le2 0 k
| succ_le2_succ : Π{n k : ℕ}, le2 n k → le2 (succ n) (succ k)
open le2
definition le2_of_le {n m : ℕ} (H : n ≤ m) : le2 n m :=
begin
induction H with m H IH,
{ induction n with n IH,
{ exact zero_le2 0},
{ exact succ_le2_succ IH}},
{ clear H, induction IH with m n m IH IH2,
{ apply zero_le2},
{ exact succ_le2_succ IH2}}
end
definition le_of_le2 {n m : ℕ} (H : le2 n m) : n ≤ m :=
begin
induction H with m n m H IH,
{ apply zero_le},
{ apply succ_le_succ IH}
end
definition nat.le.rec2 {C : Π (n k : ℕ), n ≤ k → Type}
(H1 : Πk, C 0 k (zero_le k))
(H2 : (Π{n k : ℕ} (H : n ≤ k), C n k H → C (succ n) (succ k) (succ_le_succ H)))
{n k : ℕ} (H : n ≤ k) : C n k H :=
begin
assert lem : Π(x : le2 n k), C n k (le_of_le2 x),
{ intro x, clear H,
induction x with k n k x IH,
{ refine transport (C _ _) _ (H1 k), apply is_prop.elim},
{ refine transport (C _ _) _ (H2 (le_of_le2 x) IH), apply is_prop.elim}},
refine transport (C _ _) _ (lem (le2_of_le H)), apply is_prop.elim
end
end nat
/---------------------------------------------------------------------------------------------------
[WIP] Try to see whether having a list of paths is useful
---------------------------------------------------------------------------------------------------/
namespace lpath
open nat eq
inductive lpath {A : Type} (a : A) : A → ℕ → Type :=
lidp : lpath a a 0,
cons : Π{n : ℕ} {b c : A} (p : b = c) (l : lpath a b n), lpath a c (succ n)
open lpath.lpath
protected definition elim {A : Type} : Π{a b : A} {n : ℕ} (l : lpath a b n), a = b
| elim (lidp a) := idp
| elim (cons p l) := elim l ⬝ p
end lpath
/---------------------------------------------------------------------------------------------------
apn is weakly constant
---------------------------------------------------------------------------------------------------/
namespace apn
open eq pointed nat
definition weakly_constant_apn {A B : pType} {n : ℕ} {f : map₊ A B} (p : Ω[n] A)
(H : Π(a : A), f a = pt) : apn n f p = pt :=
begin
induction n with n IH,
{ unfold [apn, loopn] at *, apply H},
{ exact sorry}
end
end apn
/---------------------------------------------------------------------------------------------------
two quotient eliminator is unique
---------------------------------------------------------------------------------------------------/
/---------------------------------------------------------------------------------------------------
unit relation on circle
---------------------------------------------------------------------------------------------------/
namespace circlerel
open circle equiv prod eq
variables {A : Type} (a : A)
inductive R : A → A → Type :=
| Rmk : R a a
open R
definition R_equiv (x y) : R a x y ≃ a = x × a = y :=
begin
fapply equiv.MK,
{ intro r, induction r, exact (idp, idp)},
{ intro v, induction v with p₁ p₂, induction p₁, induction p₂, exact Rmk a},
{ intro v, induction v with p₁ p₂, induction p₁, induction p₂, reflexivity},
{ intro r, induction r, reflexivity}
end
definition R_circle : R base base base ≃ ℤ × ℤ :=
!R_equiv ⬝e prod_equiv_prod base_eq_base_equiv base_eq_base_equiv
end circlerel
/---------------------------------------------------------------------------------------------------
Quotienting A with the diagonal relation (smallest reflexive relation) gives A × S¹
---------------------------------------------------------------------------------------------------/
namespace circle
open eq circle prod quotient equiv
section
parameter {A : Type}
inductive reflexive_rel : A → A → Type :=
| Rmk : Πx, reflexive_rel x x
open reflexive_rel
definition times_circle := quotient reflexive_rel
definition φ [unfold 2] (x : times_circle) : A × S¹ :=
begin
induction x,
{ exact (a, base)},
{ induction H, exact ap (pair x) loop}
end
definition ψ [unfold 2] (x : A × S¹) : times_circle :=
begin
induction x with x y, induction y,
{ exact class_of _ x},
{ apply eq_of_rel, exact Rmk x}
end
definition times_circle_equiv [constructor] : times_circle ≃ A × S¹ :=
begin
fapply equiv.MK,
{ exact φ},
{ exact ψ},
{ intro x, induction x with x y, induction y,
{ reflexivity},
{ apply eq_pathover, apply hdeg_square,
rewrite [ap_compose φ (λa, ψ (x, a)), ▸*, elim_loop, ↑φ], apply elim_eq_of_rel}},
{ intro x, induction x,
{ reflexivity},
{ apply eq_pathover, apply hdeg_square, rewrite [ap_id], refine ap_compose ψ φ _ ⬝ _,
refine ap (ap ψ) !elim_eq_of_rel ⬝ _, induction H, esimp,
refine !ap_compose⁻¹ ⬝ _, esimp [function.compose], apply elim_loop}}
end
/- the same construction, where the relation is equivalent, but defined differently -/
definition reflexive_rel2 := @eq A
definition times_circle2 := quotient reflexive_rel2
definition φ' [unfold 2] (x : times_circle2) : A × S¹ :=
begin
induction x,
{ exact (a, base)},
{ induction H, exact ap (pair a) loop}
end
definition ψ' [unfold 2] (x : A × S¹) : times_circle2 :=
begin
induction x with x y, induction y,
{ exact class_of _ x},
{ apply eq_of_rel, apply idp}
end
definition times_circle2_equiv [constructor] : times_circle2 ≃ A × S¹ :=
begin
fapply equiv.MK,
{ exact φ'},
{ exact ψ'},
{ intro x, induction x with x y, induction y,
{ reflexivity},
{ apply eq_pathover, apply hdeg_square,
rewrite [ap_compose φ' (λa, ψ' (x, a)), ▸*, elim_loop, ↑φ'], apply elim_eq_of_rel}},
{ intro x, induction x,
{ reflexivity},
{ apply eq_pathover, apply hdeg_square, rewrite [ap_id], refine ap_compose ψ' φ' _ ⬝ _,
refine ap (ap ψ') !elim_eq_of_rel ⬝ _, induction H, esimp,
refine !ap_compose⁻¹ ⬝ _, esimp [function.compose], apply elim_loop}}
end
end
end circle
/---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------/
namespace pushout
open eq pi
variables {TL BL TR : Type} {f : TL → BL} {g : TL → TR}
protected definition elim_type' [unfold 9] (Pinl : BL → Type) (Pinr : TR → Type)
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) : pushout f g → Type :=
pushout.elim Pinl Pinr (λx, ua (Pglue x))
protected definition elim_type_eq {Pinl : BL → Type} {Pinr : TR → Type}
(Pglue : Π(x : TL), Pinl (f x) ≃ Pinr (g x)) (y : pushout f g) :
pushout.elim_type Pinl Pinr Pglue y ≃ pushout.elim_type' Pinl Pinr Pglue y :=
begin
fapply equiv.MK,
{ induction y, exact id, exact id, apply arrow_pathover_left, esimp, intro,
apply pathover_of_tr_eq, exact sorry},
{ induction y, exact id, exact id, apply arrow_pathover_left, esimp, intro,
apply pathover_of_tr_eq, exact sorry},
{ intro b, induction y, reflexivity, reflexivity, esimp,
apply pi_pathover_left, intro b, esimp at *, exact sorry},
{ exact sorry},
end
/---------------------------------------------------------------------------------------------------
define quotient from pushout
---------------------------------------------------------------------------------------------------/
namespace pushout
open quotient sum sigma sigma.ops
section
parameters {A : Type} (R : A → A → Type)
definition f [unfold 3] : A + (Σx y, R x y) → A
| (sum.inl a) := a
| (sum.inr v) := v.1
definition g [unfold 3] : A + (Σx y, R x y) → A
| (sum.inl a) := a
| (sum.inr v) := v.2.1
definition X [reducible] := pushout f g
definition Q [reducible] := quotient R
definition X_of_Q [unfold 3] : Q → X :=
begin
intro q, induction q,
{ exact inl a},
{ exact glue (sum.inr ⟨a, a', H⟩) ⬝ (glue (sum.inl a'))⁻¹}
end
definition Q_of_X [unfold 3] : X → Q :=
begin
intro x, induction x,
{ exact class_of R a},
{ exact class_of R a},
{ cases x with a v,
{ reflexivity},
{ exact eq_of_rel R v.2.2}}
end
definition Q_of_X_of_Q (q : Q) : Q_of_X (X_of_Q q) = q :=
begin
induction q,
{ reflexivity},
{ apply eq_pathover_id_right, apply hdeg_square, refine ap_compose Q_of_X _ _ ⬝ _,
refine ap02 Q_of_X !elim_eq_of_rel ⬝ _, refine !ap_con ⬝ _,
refine whisker_left _ !ap_inv ⬝ _,
exact !elim_glue ◾ !elim_glue⁻²}
end
definition X_of_Q_of_X (x : X) : X_of_Q (Q_of_X x) = x :=
begin
induction x,
{ reflexivity},
{ esimp, exact glue (sum.inl x)},
{ apply eq_pathover_id_right,
refine ap_compose X_of_Q _ _ ⬝ph _,
refine ap02 X_of_Q !elim_glue ⬝ph _,
cases x with a v,
{ esimp, exact square_of_eq idp},
{ cases v with a₁ v, cases v with a₂ r, esimp, refine !elim_eq_of_rel ⬝ph _,
apply square_of_eq, refine !idp_con ⬝ _, exact !inv_con_cancel_right⁻¹}}
end
end
end pushout
/---------------------------------------------------------------------------------------------------
Is the suspension of sets without decidable equality a 1-type?
---------------------------------------------------------------------------------------------------/
namespace susp
section
open susp eq bool is_trunc quotient unit pointed equiv prod int
parameter (P : Prop.{0})
inductive R : bool → bool → Type :=
| Rmk : P → R ff tt
definition Xt := quotient R
definition X : Type* := pointed.MK Xt (class_of R ff)
definition npt : X := class_of R tt
definition pth (p : P) : pt = npt :> X := eq_of_rel R (R.Rmk p)
/- X is a pointed set -/
definition code [unfold 2] : X → Prop.{0} :=
begin
intro x, induction x with b,
{ induction b, exact trunctype.mk unit _, exact P},
{ induction H, esimp, apply tua, esimp, apply equiv_of_is_prop,
{ intro u, exact a },
{ intro p, exact ⋆ }}
end
definition encode [unfold 3] (x : X) (p : pt = x) : code x :=
transport code p ⋆
definition decode [unfold 2 3] (x : X) (c : code x) : pt = x :=
begin
induction x with b,
{ induction b,
{ reflexivity },
{ esimp at c, exact pth c }},
{ induction H with p, apply arrow_pathover_left, intro c,
apply eq_pathover_constant_left_id_right, esimp at *, apply square_of_eq,
refine !idp_con ⬝ _ ⬝ !idp_con⁻¹, apply ap pth !is_prop.elim }
end
definition decode_encode (x : X) (p : pt = x) : decode x (encode x p) = p :=
begin
induction p, reflexivity
end
definition encode_decode (x : X) (c : code x) : encode x (decode x c) = c :=
!is_prop.elim
definition X_eq [constructor] (x : X) : (pt = x) ≃ code x :=
equiv.MK (encode x) (decode x) (encode_decode x) (decode_encode x)
definition X_flip [unfold 2] (x : X) : X :=
begin
induction x with b,
{ exact class_of _ (bnot b) },
{ induction H with p, exact (pth p)⁻¹ },
end
attribute bnot bnot_bnot [unfold 1]
definition X_flip_flip [unfold 2] (x : X) : X_flip (X_flip x) = x :=
begin
induction x with b,
{ exact ap (class_of R) !bnot_bnot },
{ induction H with p, apply eq_pathover_id_right, apply hdeg_square,
krewrite [ap_compose' X_flip X_flip, ↑X_flip, elim_eq_of_rel, ▸*, ap_inv, elim_eq_of_rel, ▸*],
apply inv_inv },
end
definition X_flip_equiv [constructor] : X ≃ X :=
equiv.MK X_flip X_flip X_flip_flip X_flip_flip
definition is_set_X [instance] : is_set X :=
begin
apply is_trunc_succ_intro,
intro x y,
induction x using quotient.rec_prop with b,
assert H : Π(y : X), is_prop (pt = y),
{ intro y, exact is_trunc_equiv_closed_rev _ (X_eq y) },
induction b,
{ apply H },
{ apply @(is_trunc_equiv_closed_rev _ (eq_equiv_fn_eq_of_equiv X_flip_equiv npt y)),
apply H }
end
definition Xs [constructor] : Set := trunctype.mk X _
definition Y := susp X
definition tℤ : Set := trunctype.mk ℤ _
definition loop : pt = pt :> Y :=
merid npt ⬝ (merid pt)⁻¹
definition Ycode [unfold 2] : Y → Set.{0} :=
begin
intro y, induction y with x,
{ exact tℤ }, --if P then unit else ℤ, except implement it using quotients with paths from P
{ exact tℤ }, --
{ apply tua, induction x with b,
{ induction b,
{ reflexivity },
{ apply equiv_succ }},
{ induction H with p, esimp, apply equiv_eq, intro x, exact sorry }}
end
definition Yencode [unfold 3] (y : Y) (p : pt = y) : Ycode y :=
transport Ycode p (0 : ℤ)
definition Ydecode [unfold 2 3] (y : Y) (c : Ycode y) : pt = y :=
begin
induction y with b,
{ exact power loop c },
{ exact power loop c ⬝ merid pt },
{ apply arrow_pathover_left, intro c,
apply eq_pathover_constant_left_id_right, esimp at *, exact sorry }
end
definition Ydecode_encode (y : Y) (p : pt = y) : Ydecode y (Yencode y p) = p :=
begin
induction p, reflexivity
end
definition Yencode_decode (y : Y) (c : Ycode y) : Yencode y (Ydecode y c) = c :=
begin
induction y,
{ exact sorry },
{ exact sorry },
{ apply is_prop.elimo }
end
end
end susp
/---------------------------------------------------------------------------------------------------
Suspension of a prop
---------------------------------------------------------------------------------------------------/
namespace susp
open is_trunc trunc_index pointed
-- definition susp_of_is_prop (A : Type) [is_prop A] : is_set (susp A) :=
-- begin
-- apply is_trunc_succ_of_is_trunc_loop, apply algebra.le.refl,
-- intro x, exact sorry --induction x using susp.rec_prop,
-- end
end susp
/---------------------------------------------------------------------------------------------------
start on the proof that susp (smash A B) = reduced_join A B
Here we use the join, which is equivalent(?)
---------------------------------------------------------------------------------------------------/
namespace smash
open susp join smash pointed susp
variables {A B : Type*}
definition susp_smash_of_pjoin [unfold 3] (x : pjoin A B) : susp (smash A B) :=
begin
induction x with a b a b,
{ exact pt },
{ exact south },
{ exact merid (smash.mk a b) },
end
definition pjoin_of_susp_smash_merid [unfold 3] (x : smash A B) : inl pt = inr pt :> pjoin A B :=
begin
induction x,
{ exact join.glue pt b ⬝ (join.glue a b)⁻¹ ⬝ join.glue a pt },
{ exact join.glue pt pt },
{ exact join.glue pt pt },
{ apply inv_con_cancel_right },
{ exact whisker_right _ !con.right_inv ⬝ !idp_con, }
end
definition pjoin_of_susp_smash [unfold 3] (x : susp (smash A B)) : pjoin A B :=
begin
induction x,
{ exact pt },
{ exact inr pt },
{ exact pjoin_of_susp_smash_merid a },
end
definition susp_smash_pequiv_pjoin : susp (smash A B) ≃* pjoin A B :=
begin
fapply pequiv_of_equiv,
{ fapply equiv.MK,
{ exact pjoin_of_susp_smash },
{ exact susp_smash_of_pjoin },
{ intro x, induction x: esimp,
{ exact join.glue pt pt ⬝ (join.glue x pt)⁻¹ },
{ exact (join.glue pt pt)⁻¹ ⬝ join.glue pt y },
{ apply eq_pathover_id_right,
rewrite [ap_compose' pjoin_of_susp_smash, ↑susp_smash_of_pjoin, join.elim_glue],
rewrite [↑pjoin_of_susp_smash, elim_merid, ▸*],
exact sorry}},
{ exact sorry}},
{ reflexivity }
end
end smash
/---------------------------------------------------------------------------------------------------
define dependent computation rule of the circle from the other data?
---------------------------------------------------------------------------------------------------/
namespace circlecomp
section
open circle eq sigma sigma.ops function equiv
parameters {P : circle → Type} (Pbase : P base) (Ploop : Pbase =[loop] Pbase)
include Ploop
definition eq2_pr1 {A : Type} {B : A → Type} {u v : Σa, B a} {p q : u = v} (r : p = q)
: p..1 = q..1 :=
ap eq_pr1 r
definition eq2_pr2 {A : Type} {B : A → Type} {u v : Σa, B a} {p q : u = v} (r : p = q)
: p..2 =[eq2_pr1 r] q..2 :=
!pathover_ap (apd eq_pr2 r)
-- definition natural_square_tr_loop {A : Type} {B : Type} {a : A} {f g : A → B}
-- (p : f ~ g) (q : a = a) : natural_square_tr p q = _ :=
-- _
definition pathover_ap_id {A : Type} {a a₂ : A} (B : A → Type) {p : a = a₂}
{b : B a} {b₂ : B a₂} (q : b =[p] b₂) : pathover_ap B id q = change_path (ap_id p)⁻¹ q :=
by induction q; reflexivity
definition pathover_ap_compose {A A' A'' : Type} {a a₂ : A} (B : A'' → Type)
(g : A' → A'') (f : A → A') {p : a = a₂} {b : B (g (f a))} {b₂ : B (g (f a₂))}
(q : b =[p] b₂) : pathover_ap B (g ∘ f) q
= change_path (ap_compose g f p)⁻¹ (pathover_ap B g (pathover_ap (B ∘ g) f q)) :=
by induction q; reflexivity
definition pathover_ap_compose_rev {A A' A'' : Type} {a a₂ : A} (B : A'' → Type)
(g : A' → A'') (f : A → A') {p : a = a₂} {b : B (g (f a))} {b₂ : B (g (f a₂))}
(q : b =[p] b₂) : pathover_ap B g (pathover_ap (B ∘ g) f q)
= change_path (ap_compose g f p) (pathover_ap B (g ∘ f) q) :=
by induction q; reflexivity
definition f : S¹ → sigma P := circle.elim ⟨base, Pbase⟩ (sigma_eq loop Ploop)
definition g : Πx, P x := circle.rec Pbase Ploop
definition foo1 (x : S¹) : (f x).1 = x :=
circle.rec_on x idp
begin apply eq_pathover, apply hdeg_square,
rewrite [ap_id, ap_compose pr1 f,↑f,elim_loop], apply sigma_eq_pr1 end
definition apd_eq_apd_ap {A B : Type} {C : B → Type} (g : Πb, C b) (f : A → B) {a a' : A}
(p : a = a') : apd (λx, g (f x)) p = pathover_of_pathover_ap C f (apd g (ap f p)) :=
by induction p; reflexivity
definition foo2 (x : S¹) : (f x).2 =[foo1 x] g x :=
circle.rec_on x idpo
begin
apply pathover_pathover, esimp, --rewrite pathover_ap_id,
apply transport (λx, squareover P x _ _ _ _),
apply to_left_inv !hdeg_square_equiv, esimp,
apply hdeg_squareover,
rewrite [pathover_ap_id, apd_eq_apd_ap pr2 f loop, ],
exact sorry --unfold natural_square_tr,
end
definition p : ap f loop = (sigma_eq loop Ploop) := !elim_loop
definition q : (ap f loop)..1 = loop := ap eq_pr1 p ⬝ !sigma_eq_pr1
definition r : (ap f loop)..2 =[q] Ploop :=
!eq2_pr2 ⬝o !sigma_eq_pr2
--set_option pp.notation false
theorem my_rec_loop : apd (circle.rec Pbase Ploop) loop = Ploop :=
begin
refine _ ⬝ tr_eq_of_pathover r, exact sorry
end
end
end circlecomp
/- try to use the circle to transport the computation rule from the circle to the new type -/
namespace circlecomp2
open circle eq sigma sigma.ops function equiv is_equiv
definition X : Type₀ := circle
definition b : X := base
definition l : b = b := loop
definition elim {C : Type} (c : C) (p : c = c) (x : X) : C := circle.elim c p x
definition rec {C : X → Type} (c : C b) (p : c =[l] c) (x : X) : C x := circle.rec c p x
theorem elim_l {C : Type} (c : C) (p : c = c) : ap (elim c p) l = p := elim_loop c p
attribute X l [irreducible]
attribute rec elim [unfold 4] [recursor 4]
attribute b [constructor]
example {C : Type} (c : C) (p : c = c) : elim c p b = c := begin esimp end
definition XequivS : S¹ ≃ X :=
begin
fapply equiv.MK,
{ intro y, induction y, exact b, exact l},
{ intro x, induction x, exact base, exact loop},
{ intro x, induction x, esimp, apply eq_pathover, apply hdeg_square,
rewrite [ap_id,ap_compose (circle.elim b l),elim_l], apply elim_loop},
{ intro y, induction y, esimp, apply eq_pathover, apply hdeg_square,
rewrite [ap_id,ap_compose (elim base loop),elim_loop], apply elim_l},
end
definition is_circle (X : Type) : Type :=
Σ(x₀ : X)
(p : x₀ = x₀)
(rec : Π(C : X → Type) (c : C x₀) (q : c =[p] c) (x : X), C x)
(rec_x₀ : Π(C : X → Type) (c : C x₀) (q : c =[p] c), rec C c q x₀ = c),
Π(C : X → Type) (c : C x₀) (q : c =[p] c), squareover C vrfl (apd (rec C c q) p) q (pathover_idp_of_eq !rec_x₀) (pathover_idp_of_eq !rec_x₀)
definition is_circle_circle : is_circle S¹ :=
⟨base, loop, @circle.rec, by intros; reflexivity, begin intros, apply vdeg_squareover, apply rec_loop end⟩
definition is_circle_X {X : Type₀} (f : S¹ ≃ X) : is_circle X :=
transport is_circle (ua f) is_circle_circle
namespace new
definition b : X := (is_circle_X XequivS).1
definition l : b = b := (is_circle_X XequivS).2.1
definition rec : Π{C : X → Type} (c : C b) (p : c =[l] c) (x : X), C x := (is_circle_X XequivS).2.2.1
definition rec_pt : Π{C : X → Type} (c : C b) (p : c =[l] c), rec c p b = c := (is_circle_X XequivS).2.2.2.1
-- definition rec_loop : Π{C : X → Type} (c : C b) (p : c =[l] c), squareover C vrfl (apd (rec c p) l) p (pathover_idp_of_eq !rec_pt) (pathover_idp_of_eq !rec_pt) := proof (is_circle_X XequivS).2.2.2.2 qed
end new
-- --set_option pp.notation false
-- definition foo {A A' : Type} (f : A ≃ A') (B : A → Type) {a a' : A} {p : a = a'}
-- {b : B a} {b' : B a'} : b =[p] b' ≃
-- pathover (B ∘ to_inv f) ((to_left_inv f a)⁻¹ ▸ b) (ap (to_fun f) p) ((to_left_inv f a')⁻¹ ▸ b') :=
-- begin
-- induction p, esimp,
-- refine !pathover_idp ⬝e _ ⬝e !pathover_idp⁻¹ᵉ,
-- apply eq_equiv_fn_eq
-- end
-- theorem rec_l {C : X → Type} (c : C b) (p : c =[l] c) : apdo (rec c p) l = p :=
-- begin
-- refine inj !(foo XequivS) _,
-- end
end circlecomp2
namespace merely_decidable_equality
open eq trunc is_trunc function equiv is_equiv decidable
definition decidable_equiv_closed {A B : Type} (e : A ≃ B) (H : decidable A)
: decidable B :=
begin
apply decidable_of_decidable_of_iff H,
split, exact to_fun e, exact to_inv e
end
local attribute is_trunc_decidable [instance]
example {A : Type} : decidable_eq (trunc 0 A) ↔
decidable_rel (λa a' : A, trunc -1 (a = a')) :=
begin
constructor: intro H,
{ intro a a', refine decidable_equiv_closed !tr_eq_tr_equiv (H (tr a) (tr a')) },
{ intro a a', induction a with a, induction a' with a',
refine decidable_equiv_closed _ (H a a'),
exact !tr_eq_tr_equiv⁻¹ᵉ
}
end
end merely_decidable_equality
end pushout
|
661168f12328477cd8f6d7e6cd102456718ed0cd | 682dc1c167e5900ba3168b89700ae1cf501cfa29 | /src/del/syntax/syntaxDEL.lean | 180715120fab0ca0f57020c546e9bdbc44eca808 | [] | no_license | paulaneeley/modal | 834558c87f55cdd6d8a29bb46c12f4d1de3239bc | ee5d149d4ecb337005b850bddf4453e56a5daf04 | refs/heads/master | 1,675,911,819,093 | 1,609,785,144,000 | 1,609,785,144,000 | 270,388,715 | 13 | 1 | null | null | null | null | UTF-8 | Lean | false | false | 4,007 | lean | /-
Copyright (c) 2021 Paula Neeley. All rights reserved.
Author: Paula Neeley
Following the textbook "Dynamic Epistemic Logic" by
Hans van Ditmarsch, Wiebe van der Hoek, and Barteld Kooi
-/
import del.languageDEL data.set.basic
variables {agents : Type}
---------------------- Proof System ----------------------
-- Define a context
@[reducible] def ctx (agents: Type) : Type := set (form agents)
instance : has_emptyc (ctx agents) := by apply_instance
notation Γ `∪` φ := set.insert φ Γ
-- Proof system for epistemic logic
inductive prfS5 : ctx agents → form agents → Prop
| ax {Γ} {φ} (h : φ ∈ Γ) : prfS5 Γ φ
| pl1 {Γ} {φ ψ} : prfS5 Γ (form.impl φ (form.impl ψ φ))
| pl2 {Γ} {φ ψ χ} : prfS5 Γ ((φ ⊃ (ψ ⊃ χ)) ⊃ ((φ ⊃ ψ) ⊃ (φ ⊃ χ)))
| pl3 {Γ} {φ ψ} : prfS5 Γ (((¬φ) ⊃ (¬ψ)) ⊃ (((¬φ) ⊃ ψ) ⊃ φ))
| pl4 {Γ} {φ ψ} : prfS5 Γ (φ ⊃ (ψ ⊃ (φ & ψ)))
| pl5 {Γ} {φ ψ} : prfS5 Γ ((φ & ψ) ⊃ φ)
| pl6 {Γ} {φ ψ} : prfS5 Γ ((φ & ψ) ⊃ ψ)
| pl7 {Γ} {φ ψ} : prfS5 Γ (((¬φ) ⊃ (¬ψ)) ⊃ (ψ ⊃ φ))
| kdist {Γ} {φ ψ} {a} : prfS5 Γ ((K a (φ ⊃ ψ)) ⊃ ((K a φ) ⊃ (K a ψ)))
| truth {Γ} {φ} {a} : prfS5 Γ ((K a φ) ⊃ φ)
| posintro {Γ} {φ} {a} : prfS5 Γ ((K a φ) ⊃ (K a (K a φ)))
| negintro {Γ} {φ} {a} : prfS5 Γ ((¬(K a φ)) ⊃ (K a ¬(K a φ)))
| mp {Γ} {φ ψ}
(hpq: prfS5 Γ (φ ⊃ ψ))
(hp : prfS5 Γ φ) : prfS5 Γ ψ
| nec {Γ} {φ} {a}
(hp: prfS5 Γ φ) : prfS5 Γ (K a φ)
-- Define a context
@[reducible] def ctxPA (agents: Type) : Type := set (formPA agents)
--instance : has_emptyc (ctxPA agents) := by apply_instance
notation Γ `∪` φ := set.insert φ Γ
-- Proof system for public announcement logic
inductive prfPA : ctxPA agents → formPA agents → Prop
| ax {Γ} {φ} (h : φ ∈ Γ) : prfPA Γ φ
| pl1 {Γ} {φ ψ} : prfPA Γ (φ ⊃ (ψ ⊃ φ))
| pl2 {Γ} {φ ψ χ} : prfPA Γ ((φ ⊃ (ψ ⊃ χ)) ⊃ ((φ ⊃ ψ) ⊃ (φ ⊃ χ)))
| pl3 {Γ} {φ ψ} : prfPA Γ (((¬φ) ⊃ (¬ψ)) ⊃ (((¬φ) ⊃ ψ) ⊃ φ))
| pl4 {Γ} {φ ψ} : prfPA Γ (φ ⊃ (ψ ⊃ (φ & ψ)))
| pl5 {Γ} {φ ψ} : prfPA Γ ((φ & ψ) ⊃ φ)
| pl6 {Γ} {φ ψ} : prfPA Γ ((φ & ψ) ⊃ ψ)
| pl7 {Γ} {φ ψ} : prfPA Γ (((¬φ) ⊃ (¬ψ)) ⊃ (ψ ⊃ φ))
| kdist {Γ} {φ ψ} {a} : prfPA Γ ((K a (φ ⊃ ψ)) ⊃ ((K a φ) ⊃ (K a ψ)))
| truth {Γ} {φ} {a} : prfPA Γ ((K a φ) ⊃ φ)
| posintro {Γ} {φ} {a} : prfPA Γ ((K a φ) ⊃ (K a (K a φ)))
| negintro {Γ} {φ} {a} : prfPA Γ ((¬(K a φ)) ⊃ (K a ¬(K a φ)))
| mp {Γ} {φ ψ}
(hpq: prfPA Γ (φ ⊃ ψ))
(hp : prfPA Γ φ) : prfPA Γ ψ
| nec {Γ} {φ} {a}
(hp: prfPA Γ φ) : prfPA Γ (K a φ)
| atomicbot {Γ} {φ} : prfPA Γ ((U φ ⊥) ↔ (φ ⊃ ⊥))
| atomicperm {Γ} {φ} {n} : prfPA Γ ((U φ (p n)) ↔ (φ ⊃ (p n)))
| announceneg {Γ} {φ ψ} : prfPA Γ ((U φ (¬ψ)) ↔ (φ ⊃ ¬(U φ ψ)))
| announceconj {Γ}
{φ ψ χ} : prfPA Γ ((U φ (ψ & χ)) ↔ ((U φ ψ) & (U φ χ)))
| announceimp {Γ}
{φ ψ χ} : prfPA Γ ((U φ (ψ ⊃ χ)) ↔ ((U φ ψ) ⊃ (U φ χ)))
| announceknow {Γ}
{φ ψ} {a} : prfPA Γ ((U φ (K a ψ)) ↔ (φ ⊃ (K a (U φ ψ))))
| announcecomp {Γ}
{φ ψ χ} : prfPA Γ ((U φ (U ψ χ)) ↔ (U (φ & (U φ ψ)) χ))
lemma to_prfPA {Γ : ctx agents} {φ : form agents} : prfS5 Γ φ → prfPA (to_PA '' Γ) (to_PA φ) :=
begin
intro h1,
induction h1,
rename h1_h h,
apply prfPA.ax,
use h1_φ,
split,
exact h,
refl,
exact prfPA.pl1,
exact prfPA.pl2,
exact prfPA.pl3,
exact prfPA.pl4,
exact prfPA.pl5,
exact prfPA.pl6,
exact prfPA.pl7,
exact prfPA.kdist,
exact prfPA.truth,
exact prfPA.posintro,
exact prfPA.negintro,
exact prfPA.mp h1_ih_hpq h1_ih_hp,
exact prfPA.nec h1_ih,
end
|
69d000c1234bb9297e784c870f4b4d87d24cfbc1 | ae9f8bf05de0928a4374adc7d6b36af3411d3400 | /src/formal_ml/real_measurable_space.lean | fe32409e20b3c73c22ffb2556d3efa0583b0b9a9 | [
"Apache-2.0"
] | permissive | NeoTim/formal-ml | bc42cf6beba9cd2ed56c1cd054ab4eb5402ed445 | c9cbad2837104160a9832a29245471468748bb8d | refs/heads/master | 1,671,549,160,900 | 1,601,362,989,000 | 1,601,362,989,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 25,785 | lean | /-
Copyright 2020 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-/
import measure_theory.measurable_space
import measure_theory.measure_space
import measure_theory.outer_measure
import measure_theory.lebesgue_measure
import measure_theory.integration
import measure_theory.borel_space
import formal_ml.set
import formal_ml.measurable_space
import formal_ml.topological_space
/-
The big question is how to introduce a measurable function over the reals.
The topology over the reals is induced by a metric space, using absolute difference as a
measure of distance. This metric space induces a topological_space.
The measurable space is the borel space on top of this topological space.
A key result is borel_eq_generate_from_of_subbasis. which shows that if a basis generates a
second countable topology, it generates the borel space as well. For example, consider an
arbitrary open sets on the real line. For any point x in the open set, there exists a real number
e such that the open interval (x-e,x+e) is in the set. Moreover, there is a rational number p in
the interval (x-e,x) and another in (x,x+e), implying x is in (p,q). If we take union of all the
intervals (p,q) for each x in U, then we get U. So, the set of all open intervals with rational
endpoints is a countable basis, implying the reals are a second order topology. It is also
immediately obvious that any open set can be formed with a countable union for the reals.
However, let's look at an arbitrary second countable topology. The generalization of Lindelöf's
lemma shows that for a second countable topology, there is a set of open sets such that any open
set is the countable union. For Munkres, the definition of second countable topology is that
there exists a countable basis. The definition of basis from Munkres is that for every point is
contained in at least one basis set, and for any element x in the intersection of two basis sets A
and B, there is a basis set C containing x that is a subset of B and C.
set, and to generate the topology, one considers arbitrary union of elements in the bases. Thus,
any union of a subset of a countable set must be representable as a countable union. So, there
is a countable union.
The more interesting part of borel_eq_generate_from_of_subbasis is that ANY set that generates
a second countable topology is a basis for the borel space. I am not exactly sure how this
works: regardless, the theorem gives great insights into second countable topologies
(especially, reals, nnreals, ennreals, et cetera) and borel
measures.
To start with, this implies that the cartesian product of the borel spaces is the same as the
borel space of the cartesian product of the topologies (i.e. it commutes). So, results about
continuous functions on two real variables (e.g., add, multiply, power) translate to
measurable functions on measurable spaces.
-/
/-
We begin with basic guarantees about intervals and measurable functions.
-/
set_option pp.implicit true
lemma nnreal_measurable_space_def:nnreal.measurable_space = borel nnreal := rfl
lemma real_measurable_space_def:real.measurable_space = borel real := rfl
lemma ennreal_measurable_space_def:ennreal.measurable_space = borel ennreal := rfl
lemma borel_eq_generate_Iic (α:Type*)
[topological_space α] [topological_space.second_countable_topology α]
[linear_order α] [order_topology α] :
borel α = measurable_space.generate_from (set.range set.Iic) :=
begin
rw borel_eq_generate_Ioi α,
apply le_antisymm,
{
rw measurable_space.generate_from_le_iff,
rw set.subset_def,
intros V A1,
simp,
simp at A1,
cases A1 with y A1,
subst V,
rw ← set.compl_Iic,
apply measurable_space.generate_measurable.compl,
apply measurable_space.generate_measurable.basic,
simp,
},
{
rw measurable_space.generate_from_le_iff,
rw set.subset_def,
intros V A1,
simp,
simp at A1,
cases A1 with y A1,
subst V,
rw ← set.compl_Ioi,
apply measurable_space.generate_measurable.compl,
apply measurable_space.generate_measurable.basic,
simp,
},
end
lemma is_measurable_intro_Iio (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β]
[_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β]
[_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))]
(f:α → β):(∀ b:β, is_measurable (set.preimage f {y|y < b})) →
(@measurable α β M (borel β) f) :=
begin
intros A1,
rw borel_eq_generate_Iio β,
apply generate_from_measurable,
refl,
intros,
cases H with z A2,
subst B,
unfold set.Iio,
apply A1,
end
lemma is_nnreal_measurable_intro_Iio {α:Type*}
[M:measurable_space α] (f:α → nnreal):
(∀ x:nnreal, is_measurable (set.preimage f {y|y < x})) →
(measurable f) :=
begin
rw nnreal_measurable_space_def,
apply is_measurable_intro_Iio,
end
lemma is_ennreal_measurable_intro_Iio {α:Type*}
[M:measurable_space α] (f:α → ennreal):
(∀ x:ennreal, is_measurable (set.preimage f {y|y < x})) →
(measurable f) :=
begin
rw ennreal_measurable_space_def,
apply is_measurable_intro_Iio,
end
lemma is_real_measurable_intro_Iio {α:Type*}
[M:measurable_space α] (f:α → real):
(∀ x:real, is_measurable (set.preimage f {y|y < x})) →
(measurable f) :=
begin
rw real_measurable_space_def,
apply is_measurable_intro_Iio,
end
lemma is_is_measurable_intro_Iio (β: Type*) [_inst_1 : topological_space β]
[_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β]
[_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))]
(b:β):@is_measurable β (borel β) {y|y < b} :=
begin
rw (@borel_eq_generate_Iio β _ _ _ _),
apply measurable_space.is_measurable_generate_from,
simp,
unfold set.Iio,
apply exists.intro b,
refl,
end
lemma is_nnreal_is_measurable_intro_Iio (x:nnreal):
(is_measurable {y|y < x}) :=
begin
rw nnreal_measurable_space_def,
apply is_is_measurable_intro_Iio,
end
lemma is_ennreal_is_measurable_intro_Iio (x:ennreal):
(is_measurable {y|y < x}) :=
begin
rw ennreal_measurable_space_def,
apply is_is_measurable_intro_Iio,
end
lemma is_real_is_measurable_intro_Iio (x:real):
(is_measurable {y|y < x}) :=
begin
rw real_measurable_space_def,
apply is_is_measurable_intro_Iio,
end
lemma is_is_measurable_intro_Ioi (β: Type*) [_inst_1 : topological_space β]
[_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β]
[_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))]
(b:β):@is_measurable β (borel β) {y|b < y} :=
begin
rw (@borel_eq_generate_Ioi β _ _ _ _),
apply measurable_space.is_measurable_generate_from,
simp,
unfold set.Ioi,
apply exists.intro b,
refl,
end
lemma is_nnreal_is_measurable_intro_Ioi (x:nnreal):
(is_measurable {y|x < y}) :=
begin
rw nnreal_measurable_space_def,
apply is_is_measurable_intro_Ioi,
end
lemma is_ennreal_is_measurable_intro_Ioi (x:ennreal):
(is_measurable {y|x < y}) :=
begin
rw ennreal_measurable_space_def,
apply is_is_measurable_intro_Ioi,
end
lemma is_real_is_measurable_intro_Ioi (x:real):
(is_measurable {y|x < y}) :=
begin
rw real_measurable_space_def,
apply is_is_measurable_intro_Ioi,
end
lemma is_measurable_intro_Ioi (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β]
[_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β]
[_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))]
(f:α → β):(∀ b:β, is_measurable (set.preimage f {y|b< y})) →
(@measurable α β M (borel β) f) :=
begin
intros A1,
rw borel_eq_generate_Ioi β,
apply generate_from_measurable,
refl,
intros,
cases H with z A2,
subst B,
unfold set.Ioi,
apply A1,
end
lemma is_nnreal_measurable_intro_Ioi {α:Type*}
[M:measurable_space α] (f:α → nnreal):
(∀ x:nnreal, is_measurable (set.preimage f {y|x < y})) →
(measurable f) :=
begin
rw nnreal_measurable_space_def,
apply is_measurable_intro_Ioi,
end
lemma is_ennreal_measurable_intro_Ioi {α:Type*}
[M:measurable_space α] (f:α → ennreal):
(∀ x:ennreal, is_measurable (set.preimage f {y|x < y})) →
(measurable f) :=
begin
rw ennreal_measurable_space_def,
apply is_measurable_intro_Ioi,
end
lemma is_real_measurable_intro_Ioi {α:Type*}
[M:measurable_space α] (f:α → real):
(∀ x:real, is_measurable (set.preimage f {y|x < y})) →
(measurable f) :=
begin
rw real_measurable_space_def,
apply is_measurable_intro_Ioi,
end
lemma Iic_Iio_compl (β: Type*) [L : linear_order β] (x:β):{y:β|y ≤ x}ᶜ={y:β|x < y} :=
begin
intros,
ext,split;intros A1,
{
simp,
simp at A1,
exact A1,
},
{
simp,
simp at A1,
exact A1,
}
end
lemma Iio_Ici_compl (β: Type*) [L : linear_order β] (x:β):{y:β|x < y}={y:β|y ≤ x}ᶜ :=
begin
intros,
ext,split;intros A1,
{
simp,
simp at A1,
exact A1,
},
{
simp,
simp at A1,
exact A1,
}
end
lemma Ioi_Ici_compl (β: Type*) [L : linear_order β] (x:β):{y:β|x ≤ y}={y:β|y < x}ᶜ :=
begin
intros,
ext,split;intros A1,
{
simp,
simp at A1,
exact A1,
},
{
simp,
simp at A1,
exact A1,
}
end
lemma is_measurable_intro_Iic (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β]
[_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β]
[_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))]
(f:α → β):(∀ b:β, is_measurable (set.preimage f {y|y ≤ b})) →
(@measurable α β M (borel β) f) :=
begin
intros A1,
apply is_measurable_intro_Ioi,
intros x,
rw ← Iic_Iio_compl,
apply is_measurable.compl,
apply A1,
end
lemma is_nnreal_measurable_intro_Iic {α:Type*}
[M:measurable_space α] (f:α → nnreal):
(∀ x:nnreal, is_measurable (set.preimage f {y|y ≤ x})) →
(measurable f) :=
begin
rw nnreal_measurable_space_def,
apply is_measurable_intro_Iic,
end
lemma is_measurable_elim_Iio (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β]
[_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β]
[_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))]
(f:α → β) (b:β):(@measurable α β M (borel β) f) →
(is_measurable (set.preimage f {y|y < b})) :=
begin
intros A1,
apply @measurable_elim α β M (borel β),
apply A1,
rw borel_eq_generate_Iio β,
apply measurable_space.is_measurable_generate_from,
simp,
apply exists.intro b,
unfold set.Iio,
end
lemma is_nnreal_measurable_elim_Iio {α:Type*}
[M:measurable_space α] (f:α → nnreal) (x:nnreal):
(measurable f) →
is_measurable (set.preimage f {y|y < x}) :=
begin
rw nnreal_measurable_space_def,
apply is_measurable_elim_Iio,
end
lemma is_measurable_elim_Iic (α β: Type*) [M:measurable_space α] [_inst_1 : topological_space β]
[_inst_2 : @topological_space.second_countable_topology β _inst_1] [_inst_3 : linear_order β]
[_inst_4 : @order_topology β _inst_1 (@partial_order.to_preorder β (@linear_order.to_partial_order β _inst_3))]
(f:α → β) (b:β):(@measurable α β M (borel β) f) →
(is_measurable (set.preimage f {y|y ≤ b})) :=
begin
intro A1,
apply @measurable_elim α β M (borel β),
apply A1,
rw borel_eq_generate_Ioi β,
have A2:{y|y ≤ b}={y|b<y}ᶜ,
{
ext,split; intro A2A,
{
simp,
simp at A2A,
exact A2A,
},
{
simp,
simp at A2A,
exact A2A,
}
},
rw A2,
apply is_measurable.compl,
apply measurable_space.is_measurable_generate_from,
simp,
apply exists.intro b,
refl,
end
lemma is_nnreal_measurable_elim_Iic {α:Type*}
[M:measurable_space α] (f:α → nnreal) (x:nnreal):
(measurable f) →
(is_measurable (set.preimage f {y|y ≤ x})) :=
begin
rw nnreal_measurable_space_def,
apply is_measurable_elim_Iic,
end
lemma is_real_measurable_elim_Iic {α:Type*}
[M:measurable_space α] (f:α → real) (x:real):
(measurable f) →
(is_measurable (set.preimage f {y|y ≤ x})) :=
begin
rw real_measurable_space_def,
apply is_measurable_elim_Iic,
end
/-
Many more elimination rules can be added here.
-/
lemma borel_def {α:Type*} [topological_space α]:(borel α) =
measurable_space.generate_from {s : set α | is_open s} := rfl
lemma continuous_measurable {α β:Type*} [topological_space α] [topological_space β] (f:α → β):
continuous f →
@measurable α β (borel α) (borel β) f :=
begin
intros A1,
rw borel_def,
rw borel_def,
apply generate_from_measurable,
{
refl,
},
{
intros,
apply measurable_space.is_measurable_generate_from,
unfold continuous at A1,
apply A1,
apply H,
}
end
lemma borel_def_prod_second_countable {α β:Type*} [Tα:topological_space α]
[Tβ:topological_space β]
[SCα:topological_space.second_countable_topology α]
[SCβ:topological_space.second_countable_topology β]
:(@prod.measurable_space α β (borel α) (borel β)) =
(@borel (α × β) (@prod.topological_space α β Tα Tβ)) :=
begin
/- Note that the second countable property is required here. For instance the topology with
a basis [x,y) is not second countable, and its product has a set that is open, but not
coverable with a countable number of sets (e.g., the line y = -x). So, this would not work.
The general idea is that since each topology has a countable basis the product topology
has a countable basis as well.
-/
have A1:∃b:set (set α), set.countable b ∧ Tα = topological_space.generate_from b,
{
apply SCα.is_open_generated_countable,
},
cases A1 with basisα A1,
cases A1 with A2 A3,
rw (@borel_eq_generate_from_of_subbasis α basisα _ _ A3),
have A4:∃b:set (set β), set.countable b ∧ Tβ = topological_space.generate_from b,
{
apply SCβ.is_open_generated_countable,
},
cases A4 with basisβ A4,
cases A4 with A5 A6,
rw (@borel_eq_generate_from_of_subbasis β basisβ _ _ A6),
rw prod_measurable_space_def,
rw @borel_eq_generate_from_of_subbasis,
rw A3,
rw A6,
rw prod_topological_space_def,
end
lemma continuous_measurable_binary {α β γ:Type*} [topological_space α] [topological_space β]
[topological_space γ]
[topological_space.second_countable_topology α]
[topological_space.second_countable_topology β]
(f:α × β → γ):
continuous f → (@measurable (α × β) γ (@prod.measurable_space α β (borel α) (borel β)) (borel γ) f) :=
begin
rw borel_def_prod_second_countable,
apply continuous_measurable,
end
-- Note: there used to be theorems like this in mathlib, but they disappeared
lemma is_measurable_of_is_open {α:Type*} [topological_space α]
(S:set α):is_open S → measurable_space.is_measurable' (borel α) S :=
begin
rw borel_def,
apply measurable_space.is_measurable_generate_from,
end
lemma is_measurable_of_is_closed {α:Type*} [topological_space α]
(S:set α):is_closed S → measurable_space.is_measurable' (borel α) S :=
begin
intro A1,
unfold is_closed at A1,
have A2:S = ((Sᶜ)ᶜ),
{
rw set.compl_compl,
},
rw A2,
apply measurable_space.is_measurable_compl,
apply is_measurable_of_is_open,
apply A1,
end
lemma is_open_is_measurable_binary {α β:Type*} [topological_space α] [topological_space β]
[topological_space.second_countable_topology α]
[topological_space.second_countable_topology β]
(S:set (α × β)):is_open S → measurable_space.is_measurable' (@prod.measurable_space α β (borel α) (borel β)) S :=
begin
rw borel_def_prod_second_countable,
apply is_measurable_of_is_open,
end
lemma is_closed_is_measurable_binary {α β:Type*} [topological_space α] [topological_space β]
[topological_space.second_countable_topology α]
[topological_space.second_countable_topology β]
(S:set (α × β)):is_closed S → measurable_space.is_measurable' (@prod.measurable_space α β (borel α) (borel β)) S :=
begin
rw borel_def_prod_second_countable,
apply is_measurable_of_is_closed,
end
lemma SC_composition {Ω:Type*} [MΩ:measurable_space Ω]
{β:Type*} [Tβ:topological_space β] [CT:topological_space.second_countable_topology β]
(f:Ω → β) (g:Ω → β) (h:β → β → β):
(@measurable Ω β MΩ (borel β) f) →
(@measurable Ω β MΩ (borel β) g) →
continuous2 h →
@measurable Ω β MΩ (borel β) (λ ω:Ω, h (f ω) (g ω)) :=
begin
intros A1 A2 A3,
have A4:@measurable Ω (β × β) MΩ (@prod.measurable_space β β (borel β) (borel β)) (λ ω:Ω, prod.mk (f ω) (g ω)),
{
apply measurable_fun_product_measurable;assumption,
},
have A5:@measurable (β × β) β (@prod.measurable_space β β (borel β) (borel β)) (borel β) (λ p: (β × β), h (p.fst) (p.snd)),
{
rw continuous2_def at A3,
apply continuous_measurable_binary,
apply A3,
},
have A6:(λ ω:Ω, h (f ω) (g ω))=(λ p: (β × β), h (p.fst) (p.snd)) ∘ (λ ω:Ω, prod.mk (f ω) (g ω)),
{
simp,
},
rw A6,
apply @compose_measurable_fun_measurable Ω (β × β) β MΩ (@prod.measurable_space β β (borel β) (borel β)) (borel β) (λ p: (β × β), h (p.fst) (p.snd)) (λ ω:Ω, prod.mk (f ω) (g ω)) A5 A4,
end
lemma nnreal_composition {Ω:Type*} [MΩ:measurable_space Ω]
(f:Ω → nnreal) (g:Ω → nnreal) (h:nnreal → nnreal → nnreal):
measurable f →
measurable g →
continuous2 h →
measurable (λ ω:Ω, h (f ω) (g ω)) :=
begin
apply SC_composition,
end
lemma SC_sum_measurable {Ω:Type*} [MΩ:measurable_space Ω]
{β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β}
{CSR:add_monoid β} {TA:has_continuous_add β}
(X Y:Ω → β):
(@measurable _ _ _ (borel β) X) →
(@measurable _ _ _ (borel β) Y) →
(@measurable _ _ _ (borel β) (λ ω:Ω, (X ω + Y ω))) :=
begin
intros A1 A2,
apply @SC_composition Ω MΩ β T SC X Y,
apply A1,
apply A2,
apply TA.continuous_add,
end
lemma SC_neg_measurable {Ω:Type*} [MΩ:measurable_space Ω]
{β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β}
{CSR:add_group β} {TA:topological_add_group β}
(X:Ω → β):
(@measurable _ _ _ (borel β) X) →
(@measurable _ _ _ (borel β) (λ ω:Ω, (-X ω))) :=
begin
intros A1,
have A2:(λ (ω:Ω), -X ω)=has_neg.neg ∘ X := rfl,
rw A2,
apply @compose_measurable_fun_measurable Ω β β MΩ (borel β) (borel β),
{
apply continuous_measurable,
apply topological_add_group.continuous_neg,
},
{
apply A1,
},
end
--const_measurable {Ω:Type*} [measurable_space Ω] {β:Type*} [measurable_space β] (c:β)
/-
∀ {α : Type u_1} {β : Type u_2} [_inst_1 : measurable_space α] [_inst_2 : measurable_space β] {a : α},
@measurable β α _inst_2 _inst_1 (λ (b : β), a)
-/
def SC_sum_measurable_is_add_submonoid
{Ω:Type*} [MΩ:measurable_space Ω]
{β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β}
{CSR:add_monoid β} {TA:has_continuous_add β}:
is_add_submonoid (@measurable Ω β MΩ (borel β)) := {
zero_mem := @measurable_const β Ω (borel β) MΩ (0:β),
add_mem := @SC_sum_measurable Ω MΩ β T SC CSR TA,
}
def SC_sum_measurable_is_add_subgroup
{Ω:Type*} [MΩ:measurable_space Ω]
{β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β}
{CSR:add_group β} {TA:topological_add_group β}:
is_add_subgroup (@measurable Ω β MΩ (borel β)) := {
zero_mem := @measurable_const β Ω (borel β) MΩ (0:β),
add_mem := @SC_sum_measurable Ω MΩ β T SC (add_group.to_add_monoid β)
(topological_add_group.to_has_continuous_add),
neg_mem := @SC_neg_measurable Ω MΩ β T SC CSR TA,
}
lemma SC_mul_measurable {Ω:Type*} [MΩ:measurable_space Ω]
{β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β}
{CSR:monoid β} {TA:has_continuous_mul β}
(X Y:Ω → β):
(@measurable _ _ _ (borel β) X) → (@measurable _ _ _ (borel β) Y) →
(@measurable _ _ _ (borel β) (λ ω:Ω, (X ω * Y ω))) :=
begin
intros A1 A2,
apply @SC_composition Ω MΩ β T SC X Y,
apply A1,
apply A2,
apply TA.continuous_mul,
end
def SC_mul_measurable_is_submonoid
{Ω:Type*} [MΩ:measurable_space Ω]
{β:Type*} {T:topological_space β} {SC:topological_space.second_countable_topology β}
{CSR:monoid β} {TA:has_continuous_mul β}:
is_submonoid (@measurable Ω β MΩ (borel β)) := {
one_mem := @measurable_const β Ω (borel β) MΩ (1:β),
mul_mem := @SC_mul_measurable Ω MΩ β T SC CSR TA,
}
lemma nnreal_sum_measurable {Ω:Type*} [MΩ:measurable_space Ω] (X Y:Ω → nnreal):
(measurable X) → (measurable Y) →
measurable (λ ω:Ω, (X ω + Y ω)) :=
begin
apply SC_sum_measurable,
apply nnreal.topological_space.second_countable_topology,
apply nnreal.topological_semiring.to_has_continuous_add,
end
lemma finset_sum_measurable {Ω β:Type*} [measurable_space Ω] [decidable_eq β]
{γ:Type*} {T:topological_space γ} {SC:topological_space.second_countable_topology γ}
{CSR:add_comm_monoid γ} {TA:has_continuous_add γ}
(S:finset β) (X:β → Ω → γ):
(∀ b:β, @measurable Ω γ _ (borel γ) (X b)) →
@measurable Ω γ _ (borel γ) (λ ω:Ω, S.sum (λ b:β, ((X b) ω))) :=
begin
apply finset.induction_on S,
{
intros A1,
simp,
apply const_measurable,
},
{
intros b T A2 A3 A4,
-- A3,
-- A4 B,
have A5:(λ (ω : Ω), finset.sum (insert b T) (λ (b : β), X b ω)) =
(λ (ω : Ω), (X b ω) + finset.sum T (λ (b : β), X b ω)),
{
ext,
rw finset.sum_insert,
exact A2,
},
rw A5,
apply SC_sum_measurable,
{
apply SC,
},
{
apply TA,
},
{
apply A4,
},
{
apply A3,
exact A4,
}
}
end
lemma finset_sum_measurable_classical {Ω β:Type*} [MΩ:measurable_space Ω]
{γ:Type*} {T:topological_space γ} {SC:topological_space.second_countable_topology γ}
{CSR:add_comm_monoid γ} {TA:has_continuous_add γ}
(S:finset β) (X:β → Ω → γ):
(∀ b:β, @measurable Ω γ _ (borel γ) (X b)) →
@measurable Ω γ _ (borel γ) (λ ω:Ω, S.sum (λ b:β, ((X b) ω))) :=
begin
have D:decidable_eq β,
{
apply classical.decidable_eq β,
},
apply @finset_sum_measurable Ω β MΩ D γ T SC CSR TA S X,
end
lemma nnreal_finset_sum_measurable {Ω β:Type*} [measurable_space Ω] [decidable_eq β] (S:finset β) (X:β → Ω → nnreal):
(∀ b:β, measurable (X b)) →
measurable (λ ω:Ω, S.sum (λ b:β, ((X b) ω))) :=
begin
apply finset_sum_measurable,
apply_instance,
apply_instance,
end
lemma fintype_sum_measurable {Ω β:Type*} [F:fintype β] (X:β → Ω → nnreal) [M:measurable_space Ω]:
(∀ b:β, measurable (X b)) →
measurable (λ ω:Ω, F.elems.sum (λ b:β, ((X b) ω))) :=
begin
have A1:decidable_eq β,
{
apply classical.decidable_eq,
},
apply @nnreal_finset_sum_measurable Ω β M A1,
end
lemma nnreal_fintype_sum_measurable {Ω β:Type*} [F:fintype β] (X:β → Ω → nnreal) [M:measurable_space Ω]:
(∀ b:β, measurable (X b)) →
measurable (λ ω:Ω, F.elems.sum (λ b:β, ((X b) ω))) :=
begin
have A1:decidable_eq β,
{
apply classical.decidable_eq,
},
apply @nnreal_finset_sum_measurable Ω β M A1,
end
lemma nnreal_div_mul (x y:nnreal):(x/y = x * y⁻¹) :=
begin
refl,
end
----------------Measurable Sets For Inequalities ---------------------------------------------------
-- These are basic results about order topologies.
lemma is_closed_le_alt {α:Type*} [T:topological_space α] [P:linear_order α]
[OT:order_topology α]:is_closed {p:α × α|p.fst ≤ p.snd} :=
begin
have A1:order_closed_topology α,
{
apply @order_topology.to_order_closed_topology,
},
apply A1.is_closed_le',
end
lemma is_measurable_of_le {α:Type*} [T:topological_space α]
[SC:topological_space.second_countable_topology α] [P:linear_order α]
[OT:order_topology α]:measurable_space.is_measurable'
(@prod.measurable_space α α (borel α) (borel α))
{p:α × α|p.fst ≤ p.snd} :=
begin
apply is_closed_is_measurable_binary,
apply is_closed_le_alt,
end
lemma is_measurable_of_eq {α:Type*} [T:topological_space α]
[SC:topological_space.second_countable_topology α]
[T2:t2_space α]:measurable_space.is_measurable'
(@prod.measurable_space α α (borel α) (borel α))
{p:α × α|p.fst = p.snd} :=
begin
apply is_closed_is_measurable_binary,
apply is_closed_diagonal,
end
|
2eec682e254ed5665f82cbd2ed8231c58d79e90a | 8cae430f0a71442d02dbb1cbb14073b31048e4b0 | /src/category_theory/concrete_category/elementwise.lean | e41d647a4eaedc3621bf18efc4511854f5325102 | [
"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 | 754 | lean | /-
Copyright (c) 2022 Andrew Yang. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Andrew Yang
-/
import tactic.elementwise
import category_theory.limits.has_limits
import category_theory.limits.shapes.kernels
import category_theory.concrete_category.basic
import tactic.fresh_names
/-!
> THIS FILE IS SYNCHRONIZED WITH MATHLIB4.
> Any changes to this file require a corresponding PR to mathlib4.
In this file we provide various simp lemmas in its elementwise form via `tactic.elementwise`.
-/
open category_theory category_theory.limits
attribute [elementwise]
cone.w limit.lift_π limit.w cocone.w colimit.ι_desc colimit.w
kernel.lift_ι cokernel.π_desc
kernel.condition cokernel.condition
|
f40ef37f002818ad466e18c3f8e3f2295be50e57 | 74addaa0e41490cbaf2abd313a764c96df57b05d | /Mathlib/order/directed.lean | c1f6003db77f775cb7ce8e08eaa30a336820572d | [] | 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,404 | 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 Mathlib.PrePort
import Mathlib.Lean3Lib.init.default
import Mathlib.order.lattice
import Mathlib.data.set.basic
import Mathlib.PostPort
universes u w v u_1 l
namespace Mathlib
/-- A family of elements of α is directed (with respect to a relation `≼` on α)
if there is a member of the family `≼`-above any pair in the family. -/
def directed {α : Type u} {ι : Sort w} (r : α → α → Prop) (f : ι → α) :=
∀ (x y : ι), ∃ (z : ι), r (f x) (f z) ∧ r (f y) (f z)
/-- A subset of α is directed if there is an element of the set `≼`-above any
pair of elements in the set. -/
def directed_on {α : Type u} (r : α → α → Prop) (s : set α) :=
∀ (x : α) (H : x ∈ s) (y : α) (H : y ∈ s), ∃ (z : α), ∃ (H : z ∈ s), r x z ∧ r y z
theorem directed_on_iff_directed {α : Type u} {r : α → α → Prop} {s : set α} : directed_on r s ↔ directed r coe := sorry
theorem directed_on.directed_coe {α : Type u} {r : α → α → Prop} {s : set α} : directed_on r s → directed r coe :=
iff.mp directed_on_iff_directed
theorem directed_on_image {α : Type u} {β : Type v} {r : α → α → Prop} {s : set β} {f : β → α} : directed_on r (f '' s) ↔ directed_on (f ⁻¹'o r) s := sorry
theorem directed_on.mono {α : Type u} {r : α → α → Prop} {s : set α} (h : directed_on r s) {r' : α → α → Prop} (H : ∀ {a b : α}, r a b → r' a b) : directed_on r' s := sorry
theorem directed_comp {α : Type u} {β : Type v} {r : α → α → Prop} {ι : Sort u_1} {f : ι → β} {g : β → α} : directed r (g ∘ f) ↔ directed (g ⁻¹'o r) f :=
iff.rfl
theorem directed.mono {α : Type u} {r : α → α → Prop} {s : α → α → Prop} {ι : Sort u_1} {f : ι → α} (H : ∀ (a b : α), r a b → s a b) (h : directed r f) : directed s f := sorry
theorem directed.mono_comp {α : Type u} {β : Type v} (r : α → α → Prop) {ι : Sort u_1} {rb : β → β → Prop} {g : α → β} {f : ι → α} (hg : ∀ {x y : α}, r x y → rb (g x) (g y)) (hf : directed r f) : directed rb (g ∘ f) :=
iff.mpr directed_comp (directed.mono hg hf)
/-- A monotone function on a sup-semilattice is directed. -/
theorem directed_of_sup {α : Type u} {β : Type v} [semilattice_sup α] {f : α → β} {r : β → β → Prop} (H : ∀ {i j : α}, i ≤ j → r (f i) (f j)) : directed r f :=
fun (a b : α) => Exists.intro (a ⊔ b) { left := H le_sup_left, right := H le_sup_right }
/-- An antimonotone function on an inf-semilattice is directed. -/
theorem directed_of_inf {α : Type u} {β : Type v} [semilattice_inf α] {r : β → β → Prop} {f : α → β} (hf : ∀ (a₁ a₂ : α), a₁ ≤ a₂ → r (f a₂) (f a₁)) : directed r f :=
fun (x y : α) => Exists.intro (x ⊓ y) { left := hf (x ⊓ y) x inf_le_left, right := hf (x ⊓ y) y inf_le_right }
/-- A `preorder` is a `directed_order` if for any two elements `i`, `j`
there is an element `k` such that `i ≤ k` and `j ≤ k`. -/
class directed_order (α : Type u)
extends preorder α
where
directed : ∀ (i j : α), ∃ (k : α), i ≤ k ∧ j ≤ k
protected instance linear_order.to_directed_order (α : Type u_1) [linear_order α] : directed_order α :=
directed_order.mk sorry
|
a7f967a60c301bad9cf7df3c4313126f9eb60cb8 | 6094e25ea0b7699e642463b48e51b2ead6ddc23f | /library/data/set/equinumerosity.lean | 405f40516a3c4782d1bdef5d0a77116ab658ede6 | [
"Apache-2.0"
] | permissive | gbaz/lean | a7835c4e3006fbbb079e8f8ffe18aacc45adebfb | a501c308be3acaa50a2c0610ce2e0d71becf8032 | refs/heads/master | 1,611,198,791,433 | 1,451,339,111,000 | 1,451,339,111,000 | 48,713,797 | 0 | 0 | null | 1,451,338,939,000 | 1,451,338,939,000 | null | UTF-8 | Lean | false | false | 8,788 | lean | /-
Copyright (c) 2015 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Jeremy Avigad
Two sets are equinumerous, or equipollent, if there is a bijection between them. It is sometimes
said that two such sets "have the same cardinality."
-/
import .classical_inverse data.nat
open eq.ops classical nat
/- two versions of Cantor's theorem -/
namespace set
variables {X : Type} {A : set X}
theorem not_surj_on_pow (f : X → set X) : ¬ surj_on f A (𝒫 A) :=
let diag := {x ∈ A | x ∉ f x} in
have diag ⊆ A, from sep_subset _ _,
assume H : surj_on f A (𝒫 A),
obtain x [(xA : x ∈ A) (Hx : f x = diag)], from H `diag ⊆ A`,
have x ∉ f x, from
suppose x ∈ f x,
have x ∈ diag, from Hx ▸ this,
have x ∉ f x, from and.right this,
show false, from this `x ∈ f x`,
have x ∈ diag, from and.intro xA this,
have x ∈ f x, from Hx⁻¹ ▸ this,
show false, from `x ∉ f x` this
theorem not_inj_on_pow {f : set X → X} (H : maps_to f (𝒫 A) A) : ¬ inj_on f (𝒫 A) :=
let diag := f '[{x ∈ 𝒫 A | f x ∉ x}] in
have diag ⊆ A, from image_subset_of_maps_to H (sep_subset _ _),
assume H₁ : inj_on f (𝒫 A),
have f diag ∈ diag, from by_contradiction
(suppose f diag ∉ diag,
have diag ∈ {x ∈ 𝒫 A | f x ∉ x}, from and.intro `diag ⊆ A` this,
have f diag ∈ diag, from mem_image_of_mem f this,
show false, from `f diag ∉ diag` this),
obtain x [(Hx : x ∈ 𝒫 A ∧ f x ∉ x) (fxeq : f x = f diag)], from this,
have x = diag, from H₁ (and.left Hx) `diag ⊆ A` fxeq,
have f diag ∉ diag, from this ▸ and.right Hx,
show false, from this `f diag ∈ diag`
end set
/-
The Schröder-Bernstein theorem. The proof below is nonconstructive, in three ways:
(1) We need a left inverse to g (we could get around this by supplying one).
(2) The definition of h below assumes that membership in Union U is decidable.
(3) We ultimately case split on whether B is empty, and choose an element if it isn't.
Rather than mark every auxiliary construction as "private", we put them all in a
separate namespace.
-/
namespace schroeder_bernstein
section
open set
parameters {X Y : Type}
parameter {A : set X}
parameter {B : set Y}
parameter {f : X → Y}
parameter (f_maps_to : maps_to f A B)
parameter (finj : inj_on f A)
parameter {g : Y → X}
parameter (g_maps_to : maps_to g B A)
parameter (ginj : inj_on g B)
parameter {dflt : Y} -- for now, assume B is nonempty
parameter (dfltB : dflt ∈ B)
/- g⁻¹ : A → B -/
noncomputable definition ginv : X → Y := inv_fun g B dflt
lemma ginv_maps_to : maps_to ginv A B :=
maps_to_inv_fun dfltB
lemma ginv_g_eq {b : Y} (bB : b ∈ B) : ginv (g b) = b :=
left_inv_on_inv_fun_of_inj_on dflt ginj bB
/- define a sequence of sets U -/
definition U : ℕ → set X
| U 0 := A \ g '[B]
| U (n + 1) := g '[f '[U n]]
lemma U_subset_A : ∀ n, U n ⊆ A
| 0 := show U 0 ⊆ A,
from diff_subset _ _
| (n + 1) := have f '[U n] ⊆ B,
from image_subset_of_maps_to f_maps_to (U_subset_A n),
show U (n + 1) ⊆ A,
from image_subset_of_maps_to g_maps_to this
lemma g_ginv_eq {a : X} (aA : a ∈ A) (anU : a ∉ Union U) : g (ginv a) = a :=
have a ∈ g '[B], from by_contradiction
(suppose a ∉ g '[B],
have a ∈ U 0, from and.intro aA this,
have a ∈ Union U, from exists.intro 0 this,
show false, from anU this),
obtain b [(bB : b ∈ B) (gbeq : g b = a)], from this,
calc
g (ginv a) = g (ginv (g b)) : gbeq
... = g b : ginv_g_eq bB
... = a : gbeq
/- h : A → B -/
noncomputable definition h x := if x ∈ Union U then f x else ginv x
lemma h_maps_to : maps_to h A B :=
take a,
suppose a ∈ A,
show h a ∈ B, from
by_cases
(suppose a ∈ Union U,
by+ rewrite [↑h, if_pos this]; exact f_maps_to `a ∈ A`)
(suppose a ∉ Union U,
by+ rewrite [↑h, if_neg this]; exact ginv_maps_to `a ∈ A`)
/- h is injective -/
lemma aux {a₁ a₂ : X} (H₁ : a₁ ∈ Union U) (a₂A : a₂ ∈ A) (heq : h a₁ = h a₂) : a₂ ∈ Union U :=
obtain n (a₁Un : a₁ ∈ U n), from H₁,
have ha₁eq : h a₁ = f a₁,
from dif_pos H₁,
show a₂ ∈ Union U, from by_contradiction
(suppose a₂ ∉ Union U,
have ha₂eq : h a₂ = ginv a₂,
from dif_neg this,
have g (f a₁) = a₂, from calc
g (f a₁) = g (h a₁) : ha₁eq
... = g (h a₂) : heq
... = g (ginv a₂) : ha₂eq
... = a₂ : g_ginv_eq a₂A `a₂ ∉ Union U`,
have g (f a₁) ∈ g '[f '[U n]],
from mem_image_of_mem g (mem_image_of_mem f a₁Un),
have a₂ ∈ U (n + 1),
from `g (f a₁) = a₂` ▸ this,
have a₂ ∈ Union U,
from exists.intro _ this,
show false, from `a₂ ∉ Union U` `a₂ ∈ Union U`)
lemma h_inj : inj_on h A :=
take a₁ a₂,
suppose a₁ ∈ A,
suppose a₂ ∈ A,
assume heq : h a₁ = h a₂,
show a₁ = a₂, from
by_cases
(assume a₁UU : a₁ ∈ Union U,
have a₂UU : a₂ ∈ Union U,
from aux a₁UU `a₂ ∈ A` heq,
have f a₁ = f a₂, from calc
f a₁ = h a₁ : dif_pos a₁UU
... = h a₂ : heq
... = f a₂ : dif_pos a₂UU,
show a₁ = a₂, from
finj `a₁ ∈ A` `a₂ ∈ A` this)
(assume a₁nUU : a₁ ∉ Union U,
have a₂nUU : a₂ ∉ Union U,
from assume H, a₁nUU (aux H `a₁ ∈ A` heq⁻¹),
have eq₁ : g (ginv a₁) = a₁, from g_ginv_eq `a₁ ∈ A` a₁nUU,
have eq₂ : g (ginv a₂) = a₂, from g_ginv_eq `a₂ ∈ A` a₂nUU,
have ginv a₁ = ginv a₂, from calc
ginv a₁ = h a₁ : dif_neg a₁nUU
... = h a₂ : heq
... = ginv a₂ : dif_neg a₂nUU,
show a₁ = a₂, from calc
a₁ = g (ginv a₁) : eq₁ -- g_ginv_eq `a₁ ∈ A` a₁nUU
... = g (ginv a₂) : this
... = a₂ : eq₂) -- g_ginv_eq `a₂ ∈ A` a₂nUU)
/- h is surjective -/
lemma h_surj : surj_on h A B :=
take b,
suppose b ∈ B,
have g b ∈ A, from g_maps_to this,
by_cases
(suppose g b ∈ Union U,
obtain n (gbUn : g b ∈ U n), from this,
using ginj f_maps_to,
begin
cases n with n,
{have g b ∈ U 0, from gbUn,
have g b ∉ g '[B], from and.right this,
have g b ∈ g '[B], from mem_image_of_mem g `b ∈ B`,
show b ∈ h '[A], from absurd `g b ∈ g '[B]` `g b ∉ g '[B]`},
{have g b ∈ U (succ n), from gbUn,
have g b ∈ g '[f '[U n]], from this,
obtain b' [(b'fUn : b' ∈ f '[U n]) (geq : g b' = g b)], from this,
obtain a [(aUn : a ∈ U n) (faeq : f a = b')], from b'fUn,
have g (f a) = g b, by rewrite [faeq, geq],
have a ∈ A, from U_subset_A n aUn,
have f a ∈ B, from f_maps_to this,
have f a = b, from ginj `f a ∈ B` `b ∈ B` `g (f a) = g b`,
have a ∈ Union U, from exists.intro n aUn,
have h a = f a, from dif_pos this,
show b ∈ h '[A], from mem_image `a ∈ A` (`h a = f a` ⬝ `f a = b`)}
end)
(suppose g b ∉ Union U,
have eq₁ : h (g b) = ginv (g b), from dif_neg this,
have eq₂ : ginv (g b) = b, from ginv_g_eq `b ∈ B`,
show b ∈ h '[A], from mem_image `g b ∈ A` (eq₁ ⬝ eq₂))
end
end schroeder_bernstein
namespace set
section
parameters {X Y : Type}
parameter {A : set X}
parameter {B : set Y}
parameter {f : X → Y}
parameter (f_maps_to : maps_to f A B)
parameter (finj : inj_on f A)
parameter {g : Y → X}
parameter (g_maps_to : maps_to g B A)
parameter (ginj : inj_on g B)
theorem schroeder_bernstein : ∃ h, bij_on h A B :=
by_cases
(assume H : ∀ b, b ∉ B,
have fsurj : surj_on f A B, from take b, suppose b ∈ B, absurd this !H,
exists.intro f (and.intro f_maps_to (and.intro finj fsurj)))
(assume H : ¬ ∀ b, b ∉ B,
have ∃ b, b ∈ B, from exists_of_not_forall_not H,
obtain b bB, from this,
let h := @schroeder_bernstein.h X Y A B f g b in
have h_maps_to : maps_to h A B, from schroeder_bernstein.h_maps_to f_maps_to bB,
have hinj : inj_on h A, from schroeder_bernstein.h_inj finj ginj, -- ginj,
have hsurj : surj_on h A B, from schroeder_bernstein.h_surj f_maps_to g_maps_to ginj,
exists.intro h (and.intro h_maps_to (and.intro hinj hsurj)))
end
end set
|
12515390117b4bfa1fa089687901caac70e4eca0 | d0c6b2ba2af981e9ab0a98f6e169262caad4b9b9 | /stage0/src/Init/Data/Range.lean | 9f5655565c246528b50bf3da423470a9f45b2ebc | [
"Apache-2.0"
] | permissive | fizruk/lean4 | 953b7dcd76e78c17a0743a2c1a918394ab64bbc0 | 545ed50f83c570f772ade4edbe7d38a078cbd761 | refs/heads/master | 1,677,655,987,815 | 1,612,393,885,000 | 1,612,393,885,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 1,458 | lean | /-
Copyright (c) 2020 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
prelude
import Init.Meta
namespace Std
-- We put `Range` in `Init` because we want the notation `[i:j]` without importing `Std`
-- We don't put `Range` in the top-level namespace to avoid collisions with user defined types
structure Range where
start : Nat := 0
stop : Nat
step : Nat := 1
namespace Range
universes u v
@[inline] def forIn {β : Type u} {m : Type u → Type v} [Monad m] (range : Range) (init : β) (f : Nat → β → m (ForInStep β)) : m β :=
let rec @[specialize] loop (i : Nat) (j : Nat) (b : β) : m β := do
if j ≥ range.stop then
pure b
else match i with
| 0 => pure b
| i+1 => match ← f j b with
| ForInStep.done b => pure b
| ForInStep.yield b => loop i (j + range.step) b
loop range.stop range.start init
syntax:max "[" ":" term "]" : term
syntax:max "[" term ":" term "]" : term
syntax:max "[" ":" term ":" term "]" : term
syntax:max "[" term ":" term ":" term "]" : term
macro_rules
| `([ : $stop]) => `({ stop := $stop : Range })
| `([ $start : $stop ]) => `({ start := $start, stop := $stop : Range })
| `([ $start : $stop : $step ]) => `({ start := $start, stop := $stop, step := $step : Range })
| `([ : $stop : $step ]) => `({ stop := $stop, step := $step : Range })
end Range
end Std
|
10cbf02ed426dd047504ff3b5fe21d67477f2a2b | 618003631150032a5676f229d13a079ac875ff77 | /src/analysis/ODE/gronwall.lean | f3efe9490a34309f4524c3e3ea73e98b264014f6 | [
"Apache-2.0"
] | permissive | awainverse/mathlib | 939b68c8486df66cfda64d327ad3d9165248c777 | ea76bd8f3ca0a8bf0a166a06a475b10663dec44a | refs/heads/master | 1,659,592,962,036 | 1,590,987,592,000 | 1,590,987,592,000 | 268,436,019 | 1 | 0 | Apache-2.0 | 1,590,990,500,000 | 1,590,990,500,000 | null | UTF-8 | Lean | false | false | 13,209 | lean | /-
Copyright (c) 2020 Yury Kudryashov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yury Kudryashov
-/
import analysis.calculus.mean_value
import analysis.special_functions.exp_log
/-!
# Grönwall's inequality
The main technical result of this file is the Grönwall-like inequality
`norm_le_gronwall_bound_of_norm_deriv_right_le`. It states that if `f : ℝ → E` satisfies `∥f a∥ ≤ δ`
and `∀ x ∈ [a, b), ∥f' x∥ ≤ K * ∥f x∥ + ε`, then for all `x ∈ [a, b]` we have `∥f x∥ ≤ δ * exp (K *
x) + (ε / K) * (exp (K * x) - 1)`.
Then we use this inequality to prove some estimates on the possible rate of growth of the distance
between two approximate or exact solutions of an ordinary differential equation.
The proofs are based on [Hubbard and West, *Differential Equations: A Dynamical Systems Approach*,
Sec. 4.5][HubbardWest-ode], where `norm_le_gronwall_bound_of_norm_deriv_right_le` is called
“Fundamental Inequality”.
## TODO
- Once we have FTC, prove an inequality for a function satisfying `∥f' x∥ ≤ K x * ∥f x∥ + ε`,
or more generally `liminf_{y→x+0} (f y - f x)/(y - x) ≤ K x * f x + ε` with any sign
of `K x` and `f x`.
-/
variables {E : Type*} [normed_group E] [normed_space ℝ E]
{F : Type*} [normed_group F] [normed_space ℝ F]
open metric set asymptotics filter real
open_locale classical
/-! ### Technical lemmas about `gronwall_bound` -/
/-- Upper bound used in several Grönwall-like inequalities. -/
noncomputable def gronwall_bound (δ K ε x : ℝ) : ℝ :=
if K = 0 then δ + ε * x else δ * exp (K * x) + (ε / K) * (exp (K * x) - 1)
lemma gronwall_bound_K0 (δ ε : ℝ) : gronwall_bound δ 0 ε = λ x, δ + ε * x :=
funext $ λ x, if_pos rfl
lemma gronwall_bound_of_K_ne_0 {δ K ε : ℝ} (hK : K ≠ 0) :
gronwall_bound δ K ε = λ x, δ * exp (K * x) + (ε / K) * (exp (K * x) - 1) :=
funext $ λ x, if_neg hK
lemma has_deriv_at_gronwall_bound (δ K ε x : ℝ) :
has_deriv_at (gronwall_bound δ K ε) (K * (gronwall_bound δ K ε x) + ε) x :=
begin
by_cases hK : K = 0,
{ subst K,
simp only [gronwall_bound_K0, zero_mul, zero_add],
convert ((has_deriv_at_id x).const_mul ε).const_add δ,
rw [mul_one] },
{ simp only [gronwall_bound_of_K_ne_0 hK],
convert (((has_deriv_at_id x).const_mul K).exp.const_mul δ).add
((((has_deriv_at_id x).const_mul K).exp.sub_const 1).const_mul (ε / K)) using 1,
simp only [id, mul_add, (mul_assoc _ _ _).symm, mul_comm _ K, mul_div_cancel' _ hK],
ring }
end
lemma has_deriv_at_gronwall_bound_shift (δ K ε x a : ℝ) :
has_deriv_at (λ y, gronwall_bound δ K ε (y - a)) (K * (gronwall_bound δ K ε (x - a)) + ε) x :=
begin
convert (has_deriv_at_gronwall_bound δ K ε _).comp x ((has_deriv_at_id x).sub_const a),
rw [id, mul_one]
end
lemma gronwall_bound_x0 (δ K ε : ℝ) : gronwall_bound δ K ε 0 = δ :=
begin
by_cases hK : K = 0,
{ simp only [gronwall_bound, if_pos hK, mul_zero, add_zero] },
{ simp only [gronwall_bound, if_neg hK, mul_zero, exp_zero, sub_self, mul_one, add_zero] }
end
lemma gronwall_bound_ε0 (δ K x : ℝ) : gronwall_bound δ K 0 x = δ * exp (K * x) :=
begin
by_cases hK : K = 0,
{ simp only [gronwall_bound_K0, hK, zero_mul, exp_zero, add_zero, mul_one] },
{ simp only [gronwall_bound_of_K_ne_0 hK, zero_div, zero_mul, add_zero] }
end
lemma gronwall_bound_ε0_δ0 (K x : ℝ) : gronwall_bound 0 K 0 x = 0 :=
by simp only [gronwall_bound_ε0, zero_mul]
lemma gronwall_bound_continuous_ε (δ K x : ℝ) : continuous (λ ε, gronwall_bound δ K ε x) :=
begin
by_cases hK : K = 0,
{ simp only [gronwall_bound_K0, hK],
exact continuous_const.add (continuous_id.mul continuous_const) },
{ simp only [gronwall_bound_of_K_ne_0 hK],
exact continuous_const.add ((continuous_id.mul continuous_const).mul continuous_const) }
end
/-! ### Inequality and corollaries -/
/-- A Grönwall-like inequality: if `f : ℝ → ℝ` is continuous on `[a, b]` and satisfies
the inequalities `f a ≤ δ` and
`∀ x ∈ [a, b), liminf_{z→x+0} (f z - f x)/(z - x) ≤ K * (f x) + ε`, then `f x`
is bounded by `gronwall_bound δ K ε (x - a)` on `[a, b]`.
See also `norm_le_gronwall_bound_of_norm_deriv_right_le` for a version bounding `∥f x∥`,
`f : ℝ → E`. -/
theorem le_gronwall_bound_of_liminf_deriv_right_le {f f' : ℝ → ℝ} {δ K ε : ℝ} {a b : ℝ}
(hf : continuous_on f (Icc a b))
(hf' : ∀ x ∈ Ico a b, ∀ r, f' x < r →
∃ᶠ z in nhds_within x (Ioi x), (z - x)⁻¹ * (f z - f x) < r)
(ha : f a ≤ δ) (bound : ∀ x ∈ Ico a b, f' x ≤ K * f x + ε) :
∀ x ∈ Icc a b, f x ≤ gronwall_bound δ K ε (x - a) :=
begin
have H : ∀ x ∈ Icc a b, ∀ ε' ∈ Ioi ε, f x ≤ gronwall_bound δ K ε' (x - a),
{ assume x hx ε' hε',
apply image_le_of_liminf_slope_right_lt_deriv_boundary hf hf',
{ rwa [sub_self, gronwall_bound_x0] },
{ exact λ x, has_deriv_at_gronwall_bound_shift δ K ε' x a },
{ assume x hx hfB,
rw [← hfB],
apply lt_of_le_of_lt (bound x hx),
exact add_lt_add_left hε' _ },
{ exact hx } },
assume x hx,
change f x ≤ (λ ε', gronwall_bound δ K ε' (x - a)) ε,
convert continuous_within_at_const.closure_le _ _ (H x hx),
{ simp only [closure_Ioi, left_mem_Ici] },
exact (gronwall_bound_continuous_ε δ K (x - a)).continuous_within_at
end
/-- A Grönwall-like inequality: if `f : ℝ → E` is continuous on `[a, b]`, has right derivative
`f' x` at every point `x ∈ [a, b)`, and satisfies the inequalities `∥f a∥ ≤ δ`,
`∀ x ∈ [a, b), ∥f' x∥ ≤ K * ∥f x∥ + ε`, then `∥f x∥` is bounded by `gronwall_bound δ K ε (x - a)`
on `[a, b]`. -/
theorem norm_le_gronwall_bound_of_norm_deriv_right_le {f f' : ℝ → E} {δ K ε : ℝ} {a b : ℝ}
(hf : continuous_on f (Icc a b)) (hf' : ∀ x ∈ Ico a b, has_deriv_within_at f (f' x) (Ioi x) x)
(ha : ∥f a∥ ≤ δ) (bound : ∀ x ∈ Ico a b, ∥f' x∥ ≤ K * ∥f x∥ + ε) :
∀ x ∈ Icc a b, ∥f x∥ ≤ gronwall_bound δ K ε (x - a) :=
le_gronwall_bound_of_liminf_deriv_right_le (continuous_norm.comp_continuous_on hf)
(λ x hx r hr, (hf' x hx).liminf_right_slope_norm_le hr) ha bound
/-- If `f` and `g` are two approximate solutions of the same ODE, then the distance between them
can't grow faster than exponentially. This is a simple corollary of Grönwall's inequality, and some
people call this Grönwall's inequality too.
This version assumes all inequalities to be true in some time-dependent set `s t`,
and assumes that the solutions never leave this set. -/
theorem dist_le_of_approx_trajectories_ODE_of_mem_set {v : ℝ → E → E} {s : ℝ → set E}
{K : ℝ} (hv : ∀ t, ∀ x y ∈ s t, dist (v t x) (v t y) ≤ K * dist x y)
{f g f' g' : ℝ → E} {a b : ℝ} {εf εg δ : ℝ}
(hf : continuous_on f (Icc a b))
(hf' : ∀ t ∈ Ico a b, has_deriv_within_at f (f' t) (Ioi t) t)
(f_bound : ∀ t ∈ Ico a b, dist (f' t) (v t (f t)) ≤ εf)
(hfs : ∀ t ∈ Ico a b, f t ∈ s t)
(hg : continuous_on g (Icc a b))
(hg' : ∀ t ∈ Ico a b, has_deriv_within_at g (g' t) (Ioi t) t)
(g_bound : ∀ t ∈ Ico a b, dist (g' t) (v t (g t)) ≤ εg)
(hgs : ∀ t ∈ Ico a b, g t ∈ s t)
(ha : dist (f a) (g a) ≤ δ) :
∀ t ∈ Icc a b, dist (f t) (g t) ≤ gronwall_bound δ K (εf + εg) (t - a) :=
begin
simp only [dist_eq_norm] at ha ⊢,
have h_deriv : ∀ t ∈ Ico a b, has_deriv_within_at (λ t, f t - g t) (f' t - g' t) (Ioi t) t,
from λ t ht, (hf' t ht).sub (hg' t ht),
apply norm_le_gronwall_bound_of_norm_deriv_right_le (hf.sub hg) h_deriv ha,
assume t ht,
have := dist_triangle4_right (f' t) (g' t) (v t (f t)) (v t (g t)),
rw [dist_eq_norm] at this,
apply le_trans this,
apply le_trans (add_le_add (add_le_add (f_bound t ht) (g_bound t ht))
(hv t (f t) (g t) (hfs t ht) (hgs t ht))),
rw [dist_eq_norm, add_comm]
end
/-- If `f` and `g` are two approximate solutions of the same ODE, then the distance between them
can't grow faster than exponentially. This is a simple corollary of Grönwall's inequality, and some
people call this Grönwall's inequality too.
This version assumes all inequalities to be true in the whole space. -/
theorem dist_le_of_approx_trajectories_ODE {v : ℝ → E → E}
{K : nnreal} (hv : ∀ t, lipschitz_with K (v t))
{f g f' g' : ℝ → E} {a b : ℝ} {εf εg δ : ℝ}
(hf : continuous_on f (Icc a b))
(hf' : ∀ t ∈ Ico a b, has_deriv_within_at f (f' t) (Ioi t) t)
(f_bound : ∀ t ∈ Ico a b, dist (f' t) (v t (f t)) ≤ εf)
(hg : continuous_on g (Icc a b))
(hg' : ∀ t ∈ Ico a b, has_deriv_within_at g (g' t) (Ioi t) t)
(g_bound : ∀ t ∈ Ico a b, dist (g' t) (v t (g t)) ≤ εg)
(ha : dist (f a) (g a) ≤ δ) :
∀ t ∈ Icc a b, dist (f t) (g t) ≤ gronwall_bound δ K (εf + εg) (t - a) :=
have hfs : ∀ t ∈ Ico a b, f t ∈ (@univ E), from λ t ht, trivial,
dist_le_of_approx_trajectories_ODE_of_mem_set (λ t x y hx hy, (hv t).dist_le_mul x y)
hf hf' f_bound hfs hg hg' g_bound (λ t ht, trivial) ha
/-- If `f` and `g` are two exact solutions of the same ODE, then the distance between them
can't grow faster than exponentially. This is a simple corollary of Grönwall's inequality, and some
people call this Grönwall's inequality too.
This version assumes all inequalities to be true in some time-dependent set `s t`,
and assumes that the solutions never leave this set. -/
theorem dist_le_of_trajectories_ODE_of_mem_set {v : ℝ → E → E} {s : ℝ → set E}
{K : ℝ} (hv : ∀ t, ∀ x y ∈ s t, dist (v t x) (v t y) ≤ K * dist x y)
{f g : ℝ → E} {a b : ℝ} {δ : ℝ}
(hf : continuous_on f (Icc a b))
(hf' : ∀ t ∈ Ico a b, has_deriv_within_at f (v t (f t)) (Ioi t) t)
(hfs : ∀ t ∈ Ico a b, f t ∈ s t)
(hg : continuous_on g (Icc a b))
(hg' : ∀ t ∈ Ico a b, has_deriv_within_at g (v t (g t)) (Ioi t) t)
(hgs : ∀ t ∈ Ico a b, g t ∈ s t)
(ha : dist (f a) (g a) ≤ δ) :
∀ t ∈ Icc a b, dist (f t) (g t) ≤ δ * exp (K * (t - a)) :=
begin
have f_bound : ∀ t ∈ Ico a b, dist (v t (f t)) (v t (f t)) ≤ 0,
by { intros, rw [dist_self] },
have g_bound : ∀ t ∈ Ico a b, dist (v t (g t)) (v t (g t)) ≤ 0,
by { intros, rw [dist_self] },
assume t ht,
have := dist_le_of_approx_trajectories_ODE_of_mem_set hv hf hf' f_bound hfs hg hg' g_bound
hgs ha t ht,
rwa [zero_add, gronwall_bound_ε0] at this,
end
/-- If `f` and `g` are two exact solutions of the same ODE, then the distance between them
can't grow faster than exponentially. This is a simple corollary of Grönwall's inequality, and some
people call this Grönwall's inequality too.
This version assumes all inequalities to be true in the whole space. -/
theorem dist_le_of_trajectories_ODE {v : ℝ → E → E}
{K : nnreal} (hv : ∀ t, lipschitz_with K (v t))
{f g : ℝ → E} {a b : ℝ} {δ : ℝ}
(hf : continuous_on f (Icc a b))
(hf' : ∀ t ∈ Ico a b, has_deriv_within_at f (v t (f t)) (Ioi t) t)
(hg : continuous_on g (Icc a b))
(hg' : ∀ t ∈ Ico a b, has_deriv_within_at g (v t (g t)) (Ioi t) t)
(ha : dist (f a) (g a) ≤ δ) :
∀ t ∈ Icc a b, dist (f t) (g t) ≤ δ * exp (K * (t - a)) :=
have hfs : ∀ t ∈ Ico a b, f t ∈ (@univ E), from λ t ht, trivial,
dist_le_of_trajectories_ODE_of_mem_set (λ t x y hx hy, (hv t).dist_le_mul x y)
hf hf' hfs hg hg' (λ t ht, trivial) ha
/-- There exists only one solution of an ODE \(\dot x=v(t, x)\) in a set `s ⊆ ℝ × E` with
a given initial value provided that RHS is Lipschitz continuous in `x` within `s`,
and we consider only solutions included in `s`. -/
theorem ODE_solution_unique_of_mem_set {v : ℝ → E → E} {s : ℝ → set E}
{K : ℝ} (hv : ∀ t, ∀ x y ∈ s t, dist (v t x) (v t y) ≤ K * dist x y)
{f g : ℝ → E} {a b : ℝ}
(hf : continuous_on f (Icc a b))
(hf' : ∀ t ∈ Ico a b, has_deriv_within_at f (v t (f t)) (Ioi t) t)
(hfs : ∀ t ∈ Ico a b, f t ∈ s t)
(hg : continuous_on g (Icc a b))
(hg' : ∀ t ∈ Ico a b, has_deriv_within_at g (v t (g t)) (Ioi t) t)
(hgs : ∀ t ∈ Ico a b, g t ∈ s t)
(ha : f a = g a) :
∀ t ∈ Icc a b, f t = g t :=
begin
assume t ht,
have := dist_le_of_trajectories_ODE_of_mem_set hv hf hf' hfs hg hg' hgs
(dist_le_zero.2 ha) t ht,
rwa [zero_mul, dist_le_zero] at this
end
/-- There exists only one solution of an ODE \(\dot x=v(t, x)\) with
a given initial value provided that RHS is Lipschitz continuous in `x`. -/
theorem ODE_solution_unique {v : ℝ → E → E}
{K : nnreal} (hv : ∀ t, lipschitz_with K (v t))
{f g : ℝ → E} {a b : ℝ}
(hf : continuous_on f (Icc a b))
(hf' : ∀ t ∈ Ico a b, has_deriv_within_at f (v t (f t)) (Ioi t) t)
(hg : continuous_on g (Icc a b))
(hg' : ∀ t ∈ Ico a b, has_deriv_within_at g (v t (g t)) (Ioi t) t)
(ha : f a = g a) :
∀ t ∈ Icc a b, f t = g t :=
have hfs : ∀ t ∈ Ico a b, f t ∈ (@univ E), from λ t ht, trivial,
ODE_solution_unique_of_mem_set (λ t x y hx hy, (hv t).dist_le_mul x y)
hf hf' hfs hg hg' (λ t ht, trivial) ha
|
94c4a5b142ece52216a7fd9d011fa73e36c22b11 | 097294e9b80f0d9893ac160b9c7219aa135b51b9 | /assignments/hw8/hw8_valid_reasoning_formulae.lean | 959ea394968717f0e905576c90b4f97693aac642 | [] | no_license | AbigailCastro17/CS2102-Discrete-Math | cf296251be9418ce90206f5e66bde9163e21abf9 | d741e4d2d6a9b2e0c8380e51706218b8f608cee4 | refs/heads/main | 1,682,891,087,358 | 1,621,401,341,000 | 1,621,401,341,000 | 368,749,959 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,984 | lean | import .propositional_logic_syntax_and_semantics
open pExp
/-
Your task: We give you propositions in zero to
three variables (P, Q, and, R). You must decide
whether each is valid or not. To do this, you
must use pEval to evaluate any given propositions
under each of the possible interpretations of its
variables.
To test a 1-variable proposition will require
two interpretations; for 2 variables, four; for
three, eight, etc. In general, there are 2^n,
where n is the number of variables. Adding one
variable geometrically increases the number of
possible interpretations.
-/
/-
Here are the three propositional variables, P
Q, and R that we'll use to write propositions
and test them here.
-/
def P := pVar (var.mk 0)
def Q := pVar (var.mk 1)
def R := pVar (var.mk 2)
/-
Below is a list of formulae, many of which
express fundamental rules of valid reasoning.
We also state several fallacies: desceptively
faulty, which is to say invalid, non-rules of
logically sound reasoning.
Your job is to classify each as valid or not
valid. To do this you will produe a truth
table for each one. It is good that we have
an automatic evaluator as that makes the job
easy. For each of the formulae that's not valid,
give an English language counterexample: some
scenario that shows that the formula is
not always true.
To do this assignment, evaluate each proposition
under each of its possible interpretations. Each
interpretation defines the inputs in one row of
the truth table, and the result of applying pEval
to the expression under that interpetation, gives
you the truth value of the proposition under that
interpretation.
A given proposition will be valid if it evalutes
to true under *all* of its interpretations. You
have to define some interpretations, then apply
the pEval function to the given proposition for
each of its possible interpretation.axioms
Some of the formulae contain only one variable.
We use P. You will need two interpretations in
this cases. Call them Pt and Pf. A one-variable
propositions has two interpretations, and thus
two rows in its truth table.
Some formula have two variables. We call them
P and Q. Now there are four interpretations. Call
them PtQt, PtQf, PfQt, PfQf.
Finally, some of the propositions we give you have
three variables. Here we have eight interpretations
under which to evaluate each such proposition. You
can give them names such as PtQtRt, PtQtRf, etc.
If we look at the sequence of t's and f's in each
of these names and rewrite t's as ones f's as zeros,
then we see that our names basically count down from
2^n-1 to zero in binary notation.
PQR
ttt 111 7 2^3-1
ttf 110 6
tft 101 5
tff 100 4
ftt 011 3
ftf 010 2
fft 001 1
fff 000 0
So, for each proposition, evaluate it under each
of its possible interpretations; then look at the
list of resulting truth values. If all are true,
then you have proved that the proposition is valid.
If there is at least one interpretation under which
the proposition evaluates to true, it's decidable.
If there is no interpretation that makes it true,
then it is unsatisfiable.
-/
/- # 1.
Define three sets of interpretations, one for each
"arity" of proposition (1-, 2-, or 3 variables), as
explained above.
-/
-- Answer
/-
2. Use pEval to evaluate each of the following formulae
under each of its possible interpretations. The use the
resulting list of Boolean truth values to decide which
properties the proposition has. Here are the propositions
for which you are to decide the properties it has.
-/
def true_intro : pExp := pTrue
def false_elim := pFalse >> P
def true_imp := pTrue >> P
def and_intro := P >> Q >> (P ∧ Q)
def and_elim_left := (P ∧ Q) >> P
def and_elim_right := (P ∧ Q) >> Q
def or_intro_left := P >> (P ∨ Q)
def or_intro_right := Q >> (P ∨ Q)
def or_elim := (P ∨ Q) >> (P >> R) >> (Q >> R) >> R
def iff_intro := (P >> Q) >> (Q >> P) >> (P ↔ Q)
def iff_intro' := ((P >> Q) ∧ (Q >> P)) >> (P ↔ Q)
def iff_elim_left := (P ↔ Q) >> (P >> Q)
def iff_elim_right := (P ↔ Q) >> (Q >> P)
def arrow_elim := (P >> Q) >> (P >> Q)
def resolution := (P ∨ Q) >> (¬ Q ∨ R) >> (P ∨ R)
def unit_resolution := (P ∨ Q) >> ((¬ Q) >> P)
def syllogism := (P >> Q) >> (Q >> R) >> (P >> R)
def modus_tollens := (P >> Q) >> (¬ Q >> ¬ P)
def neg_elim := (¬ ¬ P) >> P
def excluded_middle := P ∨ (¬ P)
def neg_intro := (P >> pFalse) >> (¬ P)
def affirm_consequence := (P >> Q) >> (Q >> P)
def affirm_disjunct := (P ∨ Q) >> (P >> ¬ Q)
def deny_antecedent := (P >> Q) >> (¬ P >> ¬ Q)
/-
Study the valid rules and learn their names.
These rules, which, here, we are validating
"semantically" (by the method of truth tables)
will become fundamental "rules of inference",
for reasoning "syntactically" when we get to
predicate logic. There is not much memorizing
in this class, but this is one case where you
will find it important to learn the names and
definitions of these rules.
-/ |
5c5a7d63e8a9f5656cc15adc4ff6058a820b2352 | 76ce87faa6bc3c2aa9af5962009e01e04f2a074a | /07_Disjunction/00_intro.lean | 6ce07cb46acd99aaa656f47136f32ce56514459a | [] | no_license | Mnormansell/Discrete-Notes | db423dd9206bbe7080aecb84b4c2d275b758af97 | 61f13b98be590269fc4822be7b47924a6ddc1261 | refs/heads/master | 1,585,412,435,424 | 1,540,919,483,000 | 1,540,919,483,000 | 148,684,638 | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 9,124 | lean | /-
If P and Q are propositions, then
P ∨ Q is also a proposition. It is
read as "P or Q" and is called a
disjunction. P and Q are called the
disjuncts of the disjunction P ∨ Q.
In constructive logic, one can prove
P ∨ Q from either a proof of P or a
proof of Q. This unit presents and
discusses the basic introduction and
elimination rules for or.
-/
/-
We start by making some assumptions
that we use in the rest of this unit.
-/
variables P Q R X Y Z: Prop
variable pfP : P
variable pfQ : Q
variable pfR : R
/-
**************************
*** INTRODUCTION RULES ***
**************************
-/
/-
In constructive logic, P ∨ Q is
true if either a proof of P or
a proof of Q can be given. Having
a proof of both of course allows
one to give either proof to prove
P ∨ Q.
Here are the introduction rules
written as inference rules.
{ P } Q : Prop, pfP: P
---------------------- (or.intro_left)
P ∨ Q
Notice in the preceding rule that while
P can be inferred from pfP, Q has to be
given explicitly, because otherwise there
is no way to know what it is.
P { Q } : Prop, pfQ: Q
----------------------- (or.intro_right)
P ∨ Q
Now P has to be given explicitly, while
Q can be inferred.
-/
/-
The or.intro_left rule takes
propositions P and Q (P implicitly)
constructs a proof of P ∨ Q. No proof
of Q is needed. P need not be given
explicitly because it can be inferred
from the proof of P; however, because
no proof of Q is given, from which Q
could be inferred, Q must be given
explicitly.
The or.intro_right rule takes
a proof of Q explicitly along with a
proposition P, from which P is inferred,
and constructs a proof of P ∨ Q. No proof
of P is required, but the proposition P
has to be given explicitly, as there's
no proof to infer it from.
-/
/-
To prove a disjunction, such as
0 = 0 ∨ 0 = 1, we have to pick which
side of the disjunction we will give
a proof for in order to apply or.intro.
Here's an example. We pick the side
to prove that actually has a proof.
There is no proof of the right side.
-/
theorem goodSide : 0 = 0 ∨ 0 = 1 :=
or.intro_left (0=1) (eq.refl 0)
---------------- Q ---pfP
/-
Recall that we can use "example" in
Lean to state a theorem without giving
its proof a name.
Here we use example to state and prove
that for any proposition, P, P or false
is equivalent to P. (Remember that we
assumed above that P is defined to be a
Prop, and pfP is defined to be a proof
of P.)
-/
example : P ∨ false :=
or.intro_left false pfP
example : false ∨ Q :=
or.intro_right false pfQ
/-
EXERCISE: Prove 0 = 1 ∨ 0 = 0.
-/
theorem xyz : 0 = 1 ∨ 0 = 0 :=
or.intro_right (0 = 1) rfl
/-
Here's a proof that P or true
is always true. The key idea is
that we can always choose to give
a proof for the true side, which
is trivial.
-/
theorem zero_right_or :
∀ P : Prop, P ∨ true :=
λ P,
or.intro_right P true.intro
/-
EXERCISE: Prove that true ∨ Q is
always true as well.
-/
/-
**************************
**** Elimination RULE ****
**************************
-/
/-
The or.elim rule gives us an indirect way
to prove a proposition, R, (the goal) by
showing first that at least one of two
conditions holds (P ∨ Q), and in either
case W must be true, because both (P → R)
and (Q → R). If P ∨ Q, then if both P → R
and Q → R, then R.
Here's the inference rule in plain text.
pfPQ: P ∨ Q, pfPR: P → R, pfQR: Q → R
-------------------------------------- or.elim
R
-/
/-
As an example, suppose that (1) when it
is raining (R) the grass is wet (R → W); (2)
when the sprinkler (S) is on, the grass
is also wet (S → W); and it is raining *or*
the sprinkler is on (R ∨ S). Then the grass
must be wet (W).
Going in the other direction, if our aim
is to prove W, we can do it using or.elim
by showing that for some propositions, R
and S, that R ∨ S is true, and that *in
either case*, W must also be true.
The reasoning is by considering each case,
each possible way to have proven R ∨ S,
separately. R ∨ S means that at least one
of R and S is true, and for whichever one
is true, there must be a proof.
We first consider a case where R is true.
Then we consider the case where S is true.
In the case where R is true, we show that
W follows from R → W. In case where S is
true, we show that W follows from S → W.
W holds in either case and so W holds.
-/
/-
Now let's see it working in Lean. We
make a few basic assumptions and then
show how to combine them using the or
elimination rule. We assume ...
1. a proof of P ∨ Q
2. a proof of P → R
3. a proof of Q → R
-/
variable porq : P ∨ Q
variable p2r : P → R
variable q2r : Q → R
/-
Now we derive and check a proof of R
by applying or.elim to these terms..
-/
#check or.elim porq p2r q2r
/-
Here's the same example using more
suggestive names for propositions.
-/
-- Raining, Sprinkling, Wet are Props
variables Raining Sprinkling Wet : Prop
-- rors proves it's Raining ∨ Sprinkling.
variable rors: Raining ∨ Sprinkling
-- r2w proves if Raining then Wet
variable r2w: Raining → Wet
-- s2w proves if Sprinkling then Wet
variable s2w: Sprinkling → Wet
-- Put it all together to show Wet
theorem wet : Wet :=
or.elim rors r2w s2w
/-
The following program make the arguments to
and the result of or.elim clear, and it
gives an example of the use of or.elim.
-/
theorem orElim :
∀ R S W: Prop,
(R ∨ S) → (R → W) → (S → W) → W
:=
begin
assume R S W rors r2w s2w,
show W,
from or.elim rors r2w s2w
end
/-
This example constructs the same
proof but illustrates how we can
apply inference rules, leaving out
some arguments, to be filled in, in
the style of backward reasoning.
-/
theorem orElim' :
∀ R S W: Prop,
(R ∨ S) → (R → W) → (S → W) → W :=
begin
assume R S W rors r2w s2w,
-- apply or.elim
apply or.elim rors,
-- reduce goal to two subgoals
-- one for each required implication
exact r2w,
exact s2w
end
/-
Notice the subtle difference between using
or.elim and cases. The or.elim rule takes
the disjunction and the two implications as
arguments. The cases tactic, on the other
hand, sets you up to apply one or the other
implication depending on the case being
considered.
-/
theorem wet''' :
∀ R S W: Prop,
(R ∨ S) → (R → W) → (S → W) → W :=
begin
assume R S W r_or_s r2w s2w,
/-
We want to show that if W follows from R
and if W follows from S, then W follows
R ∨ S.
-/
show W, from
begin
/-
What we need to show is that W follows
from R ∨ S, whether because R is true and
W follows from R or because S is true and
W follows from S. We consider each case
in turn.
-/
cases r_or_s with r s,
-- W follows from (r : R) and r2w
show W, from r2w r,
-- W follows from (r : R) and s2w
show W, from s2w s,
-- Thus W follows.
end
-- QED
end
/-
We recommend approaching proofs from
disjunctions, as here, using the cases
tactic. It clearly shows that what is
happening is a case analysis. That if
the disjunction is true, then one way
or another we can reach the desired
conclusion.
-/
/-
Here's a proof that false is a right
identity for disjunction. That's a fancy
way of saying that P ∨ false is true if
and only if P is true. They're equivalent
propositions.
-/
theorem id_right_or :
∀ P : Prop, P ∨ false ↔ P
:=
λ P,
begin
-- consider each direction separately
apply iff.intro,
-- note: missing args become subgoals
-- Forward direction: P ∨ false → P
-- assume a proof of P ∨ false
assume pfPorfalse,
-- show P by case analysis on this disjunction
cases pfPorfalse with pfP pfFalse,
-- case where we have a proof of P
assumption,
-- case with proof of false
exact false.elim pfFalse,
-- Reverse direction, easy
apply or.intro_left
end
/-
Another example. The proof of another
standard rule of reasoning in natural
deduction.
-/
theorem disjunctiveSyllogism :
∀ P Q : Prop, P ∨ Q → ¬Q → P :=
λ p q pq nq,
begin
cases pq with p q,
assumption,
show p,
from false.elim (nq q),
end
/-
Exercise: Prove that false is also a
*left* identity for disjunction. That
is: false ∨ P ↔ P (false is on the left).
-/
theorem aDemorganLaw :
∀ P Q: Prop, ¬ P ∧ ¬ Q ↔ ¬ (P ∨ Q) :=
begin
assume P Q: Prop,
apply iff.intro,
--forward
begin
assume npnq : P ∧ ¬ Q,
show ¬ (P ∨ Q),
from
begin
assume pq : (P ∨ Q),
cases pq with p q,
show false,
from npnq.1 p,
show false,
from npnq.2 q,
end,
--backwards
assume p : P,
begin
have pq : P ∨ Q,
from
begin
apply or.intro_left,
show P,
from p
end,
show false,
from
end
|
aa4c987791e23220d548641540bef12ab50de119 | c3f2fcd060adfa2ca29f924839d2d925e8f2c685 | /library/algebra/category/functor.lean | 10f29442d03e5f380eda042f6008c7a3cfce7068 | [
"Apache-2.0"
] | permissive | respu/lean | 6582d19a2f2838a28ecd2b3c6f81c32d07b5341d | 8c76419c60b63d0d9f7bc04ebb0b99812d0ec654 | refs/heads/master | 1,610,882,451,231 | 1,427,747,084,000 | 1,427,747,429,000 | null | 0 | 0 | null | null | null | null | UTF-8 | Lean | false | false | 4,195 | lean | /-
Copyright (c) 2014 Floris van Doorn. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Module: algebra.category.functor
Author: Floris van Doorn
-/
import .basic
import logic.cast
open function
open category eq eq.ops heq
structure functor (C D : Category) : Type :=
(object : C → D)
(morphism : Π⦃a b : C⦄, hom a b → hom (object a) (object b))
(respect_id : Π (a : C), morphism (ID a) = ID (object a))
(respect_comp : Π ⦃a b c : C⦄ (g : hom b c) (f : hom a b),
morphism (g ∘ f) = morphism g ∘ morphism f)
infixl `⇒`:25 := functor
namespace functor
attribute object [coercion]
attribute morphism [coercion]
attribute respect_id [irreducible]
attribute respect_comp [irreducible]
variables {A B C D : Category}
protected definition compose [reducible] (G : functor B C) (F : functor A B) : functor A C :=
functor.mk
(λx, G (F x))
(λ a b f, G (F f))
(λ a, proof calc
G (F (ID a)) = G id : {respect_id F a}
--not giving the braces explicitly makes the elaborator compute a couple more seconds
... = id : respect_id G (F a) qed)
(λ a b c g f, proof calc
G (F (g ∘ f)) = G (F g ∘ F f) : respect_comp F g f
... = G (F g) ∘ G (F f) : respect_comp G (F g) (F f) qed)
infixr `∘f`:60 := compose
protected theorem assoc (H : functor C D) (G : functor B C) (F : functor A B) :
H ∘f (G ∘f F) = (H ∘f G) ∘f F :=
rfl
protected definition id [reducible] {C : Category} : functor C C :=
mk (λa, a) (λ a b f, f) (λ a, rfl) (λ a b c f g, rfl)
protected definition ID [reducible] (C : Category) : functor C C := id
protected theorem id_left (F : functor C D) : id ∘f F = F :=
functor.rec (λ obF homF idF compF, dcongr_arg4 mk rfl rfl !proof_irrel !proof_irrel) F
protected theorem id_right (F : functor C D) : F ∘f id = F :=
functor.rec (λ obF homF idF compF, dcongr_arg4 mk rfl rfl !proof_irrel !proof_irrel) F
end functor
namespace category
open functor
definition category_of_categories [reducible] : category Category :=
mk (λ a b, functor a b)
(λ a b c g f, functor.compose g f)
(λ a, functor.id)
(λ a b c d h g f, !functor.assoc)
(λ a b f, !functor.id_left)
(λ a b f, !functor.id_right)
definition Category_of_categories [reducible] := Mk category_of_categories
namespace ops
notation `Cat`:max := Category_of_categories
attribute category_of_categories [instance]
end ops
end category
namespace functor
variables {C D : Category}
theorem mk_heq {obF obG : C → D} {homF homG idF idG compF compG} (Hob : ∀x, obF x = obG x)
(Hmor : ∀(a b : C) (f : a ⟶ b), homF a b f == homG a b f)
: mk obF homF idF compF = mk obG homG idG compG :=
hddcongr_arg4 mk
(funext Hob)
(hfunext (λ a, hfunext (λ b, hfunext (λ f, !Hmor))))
!proof_irrel
!proof_irrel
protected theorem hequal {F G : C ⇒ D} : Π (Hob : ∀x, F x = G x)
(Hmor : ∀a b (f : a ⟶ b), F f == G f), F = G :=
functor.rec
(λ obF homF idF compF,
functor.rec
(λ obG homG idG compG Hob Hmor, mk_heq Hob Hmor)
G)
F
-- theorem mk_eq {obF obG : C → D} {homF homG idF idG compF compG} (Hob : ∀x, obF x = obG x)
-- (Hmor : ∀(a b : C) (f : a ⟶ b), cast (congr_arg (λ x, x a ⟶ x b) (funext Hob)) (homF a b f)
-- = homG a b f)
-- : mk obF homF idF compF = mk obG homG idG compG :=
-- dcongr_arg4 mk
-- (funext Hob)
-- (funext (λ a, funext (λ b, funext (λ f, sorry ⬝ Hmor a b f))))
-- -- to fill this sorry use (a generalization of) cast_pull
-- !proof_irrel
-- !proof_irrel
-- protected theorem equal {F G : C ⇒ D} : Π (Hob : ∀x, F x = G x)
-- (Hmor : ∀a b (f : a ⟶ b), cast (congr_arg (λ x, x a ⟶ x b) (funext Hob)) (F f) = G f), F = G :=
-- functor.rec
-- (λ obF homF idF compF,
-- functor.rec
-- (λ obG homG idG compG Hob Hmor, mk_eq Hob Hmor)
-- G)
-- F
end functor
|
cf82b04834a85cf8ac6502d671a1d55ef177c47e | 2a70b774d16dbdf5a533432ee0ebab6838df0948 | /src/C_S_variant_JvdD.lean | 40c344e5736eda189ac6f286b7ef3a6fae7d7b02 | [] | no_license | 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 | 2,308 | lean | /-
Copyright (c) 2021 Huub Vromen. All rights reserved.
Author: Huub Vromen
-/
/- outline of Lewis' theory of common knowledge (formalisation according to
the proposal of Jaap van der Does) -/
-- type of individuals in the population P
variable {indiv : Type}
variables {i j: indiv}
-- states of affairs and propositions are of the same type
variables {p ψ χ : Prop}
-- Reason-to-believe is a two-place relation between individuals and propositions
constant R : indiv → Prop → Prop
-- Indication is a three-place relation between individuals and two propositions
constant Ind : indiv → Prop → Prop → Prop
/-- The following axioms represent that state of affairs α is a basis for common
knowledge of proposition φ -/
constants {α φ: Prop}
axiom CK1 : R i α
axiom CK2 : Ind i α (R j α)
axiom CK3 : Ind i α φ
/-- `Indicative modus ponens represents the meaning of indication.
This axiom corresponds to axiom A1 in Cubitt and Sugden's account. -/
axiom I_MP : Ind i ψ χ → R i ψ → R i χ
/-- `I-introspection` represents that all individuals in P share inductive
standards and background knowledge. This axiom is a variant of axiom C4
in Cubitt and Sugden's account -/
axiom I_introspection : Ind i α ψ → Ind i α (Ind j α ψ)
/-- `LR` (Lewis' Rule) is the assumption that the population allows for introspection
with regard to using I-MP. This axiom is a variant of axiom A6 in
Cubitt and Sugden's account -/
axiom LR : (Ind i α (R j α) ∧ Ind i α (Ind j α ψ) → Ind i α (R j ψ))
/-- Now we can prove Lewis' theorem. First, we inductively define G (`generated
by φ), the property of being a proposition `p` for which we have to prove
that `R i p` holds. -/
inductive G : Prop → Prop
| base : G φ
| step (p : Prop) {i : indiv} : G p → G (R i p)
theorem Lewis (p : Prop) (hG : @G indiv p) : ∀i : indiv, R i p :=
begin
intro i,
have h1 : Ind i α p :=
begin
induction hG with u j hu ih,
{ exact CK3 },
{ have h2 : Ind i α (Ind j α u) := I_introspection ih,
have h3 : Ind i α (R j u) := LR (and.intro CK2 h2),
assumption }
end,
exact I_MP h1 CK1
end
/- Both I-MP and LR can be derived as lemmas in the theory of reasoning with
reasons. -/
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.